2 /* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
6 Copyright (c) 2003-2008 Python Software Foundation.
11 #include "structmember.h"
12 #include "stringlib/eq.h"
14 /* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
18 set_key_error(PyObject
*arg
)
21 tup
= PyTuple_Pack(1, arg
);
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError
, tup
);
28 /* This must be >= 1. */
29 #define PERTURB_SHIFT 5
31 /* Object used as dummy key to fill deleted entries */
32 static PyObject
*dummy
= NULL
; /* Initialized by first call to make_new_set() */
42 #define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
48 #define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
51 INIT_NONZERO_SET_SLOTS(so); \
54 /* Reuse scheme to save calls to malloc, free, and memset */
55 #ifndef PySet_MAXFREELIST
56 #define PySet_MAXFREELIST 80
58 static PySetObject
*free_list
[PySet_MAXFREELIST
];
59 static int numfree
= 0;
63 The basic lookup function used by all operations.
64 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65 Open addressing is preferred over chaining since the link overhead for
66 chaining would be substantial (100% with typical malloc overhead).
68 The initial probe index is computed as hash mod the table size. Subsequent
69 probe indices are computed as explained in Objects/dictobject.c.
71 All arithmetic on hash should ignore overflow.
73 Unlike the dictionary implementation, the lookkey functions can return
74 NULL if the rich comparison returns an error.
78 set_lookkey(PySetObject
*so
, PyObject
*key
, register long hash
)
80 register Py_ssize_t i
;
81 register size_t perturb
;
82 register setentry
*freeslot
;
83 register size_t mask
= so
->mask
;
84 setentry
*table
= so
->table
;
85 register setentry
*entry
;
91 if (entry
->key
== NULL
|| entry
->key
== key
)
94 if (entry
->key
== dummy
)
97 if (entry
->hash
== hash
) {
98 startkey
= entry
->key
;
100 cmp
= PyObject_RichCompareBool(startkey
, key
, Py_EQ
);
104 if (table
== so
->table
&& entry
->key
== startkey
) {
109 /* The compare did major nasty stuff to the
112 return set_lookkey(so
, key
, hash
);
118 /* In the loop, key == dummy is by far (factor of 100s) the
119 least likely outcome, so test for that last. */
120 for (perturb
= hash
; ; perturb
>>= PERTURB_SHIFT
) {
121 i
= (i
<< 2) + i
+ perturb
+ 1;
122 entry
= &table
[i
& mask
];
123 if (entry
->key
== NULL
) {
124 if (freeslot
!= NULL
)
128 if (entry
->key
== key
)
130 if (entry
->hash
== hash
&& entry
->key
!= dummy
) {
131 startkey
= entry
->key
;
133 cmp
= PyObject_RichCompareBool(startkey
, key
, Py_EQ
);
137 if (table
== so
->table
&& entry
->key
== startkey
) {
142 /* The compare did major nasty stuff to the
145 return set_lookkey(so
, key
, hash
);
148 else if (entry
->key
== dummy
&& freeslot
== NULL
)
155 * Hacked up version of set_lookkey which can assume keys are always unicode;
156 * This means we can always use unicode_eq directly and not have to check to
157 * see if the comparison altered the table.
160 set_lookkey_unicode(PySetObject
*so
, PyObject
*key
, register long hash
)
162 register Py_ssize_t i
;
163 register size_t perturb
;
164 register setentry
*freeslot
;
165 register size_t mask
= so
->mask
;
166 setentry
*table
= so
->table
;
167 register setentry
*entry
;
169 /* Make sure this function doesn't have to handle non-unicode keys,
170 including subclasses of str; e.g., one reason to subclass
171 strings is to override __eq__, and for speed we don't cater to
173 if (!PyUnicode_CheckExact(key
)) {
174 so
->lookup
= set_lookkey
;
175 return set_lookkey(so
, key
, hash
);
179 if (entry
->key
== NULL
|| entry
->key
== key
)
181 if (entry
->key
== dummy
)
184 if (entry
->hash
== hash
&& unicode_eq(entry
->key
, key
))
189 /* In the loop, key == dummy is by far (factor of 100s) the
190 least likely outcome, so test for that last. */
191 for (perturb
= hash
; ; perturb
>>= PERTURB_SHIFT
) {
192 i
= (i
<< 2) + i
+ perturb
+ 1;
193 entry
= &table
[i
& mask
];
194 if (entry
->key
== NULL
)
195 return freeslot
== NULL
? entry
: freeslot
;
196 if (entry
->key
== key
197 || (entry
->hash
== hash
198 && entry
->key
!= dummy
199 && unicode_eq(entry
->key
, key
)))
201 if (entry
->key
== dummy
&& freeslot
== NULL
)
204 assert(0); /* NOT REACHED */
209 Internal routine to insert a new key into the table.
210 Used by the public insert routine.
211 Eats a reference to key.
214 set_insert_key(register PySetObject
*so
, PyObject
*key
, long hash
)
216 register setentry
*entry
;
217 typedef setentry
*(*lookupfunc
)(PySetObject
*, PyObject
*, long);
219 assert(so
->lookup
!= NULL
);
220 entry
= so
->lookup(so
, key
, hash
);
223 if (entry
->key
== NULL
) {
229 } else if (entry
->key
== dummy
) {
243 Internal routine used by set_table_resize() to insert an item which is
244 known to be absent from the set. This routine also assumes that
245 the set contains no deleted entries. Besides the performance benefit,
246 using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
247 Note that no refcounts are changed by this routine; if needed, the caller
248 is responsible for incref'ing `key`.
251 set_insert_clean(register PySetObject
*so
, PyObject
*key
, long hash
)
254 register size_t perturb
;
255 register size_t mask
= (size_t)so
->mask
;
256 setentry
*table
= so
->table
;
257 register setentry
*entry
;
261 for (perturb
= hash
; entry
->key
!= NULL
; perturb
>>= PERTURB_SHIFT
) {
262 i
= (i
<< 2) + i
+ perturb
+ 1;
263 entry
= &table
[i
& mask
];
272 Restructure the table by allocating a new table and reinserting all
273 keys again. When entries have been deleted, the new table may
274 actually be smaller than the old one.
277 set_table_resize(PySetObject
*so
, Py_ssize_t minused
)
280 setentry
*oldtable
, *newtable
, *entry
;
282 int is_oldtable_malloced
;
283 setentry small_copy
[PySet_MINSIZE
];
285 assert(minused
>= 0);
287 /* Find the smallest table size > minused. */
288 for (newsize
= PySet_MINSIZE
;
289 newsize
<= minused
&& newsize
> 0;
297 /* Get space for a new table. */
298 oldtable
= so
->table
;
299 assert(oldtable
!= NULL
);
300 is_oldtable_malloced
= oldtable
!= so
->smalltable
;
302 if (newsize
== PySet_MINSIZE
) {
303 /* A large table is shrinking, or we can't get any smaller. */
304 newtable
= so
->smalltable
;
305 if (newtable
== oldtable
) {
306 if (so
->fill
== so
->used
) {
307 /* No dummies, so no point doing anything. */
310 /* We're not going to resize it, but rebuild the
311 table anyway to purge old dummy entries.
312 Subtle: This is *necessary* if fill==size,
313 as set_lookkey needs at least one virgin slot to
314 terminate failing searches. If fill < size, it's
315 merely desirable, as dummies slow searches. */
316 assert(so
->fill
> so
->used
);
317 memcpy(small_copy
, oldtable
, sizeof(small_copy
));
318 oldtable
= small_copy
;
322 newtable
= PyMem_NEW(setentry
, newsize
);
323 if (newtable
== NULL
) {
329 /* Make the set empty, using the new table. */
330 assert(newtable
!= oldtable
);
331 so
->table
= newtable
;
332 so
->mask
= newsize
- 1;
333 memset(newtable
, 0, sizeof(setentry
) * newsize
);
338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
340 for (entry
= oldtable
; i
> 0; entry
++) {
341 if (entry
->key
== NULL
) {
344 } else if (entry
->key
== dummy
) {
347 assert(entry
->key
== dummy
);
348 Py_DECREF(entry
->key
);
352 set_insert_clean(so
, entry
->key
, entry
->hash
);
356 if (is_oldtable_malloced
)
361 /* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
364 set_add_entry(register PySetObject
*so
, setentry
*entry
)
366 register Py_ssize_t n_used
;
368 assert(so
->fill
<= so
->mask
); /* at least one empty slot */
370 Py_INCREF(entry
->key
);
371 if (set_insert_key(so
, entry
->key
, entry
->hash
) == -1) {
372 Py_DECREF(entry
->key
);
375 if (!(so
->used
> n_used
&& so
->fill
*3 >= (so
->mask
+1)*2))
377 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
381 set_add_key(register PySetObject
*so
, PyObject
*key
)
384 register Py_ssize_t n_used
;
386 if (!PyUnicode_CheckExact(key
) ||
387 (hash
= ((PyUnicodeObject
*) key
)->hash
) == -1) {
388 hash
= PyObject_Hash(key
);
392 assert(so
->fill
<= so
->mask
); /* at least one empty slot */
395 if (set_insert_key(so
, key
, hash
) == -1) {
399 if (!(so
->used
> n_used
&& so
->fill
*3 >= (so
->mask
+1)*2))
401 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
404 #define DISCARD_NOTFOUND 0
405 #define DISCARD_FOUND 1
408 set_discard_entry(PySetObject
*so
, setentry
*oldentry
)
409 { register setentry
*entry
;
412 entry
= (so
->lookup
)(so
, oldentry
->key
, oldentry
->hash
);
415 if (entry
->key
== NULL
|| entry
->key
== dummy
)
416 return DISCARD_NOTFOUND
;
417 old_key
= entry
->key
;
422 return DISCARD_FOUND
;
426 set_discard_key(PySetObject
*so
, PyObject
*key
)
429 register setentry
*entry
;
432 assert (PyAnySet_Check(so
));
434 if (!PyUnicode_CheckExact(key
) ||
435 (hash
= ((PyUnicodeObject
*) key
)->hash
) == -1) {
436 hash
= PyObject_Hash(key
);
440 entry
= (so
->lookup
)(so
, key
, hash
);
443 if (entry
->key
== NULL
|| entry
->key
== dummy
)
444 return DISCARD_NOTFOUND
;
445 old_key
= entry
->key
;
450 return DISCARD_FOUND
;
454 set_clear_internal(PySetObject
*so
)
456 setentry
*entry
, *table
;
457 int table_is_malloced
;
459 setentry small_copy
[PySet_MINSIZE
];
462 assert (PyAnySet_Check(so
));
469 assert(table
!= NULL
);
470 table_is_malloced
= table
!= so
->smalltable
;
472 /* This is delicate. During the process of clearing the set,
473 * decrefs can cause the set to mutate. To avoid fatal confusion
474 * (voice of experience), we have to make the set empty before
475 * clearing the slots, and never refer to anything via so->ref while
479 if (table_is_malloced
)
480 EMPTY_TO_MINSIZE(so
);
483 /* It's a small table with something that needs to be cleared.
484 * Afraid the only safe way is to copy the set entries into
485 * another small table first.
487 memcpy(small_copy
, table
, sizeof(small_copy
));
489 EMPTY_TO_MINSIZE(so
);
491 /* else it's a small table that's already empty */
493 /* Now we can finally clear things. If C had refcounts, we could
494 * assert that the refcount on table is 1 now, i.e. that this function
495 * has unique access to it, so decref side-effects can't alter it.
497 for (entry
= table
; fill
> 0; ++entry
) {
504 Py_DECREF(entry
->key
);
508 assert(entry
->key
== NULL
);
512 if (table_is_malloced
)
518 * Iterate over a set table. Use like so:
522 * pos = 0; # important! pos should not otherwise be changed by you
523 * while (set_next(yourset, &pos, &entry)) {
524 * Refer to borrowed reference in entry->key.
527 * CAUTION: In general, it isn't safe to use set_next in a loop that
531 set_next(PySetObject
*so
, Py_ssize_t
*pos_ptr
, setentry
**entry_ptr
)
535 register setentry
*table
;
537 assert (PyAnySet_Check(so
));
542 while (i
<= mask
&& (table
[i
].key
== NULL
|| table
[i
].key
== dummy
))
547 assert(table
[i
].key
!= NULL
);
548 *entry_ptr
= &table
[i
];
553 set_dealloc(PySetObject
*so
)
555 register setentry
*entry
;
556 Py_ssize_t fill
= so
->fill
;
557 PyObject_GC_UnTrack(so
);
558 Py_TRASHCAN_SAFE_BEGIN(so
)
559 if (so
->weakreflist
!= NULL
)
560 PyObject_ClearWeakRefs((PyObject
*) so
);
562 for (entry
= so
->table
; fill
> 0; entry
++) {
565 Py_DECREF(entry
->key
);
568 if (so
->table
!= so
->smalltable
)
569 PyMem_DEL(so
->table
);
570 if (numfree
< PySet_MAXFREELIST
&& PyAnySet_CheckExact(so
))
571 free_list
[numfree
++] = so
;
573 Py_TYPE(so
)->tp_free(so
);
574 Py_TRASHCAN_SAFE_END(so
)
578 set_repr(PySetObject
*so
)
580 PyObject
*keys
, *result
=NULL
;
582 int status
= Py_ReprEnter((PyObject
*)so
);
589 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so
)->tp_name
);
592 /* shortcut for the empty set */
594 Py_ReprLeave((PyObject
*)so
);
595 return PyUnicode_FromFormat("%s()", Py_TYPE(so
)->tp_name
);
598 keys
= PySequence_List((PyObject
*)so
);
602 listrepr
= PyObject_Repr(keys
);
604 if (listrepr
== NULL
) {
608 newsize
= PyUnicode_GET_SIZE(listrepr
);
609 result
= PyUnicode_FromUnicode(NULL
, newsize
);
611 u
= PyUnicode_AS_UNICODE(result
);
613 /* Omit the brackets from the listrepr */
614 Py_UNICODE_COPY(u
, PyUnicode_AS_UNICODE(listrepr
)+1,
615 PyUnicode_GET_SIZE(listrepr
)-2);
620 if (Py_TYPE(so
) != &PySet_Type
) {
621 PyObject
*tmp
= PyUnicode_FromFormat("%s(%U)",
622 Py_TYPE(so
)->tp_name
,
628 Py_ReprLeave((PyObject
*)so
);
633 set_len(PyObject
*so
)
635 return ((PySetObject
*)so
)->used
;
639 set_merge(PySetObject
*so
, PyObject
*otherset
)
642 register Py_ssize_t i
;
643 register setentry
*entry
;
645 assert (PyAnySet_Check(so
));
646 assert (PyAnySet_Check(otherset
));
648 other
= (PySetObject
*)otherset
;
649 if (other
== so
|| other
->used
== 0)
650 /* a.update(a) or a.update({}); nothing to do */
652 /* Do one big resize at the start, rather than
653 * incrementally resizing as we insert new keys. Expect
654 * that there will be no (or few) overlapping keys.
656 if ((so
->fill
+ other
->used
)*3 >= (so
->mask
+1)*2) {
657 if (set_table_resize(so
, (so
->used
+ other
->used
)*2) != 0)
660 for (i
= 0; i
<= other
->mask
; i
++) {
661 entry
= &other
->table
[i
];
662 if (entry
->key
!= NULL
&&
663 entry
->key
!= dummy
) {
664 Py_INCREF(entry
->key
);
665 if (set_insert_key(so
, entry
->key
, entry
->hash
) == -1) {
666 Py_DECREF(entry
->key
);
675 set_contains_key(PySetObject
*so
, PyObject
*key
)
680 if (!PyUnicode_CheckExact(key
) ||
681 (hash
= ((PyUnicodeObject
*) key
)->hash
) == -1) {
682 hash
= PyObject_Hash(key
);
686 entry
= (so
->lookup
)(so
, key
, hash
);
690 return key
!= NULL
&& key
!= dummy
;
694 set_contains_entry(PySetObject
*so
, setentry
*entry
)
699 lu_entry
= (so
->lookup
)(so
, entry
->key
, entry
->hash
);
700 if (lu_entry
== NULL
)
703 return key
!= NULL
&& key
!= dummy
;
707 set_pop(PySetObject
*so
)
709 register Py_ssize_t i
= 0;
710 register setentry
*entry
;
713 assert (PyAnySet_Check(so
));
715 PyErr_SetString(PyExc_KeyError
, "pop from an empty set");
719 /* Set entry to "the first" unused or dummy set entry. We abuse
720 * the hash field of slot 0 to hold a search finger:
721 * If slot 0 has a value, use slot 0.
722 * Else slot 0 is being used to hold a search finger,
723 * and we use its hash value as the first index to look.
725 entry
= &so
->table
[0];
726 if (entry
->key
== NULL
|| entry
->key
== dummy
) {
728 /* The hash field may be a real hash value, or it may be a
729 * legit search finger, or it may be a once-legit search
730 * finger that's out of bounds now because it wrapped around
731 * or the table shrunk -- simply make sure it's in bounds now.
733 if (i
> so
->mask
|| i
< 1)
734 i
= 1; /* skip slot 0 */
735 while ((entry
= &so
->table
[i
])->key
== NULL
|| entry
->key
==dummy
) {
745 so
->table
[0].hash
= i
+ 1; /* next place to start */
749 PyDoc_STRVAR(pop_doc
, "Remove and return an arbitrary set element.\n\
750 Raises KeyError if the set is empty.");
753 set_traverse(PySetObject
*so
, visitproc visit
, void *arg
)
758 while (set_next(so
, &pos
, &entry
))
759 Py_VISIT(entry
->key
);
764 frozenset_hash(PyObject
*self
)
766 PySetObject
*so
= (PySetObject
*)self
;
767 long h
, hash
= 1927868237L;
774 hash
*= PySet_GET_SIZE(self
) + 1;
775 while (set_next(so
, &pos
, &entry
)) {
776 /* Work to increase the bit dispersion for closely spaced hash
777 values. The is important because some use cases have many
778 combinations of a small number of elements with nearby
779 hashes so that many distinct combinations collapse to only
780 a handful of distinct hash values. */
782 hash
^= (h
^ (h
<< 16) ^ 89869747L) * 3644798167u;
784 hash
= hash
* 69069L + 907133923L;
791 /***** Set iterator type ***********************************************/
795 PySetObject
*si_set
; /* Set to NULL when iterator is exhausted */
802 setiter_dealloc(setiterobject
*si
)
804 Py_XDECREF(si
->si_set
);
809 setiter_traverse(setiterobject
*si
, visitproc visit
, void *arg
)
811 Py_VISIT(si
->si_set
);
816 setiter_len(setiterobject
*si
)
819 if (si
->si_set
!= NULL
&& si
->si_used
== si
->si_set
->used
)
821 return PyLong_FromLong(len
);
824 PyDoc_STRVAR(length_hint_doc
, "Private method returning an estimate of len(list(it)).");
826 static PyMethodDef setiter_methods
[] = {
827 {"__length_hint__", (PyCFunction
)setiter_len
, METH_NOARGS
, length_hint_doc
},
828 {NULL
, NULL
} /* sentinel */
831 static PyObject
*setiter_iternext(setiterobject
*si
)
834 register Py_ssize_t i
, mask
;
835 register setentry
*entry
;
836 PySetObject
*so
= si
->si_set
;
840 assert (PyAnySet_Check(so
));
842 if (si
->si_used
!= so
->used
) {
843 PyErr_SetString(PyExc_RuntimeError
,
844 "Set changed size during iteration");
845 si
->si_used
= -1; /* Make this state sticky */
853 while (i
<= mask
&& (entry
[i
].key
== NULL
|| entry
[i
].key
== dummy
))
869 PyTypeObject PySetIter_Type
= {
870 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
871 "set_iterator", /* tp_name */
872 sizeof(setiterobject
), /* tp_basicsize */
875 (destructor
)setiter_dealloc
, /* tp_dealloc */
881 0, /* tp_as_number */
882 0, /* tp_as_sequence */
883 0, /* tp_as_mapping */
887 PyObject_GenericGetAttr
, /* tp_getattro */
889 0, /* tp_as_buffer */
890 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
892 (traverseproc
)setiter_traverse
, /* tp_traverse */
894 0, /* tp_richcompare */
895 0, /* tp_weaklistoffset */
896 PyObject_SelfIter
, /* tp_iter */
897 (iternextfunc
)setiter_iternext
, /* tp_iternext */
898 setiter_methods
, /* tp_methods */
903 set_iter(PySetObject
*so
)
905 setiterobject
*si
= PyObject_GC_New(setiterobject
, &PySetIter_Type
);
910 si
->si_used
= so
->used
;
913 _PyObject_GC_TRACK(si
);
914 return (PyObject
*)si
;
918 set_update_internal(PySetObject
*so
, PyObject
*other
)
922 if (PyAnySet_Check(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
*args
)
975 for (i
=0 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
976 PyObject
*other
= PyTuple_GET_ITEM(args
, i
);
977 if (set_update_internal(so
, other
) == -1)
983 PyDoc_STRVAR(update_doc
,
984 "Update a set with the union of itself and others.");
987 make_new_set(PyTypeObject
*type
, PyObject
*iterable
)
989 register PySetObject
*so
= NULL
;
991 if (dummy
== NULL
) { /* Auto-initialize dummy */
992 dummy
= PyUnicode_FromString("<dummy key>");
997 /* create PySetObject structure */
999 (type
== &PySet_Type
|| type
== &PyFrozenSet_Type
)) {
1000 so
= free_list
[--numfree
];
1001 assert (so
!= NULL
&& PyAnySet_CheckExact(so
));
1003 _Py_NewReference((PyObject
*)so
);
1004 EMPTY_TO_MINSIZE(so
);
1005 PyObject_GC_Track(so
);
1007 so
= (PySetObject
*)type
->tp_alloc(type
, 0);
1010 /* tp_alloc has already zeroed the structure */
1011 assert(so
->table
== NULL
&& so
->fill
== 0 && so
->used
== 0);
1012 INIT_NONZERO_SET_SLOTS(so
);
1015 so
->lookup
= set_lookkey_unicode
;
1016 so
->weakreflist
= NULL
;
1018 if (iterable
!= NULL
) {
1019 if (set_update_internal(so
, iterable
) == -1) {
1025 return (PyObject
*)so
;
1029 make_new_set_basetype(PyTypeObject
*type
, PyObject
*iterable
)
1031 if (type
!= &PySet_Type
&& type
!= &PyFrozenSet_Type
) {
1032 if (PyType_IsSubtype(type
, &PySet_Type
))
1035 type
= &PyFrozenSet_Type
;
1037 return make_new_set(type
, iterable
);
1040 /* The empty frozenset is a singleton */
1041 static PyObject
*emptyfrozenset
= NULL
;
1044 frozenset_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1046 PyObject
*iterable
= NULL
, *result
;
1048 if (type
== &PyFrozenSet_Type
&& !_PyArg_NoKeywords("frozenset()", kwds
))
1051 if (!PyArg_UnpackTuple(args
, type
->tp_name
, 0, 1, &iterable
))
1054 if (type
!= &PyFrozenSet_Type
)
1055 return make_new_set(type
, iterable
);
1057 if (iterable
!= NULL
) {
1058 /* frozenset(f) is idempotent */
1059 if (PyFrozenSet_CheckExact(iterable
)) {
1060 Py_INCREF(iterable
);
1063 result
= make_new_set(type
, iterable
);
1064 if (result
== NULL
|| PySet_GET_SIZE(result
))
1068 /* The empty frozenset is a singleton */
1069 if (emptyfrozenset
== NULL
)
1070 emptyfrozenset
= make_new_set(type
, NULL
);
1071 Py_XINCREF(emptyfrozenset
);
1072 return emptyfrozenset
;
1082 so
= free_list
[numfree
];
1083 PyObject_GC_Del(so
);
1086 Py_CLEAR(emptyfrozenset
);
1090 set_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1092 if (type
== &PySet_Type
&& !_PyArg_NoKeywords("set()", kwds
))
1095 return make_new_set(type
, NULL
);
1098 /* set_swap_bodies() switches the contents of any two sets by moving their
1099 internal data pointers and, if needed, copying the internal smalltables.
1100 Semantically equivalent to:
1102 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1104 The function always succeeds and it leaves both objects in a stable state.
1105 Useful for creating temporary frozensets from sets for membership testing
1106 in __contains__(), discard(), and remove(). Also useful for operations
1107 that update in-place (by allowing an intermediate result to be swapped
1108 into one of the original inputs).
1112 set_swap_bodies(PySetObject
*a
, PySetObject
*b
)
1116 setentry
*(*f
)(PySetObject
*so
, PyObject
*key
, long hash
);
1117 setentry tab
[PySet_MINSIZE
];
1120 t
= a
->fill
; a
->fill
= b
->fill
; b
->fill
= t
;
1121 t
= a
->used
; a
->used
= b
->used
; b
->used
= t
;
1122 t
= a
->mask
; a
->mask
= b
->mask
; b
->mask
= t
;
1125 if (a
->table
== a
->smalltable
)
1127 a
->table
= b
->table
;
1128 if (b
->table
== b
->smalltable
)
1129 a
->table
= a
->smalltable
;
1132 f
= a
->lookup
; a
->lookup
= b
->lookup
; b
->lookup
= f
;
1134 if (a
->table
== a
->smalltable
|| b
->table
== b
->smalltable
) {
1135 memcpy(tab
, a
->smalltable
, sizeof(tab
));
1136 memcpy(a
->smalltable
, b
->smalltable
, sizeof(tab
));
1137 memcpy(b
->smalltable
, tab
, sizeof(tab
));
1140 if (PyType_IsSubtype(Py_TYPE(a
), &PyFrozenSet_Type
) &&
1141 PyType_IsSubtype(Py_TYPE(b
), &PyFrozenSet_Type
)) {
1142 h
= a
->hash
; a
->hash
= b
->hash
; b
->hash
= h
;
1150 set_copy(PySetObject
*so
)
1152 return make_new_set_basetype(Py_TYPE(so
), (PyObject
*)so
);
1156 frozenset_copy(PySetObject
*so
)
1158 if (PyFrozenSet_CheckExact(so
)) {
1160 return (PyObject
*)so
;
1162 return set_copy(so
);
1165 PyDoc_STRVAR(copy_doc
, "Return a shallow copy of a set.");
1168 set_clear(PySetObject
*so
)
1170 set_clear_internal(so
);
1174 PyDoc_STRVAR(clear_doc
, "Remove all elements from this set.");
1177 set_union(PySetObject
*so
, PyObject
*args
)
1179 PySetObject
*result
;
1183 result
= (PySetObject
*)set_copy(so
);
1187 for (i
=0 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
1188 other
= PyTuple_GET_ITEM(args
, i
);
1189 if ((PyObject
*)so
== other
)
1191 if (set_update_internal(result
, other
) == -1) {
1196 return (PyObject
*)result
;
1199 PyDoc_STRVAR(union_doc
,
1200 "Return the union of sets as a new set.\n\
1202 (i.e. all elements that are in either set.)");
1205 set_or(PySetObject
*so
, PyObject
*other
)
1207 PySetObject
*result
;
1209 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1210 Py_INCREF(Py_NotImplemented
);
1211 return Py_NotImplemented
;
1214 result
= (PySetObject
*)set_copy(so
);
1217 if ((PyObject
*)so
== other
)
1218 return (PyObject
*)result
;
1219 if (set_update_internal(result
, other
) == -1) {
1223 return (PyObject
*)result
;
1227 set_ior(PySetObject
*so
, PyObject
*other
)
1229 if (!PyAnySet_Check(other
)) {
1230 Py_INCREF(Py_NotImplemented
);
1231 return Py_NotImplemented
;
1233 if (set_update_internal(so
, other
) == -1)
1236 return (PyObject
*)so
;
1240 set_intersection(PySetObject
*so
, PyObject
*other
)
1242 PySetObject
*result
;
1243 PyObject
*key
, *it
, *tmp
;
1245 if ((PyObject
*)so
== other
)
1246 return set_copy(so
);
1248 result
= (PySetObject
*)make_new_set_basetype(Py_TYPE(so
), NULL
);
1252 if (PyAnySet_Check(other
)) {
1256 if (PySet_GET_SIZE(other
) > PySet_GET_SIZE(so
)) {
1257 tmp
= (PyObject
*)so
;
1258 so
= (PySetObject
*)other
;
1262 while (set_next((PySetObject
*)other
, &pos
, &entry
)) {
1263 int rv
= set_contains_entry(so
, entry
);
1269 if (set_add_entry(result
, entry
) == -1) {
1275 return (PyObject
*)result
;
1278 it
= PyObject_GetIter(other
);
1284 while ((key
= PyIter_Next(it
)) != NULL
) {
1287 long hash
= PyObject_Hash(key
);
1297 rv
= set_contains_entry(so
, &entry
);
1305 if (set_add_entry(result
, &entry
) == -1) {
1315 if (PyErr_Occurred()) {
1319 return (PyObject
*)result
;
1323 set_intersection_multi(PySetObject
*so
, PyObject
*args
)
1326 PyObject
*result
= (PyObject
*)so
;
1328 if (PyTuple_GET_SIZE(args
) == 0)
1329 return set_copy(so
);
1332 for (i
=0 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
1333 PyObject
*other
= PyTuple_GET_ITEM(args
, i
);
1334 PyObject
*newresult
= set_intersection((PySetObject
*)result
, other
);
1335 if (newresult
== NULL
) {
1345 PyDoc_STRVAR(intersection_doc
,
1346 "Return the intersection of two or more sets as a new set.\n\
1348 (i.e. elements that are common to all of the sets.)");
1351 set_intersection_update(PySetObject
*so
, PyObject
*other
)
1355 tmp
= set_intersection(so
, other
);
1358 set_swap_bodies(so
, (PySetObject
*)tmp
);
1364 set_intersection_update_multi(PySetObject
*so
, PyObject
*args
)
1368 tmp
= set_intersection_multi(so
, args
);
1371 set_swap_bodies(so
, (PySetObject
*)tmp
);
1376 PyDoc_STRVAR(intersection_update_doc
,
1377 "Update a set with the intersection of itself and another.");
1380 set_and(PySetObject
*so
, PyObject
*other
)
1382 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1383 Py_INCREF(Py_NotImplemented
);
1384 return Py_NotImplemented
;
1386 return set_intersection(so
, other
);
1390 set_iand(PySetObject
*so
, PyObject
*other
)
1394 if (!PyAnySet_Check(other
)) {
1395 Py_INCREF(Py_NotImplemented
);
1396 return Py_NotImplemented
;
1398 result
= set_intersection_update(so
, other
);
1403 return (PyObject
*)so
;
1407 set_isdisjoint(PySetObject
*so
, PyObject
*other
)
1409 PyObject
*key
, *it
, *tmp
;
1411 if ((PyObject
*)so
== other
) {
1412 if (PySet_GET_SIZE(so
) == 0)
1418 if (PyAnySet_CheckExact(other
)) {
1422 if (PySet_GET_SIZE(other
) > PySet_GET_SIZE(so
)) {
1423 tmp
= (PyObject
*)so
;
1424 so
= (PySetObject
*)other
;
1427 while (set_next((PySetObject
*)other
, &pos
, &entry
)) {
1428 int rv
= set_contains_entry(so
, entry
);
1437 it
= PyObject_GetIter(other
);
1441 while ((key
= PyIter_Next(it
)) != NULL
) {
1444 long hash
= PyObject_Hash(key
);;
1453 rv
= set_contains_entry(so
, &entry
);
1465 if (PyErr_Occurred())
1470 PyDoc_STRVAR(isdisjoint_doc
,
1471 "Return True if two sets have a null intersection.");
1474 set_difference_update_internal(PySetObject
*so
, PyObject
*other
)
1476 if ((PyObject
*)so
== other
)
1477 return set_clear_internal(so
);
1479 if (PyAnySet_Check(other
)) {
1483 while (set_next((PySetObject
*)other
, &pos
, &entry
))
1484 if (set_discard_entry(so
, entry
) == -1)
1488 it
= PyObject_GetIter(other
);
1492 while ((key
= PyIter_Next(it
)) != NULL
) {
1493 if (set_discard_key(so
, key
) == -1) {
1501 if (PyErr_Occurred())
1504 /* If more than 1/5 are dummies, then resize them away. */
1505 if ((so
->fill
- so
->used
) * 5 < so
->mask
)
1507 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
1511 set_difference_update(PySetObject
*so
, PyObject
*args
)
1515 for (i
=0 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
1516 PyObject
*other
= PyTuple_GET_ITEM(args
, i
);
1517 if (set_difference_update_internal(so
, other
) == -1)
1523 PyDoc_STRVAR(difference_update_doc
,
1524 "Remove all elements of another set from this set.");
1527 set_difference(PySetObject
*so
, PyObject
*other
)
1533 if (!PyAnySet_Check(other
) && !PyDict_CheckExact(other
)) {
1534 result
= set_copy(so
);
1537 if (set_difference_update_internal((PySetObject
*)result
, other
) != -1)
1543 result
= make_new_set_basetype(Py_TYPE(so
), NULL
);
1547 if (PyDict_CheckExact(other
)) {
1548 while (set_next(so
, &pos
, &entry
)) {
1550 entrycopy
.hash
= entry
->hash
;
1551 entrycopy
.key
= entry
->key
;
1552 if (!_PyDict_Contains(other
, entry
->key
, entry
->hash
)) {
1553 if (set_add_entry((PySetObject
*)result
, &entrycopy
) == -1) {
1562 while (set_next(so
, &pos
, &entry
)) {
1563 int rv
= set_contains_entry((PySetObject
*)other
, entry
);
1569 if (set_add_entry((PySetObject
*)result
, entry
) == -1) {
1579 set_difference_multi(PySetObject
*so
, PyObject
*args
)
1582 PyObject
*result
, *other
;
1584 if (PyTuple_GET_SIZE(args
) == 0)
1585 return set_copy(so
);
1587 other
= PyTuple_GET_ITEM(args
, 0);
1588 result
= set_difference(so
, other
);
1592 for (i
=1 ; i
<PyTuple_GET_SIZE(args
) ; i
++) {
1593 other
= PyTuple_GET_ITEM(args
, i
);
1594 if (set_difference_update_internal((PySetObject
*)result
, other
) == -1) {
1602 PyDoc_STRVAR(difference_doc
,
1603 "Return the difference of two or more sets as a new set.\n\
1605 (i.e. all elements that are in this set but not the others.)");
1607 set_sub(PySetObject
*so
, PyObject
*other
)
1609 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1610 Py_INCREF(Py_NotImplemented
);
1611 return Py_NotImplemented
;
1613 return set_difference(so
, other
);
1617 set_isub(PySetObject
*so
, PyObject
*other
)
1619 if (!PyAnySet_Check(other
)) {
1620 Py_INCREF(Py_NotImplemented
);
1621 return Py_NotImplemented
;
1623 if (set_difference_update_internal(so
, other
) == -1)
1626 return (PyObject
*)so
;
1630 set_symmetric_difference_update(PySetObject
*so
, PyObject
*other
)
1632 PySetObject
*otherset
;
1637 if ((PyObject
*)so
== other
)
1638 return set_clear(so
);
1640 if (PyDict_CheckExact(other
)) {
1644 while (_PyDict_Next(other
, &pos
, &key
, &value
, &hash
)) {
1647 an_entry
.hash
= hash
;
1649 rv
= set_discard_entry(so
, &an_entry
);
1652 if (rv
== DISCARD_NOTFOUND
) {
1653 if (set_add_entry(so
, &an_entry
) == -1)
1660 if (PyAnySet_Check(other
)) {
1662 otherset
= (PySetObject
*)other
;
1664 otherset
= (PySetObject
*)make_new_set_basetype(Py_TYPE(so
), other
);
1665 if (otherset
== NULL
)
1669 while (set_next(otherset
, &pos
, &entry
)) {
1670 int rv
= set_discard_entry(so
, entry
);
1672 Py_DECREF(otherset
);
1675 if (rv
== DISCARD_NOTFOUND
) {
1676 if (set_add_entry(so
, entry
) == -1) {
1677 Py_DECREF(otherset
);
1682 Py_DECREF(otherset
);
1686 PyDoc_STRVAR(symmetric_difference_update_doc
,
1687 "Update a set with the symmetric difference of itself and another.");
1690 set_symmetric_difference(PySetObject
*so
, PyObject
*other
)
1693 PySetObject
*otherset
;
1695 otherset
= (PySetObject
*)make_new_set_basetype(Py_TYPE(so
), other
);
1696 if (otherset
== NULL
)
1698 rv
= set_symmetric_difference_update(otherset
, (PyObject
*)so
);
1702 return (PyObject
*)otherset
;
1705 PyDoc_STRVAR(symmetric_difference_doc
,
1706 "Return the symmetric difference of two sets as a new set.\n\
1708 (i.e. all elements that are in exactly one of the sets.)");
1711 set_xor(PySetObject
*so
, PyObject
*other
)
1713 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1714 Py_INCREF(Py_NotImplemented
);
1715 return Py_NotImplemented
;
1717 return set_symmetric_difference(so
, other
);
1721 set_ixor(PySetObject
*so
, PyObject
*other
)
1725 if (!PyAnySet_Check(other
)) {
1726 Py_INCREF(Py_NotImplemented
);
1727 return Py_NotImplemented
;
1729 result
= set_symmetric_difference_update(so
, other
);
1734 return (PyObject
*)so
;
1738 set_issubset(PySetObject
*so
, PyObject
*other
)
1743 if (!PyAnySet_Check(other
)) {
1744 PyObject
*tmp
, *result
;
1745 tmp
= make_new_set(&PySet_Type
, other
);
1748 result
= set_issubset(so
, tmp
);
1752 if (PySet_GET_SIZE(so
) > PySet_GET_SIZE(other
))
1755 while (set_next(so
, &pos
, &entry
)) {
1756 int rv
= set_contains_entry((PySetObject
*)other
, entry
);
1765 PyDoc_STRVAR(issubset_doc
, "Report whether another set contains this set.");
1768 set_issuperset(PySetObject
*so
, PyObject
*other
)
1770 PyObject
*tmp
, *result
;
1772 if (!PyAnySet_Check(other
)) {
1773 tmp
= make_new_set(&PySet_Type
, other
);
1776 result
= set_issuperset(so
, tmp
);
1780 return set_issubset((PySetObject
*)other
, (PyObject
*)so
);
1783 PyDoc_STRVAR(issuperset_doc
, "Report whether this set contains another set.");
1786 set_richcompare(PySetObject
*v
, PyObject
*w
, int op
)
1790 if(!PyAnySet_Check(w
)) {
1791 Py_INCREF(Py_NotImplemented
);
1792 return Py_NotImplemented
;
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_add(PySetObject
*so
, PyObject
*key
)
1830 if (set_add_key(so
, key
) == -1)
1835 PyDoc_STRVAR(add_doc
,
1836 "Add an element to a set.\n\
1838 This has no effect if the element is already present.");
1841 set_contains(PySetObject
*so
, PyObject
*key
)
1846 rv
= set_contains_key(so
, key
);
1848 if (!PySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1851 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1854 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1855 rv
= set_contains(so
, tmpkey
);
1856 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1863 set_direct_contains(PySetObject
*so
, PyObject
*key
)
1867 result
= set_contains(so
, key
);
1870 return PyBool_FromLong(result
);
1873 PyDoc_STRVAR(contains_doc
, "x.__contains__(y) <==> y in x.");
1876 set_remove(PySetObject
*so
, PyObject
*key
)
1881 rv
= set_discard_key(so
, key
);
1883 if (!PySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1886 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1889 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1890 rv
= set_discard_key(so
, tmpkey
);
1891 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1897 if (rv
== DISCARD_NOTFOUND
) {
1904 PyDoc_STRVAR(remove_doc
,
1905 "Remove an element from a set; it must be a member.\n\
1907 If the element is not a member, raise a KeyError.");
1910 set_discard(PySetObject
*so
, PyObject
*key
)
1912 PyObject
*tmpkey
, *result
;
1915 rv
= set_discard_key(so
, key
);
1917 if (!PySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1920 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1923 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1924 result
= set_discard(so
, tmpkey
);
1925 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1932 PyDoc_STRVAR(discard_doc
,
1933 "Remove an element from a set if it is a member.\n\
1935 If the element is not a member, do nothing.");
1938 set_reduce(PySetObject
*so
)
1940 PyObject
*keys
=NULL
, *args
=NULL
, *result
=NULL
, *dict
=NULL
;
1942 keys
= PySequence_List((PyObject
*)so
);
1945 args
= PyTuple_Pack(1, keys
);
1948 dict
= PyObject_GetAttrString((PyObject
*)so
, "__dict__");
1954 result
= PyTuple_Pack(3, Py_TYPE(so
), args
, dict
);
1962 PyDoc_STRVAR(reduce_doc
, "Return state information for pickling.");
1965 set_sizeof(PySetObject
*so
)
1969 res
= sizeof(PySetObject
);
1970 if (so
->table
!= so
->smalltable
)
1971 res
= res
+ (so
->mask
+ 1) * sizeof(setentry
);
1972 return PyLong_FromSsize_t(res
);
1975 PyDoc_STRVAR(sizeof_doc
, "S.__sizeof__() -> size of S in memory, in bytes");
1977 set_init(PySetObject
*self
, PyObject
*args
, PyObject
*kwds
)
1979 PyObject
*iterable
= NULL
;
1981 if (!PyAnySet_Check(self
))
1983 if (!PyArg_UnpackTuple(args
, Py_TYPE(self
)->tp_name
, 0, 1, &iterable
))
1985 set_clear_internal(self
);
1987 if (iterable
== NULL
)
1989 return set_update_internal(self
, iterable
);
1992 static PySequenceMethods set_as_sequence
= {
1993 set_len
, /* sq_length */
1998 0, /* sq_ass_item */
1999 0, /* sq_ass_slice */
2000 (objobjproc
)set_contains
, /* sq_contains */
2003 /* set object ********************************************************/
2006 static PyObject
*test_c_api(PySetObject
*so
);
2008 PyDoc_STRVAR(test_c_api_doc
, "Exercises C API. Returns True.\n\
2009 All is well if assertions don't fail.");
2012 static PyMethodDef set_methods
[] = {
2013 {"add", (PyCFunction
)set_add
, METH_O
,
2015 {"clear", (PyCFunction
)set_clear
, METH_NOARGS
,
2017 {"__contains__",(PyCFunction
)set_direct_contains
, METH_O
| METH_COEXIST
,
2019 {"copy", (PyCFunction
)set_copy
, METH_NOARGS
,
2021 {"discard", (PyCFunction
)set_discard
, METH_O
,
2023 {"difference", (PyCFunction
)set_difference_multi
, METH_VARARGS
,
2025 {"difference_update", (PyCFunction
)set_difference_update
, METH_VARARGS
,
2026 difference_update_doc
},
2027 {"intersection",(PyCFunction
)set_intersection_multi
, METH_VARARGS
,
2029 {"intersection_update",(PyCFunction
)set_intersection_update_multi
, METH_VARARGS
,
2030 intersection_update_doc
},
2031 {"isdisjoint", (PyCFunction
)set_isdisjoint
, METH_O
,
2033 {"issubset", (PyCFunction
)set_issubset
, METH_O
,
2035 {"issuperset", (PyCFunction
)set_issuperset
, METH_O
,
2037 {"pop", (PyCFunction
)set_pop
, METH_NOARGS
,
2039 {"__reduce__", (PyCFunction
)set_reduce
, METH_NOARGS
,
2041 {"remove", (PyCFunction
)set_remove
, METH_O
,
2043 {"__sizeof__", (PyCFunction
)set_sizeof
, METH_NOARGS
,
2045 {"symmetric_difference",(PyCFunction
)set_symmetric_difference
, METH_O
,
2046 symmetric_difference_doc
},
2047 {"symmetric_difference_update",(PyCFunction
)set_symmetric_difference_update
, METH_O
,
2048 symmetric_difference_update_doc
},
2050 {"test_c_api", (PyCFunction
)test_c_api
, METH_NOARGS
,
2053 {"union", (PyCFunction
)set_union
, METH_VARARGS
,
2055 {"update", (PyCFunction
)set_update
, METH_VARARGS
,
2057 {NULL
, NULL
} /* sentinel */
2060 static PyNumberMethods set_as_number
= {
2062 (binaryfunc
)set_sub
, /*nb_subtract*/
2074 (binaryfunc
)set_and
, /*nb_and*/
2075 (binaryfunc
)set_xor
, /*nb_xor*/
2076 (binaryfunc
)set_or
, /*nb_or*/
2080 0, /*nb_inplace_add*/
2081 (binaryfunc
)set_isub
, /*nb_inplace_subtract*/
2082 0, /*nb_inplace_multiply*/
2083 0, /*nb_inplace_remainder*/
2084 0, /*nb_inplace_power*/
2085 0, /*nb_inplace_lshift*/
2086 0, /*nb_inplace_rshift*/
2087 (binaryfunc
)set_iand
, /*nb_inplace_and*/
2088 (binaryfunc
)set_ixor
, /*nb_inplace_xor*/
2089 (binaryfunc
)set_ior
, /*nb_inplace_or*/
2092 PyDoc_STRVAR(set_doc
,
2093 "set() -> new empty set object\n\
2094 set(iterable) -> new set object\n\
2096 Build an unordered collection of unique elements.");
2098 PyTypeObject PySet_Type
= {
2099 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2100 "set", /* tp_name */
2101 sizeof(PySetObject
), /* tp_basicsize */
2102 0, /* tp_itemsize */
2104 (destructor
)set_dealloc
, /* tp_dealloc */
2108 0, /* tp_reserved */
2109 (reprfunc
)set_repr
, /* tp_repr */
2110 &set_as_number
, /* tp_as_number */
2111 &set_as_sequence
, /* tp_as_sequence */
2112 0, /* tp_as_mapping */
2113 (hashfunc
)PyObject_HashNotImplemented
, /* tp_hash */
2116 PyObject_GenericGetAttr
, /* tp_getattro */
2117 0, /* tp_setattro */
2118 0, /* tp_as_buffer */
2119 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2120 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2121 set_doc
, /* tp_doc */
2122 (traverseproc
)set_traverse
, /* tp_traverse */
2123 (inquiry
)set_clear_internal
, /* tp_clear */
2124 (richcmpfunc
)set_richcompare
, /* tp_richcompare */
2125 offsetof(PySetObject
, weakreflist
), /* tp_weaklistoffset */
2126 (getiterfunc
)set_iter
, /* tp_iter */
2127 0, /* tp_iternext */
2128 set_methods
, /* tp_methods */
2133 0, /* tp_descr_get */
2134 0, /* tp_descr_set */
2135 0, /* tp_dictoffset */
2136 (initproc
)set_init
, /* tp_init */
2137 PyType_GenericAlloc
, /* tp_alloc */
2138 set_new
, /* tp_new */
2139 PyObject_GC_Del
, /* tp_free */
2142 /* frozenset object ********************************************************/
2145 static PyMethodDef frozenset_methods
[] = {
2146 {"__contains__",(PyCFunction
)set_direct_contains
, METH_O
| METH_COEXIST
,
2148 {"copy", (PyCFunction
)frozenset_copy
, METH_NOARGS
,
2150 {"difference", (PyCFunction
)set_difference_multi
, METH_VARARGS
,
2152 {"intersection",(PyCFunction
)set_intersection_multi
, METH_VARARGS
,
2154 {"isdisjoint", (PyCFunction
)set_isdisjoint
, METH_O
,
2156 {"issubset", (PyCFunction
)set_issubset
, METH_O
,
2158 {"issuperset", (PyCFunction
)set_issuperset
, METH_O
,
2160 {"__reduce__", (PyCFunction
)set_reduce
, METH_NOARGS
,
2162 {"__sizeof__", (PyCFunction
)set_sizeof
, METH_NOARGS
,
2164 {"symmetric_difference",(PyCFunction
)set_symmetric_difference
, METH_O
,
2165 symmetric_difference_doc
},
2166 {"union", (PyCFunction
)set_union
, METH_VARARGS
,
2168 {NULL
, NULL
} /* sentinel */
2171 static PyNumberMethods frozenset_as_number
= {
2173 (binaryfunc
)set_sub
, /*nb_subtract*/
2185 (binaryfunc
)set_and
, /*nb_and*/
2186 (binaryfunc
)set_xor
, /*nb_xor*/
2187 (binaryfunc
)set_or
, /*nb_or*/
2190 PyDoc_STRVAR(frozenset_doc
,
2191 "frozenset() -> empty frozenset object\n\
2192 frozenset(iterable) -> frozenset object\n\
2194 Build an immutable unordered collection of unique elements.");
2196 PyTypeObject PyFrozenSet_Type
= {
2197 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2198 "frozenset", /* tp_name */
2199 sizeof(PySetObject
), /* tp_basicsize */
2200 0, /* tp_itemsize */
2202 (destructor
)set_dealloc
, /* tp_dealloc */
2206 0, /* tp_reserved */
2207 (reprfunc
)set_repr
, /* tp_repr */
2208 &frozenset_as_number
, /* tp_as_number */
2209 &set_as_sequence
, /* tp_as_sequence */
2210 0, /* tp_as_mapping */
2211 frozenset_hash
, /* tp_hash */
2214 PyObject_GenericGetAttr
, /* tp_getattro */
2215 0, /* tp_setattro */
2216 0, /* tp_as_buffer */
2217 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2218 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2219 frozenset_doc
, /* tp_doc */
2220 (traverseproc
)set_traverse
, /* tp_traverse */
2221 (inquiry
)set_clear_internal
, /* tp_clear */
2222 (richcmpfunc
)set_richcompare
, /* tp_richcompare */
2223 offsetof(PySetObject
, weakreflist
), /* tp_weaklistoffset */
2224 (getiterfunc
)set_iter
, /* tp_iter */
2225 0, /* tp_iternext */
2226 frozenset_methods
, /* tp_methods */
2231 0, /* tp_descr_get */
2232 0, /* tp_descr_set */
2233 0, /* tp_dictoffset */
2235 PyType_GenericAlloc
, /* tp_alloc */
2236 frozenset_new
, /* tp_new */
2237 PyObject_GC_Del
, /* tp_free */
2241 /***** C API functions *************************************************/
2244 PySet_New(PyObject
*iterable
)
2246 return make_new_set(&PySet_Type
, iterable
);
2250 PyFrozenSet_New(PyObject
*iterable
)
2252 return make_new_set(&PyFrozenSet_Type
, iterable
);
2256 PySet_Size(PyObject
*anyset
)
2258 if (!PyAnySet_Check(anyset
)) {
2259 PyErr_BadInternalCall();
2262 return PySet_GET_SIZE(anyset
);
2266 PySet_Clear(PyObject
*set
)
2268 if (!PySet_Check(set
)) {
2269 PyErr_BadInternalCall();
2272 return set_clear_internal((PySetObject
*)set
);
2276 PySet_Contains(PyObject
*anyset
, PyObject
*key
)
2278 if (!PyAnySet_Check(anyset
)) {
2279 PyErr_BadInternalCall();
2282 return set_contains_key((PySetObject
*)anyset
, key
);
2286 PySet_Discard(PyObject
*set
, PyObject
*key
)
2288 if (!PySet_Check(set
)) {
2289 PyErr_BadInternalCall();
2292 return set_discard_key((PySetObject
*)set
, key
);
2296 PySet_Add(PyObject
*anyset
, PyObject
*key
)
2298 if (!PySet_Check(anyset
) &&
2299 (!PyFrozenSet_Check(anyset
) || Py_REFCNT(anyset
) != 1)) {
2300 PyErr_BadInternalCall();
2303 return set_add_key((PySetObject
*)anyset
, key
);
2307 _PySet_NextEntry(PyObject
*set
, Py_ssize_t
*pos
, PyObject
**key
, long *hash
)
2311 if (!PyAnySet_Check(set
)) {
2312 PyErr_BadInternalCall();
2315 if (set_next((PySetObject
*)set
, pos
, &entry
) == 0)
2318 *hash
= entry
->hash
;
2323 PySet_Pop(PyObject
*set
)
2325 if (!PySet_Check(set
)) {
2326 PyErr_BadInternalCall();
2329 return set_pop((PySetObject
*)set
);
2333 _PySet_Update(PyObject
*set
, PyObject
*iterable
)
2335 if (!PySet_Check(set
)) {
2336 PyErr_BadInternalCall();
2339 return set_update_internal((PySetObject
*)set
, iterable
);
2344 /* Test code to be called with any three element set.
2345 Returns True and original set is restored. */
2347 #define assertRaises(call_return_value, exception) \
2349 assert(call_return_value); \
2350 assert(PyErr_ExceptionMatches(exception)); \
2355 test_c_api(PySetObject
*so
)
2360 PyObject
*elem
=NULL
, *dup
=NULL
, *t
, *f
, *dup2
, *x
;
2361 PyObject
*ob
= (PyObject
*)so
;
2364 /* Verify preconditions and exercise type/size checks */
2365 assert(PyAnySet_Check(ob
));
2366 assert(PyAnySet_CheckExact(ob
));
2367 assert(!PyFrozenSet_CheckExact(ob
));
2368 assert(PySet_Size(ob
) == 3);
2369 assert(PySet_GET_SIZE(ob
) == 3);
2371 /* Raise TypeError for non-iterable constructor arguments */
2372 assertRaises(PySet_New(Py_None
) == NULL
, PyExc_TypeError
);
2373 assertRaises(PyFrozenSet_New(Py_None
) == NULL
, PyExc_TypeError
);
2375 /* Raise TypeError for unhashable key */
2376 dup
= PySet_New(ob
);
2377 assertRaises(PySet_Discard(ob
, dup
) == -1, PyExc_TypeError
);
2378 assertRaises(PySet_Contains(ob
, dup
) == -1, PyExc_TypeError
);
2379 assertRaises(PySet_Add(ob
, dup
) == -1, PyExc_TypeError
);
2381 /* Exercise successful pop, contains, add, and discard */
2382 elem
= PySet_Pop(ob
);
2383 assert(PySet_Contains(ob
, elem
) == 0);
2384 assert(PySet_GET_SIZE(ob
) == 2);
2385 assert(PySet_Add(ob
, elem
) == 0);
2386 assert(PySet_Contains(ob
, elem
) == 1);
2387 assert(PySet_GET_SIZE(ob
) == 3);
2388 assert(PySet_Discard(ob
, elem
) == 1);
2389 assert(PySet_GET_SIZE(ob
) == 2);
2390 assert(PySet_Discard(ob
, elem
) == 0);
2391 assert(PySet_GET_SIZE(ob
) == 2);
2393 /* Exercise clear */
2394 dup2
= PySet_New(dup
);
2395 assert(PySet_Clear(dup2
) == 0);
2396 assert(PySet_Size(dup2
) == 0);
2399 /* Raise SystemError on clear or update of frozen set */
2400 f
= PyFrozenSet_New(dup
);
2401 assertRaises(PySet_Clear(f
) == -1, PyExc_SystemError
);
2402 assertRaises(_PySet_Update(f
, dup
) == -1, PyExc_SystemError
);
2403 assert(PySet_Add(f
, elem
) == 0);
2405 assertRaises(PySet_Add(f
, elem
) == -1, PyExc_SystemError
);
2409 /* Exercise direct iteration */
2411 while (_PySet_NextEntry((PyObject
*)dup
, &i
, &x
, &hash
)) {
2412 s
= _PyUnicode_AsString(x
);
2413 assert(s
&& (s
[0] == 'a' || s
[0] == 'b' || s
[0] == 'c'));
2418 /* Exercise updates */
2419 dup2
= PySet_New(NULL
);
2420 assert(_PySet_Update(dup2
, dup
) == 0);
2421 assert(PySet_Size(dup2
) == 3);
2422 assert(_PySet_Update(dup2
, dup
) == 0);
2423 assert(PySet_Size(dup2
) == 3);
2426 /* Raise SystemError when self argument is not a set or frozenset. */
2428 assertRaises(PySet_Size(t
) == -1, PyExc_SystemError
);
2429 assertRaises(PySet_Contains(t
, elem
) == -1, PyExc_SystemError
);
2432 /* Raise SystemError when self argument is not a set. */
2433 f
= PyFrozenSet_New(dup
);
2434 assert(PySet_Size(f
) == 3);
2435 assert(PyFrozenSet_CheckExact(f
));
2436 assertRaises(PySet_Discard(f
, elem
) == -1, PyExc_SystemError
);
2437 assertRaises(PySet_Pop(f
) == NULL
, PyExc_SystemError
);
2440 /* Raise KeyError when popping from an empty set */
2441 assert(PyNumber_InPlaceSubtract(ob
, ob
) == ob
);
2443 assert(PySet_GET_SIZE(ob
) == 0);
2444 assertRaises(PySet_Pop(ob
) == NULL
, PyExc_KeyError
);
2446 /* Restore the set from the copy using the PyNumber API */
2447 assert(PyNumber_InPlaceOr(ob
, dup
) == ob
);
2450 /* Verify constructors accept NULL arguments */
2451 f
= PySet_New(NULL
);
2453 assert(PySet_GET_SIZE(f
) == 0);
2455 f
= PyFrozenSet_New(NULL
);
2457 assert(PyFrozenSet_CheckExact(f
));
2458 assert(PySet_GET_SIZE(f
) == 0);