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 #define MAXFREESETS 80
55 static PySetObject
*free_sets
[MAXFREESETS
];
56 static int num_free_sets
= 0;
59 The basic lookup function used by all operations.
60 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
61 Open addressing is preferred over chaining since the link overhead for
62 chaining would be substantial (100% with typical malloc overhead).
64 The initial probe index is computed as hash mod the table size. Subsequent
65 probe indices are computed as explained in Objects/dictobject.c.
67 All arithmetic on hash should ignore overflow.
69 Unlike the dictionary implementation, the lookkey functions can return
70 NULL if the rich comparison returns an error.
74 set_lookkey(PySetObject
*so
, PyObject
*key
, register long hash
)
76 register Py_ssize_t i
;
77 register size_t perturb
;
78 register setentry
*freeslot
;
79 register size_t mask
= so
->mask
;
80 setentry
*table
= so
->table
;
81 register setentry
*entry
;
87 if (entry
->key
== NULL
|| entry
->key
== key
)
90 if (entry
->key
== dummy
)
93 if (entry
->hash
== hash
) {
94 startkey
= entry
->key
;
95 cmp
= PyObject_RichCompareBool(startkey
, key
, Py_EQ
);
98 if (table
== so
->table
&& entry
->key
== startkey
) {
103 /* The compare did major nasty stuff to the
106 return set_lookkey(so
, key
, hash
);
112 /* In the loop, key == dummy is by far (factor of 100s) the
113 least likely outcome, so test for that last. */
114 for (perturb
= hash
; ; perturb
>>= PERTURB_SHIFT
) {
115 i
= (i
<< 2) + i
+ perturb
+ 1;
116 entry
= &table
[i
& mask
];
117 if (entry
->key
== NULL
) {
118 if (freeslot
!= NULL
)
122 if (entry
->key
== key
)
124 if (entry
->hash
== hash
&& entry
->key
!= dummy
) {
125 startkey
= entry
->key
;
126 cmp
= PyObject_RichCompareBool(startkey
, key
, Py_EQ
);
129 if (table
== so
->table
&& entry
->key
== startkey
) {
134 /* The compare did major nasty stuff to the
137 return set_lookkey(so
, key
, hash
);
140 else if (entry
->key
== dummy
&& freeslot
== NULL
)
147 * Hacked up version of set_lookkey which can assume keys are always strings;
148 * This means we can always use _PyString_Eq directly and not have to check to
149 * see if the comparison altered the table.
152 set_lookkey_string(PySetObject
*so
, PyObject
*key
, register long hash
)
154 register Py_ssize_t i
;
155 register size_t perturb
;
156 register setentry
*freeslot
;
157 register size_t mask
= so
->mask
;
158 setentry
*table
= so
->table
;
159 register setentry
*entry
;
161 /* Make sure this function doesn't have to handle non-string keys,
162 including subclasses of str; e.g., one reason to subclass
163 strings is to override __eq__, and for speed we don't cater to
165 if (!PyString_CheckExact(key
)) {
166 so
->lookup
= set_lookkey
;
167 return set_lookkey(so
, key
, hash
);
171 if (entry
->key
== NULL
|| entry
->key
== key
)
173 if (entry
->key
== dummy
)
176 if (entry
->hash
== hash
&& _PyString_Eq(entry
->key
, key
))
181 /* In the loop, key == dummy is by far (factor of 100s) the
182 least likely outcome, so test for that last. */
183 for (perturb
= hash
; ; perturb
>>= PERTURB_SHIFT
) {
184 i
= (i
<< 2) + i
+ perturb
+ 1;
185 entry
= &table
[i
& mask
];
186 if (entry
->key
== NULL
)
187 return freeslot
== NULL
? entry
: freeslot
;
188 if (entry
->key
== key
189 || (entry
->hash
== hash
190 && entry
->key
!= dummy
191 && _PyString_Eq(entry
->key
, key
)))
193 if (entry
->key
== dummy
&& freeslot
== NULL
)
196 assert(0); /* NOT REACHED */
201 Internal routine to insert a new key into the table.
202 Used by the public insert routine.
203 Eats a reference to key.
206 set_insert_key(register PySetObject
*so
, PyObject
*key
, long hash
)
208 register setentry
*entry
;
209 typedef setentry
*(*lookupfunc
)(PySetObject
*, PyObject
*, long);
211 assert(so
->lookup
!= NULL
);
212 entry
= so
->lookup(so
, key
, hash
);
215 if (entry
->key
== NULL
) {
221 } else if (entry
->key
== dummy
) {
235 Internal routine used by set_table_resize() to insert an item which is
236 known to be absent from the set. This routine also assumes that
237 the set contains no deleted entries. Besides the performance benefit,
238 using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
239 Note that no refcounts are changed by this routine; if needed, the caller
240 is responsible for incref'ing `key`.
243 set_insert_clean(register PySetObject
*so
, PyObject
*key
, long hash
)
246 register size_t perturb
;
247 register size_t mask
= (size_t)so
->mask
;
248 setentry
*table
= so
->table
;
249 register setentry
*entry
;
253 for (perturb
= hash
; entry
->key
!= NULL
; perturb
>>= PERTURB_SHIFT
) {
254 i
= (i
<< 2) + i
+ perturb
+ 1;
255 entry
= &table
[i
& mask
];
264 Restructure the table by allocating a new table and reinserting all
265 keys again. When entries have been deleted, the new table may
266 actually be smaller than the old one.
269 set_table_resize(PySetObject
*so
, Py_ssize_t minused
)
272 setentry
*oldtable
, *newtable
, *entry
;
274 int is_oldtable_malloced
;
275 setentry small_copy
[PySet_MINSIZE
];
277 assert(minused
>= 0);
279 /* Find the smallest table size > minused. */
280 for (newsize
= PySet_MINSIZE
;
281 newsize
<= minused
&& newsize
> 0;
289 /* Get space for a new table. */
290 oldtable
= so
->table
;
291 assert(oldtable
!= NULL
);
292 is_oldtable_malloced
= oldtable
!= so
->smalltable
;
294 if (newsize
== PySet_MINSIZE
) {
295 /* A large table is shrinking, or we can't get any smaller. */
296 newtable
= so
->smalltable
;
297 if (newtable
== oldtable
) {
298 if (so
->fill
== so
->used
) {
299 /* No dummies, so no point doing anything. */
302 /* We're not going to resize it, but rebuild the
303 table anyway to purge old dummy entries.
304 Subtle: This is *necessary* if fill==size,
305 as set_lookkey needs at least one virgin slot to
306 terminate failing searches. If fill < size, it's
307 merely desirable, as dummies slow searches. */
308 assert(so
->fill
> so
->used
);
309 memcpy(small_copy
, oldtable
, sizeof(small_copy
));
310 oldtable
= small_copy
;
314 newtable
= PyMem_NEW(setentry
, newsize
);
315 if (newtable
== NULL
) {
321 /* Make the set empty, using the new table. */
322 assert(newtable
!= oldtable
);
323 so
->table
= newtable
;
324 so
->mask
= newsize
- 1;
325 memset(newtable
, 0, sizeof(setentry
) * newsize
);
330 /* Copy the data over; this is refcount-neutral for active entries;
331 dummy entries aren't copied over, of course */
332 for (entry
= oldtable
; i
> 0; entry
++) {
333 if (entry
->key
== NULL
) {
336 } else if (entry
->key
== dummy
) {
339 assert(entry
->key
== dummy
);
340 Py_DECREF(entry
->key
);
344 set_insert_clean(so
, entry
->key
, entry
->hash
);
348 if (is_oldtable_malloced
)
353 /* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
356 set_add_entry(register PySetObject
*so
, setentry
*entry
)
358 register Py_ssize_t n_used
;
360 assert(so
->fill
<= so
->mask
); /* at least one empty slot */
362 Py_INCREF(entry
->key
);
363 if (set_insert_key(so
, entry
->key
, entry
->hash
) == -1) {
364 Py_DECREF(entry
->key
);
367 if (!(so
->used
> n_used
&& so
->fill
*3 >= (so
->mask
+1)*2))
369 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
373 set_add_key(register PySetObject
*so
, PyObject
*key
)
376 register Py_ssize_t n_used
;
378 if (!PyString_CheckExact(key
) ||
379 (hash
= ((PyStringObject
*) key
)->ob_shash
) == -1) {
380 hash
= PyObject_Hash(key
);
384 assert(so
->fill
<= so
->mask
); /* at least one empty slot */
387 if (set_insert_key(so
, key
, hash
) == -1) {
391 if (!(so
->used
> n_used
&& so
->fill
*3 >= (so
->mask
+1)*2))
393 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
396 #define DISCARD_NOTFOUND 0
397 #define DISCARD_FOUND 1
400 set_discard_entry(PySetObject
*so
, setentry
*oldentry
)
401 { register setentry
*entry
;
404 entry
= (so
->lookup
)(so
, oldentry
->key
, oldentry
->hash
);
407 if (entry
->key
== NULL
|| entry
->key
== dummy
)
408 return DISCARD_NOTFOUND
;
409 old_key
= entry
->key
;
414 return DISCARD_FOUND
;
418 set_discard_key(PySetObject
*so
, PyObject
*key
)
421 register setentry
*entry
;
424 assert (PyAnySet_Check(so
));
425 if (!PyString_CheckExact(key
) ||
426 (hash
= ((PyStringObject
*) key
)->ob_shash
) == -1) {
427 hash
= PyObject_Hash(key
);
431 entry
= (so
->lookup
)(so
, key
, hash
);
434 if (entry
->key
== NULL
|| entry
->key
== dummy
)
435 return DISCARD_NOTFOUND
;
436 old_key
= entry
->key
;
441 return DISCARD_FOUND
;
445 set_clear_internal(PySetObject
*so
)
447 setentry
*entry
, *table
;
448 int table_is_malloced
;
450 setentry small_copy
[PySet_MINSIZE
];
453 assert (PyAnySet_Check(so
));
460 assert(table
!= NULL
);
461 table_is_malloced
= table
!= so
->smalltable
;
463 /* This is delicate. During the process of clearing the set,
464 * decrefs can cause the set to mutate. To avoid fatal confusion
465 * (voice of experience), we have to make the set empty before
466 * clearing the slots, and never refer to anything via so->ref while
470 if (table_is_malloced
)
471 EMPTY_TO_MINSIZE(so
);
474 /* It's a small table with something that needs to be cleared.
475 * Afraid the only safe way is to copy the set entries into
476 * another small table first.
478 memcpy(small_copy
, table
, sizeof(small_copy
));
480 EMPTY_TO_MINSIZE(so
);
482 /* else it's a small table that's already empty */
484 /* Now we can finally clear things. If C had refcounts, we could
485 * assert that the refcount on table is 1 now, i.e. that this function
486 * has unique access to it, so decref side-effects can't alter it.
488 for (entry
= table
; fill
> 0; ++entry
) {
495 Py_DECREF(entry
->key
);
499 assert(entry
->key
== NULL
);
503 if (table_is_malloced
)
509 * Iterate over a set table. Use like so:
513 * pos = 0; # important! pos should not otherwise be changed by you
514 * while (set_next(yourset, &pos, &entry)) {
515 * Refer to borrowed reference in entry->key.
518 * CAUTION: In general, it isn't safe to use set_next in a loop that
522 set_next(PySetObject
*so
, Py_ssize_t
*pos_ptr
, setentry
**entry_ptr
)
526 register setentry
*table
;
528 assert (PyAnySet_Check(so
));
533 while (i
<= mask
&& (table
[i
].key
== NULL
|| table
[i
].key
== dummy
))
538 assert(table
[i
].key
!= NULL
);
539 *entry_ptr
= &table
[i
];
544 set_dealloc(PySetObject
*so
)
546 register setentry
*entry
;
547 Py_ssize_t fill
= so
->fill
;
548 PyObject_GC_UnTrack(so
);
549 Py_TRASHCAN_SAFE_BEGIN(so
)
550 if (so
->weakreflist
!= NULL
)
551 PyObject_ClearWeakRefs((PyObject
*) so
);
553 for (entry
= so
->table
; fill
> 0; entry
++) {
556 Py_DECREF(entry
->key
);
559 if (so
->table
!= so
->smalltable
)
560 PyMem_DEL(so
->table
);
561 if (num_free_sets
< MAXFREESETS
&& PyAnySet_CheckExact(so
))
562 free_sets
[num_free_sets
++] = so
;
564 Py_Type(so
)->tp_free(so
);
565 Py_TRASHCAN_SAFE_END(so
)
569 set_tp_print(PySetObject
*so
, FILE *fp
, int flags
)
573 char *emit
= ""; /* No separator emitted on first pass */
574 char *separator
= ", ";
575 int status
= Py_ReprEnter((PyObject
*)so
);
580 Py_BEGIN_ALLOW_THREADS
581 fprintf(fp
, "%s(...)", so
->ob_type
->tp_name
);
586 Py_BEGIN_ALLOW_THREADS
587 fprintf(fp
, "%s([", so
->ob_type
->tp_name
);
589 while (set_next(so
, &pos
, &entry
)) {
590 Py_BEGIN_ALLOW_THREADS
594 if (PyObject_Print(entry
->key
, fp
, 0) != 0) {
595 Py_ReprLeave((PyObject
*)so
);
599 Py_BEGIN_ALLOW_THREADS
602 Py_ReprLeave((PyObject
*)so
);
607 set_repr(PySetObject
*so
)
609 PyObject
*keys
, *result
=NULL
, *listrepr
;
610 int status
= Py_ReprEnter((PyObject
*)so
);
615 return PyString_FromFormat("%s(...)", so
->ob_type
->tp_name
);
618 keys
= PySequence_List((PyObject
*)so
);
621 listrepr
= PyObject_Repr(keys
);
623 if (listrepr
== NULL
)
626 result
= PyString_FromFormat("%s(%s)", so
->ob_type
->tp_name
,
627 PyString_AS_STRING(listrepr
));
630 Py_ReprLeave((PyObject
*)so
);
635 set_len(PyObject
*so
)
637 return ((PySetObject
*)so
)->used
;
641 set_merge(PySetObject
*so
, PyObject
*otherset
)
644 register Py_ssize_t i
;
645 register setentry
*entry
;
647 assert (PyAnySet_Check(so
));
648 assert (PyAnySet_Check(otherset
));
650 other
= (PySetObject
*)otherset
;
651 if (other
== so
|| other
->used
== 0)
652 /* a.update(a) or a.update({}); nothing to do */
654 /* Do one big resize at the start, rather than
655 * incrementally resizing as we insert new keys. Expect
656 * that there will be no (or few) overlapping keys.
658 if ((so
->fill
+ other
->used
)*3 >= (so
->mask
+1)*2) {
659 if (set_table_resize(so
, (so
->used
+ other
->used
)*2) != 0)
662 for (i
= 0; i
<= other
->mask
; i
++) {
663 entry
= &other
->table
[i
];
664 if (entry
->key
!= NULL
&&
665 entry
->key
!= dummy
) {
666 Py_INCREF(entry
->key
);
667 if (set_insert_key(so
, entry
->key
, entry
->hash
) == -1) {
668 Py_DECREF(entry
->key
);
677 set_contains_key(PySetObject
*so
, PyObject
*key
)
682 if (!PyString_CheckExact(key
) ||
683 (hash
= ((PyStringObject
*) key
)->ob_shash
) == -1) {
684 hash
= PyObject_Hash(key
);
688 entry
= (so
->lookup
)(so
, key
, hash
);
692 return key
!= NULL
&& key
!= dummy
;
696 set_contains_entry(PySetObject
*so
, setentry
*entry
)
701 lu_entry
= (so
->lookup
)(so
, entry
->key
, entry
->hash
);
702 if (lu_entry
== NULL
)
705 return key
!= NULL
&& key
!= dummy
;
709 set_pop(PySetObject
*so
)
711 register Py_ssize_t i
= 0;
712 register setentry
*entry
;
715 assert (PyAnySet_Check(so
));
717 PyErr_SetString(PyExc_KeyError
, "pop from an empty set");
721 /* Set entry to "the first" unused or dummy set entry. We abuse
722 * the hash field of slot 0 to hold a search finger:
723 * If slot 0 has a value, use slot 0.
724 * Else slot 0 is being used to hold a search finger,
725 * and we use its hash value as the first index to look.
727 entry
= &so
->table
[0];
728 if (entry
->key
== NULL
|| entry
->key
== dummy
) {
730 /* The hash field may be a real hash value, or it may be a
731 * legit search finger, or it may be a once-legit search
732 * finger that's out of bounds now because it wrapped around
733 * or the table shrunk -- simply make sure it's in bounds now.
735 if (i
> so
->mask
|| i
< 1)
736 i
= 1; /* skip slot 0 */
737 while ((entry
= &so
->table
[i
])->key
== NULL
|| entry
->key
==dummy
) {
747 so
->table
[0].hash
= i
+ 1; /* next place to start */
751 PyDoc_STRVAR(pop_doc
, "Remove and return an arbitrary set element.");
754 set_traverse(PySetObject
*so
, visitproc visit
, void *arg
)
759 while (set_next(so
, &pos
, &entry
))
760 Py_VISIT(entry
->key
);
765 frozenset_hash(PyObject
*self
)
767 PySetObject
*so
= (PySetObject
*)self
;
768 long h
, hash
= 1927868237L;
775 hash
*= PySet_GET_SIZE(self
) + 1;
776 while (set_next(so
, &pos
, &entry
)) {
777 /* Work to increase the bit dispersion for closely spaced hash
778 values. The is important because some use cases have many
779 combinations of a small number of elements with nearby
780 hashes so that many distinct combinations collapse to only
781 a handful of distinct hash values. */
783 hash
^= (h
^ (h
<< 16) ^ 89869747L) * 3644798167u;
785 hash
= hash
* 69069L + 907133923L;
793 set_nohash(PyObject
*self
)
795 PyErr_SetString(PyExc_TypeError
, "set objects are unhashable");
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_len(setiterobject
*si
)
820 if (si
->si_set
!= NULL
&& si
->si_used
== si
->si_set
->used
)
822 return PyInt_FromLong(len
);
825 PyDoc_STRVAR(length_hint_doc
, "Private method returning an estimate of len(list(it)).");
827 static PyMethodDef setiter_methods
[] = {
828 {"__length_hint__", (PyCFunction
)setiter_len
, METH_NOARGS
, length_hint_doc
},
829 {NULL
, NULL
} /* sentinel */
832 static PyObject
*setiter_iternext(setiterobject
*si
)
835 register Py_ssize_t i
, mask
;
836 register setentry
*entry
;
837 PySetObject
*so
= si
->si_set
;
841 assert (PyAnySet_Check(so
));
843 if (si
->si_used
!= so
->used
) {
844 PyErr_SetString(PyExc_RuntimeError
,
845 "Set changed size during iteration");
846 si
->si_used
= -1; /* Make this state sticky */
854 while (i
<= mask
&& (entry
[i
].key
== NULL
|| entry
[i
].key
== dummy
))
870 static PyTypeObject PySetIter_Type
= {
871 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
872 "setiterator", /* tp_name */
873 sizeof(setiterobject
), /* tp_basicsize */
876 (destructor
)setiter_dealloc
, /* tp_dealloc */
882 0, /* tp_as_number */
883 0, /* tp_as_sequence */
884 0, /* tp_as_mapping */
888 PyObject_GenericGetAttr
, /* tp_getattro */
890 0, /* tp_as_buffer */
891 Py_TPFLAGS_DEFAULT
, /* tp_flags */
895 0, /* tp_richcompare */
896 0, /* tp_weaklistoffset */
897 PyObject_SelfIter
, /* tp_iter */
898 (iternextfunc
)setiter_iternext
, /* tp_iternext */
899 setiter_methods
, /* tp_methods */
904 set_iter(PySetObject
*so
)
906 setiterobject
*si
= PyObject_New(setiterobject
, &PySetIter_Type
);
911 si
->si_used
= so
->used
;
914 return (PyObject
*)si
;
918 set_update_internal(PySetObject
*so
, PyObject
*other
)
922 if (PyAnySet_CheckExact(other
))
923 return set_merge(so
, other
);
925 if (PyDict_CheckExact(other
)) {
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.
937 if ((so
->fill
+ dictsize
)*3 >= (so
->mask
+1)*2) {
938 if (set_table_resize(so
, (so
->used
+ dictsize
)*2) != 0)
941 while (_PyDict_Next(other
, &pos
, &key
, &value
, &hash
)) {
944 an_entry
.hash
= hash
;
946 if (set_add_entry(so
, &an_entry
) == -1)
952 it
= PyObject_GetIter(other
);
956 while ((key
= PyIter_Next(it
)) != NULL
) {
957 if (set_add_key(so
, key
) == -1) {
965 if (PyErr_Occurred())
971 set_update(PySetObject
*so
, PyObject
*other
)
973 if (set_update_internal(so
, other
) == -1)
978 PyDoc_STRVAR(update_doc
,
979 "Update a set with the union of itself and another.");
982 make_new_set(PyTypeObject
*type
, PyObject
*iterable
)
984 register PySetObject
*so
= NULL
;
986 if (dummy
== NULL
) { /* Auto-initialize dummy */
987 dummy
= PyString_FromString("<dummy key>");
992 /* create PySetObject structure */
994 (type
== &PySet_Type
|| type
== &PyFrozenSet_Type
)) {
995 so
= free_sets
[--num_free_sets
];
996 assert (so
!= NULL
&& PyAnySet_CheckExact(so
));
998 _Py_NewReference((PyObject
*)so
);
999 EMPTY_TO_MINSIZE(so
);
1000 PyObject_GC_Track(so
);
1002 so
= (PySetObject
*)type
->tp_alloc(type
, 0);
1005 /* tp_alloc has already zeroed the structure */
1006 assert(so
->table
== NULL
&& so
->fill
== 0 && so
->used
== 0);
1007 INIT_NONZERO_SET_SLOTS(so
);
1010 so
->lookup
= set_lookkey_string
;
1011 so
->weakreflist
= NULL
;
1013 if (iterable
!= NULL
) {
1014 if (set_update_internal(so
, iterable
) == -1) {
1020 return (PyObject
*)so
;
1023 /* The empty frozenset is a singleton */
1024 static PyObject
*emptyfrozenset
= NULL
;
1027 frozenset_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1029 PyObject
*iterable
= NULL
, *result
;
1031 if (type
== &PyFrozenSet_Type
&& !_PyArg_NoKeywords("frozenset()", kwds
))
1034 if (!PyArg_UnpackTuple(args
, type
->tp_name
, 0, 1, &iterable
))
1037 if (type
!= &PyFrozenSet_Type
)
1038 return make_new_set(type
, iterable
);
1040 if (iterable
!= NULL
) {
1041 /* frozenset(f) is idempotent */
1042 if (PyFrozenSet_CheckExact(iterable
)) {
1043 Py_INCREF(iterable
);
1046 result
= make_new_set(type
, iterable
);
1047 if (result
== NULL
|| PySet_GET_SIZE(result
))
1051 /* The empty frozenset is a singleton */
1052 if (emptyfrozenset
== NULL
)
1053 emptyfrozenset
= make_new_set(type
, NULL
);
1054 Py_XINCREF(emptyfrozenset
);
1055 return emptyfrozenset
;
1063 while (num_free_sets
) {
1065 so
= free_sets
[num_free_sets
];
1066 PyObject_GC_Del(so
);
1069 Py_CLEAR(emptyfrozenset
);
1073 set_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1075 if (type
== &PySet_Type
&& !_PyArg_NoKeywords("set()", kwds
))
1078 return make_new_set(type
, NULL
);
1081 /* set_swap_bodies() switches the contents of any two sets by moving their
1082 internal data pointers and, if needed, copying the internal smalltables.
1083 Semantically equivalent to:
1085 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1087 The function always succeeds and it leaves both objects in a stable state.
1088 Useful for creating temporary frozensets from sets for membership testing
1089 in __contains__(), discard(), and remove(). Also useful for operations
1090 that update in-place (by allowing an intermediate result to be swapped
1091 into one of the original inputs).
1095 set_swap_bodies(PySetObject
*a
, PySetObject
*b
)
1099 setentry
*(*f
)(PySetObject
*so
, PyObject
*key
, long hash
);
1100 setentry tab
[PySet_MINSIZE
];
1103 t
= a
->fill
; a
->fill
= b
->fill
; b
->fill
= t
;
1104 t
= a
->used
; a
->used
= b
->used
; b
->used
= t
;
1105 t
= a
->mask
; a
->mask
= b
->mask
; b
->mask
= t
;
1108 if (a
->table
== a
->smalltable
)
1110 a
->table
= b
->table
;
1111 if (b
->table
== b
->smalltable
)
1112 a
->table
= a
->smalltable
;
1115 f
= a
->lookup
; a
->lookup
= b
->lookup
; b
->lookup
= f
;
1117 if (a
->table
== a
->smalltable
|| b
->table
== b
->smalltable
) {
1118 memcpy(tab
, a
->smalltable
, sizeof(tab
));
1119 memcpy(a
->smalltable
, b
->smalltable
, sizeof(tab
));
1120 memcpy(b
->smalltable
, tab
, sizeof(tab
));
1123 if (PyType_IsSubtype(Py_Type(a
), &PyFrozenSet_Type
) &&
1124 PyType_IsSubtype(Py_Type(b
), &PyFrozenSet_Type
)) {
1125 h
= a
->hash
; a
->hash
= b
->hash
; b
->hash
= h
;
1133 set_copy(PySetObject
*so
)
1135 return make_new_set(Py_Type(so
), (PyObject
*)so
);
1139 frozenset_copy(PySetObject
*so
)
1141 if (PyFrozenSet_CheckExact(so
)) {
1143 return (PyObject
*)so
;
1145 return set_copy(so
);
1148 PyDoc_STRVAR(copy_doc
, "Return a shallow copy of a set.");
1151 set_clear(PySetObject
*so
)
1153 set_clear_internal(so
);
1157 PyDoc_STRVAR(clear_doc
, "Remove all elements from this set.");
1160 set_union(PySetObject
*so
, PyObject
*other
)
1162 PySetObject
*result
;
1164 result
= (PySetObject
*)set_copy(so
);
1167 if ((PyObject
*)so
== other
)
1168 return (PyObject
*)result
;
1169 if (set_update_internal(result
, other
) == -1) {
1173 return (PyObject
*)result
;
1176 PyDoc_STRVAR(union_doc
,
1177 "Return the union of two sets as a new set.\n\
1179 (i.e. all elements that are in either set.)");
1182 set_or(PySetObject
*so
, PyObject
*other
)
1184 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1185 Py_INCREF(Py_NotImplemented
);
1186 return Py_NotImplemented
;
1188 return set_union(so
, other
);
1192 set_ior(PySetObject
*so
, PyObject
*other
)
1194 if (!PyAnySet_Check(other
)) {
1195 Py_INCREF(Py_NotImplemented
);
1196 return Py_NotImplemented
;
1198 if (set_update_internal(so
, other
) == -1)
1201 return (PyObject
*)so
;
1205 set_intersection(PySetObject
*so
, PyObject
*other
)
1207 PySetObject
*result
;
1208 PyObject
*key
, *it
, *tmp
;
1210 if ((PyObject
*)so
== other
)
1211 return set_copy(so
);
1213 result
= (PySetObject
*)make_new_set(Py_Type(so
), NULL
);
1217 if (PyAnySet_CheckExact(other
)) {
1221 if (PySet_GET_SIZE(other
) > PySet_GET_SIZE(so
)) {
1222 tmp
= (PyObject
*)so
;
1223 so
= (PySetObject
*)other
;
1227 while (set_next((PySetObject
*)other
, &pos
, &entry
)) {
1228 int rv
= set_contains_entry(so
, entry
);
1234 if (set_add_entry(result
, entry
) == -1) {
1240 return (PyObject
*)result
;
1243 it
= PyObject_GetIter(other
);
1249 while ((key
= PyIter_Next(it
)) != NULL
) {
1252 long hash
= PyObject_Hash(key
);
1262 rv
= set_contains_entry(so
, &entry
);
1270 if (set_add_entry(result
, &entry
) == -1) {
1280 if (PyErr_Occurred()) {
1284 return (PyObject
*)result
;
1287 PyDoc_STRVAR(intersection_doc
,
1288 "Return the intersection of two sets as a new set.\n\
1290 (i.e. all elements that are in both sets.)");
1293 set_intersection_update(PySetObject
*so
, PyObject
*other
)
1297 tmp
= set_intersection(so
, other
);
1300 set_swap_bodies(so
, (PySetObject
*)tmp
);
1305 PyDoc_STRVAR(intersection_update_doc
,
1306 "Update a set with the intersection of itself and another.");
1309 set_and(PySetObject
*so
, PyObject
*other
)
1311 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1312 Py_INCREF(Py_NotImplemented
);
1313 return Py_NotImplemented
;
1315 return set_intersection(so
, other
);
1319 set_iand(PySetObject
*so
, PyObject
*other
)
1323 if (!PyAnySet_Check(other
)) {
1324 Py_INCREF(Py_NotImplemented
);
1325 return Py_NotImplemented
;
1327 result
= set_intersection_update(so
, other
);
1332 return (PyObject
*)so
;
1336 set_difference_update_internal(PySetObject
*so
, PyObject
*other
)
1338 if ((PyObject
*)so
== other
)
1339 return set_clear_internal(so
);
1341 if (PyAnySet_CheckExact(other
)) {
1345 while (set_next((PySetObject
*)other
, &pos
, &entry
))
1346 if (set_discard_entry(so
, entry
) == -1)
1350 it
= PyObject_GetIter(other
);
1354 while ((key
= PyIter_Next(it
)) != NULL
) {
1355 if (set_discard_key(so
, key
) == -1) {
1363 if (PyErr_Occurred())
1366 /* If more than 1/5 are dummies, then resize them away. */
1367 if ((so
->fill
- so
->used
) * 5 < so
->mask
)
1369 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
1373 set_difference_update(PySetObject
*so
, PyObject
*other
)
1375 if (set_difference_update_internal(so
, other
) != -1)
1380 PyDoc_STRVAR(difference_update_doc
,
1381 "Remove all elements of another set from this set.");
1384 set_difference(PySetObject
*so
, PyObject
*other
)
1390 if (!PyAnySet_CheckExact(other
) && !PyDict_CheckExact(other
)) {
1391 result
= set_copy(so
);
1394 if (set_difference_update_internal((PySetObject
*)result
, other
) != -1)
1400 result
= make_new_set(Py_Type(so
), NULL
);
1404 if (PyDict_CheckExact(other
)) {
1405 while (set_next(so
, &pos
, &entry
)) {
1407 entrycopy
.hash
= entry
->hash
;
1408 entrycopy
.key
= entry
->key
;
1409 if (!_PyDict_Contains(other
, entry
->key
, entry
->hash
)) {
1410 if (set_add_entry((PySetObject
*)result
, &entrycopy
) == -1) {
1419 while (set_next(so
, &pos
, &entry
)) {
1420 int rv
= set_contains_entry((PySetObject
*)other
, entry
);
1426 if (set_add_entry((PySetObject
*)result
, entry
) == -1) {
1435 PyDoc_STRVAR(difference_doc
,
1436 "Return the difference of two sets as a new set.\n\
1438 (i.e. all elements that are in this set but not the other.)");
1440 set_sub(PySetObject
*so
, PyObject
*other
)
1442 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1443 Py_INCREF(Py_NotImplemented
);
1444 return Py_NotImplemented
;
1446 return set_difference(so
, other
);
1450 set_isub(PySetObject
*so
, PyObject
*other
)
1454 if (!PyAnySet_Check(other
)) {
1455 Py_INCREF(Py_NotImplemented
);
1456 return Py_NotImplemented
;
1458 result
= set_difference_update(so
, other
);
1463 return (PyObject
*)so
;
1467 set_symmetric_difference_update(PySetObject
*so
, PyObject
*other
)
1469 PySetObject
*otherset
;
1474 if ((PyObject
*)so
== other
)
1475 return set_clear(so
);
1477 if (PyDict_CheckExact(other
)) {
1481 while (_PyDict_Next(other
, &pos
, &key
, &value
, &hash
)) {
1484 an_entry
.hash
= hash
;
1486 rv
= set_discard_entry(so
, &an_entry
);
1489 if (rv
== DISCARD_NOTFOUND
) {
1490 if (set_add_entry(so
, &an_entry
) == -1)
1497 if (PyAnySet_CheckExact(other
)) {
1499 otherset
= (PySetObject
*)other
;
1501 otherset
= (PySetObject
*)make_new_set(Py_Type(so
), other
);
1502 if (otherset
== NULL
)
1506 while (set_next(otherset
, &pos
, &entry
)) {
1507 int rv
= set_discard_entry(so
, entry
);
1509 Py_DECREF(otherset
);
1512 if (rv
== DISCARD_NOTFOUND
) {
1513 if (set_add_entry(so
, entry
) == -1) {
1514 Py_DECREF(otherset
);
1519 Py_DECREF(otherset
);
1523 PyDoc_STRVAR(symmetric_difference_update_doc
,
1524 "Update a set with the symmetric difference of itself and another.");
1527 set_symmetric_difference(PySetObject
*so
, PyObject
*other
)
1530 PySetObject
*otherset
;
1532 otherset
= (PySetObject
*)make_new_set(Py_Type(so
), other
);
1533 if (otherset
== NULL
)
1535 rv
= set_symmetric_difference_update(otherset
, (PyObject
*)so
);
1539 return (PyObject
*)otherset
;
1542 PyDoc_STRVAR(symmetric_difference_doc
,
1543 "Return the symmetric difference of two sets as a new set.\n\
1545 (i.e. all elements that are in exactly one of the sets.)");
1548 set_xor(PySetObject
*so
, PyObject
*other
)
1550 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1551 Py_INCREF(Py_NotImplemented
);
1552 return Py_NotImplemented
;
1554 return set_symmetric_difference(so
, other
);
1558 set_ixor(PySetObject
*so
, PyObject
*other
)
1562 if (!PyAnySet_Check(other
)) {
1563 Py_INCREF(Py_NotImplemented
);
1564 return Py_NotImplemented
;
1566 result
= set_symmetric_difference_update(so
, other
);
1571 return (PyObject
*)so
;
1575 set_issubset(PySetObject
*so
, PyObject
*other
)
1580 if (!PyAnySet_CheckExact(other
)) {
1581 PyObject
*tmp
, *result
;
1582 tmp
= make_new_set(&PySet_Type
, other
);
1585 result
= set_issubset(so
, tmp
);
1589 if (PySet_GET_SIZE(so
) > PySet_GET_SIZE(other
))
1592 while (set_next(so
, &pos
, &entry
)) {
1593 int rv
= set_contains_entry((PySetObject
*)other
, entry
);
1602 PyDoc_STRVAR(issubset_doc
, "Report whether another set contains this set.");
1605 set_issuperset(PySetObject
*so
, PyObject
*other
)
1607 PyObject
*tmp
, *result
;
1609 if (!PyAnySet_CheckExact(other
)) {
1610 tmp
= make_new_set(&PySet_Type
, other
);
1613 result
= set_issuperset(so
, tmp
);
1617 return set_issubset((PySetObject
*)other
, (PyObject
*)so
);
1620 PyDoc_STRVAR(issuperset_doc
, "Report whether this set contains another set.");
1623 set_richcompare(PySetObject
*v
, PyObject
*w
, int op
)
1627 if(!PyAnySet_Check(w
)) {
1632 PyErr_SetString(PyExc_TypeError
, "can only compare to a set");
1637 if (PySet_GET_SIZE(v
) != PySet_GET_SIZE(w
))
1639 if (v
->hash
!= -1 &&
1640 ((PySetObject
*)w
)->hash
!= -1 &&
1641 v
->hash
!= ((PySetObject
*)w
)->hash
)
1643 return set_issubset(v
, w
);
1645 r1
= set_richcompare(v
, w
, Py_EQ
);
1648 r2
= PyBool_FromLong(PyObject_Not(r1
));
1652 return set_issubset(v
, w
);
1654 return set_issuperset(v
, w
);
1656 if (PySet_GET_SIZE(v
) >= PySet_GET_SIZE(w
))
1658 return set_issubset(v
, w
);
1660 if (PySet_GET_SIZE(v
) <= PySet_GET_SIZE(w
))
1662 return set_issuperset(v
, w
);
1664 Py_INCREF(Py_NotImplemented
);
1665 return Py_NotImplemented
;
1669 set_nocmp(PyObject
*self
, PyObject
*other
)
1671 PyErr_SetString(PyExc_TypeError
, "cannot compare sets using cmp()");
1676 set_add(PySetObject
*so
, PyObject
*key
)
1678 if (set_add_key(so
, key
) == -1)
1683 PyDoc_STRVAR(add_doc
,
1684 "Add an element to a set.\n\
1686 This has no effect if the element is already present.");
1689 set_contains(PySetObject
*so
, PyObject
*key
)
1694 rv
= set_contains_key(so
, key
);
1696 if (!PyAnySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1699 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1702 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1703 rv
= set_contains(so
, tmpkey
);
1704 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1711 set_direct_contains(PySetObject
*so
, PyObject
*key
)
1715 result
= set_contains(so
, key
);
1718 return PyBool_FromLong(result
);
1721 PyDoc_STRVAR(contains_doc
, "x.__contains__(y) <==> y in x.");
1724 set_remove(PySetObject
*so
, PyObject
*key
)
1726 PyObject
*tmpkey
, *result
;
1729 rv
= set_discard_key(so
, key
);
1731 if (!PyAnySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1734 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1737 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1738 result
= set_remove(so
, tmpkey
);
1739 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1742 } else if (rv
== DISCARD_NOTFOUND
) {
1749 PyDoc_STRVAR(remove_doc
,
1750 "Remove an element from a set; it must be a member.\n\
1752 If the element is not a member, raise a KeyError.");
1755 set_discard(PySetObject
*so
, PyObject
*key
)
1757 PyObject
*tmpkey
, *result
;
1760 rv
= set_discard_key(so
, key
);
1762 if (!PyAnySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1765 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1768 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1769 result
= set_discard(so
, tmpkey
);
1770 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1777 PyDoc_STRVAR(discard_doc
,
1778 "Remove an element from a set if it is a member.\n\
1780 If the element is not a member, do nothing.");
1783 set_reduce(PySetObject
*so
)
1785 PyObject
*keys
=NULL
, *args
=NULL
, *result
=NULL
, *dict
=NULL
;
1787 keys
= PySequence_List((PyObject
*)so
);
1790 args
= PyTuple_Pack(1, keys
);
1793 dict
= PyObject_GetAttrString((PyObject
*)so
, "__dict__");
1799 result
= PyTuple_Pack(3, Py_Type(so
), args
, dict
);
1807 PyDoc_STRVAR(reduce_doc
, "Return state information for pickling.");
1810 set_init(PySetObject
*self
, PyObject
*args
, PyObject
*kwds
)
1812 PyObject
*iterable
= NULL
;
1814 if (!PyAnySet_Check(self
))
1816 if (!PyArg_UnpackTuple(args
, Py_Type(self
)->tp_name
, 0, 1, &iterable
))
1818 set_clear_internal(self
);
1820 if (iterable
== NULL
)
1822 return set_update_internal(self
, iterable
);
1825 static PySequenceMethods set_as_sequence
= {
1826 set_len
, /* sq_length */
1831 0, /* sq_ass_item */
1832 0, /* sq_ass_slice */
1833 (objobjproc
)set_contains
, /* sq_contains */
1836 /* set object ********************************************************/
1839 static PyObject
*test_c_api(PySetObject
*so
);
1841 PyDoc_STRVAR(test_c_api_doc
, "Exercises C API. Returns True.\n\
1842 All is well if assertions don't fail.");
1845 static PyMethodDef set_methods
[] = {
1846 {"add", (PyCFunction
)set_add
, METH_O
,
1848 {"clear", (PyCFunction
)set_clear
, METH_NOARGS
,
1850 {"__contains__",(PyCFunction
)set_direct_contains
, METH_O
| METH_COEXIST
,
1852 {"copy", (PyCFunction
)set_copy
, METH_NOARGS
,
1854 {"discard", (PyCFunction
)set_discard
, METH_O
,
1856 {"difference", (PyCFunction
)set_difference
, METH_O
,
1858 {"difference_update", (PyCFunction
)set_difference_update
, METH_O
,
1859 difference_update_doc
},
1860 {"intersection",(PyCFunction
)set_intersection
, METH_O
,
1862 {"intersection_update",(PyCFunction
)set_intersection_update
, METH_O
,
1863 intersection_update_doc
},
1864 {"issubset", (PyCFunction
)set_issubset
, METH_O
,
1866 {"issuperset", (PyCFunction
)set_issuperset
, METH_O
,
1868 {"pop", (PyCFunction
)set_pop
, METH_NOARGS
,
1870 {"__reduce__", (PyCFunction
)set_reduce
, METH_NOARGS
,
1872 {"remove", (PyCFunction
)set_remove
, METH_O
,
1874 {"symmetric_difference",(PyCFunction
)set_symmetric_difference
, METH_O
,
1875 symmetric_difference_doc
},
1876 {"symmetric_difference_update",(PyCFunction
)set_symmetric_difference_update
, METH_O
,
1877 symmetric_difference_update_doc
},
1879 {"test_c_api", (PyCFunction
)test_c_api
, METH_NOARGS
,
1882 {"union", (PyCFunction
)set_union
, METH_O
,
1884 {"update", (PyCFunction
)set_update
, METH_O
,
1886 {NULL
, NULL
} /* sentinel */
1889 static PyNumberMethods set_as_number
= {
1891 (binaryfunc
)set_sub
, /*nb_subtract*/
1904 (binaryfunc
)set_and
, /*nb_and*/
1905 (binaryfunc
)set_xor
, /*nb_xor*/
1906 (binaryfunc
)set_or
, /*nb_or*/
1913 0, /*nb_inplace_add*/
1914 (binaryfunc
)set_isub
, /*nb_inplace_subtract*/
1915 0, /*nb_inplace_multiply*/
1916 0, /*nb_inplace_divide*/
1917 0, /*nb_inplace_remainder*/
1918 0, /*nb_inplace_power*/
1919 0, /*nb_inplace_lshift*/
1920 0, /*nb_inplace_rshift*/
1921 (binaryfunc
)set_iand
, /*nb_inplace_and*/
1922 (binaryfunc
)set_ixor
, /*nb_inplace_xor*/
1923 (binaryfunc
)set_ior
, /*nb_inplace_or*/
1926 PyDoc_STRVAR(set_doc
,
1927 "set(iterable) --> set object\n\
1929 Build an unordered collection of unique elements.");
1931 PyTypeObject PySet_Type
= {
1932 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
1933 "set", /* tp_name */
1934 sizeof(PySetObject
), /* tp_basicsize */
1935 0, /* tp_itemsize */
1937 (destructor
)set_dealloc
, /* tp_dealloc */
1938 (printfunc
)set_tp_print
, /* tp_print */
1941 set_nocmp
, /* tp_compare */
1942 (reprfunc
)set_repr
, /* tp_repr */
1943 &set_as_number
, /* tp_as_number */
1944 &set_as_sequence
, /* tp_as_sequence */
1945 0, /* tp_as_mapping */
1946 set_nohash
, /* tp_hash */
1949 PyObject_GenericGetAttr
, /* tp_getattro */
1950 0, /* tp_setattro */
1951 0, /* tp_as_buffer */
1952 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES
|
1953 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1954 set_doc
, /* tp_doc */
1955 (traverseproc
)set_traverse
, /* tp_traverse */
1956 (inquiry
)set_clear_internal
, /* tp_clear */
1957 (richcmpfunc
)set_richcompare
, /* tp_richcompare */
1958 offsetof(PySetObject
, weakreflist
), /* tp_weaklistoffset */
1959 (getiterfunc
)set_iter
, /* tp_iter */
1960 0, /* tp_iternext */
1961 set_methods
, /* tp_methods */
1966 0, /* tp_descr_get */
1967 0, /* tp_descr_set */
1968 0, /* tp_dictoffset */
1969 (initproc
)set_init
, /* tp_init */
1970 PyType_GenericAlloc
, /* tp_alloc */
1971 set_new
, /* tp_new */
1972 PyObject_GC_Del
, /* tp_free */
1975 /* frozenset object ********************************************************/
1978 static PyMethodDef frozenset_methods
[] = {
1979 {"__contains__",(PyCFunction
)set_direct_contains
, METH_O
| METH_COEXIST
,
1981 {"copy", (PyCFunction
)frozenset_copy
, METH_NOARGS
,
1983 {"difference", (PyCFunction
)set_difference
, METH_O
,
1985 {"intersection",(PyCFunction
)set_intersection
, METH_O
,
1987 {"issubset", (PyCFunction
)set_issubset
, METH_O
,
1989 {"issuperset", (PyCFunction
)set_issuperset
, METH_O
,
1991 {"__reduce__", (PyCFunction
)set_reduce
, METH_NOARGS
,
1993 {"symmetric_difference",(PyCFunction
)set_symmetric_difference
, METH_O
,
1994 symmetric_difference_doc
},
1995 {"union", (PyCFunction
)set_union
, METH_O
,
1997 {NULL
, NULL
} /* sentinel */
2000 static PyNumberMethods frozenset_as_number
= {
2002 (binaryfunc
)set_sub
, /*nb_subtract*/
2015 (binaryfunc
)set_and
, /*nb_and*/
2016 (binaryfunc
)set_xor
, /*nb_xor*/
2017 (binaryfunc
)set_or
, /*nb_or*/
2020 PyDoc_STRVAR(frozenset_doc
,
2021 "frozenset(iterable) --> frozenset object\n\
2023 Build an immutable unordered collection of unique elements.");
2025 PyTypeObject PyFrozenSet_Type
= {
2026 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2027 "frozenset", /* tp_name */
2028 sizeof(PySetObject
), /* tp_basicsize */
2029 0, /* tp_itemsize */
2031 (destructor
)set_dealloc
, /* tp_dealloc */
2032 (printfunc
)set_tp_print
, /* tp_print */
2035 set_nocmp
, /* tp_compare */
2036 (reprfunc
)set_repr
, /* tp_repr */
2037 &frozenset_as_number
, /* tp_as_number */
2038 &set_as_sequence
, /* tp_as_sequence */
2039 0, /* tp_as_mapping */
2040 frozenset_hash
, /* tp_hash */
2043 PyObject_GenericGetAttr
, /* tp_getattro */
2044 0, /* tp_setattro */
2045 0, /* tp_as_buffer */
2046 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES
|
2047 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2048 frozenset_doc
, /* tp_doc */
2049 (traverseproc
)set_traverse
, /* tp_traverse */
2050 (inquiry
)set_clear_internal
, /* tp_clear */
2051 (richcmpfunc
)set_richcompare
, /* tp_richcompare */
2052 offsetof(PySetObject
, weakreflist
), /* tp_weaklistoffset */
2053 (getiterfunc
)set_iter
, /* tp_iter */
2054 0, /* tp_iternext */
2055 frozenset_methods
, /* tp_methods */
2060 0, /* tp_descr_get */
2061 0, /* tp_descr_set */
2062 0, /* tp_dictoffset */
2064 PyType_GenericAlloc
, /* tp_alloc */
2065 frozenset_new
, /* tp_new */
2066 PyObject_GC_Del
, /* tp_free */
2070 /***** C API functions *************************************************/
2073 PySet_New(PyObject
*iterable
)
2075 return make_new_set(&PySet_Type
, iterable
);
2079 PyFrozenSet_New(PyObject
*iterable
)
2081 PyObject
*args
, *result
;
2083 if (iterable
== NULL
)
2084 args
= PyTuple_New(0);
2086 args
= PyTuple_Pack(1, iterable
);
2089 result
= frozenset_new(&PyFrozenSet_Type
, args
, NULL
);
2095 PySet_Size(PyObject
*anyset
)
2097 if (!PyAnySet_Check(anyset
)) {
2098 PyErr_BadInternalCall();
2101 return PySet_GET_SIZE(anyset
);
2105 PySet_Clear(PyObject
*set
)
2107 if (!PyType_IsSubtype(Py_Type(set
), &PySet_Type
)) {
2108 PyErr_BadInternalCall();
2111 return set_clear_internal((PySetObject
*)set
);
2115 PySet_Contains(PyObject
*anyset
, PyObject
*key
)
2117 if (!PyAnySet_Check(anyset
)) {
2118 PyErr_BadInternalCall();
2121 return set_contains_key((PySetObject
*)anyset
, key
);
2125 PySet_Discard(PyObject
*set
, PyObject
*key
)
2127 if (!PyType_IsSubtype(Py_Type(set
), &PySet_Type
)) {
2128 PyErr_BadInternalCall();
2131 return set_discard_key((PySetObject
*)set
, key
);
2135 PySet_Add(PyObject
*set
, PyObject
*key
)
2137 if (!PyType_IsSubtype(Py_Type(set
), &PySet_Type
)) {
2138 PyErr_BadInternalCall();
2141 return set_add_key((PySetObject
*)set
, key
);
2145 _PySet_Next(PyObject
*set
, Py_ssize_t
*pos
, PyObject
**key
)
2147 setentry
*entry_ptr
;
2149 if (!PyAnySet_Check(set
)) {
2150 PyErr_BadInternalCall();
2153 if (set_next((PySetObject
*)set
, pos
, &entry_ptr
) == 0)
2155 *key
= entry_ptr
->key
;
2160 _PySet_NextEntry(PyObject
*set
, Py_ssize_t
*pos
, PyObject
**key
, long *hash
)
2164 if (!PyAnySet_Check(set
)) {
2165 PyErr_BadInternalCall();
2168 if (set_next((PySetObject
*)set
, pos
, &entry
) == 0)
2171 *hash
= entry
->hash
;
2176 PySet_Pop(PyObject
*set
)
2178 if (!PyType_IsSubtype(Py_Type(set
), &PySet_Type
)) {
2179 PyErr_BadInternalCall();
2182 return set_pop((PySetObject
*)set
);
2186 _PySet_Update(PyObject
*set
, PyObject
*iterable
)
2188 if (!PyType_IsSubtype(Py_Type(set
), &PySet_Type
)) {
2189 PyErr_BadInternalCall();
2192 return set_update_internal((PySetObject
*)set
, iterable
);
2197 /* Test code to be called with any three element set.
2198 Returns True and original set is restored. */
2200 #define assertRaises(call_return_value, exception) \
2202 assert(call_return_value); \
2203 assert(PyErr_ExceptionMatches(exception)); \
2208 test_c_api(PySetObject
*so
)
2213 PyObject
*elem
=NULL
, *dup
=NULL
, *t
, *f
, *dup2
, *x
;
2214 PyObject
*ob
= (PyObject
*)so
;
2216 /* Verify preconditions and exercise type/size checks */
2217 assert(PyAnySet_Check(ob
));
2218 assert(PyAnySet_CheckExact(ob
));
2219 assert(!PyFrozenSet_CheckExact(ob
));
2220 assert(PySet_Size(ob
) == 3);
2221 assert(PySet_GET_SIZE(ob
) == 3);
2223 /* Raise TypeError for non-iterable constructor arguments */
2224 assertRaises(PySet_New(Py_None
) == NULL
, PyExc_TypeError
);
2225 assertRaises(PyFrozenSet_New(Py_None
) == NULL
, PyExc_TypeError
);
2227 /* Raise TypeError for unhashable key */
2228 dup
= PySet_New(ob
);
2229 assertRaises(PySet_Discard(ob
, dup
) == -1, PyExc_TypeError
);
2230 assertRaises(PySet_Contains(ob
, dup
) == -1, PyExc_TypeError
);
2231 assertRaises(PySet_Add(ob
, dup
) == -1, PyExc_TypeError
);
2233 /* Exercise successful pop, contains, add, and discard */
2234 elem
= PySet_Pop(ob
);
2235 assert(PySet_Contains(ob
, elem
) == 0);
2236 assert(PySet_GET_SIZE(ob
) == 2);
2237 assert(PySet_Add(ob
, elem
) == 0);
2238 assert(PySet_Contains(ob
, elem
) == 1);
2239 assert(PySet_GET_SIZE(ob
) == 3);
2240 assert(PySet_Discard(ob
, elem
) == 1);
2241 assert(PySet_GET_SIZE(ob
) == 2);
2242 assert(PySet_Discard(ob
, elem
) == 0);
2243 assert(PySet_GET_SIZE(ob
) == 2);
2245 /* Exercise clear */
2246 dup2
= PySet_New(dup
);
2247 assert(PySet_Clear(dup2
) == 0);
2248 assert(PySet_Size(dup2
) == 0);
2251 /* Raise SystemError on clear or update of frozen set */
2252 f
= PyFrozenSet_New(dup
);
2253 assertRaises(PySet_Clear(f
) == -1, PyExc_SystemError
);
2254 assertRaises(_PySet_Update(f
, dup
) == -1, PyExc_SystemError
);
2257 /* Exercise direct iteration */
2259 while (_PySet_Next((PyObject
*)dup
, &i
, &x
)) {
2260 s
= PyString_AsString(x
);
2261 assert(s
&& (s
[0] == 'a' || s
[0] == 'b' || s
[0] == 'c'));
2266 /* Exercise updates */
2267 dup2
= PySet_New(NULL
);
2268 assert(_PySet_Update(dup2
, dup
) == 0);
2269 assert(PySet_Size(dup2
) == 3);
2270 assert(_PySet_Update(dup2
, dup
) == 0);
2271 assert(PySet_Size(dup2
) == 3);
2274 /* Raise SystemError when self argument is not a set or frozenset. */
2276 assertRaises(PySet_Size(t
) == -1, PyExc_SystemError
);
2277 assertRaises(PySet_Contains(t
, elem
) == -1, PyExc_SystemError
);
2280 /* Raise SystemError when self argument is not a set. */
2281 f
= PyFrozenSet_New(dup
);
2282 assert(PySet_Size(f
) == 3);
2283 assert(PyFrozenSet_CheckExact(f
));
2284 assertRaises(PySet_Add(f
, elem
) == -1, PyExc_SystemError
);
2285 assertRaises(PySet_Discard(f
, elem
) == -1, PyExc_SystemError
);
2286 assertRaises(PySet_Pop(f
) == NULL
, PyExc_SystemError
);
2289 /* Raise KeyError when popping from an empty set */
2290 assert(PyNumber_InPlaceSubtract(ob
, ob
) == ob
);
2292 assert(PySet_GET_SIZE(ob
) == 0);
2293 assertRaises(PySet_Pop(ob
) == NULL
, PyExc_KeyError
);
2295 /* Restore the set from the copy using the PyNumber API */
2296 assert(PyNumber_InPlaceOr(ob
, dup
) == ob
);
2299 /* Verify constructors accept NULL arguments */
2300 f
= PySet_New(NULL
);
2302 assert(PySet_GET_SIZE(f
) == 0);
2304 f
= PyFrozenSet_New(NULL
);
2306 assert(PyFrozenSet_CheckExact(f
));
2307 assert(PySet_GET_SIZE(f
) == 0);