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-2007 Python Software Foundation.
11 #include "structmember.h"
13 /* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
17 set_key_error(PyObject
*arg
)
20 tup
= PyTuple_Pack(1, arg
);
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError
, tup
);
27 /* This must be >= 1. */
28 #define PERTURB_SHIFT 5
30 /* Object used as dummy key to fill deleted entries */
31 static PyObject
*dummy
= NULL
; /* Initialized by first call to make_new_set() */
41 #define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
47 #define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
50 INIT_NONZERO_SET_SLOTS(so); \
53 /* Reuse scheme to save calls to malloc, free, and memset */
54 #ifndef PySet_MAXFREELIST
55 #define PySet_MAXFREELIST 80
57 static PySetObject
*free_list
[PySet_MAXFREELIST
];
58 static int numfree
= 0;
61 The basic lookup function used by all operations.
62 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
63 Open addressing is preferred over chaining since the link overhead for
64 chaining would be substantial (100% with typical malloc overhead).
66 The initial probe index is computed as hash mod the table size. Subsequent
67 probe indices are computed as explained in Objects/dictobject.c.
69 All arithmetic on hash should ignore overflow.
71 Unlike the dictionary implementation, the lookkey functions can return
72 NULL if the rich comparison returns an error.
76 set_lookkey(PySetObject
*so
, PyObject
*key
, register long hash
)
78 register Py_ssize_t i
;
79 register size_t perturb
;
80 register setentry
*freeslot
;
81 register size_t mask
= so
->mask
;
82 setentry
*table
= so
->table
;
83 register setentry
*entry
;
89 if (entry
->key
== NULL
|| entry
->key
== key
)
92 if (entry
->key
== dummy
)
95 if (entry
->hash
== hash
) {
96 startkey
= entry
->key
;
98 cmp
= PyObject_RichCompareBool(startkey
, key
, Py_EQ
);
102 if (table
== so
->table
&& entry
->key
== startkey
) {
107 /* The compare did major nasty stuff to the
110 return set_lookkey(so
, key
, hash
);
116 /* In the loop, key == dummy is by far (factor of 100s) the
117 least likely outcome, so test for that last. */
118 for (perturb
= hash
; ; perturb
>>= PERTURB_SHIFT
) {
119 i
= (i
<< 2) + i
+ perturb
+ 1;
120 entry
= &table
[i
& mask
];
121 if (entry
->key
== NULL
) {
122 if (freeslot
!= NULL
)
126 if (entry
->key
== key
)
128 if (entry
->hash
== hash
&& entry
->key
!= dummy
) {
129 startkey
= entry
->key
;
131 cmp
= PyObject_RichCompareBool(startkey
, key
, Py_EQ
);
135 if (table
== so
->table
&& entry
->key
== startkey
) {
140 /* The compare did major nasty stuff to the
143 return set_lookkey(so
, key
, hash
);
146 else if (entry
->key
== dummy
&& freeslot
== NULL
)
153 * Hacked up version of set_lookkey which can assume keys are always strings;
154 * This means we can always use _PyString_Eq directly and not have to check to
155 * see if the comparison altered the table.
158 set_lookkey_string(PySetObject
*so
, PyObject
*key
, register long hash
)
160 register Py_ssize_t i
;
161 register size_t perturb
;
162 register setentry
*freeslot
;
163 register size_t mask
= so
->mask
;
164 setentry
*table
= so
->table
;
165 register setentry
*entry
;
167 /* Make sure this function doesn't have to handle non-string keys,
168 including subclasses of str; e.g., one reason to subclass
169 strings is to override __eq__, and for speed we don't cater to
171 if (!PyString_CheckExact(key
)) {
172 so
->lookup
= set_lookkey
;
173 return set_lookkey(so
, key
, hash
);
177 if (entry
->key
== NULL
|| entry
->key
== key
)
179 if (entry
->key
== dummy
)
182 if (entry
->hash
== hash
&& _PyString_Eq(entry
->key
, key
))
187 /* In the loop, key == dummy is by far (factor of 100s) the
188 least likely outcome, so test for that last. */
189 for (perturb
= hash
; ; perturb
>>= PERTURB_SHIFT
) {
190 i
= (i
<< 2) + i
+ perturb
+ 1;
191 entry
= &table
[i
& mask
];
192 if (entry
->key
== NULL
)
193 return freeslot
== NULL
? entry
: freeslot
;
194 if (entry
->key
== key
195 || (entry
->hash
== hash
196 && entry
->key
!= dummy
197 && _PyString_Eq(entry
->key
, key
)))
199 if (entry
->key
== dummy
&& freeslot
== NULL
)
202 assert(0); /* NOT REACHED */
207 Internal routine to insert a new key into the table.
208 Used by the public insert routine.
209 Eats a reference to key.
212 set_insert_key(register PySetObject
*so
, PyObject
*key
, long hash
)
214 register setentry
*entry
;
215 typedef setentry
*(*lookupfunc
)(PySetObject
*, PyObject
*, long);
217 assert(so
->lookup
!= NULL
);
218 entry
= so
->lookup(so
, key
, hash
);
221 if (entry
->key
== NULL
) {
227 } else if (entry
->key
== dummy
) {
241 Internal routine used by set_table_resize() to insert an item which is
242 known to be absent from the set. This routine also assumes that
243 the set contains no deleted entries. Besides the performance benefit,
244 using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
245 Note that no refcounts are changed by this routine; if needed, the caller
246 is responsible for incref'ing `key`.
249 set_insert_clean(register PySetObject
*so
, PyObject
*key
, long hash
)
252 register size_t perturb
;
253 register size_t mask
= (size_t)so
->mask
;
254 setentry
*table
= so
->table
;
255 register setentry
*entry
;
259 for (perturb
= hash
; entry
->key
!= NULL
; perturb
>>= PERTURB_SHIFT
) {
260 i
= (i
<< 2) + i
+ perturb
+ 1;
261 entry
= &table
[i
& mask
];
270 Restructure the table by allocating a new table and reinserting all
271 keys again. When entries have been deleted, the new table may
272 actually be smaller than the old one.
275 set_table_resize(PySetObject
*so
, Py_ssize_t minused
)
278 setentry
*oldtable
, *newtable
, *entry
;
280 int is_oldtable_malloced
;
281 setentry small_copy
[PySet_MINSIZE
];
283 assert(minused
>= 0);
285 /* Find the smallest table size > minused. */
286 for (newsize
= PySet_MINSIZE
;
287 newsize
<= minused
&& newsize
> 0;
295 /* Get space for a new table. */
296 oldtable
= so
->table
;
297 assert(oldtable
!= NULL
);
298 is_oldtable_malloced
= oldtable
!= so
->smalltable
;
300 if (newsize
== PySet_MINSIZE
) {
301 /* A large table is shrinking, or we can't get any smaller. */
302 newtable
= so
->smalltable
;
303 if (newtable
== oldtable
) {
304 if (so
->fill
== so
->used
) {
305 /* No dummies, so no point doing anything. */
308 /* We're not going to resize it, but rebuild the
309 table anyway to purge old dummy entries.
310 Subtle: This is *necessary* if fill==size,
311 as set_lookkey needs at least one virgin slot to
312 terminate failing searches. If fill < size, it's
313 merely desirable, as dummies slow searches. */
314 assert(so
->fill
> so
->used
);
315 memcpy(small_copy
, oldtable
, sizeof(small_copy
));
316 oldtable
= small_copy
;
320 newtable
= PyMem_NEW(setentry
, newsize
);
321 if (newtable
== NULL
) {
327 /* Make the set empty, using the new table. */
328 assert(newtable
!= oldtable
);
329 so
->table
= newtable
;
330 so
->mask
= newsize
- 1;
331 memset(newtable
, 0, sizeof(setentry
) * newsize
);
336 /* Copy the data over; this is refcount-neutral for active entries;
337 dummy entries aren't copied over, of course */
338 for (entry
= oldtable
; i
> 0; entry
++) {
339 if (entry
->key
== NULL
) {
342 } else if (entry
->key
== dummy
) {
345 assert(entry
->key
== dummy
);
346 Py_DECREF(entry
->key
);
350 set_insert_clean(so
, entry
->key
, entry
->hash
);
354 if (is_oldtable_malloced
)
359 /* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
362 set_add_entry(register PySetObject
*so
, setentry
*entry
)
364 register Py_ssize_t n_used
;
366 assert(so
->fill
<= so
->mask
); /* at least one empty slot */
368 Py_INCREF(entry
->key
);
369 if (set_insert_key(so
, entry
->key
, entry
->hash
) == -1) {
370 Py_DECREF(entry
->key
);
373 if (!(so
->used
> n_used
&& so
->fill
*3 >= (so
->mask
+1)*2))
375 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
379 set_add_key(register PySetObject
*so
, PyObject
*key
)
382 register Py_ssize_t n_used
;
384 if (!PyString_CheckExact(key
) ||
385 (hash
= ((PyStringObject
*) key
)->ob_shash
) == -1) {
386 hash
= PyObject_Hash(key
);
390 assert(so
->fill
<= so
->mask
); /* at least one empty slot */
393 if (set_insert_key(so
, key
, hash
) == -1) {
397 if (!(so
->used
> n_used
&& so
->fill
*3 >= (so
->mask
+1)*2))
399 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
402 #define DISCARD_NOTFOUND 0
403 #define DISCARD_FOUND 1
406 set_discard_entry(PySetObject
*so
, setentry
*oldentry
)
407 { register setentry
*entry
;
410 entry
= (so
->lookup
)(so
, oldentry
->key
, oldentry
->hash
);
413 if (entry
->key
== NULL
|| entry
->key
== dummy
)
414 return DISCARD_NOTFOUND
;
415 old_key
= entry
->key
;
420 return DISCARD_FOUND
;
424 set_discard_key(PySetObject
*so
, PyObject
*key
)
427 register setentry
*entry
;
430 assert (PyAnySet_Check(so
));
431 if (!PyString_CheckExact(key
) ||
432 (hash
= ((PyStringObject
*) key
)->ob_shash
) == -1) {
433 hash
= PyObject_Hash(key
);
437 entry
= (so
->lookup
)(so
, key
, hash
);
440 if (entry
->key
== NULL
|| entry
->key
== dummy
)
441 return DISCARD_NOTFOUND
;
442 old_key
= entry
->key
;
447 return DISCARD_FOUND
;
451 set_clear_internal(PySetObject
*so
)
453 setentry
*entry
, *table
;
454 int table_is_malloced
;
456 setentry small_copy
[PySet_MINSIZE
];
459 assert (PyAnySet_Check(so
));
466 assert(table
!= NULL
);
467 table_is_malloced
= table
!= so
->smalltable
;
469 /* This is delicate. During the process of clearing the set,
470 * decrefs can cause the set to mutate. To avoid fatal confusion
471 * (voice of experience), we have to make the set empty before
472 * clearing the slots, and never refer to anything via so->ref while
476 if (table_is_malloced
)
477 EMPTY_TO_MINSIZE(so
);
480 /* It's a small table with something that needs to be cleared.
481 * Afraid the only safe way is to copy the set entries into
482 * another small table first.
484 memcpy(small_copy
, table
, sizeof(small_copy
));
486 EMPTY_TO_MINSIZE(so
);
488 /* else it's a small table that's already empty */
490 /* Now we can finally clear things. If C had refcounts, we could
491 * assert that the refcount on table is 1 now, i.e. that this function
492 * has unique access to it, so decref side-effects can't alter it.
494 for (entry
= table
; fill
> 0; ++entry
) {
501 Py_DECREF(entry
->key
);
505 assert(entry
->key
== NULL
);
509 if (table_is_malloced
)
515 * Iterate over a set table. Use like so:
519 * pos = 0; # important! pos should not otherwise be changed by you
520 * while (set_next(yourset, &pos, &entry)) {
521 * Refer to borrowed reference in entry->key.
524 * CAUTION: In general, it isn't safe to use set_next in a loop that
528 set_next(PySetObject
*so
, Py_ssize_t
*pos_ptr
, setentry
**entry_ptr
)
532 register setentry
*table
;
534 assert (PyAnySet_Check(so
));
539 while (i
<= mask
&& (table
[i
].key
== NULL
|| table
[i
].key
== dummy
))
544 assert(table
[i
].key
!= NULL
);
545 *entry_ptr
= &table
[i
];
550 set_dealloc(PySetObject
*so
)
552 register setentry
*entry
;
553 Py_ssize_t fill
= so
->fill
;
554 PyObject_GC_UnTrack(so
);
555 Py_TRASHCAN_SAFE_BEGIN(so
)
556 if (so
->weakreflist
!= NULL
)
557 PyObject_ClearWeakRefs((PyObject
*) so
);
559 for (entry
= so
->table
; fill
> 0; entry
++) {
562 Py_DECREF(entry
->key
);
565 if (so
->table
!= so
->smalltable
)
566 PyMem_DEL(so
->table
);
567 if (numfree
< PySet_MAXFREELIST
&& PyAnySet_CheckExact(so
))
568 free_list
[numfree
++] = so
;
570 Py_TYPE(so
)->tp_free(so
);
571 Py_TRASHCAN_SAFE_END(so
)
575 set_tp_print(PySetObject
*so
, FILE *fp
, int flags
)
579 char *emit
= ""; /* No separator emitted on first pass */
580 char *separator
= ", ";
581 int status
= Py_ReprEnter((PyObject
*)so
);
586 Py_BEGIN_ALLOW_THREADS
587 fprintf(fp
, "%s(...)", so
->ob_type
->tp_name
);
592 Py_BEGIN_ALLOW_THREADS
593 fprintf(fp
, "%s([", so
->ob_type
->tp_name
);
595 while (set_next(so
, &pos
, &entry
)) {
596 Py_BEGIN_ALLOW_THREADS
600 if (PyObject_Print(entry
->key
, fp
, 0) != 0) {
601 Py_ReprLeave((PyObject
*)so
);
605 Py_BEGIN_ALLOW_THREADS
608 Py_ReprLeave((PyObject
*)so
);
613 set_repr(PySetObject
*so
)
615 PyObject
*keys
, *result
=NULL
, *listrepr
;
616 int status
= Py_ReprEnter((PyObject
*)so
);
621 return PyString_FromFormat("%s(...)", so
->ob_type
->tp_name
);
624 keys
= PySequence_List((PyObject
*)so
);
627 listrepr
= PyObject_Repr(keys
);
629 if (listrepr
== NULL
)
632 result
= PyString_FromFormat("%s(%s)", so
->ob_type
->tp_name
,
633 PyString_AS_STRING(listrepr
));
636 Py_ReprLeave((PyObject
*)so
);
641 set_len(PyObject
*so
)
643 return ((PySetObject
*)so
)->used
;
647 set_merge(PySetObject
*so
, PyObject
*otherset
)
650 register Py_ssize_t i
;
651 register setentry
*entry
;
653 assert (PyAnySet_Check(so
));
654 assert (PyAnySet_Check(otherset
));
656 other
= (PySetObject
*)otherset
;
657 if (other
== so
|| other
->used
== 0)
658 /* a.update(a) or a.update({}); nothing to do */
660 /* Do one big resize at the start, rather than
661 * incrementally resizing as we insert new keys. Expect
662 * that there will be no (or few) overlapping keys.
664 if ((so
->fill
+ other
->used
)*3 >= (so
->mask
+1)*2) {
665 if (set_table_resize(so
, (so
->used
+ other
->used
)*2) != 0)
668 for (i
= 0; i
<= other
->mask
; i
++) {
669 entry
= &other
->table
[i
];
670 if (entry
->key
!= NULL
&&
671 entry
->key
!= dummy
) {
672 Py_INCREF(entry
->key
);
673 if (set_insert_key(so
, entry
->key
, entry
->hash
) == -1) {
674 Py_DECREF(entry
->key
);
683 set_contains_key(PySetObject
*so
, PyObject
*key
)
688 if (!PyString_CheckExact(key
) ||
689 (hash
= ((PyStringObject
*) key
)->ob_shash
) == -1) {
690 hash
= PyObject_Hash(key
);
694 entry
= (so
->lookup
)(so
, key
, hash
);
698 return key
!= NULL
&& key
!= dummy
;
702 set_contains_entry(PySetObject
*so
, setentry
*entry
)
707 lu_entry
= (so
->lookup
)(so
, entry
->key
, entry
->hash
);
708 if (lu_entry
== NULL
)
711 return key
!= NULL
&& key
!= dummy
;
715 set_pop(PySetObject
*so
)
717 register Py_ssize_t i
= 0;
718 register setentry
*entry
;
721 assert (PyAnySet_Check(so
));
723 PyErr_SetString(PyExc_KeyError
, "pop from an empty set");
727 /* Set entry to "the first" unused or dummy set entry. We abuse
728 * the hash field of slot 0 to hold a search finger:
729 * If slot 0 has a value, use slot 0.
730 * Else slot 0 is being used to hold a search finger,
731 * and we use its hash value as the first index to look.
733 entry
= &so
->table
[0];
734 if (entry
->key
== NULL
|| entry
->key
== dummy
) {
736 /* The hash field may be a real hash value, or it may be a
737 * legit search finger, or it may be a once-legit search
738 * finger that's out of bounds now because it wrapped around
739 * or the table shrunk -- simply make sure it's in bounds now.
741 if (i
> so
->mask
|| i
< 1)
742 i
= 1; /* skip slot 0 */
743 while ((entry
= &so
->table
[i
])->key
== NULL
|| entry
->key
==dummy
) {
753 so
->table
[0].hash
= i
+ 1; /* next place to start */
757 PyDoc_STRVAR(pop_doc
, "Remove and return an arbitrary set element.\n\
758 Raises KeyError if the set is empty.");
761 set_traverse(PySetObject
*so
, visitproc visit
, void *arg
)
766 while (set_next(so
, &pos
, &entry
))
767 Py_VISIT(entry
->key
);
772 frozenset_hash(PyObject
*self
)
774 PySetObject
*so
= (PySetObject
*)self
;
775 long h
, hash
= 1927868237L;
782 hash
*= PySet_GET_SIZE(self
) + 1;
783 while (set_next(so
, &pos
, &entry
)) {
784 /* Work to increase the bit dispersion for closely spaced hash
785 values. The is important because some use cases have many
786 combinations of a small number of elements with nearby
787 hashes so that many distinct combinations collapse to only
788 a handful of distinct hash values. */
790 hash
^= (h
^ (h
<< 16) ^ 89869747L) * 3644798167u;
792 hash
= hash
* 69069L + 907133923L;
799 /***** Set iterator type ***********************************************/
803 PySetObject
*si_set
; /* Set to NULL when iterator is exhausted */
810 setiter_dealloc(setiterobject
*si
)
812 Py_XDECREF(si
->si_set
);
817 setiter_traverse(setiterobject
*si
, visitproc visit
, void *arg
)
819 Py_VISIT(si
->si_set
);
824 setiter_len(setiterobject
*si
)
827 if (si
->si_set
!= NULL
&& si
->si_used
== si
->si_set
->used
)
829 return PyInt_FromLong(len
);
832 PyDoc_STRVAR(length_hint_doc
, "Private method returning an estimate of len(list(it)).");
834 static PyMethodDef setiter_methods
[] = {
835 {"__length_hint__", (PyCFunction
)setiter_len
, METH_NOARGS
, length_hint_doc
},
836 {NULL
, NULL
} /* sentinel */
839 static PyObject
*setiter_iternext(setiterobject
*si
)
842 register Py_ssize_t i
, mask
;
843 register setentry
*entry
;
844 PySetObject
*so
= si
->si_set
;
848 assert (PyAnySet_Check(so
));
850 if (si
->si_used
!= so
->used
) {
851 PyErr_SetString(PyExc_RuntimeError
,
852 "Set changed size during iteration");
853 si
->si_used
= -1; /* Make this state sticky */
861 while (i
<= mask
&& (entry
[i
].key
== NULL
|| entry
[i
].key
== dummy
))
877 static PyTypeObject PySetIter_Type
= {
878 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
879 "setiterator", /* tp_name */
880 sizeof(setiterobject
), /* tp_basicsize */
883 (destructor
)setiter_dealloc
, /* tp_dealloc */
889 0, /* tp_as_number */
890 0, /* tp_as_sequence */
891 0, /* tp_as_mapping */
895 PyObject_GenericGetAttr
, /* tp_getattro */
897 0, /* tp_as_buffer */
898 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
900 (traverseproc
)setiter_traverse
, /* tp_traverse */
902 0, /* tp_richcompare */
903 0, /* tp_weaklistoffset */
904 PyObject_SelfIter
, /* tp_iter */
905 (iternextfunc
)setiter_iternext
, /* tp_iternext */
906 setiter_methods
, /* tp_methods */
911 set_iter(PySetObject
*so
)
913 setiterobject
*si
= PyObject_GC_New(setiterobject
, &PySetIter_Type
);
918 si
->si_used
= so
->used
;
921 _PyObject_GC_TRACK(si
);
922 return (PyObject
*)si
;
926 set_update_internal(PySetObject
*so
, PyObject
*other
)
930 if (PyAnySet_Check(other
))
931 return set_merge(so
, other
);
933 if (PyDict_CheckExact(other
)) {
937 Py_ssize_t dictsize
= PyDict_Size(other
);
939 /* Do one big resize at the start, rather than
940 * incrementally resizing as we insert new keys. Expect
941 * that there will be no (or few) overlapping keys.
945 if ((so
->fill
+ dictsize
)*3 >= (so
->mask
+1)*2) {
946 if (set_table_resize(so
, (so
->used
+ dictsize
)*2) != 0)
949 while (_PyDict_Next(other
, &pos
, &key
, &value
, &hash
)) {
952 an_entry
.hash
= hash
;
954 if (set_add_entry(so
, &an_entry
) == -1)
960 it
= PyObject_GetIter(other
);
964 while ((key
= PyIter_Next(it
)) != NULL
) {
965 if (set_add_key(so
, key
) == -1) {
973 if (PyErr_Occurred())
979 set_update(PySetObject
*so
, PyObject
*args
)
983 for (i
=0 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
984 PyObject
*other
= PyTuple_GET_ITEM(args
, i
);
985 if (set_update_internal(so
, other
) == -1)
991 PyDoc_STRVAR(update_doc
,
992 "Update a set with the union of itself and others.");
995 make_new_set(PyTypeObject
*type
, PyObject
*iterable
)
997 register PySetObject
*so
= NULL
;
999 if (dummy
== NULL
) { /* Auto-initialize dummy */
1000 dummy
= PyString_FromString("<dummy key>");
1005 /* create PySetObject structure */
1007 (type
== &PySet_Type
|| type
== &PyFrozenSet_Type
)) {
1008 so
= free_list
[--numfree
];
1009 assert (so
!= NULL
&& PyAnySet_CheckExact(so
));
1011 _Py_NewReference((PyObject
*)so
);
1012 EMPTY_TO_MINSIZE(so
);
1013 PyObject_GC_Track(so
);
1015 so
= (PySetObject
*)type
->tp_alloc(type
, 0);
1018 /* tp_alloc has already zeroed the structure */
1019 assert(so
->table
== NULL
&& so
->fill
== 0 && so
->used
== 0);
1020 INIT_NONZERO_SET_SLOTS(so
);
1023 so
->lookup
= set_lookkey_string
;
1024 so
->weakreflist
= NULL
;
1026 if (iterable
!= NULL
) {
1027 if (set_update_internal(so
, iterable
) == -1) {
1033 return (PyObject
*)so
;
1036 /* The empty frozenset is a singleton */
1037 static PyObject
*emptyfrozenset
= NULL
;
1040 frozenset_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1042 PyObject
*iterable
= NULL
, *result
;
1044 if (type
== &PyFrozenSet_Type
&& !_PyArg_NoKeywords("frozenset()", kwds
))
1047 if (!PyArg_UnpackTuple(args
, type
->tp_name
, 0, 1, &iterable
))
1050 if (type
!= &PyFrozenSet_Type
)
1051 return make_new_set(type
, iterable
);
1053 if (iterable
!= NULL
) {
1054 /* frozenset(f) is idempotent */
1055 if (PyFrozenSet_CheckExact(iterable
)) {
1056 Py_INCREF(iterable
);
1059 result
= make_new_set(type
, iterable
);
1060 if (result
== NULL
|| PySet_GET_SIZE(result
))
1064 /* The empty frozenset is a singleton */
1065 if (emptyfrozenset
== NULL
)
1066 emptyfrozenset
= make_new_set(type
, NULL
);
1067 Py_XINCREF(emptyfrozenset
);
1068 return emptyfrozenset
;
1078 so
= free_list
[numfree
];
1079 PyObject_GC_Del(so
);
1082 Py_CLEAR(emptyfrozenset
);
1086 set_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1088 if (type
== &PySet_Type
&& !_PyArg_NoKeywords("set()", kwds
))
1091 return make_new_set(type
, NULL
);
1094 /* set_swap_bodies() switches the contents of any two sets by moving their
1095 internal data pointers and, if needed, copying the internal smalltables.
1096 Semantically equivalent to:
1098 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1100 The function always succeeds and it leaves both objects in a stable state.
1101 Useful for creating temporary frozensets from sets for membership testing
1102 in __contains__(), discard(), and remove(). Also useful for operations
1103 that update in-place (by allowing an intermediate result to be swapped
1104 into one of the original inputs).
1108 set_swap_bodies(PySetObject
*a
, PySetObject
*b
)
1112 setentry
*(*f
)(PySetObject
*so
, PyObject
*key
, long hash
);
1113 setentry tab
[PySet_MINSIZE
];
1116 t
= a
->fill
; a
->fill
= b
->fill
; b
->fill
= t
;
1117 t
= a
->used
; a
->used
= b
->used
; b
->used
= t
;
1118 t
= a
->mask
; a
->mask
= b
->mask
; b
->mask
= t
;
1121 if (a
->table
== a
->smalltable
)
1123 a
->table
= b
->table
;
1124 if (b
->table
== b
->smalltable
)
1125 a
->table
= a
->smalltable
;
1128 f
= a
->lookup
; a
->lookup
= b
->lookup
; b
->lookup
= f
;
1130 if (a
->table
== a
->smalltable
|| b
->table
== b
->smalltable
) {
1131 memcpy(tab
, a
->smalltable
, sizeof(tab
));
1132 memcpy(a
->smalltable
, b
->smalltable
, sizeof(tab
));
1133 memcpy(b
->smalltable
, tab
, sizeof(tab
));
1136 if (PyType_IsSubtype(Py_TYPE(a
), &PyFrozenSet_Type
) &&
1137 PyType_IsSubtype(Py_TYPE(b
), &PyFrozenSet_Type
)) {
1138 h
= a
->hash
; a
->hash
= b
->hash
; b
->hash
= h
;
1146 set_copy(PySetObject
*so
)
1148 return make_new_set(Py_TYPE(so
), (PyObject
*)so
);
1152 frozenset_copy(PySetObject
*so
)
1154 if (PyFrozenSet_CheckExact(so
)) {
1156 return (PyObject
*)so
;
1158 return set_copy(so
);
1161 PyDoc_STRVAR(copy_doc
, "Return a shallow copy of a set.");
1164 set_clear(PySetObject
*so
)
1166 set_clear_internal(so
);
1170 PyDoc_STRVAR(clear_doc
, "Remove all elements from this set.");
1173 set_union(PySetObject
*so
, PyObject
*args
)
1175 PySetObject
*result
;
1179 result
= (PySetObject
*)set_copy(so
);
1183 for (i
=0 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
1184 other
= PyTuple_GET_ITEM(args
, i
);
1185 if ((PyObject
*)so
== other
)
1187 if (set_update_internal(result
, other
) == -1) {
1192 return (PyObject
*)result
;
1195 PyDoc_STRVAR(union_doc
,
1196 "Return the union of sets as a new set.\n\
1198 (i.e. all elements that are in either set.)");
1201 set_or(PySetObject
*so
, PyObject
*other
)
1203 PySetObject
*result
;
1205 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1206 Py_INCREF(Py_NotImplemented
);
1207 return Py_NotImplemented
;
1210 result
= (PySetObject
*)set_copy(so
);
1213 if ((PyObject
*)so
== other
)
1214 return (PyObject
*)result
;
1215 if (set_update_internal(result
, other
) == -1) {
1219 return (PyObject
*)result
;
1223 set_ior(PySetObject
*so
, PyObject
*other
)
1225 if (!PyAnySet_Check(other
)) {
1226 Py_INCREF(Py_NotImplemented
);
1227 return Py_NotImplemented
;
1229 if (set_update_internal(so
, other
) == -1)
1232 return (PyObject
*)so
;
1236 set_intersection(PySetObject
*so
, PyObject
*other
)
1238 PySetObject
*result
;
1239 PyObject
*key
, *it
, *tmp
;
1241 if ((PyObject
*)so
== other
)
1242 return set_copy(so
);
1244 result
= (PySetObject
*)make_new_set(Py_TYPE(so
), NULL
);
1248 if (PyAnySet_Check(other
)) {
1252 if (PySet_GET_SIZE(other
) > PySet_GET_SIZE(so
)) {
1253 tmp
= (PyObject
*)so
;
1254 so
= (PySetObject
*)other
;
1258 while (set_next((PySetObject
*)other
, &pos
, &entry
)) {
1259 int rv
= set_contains_entry(so
, entry
);
1265 if (set_add_entry(result
, entry
) == -1) {
1271 return (PyObject
*)result
;
1274 it
= PyObject_GetIter(other
);
1280 while ((key
= PyIter_Next(it
)) != NULL
) {
1283 long hash
= PyObject_Hash(key
);
1293 rv
= set_contains_entry(so
, &entry
);
1301 if (set_add_entry(result
, &entry
) == -1) {
1311 if (PyErr_Occurred()) {
1315 return (PyObject
*)result
;
1319 set_intersection_multi(PySetObject
*so
, PyObject
*args
)
1322 PyObject
*result
= (PyObject
*)so
;
1324 if (PyTuple_GET_SIZE(args
) == 0)
1325 return set_copy(so
);
1328 for (i
=0 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
1329 PyObject
*other
= PyTuple_GET_ITEM(args
, i
);
1330 PyObject
*newresult
= set_intersection((PySetObject
*)result
, other
);
1331 if (newresult
== NULL
) {
1341 PyDoc_STRVAR(intersection_doc
,
1342 "Return the intersection of two or more sets as a new set.\n\
1344 (i.e. elements that are common to all of the sets.)");
1347 set_intersection_update(PySetObject
*so
, PyObject
*other
)
1351 tmp
= set_intersection(so
, other
);
1354 set_swap_bodies(so
, (PySetObject
*)tmp
);
1360 set_intersection_update_multi(PySetObject
*so
, PyObject
*args
)
1364 tmp
= set_intersection_multi(so
, args
);
1367 set_swap_bodies(so
, (PySetObject
*)tmp
);
1372 PyDoc_STRVAR(intersection_update_doc
,
1373 "Update a set with the intersection of itself and another.");
1376 set_and(PySetObject
*so
, PyObject
*other
)
1378 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1379 Py_INCREF(Py_NotImplemented
);
1380 return Py_NotImplemented
;
1382 return set_intersection(so
, other
);
1386 set_iand(PySetObject
*so
, PyObject
*other
)
1390 if (!PyAnySet_Check(other
)) {
1391 Py_INCREF(Py_NotImplemented
);
1392 return Py_NotImplemented
;
1394 result
= set_intersection_update(so
, other
);
1399 return (PyObject
*)so
;
1403 set_isdisjoint(PySetObject
*so
, PyObject
*other
)
1405 PyObject
*key
, *it
, *tmp
;
1407 if ((PyObject
*)so
== other
) {
1408 if (PySet_GET_SIZE(so
) == 0)
1414 if (PyAnySet_CheckExact(other
)) {
1418 if (PySet_GET_SIZE(other
) > PySet_GET_SIZE(so
)) {
1419 tmp
= (PyObject
*)so
;
1420 so
= (PySetObject
*)other
;
1423 while (set_next((PySetObject
*)other
, &pos
, &entry
)) {
1424 int rv
= set_contains_entry(so
, entry
);
1433 it
= PyObject_GetIter(other
);
1437 while ((key
= PyIter_Next(it
)) != NULL
) {
1440 long hash
= PyObject_Hash(key
);
1449 rv
= set_contains_entry(so
, &entry
);
1461 if (PyErr_Occurred())
1466 PyDoc_STRVAR(isdisjoint_doc
,
1467 "Return True if two sets have a null intersection.");
1470 set_difference_update_internal(PySetObject
*so
, PyObject
*other
)
1472 if ((PyObject
*)so
== other
)
1473 return set_clear_internal(so
);
1475 if (PyAnySet_Check(other
)) {
1479 while (set_next((PySetObject
*)other
, &pos
, &entry
))
1480 if (set_discard_entry(so
, entry
) == -1)
1484 it
= PyObject_GetIter(other
);
1488 while ((key
= PyIter_Next(it
)) != NULL
) {
1489 if (set_discard_key(so
, key
) == -1) {
1497 if (PyErr_Occurred())
1500 /* If more than 1/5 are dummies, then resize them away. */
1501 if ((so
->fill
- so
->used
) * 5 < so
->mask
)
1503 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
1507 set_difference_update(PySetObject
*so
, PyObject
*args
)
1511 for (i
=0 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
1512 PyObject
*other
= PyTuple_GET_ITEM(args
, i
);
1513 if (set_difference_update_internal(so
, other
) == -1)
1519 PyDoc_STRVAR(difference_update_doc
,
1520 "Remove all elements of another set from this set.");
1523 set_difference(PySetObject
*so
, PyObject
*other
)
1529 if (!PyAnySet_Check(other
) && !PyDict_CheckExact(other
)) {
1530 result
= set_copy(so
);
1533 if (set_difference_update_internal((PySetObject
*)result
, other
) != -1)
1539 result
= make_new_set(Py_TYPE(so
), NULL
);
1543 if (PyDict_CheckExact(other
)) {
1544 while (set_next(so
, &pos
, &entry
)) {
1546 entrycopy
.hash
= entry
->hash
;
1547 entrycopy
.key
= entry
->key
;
1548 if (!_PyDict_Contains(other
, entry
->key
, entry
->hash
)) {
1549 if (set_add_entry((PySetObject
*)result
, &entrycopy
) == -1) {
1558 while (set_next(so
, &pos
, &entry
)) {
1559 int rv
= set_contains_entry((PySetObject
*)other
, entry
);
1565 if (set_add_entry((PySetObject
*)result
, entry
) == -1) {
1575 set_difference_multi(PySetObject
*so
, PyObject
*args
)
1578 PyObject
*result
, *other
;
1580 if (PyTuple_GET_SIZE(args
) == 0)
1581 return set_copy(so
);
1583 other
= PyTuple_GET_ITEM(args
, 0);
1584 result
= set_difference(so
, other
);
1588 for (i
=1 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
1589 other
= PyTuple_GET_ITEM(args
, i
);
1590 if (set_difference_update_internal((PySetObject
*)result
, other
) == -1) {
1598 PyDoc_STRVAR(difference_doc
,
1599 "Return the difference of two or more sets as a new set.\n\
1601 (i.e. all elements that are in this set but not the others.)");
1603 set_sub(PySetObject
*so
, PyObject
*other
)
1605 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1606 Py_INCREF(Py_NotImplemented
);
1607 return Py_NotImplemented
;
1609 return set_difference(so
, other
);
1613 set_isub(PySetObject
*so
, PyObject
*other
)
1615 if (!PyAnySet_Check(other
)) {
1616 Py_INCREF(Py_NotImplemented
);
1617 return Py_NotImplemented
;
1619 if (set_difference_update_internal(so
, other
) == -1)
1622 return (PyObject
*)so
;
1626 set_symmetric_difference_update(PySetObject
*so
, PyObject
*other
)
1628 PySetObject
*otherset
;
1633 if ((PyObject
*)so
== other
)
1634 return set_clear(so
);
1636 if (PyDict_CheckExact(other
)) {
1640 while (_PyDict_Next(other
, &pos
, &key
, &value
, &hash
)) {
1643 an_entry
.hash
= hash
;
1645 rv
= set_discard_entry(so
, &an_entry
);
1648 if (rv
== DISCARD_NOTFOUND
) {
1649 if (set_add_entry(so
, &an_entry
) == -1)
1656 if (PyAnySet_Check(other
)) {
1658 otherset
= (PySetObject
*)other
;
1660 otherset
= (PySetObject
*)make_new_set(Py_TYPE(so
), other
);
1661 if (otherset
== NULL
)
1665 while (set_next(otherset
, &pos
, &entry
)) {
1666 int rv
= set_discard_entry(so
, entry
);
1668 Py_DECREF(otherset
);
1671 if (rv
== DISCARD_NOTFOUND
) {
1672 if (set_add_entry(so
, entry
) == -1) {
1673 Py_DECREF(otherset
);
1678 Py_DECREF(otherset
);
1682 PyDoc_STRVAR(symmetric_difference_update_doc
,
1683 "Update a set with the symmetric difference of itself and another.");
1686 set_symmetric_difference(PySetObject
*so
, PyObject
*other
)
1689 PySetObject
*otherset
;
1691 otherset
= (PySetObject
*)make_new_set(Py_TYPE(so
), other
);
1692 if (otherset
== NULL
)
1694 rv
= set_symmetric_difference_update(otherset
, (PyObject
*)so
);
1698 return (PyObject
*)otherset
;
1701 PyDoc_STRVAR(symmetric_difference_doc
,
1702 "Return the symmetric difference of two sets as a new set.\n\
1704 (i.e. all elements that are in exactly one of the sets.)");
1707 set_xor(PySetObject
*so
, PyObject
*other
)
1709 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1710 Py_INCREF(Py_NotImplemented
);
1711 return Py_NotImplemented
;
1713 return set_symmetric_difference(so
, other
);
1717 set_ixor(PySetObject
*so
, PyObject
*other
)
1721 if (!PyAnySet_Check(other
)) {
1722 Py_INCREF(Py_NotImplemented
);
1723 return Py_NotImplemented
;
1725 result
= set_symmetric_difference_update(so
, other
);
1730 return (PyObject
*)so
;
1734 set_issubset(PySetObject
*so
, PyObject
*other
)
1739 if (!PyAnySet_Check(other
)) {
1740 PyObject
*tmp
, *result
;
1741 tmp
= make_new_set(&PySet_Type
, other
);
1744 result
= set_issubset(so
, tmp
);
1748 if (PySet_GET_SIZE(so
) > PySet_GET_SIZE(other
))
1751 while (set_next(so
, &pos
, &entry
)) {
1752 int rv
= set_contains_entry((PySetObject
*)other
, entry
);
1761 PyDoc_STRVAR(issubset_doc
, "Report whether another set contains this set.");
1764 set_issuperset(PySetObject
*so
, PyObject
*other
)
1766 PyObject
*tmp
, *result
;
1768 if (!PyAnySet_Check(other
)) {
1769 tmp
= make_new_set(&PySet_Type
, other
);
1772 result
= set_issuperset(so
, tmp
);
1776 return set_issubset((PySetObject
*)other
, (PyObject
*)so
);
1779 PyDoc_STRVAR(issuperset_doc
, "Report whether this set contains another set.");
1782 set_richcompare(PySetObject
*v
, PyObject
*w
, int op
)
1786 if(!PyAnySet_Check(w
)) {
1791 PyErr_SetString(PyExc_TypeError
, "can only compare to a set");
1796 if (PySet_GET_SIZE(v
) != PySet_GET_SIZE(w
))
1798 if (v
->hash
!= -1 &&
1799 ((PySetObject
*)w
)->hash
!= -1 &&
1800 v
->hash
!= ((PySetObject
*)w
)->hash
)
1802 return set_issubset(v
, w
);
1804 r1
= set_richcompare(v
, w
, Py_EQ
);
1807 r2
= PyBool_FromLong(PyObject_Not(r1
));
1811 return set_issubset(v
, w
);
1813 return set_issuperset(v
, w
);
1815 if (PySet_GET_SIZE(v
) >= PySet_GET_SIZE(w
))
1817 return set_issubset(v
, w
);
1819 if (PySet_GET_SIZE(v
) <= PySet_GET_SIZE(w
))
1821 return set_issuperset(v
, w
);
1823 Py_INCREF(Py_NotImplemented
);
1824 return Py_NotImplemented
;
1828 set_nocmp(PyObject
*self
, PyObject
*other
)
1830 PyErr_SetString(PyExc_TypeError
, "cannot compare sets using cmp()");
1835 set_add(PySetObject
*so
, PyObject
*key
)
1837 if (set_add_key(so
, key
) == -1)
1842 PyDoc_STRVAR(add_doc
,
1843 "Add an element to a set.\n\
1845 This has no effect if the element is already present.");
1848 set_contains(PySetObject
*so
, PyObject
*key
)
1853 rv
= set_contains_key(so
, key
);
1855 if (!PySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1858 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1861 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1862 rv
= set_contains(so
, tmpkey
);
1863 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1870 set_direct_contains(PySetObject
*so
, PyObject
*key
)
1874 result
= set_contains(so
, key
);
1877 return PyBool_FromLong(result
);
1880 PyDoc_STRVAR(contains_doc
, "x.__contains__(y) <==> y in x.");
1883 set_remove(PySetObject
*so
, PyObject
*key
)
1888 rv
= set_discard_key(so
, key
);
1890 if (!PySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1893 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1896 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1897 rv
= set_discard_key(so
, tmpkey
);
1898 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1904 if (rv
== DISCARD_NOTFOUND
) {
1911 PyDoc_STRVAR(remove_doc
,
1912 "Remove an element from a set; it must be a member.\n\
1914 If the element is not a member, raise a KeyError.");
1917 set_discard(PySetObject
*so
, PyObject
*key
)
1919 PyObject
*tmpkey
, *result
;
1922 rv
= set_discard_key(so
, key
);
1924 if (!PySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1927 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1930 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1931 result
= set_discard(so
, tmpkey
);
1932 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1939 PyDoc_STRVAR(discard_doc
,
1940 "Remove an element from a set if it is a member.\n\
1942 If the element is not a member, do nothing.");
1945 set_reduce(PySetObject
*so
)
1947 PyObject
*keys
=NULL
, *args
=NULL
, *result
=NULL
, *dict
=NULL
;
1949 keys
= PySequence_List((PyObject
*)so
);
1952 args
= PyTuple_Pack(1, keys
);
1955 dict
= PyObject_GetAttrString((PyObject
*)so
, "__dict__");
1961 result
= PyTuple_Pack(3, Py_TYPE(so
), args
, dict
);
1969 PyDoc_STRVAR(reduce_doc
, "Return state information for pickling.");
1972 set_sizeof(PySetObject
*so
)
1976 res
= sizeof(PySetObject
);
1977 if (so
->table
!= so
->smalltable
)
1978 res
= res
+ (so
->mask
+ 1) * sizeof(setentry
);
1979 return PyInt_FromSsize_t(res
);
1982 PyDoc_STRVAR(sizeof_doc
, "S.__sizeof__() -> size of S in memory, in bytes");
1984 set_init(PySetObject
*self
, PyObject
*args
, PyObject
*kwds
)
1986 PyObject
*iterable
= NULL
;
1988 if (!PyAnySet_Check(self
))
1990 if (!PyArg_UnpackTuple(args
, Py_TYPE(self
)->tp_name
, 0, 1, &iterable
))
1992 set_clear_internal(self
);
1994 if (iterable
== NULL
)
1996 return set_update_internal(self
, iterable
);
1999 static PySequenceMethods set_as_sequence
= {
2000 set_len
, /* sq_length */
2005 0, /* sq_ass_item */
2006 0, /* sq_ass_slice */
2007 (objobjproc
)set_contains
, /* sq_contains */
2010 /* set object ********************************************************/
2013 static PyObject
*test_c_api(PySetObject
*so
);
2015 PyDoc_STRVAR(test_c_api_doc
, "Exercises C API. Returns True.\n\
2016 All is well if assertions don't fail.");
2019 static PyMethodDef set_methods
[] = {
2020 {"add", (PyCFunction
)set_add
, METH_O
,
2022 {"clear", (PyCFunction
)set_clear
, METH_NOARGS
,
2024 {"__contains__",(PyCFunction
)set_direct_contains
, METH_O
| METH_COEXIST
,
2026 {"copy", (PyCFunction
)set_copy
, METH_NOARGS
,
2028 {"discard", (PyCFunction
)set_discard
, METH_O
,
2030 {"difference", (PyCFunction
)set_difference_multi
, METH_VARARGS
,
2032 {"difference_update", (PyCFunction
)set_difference_update
, METH_VARARGS
,
2033 difference_update_doc
},
2034 {"intersection",(PyCFunction
)set_intersection_multi
, METH_VARARGS
,
2036 {"intersection_update",(PyCFunction
)set_intersection_update_multi
, METH_VARARGS
,
2037 intersection_update_doc
},
2038 {"isdisjoint", (PyCFunction
)set_isdisjoint
, METH_O
,
2040 {"issubset", (PyCFunction
)set_issubset
, METH_O
,
2042 {"issuperset", (PyCFunction
)set_issuperset
, METH_O
,
2044 {"pop", (PyCFunction
)set_pop
, METH_NOARGS
,
2046 {"__reduce__", (PyCFunction
)set_reduce
, METH_NOARGS
,
2048 {"remove", (PyCFunction
)set_remove
, METH_O
,
2050 {"__sizeof__", (PyCFunction
)set_sizeof
, METH_NOARGS
,
2052 {"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
},
2057 {"test_c_api", (PyCFunction
)test_c_api
, METH_NOARGS
,
2060 {"union", (PyCFunction
)set_union
, METH_VARARGS
,
2062 {"update", (PyCFunction
)set_update
, METH_VARARGS
,
2064 {NULL
, NULL
} /* sentinel */
2067 static PyNumberMethods set_as_number
= {
2069 (binaryfunc
)set_sub
, /*nb_subtract*/
2082 (binaryfunc
)set_and
, /*nb_and*/
2083 (binaryfunc
)set_xor
, /*nb_xor*/
2084 (binaryfunc
)set_or
, /*nb_or*/
2091 0, /*nb_inplace_add*/
2092 (binaryfunc
)set_isub
, /*nb_inplace_subtract*/
2093 0, /*nb_inplace_multiply*/
2094 0, /*nb_inplace_divide*/
2095 0, /*nb_inplace_remainder*/
2096 0, /*nb_inplace_power*/
2097 0, /*nb_inplace_lshift*/
2098 0, /*nb_inplace_rshift*/
2099 (binaryfunc
)set_iand
, /*nb_inplace_and*/
2100 (binaryfunc
)set_ixor
, /*nb_inplace_xor*/
2101 (binaryfunc
)set_ior
, /*nb_inplace_or*/
2104 PyDoc_STRVAR(set_doc
,
2105 "set(iterable) --> set object\n\
2107 Build an unordered collection of unique elements.");
2109 PyTypeObject PySet_Type
= {
2110 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2111 "set", /* tp_name */
2112 sizeof(PySetObject
), /* tp_basicsize */
2113 0, /* tp_itemsize */
2115 (destructor
)set_dealloc
, /* tp_dealloc */
2116 (printfunc
)set_tp_print
, /* tp_print */
2119 set_nocmp
, /* tp_compare */
2120 (reprfunc
)set_repr
, /* tp_repr */
2121 &set_as_number
, /* tp_as_number */
2122 &set_as_sequence
, /* tp_as_sequence */
2123 0, /* tp_as_mapping */
2124 (hashfunc
)PyObject_HashNotImplemented
, /* tp_hash */
2127 PyObject_GenericGetAttr
, /* tp_getattro */
2128 0, /* tp_setattro */
2129 0, /* tp_as_buffer */
2130 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES
|
2131 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2132 set_doc
, /* tp_doc */
2133 (traverseproc
)set_traverse
, /* tp_traverse */
2134 (inquiry
)set_clear_internal
, /* tp_clear */
2135 (richcmpfunc
)set_richcompare
, /* tp_richcompare */
2136 offsetof(PySetObject
, weakreflist
), /* tp_weaklistoffset */
2137 (getiterfunc
)set_iter
, /* tp_iter */
2138 0, /* tp_iternext */
2139 set_methods
, /* tp_methods */
2144 0, /* tp_descr_get */
2145 0, /* tp_descr_set */
2146 0, /* tp_dictoffset */
2147 (initproc
)set_init
, /* tp_init */
2148 PyType_GenericAlloc
, /* tp_alloc */
2149 set_new
, /* tp_new */
2150 PyObject_GC_Del
, /* tp_free */
2153 /* frozenset object ********************************************************/
2156 static PyMethodDef frozenset_methods
[] = {
2157 {"__contains__",(PyCFunction
)set_direct_contains
, METH_O
| METH_COEXIST
,
2159 {"copy", (PyCFunction
)frozenset_copy
, METH_NOARGS
,
2161 {"difference", (PyCFunction
)set_difference_multi
, METH_VARARGS
,
2163 {"intersection",(PyCFunction
)set_intersection_multi
, METH_VARARGS
,
2165 {"isdisjoint", (PyCFunction
)set_isdisjoint
, METH_O
,
2167 {"issubset", (PyCFunction
)set_issubset
, METH_O
,
2169 {"issuperset", (PyCFunction
)set_issuperset
, METH_O
,
2171 {"__reduce__", (PyCFunction
)set_reduce
, METH_NOARGS
,
2173 {"__sizeof__", (PyCFunction
)set_sizeof
, METH_NOARGS
,
2175 {"symmetric_difference",(PyCFunction
)set_symmetric_difference
, METH_O
,
2176 symmetric_difference_doc
},
2177 {"union", (PyCFunction
)set_union
, METH_VARARGS
,
2179 {NULL
, NULL
} /* sentinel */
2182 static PyNumberMethods frozenset_as_number
= {
2184 (binaryfunc
)set_sub
, /*nb_subtract*/
2197 (binaryfunc
)set_and
, /*nb_and*/
2198 (binaryfunc
)set_xor
, /*nb_xor*/
2199 (binaryfunc
)set_or
, /*nb_or*/
2202 PyDoc_STRVAR(frozenset_doc
,
2203 "frozenset(iterable) --> frozenset object\n\
2205 Build an immutable unordered collection of unique elements.");
2207 PyTypeObject PyFrozenSet_Type
= {
2208 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2209 "frozenset", /* tp_name */
2210 sizeof(PySetObject
), /* tp_basicsize */
2211 0, /* tp_itemsize */
2213 (destructor
)set_dealloc
, /* tp_dealloc */
2214 (printfunc
)set_tp_print
, /* tp_print */
2217 set_nocmp
, /* tp_compare */
2218 (reprfunc
)set_repr
, /* tp_repr */
2219 &frozenset_as_number
, /* tp_as_number */
2220 &set_as_sequence
, /* tp_as_sequence */
2221 0, /* tp_as_mapping */
2222 frozenset_hash
, /* tp_hash */
2225 PyObject_GenericGetAttr
, /* tp_getattro */
2226 0, /* tp_setattro */
2227 0, /* tp_as_buffer */
2228 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES
|
2229 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2230 frozenset_doc
, /* tp_doc */
2231 (traverseproc
)set_traverse
, /* tp_traverse */
2232 (inquiry
)set_clear_internal
, /* tp_clear */
2233 (richcmpfunc
)set_richcompare
, /* tp_richcompare */
2234 offsetof(PySetObject
, weakreflist
), /* tp_weaklistoffset */
2235 (getiterfunc
)set_iter
, /* tp_iter */
2236 0, /* tp_iternext */
2237 frozenset_methods
, /* tp_methods */
2242 0, /* tp_descr_get */
2243 0, /* tp_descr_set */
2244 0, /* tp_dictoffset */
2246 PyType_GenericAlloc
, /* tp_alloc */
2247 frozenset_new
, /* tp_new */
2248 PyObject_GC_Del
, /* tp_free */
2252 /***** C API functions *************************************************/
2255 PySet_New(PyObject
*iterable
)
2257 return make_new_set(&PySet_Type
, iterable
);
2261 PyFrozenSet_New(PyObject
*iterable
)
2263 return make_new_set(&PyFrozenSet_Type
, iterable
);
2267 PySet_Size(PyObject
*anyset
)
2269 if (!PyAnySet_Check(anyset
)) {
2270 PyErr_BadInternalCall();
2273 return PySet_GET_SIZE(anyset
);
2277 PySet_Clear(PyObject
*set
)
2279 if (!PySet_Check(set
)) {
2280 PyErr_BadInternalCall();
2283 return set_clear_internal((PySetObject
*)set
);
2287 PySet_Contains(PyObject
*anyset
, PyObject
*key
)
2289 if (!PyAnySet_Check(anyset
)) {
2290 PyErr_BadInternalCall();
2293 return set_contains_key((PySetObject
*)anyset
, key
);
2297 PySet_Discard(PyObject
*set
, PyObject
*key
)
2299 if (!PySet_Check(set
)) {
2300 PyErr_BadInternalCall();
2303 return set_discard_key((PySetObject
*)set
, key
);
2307 PySet_Add(PyObject
*anyset
, PyObject
*key
)
2309 if (!PySet_Check(anyset
) &&
2310 (!PyFrozenSet_Check(anyset
) || Py_REFCNT(anyset
) != 1)) {
2311 PyErr_BadInternalCall();
2314 return set_add_key((PySetObject
*)anyset
, key
);
2318 _PySet_Next(PyObject
*set
, Py_ssize_t
*pos
, PyObject
**key
)
2320 setentry
*entry_ptr
;
2322 if (!PyAnySet_Check(set
)) {
2323 PyErr_BadInternalCall();
2326 if (set_next((PySetObject
*)set
, pos
, &entry_ptr
) == 0)
2328 *key
= entry_ptr
->key
;
2333 _PySet_NextEntry(PyObject
*set
, Py_ssize_t
*pos
, PyObject
**key
, long *hash
)
2337 if (!PyAnySet_Check(set
)) {
2338 PyErr_BadInternalCall();
2341 if (set_next((PySetObject
*)set
, pos
, &entry
) == 0)
2344 *hash
= entry
->hash
;
2349 PySet_Pop(PyObject
*set
)
2351 if (!PySet_Check(set
)) {
2352 PyErr_BadInternalCall();
2355 return set_pop((PySetObject
*)set
);
2359 _PySet_Update(PyObject
*set
, PyObject
*iterable
)
2361 if (!PySet_Check(set
)) {
2362 PyErr_BadInternalCall();
2365 return set_update_internal((PySetObject
*)set
, iterable
);
2370 /* Test code to be called with any three element set.
2371 Returns True and original set is restored. */
2373 #define assertRaises(call_return_value, exception) \
2375 assert(call_return_value); \
2376 assert(PyErr_ExceptionMatches(exception)); \
2381 test_c_api(PySetObject
*so
)
2386 PyObject
*elem
=NULL
, *dup
=NULL
, *t
, *f
, *dup2
, *x
;
2387 PyObject
*ob
= (PyObject
*)so
;
2389 /* Verify preconditions and exercise type/size checks */
2390 assert(PyAnySet_Check(ob
));
2391 assert(PyAnySet_CheckExact(ob
));
2392 assert(!PyFrozenSet_CheckExact(ob
));
2393 assert(PySet_Size(ob
) == 3);
2394 assert(PySet_GET_SIZE(ob
) == 3);
2396 /* Raise TypeError for non-iterable constructor arguments */
2397 assertRaises(PySet_New(Py_None
) == NULL
, PyExc_TypeError
);
2398 assertRaises(PyFrozenSet_New(Py_None
) == NULL
, PyExc_TypeError
);
2400 /* Raise TypeError for unhashable key */
2401 dup
= PySet_New(ob
);
2402 assertRaises(PySet_Discard(ob
, dup
) == -1, PyExc_TypeError
);
2403 assertRaises(PySet_Contains(ob
, dup
) == -1, PyExc_TypeError
);
2404 assertRaises(PySet_Add(ob
, dup
) == -1, PyExc_TypeError
);
2406 /* Exercise successful pop, contains, add, and discard */
2407 elem
= PySet_Pop(ob
);
2408 assert(PySet_Contains(ob
, elem
) == 0);
2409 assert(PySet_GET_SIZE(ob
) == 2);
2410 assert(PySet_Add(ob
, elem
) == 0);
2411 assert(PySet_Contains(ob
, elem
) == 1);
2412 assert(PySet_GET_SIZE(ob
) == 3);
2413 assert(PySet_Discard(ob
, elem
) == 1);
2414 assert(PySet_GET_SIZE(ob
) == 2);
2415 assert(PySet_Discard(ob
, elem
) == 0);
2416 assert(PySet_GET_SIZE(ob
) == 2);
2418 /* Exercise clear */
2419 dup2
= PySet_New(dup
);
2420 assert(PySet_Clear(dup2
) == 0);
2421 assert(PySet_Size(dup2
) == 0);
2424 /* Raise SystemError on clear or update of frozen set */
2425 f
= PyFrozenSet_New(dup
);
2426 assertRaises(PySet_Clear(f
) == -1, PyExc_SystemError
);
2427 assertRaises(_PySet_Update(f
, dup
) == -1, PyExc_SystemError
);
2428 assert(PySet_Add(f
, elem
) == 0);
2430 assertRaises(PySet_Add(f
, elem
) == -1, PyExc_SystemError
);
2434 /* Exercise direct iteration */
2436 while (_PySet_Next((PyObject
*)dup
, &i
, &x
)) {
2437 s
= PyString_AsString(x
);
2438 assert(s
&& (s
[0] == 'a' || s
[0] == 'b' || s
[0] == 'c'));
2443 /* Exercise updates */
2444 dup2
= PySet_New(NULL
);
2445 assert(_PySet_Update(dup2
, dup
) == 0);
2446 assert(PySet_Size(dup2
) == 3);
2447 assert(_PySet_Update(dup2
, dup
) == 0);
2448 assert(PySet_Size(dup2
) == 3);
2451 /* Raise SystemError when self argument is not a set or frozenset. */
2453 assertRaises(PySet_Size(t
) == -1, PyExc_SystemError
);
2454 assertRaises(PySet_Contains(t
, elem
) == -1, PyExc_SystemError
);
2457 /* Raise SystemError when self argument is not a set. */
2458 f
= PyFrozenSet_New(dup
);
2459 assert(PySet_Size(f
) == 3);
2460 assert(PyFrozenSet_CheckExact(f
));
2461 assertRaises(PySet_Discard(f
, elem
) == -1, PyExc_SystemError
);
2462 assertRaises(PySet_Pop(f
) == NULL
, PyExc_SystemError
);
2465 /* Raise KeyError when popping from an empty set */
2466 assert(PyNumber_InPlaceSubtract(ob
, ob
) == ob
);
2468 assert(PySet_GET_SIZE(ob
) == 0);
2469 assertRaises(PySet_Pop(ob
) == NULL
, PyExc_KeyError
);
2471 /* Restore the set from the copy using the PyNumber API */
2472 assert(PyNumber_InPlaceOr(ob
, dup
) == ob
);
2475 /* Verify constructors accept NULL arguments */
2476 f
= PySet_New(NULL
);
2478 assert(PySet_GET_SIZE(f
) == 0);
2480 f
= PyFrozenSet_New(NULL
);
2482 assert(PyFrozenSet_CheckExact(f
));
2483 assert(PySet_GET_SIZE(f
) == 0);