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-6 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 so
->ob_type
->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 fprintf(fp
, "%s(...)", so
->ob_type
->tp_name
);
584 fprintf(fp
, "%s([", so
->ob_type
->tp_name
);
585 while (set_next(so
, &pos
, &entry
)) {
588 if (PyObject_Print(entry
->key
, fp
, 0) != 0) {
589 Py_ReprLeave((PyObject
*)so
);
594 Py_ReprLeave((PyObject
*)so
);
599 set_repr(PySetObject
*so
)
601 PyObject
*keys
, *result
=NULL
, *listrepr
;
602 int status
= Py_ReprEnter((PyObject
*)so
);
607 return PyString_FromFormat("%s(...)", so
->ob_type
->tp_name
);
610 keys
= PySequence_List((PyObject
*)so
);
613 listrepr
= PyObject_Repr(keys
);
615 if (listrepr
== NULL
)
618 result
= PyString_FromFormat("%s(%s)", so
->ob_type
->tp_name
,
619 PyString_AS_STRING(listrepr
));
622 Py_ReprLeave((PyObject
*)so
);
627 set_len(PyObject
*so
)
629 return ((PySetObject
*)so
)->used
;
633 set_merge(PySetObject
*so
, PyObject
*otherset
)
636 register Py_ssize_t i
;
637 register setentry
*entry
;
639 assert (PyAnySet_Check(so
));
640 assert (PyAnySet_Check(otherset
));
642 other
= (PySetObject
*)otherset
;
643 if (other
== so
|| other
->used
== 0)
644 /* a.update(a) or a.update({}); nothing to do */
646 /* Do one big resize at the start, rather than
647 * incrementally resizing as we insert new keys. Expect
648 * that there will be no (or few) overlapping keys.
650 if ((so
->fill
+ other
->used
)*3 >= (so
->mask
+1)*2) {
651 if (set_table_resize(so
, (so
->used
+ other
->used
)*2) != 0)
654 for (i
= 0; i
<= other
->mask
; i
++) {
655 entry
= &other
->table
[i
];
656 if (entry
->key
!= NULL
&&
657 entry
->key
!= dummy
) {
658 Py_INCREF(entry
->key
);
659 if (set_insert_key(so
, entry
->key
, entry
->hash
) == -1) {
660 Py_DECREF(entry
->key
);
669 set_contains_key(PySetObject
*so
, PyObject
*key
)
674 if (!PyString_CheckExact(key
) ||
675 (hash
= ((PyStringObject
*) key
)->ob_shash
) == -1) {
676 hash
= PyObject_Hash(key
);
680 entry
= (so
->lookup
)(so
, key
, hash
);
684 return key
!= NULL
&& key
!= dummy
;
688 set_contains_entry(PySetObject
*so
, setentry
*entry
)
693 lu_entry
= (so
->lookup
)(so
, entry
->key
, entry
->hash
);
694 if (lu_entry
== NULL
)
697 return key
!= NULL
&& key
!= dummy
;
701 set_pop(PySetObject
*so
)
703 register Py_ssize_t i
= 0;
704 register setentry
*entry
;
707 assert (PyAnySet_Check(so
));
709 PyErr_SetString(PyExc_KeyError
, "pop from an empty set");
713 /* Set entry to "the first" unused or dummy set entry. We abuse
714 * the hash field of slot 0 to hold a search finger:
715 * If slot 0 has a value, use slot 0.
716 * Else slot 0 is being used to hold a search finger,
717 * and we use its hash value as the first index to look.
719 entry
= &so
->table
[0];
720 if (entry
->key
== NULL
|| entry
->key
== dummy
) {
722 /* The hash field may be a real hash value, or it may be a
723 * legit search finger, or it may be a once-legit search
724 * finger that's out of bounds now because it wrapped around
725 * or the table shrunk -- simply make sure it's in bounds now.
727 if (i
> so
->mask
|| i
< 1)
728 i
= 1; /* skip slot 0 */
729 while ((entry
= &so
->table
[i
])->key
== NULL
|| entry
->key
==dummy
) {
739 so
->table
[0].hash
= i
+ 1; /* next place to start */
743 PyDoc_STRVAR(pop_doc
, "Remove and return an arbitrary set element.");
746 set_traverse(PySetObject
*so
, visitproc visit
, void *arg
)
751 while (set_next(so
, &pos
, &entry
))
752 Py_VISIT(entry
->key
);
757 frozenset_hash(PyObject
*self
)
759 PySetObject
*so
= (PySetObject
*)self
;
760 long h
, hash
= 1927868237L;
767 hash
*= PySet_GET_SIZE(self
) + 1;
768 while (set_next(so
, &pos
, &entry
)) {
769 /* Work to increase the bit dispersion for closely spaced hash
770 values. The is important because some use cases have many
771 combinations of a small number of elements with nearby
772 hashes so that many distinct combinations collapse to only
773 a handful of distinct hash values. */
775 hash
^= (h
^ (h
<< 16) ^ 89869747L) * 3644798167u;
777 hash
= hash
* 69069L + 907133923L;
785 set_nohash(PyObject
*self
)
787 PyErr_SetString(PyExc_TypeError
, "set objects are unhashable");
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_len(setiterobject
*si
)
812 if (si
->si_set
!= NULL
&& si
->si_used
== si
->si_set
->used
)
814 return PyInt_FromLong(len
);
817 PyDoc_STRVAR(length_hint_doc
, "Private method returning an estimate of len(list(it)).");
819 static PyMethodDef setiter_methods
[] = {
820 {"__length_hint__", (PyCFunction
)setiter_len
, METH_NOARGS
, length_hint_doc
},
821 {NULL
, NULL
} /* sentinel */
824 static PyObject
*setiter_iternext(setiterobject
*si
)
827 register Py_ssize_t i
, mask
;
828 register setentry
*entry
;
829 PySetObject
*so
= si
->si_set
;
833 assert (PyAnySet_Check(so
));
835 if (si
->si_used
!= so
->used
) {
836 PyErr_SetString(PyExc_RuntimeError
,
837 "Set changed size during iteration");
838 si
->si_used
= -1; /* Make this state sticky */
846 while (i
<= mask
&& (entry
[i
].key
== NULL
|| entry
[i
].key
== dummy
))
862 static PyTypeObject PySetIter_Type
= {
863 PyObject_HEAD_INIT(&PyType_Type
)
865 "setiterator", /* tp_name */
866 sizeof(setiterobject
), /* tp_basicsize */
869 (destructor
)setiter_dealloc
, /* tp_dealloc */
875 0, /* tp_as_number */
876 0, /* tp_as_sequence */
877 0, /* tp_as_mapping */
881 PyObject_GenericGetAttr
, /* tp_getattro */
883 0, /* tp_as_buffer */
884 Py_TPFLAGS_DEFAULT
, /* tp_flags */
888 0, /* tp_richcompare */
889 0, /* tp_weaklistoffset */
890 PyObject_SelfIter
, /* tp_iter */
891 (iternextfunc
)setiter_iternext
, /* tp_iternext */
892 setiter_methods
, /* tp_methods */
897 set_iter(PySetObject
*so
)
899 setiterobject
*si
= PyObject_New(setiterobject
, &PySetIter_Type
);
904 si
->si_used
= so
->used
;
907 return (PyObject
*)si
;
911 set_update_internal(PySetObject
*so
, PyObject
*other
)
915 if (PyAnySet_Check(other
))
916 return set_merge(so
, other
);
918 if (PyDict_Check(other
)) {
921 while (PyDict_Next(other
, &pos
, &key
, &value
)) {
922 if (set_add_key(so
, key
) == -1)
928 it
= PyObject_GetIter(other
);
932 while ((key
= PyIter_Next(it
)) != NULL
) {
933 if (set_add_key(so
, key
) == -1) {
941 if (PyErr_Occurred())
947 set_update(PySetObject
*so
, PyObject
*other
)
949 if (set_update_internal(so
, other
) == -1)
954 PyDoc_STRVAR(update_doc
,
955 "Update a set with the union of itself and another.");
958 make_new_set(PyTypeObject
*type
, PyObject
*iterable
)
960 register PySetObject
*so
= NULL
;
962 if (dummy
== NULL
) { /* Auto-initialize dummy */
963 dummy
= PyString_FromString("<dummy key>");
968 /* create PySetObject structure */
970 (type
== &PySet_Type
|| type
== &PyFrozenSet_Type
)) {
971 so
= free_sets
[--num_free_sets
];
972 assert (so
!= NULL
&& PyAnySet_CheckExact(so
));
974 _Py_NewReference((PyObject
*)so
);
975 EMPTY_TO_MINSIZE(so
);
976 PyObject_GC_Track(so
);
978 so
= (PySetObject
*)type
->tp_alloc(type
, 0);
981 /* tp_alloc has already zeroed the structure */
982 assert(so
->table
== NULL
&& so
->fill
== 0 && so
->used
== 0);
983 INIT_NONZERO_SET_SLOTS(so
);
986 so
->lookup
= set_lookkey_string
;
987 so
->weakreflist
= NULL
;
989 if (iterable
!= NULL
) {
990 if (set_update_internal(so
, iterable
) == -1) {
996 return (PyObject
*)so
;
999 /* The empty frozenset is a singleton */
1000 static PyObject
*emptyfrozenset
= NULL
;
1003 frozenset_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1005 PyObject
*iterable
= NULL
, *result
;
1007 if (type
== &PyFrozenSet_Type
&& !_PyArg_NoKeywords("frozenset()", kwds
))
1010 if (!PyArg_UnpackTuple(args
, type
->tp_name
, 0, 1, &iterable
))
1013 if (type
!= &PyFrozenSet_Type
)
1014 return make_new_set(type
, iterable
);
1016 if (iterable
!= NULL
) {
1017 /* frozenset(f) is idempotent */
1018 if (PyFrozenSet_CheckExact(iterable
)) {
1019 Py_INCREF(iterable
);
1022 result
= make_new_set(type
, iterable
);
1023 if (result
== NULL
|| PySet_GET_SIZE(result
))
1027 /* The empty frozenset is a singleton */
1028 if (emptyfrozenset
== NULL
)
1029 emptyfrozenset
= make_new_set(type
, NULL
);
1030 Py_XINCREF(emptyfrozenset
);
1031 return emptyfrozenset
;
1039 while (num_free_sets
) {
1041 so
= free_sets
[num_free_sets
];
1042 PyObject_GC_Del(so
);
1045 Py_CLEAR(emptyfrozenset
);
1049 set_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1051 if (type
== &PySet_Type
&& !_PyArg_NoKeywords("set()", kwds
))
1054 return make_new_set(type
, NULL
);
1057 /* set_swap_bodies() switches the contents of any two sets by moving their
1058 internal data pointers and, if needed, copying the internal smalltables.
1059 Semantically equivalent to:
1061 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1063 The function always succeeds and it leaves both objects in a stable state.
1064 Useful for creating temporary frozensets from sets for membership testing
1065 in __contains__(), discard(), and remove(). Also useful for operations
1066 that update in-place (by allowing an intermediate result to be swapped
1067 into one of the original inputs).
1071 set_swap_bodies(PySetObject
*a
, PySetObject
*b
)
1075 setentry
*(*f
)(PySetObject
*so
, PyObject
*key
, long hash
);
1076 setentry tab
[PySet_MINSIZE
];
1079 t
= a
->fill
; a
->fill
= b
->fill
; b
->fill
= t
;
1080 t
= a
->used
; a
->used
= b
->used
; b
->used
= t
;
1081 t
= a
->mask
; a
->mask
= b
->mask
; b
->mask
= t
;
1084 if (a
->table
== a
->smalltable
)
1086 a
->table
= b
->table
;
1087 if (b
->table
== b
->smalltable
)
1088 a
->table
= a
->smalltable
;
1091 f
= a
->lookup
; a
->lookup
= b
->lookup
; b
->lookup
= f
;
1093 if (a
->table
== a
->smalltable
|| b
->table
== b
->smalltable
) {
1094 memcpy(tab
, a
->smalltable
, sizeof(tab
));
1095 memcpy(a
->smalltable
, b
->smalltable
, sizeof(tab
));
1096 memcpy(b
->smalltable
, tab
, sizeof(tab
));
1099 if (PyType_IsSubtype(a
->ob_type
, &PyFrozenSet_Type
) &&
1100 PyType_IsSubtype(b
->ob_type
, &PyFrozenSet_Type
)) {
1101 h
= a
->hash
; a
->hash
= b
->hash
; b
->hash
= h
;
1109 set_copy(PySetObject
*so
)
1111 return make_new_set(so
->ob_type
, (PyObject
*)so
);
1115 frozenset_copy(PySetObject
*so
)
1117 if (PyFrozenSet_CheckExact(so
)) {
1119 return (PyObject
*)so
;
1121 return set_copy(so
);
1124 PyDoc_STRVAR(copy_doc
, "Return a shallow copy of a set.");
1127 set_clear(PySetObject
*so
)
1129 set_clear_internal(so
);
1133 PyDoc_STRVAR(clear_doc
, "Remove all elements from this set.");
1136 set_union(PySetObject
*so
, PyObject
*other
)
1138 PySetObject
*result
;
1140 result
= (PySetObject
*)set_copy(so
);
1143 if ((PyObject
*)so
== other
)
1144 return (PyObject
*)result
;
1145 if (set_update_internal(result
, other
) == -1) {
1149 return (PyObject
*)result
;
1152 PyDoc_STRVAR(union_doc
,
1153 "Return the union of two sets as a new set.\n\
1155 (i.e. all elements that are in either set.)");
1158 set_or(PySetObject
*so
, PyObject
*other
)
1160 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1161 Py_INCREF(Py_NotImplemented
);
1162 return Py_NotImplemented
;
1164 return set_union(so
, other
);
1168 set_ior(PySetObject
*so
, PyObject
*other
)
1170 if (!PyAnySet_Check(other
)) {
1171 Py_INCREF(Py_NotImplemented
);
1172 return Py_NotImplemented
;
1174 if (set_update_internal(so
, other
) == -1)
1177 return (PyObject
*)so
;
1181 set_intersection(PySetObject
*so
, PyObject
*other
)
1183 PySetObject
*result
;
1184 PyObject
*key
, *it
, *tmp
;
1186 if ((PyObject
*)so
== other
)
1187 return set_copy(so
);
1189 result
= (PySetObject
*)make_new_set(so
->ob_type
, NULL
);
1193 if (PyAnySet_Check(other
)) {
1197 if (PySet_GET_SIZE(other
) > PySet_GET_SIZE(so
)) {
1198 tmp
= (PyObject
*)so
;
1199 so
= (PySetObject
*)other
;
1203 while (set_next((PySetObject
*)other
, &pos
, &entry
)) {
1204 int rv
= set_contains_entry(so
, entry
);
1210 if (set_add_entry(result
, entry
) == -1) {
1216 return (PyObject
*)result
;
1219 it
= PyObject_GetIter(other
);
1225 while ((key
= PyIter_Next(it
)) != NULL
) {
1228 long hash
= PyObject_Hash(key
);
1238 rv
= set_contains_entry(so
, &entry
);
1246 if (set_add_entry(result
, &entry
) == -1) {
1256 if (PyErr_Occurred()) {
1260 return (PyObject
*)result
;
1263 PyDoc_STRVAR(intersection_doc
,
1264 "Return the intersection of two sets as a new set.\n\
1266 (i.e. all elements that are in both sets.)");
1269 set_intersection_update(PySetObject
*so
, PyObject
*other
)
1273 tmp
= set_intersection(so
, other
);
1276 set_swap_bodies(so
, (PySetObject
*)tmp
);
1281 PyDoc_STRVAR(intersection_update_doc
,
1282 "Update a set with the intersection of itself and another.");
1285 set_and(PySetObject
*so
, PyObject
*other
)
1287 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1288 Py_INCREF(Py_NotImplemented
);
1289 return Py_NotImplemented
;
1291 return set_intersection(so
, other
);
1295 set_iand(PySetObject
*so
, PyObject
*other
)
1299 if (!PyAnySet_Check(other
)) {
1300 Py_INCREF(Py_NotImplemented
);
1301 return Py_NotImplemented
;
1303 result
= set_intersection_update(so
, other
);
1308 return (PyObject
*)so
;
1312 set_difference_update_internal(PySetObject
*so
, PyObject
*other
)
1314 if ((PyObject
*)so
== other
)
1315 return set_clear_internal(so
);
1317 if (PyAnySet_Check(other
)) {
1321 while (set_next((PySetObject
*)other
, &pos
, &entry
))
1322 if (set_discard_entry(so
, entry
) == -1)
1326 it
= PyObject_GetIter(other
);
1330 while ((key
= PyIter_Next(it
)) != NULL
) {
1331 if (set_discard_key(so
, key
) == -1) {
1339 if (PyErr_Occurred())
1342 /* If more than 1/5 are dummies, then resize them away. */
1343 if ((so
->fill
- so
->used
) * 5 < so
->mask
)
1345 return set_table_resize(so
, so
->used
>50000 ? so
->used
*2 : so
->used
*4);
1349 set_difference_update(PySetObject
*so
, PyObject
*other
)
1351 if (set_difference_update_internal(so
, other
) != -1)
1356 PyDoc_STRVAR(difference_update_doc
,
1357 "Remove all elements of another set from this set.");
1360 set_difference(PySetObject
*so
, PyObject
*other
)
1366 if (!PyAnySet_Check(other
) && !PyDict_Check(other
)) {
1367 result
= set_copy(so
);
1370 if (set_difference_update_internal((PySetObject
*)result
, other
) != -1)
1376 result
= make_new_set(so
->ob_type
, NULL
);
1380 if (PyDict_Check(other
)) {
1381 while (set_next(so
, &pos
, &entry
)) {
1383 entrycopy
.hash
= entry
->hash
;
1384 entrycopy
.key
= entry
->key
;
1385 if (!PyDict_Contains(other
, entry
->key
)) {
1386 if (set_add_entry((PySetObject
*)result
, &entrycopy
) == -1) {
1395 while (set_next(so
, &pos
, &entry
)) {
1396 int rv
= set_contains_entry((PySetObject
*)other
, entry
);
1402 if (set_add_entry((PySetObject
*)result
, entry
) == -1) {
1411 PyDoc_STRVAR(difference_doc
,
1412 "Return the difference of two sets as a new set.\n\
1414 (i.e. all elements that are in this set but not the other.)");
1416 set_sub(PySetObject
*so
, PyObject
*other
)
1418 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1419 Py_INCREF(Py_NotImplemented
);
1420 return Py_NotImplemented
;
1422 return set_difference(so
, other
);
1426 set_isub(PySetObject
*so
, PyObject
*other
)
1430 if (!PyAnySet_Check(other
)) {
1431 Py_INCREF(Py_NotImplemented
);
1432 return Py_NotImplemented
;
1434 result
= set_difference_update(so
, other
);
1439 return (PyObject
*)so
;
1443 set_symmetric_difference_update(PySetObject
*so
, PyObject
*other
)
1445 PySetObject
*otherset
;
1450 if ((PyObject
*)so
== other
)
1451 return set_clear(so
);
1453 if (PyDict_Check(other
)) {
1456 while (PyDict_Next(other
, &pos
, &key
, &value
)) {
1458 long hash
= PyObject_Hash(key
);
1462 an_entry
.hash
= hash
;
1464 rv
= set_discard_entry(so
, &an_entry
);
1467 if (rv
== DISCARD_NOTFOUND
) {
1468 if (set_add_entry(so
, &an_entry
) == -1)
1475 if (PyAnySet_Check(other
)) {
1477 otherset
= (PySetObject
*)other
;
1479 otherset
= (PySetObject
*)make_new_set(so
->ob_type
, other
);
1480 if (otherset
== NULL
)
1484 while (set_next(otherset
, &pos
, &entry
)) {
1485 int rv
= set_discard_entry(so
, entry
);
1487 Py_DECREF(otherset
);
1490 if (rv
== DISCARD_NOTFOUND
) {
1491 if (set_add_entry(so
, entry
) == -1) {
1492 Py_DECREF(otherset
);
1497 Py_DECREF(otherset
);
1501 PyDoc_STRVAR(symmetric_difference_update_doc
,
1502 "Update a set with the symmetric difference of itself and another.");
1505 set_symmetric_difference(PySetObject
*so
, PyObject
*other
)
1508 PySetObject
*otherset
;
1510 otherset
= (PySetObject
*)make_new_set(so
->ob_type
, other
);
1511 if (otherset
== NULL
)
1513 rv
= set_symmetric_difference_update(otherset
, (PyObject
*)so
);
1517 return (PyObject
*)otherset
;
1520 PyDoc_STRVAR(symmetric_difference_doc
,
1521 "Return the symmetric difference of two sets as a new set.\n\
1523 (i.e. all elements that are in exactly one of the sets.)");
1526 set_xor(PySetObject
*so
, PyObject
*other
)
1528 if (!PyAnySet_Check(so
) || !PyAnySet_Check(other
)) {
1529 Py_INCREF(Py_NotImplemented
);
1530 return Py_NotImplemented
;
1532 return set_symmetric_difference(so
, other
);
1536 set_ixor(PySetObject
*so
, PyObject
*other
)
1540 if (!PyAnySet_Check(other
)) {
1541 Py_INCREF(Py_NotImplemented
);
1542 return Py_NotImplemented
;
1544 result
= set_symmetric_difference_update(so
, other
);
1549 return (PyObject
*)so
;
1553 set_issubset(PySetObject
*so
, PyObject
*other
)
1558 if (!PyAnySet_Check(other
)) {
1559 PyObject
*tmp
, *result
;
1560 tmp
= make_new_set(&PySet_Type
, other
);
1563 result
= set_issubset(so
, tmp
);
1567 if (PySet_GET_SIZE(so
) > PySet_GET_SIZE(other
))
1570 while (set_next(so
, &pos
, &entry
)) {
1571 int rv
= set_contains_entry((PySetObject
*)other
, entry
);
1580 PyDoc_STRVAR(issubset_doc
, "Report whether another set contains this set.");
1583 set_issuperset(PySetObject
*so
, PyObject
*other
)
1585 PyObject
*tmp
, *result
;
1587 if (!PyAnySet_Check(other
)) {
1588 tmp
= make_new_set(&PySet_Type
, other
);
1591 result
= set_issuperset(so
, tmp
);
1595 return set_issubset((PySetObject
*)other
, (PyObject
*)so
);
1598 PyDoc_STRVAR(issuperset_doc
, "Report whether this set contains another set.");
1601 set_richcompare(PySetObject
*v
, PyObject
*w
, int op
)
1605 if(!PyAnySet_Check(w
)) {
1610 PyErr_SetString(PyExc_TypeError
, "can only compare to a set");
1615 if (PySet_GET_SIZE(v
) != PySet_GET_SIZE(w
))
1617 if (v
->hash
!= -1 &&
1618 ((PySetObject
*)w
)->hash
!= -1 &&
1619 v
->hash
!= ((PySetObject
*)w
)->hash
)
1621 return set_issubset(v
, w
);
1623 r1
= set_richcompare(v
, w
, Py_EQ
);
1626 r2
= PyBool_FromLong(PyObject_Not(r1
));
1630 return set_issubset(v
, w
);
1632 return set_issuperset(v
, w
);
1634 if (PySet_GET_SIZE(v
) >= PySet_GET_SIZE(w
))
1636 return set_issubset(v
, w
);
1638 if (PySet_GET_SIZE(v
) <= PySet_GET_SIZE(w
))
1640 return set_issuperset(v
, w
);
1642 Py_INCREF(Py_NotImplemented
);
1643 return Py_NotImplemented
;
1647 set_nocmp(PyObject
*self
, PyObject
*other
)
1649 PyErr_SetString(PyExc_TypeError
, "cannot compare sets using cmp()");
1654 set_add(PySetObject
*so
, PyObject
*key
)
1656 if (set_add_key(so
, key
) == -1)
1661 PyDoc_STRVAR(add_doc
,
1662 "Add an element to a set.\n\
1664 This has no effect if the element is already present.");
1667 set_contains(PySetObject
*so
, PyObject
*key
)
1672 rv
= set_contains_key(so
, key
);
1674 if (!PyAnySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1677 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1680 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1681 rv
= set_contains(so
, tmpkey
);
1682 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1689 set_direct_contains(PySetObject
*so
, PyObject
*key
)
1693 result
= set_contains(so
, key
);
1696 return PyBool_FromLong(result
);
1699 PyDoc_STRVAR(contains_doc
, "x.__contains__(y) <==> y in x.");
1702 set_remove(PySetObject
*so
, PyObject
*key
)
1704 PyObject
*tmpkey
, *result
;
1707 rv
= set_discard_key(so
, key
);
1709 if (!PyAnySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1712 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1715 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1716 result
= set_remove(so
, tmpkey
);
1717 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1720 } else if (rv
== DISCARD_NOTFOUND
) {
1727 PyDoc_STRVAR(remove_doc
,
1728 "Remove an element from a set; it must be a member.\n\
1730 If the element is not a member, raise a KeyError.");
1733 set_discard(PySetObject
*so
, PyObject
*key
)
1735 PyObject
*tmpkey
, *result
;
1738 rv
= set_discard_key(so
, key
);
1740 if (!PyAnySet_Check(key
) || !PyErr_ExceptionMatches(PyExc_TypeError
))
1743 tmpkey
= make_new_set(&PyFrozenSet_Type
, NULL
);
1746 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1747 result
= set_discard(so
, tmpkey
);
1748 set_swap_bodies((PySetObject
*)tmpkey
, (PySetObject
*)key
);
1755 PyDoc_STRVAR(discard_doc
,
1756 "Remove an element from a set if it is a member.\n\
1758 If the element is not a member, do nothing.");
1761 set_reduce(PySetObject
*so
)
1763 PyObject
*keys
=NULL
, *args
=NULL
, *result
=NULL
, *dict
=NULL
;
1765 keys
= PySequence_List((PyObject
*)so
);
1768 args
= PyTuple_Pack(1, keys
);
1771 dict
= PyObject_GetAttrString((PyObject
*)so
, "__dict__");
1777 result
= PyTuple_Pack(3, so
->ob_type
, args
, dict
);
1785 PyDoc_STRVAR(reduce_doc
, "Return state information for pickling.");
1788 set_init(PySetObject
*self
, PyObject
*args
, PyObject
*kwds
)
1790 PyObject
*iterable
= NULL
;
1792 if (!PyAnySet_Check(self
))
1794 if (!PyArg_UnpackTuple(args
, self
->ob_type
->tp_name
, 0, 1, &iterable
))
1796 set_clear_internal(self
);
1798 if (iterable
== NULL
)
1800 return set_update_internal(self
, iterable
);
1803 static PySequenceMethods set_as_sequence
= {
1804 set_len
, /* sq_length */
1809 0, /* sq_ass_item */
1810 0, /* sq_ass_slice */
1811 (objobjproc
)set_contains
, /* sq_contains */
1814 /* set object ********************************************************/
1817 static PyObject
*test_c_api(PySetObject
*so
);
1819 PyDoc_STRVAR(test_c_api_doc
, "Exercises C API. Returns True.\n\
1820 All is well if assertions don't fail.");
1823 static PyMethodDef set_methods
[] = {
1824 {"add", (PyCFunction
)set_add
, METH_O
,
1826 {"clear", (PyCFunction
)set_clear
, METH_NOARGS
,
1828 {"__contains__",(PyCFunction
)set_direct_contains
, METH_O
| METH_COEXIST
,
1830 {"copy", (PyCFunction
)set_copy
, METH_NOARGS
,
1832 {"discard", (PyCFunction
)set_discard
, METH_O
,
1834 {"difference", (PyCFunction
)set_difference
, METH_O
,
1836 {"difference_update", (PyCFunction
)set_difference_update
, METH_O
,
1837 difference_update_doc
},
1838 {"intersection",(PyCFunction
)set_intersection
, METH_O
,
1840 {"intersection_update",(PyCFunction
)set_intersection_update
, METH_O
,
1841 intersection_update_doc
},
1842 {"issubset", (PyCFunction
)set_issubset
, METH_O
,
1844 {"issuperset", (PyCFunction
)set_issuperset
, METH_O
,
1846 {"pop", (PyCFunction
)set_pop
, METH_NOARGS
,
1848 {"__reduce__", (PyCFunction
)set_reduce
, METH_NOARGS
,
1850 {"remove", (PyCFunction
)set_remove
, METH_O
,
1852 {"symmetric_difference",(PyCFunction
)set_symmetric_difference
, METH_O
,
1853 symmetric_difference_doc
},
1854 {"symmetric_difference_update",(PyCFunction
)set_symmetric_difference_update
, METH_O
,
1855 symmetric_difference_update_doc
},
1857 {"test_c_api", (PyCFunction
)test_c_api
, METH_NOARGS
,
1860 {"union", (PyCFunction
)set_union
, METH_O
,
1862 {"update", (PyCFunction
)set_update
, METH_O
,
1864 {NULL
, NULL
} /* sentinel */
1867 static PyNumberMethods set_as_number
= {
1869 (binaryfunc
)set_sub
, /*nb_subtract*/
1882 (binaryfunc
)set_and
, /*nb_and*/
1883 (binaryfunc
)set_xor
, /*nb_xor*/
1884 (binaryfunc
)set_or
, /*nb_or*/
1891 0, /*nb_inplace_add*/
1892 (binaryfunc
)set_isub
, /*nb_inplace_subtract*/
1893 0, /*nb_inplace_multiply*/
1894 0, /*nb_inplace_divide*/
1895 0, /*nb_inplace_remainder*/
1896 0, /*nb_inplace_power*/
1897 0, /*nb_inplace_lshift*/
1898 0, /*nb_inplace_rshift*/
1899 (binaryfunc
)set_iand
, /*nb_inplace_and*/
1900 (binaryfunc
)set_ixor
, /*nb_inplace_xor*/
1901 (binaryfunc
)set_ior
, /*nb_inplace_or*/
1904 PyDoc_STRVAR(set_doc
,
1905 "set(iterable) --> set object\n\
1907 Build an unordered collection of unique elements.");
1909 PyTypeObject PySet_Type
= {
1910 PyObject_HEAD_INIT(&PyType_Type
)
1912 "set", /* tp_name */
1913 sizeof(PySetObject
), /* tp_basicsize */
1914 0, /* tp_itemsize */
1916 (destructor
)set_dealloc
, /* tp_dealloc */
1917 (printfunc
)set_tp_print
, /* tp_print */
1920 set_nocmp
, /* tp_compare */
1921 (reprfunc
)set_repr
, /* tp_repr */
1922 &set_as_number
, /* tp_as_number */
1923 &set_as_sequence
, /* tp_as_sequence */
1924 0, /* tp_as_mapping */
1925 set_nohash
, /* tp_hash */
1928 PyObject_GenericGetAttr
, /* tp_getattro */
1929 0, /* tp_setattro */
1930 0, /* tp_as_buffer */
1931 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES
|
1932 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1933 set_doc
, /* tp_doc */
1934 (traverseproc
)set_traverse
, /* tp_traverse */
1935 (inquiry
)set_clear_internal
, /* tp_clear */
1936 (richcmpfunc
)set_richcompare
, /* tp_richcompare */
1937 offsetof(PySetObject
, weakreflist
), /* tp_weaklistoffset */
1938 (getiterfunc
)set_iter
, /* tp_iter */
1939 0, /* tp_iternext */
1940 set_methods
, /* tp_methods */
1945 0, /* tp_descr_get */
1946 0, /* tp_descr_set */
1947 0, /* tp_dictoffset */
1948 (initproc
)set_init
, /* tp_init */
1949 PyType_GenericAlloc
, /* tp_alloc */
1950 set_new
, /* tp_new */
1951 PyObject_GC_Del
, /* tp_free */
1954 /* frozenset object ********************************************************/
1957 static PyMethodDef frozenset_methods
[] = {
1958 {"__contains__",(PyCFunction
)set_direct_contains
, METH_O
| METH_COEXIST
,
1960 {"copy", (PyCFunction
)frozenset_copy
, METH_NOARGS
,
1962 {"difference", (PyCFunction
)set_difference
, METH_O
,
1964 {"intersection",(PyCFunction
)set_intersection
, METH_O
,
1966 {"issubset", (PyCFunction
)set_issubset
, METH_O
,
1968 {"issuperset", (PyCFunction
)set_issuperset
, METH_O
,
1970 {"__reduce__", (PyCFunction
)set_reduce
, METH_NOARGS
,
1972 {"symmetric_difference",(PyCFunction
)set_symmetric_difference
, METH_O
,
1973 symmetric_difference_doc
},
1974 {"union", (PyCFunction
)set_union
, METH_O
,
1976 {NULL
, NULL
} /* sentinel */
1979 static PyNumberMethods frozenset_as_number
= {
1981 (binaryfunc
)set_sub
, /*nb_subtract*/
1994 (binaryfunc
)set_and
, /*nb_and*/
1995 (binaryfunc
)set_xor
, /*nb_xor*/
1996 (binaryfunc
)set_or
, /*nb_or*/
1999 PyDoc_STRVAR(frozenset_doc
,
2000 "frozenset(iterable) --> frozenset object\n\
2002 Build an immutable unordered collection of unique elements.");
2004 PyTypeObject PyFrozenSet_Type
= {
2005 PyObject_HEAD_INIT(&PyType_Type
)
2007 "frozenset", /* tp_name */
2008 sizeof(PySetObject
), /* tp_basicsize */
2009 0, /* tp_itemsize */
2011 (destructor
)set_dealloc
, /* tp_dealloc */
2012 (printfunc
)set_tp_print
, /* tp_print */
2015 set_nocmp
, /* tp_compare */
2016 (reprfunc
)set_repr
, /* tp_repr */
2017 &frozenset_as_number
, /* tp_as_number */
2018 &set_as_sequence
, /* tp_as_sequence */
2019 0, /* tp_as_mapping */
2020 frozenset_hash
, /* tp_hash */
2023 PyObject_GenericGetAttr
, /* tp_getattro */
2024 0, /* tp_setattro */
2025 0, /* tp_as_buffer */
2026 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
| Py_TPFLAGS_CHECKTYPES
|
2027 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2028 frozenset_doc
, /* tp_doc */
2029 (traverseproc
)set_traverse
, /* tp_traverse */
2030 (inquiry
)set_clear_internal
, /* tp_clear */
2031 (richcmpfunc
)set_richcompare
, /* tp_richcompare */
2032 offsetof(PySetObject
, weakreflist
), /* tp_weaklistoffset */
2033 (getiterfunc
)set_iter
, /* tp_iter */
2034 0, /* tp_iternext */
2035 frozenset_methods
, /* tp_methods */
2040 0, /* tp_descr_get */
2041 0, /* tp_descr_set */
2042 0, /* tp_dictoffset */
2044 PyType_GenericAlloc
, /* tp_alloc */
2045 frozenset_new
, /* tp_new */
2046 PyObject_GC_Del
, /* tp_free */
2050 /***** C API functions *************************************************/
2053 PySet_New(PyObject
*iterable
)
2055 return make_new_set(&PySet_Type
, iterable
);
2059 PyFrozenSet_New(PyObject
*iterable
)
2061 PyObject
*args
, *result
;
2063 if (iterable
== NULL
)
2064 args
= PyTuple_New(0);
2066 args
= PyTuple_Pack(1, iterable
);
2069 result
= frozenset_new(&PyFrozenSet_Type
, args
, NULL
);
2075 PySet_Size(PyObject
*anyset
)
2077 if (!PyAnySet_Check(anyset
)) {
2078 PyErr_BadInternalCall();
2081 return PySet_GET_SIZE(anyset
);
2085 PySet_Clear(PyObject
*set
)
2087 if (!PyType_IsSubtype(set
->ob_type
, &PySet_Type
)) {
2088 PyErr_BadInternalCall();
2091 return set_clear_internal((PySetObject
*)set
);
2095 PySet_Contains(PyObject
*anyset
, PyObject
*key
)
2097 if (!PyAnySet_Check(anyset
)) {
2098 PyErr_BadInternalCall();
2101 return set_contains_key((PySetObject
*)anyset
, key
);
2105 PySet_Discard(PyObject
*set
, PyObject
*key
)
2107 if (!PyType_IsSubtype(set
->ob_type
, &PySet_Type
)) {
2108 PyErr_BadInternalCall();
2111 return set_discard_key((PySetObject
*)set
, key
);
2115 PySet_Add(PyObject
*set
, PyObject
*key
)
2117 if (!PyType_IsSubtype(set
->ob_type
, &PySet_Type
)) {
2118 PyErr_BadInternalCall();
2121 return set_add_key((PySetObject
*)set
, key
);
2125 _PySet_Next(PyObject
*set
, Py_ssize_t
*pos
, PyObject
**entry
)
2127 setentry
*entry_ptr
;
2129 if (!PyAnySet_Check(set
)) {
2130 PyErr_BadInternalCall();
2133 if (set_next((PySetObject
*)set
, pos
, &entry_ptr
) == 0)
2135 *entry
= entry_ptr
->key
;
2140 PySet_Pop(PyObject
*set
)
2142 if (!PyType_IsSubtype(set
->ob_type
, &PySet_Type
)) {
2143 PyErr_BadInternalCall();
2146 return set_pop((PySetObject
*)set
);
2150 _PySet_Update(PyObject
*set
, PyObject
*iterable
)
2152 if (!PyType_IsSubtype(set
->ob_type
, &PySet_Type
)) {
2153 PyErr_BadInternalCall();
2156 return set_update_internal((PySetObject
*)set
, iterable
);
2161 /* Test code to be called with any three element set.
2162 Returns True and original set is restored. */
2164 #define assertRaises(call_return_value, exception) \
2166 assert(call_return_value); \
2167 assert(PyErr_ExceptionMatches(exception)); \
2172 test_c_api(PySetObject
*so
)
2177 PyObject
*elem
, *dup
, *t
, *f
, *dup2
;
2178 PyObject
*ob
= (PyObject
*)so
;
2180 /* Verify preconditions and exercise type/size checks */
2181 assert(PyAnySet_Check(ob
));
2182 assert(PyAnySet_CheckExact(ob
));
2183 assert(!PyFrozenSet_CheckExact(ob
));
2184 assert(PySet_Size(ob
) == 3);
2185 assert(PySet_GET_SIZE(ob
) == 3);
2187 /* Raise TypeError for non-iterable constructor arguments */
2188 assertRaises(PySet_New(Py_None
) == NULL
, PyExc_TypeError
);
2189 assertRaises(PyFrozenSet_New(Py_None
) == NULL
, PyExc_TypeError
);
2191 /* Raise TypeError for unhashable key */
2192 dup
= PySet_New(ob
);
2193 assertRaises(PySet_Discard(ob
, dup
) == -1, PyExc_TypeError
);
2194 assertRaises(PySet_Contains(ob
, dup
) == -1, PyExc_TypeError
);
2195 assertRaises(PySet_Add(ob
, dup
) == -1, PyExc_TypeError
);
2197 /* Exercise successful pop, contains, add, and discard */
2198 elem
= PySet_Pop(ob
);
2199 assert(PySet_Contains(ob
, elem
) == 0);
2200 assert(PySet_GET_SIZE(ob
) == 2);
2201 assert(PySet_Add(ob
, elem
) == 0);
2202 assert(PySet_Contains(ob
, elem
) == 1);
2203 assert(PySet_GET_SIZE(ob
) == 3);
2204 assert(PySet_Discard(ob
, elem
) == 1);
2205 assert(PySet_GET_SIZE(ob
) == 2);
2206 assert(PySet_Discard(ob
, elem
) == 0);
2207 assert(PySet_GET_SIZE(ob
) == 2);
2209 /* Exercise clear */
2210 dup2
= PySet_New(dup
);
2211 assert(PySet_Clear(dup2
) == 0);
2212 assert(PySet_Size(dup2
) == 0);
2215 /* Raise SystemError on clear or update of frozen set */
2216 f
= PyFrozenSet_New(dup
);
2217 assertRaises(PySet_Clear(f
) == -1, PyExc_SystemError
);
2218 assertRaises(_PySet_Update(f
, dup
) == -1, PyExc_SystemError
);
2221 /* Exercise direct iteration */
2223 while (_PySet_Next((PyObject
*)dup
, &i
, &elem
)) {
2224 s
= PyString_AsString(elem
);
2225 assert(s
&& (s
[0] == 'a' || s
[0] == 'b' || s
[0] == 'c'));
2230 /* Exercise updates */
2231 dup2
= PySet_New(NULL
);
2232 assert(_PySet_Update(dup2
, dup
) == 0);
2233 assert(PySet_Size(dup2
) == 3);
2234 assert(_PySet_Update(dup2
, dup
) == 0);
2235 assert(PySet_Size(dup2
) == 3);
2238 /* Raise SystemError when self argument is not a set or frozenset. */
2240 assertRaises(PySet_Size(t
) == -1, PyExc_SystemError
);
2241 assertRaises(PySet_Contains(t
, elem
) == -1, PyExc_SystemError
);
2244 /* Raise SystemError when self argument is not a set. */
2245 f
= PyFrozenSet_New(dup
);
2246 assert(PySet_Size(f
) == 3);
2247 assert(PyFrozenSet_CheckExact(f
));
2248 assertRaises(PySet_Add(f
, elem
) == -1, PyExc_SystemError
);
2249 assertRaises(PySet_Discard(f
, elem
) == -1, PyExc_SystemError
);
2250 assertRaises(PySet_Pop(f
) == NULL
, PyExc_SystemError
);
2253 /* Raise KeyError when popping from an empty set */
2254 assert(PyNumber_InPlaceSubtract(ob
, ob
) == ob
);
2256 assert(PySet_GET_SIZE(ob
) == 0);
2257 assertRaises(PySet_Pop(ob
) == NULL
, PyExc_KeyError
);
2259 /* Restore the set from the copy using the PyNumber API */
2260 assert(PyNumber_InPlaceOr(ob
, dup
) == ob
);
2263 /* Verify constructors accept NULL arguments */
2264 f
= PySet_New(NULL
);
2266 assert(PySet_GET_SIZE(f
) == 0);
2268 f
= PyFrozenSet_New(NULL
);
2270 assert(PyFrozenSet_CheckExact(f
));
2271 assert(PySet_GET_SIZE(f
) == 0);