blob: d3243dd77a3c77cfa21ae04486e9110b55539fb6 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Raymond Hettinger2212e522008-11-30 23:43:36 +00006 Copyright (c) 2003-2008 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000012#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
26}
27
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028/* This must be >= 1. */
29#define PERTURB_SHIFT 5
30
31/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000032static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#ifdef Py_REF_DEBUG
35PyObject *
36_PySet_Dummy(void)
37{
38 return dummy;
39}
40#endif
41
Raymond Hettingerbc841a12005-08-07 13:02:53 +000042#define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
45 (so)->hash = -1; \
46 } while(0)
47
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000048#define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000051 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052 } while(0)
53
Raymond Hettingerbc841a12005-08-07 13:02:53 +000054/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000055#ifndef PySet_MAXFREELIST
56#define PySet_MAXFREELIST 80
57#endif
58static PySetObject *free_list[PySet_MAXFREELIST];
59static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060
Christian Heimes0ded5b52007-12-10 15:50:56 +000061
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062/*
63The basic lookup function used by all operations.
64This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65Open addressing is preferred over chaining since the link overhead for
66chaining would be substantial (100% with typical malloc overhead).
67
68The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000069probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070
71All arithmetic on hash should ignore overflow.
72
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000073Unlike the dictionary implementation, the lookkey functions can return
74NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075*/
76
77static setentry *
78set_lookkey(PySetObject *so, PyObject *key, register long hash)
79{
Martin v. Löwis18e16552006-02-15 17:27:45 +000080 register Py_ssize_t i;
81 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000084 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000085 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000087 PyObject *startkey;
88
89 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000090 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000091 if (entry->key == NULL || entry->key == key)
92 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 if (entry->key == dummy)
95 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000096 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000097 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000098 startkey = entry->key;
Georg Brandlf08a9dd2008-06-10 16:57:31 +000099 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000101 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000102 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000103 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000104 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000105 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000106 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000107 }
108 else {
109 /* The compare did major nasty stuff to the
110 * set: start over.
111 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000112 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000113 }
114 }
115 freeslot = NULL;
116 }
117
118 /* In the loop, key == dummy is by far (factor of 100s) the
119 least likely outcome, so test for that last. */
120 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
121 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000122 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000123 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 break;
127 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000128 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000129 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000130 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000131 startkey = entry->key;
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000132 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000134 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000135 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000136 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000137 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138 if (cmp > 0)
139 break;
140 }
141 else {
142 /* The compare did major nasty stuff to the
143 * set: start over.
144 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000145 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146 }
147 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000148 else if (entry->key == dummy && freeslot == NULL)
149 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000151 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152}
153
154/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000155 * Hacked up version of set_lookkey which can assume keys are always unicode;
156 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000157 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158 */
159static setentry *
Christian Heimes0ded5b52007-12-10 15:50:56 +0000160set_lookkey_unicode(PySetObject *so, PyObject *key, register long hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000161{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000162 register Py_ssize_t i;
163 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000164 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000165 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000166 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000167 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000168
Christian Heimes0ded5b52007-12-10 15:50:56 +0000169 /* Make sure this function doesn't have to handle non-unicode keys,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000170 including subclasses of str; e.g., one reason to subclass
171 strings is to override __eq__, and for speed we don't cater to
172 that here. */
Christian Heimes0ded5b52007-12-10 15:50:56 +0000173 if (!PyUnicode_CheckExact(key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000174 so->lookup = set_lookkey;
175 return set_lookkey(so, key, hash);
176 }
177 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000178 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000179 if (entry->key == NULL || entry->key == key)
180 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000181 if (entry->key == dummy)
182 freeslot = entry;
183 else {
Christian Heimes0ded5b52007-12-10 15:50:56 +0000184 if (entry->hash == hash && unicode_eq(entry->key, key))
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000185 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000186 freeslot = NULL;
187 }
188
189 /* In the loop, key == dummy is by far (factor of 100s) the
190 least likely outcome, so test for that last. */
191 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
192 i = (i << 2) + i + perturb + 1;
193 entry = &table[i & mask];
194 if (entry->key == NULL)
195 return freeslot == NULL ? entry : freeslot;
196 if (entry->key == key
197 || (entry->hash == hash
198 && entry->key != dummy
Christian Heimes0ded5b52007-12-10 15:50:56 +0000199 && unicode_eq(entry->key, key)))
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000200 return entry;
201 if (entry->key == dummy && freeslot == NULL)
202 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000203 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204 assert(0); /* NOT REACHED */
205 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206}
207
208/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211Eats a reference to key.
212*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000214set_insert_key(register PySetObject *so, PyObject *key, long hash)
215{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000216 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
218
219 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000221 if (entry == NULL)
222 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000224 /* UNUSED */
225 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000226 entry->key = key;
227 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000228 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000229 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000230 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000231 entry->key = key;
232 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000233 so->used++;
234 Py_DECREF(dummy);
235 } else {
236 /* ACTIVE */
237 Py_DECREF(key);
238 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000239 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000240}
241
242/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000243Internal routine used by set_table_resize() to insert an item which is
244known to be absent from the set. This routine also assumes that
245the set contains no deleted entries. Besides the performance benefit,
246using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
247Note that no refcounts are changed by this routine; if needed, the caller
248is responsible for incref'ing `key`.
249*/
250static void
251set_insert_clean(register PySetObject *so, PyObject *key, long hash)
252{
253 register size_t i;
254 register size_t perturb;
255 register size_t mask = (size_t)so->mask;
256 setentry *table = so->table;
257 register setentry *entry;
258
259 i = hash & mask;
260 entry = &table[i];
261 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
262 i = (i << 2) + i + perturb + 1;
263 entry = &table[i & mask];
264 }
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
269}
270
271/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000273keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274actually be smaller than the old one.
275*/
276static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000280 setentry *oldtable, *newtable, *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000281 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE];
284
285 assert(minused >= 0);
286
287 /* Find the smallest table size > minused. */
288 for (newsize = PySet_MINSIZE;
289 newsize <= minused && newsize > 0;
290 newsize <<= 1)
291 ;
292 if (newsize <= 0) {
293 PyErr_NoMemory();
294 return -1;
295 }
296
297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL);
300 is_oldtable_malloced = oldtable != so->smalltable;
301
302 if (newsize == PySet_MINSIZE) {
303 /* A large table is shrinking, or we can't get any smaller. */
304 newtable = so->smalltable;
305 if (newtable == oldtable) {
306 if (so->fill == so->used) {
307 /* No dummies, so no point doing anything. */
308 return 0;
309 }
310 /* We're not going to resize it, but rebuild the
311 table anyway to purge old dummy entries.
312 Subtle: This is *necessary* if fill==size,
313 as set_lookkey needs at least one virgin slot to
314 terminate failing searches. If fill < size, it's
315 merely desirable, as dummies slow searches. */
316 assert(so->fill > so->used);
317 memcpy(small_copy, oldtable, sizeof(small_copy));
318 oldtable = small_copy;
319 }
320 }
321 else {
322 newtable = PyMem_NEW(setentry, newsize);
323 if (newtable == NULL) {
324 PyErr_NoMemory();
325 return -1;
326 }
327 }
328
329 /* Make the set empty, using the new table. */
330 assert(newtable != oldtable);
331 so->table = newtable;
332 so->mask = newsize - 1;
333 memset(newtable, 0, sizeof(setentry) * newsize);
334 so->used = 0;
335 i = so->fill;
336 so->fill = 0;
337
338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000340 for (entry = oldtable; i > 0; entry++) {
341 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000342 /* UNUSED */
343 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000344 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345 /* DUMMY */
346 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000347 assert(entry->key == dummy);
348 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000349 } else {
350 /* ACTIVE */
351 --i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000352 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000353 }
354 }
355
356 if (is_oldtable_malloced)
357 PyMem_DEL(oldtable);
358 return 0;
359}
360
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
362
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000364set_add_entry(register PySetObject *so, setentry *entry)
365{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000366 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000367
368 assert(so->fill <= so->mask); /* at least one empty slot */
369 n_used = so->used;
370 Py_INCREF(entry->key);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000371 if (set_insert_key(so, entry->key, entry->hash) == -1) {
372 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000373 return -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000374 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000375 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
376 return 0;
377 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
378}
379
380static int
381set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382{
383 register long hash;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000384 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000385
Christian Heimes0ded5b52007-12-10 15:50:56 +0000386 if (!PyUnicode_CheckExact(key) ||
387 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000388 hash = PyObject_Hash(key);
389 if (hash == -1)
390 return -1;
391 }
392 assert(so->fill <= so->mask); /* at least one empty slot */
393 n_used = so->used;
394 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000395 if (set_insert_key(so, key, hash) == -1) {
396 Py_DECREF(key);
397 return -1;
398 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000399 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
400 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000401 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402}
403
404#define DISCARD_NOTFOUND 0
405#define DISCARD_FOUND 1
406
407static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000408set_discard_entry(PySetObject *so, setentry *oldentry)
409{ register setentry *entry;
410 PyObject *old_key;
411
412 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000413 if (entry == NULL)
414 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000415 if (entry->key == NULL || entry->key == dummy)
416 return DISCARD_NOTFOUND;
417 old_key = entry->key;
418 Py_INCREF(dummy);
419 entry->key = dummy;
420 so->used--;
421 Py_DECREF(old_key);
422 return DISCARD_FOUND;
423}
424
425static int
426set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000427{
428 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000429 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000430 PyObject *old_key;
431
432 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000433
434 if (!PyUnicode_CheckExact(key) ||
435 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000436 hash = PyObject_Hash(key);
437 if (hash == -1)
438 return -1;
439 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000440 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000441 if (entry == NULL)
442 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000443 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000444 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000445 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000446 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000447 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448 so->used--;
449 Py_DECREF(old_key);
450 return DISCARD_FOUND;
451}
452
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000453static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454set_clear_internal(PySetObject *so)
455{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000456 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457 int table_is_malloced;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000458 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000459 setentry small_copy[PySet_MINSIZE];
460#ifdef Py_DEBUG
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000461 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000463
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464 n = so->mask + 1;
465 i = 0;
466#endif
467
468 table = so->table;
469 assert(table != NULL);
470 table_is_malloced = table != so->smalltable;
471
472 /* This is delicate. During the process of clearing the set,
473 * decrefs can cause the set to mutate. To avoid fatal confusion
474 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000475 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476 * clearing.
477 */
478 fill = so->fill;
479 if (table_is_malloced)
480 EMPTY_TO_MINSIZE(so);
481
482 else if (fill > 0) {
483 /* It's a small table with something that needs to be cleared.
484 * Afraid the only safe way is to copy the set entries into
485 * another small table first.
486 */
487 memcpy(small_copy, table, sizeof(small_copy));
488 table = small_copy;
489 EMPTY_TO_MINSIZE(so);
490 }
491 /* else it's a small table that's already empty */
492
493 /* Now we can finally clear things. If C had refcounts, we could
494 * assert that the refcount on table is 1 now, i.e. that this function
495 * has unique access to it, so decref side-effects can't alter it.
496 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000497 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498#ifdef Py_DEBUG
499 assert(i < n);
500 ++i;
501#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000502 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000504 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505 }
506#ifdef Py_DEBUG
507 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509#endif
510 }
511
512 if (table_is_malloced)
513 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000514 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515}
516
517/*
518 * Iterate over a set table. Use like so:
519 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000520 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000521 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000522 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000523 * while (set_next(yourset, &pos, &entry)) {
524 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525 * }
526 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000527 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000528 * mutates the table.
529 */
530static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000531set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000533 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000534 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536
537 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000538 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000539 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000540 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000541 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000542 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000544 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545 if (i > mask)
546 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000547 assert(table[i].key != NULL);
548 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000549 return 1;
550}
551
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000552static void
553set_dealloc(PySetObject *so)
554{
555 register setentry *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000556 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000557 PyObject_GC_UnTrack(so);
558 Py_TRASHCAN_SAFE_BEGIN(so)
559 if (so->weakreflist != NULL)
560 PyObject_ClearWeakRefs((PyObject *) so);
561
562 for (entry = so->table; fill > 0; entry++) {
563 if (entry->key) {
564 --fill;
565 Py_DECREF(entry->key);
566 }
567 }
568 if (so->table != so->smalltable)
569 PyMem_DEL(so->table);
Christian Heimes2202f872008-02-06 14:31:34 +0000570 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
571 free_list[numfree++] = so;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000572 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000573 Py_TYPE(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000574 Py_TRASHCAN_SAFE_END(so)
575}
576
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000577static PyObject *
578set_repr(PySetObject *so)
579{
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000580 PyObject *keys, *result=NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000581 Py_UNICODE *u;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000582 int status = Py_ReprEnter((PyObject*)so);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000583 PyObject *listrepr;
584 Py_ssize_t newsize;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000585
586 if (status != 0) {
587 if (status < 0)
588 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000589 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000590 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591
Georg Brandlc4996ba2006-08-28 19:37:11 +0000592 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000593 if (!so->used) {
594 Py_ReprLeave((PyObject*)so);
Christian Heimes90aa7642007-12-19 02:45:37 +0000595 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000596 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000597
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000598 keys = PySequence_List((PyObject *)so);
599 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000600 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000602 listrepr = PyObject_Repr(keys);
603 Py_DECREF(keys);
604 if (listrepr == NULL) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000605 Py_DECREF(keys);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000606 goto done;
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000607 }
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000608 newsize = PyUnicode_GET_SIZE(listrepr);
609 result = PyUnicode_FromUnicode(NULL, newsize);
610 if (result) {
611 u = PyUnicode_AS_UNICODE(result);
612 *u++ = '{';
613 /* Omit the brackets from the listrepr */
614 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
615 PyUnicode_GET_SIZE(listrepr)-2);
616 u += newsize-2;
617 *u++ = '}';
618 }
619 Py_DECREF(listrepr);
Christian Heimes90aa7642007-12-19 02:45:37 +0000620 if (Py_TYPE(so) != &PySet_Type) {
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000621 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000622 Py_TYPE(so)->tp_name,
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000623 result);
624 Py_DECREF(result);
625 result = tmp;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000626 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000627done:
628 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000629 return result;
630}
631
Martin v. Löwis18e16552006-02-15 17:27:45 +0000632static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000633set_len(PyObject *so)
634{
635 return ((PySetObject *)so)->used;
636}
637
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000638static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000639set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000640{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000641 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000643 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000644
645 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000646 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000647
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000648 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000649 if (other == so || other->used == 0)
650 /* a.update(a) or a.update({}); nothing to do */
651 return 0;
652 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000653 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000654 * that there will be no (or few) overlapping keys.
655 */
656 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
657 if (set_table_resize(so, (so->used + other->used)*2) != 0)
658 return -1;
659 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000660 for (i = 0; i <= other->mask; i++) {
661 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000662 if (entry->key != NULL &&
663 entry->key != dummy) {
664 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000665 if (set_insert_key(so, entry->key, entry->hash) == -1) {
666 Py_DECREF(entry->key);
667 return -1;
668 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000669 }
670 }
671 return 0;
672}
673
674static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000675set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000676{
677 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000678 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000679
Christian Heimes0ded5b52007-12-10 15:50:56 +0000680 if (!PyUnicode_CheckExact(key) ||
681 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000682 hash = PyObject_Hash(key);
683 if (hash == -1)
684 return -1;
685 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000686 entry = (so->lookup)(so, key, hash);
687 if (entry == NULL)
688 return -1;
689 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000690 return key != NULL && key != dummy;
691}
692
Raymond Hettingerc991db22005-08-11 07:58:45 +0000693static int
694set_contains_entry(PySetObject *so, setentry *entry)
695{
696 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000697 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000698
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000699 lu_entry = (so->lookup)(so, entry->key, entry->hash);
700 if (lu_entry == NULL)
701 return -1;
702 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000703 return key != NULL && key != dummy;
704}
705
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000706static PyObject *
707set_pop(PySetObject *so)
708{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000709 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000710 register setentry *entry;
711 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000712
713 assert (PyAnySet_Check(so));
714 if (so->used == 0) {
715 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
716 return NULL;
717 }
718
719 /* Set entry to "the first" unused or dummy set entry. We abuse
720 * the hash field of slot 0 to hold a search finger:
721 * If slot 0 has a value, use slot 0.
722 * Else slot 0 is being used to hold a search finger,
723 * and we use its hash value as the first index to look.
724 */
725 entry = &so->table[0];
726 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000727 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000728 /* The hash field may be a real hash value, or it may be a
729 * legit search finger, or it may be a once-legit search
730 * finger that's out of bounds now because it wrapped around
731 * or the table shrunk -- simply make sure it's in bounds now.
732 */
733 if (i > so->mask || i < 1)
734 i = 1; /* skip slot 0 */
735 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
736 i++;
737 if (i > so->mask)
738 i = 1;
739 }
740 }
741 key = entry->key;
742 Py_INCREF(dummy);
743 entry->key = dummy;
744 so->used--;
745 so->table[0].hash = i + 1; /* next place to start */
746 return key;
747}
748
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000749PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
750Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000751
752static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000753set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000754{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000755 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000756 setentry *entry;
757
758 while (set_next(so, &pos, &entry))
759 Py_VISIT(entry->key);
760 return 0;
761}
762
763static long
764frozenset_hash(PyObject *self)
765{
766 PySetObject *so = (PySetObject *)self;
767 long h, hash = 1927868237L;
768 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000769 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000770
771 if (so->hash != -1)
772 return so->hash;
773
774 hash *= PySet_GET_SIZE(self) + 1;
775 while (set_next(so, &pos, &entry)) {
776 /* Work to increase the bit dispersion for closely spaced hash
777 values. The is important because some use cases have many
778 combinations of a small number of elements with nearby
779 hashes so that many distinct combinations collapse to only
780 a handful of distinct hash values. */
781 h = entry->hash;
782 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
783 }
784 hash = hash * 69069L + 907133923L;
785 if (hash == -1)
786 hash = 590923713L;
787 so->hash = hash;
788 return hash;
789}
790
Raymond Hettingera9d99362005-08-05 00:01:15 +0000791/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793typedef struct {
794 PyObject_HEAD
795 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000796 Py_ssize_t si_used;
797 Py_ssize_t si_pos;
798 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799} setiterobject;
800
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801static void
802setiter_dealloc(setiterobject *si)
803{
804 Py_XDECREF(si->si_set);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000805 PyObject_GC_Del(si);
806}
807
808static int
809setiter_traverse(setiterobject *si, visitproc visit, void *arg)
810{
811 Py_VISIT(si->si_set);
812 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813}
814
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000815static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000816setiter_len(setiterobject *si)
817{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000818 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000819 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000820 len = si->len;
Christian Heimes217cfd12007-12-02 14:31:20 +0000821 return PyLong_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822}
823
Armin Rigof5b3e362006-02-11 21:32:43 +0000824PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000825
826static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000827 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000828 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829};
830
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000831static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832{
833 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000834 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000835 register setentry *entry;
836 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000838 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000840 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000841
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000842 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000843 PyErr_SetString(PyExc_RuntimeError,
844 "Set changed size during iteration");
845 si->si_used = -1; /* Make this state sticky */
846 return NULL;
847 }
848
849 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000850 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000851 entry = so->table;
852 mask = so->mask;
853 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000854 i++;
855 si->si_pos = i+1;
856 if (i > mask)
857 goto fail;
858 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000859 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000860 Py_INCREF(key);
861 return key;
862
863fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000864 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000865 si->si_set = NULL;
866 return NULL;
867}
868
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000869PyTypeObject PySetIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000870 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +0000871 "set_iterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872 sizeof(setiterobject), /* tp_basicsize */
873 0, /* tp_itemsize */
874 /* methods */
875 (destructor)setiter_dealloc, /* tp_dealloc */
876 0, /* tp_print */
877 0, /* tp_getattr */
878 0, /* tp_setattr */
879 0, /* tp_compare */
880 0, /* tp_repr */
881 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000882 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000883 0, /* tp_as_mapping */
884 0, /* tp_hash */
885 0, /* tp_call */
886 0, /* tp_str */
887 PyObject_GenericGetAttr, /* tp_getattro */
888 0, /* tp_setattro */
889 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000890 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000892 (traverseproc)setiter_traverse, /* tp_traverse */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000893 0, /* tp_clear */
894 0, /* tp_richcompare */
895 0, /* tp_weaklistoffset */
896 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000897 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000898 setiter_methods, /* tp_methods */
899 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000900};
901
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000902static PyObject *
903set_iter(PySetObject *so)
904{
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000905 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000906 if (si == NULL)
907 return NULL;
908 Py_INCREF(so);
909 si->si_set = so;
910 si->si_used = so->used;
911 si->si_pos = 0;
912 si->len = so->used;
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000913 _PyObject_GC_TRACK(si);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000914 return (PyObject *)si;
915}
916
Raymond Hettingerd7946662005-08-01 21:39:29 +0000917static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000918set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000919{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000920 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000921
Christian Heimesaf98da12008-01-27 15:18:18 +0000922 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000923 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000924
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000925 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000926 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000927 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000928 long hash;
929 Py_ssize_t dictsize = PyDict_Size(other);
930
931 /* Do one big resize at the start, rather than
932 * incrementally resizing as we insert new keys. Expect
933 * that there will be no (or few) overlapping keys.
934 */
935 if (dictsize == -1)
936 return -1;
937 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
938 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
939 return -1;
940 }
941 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
942 setentry an_entry;
943
944 an_entry.hash = hash;
945 an_entry.key = key;
946 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000947 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000948 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000950 }
951
Raymond Hettingera38123e2003-11-24 22:18:49 +0000952 it = PyObject_GetIter(other);
953 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000954 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000955
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000956 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000957 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000958 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000959 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000960 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000961 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000962 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000963 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000964 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000965 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000966 return -1;
967 return 0;
968}
969
970static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000971set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000972{
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000973 Py_ssize_t i;
974
975 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
976 PyObject *other = PyTuple_GET_ITEM(args, i);
977 if (set_update_internal(so, other) == -1)
978 return NULL;
979 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000980 Py_RETURN_NONE;
981}
982
983PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000984"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000985
986static PyObject *
987make_new_set(PyTypeObject *type, PyObject *iterable)
988{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000989 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000990
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000991 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000992 dummy = PyUnicode_FromString("<dummy key>");
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000993 if (dummy == NULL)
994 return NULL;
995 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000996
997 /* create PySetObject structure */
Christian Heimes2202f872008-02-06 14:31:34 +0000998 if (numfree &&
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000999 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
Christian Heimes2202f872008-02-06 14:31:34 +00001000 so = free_list[--numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001001 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimes90aa7642007-12-19 02:45:37 +00001002 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001003 _Py_NewReference((PyObject *)so);
1004 EMPTY_TO_MINSIZE(so);
1005 PyObject_GC_Track(so);
1006 } else {
1007 so = (PySetObject *)type->tp_alloc(type, 0);
1008 if (so == NULL)
1009 return NULL;
1010 /* tp_alloc has already zeroed the structure */
1011 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1012 INIT_NONZERO_SET_SLOTS(so);
1013 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001014
Christian Heimes0ded5b52007-12-10 15:50:56 +00001015 so->lookup = set_lookkey_unicode;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001016 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001017
Raymond Hettingera38123e2003-11-24 22:18:49 +00001018 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001019 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001020 Py_DECREF(so);
1021 return NULL;
1022 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001023 }
1024
Raymond Hettingera690a992003-11-16 16:17:49 +00001025 return (PyObject *)so;
1026}
1027
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001028static PyObject *
1029make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1030{
1031 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1032 if (PyType_IsSubtype(type, &PySet_Type))
1033 type = &PySet_Type;
1034 else
1035 type = &PyFrozenSet_Type;
1036 }
1037 return make_new_set(type, iterable);
1038}
1039
Raymond Hettingerd7946662005-08-01 21:39:29 +00001040/* The empty frozenset is a singleton */
1041static PyObject *emptyfrozenset = NULL;
1042
Raymond Hettingera690a992003-11-16 16:17:49 +00001043static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001044frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001045{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001046 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001047
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001048 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001049 return NULL;
1050
Raymond Hettingera690a992003-11-16 16:17:49 +00001051 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1052 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001053
1054 if (type != &PyFrozenSet_Type)
1055 return make_new_set(type, iterable);
1056
1057 if (iterable != NULL) {
1058 /* frozenset(f) is idempotent */
1059 if (PyFrozenSet_CheckExact(iterable)) {
1060 Py_INCREF(iterable);
1061 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001062 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001063 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001064 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001065 return result;
1066 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001067 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001068 /* The empty frozenset is a singleton */
1069 if (emptyfrozenset == NULL)
1070 emptyfrozenset = make_new_set(type, NULL);
1071 Py_XINCREF(emptyfrozenset);
1072 return emptyfrozenset;
1073}
1074
1075void
1076PySet_Fini(void)
1077{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001078 PySetObject *so;
1079
Christian Heimes2202f872008-02-06 14:31:34 +00001080 while (numfree) {
1081 numfree--;
1082 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001083 PyObject_GC_Del(so);
1084 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085 Py_CLEAR(dummy);
1086 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001087}
1088
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001089static PyObject *
1090set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1091{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001092 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001093 return NULL;
1094
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001095 return make_new_set(type, NULL);
1096}
1097
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001098/* set_swap_bodies() switches the contents of any two sets by moving their
1099 internal data pointers and, if needed, copying the internal smalltables.
1100 Semantically equivalent to:
1101
1102 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1103
1104 The function always succeeds and it leaves both objects in a stable state.
1105 Useful for creating temporary frozensets from sets for membership testing
1106 in __contains__(), discard(), and remove(). Also useful for operations
1107 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001108 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001109*/
1110
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001111static void
1112set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001113{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001114 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001115 setentry *u;
1116 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1117 setentry tab[PySet_MINSIZE];
1118 long h;
1119
1120 t = a->fill; a->fill = b->fill; b->fill = t;
1121 t = a->used; a->used = b->used; b->used = t;
1122 t = a->mask; a->mask = b->mask; b->mask = t;
1123
1124 u = a->table;
1125 if (a->table == a->smalltable)
1126 u = b->smalltable;
1127 a->table = b->table;
1128 if (b->table == b->smalltable)
1129 a->table = a->smalltable;
1130 b->table = u;
1131
1132 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1133
1134 if (a->table == a->smalltable || b->table == b->smalltable) {
1135 memcpy(tab, a->smalltable, sizeof(tab));
1136 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1137 memcpy(b->smalltable, tab, sizeof(tab));
1138 }
1139
Christian Heimes90aa7642007-12-19 02:45:37 +00001140 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1141 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001142 h = a->hash; a->hash = b->hash; b->hash = h;
1143 } else {
1144 a->hash = -1;
1145 b->hash = -1;
1146 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001147}
1148
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001149static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001150set_copy(PySetObject *so)
1151{
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001152 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001153}
1154
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001155static PyObject *
1156frozenset_copy(PySetObject *so)
1157{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001158 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001159 Py_INCREF(so);
1160 return (PyObject *)so;
1161 }
1162 return set_copy(so);
1163}
1164
Raymond Hettingera690a992003-11-16 16:17:49 +00001165PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1166
1167static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001168set_clear(PySetObject *so)
1169{
1170 set_clear_internal(so);
1171 Py_RETURN_NONE;
1172}
1173
1174PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1175
1176static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001177set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001178{
1179 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001180 PyObject *other;
1181 Py_ssize_t i;
1182
1183 result = (PySetObject *)set_copy(so);
1184 if (result == NULL)
1185 return NULL;
1186
1187 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1188 other = PyTuple_GET_ITEM(args, i);
1189 if ((PyObject *)so == other)
1190 return (PyObject *)result;
1191 if (set_update_internal(result, other) == -1) {
1192 Py_DECREF(result);
1193 return NULL;
1194 }
1195 }
1196 return (PyObject *)result;
1197}
1198
1199PyDoc_STRVAR(union_doc,
1200 "Return the union of sets as a new set.\n\
1201\n\
1202(i.e. all elements that are in either set.)");
1203
1204static PyObject *
1205set_or(PySetObject *so, PyObject *other)
1206{
1207 PySetObject *result;
1208
1209 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1210 Py_INCREF(Py_NotImplemented);
1211 return Py_NotImplemented;
1212 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001213
1214 result = (PySetObject *)set_copy(so);
1215 if (result == NULL)
1216 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001217 if ((PyObject *)so == other)
1218 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001219 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001220 Py_DECREF(result);
1221 return NULL;
1222 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001223 return (PyObject *)result;
1224}
1225
Raymond Hettingera690a992003-11-16 16:17:49 +00001226static PyObject *
1227set_ior(PySetObject *so, PyObject *other)
1228{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001229 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001230 Py_INCREF(Py_NotImplemented);
1231 return Py_NotImplemented;
1232 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001233 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001234 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001235 Py_INCREF(so);
1236 return (PyObject *)so;
1237}
1238
1239static PyObject *
1240set_intersection(PySetObject *so, PyObject *other)
1241{
1242 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001243 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001244
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001245 if ((PyObject *)so == other)
1246 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001247
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001248 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001249 if (result == NULL)
1250 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001251
Christian Heimesaf98da12008-01-27 15:18:18 +00001252 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001253 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001254 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001255
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001256 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001257 tmp = (PyObject *)so;
1258 so = (PySetObject *)other;
1259 other = tmp;
1260 }
1261
Raymond Hettingerc991db22005-08-11 07:58:45 +00001262 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001263 int rv = set_contains_entry(so, entry);
1264 if (rv == -1) {
1265 Py_DECREF(result);
1266 return NULL;
1267 }
1268 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001269 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001270 Py_DECREF(result);
1271 return NULL;
1272 }
1273 }
1274 }
1275 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001276 }
1277
Raymond Hettingera690a992003-11-16 16:17:49 +00001278 it = PyObject_GetIter(other);
1279 if (it == NULL) {
1280 Py_DECREF(result);
1281 return NULL;
1282 }
1283
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001284 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001285 int rv;
1286 setentry entry;
1287 long hash = PyObject_Hash(key);
1288
1289 if (hash == -1) {
1290 Py_DECREF(it);
1291 Py_DECREF(result);
1292 Py_DECREF(key);
1293 return NULL;
1294 }
1295 entry.hash = hash;
1296 entry.key = key;
1297 rv = set_contains_entry(so, &entry);
1298 if (rv == -1) {
1299 Py_DECREF(it);
1300 Py_DECREF(result);
1301 Py_DECREF(key);
1302 return NULL;
1303 }
1304 if (rv) {
1305 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001306 Py_DECREF(it);
1307 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001308 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001309 return NULL;
1310 }
1311 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001312 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001313 }
1314 Py_DECREF(it);
1315 if (PyErr_Occurred()) {
1316 Py_DECREF(result);
1317 return NULL;
1318 }
1319 return (PyObject *)result;
1320}
1321
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001322static PyObject *
1323set_intersection_multi(PySetObject *so, PyObject *args)
1324{
1325 Py_ssize_t i;
1326 PyObject *result = (PyObject *)so;
1327
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001328 if (PyTuple_GET_SIZE(args) == 0)
1329 return set_copy(so);
1330
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001331 Py_INCREF(so);
1332 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1333 PyObject *other = PyTuple_GET_ITEM(args, i);
1334 PyObject *newresult = set_intersection((PySetObject *)result, other);
1335 if (newresult == NULL) {
1336 Py_DECREF(result);
1337 return NULL;
1338 }
1339 Py_DECREF(result);
1340 result = newresult;
1341 }
1342 return result;
1343}
1344
Raymond Hettingera690a992003-11-16 16:17:49 +00001345PyDoc_STRVAR(intersection_doc,
1346"Return the intersection of two sets as a new set.\n\
1347\n\
1348(i.e. all elements that are in both sets.)");
1349
1350static PyObject *
1351set_intersection_update(PySetObject *so, PyObject *other)
1352{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001353 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001354
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001355 tmp = set_intersection(so, other);
1356 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001357 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001358 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001359 Py_DECREF(tmp);
1360 Py_RETURN_NONE;
1361}
1362
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001363static PyObject *
1364set_intersection_update_multi(PySetObject *so, PyObject *args)
1365{
1366 PyObject *tmp;
1367
1368 tmp = set_intersection_multi(so, args);
1369 if (tmp == NULL)
1370 return NULL;
1371 set_swap_bodies(so, (PySetObject *)tmp);
1372 Py_DECREF(tmp);
1373 Py_RETURN_NONE;
1374}
1375
Raymond Hettingera690a992003-11-16 16:17:49 +00001376PyDoc_STRVAR(intersection_update_doc,
1377"Update a set with the intersection of itself and another.");
1378
1379static PyObject *
1380set_and(PySetObject *so, PyObject *other)
1381{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001382 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001383 Py_INCREF(Py_NotImplemented);
1384 return Py_NotImplemented;
1385 }
1386 return set_intersection(so, other);
1387}
1388
1389static PyObject *
1390set_iand(PySetObject *so, PyObject *other)
1391{
1392 PyObject *result;
1393
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001394 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001395 Py_INCREF(Py_NotImplemented);
1396 return Py_NotImplemented;
1397 }
1398 result = set_intersection_update(so, other);
1399 if (result == NULL)
1400 return NULL;
1401 Py_DECREF(result);
1402 Py_INCREF(so);
1403 return (PyObject *)so;
1404}
1405
Guido van Rossum58da9312007-11-10 23:39:45 +00001406static PyObject *
1407set_isdisjoint(PySetObject *so, PyObject *other)
1408{
1409 PyObject *key, *it, *tmp;
1410
1411 if ((PyObject *)so == other) {
1412 if (PySet_GET_SIZE(so) == 0)
1413 Py_RETURN_TRUE;
1414 else
1415 Py_RETURN_FALSE;
1416 }
1417
1418 if (PyAnySet_CheckExact(other)) {
1419 Py_ssize_t pos = 0;
1420 setentry *entry;
1421
1422 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1423 tmp = (PyObject *)so;
1424 so = (PySetObject *)other;
1425 other = tmp;
1426 }
1427 while (set_next((PySetObject *)other, &pos, &entry)) {
1428 int rv = set_contains_entry(so, entry);
1429 if (rv == -1)
1430 return NULL;
1431 if (rv)
1432 Py_RETURN_FALSE;
1433 }
1434 Py_RETURN_TRUE;
1435 }
1436
1437 it = PyObject_GetIter(other);
1438 if (it == NULL)
1439 return NULL;
1440
1441 while ((key = PyIter_Next(it)) != NULL) {
1442 int rv;
1443 setentry entry;
Christian Heimes0ded5b52007-12-10 15:50:56 +00001444 long hash = PyObject_Hash(key);;
Guido van Rossum58da9312007-11-10 23:39:45 +00001445
1446 if (hash == -1) {
1447 Py_DECREF(key);
1448 Py_DECREF(it);
1449 return NULL;
1450 }
1451 entry.hash = hash;
1452 entry.key = key;
1453 rv = set_contains_entry(so, &entry);
1454 Py_DECREF(key);
1455 if (rv == -1) {
1456 Py_DECREF(it);
1457 return NULL;
1458 }
1459 if (rv) {
1460 Py_DECREF(it);
1461 Py_RETURN_FALSE;
1462 }
1463 }
1464 Py_DECREF(it);
1465 if (PyErr_Occurred())
1466 return NULL;
1467 Py_RETURN_TRUE;
1468}
1469
1470PyDoc_STRVAR(isdisjoint_doc,
1471"Return True if two sets have a null intersection.");
1472
Neal Norwitz6576bd82005-11-13 18:41:28 +00001473static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001474set_difference_update_internal(PySetObject *so, PyObject *other)
1475{
1476 if ((PyObject *)so == other)
1477 return set_clear_internal(so);
1478
Christian Heimesaf98da12008-01-27 15:18:18 +00001479 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001480 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001481 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001482
1483 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001484 if (set_discard_entry(so, entry) == -1)
1485 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001486 } else {
1487 PyObject *key, *it;
1488 it = PyObject_GetIter(other);
1489 if (it == NULL)
1490 return -1;
1491
1492 while ((key = PyIter_Next(it)) != NULL) {
1493 if (set_discard_key(so, key) == -1) {
1494 Py_DECREF(it);
1495 Py_DECREF(key);
1496 return -1;
1497 }
1498 Py_DECREF(key);
1499 }
1500 Py_DECREF(it);
1501 if (PyErr_Occurred())
1502 return -1;
1503 }
1504 /* If more than 1/5 are dummies, then resize them away. */
1505 if ((so->fill - so->used) * 5 < so->mask)
1506 return 0;
1507 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1508}
1509
Raymond Hettingera690a992003-11-16 16:17:49 +00001510static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001511set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001512{
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001513 Py_ssize_t i;
1514
1515 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1516 PyObject *other = PyTuple_GET_ITEM(args, i);
1517 if (set_difference_update_internal(so, other) == -1)
1518 return NULL;
1519 }
1520 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001521}
1522
1523PyDoc_STRVAR(difference_update_doc,
1524"Remove all elements of another set from this set.");
1525
1526static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001527set_difference(PySetObject *so, PyObject *other)
1528{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001529 PyObject *result;
1530 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001531 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001532
Christian Heimesaf98da12008-01-27 15:18:18 +00001533 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001534 result = set_copy(so);
1535 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001536 return NULL;
1537 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001538 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001539 Py_DECREF(result);
1540 return NULL;
1541 }
1542
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001543 result = make_new_set_basetype(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001544 if (result == NULL)
1545 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001546
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001547 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001548 while (set_next(so, &pos, &entry)) {
1549 setentry entrycopy;
1550 entrycopy.hash = entry->hash;
1551 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001552 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001553 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1554 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001555 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001556 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001557 }
1558 }
1559 return result;
1560 }
1561
Raymond Hettingerc991db22005-08-11 07:58:45 +00001562 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001563 int rv = set_contains_entry((PySetObject *)other, entry);
1564 if (rv == -1) {
1565 Py_DECREF(result);
1566 return NULL;
1567 }
1568 if (!rv) {
1569 if (set_add_entry((PySetObject *)result, entry) == -1) {
1570 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001571 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001572 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001573 }
1574 }
1575 return result;
1576}
1577
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001578static PyObject *
1579set_difference_multi(PySetObject *so, PyObject *args)
1580{
1581 Py_ssize_t i;
1582 PyObject *result, *other;
1583
1584 if (PyTuple_GET_SIZE(args) == 0)
1585 return set_copy(so);
1586
1587 other = PyTuple_GET_ITEM(args, 0);
1588 result = set_difference(so, other);
1589 if (result == NULL)
1590 return NULL;
1591
1592 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1593 other = PyTuple_GET_ITEM(args, i);
1594 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1595 Py_DECREF(result);
1596 return NULL;
1597 }
1598 }
1599 return result;
1600}
1601
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001602PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001603"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001604\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001605(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001606static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001607set_sub(PySetObject *so, PyObject *other)
1608{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001609 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001610 Py_INCREF(Py_NotImplemented);
1611 return Py_NotImplemented;
1612 }
1613 return set_difference(so, other);
1614}
1615
1616static PyObject *
1617set_isub(PySetObject *so, PyObject *other)
1618{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001619 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001620 Py_INCREF(Py_NotImplemented);
1621 return Py_NotImplemented;
1622 }
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001623 if (set_difference_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001624 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001625 Py_INCREF(so);
1626 return (PyObject *)so;
1627}
1628
1629static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001630set_symmetric_difference_update(PySetObject *so, PyObject *other)
1631{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001632 PySetObject *otherset;
1633 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001634 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001635 setentry *entry;
1636
1637 if ((PyObject *)so == other)
1638 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001639
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001640 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001641 PyObject *value;
1642 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001643 long hash;
1644 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001645 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001646
Thomas Wouters89f507f2006-12-13 04:49:30 +00001647 an_entry.hash = hash;
1648 an_entry.key = key;
1649 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001650 if (rv == -1)
1651 return NULL;
1652 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001653 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001654 return NULL;
1655 }
1656 }
1657 Py_RETURN_NONE;
1658 }
1659
Christian Heimesaf98da12008-01-27 15:18:18 +00001660 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001661 Py_INCREF(other);
1662 otherset = (PySetObject *)other;
1663 } else {
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001664 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001665 if (otherset == NULL)
1666 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001667 }
1668
Raymond Hettingerc991db22005-08-11 07:58:45 +00001669 while (set_next(otherset, &pos, &entry)) {
1670 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001671 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001672 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001673 return NULL;
1674 }
1675 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001676 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001677 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001678 return NULL;
1679 }
1680 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001681 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001682 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001683 Py_RETURN_NONE;
1684}
1685
1686PyDoc_STRVAR(symmetric_difference_update_doc,
1687"Update a set with the symmetric difference of itself and another.");
1688
1689static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001690set_symmetric_difference(PySetObject *so, PyObject *other)
1691{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001692 PyObject *rv;
1693 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001694
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001695 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001696 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001697 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001698 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1699 if (rv == NULL)
1700 return NULL;
1701 Py_DECREF(rv);
1702 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001703}
1704
1705PyDoc_STRVAR(symmetric_difference_doc,
1706"Return the symmetric difference of two sets as a new set.\n\
1707\n\
1708(i.e. all elements that are in exactly one of the sets.)");
1709
1710static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001711set_xor(PySetObject *so, PyObject *other)
1712{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001713 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001714 Py_INCREF(Py_NotImplemented);
1715 return Py_NotImplemented;
1716 }
1717 return set_symmetric_difference(so, other);
1718}
1719
1720static PyObject *
1721set_ixor(PySetObject *so, PyObject *other)
1722{
1723 PyObject *result;
1724
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001725 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001726 Py_INCREF(Py_NotImplemented);
1727 return Py_NotImplemented;
1728 }
1729 result = set_symmetric_difference_update(so, other);
1730 if (result == NULL)
1731 return NULL;
1732 Py_DECREF(result);
1733 Py_INCREF(so);
1734 return (PyObject *)so;
1735}
1736
1737static PyObject *
1738set_issubset(PySetObject *so, PyObject *other)
1739{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001740 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001741 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001742
Christian Heimesaf98da12008-01-27 15:18:18 +00001743 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001744 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001745 tmp = make_new_set(&PySet_Type, other);
1746 if (tmp == NULL)
1747 return NULL;
1748 result = set_issubset(so, tmp);
1749 Py_DECREF(tmp);
1750 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001751 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001752 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001753 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001754
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001755 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001756 int rv = set_contains_entry((PySetObject *)other, entry);
1757 if (rv == -1)
1758 return NULL;
1759 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001760 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001761 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001762 Py_RETURN_TRUE;
1763}
1764
1765PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1766
1767static PyObject *
1768set_issuperset(PySetObject *so, PyObject *other)
1769{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001770 PyObject *tmp, *result;
1771
Christian Heimesaf98da12008-01-27 15:18:18 +00001772 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001773 tmp = make_new_set(&PySet_Type, other);
1774 if (tmp == NULL)
1775 return NULL;
1776 result = set_issuperset(so, tmp);
1777 Py_DECREF(tmp);
1778 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001779 }
1780 return set_issubset((PySetObject *)other, (PyObject *)so);
1781}
1782
1783PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1784
Raymond Hettingera690a992003-11-16 16:17:49 +00001785static PyObject *
1786set_richcompare(PySetObject *v, PyObject *w, int op)
1787{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001788 PyObject *r1, *r2;
1789
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001790 if(!PyAnySet_Check(w)) {
Guido van Rossum10ab4ae2007-08-23 23:57:24 +00001791 Py_INCREF(Py_NotImplemented);
1792 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001793 }
1794 switch (op) {
1795 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001796 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001797 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001798 if (v->hash != -1 &&
1799 ((PySetObject *)w)->hash != -1 &&
1800 v->hash != ((PySetObject *)w)->hash)
1801 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001802 return set_issubset(v, w);
1803 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001804 r1 = set_richcompare(v, w, Py_EQ);
1805 if (r1 == NULL)
1806 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001807 r2 = PyBool_FromLong(PyObject_Not(r1));
1808 Py_DECREF(r1);
1809 return r2;
1810 case Py_LE:
1811 return set_issubset(v, w);
1812 case Py_GE:
1813 return set_issuperset(v, w);
1814 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001815 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001816 Py_RETURN_FALSE;
1817 return set_issubset(v, w);
1818 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001819 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001820 Py_RETURN_FALSE;
1821 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001822 }
1823 Py_INCREF(Py_NotImplemented);
1824 return Py_NotImplemented;
1825}
1826
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001827static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001828set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001829{
1830 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1831 return -1;
1832}
1833
Raymond Hettingera690a992003-11-16 16:17:49 +00001834static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001835set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001836{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001837 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001838 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001839 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001840}
1841
1842PyDoc_STRVAR(add_doc,
1843"Add an element to a set.\n\
1844\n\
1845This has no effect if the element is already present.");
1846
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001847static int
1848set_contains(PySetObject *so, PyObject *key)
1849{
1850 PyObject *tmpkey;
1851 int rv;
1852
1853 rv = set_contains_key(so, key);
1854 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001855 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001856 return -1;
1857 PyErr_Clear();
1858 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1859 if (tmpkey == NULL)
1860 return -1;
1861 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1862 rv = set_contains(so, tmpkey);
1863 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1864 Py_DECREF(tmpkey);
1865 }
1866 return rv;
1867}
1868
1869static PyObject *
1870set_direct_contains(PySetObject *so, PyObject *key)
1871{
1872 long result;
1873
1874 result = set_contains(so, key);
1875 if (result == -1)
1876 return NULL;
1877 return PyBool_FromLong(result);
1878}
1879
1880PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1881
Raymond Hettingera690a992003-11-16 16:17:49 +00001882static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001883set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001884{
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001885 PyObject *tmpkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001886 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001887
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001888 rv = set_discard_key(so, key);
1889 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001890 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001891 return NULL;
1892 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001893 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1894 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001895 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001896 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001897 rv = set_discard_key(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001898 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001899 Py_DECREF(tmpkey);
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001900 if (rv == -1)
1901 return NULL;
1902 }
1903
1904 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001905 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001906 return NULL;
1907 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001908 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001909}
1910
1911PyDoc_STRVAR(remove_doc,
1912"Remove an element from a set; it must be a member.\n\
1913\n\
1914If the element is not a member, raise a KeyError.");
1915
1916static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001917set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001918{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001919 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001920 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001921
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001922 rv = set_discard_key(so, key);
1923 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001924 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001925 return NULL;
1926 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001927 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1928 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001929 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001930 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001931 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001932 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001933 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001934 return result;
1935 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001936 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001937}
1938
1939PyDoc_STRVAR(discard_doc,
1940"Remove an element from a set if it is a member.\n\
1941\n\
1942If the element is not a member, do nothing.");
1943
1944static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001945set_reduce(PySetObject *so)
1946{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001947 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001948
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001949 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001950 if (keys == NULL)
1951 goto done;
1952 args = PyTuple_Pack(1, keys);
1953 if (args == NULL)
1954 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001955 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1956 if (dict == NULL) {
1957 PyErr_Clear();
1958 dict = Py_None;
1959 Py_INCREF(dict);
1960 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001961 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001962done:
1963 Py_XDECREF(args);
1964 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001965 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001966 return result;
1967}
1968
1969PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1970
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001971static PyObject *
1972set_sizeof(PySetObject *so)
1973{
1974 Py_ssize_t res;
1975
1976 res = sizeof(PySetObject);
1977 if (so->table != so->smalltable)
1978 res = res + (so->mask + 1) * sizeof(setentry);
1979 return PyLong_FromSsize_t(res);
1980}
1981
1982PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001983static int
1984set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1985{
1986 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001987
1988 if (!PyAnySet_Check(self))
1989 return -1;
Christian Heimes90aa7642007-12-19 02:45:37 +00001990 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001991 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001992 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001993 self->hash = -1;
1994 if (iterable == NULL)
1995 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001996 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001997}
1998
Raymond Hettingera690a992003-11-16 16:17:49 +00001999static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002000 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00002001 0, /* sq_concat */
2002 0, /* sq_repeat */
2003 0, /* sq_item */
2004 0, /* sq_slice */
2005 0, /* sq_ass_item */
2006 0, /* sq_ass_slice */
2007 (objobjproc)set_contains, /* sq_contains */
2008};
2009
2010/* set object ********************************************************/
2011
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002012#ifdef Py_DEBUG
2013static PyObject *test_c_api(PySetObject *so);
2014
2015PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2016All is well if assertions don't fail.");
2017#endif
2018
Raymond Hettingera690a992003-11-16 16:17:49 +00002019static PyMethodDef set_methods[] = {
2020 {"add", (PyCFunction)set_add, METH_O,
2021 add_doc},
2022 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2023 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00002024 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002025 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002026 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2027 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002028 {"discard", (PyCFunction)set_discard, METH_O,
2029 discard_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002030 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002031 difference_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002032 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002033 difference_update_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002034 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002035 intersection_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002036 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002037 intersection_update_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002038 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2039 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002040 {"issubset", (PyCFunction)set_issubset, METH_O,
2041 issubset_doc},
2042 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2043 issuperset_doc},
2044 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2045 pop_doc},
2046 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2047 reduce_doc},
2048 {"remove", (PyCFunction)set_remove, METH_O,
2049 remove_doc},
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002050 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2051 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002052 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2053 symmetric_difference_doc},
2054 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2055 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002056#ifdef Py_DEBUG
2057 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2058 test_c_api_doc},
2059#endif
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002060 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002061 union_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002062 {"update", (PyCFunction)set_update, METH_VARARGS,
Raymond Hettingera38123e2003-11-24 22:18:49 +00002063 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002064 {NULL, NULL} /* sentinel */
2065};
2066
2067static PyNumberMethods set_as_number = {
2068 0, /*nb_add*/
2069 (binaryfunc)set_sub, /*nb_subtract*/
2070 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002071 0, /*nb_remainder*/
2072 0, /*nb_divmod*/
2073 0, /*nb_power*/
2074 0, /*nb_negative*/
2075 0, /*nb_positive*/
2076 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002077 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002078 0, /*nb_invert*/
2079 0, /*nb_lshift*/
2080 0, /*nb_rshift*/
2081 (binaryfunc)set_and, /*nb_and*/
2082 (binaryfunc)set_xor, /*nb_xor*/
2083 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002084 0, /*nb_int*/
2085 0, /*nb_long*/
2086 0, /*nb_float*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002087 0, /*nb_inplace_add*/
2088 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2089 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002090 0, /*nb_inplace_remainder*/
2091 0, /*nb_inplace_power*/
2092 0, /*nb_inplace_lshift*/
2093 0, /*nb_inplace_rshift*/
2094 (binaryfunc)set_iand, /*nb_inplace_and*/
2095 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2096 (binaryfunc)set_ior, /*nb_inplace_or*/
2097};
2098
2099PyDoc_STRVAR(set_doc,
2100"set(iterable) --> set object\n\
2101\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002102Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002103
2104PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002105 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002106 "set", /* tp_name */
2107 sizeof(PySetObject), /* tp_basicsize */
2108 0, /* tp_itemsize */
2109 /* methods */
2110 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002111 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002112 0, /* tp_getattr */
2113 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002114 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002115 (reprfunc)set_repr, /* tp_repr */
2116 &set_as_number, /* tp_as_number */
2117 &set_as_sequence, /* tp_as_sequence */
2118 0, /* tp_as_mapping */
Nick Coghland1abd252008-07-15 15:46:38 +00002119 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00002120 0, /* tp_call */
2121 0, /* tp_str */
2122 PyObject_GenericGetAttr, /* tp_getattro */
2123 0, /* tp_setattro */
2124 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002125 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002126 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002127 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002128 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002129 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002130 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002131 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002132 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002133 0, /* tp_iternext */
2134 set_methods, /* tp_methods */
2135 0, /* tp_members */
2136 0, /* tp_getset */
2137 0, /* tp_base */
2138 0, /* tp_dict */
2139 0, /* tp_descr_get */
2140 0, /* tp_descr_set */
2141 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002142 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002143 PyType_GenericAlloc, /* tp_alloc */
2144 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002145 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002146};
2147
2148/* frozenset object ********************************************************/
2149
2150
2151static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002152 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002153 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002154 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002155 copy_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002156 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002157 difference_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002158 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002159 intersection_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002160 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2161 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002162 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002163 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002164 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002165 issuperset_doc},
2166 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2167 reduce_doc},
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002168 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2169 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002170 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2171 symmetric_difference_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002172 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002173 union_doc},
2174 {NULL, NULL} /* sentinel */
2175};
2176
2177static PyNumberMethods frozenset_as_number = {
2178 0, /*nb_add*/
2179 (binaryfunc)set_sub, /*nb_subtract*/
2180 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002181 0, /*nb_remainder*/
2182 0, /*nb_divmod*/
2183 0, /*nb_power*/
2184 0, /*nb_negative*/
2185 0, /*nb_positive*/
2186 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002187 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002188 0, /*nb_invert*/
2189 0, /*nb_lshift*/
2190 0, /*nb_rshift*/
2191 (binaryfunc)set_and, /*nb_and*/
2192 (binaryfunc)set_xor, /*nb_xor*/
2193 (binaryfunc)set_or, /*nb_or*/
2194};
2195
2196PyDoc_STRVAR(frozenset_doc,
2197"frozenset(iterable) --> frozenset object\n\
2198\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002199Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002200
2201PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002202 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002203 "frozenset", /* tp_name */
2204 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002205 0, /* tp_itemsize */
2206 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002207 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002208 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002209 0, /* tp_getattr */
2210 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002211 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002212 (reprfunc)set_repr, /* tp_repr */
2213 &frozenset_as_number, /* tp_as_number */
2214 &set_as_sequence, /* tp_as_sequence */
2215 0, /* tp_as_mapping */
2216 frozenset_hash, /* tp_hash */
2217 0, /* tp_call */
2218 0, /* tp_str */
2219 PyObject_GenericGetAttr, /* tp_getattro */
2220 0, /* tp_setattro */
2221 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002222 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002223 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002224 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002225 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002226 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002227 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002228 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002229 (getiterfunc)set_iter, /* tp_iter */
2230 0, /* tp_iternext */
2231 frozenset_methods, /* tp_methods */
2232 0, /* tp_members */
2233 0, /* tp_getset */
2234 0, /* tp_base */
2235 0, /* tp_dict */
2236 0, /* tp_descr_get */
2237 0, /* tp_descr_set */
2238 0, /* tp_dictoffset */
2239 0, /* tp_init */
2240 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002241 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002242 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002243};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002244
2245
2246/***** C API functions *************************************************/
2247
2248PyObject *
2249PySet_New(PyObject *iterable)
2250{
2251 return make_new_set(&PySet_Type, iterable);
2252}
2253
2254PyObject *
2255PyFrozenSet_New(PyObject *iterable)
2256{
Christian Heimesfd66e512008-01-29 12:18:50 +00002257 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002258}
2259
Neal Norwitz8c49c822006-03-04 18:41:19 +00002260Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002261PySet_Size(PyObject *anyset)
2262{
2263 if (!PyAnySet_Check(anyset)) {
2264 PyErr_BadInternalCall();
2265 return -1;
2266 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002267 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002268}
2269
2270int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002271PySet_Clear(PyObject *set)
2272{
Christian Heimesfd66e512008-01-29 12:18:50 +00002273 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002274 PyErr_BadInternalCall();
2275 return -1;
2276 }
2277 return set_clear_internal((PySetObject *)set);
2278}
2279
2280int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002281PySet_Contains(PyObject *anyset, PyObject *key)
2282{
2283 if (!PyAnySet_Check(anyset)) {
2284 PyErr_BadInternalCall();
2285 return -1;
2286 }
2287 return set_contains_key((PySetObject *)anyset, key);
2288}
2289
2290int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002291PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002292{
Christian Heimesfd66e512008-01-29 12:18:50 +00002293 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002294 PyErr_BadInternalCall();
2295 return -1;
2296 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002297 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002298}
2299
2300int
Christian Heimesfd66e512008-01-29 12:18:50 +00002301PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002302{
Christian Heimes15ebc882008-02-04 18:48:49 +00002303 if (!PySet_Check(anyset) &&
2304 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002305 PyErr_BadInternalCall();
2306 return -1;
2307 }
Christian Heimesfd66e512008-01-29 12:18:50 +00002308 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002309}
2310
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002311int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002312_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2313{
2314 setentry *entry;
2315
2316 if (!PyAnySet_Check(set)) {
2317 PyErr_BadInternalCall();
2318 return -1;
2319 }
2320 if (set_next((PySetObject *)set, pos, &entry) == 0)
2321 return 0;
2322 *key = entry->key;
2323 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002324 return 1;
2325}
2326
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002327PyObject *
2328PySet_Pop(PyObject *set)
2329{
Christian Heimesfd66e512008-01-29 12:18:50 +00002330 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002331 PyErr_BadInternalCall();
2332 return NULL;
2333 }
2334 return set_pop((PySetObject *)set);
2335}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002336
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002337int
2338_PySet_Update(PyObject *set, PyObject *iterable)
2339{
Christian Heimesfd66e512008-01-29 12:18:50 +00002340 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002341 PyErr_BadInternalCall();
2342 return -1;
2343 }
2344 return set_update_internal((PySetObject *)set, iterable);
2345}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002346
2347#ifdef Py_DEBUG
2348
2349/* Test code to be called with any three element set.
2350 Returns True and original set is restored. */
2351
2352#define assertRaises(call_return_value, exception) \
2353 do { \
2354 assert(call_return_value); \
2355 assert(PyErr_ExceptionMatches(exception)); \
2356 PyErr_Clear(); \
2357 } while(0)
2358
2359static PyObject *
2360test_c_api(PySetObject *so)
2361{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002362 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002363 char *s;
2364 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002365 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002366 PyObject *ob = (PyObject *)so;
Christian Heimesdb967892008-01-31 01:08:32 +00002367 long hash;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002368
2369 /* Verify preconditions and exercise type/size checks */
2370 assert(PyAnySet_Check(ob));
2371 assert(PyAnySet_CheckExact(ob));
2372 assert(!PyFrozenSet_CheckExact(ob));
2373 assert(PySet_Size(ob) == 3);
2374 assert(PySet_GET_SIZE(ob) == 3);
2375
2376 /* Raise TypeError for non-iterable constructor arguments */
2377 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2378 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2379
2380 /* Raise TypeError for unhashable key */
2381 dup = PySet_New(ob);
2382 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2383 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2384 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2385
2386 /* Exercise successful pop, contains, add, and discard */
2387 elem = PySet_Pop(ob);
2388 assert(PySet_Contains(ob, elem) == 0);
2389 assert(PySet_GET_SIZE(ob) == 2);
2390 assert(PySet_Add(ob, elem) == 0);
2391 assert(PySet_Contains(ob, elem) == 1);
2392 assert(PySet_GET_SIZE(ob) == 3);
2393 assert(PySet_Discard(ob, elem) == 1);
2394 assert(PySet_GET_SIZE(ob) == 2);
2395 assert(PySet_Discard(ob, elem) == 0);
2396 assert(PySet_GET_SIZE(ob) == 2);
2397
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002398 /* Exercise clear */
2399 dup2 = PySet_New(dup);
2400 assert(PySet_Clear(dup2) == 0);
2401 assert(PySet_Size(dup2) == 0);
2402 Py_DECREF(dup2);
2403
2404 /* Raise SystemError on clear or update of frozen set */
2405 f = PyFrozenSet_New(dup);
2406 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2407 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Christian Heimes15ebc882008-02-04 18:48:49 +00002408 assert(PySet_Add(f, elem) == 0);
2409 Py_INCREF(f);
2410 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2411 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002412 Py_DECREF(f);
2413
2414 /* Exercise direct iteration */
2415 i = 0, count = 0;
Christian Heimesdb967892008-01-31 01:08:32 +00002416 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002417 s = _PyUnicode_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002418 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2419 count++;
2420 }
2421 assert(count == 3);
2422
2423 /* Exercise updates */
2424 dup2 = PySet_New(NULL);
2425 assert(_PySet_Update(dup2, dup) == 0);
2426 assert(PySet_Size(dup2) == 3);
2427 assert(_PySet_Update(dup2, dup) == 0);
2428 assert(PySet_Size(dup2) == 3);
2429 Py_DECREF(dup2);
2430
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002431 /* Raise SystemError when self argument is not a set or frozenset. */
2432 t = PyTuple_New(0);
2433 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2434 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2435 Py_DECREF(t);
2436
2437 /* Raise SystemError when self argument is not a set. */
2438 f = PyFrozenSet_New(dup);
2439 assert(PySet_Size(f) == 3);
2440 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002441 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2442 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2443 Py_DECREF(f);
2444
2445 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002446 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2447 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002448 assert(PySet_GET_SIZE(ob) == 0);
2449 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2450
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002451 /* Restore the set from the copy using the PyNumber API */
2452 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2453 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002454
2455 /* Verify constructors accept NULL arguments */
2456 f = PySet_New(NULL);
2457 assert(f != NULL);
2458 assert(PySet_GET_SIZE(f) == 0);
2459 Py_DECREF(f);
2460 f = PyFrozenSet_New(NULL);
2461 assert(f != NULL);
2462 assert(PyFrozenSet_CheckExact(f));
2463 assert(PySet_GET_SIZE(f) == 0);
2464 Py_DECREF(f);
2465
2466 Py_DECREF(elem);
2467 Py_DECREF(dup);
2468 Py_RETURN_TRUE;
2469}
2470
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002471#undef assertRaises
2472
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002473#endif