Merged revisions 78818 via svnmerge from
[python/dscho.git] / Objects / setobject.c
blob742dadcaa2bf95eb71a65b08956fc7415f9d101d
2 /* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
6 Copyright (c) 2003-2008 Python Software Foundation.
7 All rights reserved.
8 */
10 #include "Python.h"
11 #include "structmember.h"
12 #include "stringlib/eq.h"
14 /* 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. */
17 static void
18 set_key_error(PyObject *arg)
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);
28 /* This must be >= 1. */
29 #define PERTURB_SHIFT 5
31 /* Object used as dummy key to fill deleted entries */
32 static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
34 #ifdef Py_REF_DEBUG
35 PyObject *
36 _PySet_Dummy(void)
38 return dummy;
40 #endif
42 #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)
48 #define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
51 INIT_NONZERO_SET_SLOTS(so); \
52 } while(0)
54 /* Reuse scheme to save calls to malloc, free, and memset */
55 #ifndef PySet_MAXFREELIST
56 #define PySet_MAXFREELIST 80
57 #endif
58 static PySetObject *free_list[PySet_MAXFREELIST];
59 static int numfree = 0;
63 The basic lookup function used by all operations.
64 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65 Open addressing is preferred over chaining since the link overhead for
66 chaining would be substantial (100% with typical malloc overhead).
68 The initial probe index is computed as hash mod the table size. Subsequent
69 probe indices are computed as explained in Objects/dictobject.c.
71 All arithmetic on hash should ignore overflow.
73 Unlike the dictionary implementation, the lookkey functions can return
74 NULL if the rich comparison returns an error.
77 static setentry *
78 set_lookkey(PySetObject *so, PyObject *key, register long hash)
80 register Py_ssize_t i;
81 register size_t perturb;
82 register setentry *freeslot;
83 register size_t mask = so->mask;
84 setentry *table = so->table;
85 register setentry *entry;
86 register int cmp;
87 PyObject *startkey;
89 i = hash & mask;
90 entry = &table[i];
91 if (entry->key == NULL || entry->key == key)
92 return entry;
94 if (entry->key == dummy)
95 freeslot = entry;
96 else {
97 if (entry->hash == hash) {
98 startkey = entry->key;
99 Py_INCREF(startkey);
100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
101 Py_DECREF(startkey);
102 if (cmp < 0)
103 return NULL;
104 if (table == so->table && entry->key == startkey) {
105 if (cmp > 0)
106 return entry;
108 else {
109 /* The compare did major nasty stuff to the
110 * set: start over.
112 return set_lookkey(so, key, hash);
115 freeslot = NULL;
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;
122 entry = &table[i & mask];
123 if (entry->key == NULL) {
124 if (freeslot != NULL)
125 entry = freeslot;
126 break;
128 if (entry->key == key)
129 break;
130 if (entry->hash == hash && entry->key != dummy) {
131 startkey = entry->key;
132 Py_INCREF(startkey);
133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
134 Py_DECREF(startkey);
135 if (cmp < 0)
136 return NULL;
137 if (table == so->table && entry->key == startkey) {
138 if (cmp > 0)
139 break;
141 else {
142 /* The compare did major nasty stuff to the
143 * set: start over.
145 return set_lookkey(so, key, hash);
148 else if (entry->key == dummy && freeslot == NULL)
149 freeslot = entry;
151 return entry;
155 * 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
157 * see if the comparison altered the table.
159 static setentry *
160 set_lookkey_unicode(PySetObject *so, PyObject *key, register long hash)
162 register Py_ssize_t i;
163 register size_t perturb;
164 register setentry *freeslot;
165 register size_t mask = so->mask;
166 setentry *table = so->table;
167 register setentry *entry;
169 /* Make sure this function doesn't have to handle non-unicode keys,
170 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. */
173 if (!PyUnicode_CheckExact(key)) {
174 so->lookup = set_lookkey;
175 return set_lookkey(so, key, hash);
177 i = hash & mask;
178 entry = &table[i];
179 if (entry->key == NULL || entry->key == key)
180 return entry;
181 if (entry->key == dummy)
182 freeslot = entry;
183 else {
184 if (entry->hash == hash && unicode_eq(entry->key, key))
185 return entry;
186 freeslot = NULL;
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
199 && unicode_eq(entry->key, key)))
200 return entry;
201 if (entry->key == dummy && freeslot == NULL)
202 freeslot = entry;
204 assert(0); /* NOT REACHED */
205 return 0;
209 Internal routine to insert a new key into the table.
210 Used by the public insert routine.
211 Eats a reference to key.
213 static int
214 set_insert_key(register PySetObject *so, PyObject *key, long hash)
216 register setentry *entry;
217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
219 assert(so->lookup != NULL);
220 entry = so->lookup(so, key, hash);
221 if (entry == NULL)
222 return -1;
223 if (entry->key == NULL) {
224 /* UNUSED */
225 so->fill++;
226 entry->key = key;
227 entry->hash = hash;
228 so->used++;
229 } else if (entry->key == dummy) {
230 /* DUMMY */
231 entry->key = key;
232 entry->hash = hash;
233 so->used++;
234 Py_DECREF(dummy);
235 } else {
236 /* ACTIVE */
237 Py_DECREF(key);
239 return 0;
243 Internal routine used by set_table_resize() to insert an item which is
244 known to be absent from the set. This routine also assumes that
245 the set contains no deleted entries. Besides the performance benefit,
246 using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
247 Note that no refcounts are changed by this routine; if needed, the caller
248 is responsible for incref'ing `key`.
250 static void
251 set_insert_clean(register PySetObject *so, PyObject *key, long hash)
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;
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];
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
272 Restructure the table by allocating a new table and reinserting all
273 keys again. When entries have been deleted, the new table may
274 actually be smaller than the old one.
276 static int
277 set_table_resize(PySetObject *so, Py_ssize_t minused)
279 Py_ssize_t newsize;
280 setentry *oldtable, *newtable, *entry;
281 Py_ssize_t i;
282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE];
285 assert(minused >= 0);
287 /* Find the smallest table size > minused. */
288 for (newsize = PySet_MINSIZE;
289 newsize <= minused && newsize > 0;
290 newsize <<= 1)
292 if (newsize <= 0) {
293 PyErr_NoMemory();
294 return -1;
297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL);
300 is_oldtable_malloced = oldtable != so->smalltable;
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;
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;
321 else {
322 newtable = PyMem_NEW(setentry, newsize);
323 if (newtable == NULL) {
324 PyErr_NoMemory();
325 return -1;
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;
338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
340 for (entry = oldtable; i > 0; entry++) {
341 if (entry->key == NULL) {
342 /* UNUSED */
344 } else if (entry->key == dummy) {
345 /* DUMMY */
346 --i;
347 assert(entry->key == dummy);
348 Py_DECREF(entry->key);
349 } else {
350 /* ACTIVE */
351 --i;
352 set_insert_clean(so, entry->key, entry->hash);
356 if (is_oldtable_malloced)
357 PyMem_DEL(oldtable);
358 return 0;
361 /* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
363 static int
364 set_add_entry(register PySetObject *so, setentry *entry)
366 register Py_ssize_t n_used;
368 assert(so->fill <= so->mask); /* at least one empty slot */
369 n_used = so->used;
370 Py_INCREF(entry->key);
371 if (set_insert_key(so, entry->key, entry->hash) == -1) {
372 Py_DECREF(entry->key);
373 return -1;
375 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);
380 static int
381 set_add_key(register PySetObject *so, PyObject *key)
383 register long hash;
384 register Py_ssize_t n_used;
386 if (!PyUnicode_CheckExact(key) ||
387 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
388 hash = PyObject_Hash(key);
389 if (hash == -1)
390 return -1;
392 assert(so->fill <= so->mask); /* at least one empty slot */
393 n_used = so->used;
394 Py_INCREF(key);
395 if (set_insert_key(so, key, hash) == -1) {
396 Py_DECREF(key);
397 return -1;
399 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
400 return 0;
401 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
404 #define DISCARD_NOTFOUND 0
405 #define DISCARD_FOUND 1
407 static int
408 set_discard_entry(PySetObject *so, setentry *oldentry)
409 { register setentry *entry;
410 PyObject *old_key;
412 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
413 if (entry == NULL)
414 return -1;
415 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;
425 static int
426 set_discard_key(PySetObject *so, PyObject *key)
428 register long hash;
429 register setentry *entry;
430 PyObject *old_key;
432 assert (PyAnySet_Check(so));
434 if (!PyUnicode_CheckExact(key) ||
435 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
436 hash = PyObject_Hash(key);
437 if (hash == -1)
438 return -1;
440 entry = (so->lookup)(so, key, hash);
441 if (entry == NULL)
442 return -1;
443 if (entry->key == NULL || entry->key == dummy)
444 return DISCARD_NOTFOUND;
445 old_key = entry->key;
446 Py_INCREF(dummy);
447 entry->key = dummy;
448 so->used--;
449 Py_DECREF(old_key);
450 return DISCARD_FOUND;
453 static int
454 set_clear_internal(PySetObject *so)
456 setentry *entry, *table;
457 int table_is_malloced;
458 Py_ssize_t fill;
459 setentry small_copy[PySet_MINSIZE];
460 #ifdef Py_DEBUG
461 Py_ssize_t i, n;
462 assert (PyAnySet_Check(so));
464 n = so->mask + 1;
465 i = 0;
466 #endif
468 table = so->table;
469 assert(table != NULL);
470 table_is_malloced = table != so->smalltable;
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
475 * clearing the slots, and never refer to anything via so->ref while
476 * clearing.
478 fill = so->fill;
479 if (table_is_malloced)
480 EMPTY_TO_MINSIZE(so);
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.
487 memcpy(small_copy, table, sizeof(small_copy));
488 table = small_copy;
489 EMPTY_TO_MINSIZE(so);
491 /* else it's a small table that's already empty */
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.
497 for (entry = table; fill > 0; ++entry) {
498 #ifdef Py_DEBUG
499 assert(i < n);
500 ++i;
501 #endif
502 if (entry->key) {
503 --fill;
504 Py_DECREF(entry->key);
506 #ifdef Py_DEBUG
507 else
508 assert(entry->key == NULL);
509 #endif
512 if (table_is_malloced)
513 PyMem_DEL(table);
514 return 0;
518 * Iterate over a set table. Use like so:
520 * Py_ssize_t pos;
521 * setentry *entry;
522 * pos = 0; # important! pos should not otherwise be changed by you
523 * while (set_next(yourset, &pos, &entry)) {
524 * Refer to borrowed reference in entry->key.
527 * CAUTION: In general, it isn't safe to use set_next in a loop that
528 * mutates the table.
530 static int
531 set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
533 Py_ssize_t i;
534 Py_ssize_t mask;
535 register setentry *table;
537 assert (PyAnySet_Check(so));
538 i = *pos_ptr;
539 assert(i >= 0);
540 table = so->table;
541 mask = so->mask;
542 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
543 i++;
544 *pos_ptr = i+1;
545 if (i > mask)
546 return 0;
547 assert(table[i].key != NULL);
548 *entry_ptr = &table[i];
549 return 1;
552 static void
553 set_dealloc(PySetObject *so)
555 register setentry *entry;
556 Py_ssize_t fill = so->fill;
557 PyObject_GC_UnTrack(so);
558 Py_TRASHCAN_SAFE_BEGIN(so)
559 if (so->weakreflist != NULL)
560 PyObject_ClearWeakRefs((PyObject *) so);
562 for (entry = so->table; fill > 0; entry++) {
563 if (entry->key) {
564 --fill;
565 Py_DECREF(entry->key);
568 if (so->table != so->smalltable)
569 PyMem_DEL(so->table);
570 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
571 free_list[numfree++] = so;
572 else
573 Py_TYPE(so)->tp_free(so);
574 Py_TRASHCAN_SAFE_END(so)
577 static PyObject *
578 set_repr(PySetObject *so)
580 PyObject *keys, *result=NULL;
581 Py_UNICODE *u;
582 int status = Py_ReprEnter((PyObject*)so);
583 PyObject *listrepr;
584 Py_ssize_t newsize;
586 if (status != 0) {
587 if (status < 0)
588 return NULL;
589 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
592 /* shortcut for the empty set */
593 if (!so->used) {
594 Py_ReprLeave((PyObject*)so);
595 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
598 keys = PySequence_List((PyObject *)so);
599 if (keys == NULL)
600 goto done;
602 listrepr = PyObject_Repr(keys);
603 Py_DECREF(keys);
604 if (listrepr == NULL) {
605 Py_DECREF(keys);
606 goto done;
608 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++ = '}';
619 Py_DECREF(listrepr);
620 if (Py_TYPE(so) != &PySet_Type) {
621 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
622 Py_TYPE(so)->tp_name,
623 result);
624 Py_DECREF(result);
625 result = tmp;
627 done:
628 Py_ReprLeave((PyObject*)so);
629 return result;
632 static Py_ssize_t
633 set_len(PyObject *so)
635 return ((PySetObject *)so)->used;
638 static int
639 set_merge(PySetObject *so, PyObject *otherset)
641 PySetObject *other;
642 register Py_ssize_t i;
643 register setentry *entry;
645 assert (PyAnySet_Check(so));
646 assert (PyAnySet_Check(otherset));
648 other = (PySetObject*)otherset;
649 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
653 * incrementally resizing as we insert new keys. Expect
654 * that there will be no (or few) overlapping keys.
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;
660 for (i = 0; i <= other->mask; i++) {
661 entry = &other->table[i];
662 if (entry->key != NULL &&
663 entry->key != dummy) {
664 Py_INCREF(entry->key);
665 if (set_insert_key(so, entry->key, entry->hash) == -1) {
666 Py_DECREF(entry->key);
667 return -1;
671 return 0;
674 static int
675 set_contains_key(PySetObject *so, PyObject *key)
677 long hash;
678 setentry *entry;
680 if (!PyUnicode_CheckExact(key) ||
681 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
682 hash = PyObject_Hash(key);
683 if (hash == -1)
684 return -1;
686 entry = (so->lookup)(so, key, hash);
687 if (entry == NULL)
688 return -1;
689 key = entry->key;
690 return key != NULL && key != dummy;
693 static int
694 set_contains_entry(PySetObject *so, setentry *entry)
696 PyObject *key;
697 setentry *lu_entry;
699 lu_entry = (so->lookup)(so, entry->key, entry->hash);
700 if (lu_entry == NULL)
701 return -1;
702 key = lu_entry->key;
703 return key != NULL && key != dummy;
706 static PyObject *
707 set_pop(PySetObject *so)
709 register Py_ssize_t i = 0;
710 register setentry *entry;
711 PyObject *key;
713 assert (PyAnySet_Check(so));
714 if (so->used == 0) {
715 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
716 return NULL;
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.
725 entry = &so->table[0];
726 if (entry->key == NULL || entry->key == dummy) {
727 i = entry->hash;
728 /* 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.
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;
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;
749 PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
750 Raises KeyError if the set is empty.");
752 static int
753 set_traverse(PySetObject *so, visitproc visit, void *arg)
755 Py_ssize_t pos = 0;
756 setentry *entry;
758 while (set_next(so, &pos, &entry))
759 Py_VISIT(entry->key);
760 return 0;
763 static long
764 frozenset_hash(PyObject *self)
766 PySetObject *so = (PySetObject *)self;
767 long h, hash = 1927868237L;
768 setentry *entry;
769 Py_ssize_t pos = 0;
771 if (so->hash != -1)
772 return so->hash;
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;
784 hash = hash * 69069L + 907133923L;
785 if (hash == -1)
786 hash = 590923713L;
787 so->hash = hash;
788 return hash;
791 /***** Set iterator type ***********************************************/
793 typedef struct {
794 PyObject_HEAD
795 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
796 Py_ssize_t si_used;
797 Py_ssize_t si_pos;
798 Py_ssize_t len;
799 } setiterobject;
801 static void
802 setiter_dealloc(setiterobject *si)
804 Py_XDECREF(si->si_set);
805 PyObject_GC_Del(si);
808 static int
809 setiter_traverse(setiterobject *si, visitproc visit, void *arg)
811 Py_VISIT(si->si_set);
812 return 0;
815 static PyObject *
816 setiter_len(setiterobject *si)
818 Py_ssize_t len = 0;
819 if (si->si_set != NULL && si->si_used == si->si_set->used)
820 len = si->len;
821 return PyLong_FromLong(len);
824 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
826 static PyMethodDef setiter_methods[] = {
827 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
828 {NULL, NULL} /* sentinel */
831 static PyObject *setiter_iternext(setiterobject *si)
833 PyObject *key;
834 register Py_ssize_t i, mask;
835 register setentry *entry;
836 PySetObject *so = si->si_set;
838 if (so == NULL)
839 return NULL;
840 assert (PyAnySet_Check(so));
842 if (si->si_used != so->used) {
843 PyErr_SetString(PyExc_RuntimeError,
844 "Set changed size during iteration");
845 si->si_used = -1; /* Make this state sticky */
846 return NULL;
849 i = si->si_pos;
850 assert(i>=0);
851 entry = so->table;
852 mask = so->mask;
853 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
854 i++;
855 si->si_pos = i+1;
856 if (i > mask)
857 goto fail;
858 si->len--;
859 key = entry[i].key;
860 Py_INCREF(key);
861 return key;
863 fail:
864 Py_DECREF(so);
865 si->si_set = NULL;
866 return NULL;
869 PyTypeObject PySetIter_Type = {
870 PyVarObject_HEAD_INIT(&PyType_Type, 0)
871 "set_iterator", /* tp_name */
872 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_reserved */
880 0, /* tp_repr */
881 0, /* tp_as_number */
882 0, /* tp_as_sequence */
883 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 */
890 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
891 0, /* tp_doc */
892 (traverseproc)setiter_traverse, /* tp_traverse */
893 0, /* tp_clear */
894 0, /* tp_richcompare */
895 0, /* tp_weaklistoffset */
896 PyObject_SelfIter, /* tp_iter */
897 (iternextfunc)setiter_iternext, /* tp_iternext */
898 setiter_methods, /* tp_methods */
902 static PyObject *
903 set_iter(PySetObject *so)
905 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
906 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;
913 _PyObject_GC_TRACK(si);
914 return (PyObject *)si;
917 static int
918 set_update_internal(PySetObject *so, PyObject *other)
920 PyObject *key, *it;
922 if (PyAnySet_Check(other))
923 return set_merge(so, other);
925 if (PyDict_CheckExact(other)) {
926 PyObject *value;
927 Py_ssize_t pos = 0;
928 long hash;
929 Py_ssize_t dictsize = PyDict_Size(other);
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.
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;
941 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
942 setentry an_entry;
944 an_entry.hash = hash;
945 an_entry.key = key;
946 if (set_add_entry(so, &an_entry) == -1)
947 return -1;
949 return 0;
952 it = PyObject_GetIter(other);
953 if (it == NULL)
954 return -1;
956 while ((key = PyIter_Next(it)) != NULL) {
957 if (set_add_key(so, key) == -1) {
958 Py_DECREF(it);
959 Py_DECREF(key);
960 return -1;
962 Py_DECREF(key);
964 Py_DECREF(it);
965 if (PyErr_Occurred())
966 return -1;
967 return 0;
970 static PyObject *
971 set_update(PySetObject *so, PyObject *args)
973 Py_ssize_t i;
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;
980 Py_RETURN_NONE;
983 PyDoc_STRVAR(update_doc,
984 "Update a set with the union of itself and others.");
986 static PyObject *
987 make_new_set(PyTypeObject *type, PyObject *iterable)
989 register PySetObject *so = NULL;
991 if (dummy == NULL) { /* Auto-initialize dummy */
992 dummy = PyUnicode_FromString("<dummy key>");
993 if (dummy == NULL)
994 return NULL;
997 /* create PySetObject structure */
998 if (numfree &&
999 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1000 so = free_list[--numfree];
1001 assert (so != NULL && PyAnySet_CheckExact(so));
1002 Py_TYPE(so) = type;
1003 _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);
1015 so->lookup = set_lookkey_unicode;
1016 so->weakreflist = NULL;
1018 if (iterable != NULL) {
1019 if (set_update_internal(so, iterable) == -1) {
1020 Py_DECREF(so);
1021 return NULL;
1025 return (PyObject *)so;
1028 static PyObject *
1029 make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
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;
1037 return make_new_set(type, iterable);
1040 /* The empty frozenset is a singleton */
1041 static PyObject *emptyfrozenset = NULL;
1043 static PyObject *
1044 frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1046 PyObject *iterable = NULL, *result;
1048 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1049 return NULL;
1051 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1052 return NULL;
1054 if (type != &PyFrozenSet_Type)
1055 return make_new_set(type, iterable);
1057 if (iterable != NULL) {
1058 /* frozenset(f) is idempotent */
1059 if (PyFrozenSet_CheckExact(iterable)) {
1060 Py_INCREF(iterable);
1061 return iterable;
1063 result = make_new_set(type, iterable);
1064 if (result == NULL || PySet_GET_SIZE(result))
1065 return result;
1066 Py_DECREF(result);
1068 /* The empty frozenset is a singleton */
1069 if (emptyfrozenset == NULL)
1070 emptyfrozenset = make_new_set(type, NULL);
1071 Py_XINCREF(emptyfrozenset);
1072 return emptyfrozenset;
1075 void
1076 PySet_Fini(void)
1078 PySetObject *so;
1080 while (numfree) {
1081 numfree--;
1082 so = free_list[numfree];
1083 PyObject_GC_Del(so);
1085 Py_CLEAR(dummy);
1086 Py_CLEAR(emptyfrozenset);
1089 static PyObject *
1090 set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1092 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1093 return NULL;
1095 return make_new_set(type, NULL);
1098 /* 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:
1102 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
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
1108 into one of the original inputs).
1111 static void
1112 set_swap_bodies(PySetObject *a, PySetObject *b)
1114 Py_ssize_t t;
1115 setentry *u;
1116 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1117 setentry tab[PySet_MINSIZE];
1118 long h;
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;
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;
1132 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
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));
1140 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1141 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1142 h = a->hash; a->hash = b->hash; b->hash = h;
1143 } else {
1144 a->hash = -1;
1145 b->hash = -1;
1149 static PyObject *
1150 set_copy(PySetObject *so)
1152 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
1155 static PyObject *
1156 frozenset_copy(PySetObject *so)
1158 if (PyFrozenSet_CheckExact(so)) {
1159 Py_INCREF(so);
1160 return (PyObject *)so;
1162 return set_copy(so);
1165 PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1167 static PyObject *
1168 set_clear(PySetObject *so)
1170 set_clear_internal(so);
1171 Py_RETURN_NONE;
1174 PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1176 static PyObject *
1177 set_union(PySetObject *so, PyObject *args)
1179 PySetObject *result;
1180 PyObject *other;
1181 Py_ssize_t i;
1183 result = (PySetObject *)set_copy(so);
1184 if (result == NULL)
1185 return NULL;
1187 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1188 other = PyTuple_GET_ITEM(args, i);
1189 if ((PyObject *)so == other)
1190 continue;
1191 if (set_update_internal(result, other) == -1) {
1192 Py_DECREF(result);
1193 return NULL;
1196 return (PyObject *)result;
1199 PyDoc_STRVAR(union_doc,
1200 "Return the union of sets as a new set.\n\
1202 (i.e. all elements that are in either set.)");
1204 static PyObject *
1205 set_or(PySetObject *so, PyObject *other)
1207 PySetObject *result;
1209 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1210 Py_INCREF(Py_NotImplemented);
1211 return Py_NotImplemented;
1214 result = (PySetObject *)set_copy(so);
1215 if (result == NULL)
1216 return NULL;
1217 if ((PyObject *)so == other)
1218 return (PyObject *)result;
1219 if (set_update_internal(result, other) == -1) {
1220 Py_DECREF(result);
1221 return NULL;
1223 return (PyObject *)result;
1226 static PyObject *
1227 set_ior(PySetObject *so, PyObject *other)
1229 if (!PyAnySet_Check(other)) {
1230 Py_INCREF(Py_NotImplemented);
1231 return Py_NotImplemented;
1233 if (set_update_internal(so, other) == -1)
1234 return NULL;
1235 Py_INCREF(so);
1236 return (PyObject *)so;
1239 static PyObject *
1240 set_intersection(PySetObject *so, PyObject *other)
1242 PySetObject *result;
1243 PyObject *key, *it, *tmp;
1245 if ((PyObject *)so == other)
1246 return set_copy(so);
1248 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1249 if (result == NULL)
1250 return NULL;
1252 if (PyAnySet_Check(other)) {
1253 Py_ssize_t pos = 0;
1254 setentry *entry;
1256 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1257 tmp = (PyObject *)so;
1258 so = (PySetObject *)other;
1259 other = tmp;
1262 while (set_next((PySetObject *)other, &pos, &entry)) {
1263 int rv = set_contains_entry(so, entry);
1264 if (rv == -1) {
1265 Py_DECREF(result);
1266 return NULL;
1268 if (rv) {
1269 if (set_add_entry(result, entry) == -1) {
1270 Py_DECREF(result);
1271 return NULL;
1275 return (PyObject *)result;
1278 it = PyObject_GetIter(other);
1279 if (it == NULL) {
1280 Py_DECREF(result);
1281 return NULL;
1284 while ((key = PyIter_Next(it)) != NULL) {
1285 int rv;
1286 setentry entry;
1287 long hash = PyObject_Hash(key);
1289 if (hash == -1) {
1290 Py_DECREF(it);
1291 Py_DECREF(result);
1292 Py_DECREF(key);
1293 return NULL;
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;
1304 if (rv) {
1305 if (set_add_entry(result, &entry) == -1) {
1306 Py_DECREF(it);
1307 Py_DECREF(result);
1308 Py_DECREF(key);
1309 return NULL;
1312 Py_DECREF(key);
1314 Py_DECREF(it);
1315 if (PyErr_Occurred()) {
1316 Py_DECREF(result);
1317 return NULL;
1319 return (PyObject *)result;
1322 static PyObject *
1323 set_intersection_multi(PySetObject *so, PyObject *args)
1325 Py_ssize_t i;
1326 PyObject *result = (PyObject *)so;
1328 if (PyTuple_GET_SIZE(args) == 0)
1329 return set_copy(so);
1331 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;
1339 Py_DECREF(result);
1340 result = newresult;
1342 return result;
1345 PyDoc_STRVAR(intersection_doc,
1346 "Return the intersection of two or more sets as a new set.\n\
1348 (i.e. elements that are common to all of the sets.)");
1350 static PyObject *
1351 set_intersection_update(PySetObject *so, PyObject *other)
1353 PyObject *tmp;
1355 tmp = set_intersection(so, other);
1356 if (tmp == NULL)
1357 return NULL;
1358 set_swap_bodies(so, (PySetObject *)tmp);
1359 Py_DECREF(tmp);
1360 Py_RETURN_NONE;
1363 static PyObject *
1364 set_intersection_update_multi(PySetObject *so, PyObject *args)
1366 PyObject *tmp;
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;
1376 PyDoc_STRVAR(intersection_update_doc,
1377 "Update a set with the intersection of itself and another.");
1379 static PyObject *
1380 set_and(PySetObject *so, PyObject *other)
1382 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1383 Py_INCREF(Py_NotImplemented);
1384 return Py_NotImplemented;
1386 return set_intersection(so, other);
1389 static PyObject *
1390 set_iand(PySetObject *so, PyObject *other)
1392 PyObject *result;
1394 if (!PyAnySet_Check(other)) {
1395 Py_INCREF(Py_NotImplemented);
1396 return Py_NotImplemented;
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;
1406 static PyObject *
1407 set_isdisjoint(PySetObject *so, PyObject *other)
1409 PyObject *key, *it, *tmp;
1411 if ((PyObject *)so == other) {
1412 if (PySet_GET_SIZE(so) == 0)
1413 Py_RETURN_TRUE;
1414 else
1415 Py_RETURN_FALSE;
1418 if (PyAnySet_CheckExact(other)) {
1419 Py_ssize_t pos = 0;
1420 setentry *entry;
1422 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1423 tmp = (PyObject *)so;
1424 so = (PySetObject *)other;
1425 other = tmp;
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;
1434 Py_RETURN_TRUE;
1437 it = PyObject_GetIter(other);
1438 if (it == NULL)
1439 return NULL;
1441 while ((key = PyIter_Next(it)) != NULL) {
1442 int rv;
1443 setentry entry;
1444 long hash = PyObject_Hash(key);;
1446 if (hash == -1) {
1447 Py_DECREF(key);
1448 Py_DECREF(it);
1449 return NULL;
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;
1459 if (rv) {
1460 Py_DECREF(it);
1461 Py_RETURN_FALSE;
1464 Py_DECREF(it);
1465 if (PyErr_Occurred())
1466 return NULL;
1467 Py_RETURN_TRUE;
1470 PyDoc_STRVAR(isdisjoint_doc,
1471 "Return True if two sets have a null intersection.");
1473 static int
1474 set_difference_update_internal(PySetObject *so, PyObject *other)
1476 if ((PyObject *)so == other)
1477 return set_clear_internal(so);
1479 if (PyAnySet_Check(other)) {
1480 setentry *entry;
1481 Py_ssize_t pos = 0;
1483 while (set_next((PySetObject *)other, &pos, &entry))
1484 if (set_discard_entry(so, entry) == -1)
1485 return -1;
1486 } else {
1487 PyObject *key, *it;
1488 it = PyObject_GetIter(other);
1489 if (it == NULL)
1490 return -1;
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;
1498 Py_DECREF(key);
1500 Py_DECREF(it);
1501 if (PyErr_Occurred())
1502 return -1;
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);
1510 static PyObject *
1511 set_difference_update(PySetObject *so, PyObject *args)
1513 Py_ssize_t i;
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;
1520 Py_RETURN_NONE;
1523 PyDoc_STRVAR(difference_update_doc,
1524 "Remove all elements of another set from this set.");
1526 static PyObject *
1527 set_difference(PySetObject *so, PyObject *other)
1529 PyObject *result;
1530 setentry *entry;
1531 Py_ssize_t pos = 0;
1533 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
1534 result = set_copy(so);
1535 if (result == NULL)
1536 return NULL;
1537 if (set_difference_update_internal((PySetObject *)result, other) != -1)
1538 return result;
1539 Py_DECREF(result);
1540 return NULL;
1543 result = make_new_set_basetype(Py_TYPE(so), NULL);
1544 if (result == NULL)
1545 return NULL;
1547 if (PyDict_CheckExact(other)) {
1548 while (set_next(so, &pos, &entry)) {
1549 setentry entrycopy;
1550 entrycopy.hash = entry->hash;
1551 entrycopy.key = entry->key;
1552 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
1553 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1554 Py_DECREF(result);
1555 return NULL;
1559 return result;
1562 while (set_next(so, &pos, &entry)) {
1563 int rv = set_contains_entry((PySetObject *)other, entry);
1564 if (rv == -1) {
1565 Py_DECREF(result);
1566 return NULL;
1568 if (!rv) {
1569 if (set_add_entry((PySetObject *)result, entry) == -1) {
1570 Py_DECREF(result);
1571 return NULL;
1575 return result;
1578 static PyObject *
1579 set_difference_multi(PySetObject *so, PyObject *args)
1581 Py_ssize_t i;
1582 PyObject *result, *other;
1584 if (PyTuple_GET_SIZE(args) == 0)
1585 return set_copy(so);
1587 other = PyTuple_GET_ITEM(args, 0);
1588 result = set_difference(so, other);
1589 if (result == NULL)
1590 return NULL;
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;
1599 return result;
1602 PyDoc_STRVAR(difference_doc,
1603 "Return the difference of two or more sets as a new set.\n\
1605 (i.e. all elements that are in this set but not the others.)");
1606 static PyObject *
1607 set_sub(PySetObject *so, PyObject *other)
1609 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1610 Py_INCREF(Py_NotImplemented);
1611 return Py_NotImplemented;
1613 return set_difference(so, other);
1616 static PyObject *
1617 set_isub(PySetObject *so, PyObject *other)
1619 if (!PyAnySet_Check(other)) {
1620 Py_INCREF(Py_NotImplemented);
1621 return Py_NotImplemented;
1623 if (set_difference_update_internal(so, other) == -1)
1624 return NULL;
1625 Py_INCREF(so);
1626 return (PyObject *)so;
1629 static PyObject *
1630 set_symmetric_difference_update(PySetObject *so, PyObject *other)
1632 PySetObject *otherset;
1633 PyObject *key;
1634 Py_ssize_t pos = 0;
1635 setentry *entry;
1637 if ((PyObject *)so == other)
1638 return set_clear(so);
1640 if (PyDict_CheckExact(other)) {
1641 PyObject *value;
1642 int rv;
1643 long hash;
1644 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1645 setentry an_entry;
1647 an_entry.hash = hash;
1648 an_entry.key = key;
1649 rv = set_discard_entry(so, &an_entry);
1650 if (rv == -1)
1651 return NULL;
1652 if (rv == DISCARD_NOTFOUND) {
1653 if (set_add_entry(so, &an_entry) == -1)
1654 return NULL;
1657 Py_RETURN_NONE;
1660 if (PyAnySet_Check(other)) {
1661 Py_INCREF(other);
1662 otherset = (PySetObject *)other;
1663 } else {
1664 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1665 if (otherset == NULL)
1666 return NULL;
1669 while (set_next(otherset, &pos, &entry)) {
1670 int rv = set_discard_entry(so, entry);
1671 if (rv == -1) {
1672 Py_DECREF(otherset);
1673 return NULL;
1675 if (rv == DISCARD_NOTFOUND) {
1676 if (set_add_entry(so, entry) == -1) {
1677 Py_DECREF(otherset);
1678 return NULL;
1682 Py_DECREF(otherset);
1683 Py_RETURN_NONE;
1686 PyDoc_STRVAR(symmetric_difference_update_doc,
1687 "Update a set with the symmetric difference of itself and another.");
1689 static PyObject *
1690 set_symmetric_difference(PySetObject *so, PyObject *other)
1692 PyObject *rv;
1693 PySetObject *otherset;
1695 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1696 if (otherset == NULL)
1697 return NULL;
1698 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1699 if (rv == NULL)
1700 return NULL;
1701 Py_DECREF(rv);
1702 return (PyObject *)otherset;
1705 PyDoc_STRVAR(symmetric_difference_doc,
1706 "Return the symmetric difference of two sets as a new set.\n\
1708 (i.e. all elements that are in exactly one of the sets.)");
1710 static PyObject *
1711 set_xor(PySetObject *so, PyObject *other)
1713 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1714 Py_INCREF(Py_NotImplemented);
1715 return Py_NotImplemented;
1717 return set_symmetric_difference(so, other);
1720 static PyObject *
1721 set_ixor(PySetObject *so, PyObject *other)
1723 PyObject *result;
1725 if (!PyAnySet_Check(other)) {
1726 Py_INCREF(Py_NotImplemented);
1727 return Py_NotImplemented;
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;
1737 static PyObject *
1738 set_issubset(PySetObject *so, PyObject *other)
1740 setentry *entry;
1741 Py_ssize_t pos = 0;
1743 if (!PyAnySet_Check(other)) {
1744 PyObject *tmp, *result;
1745 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;
1752 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1753 Py_RETURN_FALSE;
1755 while (set_next(so, &pos, &entry)) {
1756 int rv = set_contains_entry((PySetObject *)other, entry);
1757 if (rv == -1)
1758 return NULL;
1759 if (!rv)
1760 Py_RETURN_FALSE;
1762 Py_RETURN_TRUE;
1765 PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1767 static PyObject *
1768 set_issuperset(PySetObject *so, PyObject *other)
1770 PyObject *tmp, *result;
1772 if (!PyAnySet_Check(other)) {
1773 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;
1780 return set_issubset((PySetObject *)other, (PyObject *)so);
1783 PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1785 static PyObject *
1786 set_richcompare(PySetObject *v, PyObject *w, int op)
1788 PyObject *r1, *r2;
1790 if(!PyAnySet_Check(w)) {
1791 Py_INCREF(Py_NotImplemented);
1792 return Py_NotImplemented;
1794 switch (op) {
1795 case Py_EQ:
1796 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1797 Py_RETURN_FALSE;
1798 if (v->hash != -1 &&
1799 ((PySetObject *)w)->hash != -1 &&
1800 v->hash != ((PySetObject *)w)->hash)
1801 Py_RETURN_FALSE;
1802 return set_issubset(v, w);
1803 case Py_NE:
1804 r1 = set_richcompare(v, w, Py_EQ);
1805 if (r1 == NULL)
1806 return NULL;
1807 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:
1815 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1816 Py_RETURN_FALSE;
1817 return set_issubset(v, w);
1818 case Py_GT:
1819 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1820 Py_RETURN_FALSE;
1821 return set_issuperset(v, w);
1823 Py_INCREF(Py_NotImplemented);
1824 return Py_NotImplemented;
1827 static PyObject *
1828 set_add(PySetObject *so, PyObject *key)
1830 if (set_add_key(so, key) == -1)
1831 return NULL;
1832 Py_RETURN_NONE;
1835 PyDoc_STRVAR(add_doc,
1836 "Add an element to a set.\n\
1838 This has no effect if the element is already present.");
1840 static int
1841 set_contains(PySetObject *so, PyObject *key)
1843 PyObject *tmpkey;
1844 int rv;
1846 rv = set_contains_key(so, key);
1847 if (rv == -1) {
1848 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1849 return -1;
1850 PyErr_Clear();
1851 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1852 if (tmpkey == NULL)
1853 return -1;
1854 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1855 rv = set_contains(so, tmpkey);
1856 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1857 Py_DECREF(tmpkey);
1859 return rv;
1862 static PyObject *
1863 set_direct_contains(PySetObject *so, PyObject *key)
1865 long result;
1867 result = set_contains(so, key);
1868 if (result == -1)
1869 return NULL;
1870 return PyBool_FromLong(result);
1873 PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1875 static PyObject *
1876 set_remove(PySetObject *so, PyObject *key)
1878 PyObject *tmpkey;
1879 int rv;
1881 rv = set_discard_key(so, key);
1882 if (rv == -1) {
1883 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1884 return NULL;
1885 PyErr_Clear();
1886 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1887 if (tmpkey == NULL)
1888 return NULL;
1889 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1890 rv = set_discard_key(so, tmpkey);
1891 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1892 Py_DECREF(tmpkey);
1893 if (rv == -1)
1894 return NULL;
1897 if (rv == DISCARD_NOTFOUND) {
1898 set_key_error(key);
1899 return NULL;
1901 Py_RETURN_NONE;
1904 PyDoc_STRVAR(remove_doc,
1905 "Remove an element from a set; it must be a member.\n\
1907 If the element is not a member, raise a KeyError.");
1909 static PyObject *
1910 set_discard(PySetObject *so, PyObject *key)
1912 PyObject *tmpkey, *result;
1913 int rv;
1915 rv = set_discard_key(so, key);
1916 if (rv == -1) {
1917 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1918 return NULL;
1919 PyErr_Clear();
1920 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1921 if (tmpkey == NULL)
1922 return NULL;
1923 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1924 result = set_discard(so, tmpkey);
1925 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1926 Py_DECREF(tmpkey);
1927 return result;
1929 Py_RETURN_NONE;
1932 PyDoc_STRVAR(discard_doc,
1933 "Remove an element from a set if it is a member.\n\
1935 If the element is not a member, do nothing.");
1937 static PyObject *
1938 set_reduce(PySetObject *so)
1940 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
1942 keys = PySequence_List((PyObject *)so);
1943 if (keys == NULL)
1944 goto done;
1945 args = PyTuple_Pack(1, keys);
1946 if (args == NULL)
1947 goto done;
1948 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1949 if (dict == NULL) {
1950 PyErr_Clear();
1951 dict = Py_None;
1952 Py_INCREF(dict);
1954 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
1955 done:
1956 Py_XDECREF(args);
1957 Py_XDECREF(keys);
1958 Py_XDECREF(dict);
1959 return result;
1962 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1964 static PyObject *
1965 set_sizeof(PySetObject *so)
1967 Py_ssize_t res;
1969 res = sizeof(PySetObject);
1970 if (so->table != so->smalltable)
1971 res = res + (so->mask + 1) * sizeof(setentry);
1972 return PyLong_FromSsize_t(res);
1975 PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
1976 static int
1977 set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1979 PyObject *iterable = NULL;
1981 if (!PyAnySet_Check(self))
1982 return -1;
1983 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1984 return -1;
1985 set_clear_internal(self);
1986 self->hash = -1;
1987 if (iterable == NULL)
1988 return 0;
1989 return set_update_internal(self, iterable);
1992 static PySequenceMethods set_as_sequence = {
1993 set_len, /* sq_length */
1994 0, /* sq_concat */
1995 0, /* sq_repeat */
1996 0, /* sq_item */
1997 0, /* sq_slice */
1998 0, /* sq_ass_item */
1999 0, /* sq_ass_slice */
2000 (objobjproc)set_contains, /* sq_contains */
2003 /* set object ********************************************************/
2005 #ifdef Py_DEBUG
2006 static PyObject *test_c_api(PySetObject *so);
2008 PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2009 All is well if assertions don't fail.");
2010 #endif
2012 static PyMethodDef set_methods[] = {
2013 {"add", (PyCFunction)set_add, METH_O,
2014 add_doc},
2015 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2016 clear_doc},
2017 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2018 contains_doc},
2019 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2020 copy_doc},
2021 {"discard", (PyCFunction)set_discard, METH_O,
2022 discard_doc},
2023 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2024 difference_doc},
2025 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2026 difference_update_doc},
2027 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2028 intersection_doc},
2029 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2030 intersection_update_doc},
2031 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2032 isdisjoint_doc},
2033 {"issubset", (PyCFunction)set_issubset, METH_O,
2034 issubset_doc},
2035 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2036 issuperset_doc},
2037 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2038 pop_doc},
2039 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2040 reduce_doc},
2041 {"remove", (PyCFunction)set_remove, METH_O,
2042 remove_doc},
2043 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2044 sizeof_doc},
2045 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2046 symmetric_difference_doc},
2047 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2048 symmetric_difference_update_doc},
2049 #ifdef Py_DEBUG
2050 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2051 test_c_api_doc},
2052 #endif
2053 {"union", (PyCFunction)set_union, METH_VARARGS,
2054 union_doc},
2055 {"update", (PyCFunction)set_update, METH_VARARGS,
2056 update_doc},
2057 {NULL, NULL} /* sentinel */
2060 static PyNumberMethods set_as_number = {
2061 0, /*nb_add*/
2062 (binaryfunc)set_sub, /*nb_subtract*/
2063 0, /*nb_multiply*/
2064 0, /*nb_remainder*/
2065 0, /*nb_divmod*/
2066 0, /*nb_power*/
2067 0, /*nb_negative*/
2068 0, /*nb_positive*/
2069 0, /*nb_absolute*/
2070 0, /*nb_bool*/
2071 0, /*nb_invert*/
2072 0, /*nb_lshift*/
2073 0, /*nb_rshift*/
2074 (binaryfunc)set_and, /*nb_and*/
2075 (binaryfunc)set_xor, /*nb_xor*/
2076 (binaryfunc)set_or, /*nb_or*/
2077 0, /*nb_int*/
2078 0, /*nb_reserved*/
2079 0, /*nb_float*/
2080 0, /*nb_inplace_add*/
2081 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2082 0, /*nb_inplace_multiply*/
2083 0, /*nb_inplace_remainder*/
2084 0, /*nb_inplace_power*/
2085 0, /*nb_inplace_lshift*/
2086 0, /*nb_inplace_rshift*/
2087 (binaryfunc)set_iand, /*nb_inplace_and*/
2088 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2089 (binaryfunc)set_ior, /*nb_inplace_or*/
2092 PyDoc_STRVAR(set_doc,
2093 "set() -> new empty set object\n\
2094 set(iterable) -> new set object\n\
2096 Build an unordered collection of unique elements.");
2098 PyTypeObject PySet_Type = {
2099 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2100 "set", /* tp_name */
2101 sizeof(PySetObject), /* tp_basicsize */
2102 0, /* tp_itemsize */
2103 /* methods */
2104 (destructor)set_dealloc, /* tp_dealloc */
2105 0, /* tp_print */
2106 0, /* tp_getattr */
2107 0, /* tp_setattr */
2108 0, /* tp_reserved */
2109 (reprfunc)set_repr, /* tp_repr */
2110 &set_as_number, /* tp_as_number */
2111 &set_as_sequence, /* tp_as_sequence */
2112 0, /* tp_as_mapping */
2113 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2114 0, /* tp_call */
2115 0, /* tp_str */
2116 PyObject_GenericGetAttr, /* tp_getattro */
2117 0, /* tp_setattro */
2118 0, /* tp_as_buffer */
2119 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2120 Py_TPFLAGS_BASETYPE, /* tp_flags */
2121 set_doc, /* tp_doc */
2122 (traverseproc)set_traverse, /* tp_traverse */
2123 (inquiry)set_clear_internal, /* tp_clear */
2124 (richcmpfunc)set_richcompare, /* tp_richcompare */
2125 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2126 (getiterfunc)set_iter, /* tp_iter */
2127 0, /* tp_iternext */
2128 set_methods, /* tp_methods */
2129 0, /* tp_members */
2130 0, /* tp_getset */
2131 0, /* tp_base */
2132 0, /* tp_dict */
2133 0, /* tp_descr_get */
2134 0, /* tp_descr_set */
2135 0, /* tp_dictoffset */
2136 (initproc)set_init, /* tp_init */
2137 PyType_GenericAlloc, /* tp_alloc */
2138 set_new, /* tp_new */
2139 PyObject_GC_Del, /* tp_free */
2142 /* frozenset object ********************************************************/
2145 static PyMethodDef frozenset_methods[] = {
2146 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2147 contains_doc},
2148 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2149 copy_doc},
2150 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2151 difference_doc},
2152 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2153 intersection_doc},
2154 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2155 isdisjoint_doc},
2156 {"issubset", (PyCFunction)set_issubset, METH_O,
2157 issubset_doc},
2158 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2159 issuperset_doc},
2160 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2161 reduce_doc},
2162 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2163 sizeof_doc},
2164 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2165 symmetric_difference_doc},
2166 {"union", (PyCFunction)set_union, METH_VARARGS,
2167 union_doc},
2168 {NULL, NULL} /* sentinel */
2171 static PyNumberMethods frozenset_as_number = {
2172 0, /*nb_add*/
2173 (binaryfunc)set_sub, /*nb_subtract*/
2174 0, /*nb_multiply*/
2175 0, /*nb_remainder*/
2176 0, /*nb_divmod*/
2177 0, /*nb_power*/
2178 0, /*nb_negative*/
2179 0, /*nb_positive*/
2180 0, /*nb_absolute*/
2181 0, /*nb_bool*/
2182 0, /*nb_invert*/
2183 0, /*nb_lshift*/
2184 0, /*nb_rshift*/
2185 (binaryfunc)set_and, /*nb_and*/
2186 (binaryfunc)set_xor, /*nb_xor*/
2187 (binaryfunc)set_or, /*nb_or*/
2190 PyDoc_STRVAR(frozenset_doc,
2191 "frozenset() -> empty frozenset object\n\
2192 frozenset(iterable) -> frozenset object\n\
2194 Build an immutable unordered collection of unique elements.");
2196 PyTypeObject PyFrozenSet_Type = {
2197 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2198 "frozenset", /* tp_name */
2199 sizeof(PySetObject), /* tp_basicsize */
2200 0, /* tp_itemsize */
2201 /* methods */
2202 (destructor)set_dealloc, /* tp_dealloc */
2203 0, /* tp_print */
2204 0, /* tp_getattr */
2205 0, /* tp_setattr */
2206 0, /* tp_reserved */
2207 (reprfunc)set_repr, /* tp_repr */
2208 &frozenset_as_number, /* tp_as_number */
2209 &set_as_sequence, /* tp_as_sequence */
2210 0, /* tp_as_mapping */
2211 frozenset_hash, /* tp_hash */
2212 0, /* tp_call */
2213 0, /* tp_str */
2214 PyObject_GenericGetAttr, /* tp_getattro */
2215 0, /* tp_setattro */
2216 0, /* tp_as_buffer */
2217 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2218 Py_TPFLAGS_BASETYPE, /* tp_flags */
2219 frozenset_doc, /* tp_doc */
2220 (traverseproc)set_traverse, /* tp_traverse */
2221 (inquiry)set_clear_internal, /* tp_clear */
2222 (richcmpfunc)set_richcompare, /* tp_richcompare */
2223 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2224 (getiterfunc)set_iter, /* tp_iter */
2225 0, /* tp_iternext */
2226 frozenset_methods, /* tp_methods */
2227 0, /* tp_members */
2228 0, /* tp_getset */
2229 0, /* tp_base */
2230 0, /* tp_dict */
2231 0, /* tp_descr_get */
2232 0, /* tp_descr_set */
2233 0, /* tp_dictoffset */
2234 0, /* tp_init */
2235 PyType_GenericAlloc, /* tp_alloc */
2236 frozenset_new, /* tp_new */
2237 PyObject_GC_Del, /* tp_free */
2241 /***** C API functions *************************************************/
2243 PyObject *
2244 PySet_New(PyObject *iterable)
2246 return make_new_set(&PySet_Type, iterable);
2249 PyObject *
2250 PyFrozenSet_New(PyObject *iterable)
2252 return make_new_set(&PyFrozenSet_Type, iterable);
2255 Py_ssize_t
2256 PySet_Size(PyObject *anyset)
2258 if (!PyAnySet_Check(anyset)) {
2259 PyErr_BadInternalCall();
2260 return -1;
2262 return PySet_GET_SIZE(anyset);
2266 PySet_Clear(PyObject *set)
2268 if (!PySet_Check(set)) {
2269 PyErr_BadInternalCall();
2270 return -1;
2272 return set_clear_internal((PySetObject *)set);
2276 PySet_Contains(PyObject *anyset, PyObject *key)
2278 if (!PyAnySet_Check(anyset)) {
2279 PyErr_BadInternalCall();
2280 return -1;
2282 return set_contains_key((PySetObject *)anyset, key);
2286 PySet_Discard(PyObject *set, PyObject *key)
2288 if (!PySet_Check(set)) {
2289 PyErr_BadInternalCall();
2290 return -1;
2292 return set_discard_key((PySetObject *)set, key);
2296 PySet_Add(PyObject *anyset, PyObject *key)
2298 if (!PySet_Check(anyset) &&
2299 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2300 PyErr_BadInternalCall();
2301 return -1;
2303 return set_add_key((PySetObject *)anyset, key);
2307 _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2309 setentry *entry;
2311 if (!PyAnySet_Check(set)) {
2312 PyErr_BadInternalCall();
2313 return -1;
2315 if (set_next((PySetObject *)set, pos, &entry) == 0)
2316 return 0;
2317 *key = entry->key;
2318 *hash = entry->hash;
2319 return 1;
2322 PyObject *
2323 PySet_Pop(PyObject *set)
2325 if (!PySet_Check(set)) {
2326 PyErr_BadInternalCall();
2327 return NULL;
2329 return set_pop((PySetObject *)set);
2333 _PySet_Update(PyObject *set, PyObject *iterable)
2335 if (!PySet_Check(set)) {
2336 PyErr_BadInternalCall();
2337 return -1;
2339 return set_update_internal((PySetObject *)set, iterable);
2342 #ifdef Py_DEBUG
2344 /* Test code to be called with any three element set.
2345 Returns True and original set is restored. */
2347 #define assertRaises(call_return_value, exception) \
2348 do { \
2349 assert(call_return_value); \
2350 assert(PyErr_ExceptionMatches(exception)); \
2351 PyErr_Clear(); \
2352 } while(0)
2354 static PyObject *
2355 test_c_api(PySetObject *so)
2357 Py_ssize_t count;
2358 char *s;
2359 Py_ssize_t i;
2360 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2361 PyObject *ob = (PyObject *)so;
2362 long hash;
2364 /* Verify preconditions and exercise type/size checks */
2365 assert(PyAnySet_Check(ob));
2366 assert(PyAnySet_CheckExact(ob));
2367 assert(!PyFrozenSet_CheckExact(ob));
2368 assert(PySet_Size(ob) == 3);
2369 assert(PySet_GET_SIZE(ob) == 3);
2371 /* Raise TypeError for non-iterable constructor arguments */
2372 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2373 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2375 /* Raise TypeError for unhashable key */
2376 dup = PySet_New(ob);
2377 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2378 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2379 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2381 /* Exercise successful pop, contains, add, and discard */
2382 elem = PySet_Pop(ob);
2383 assert(PySet_Contains(ob, elem) == 0);
2384 assert(PySet_GET_SIZE(ob) == 2);
2385 assert(PySet_Add(ob, elem) == 0);
2386 assert(PySet_Contains(ob, elem) == 1);
2387 assert(PySet_GET_SIZE(ob) == 3);
2388 assert(PySet_Discard(ob, elem) == 1);
2389 assert(PySet_GET_SIZE(ob) == 2);
2390 assert(PySet_Discard(ob, elem) == 0);
2391 assert(PySet_GET_SIZE(ob) == 2);
2393 /* Exercise clear */
2394 dup2 = PySet_New(dup);
2395 assert(PySet_Clear(dup2) == 0);
2396 assert(PySet_Size(dup2) == 0);
2397 Py_DECREF(dup2);
2399 /* Raise SystemError on clear or update of frozen set */
2400 f = PyFrozenSet_New(dup);
2401 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2402 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2403 assert(PySet_Add(f, elem) == 0);
2404 Py_INCREF(f);
2405 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2406 Py_DECREF(f);
2407 Py_DECREF(f);
2409 /* Exercise direct iteration */
2410 i = 0, count = 0;
2411 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2412 s = _PyUnicode_AsString(x);
2413 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2414 count++;
2416 assert(count == 3);
2418 /* Exercise updates */
2419 dup2 = PySet_New(NULL);
2420 assert(_PySet_Update(dup2, dup) == 0);
2421 assert(PySet_Size(dup2) == 3);
2422 assert(_PySet_Update(dup2, dup) == 0);
2423 assert(PySet_Size(dup2) == 3);
2424 Py_DECREF(dup2);
2426 /* Raise SystemError when self argument is not a set or frozenset. */
2427 t = PyTuple_New(0);
2428 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2429 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2430 Py_DECREF(t);
2432 /* Raise SystemError when self argument is not a set. */
2433 f = PyFrozenSet_New(dup);
2434 assert(PySet_Size(f) == 3);
2435 assert(PyFrozenSet_CheckExact(f));
2436 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2437 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2438 Py_DECREF(f);
2440 /* Raise KeyError when popping from an empty set */
2441 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2442 Py_DECREF(ob);
2443 assert(PySet_GET_SIZE(ob) == 0);
2444 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2446 /* Restore the set from the copy using the PyNumber API */
2447 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2448 Py_DECREF(ob);
2450 /* Verify constructors accept NULL arguments */
2451 f = PySet_New(NULL);
2452 assert(f != NULL);
2453 assert(PySet_GET_SIZE(f) == 0);
2454 Py_DECREF(f);
2455 f = PyFrozenSet_New(NULL);
2456 assert(f != NULL);
2457 assert(PyFrozenSet_CheckExact(f));
2458 assert(PySet_GET_SIZE(f) == 0);
2459 Py_DECREF(f);
2461 Py_DECREF(elem);
2462 Py_DECREF(dup);
2463 Py_RETURN_TRUE;
2466 #undef assertRaises
2468 #endif