blob: c4cd562d58246a8b7c5fa4aea96d39181d59d0e3 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Raymond Hettingera9d99362005-08-05 00:01:15 +00006 Copyright (c) 2003-5 Python Software Foundation.
7 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
13/* This must be >= 1. */
14#define PERTURB_SHIFT 5
15
16/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000017static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000018
Raymond Hettingerbc841a12005-08-07 13:02:53 +000019#define INIT_NONZERO_SET_SLOTS(so) do { \
20 (so)->table = (so)->smalltable; \
21 (so)->mask = PySet_MINSIZE - 1; \
22 (so)->hash = -1; \
23 } while(0)
24
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000025#define EMPTY_TO_MINSIZE(so) do { \
26 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
27 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000028 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000029 } while(0)
30
Raymond Hettingerbc841a12005-08-07 13:02:53 +000031/* Reuse scheme to save calls to malloc, free, and memset */
32#define MAXFREESETS 80
33static PySetObject *free_sets[MAXFREESETS];
34static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035
36/*
37The basic lookup function used by all operations.
38This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
39Open addressing is preferred over chaining since the link overhead for
40chaining would be substantial (100% with typical malloc overhead).
41
42The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000043probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000044
45All arithmetic on hash should ignore overflow.
46
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000047Unlike the dictionary implementation, the lookkey functions can return
48NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000049*/
50
51static setentry *
52set_lookkey(PySetObject *so, PyObject *key, register long hash)
53{
54 register int i;
55 register unsigned int perturb;
56 register setentry *freeslot;
57 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000058 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000059 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061 PyObject *startkey;
62
63 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000064 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000065 if (entry->key == NULL || entry->key == key)
66 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000067
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000068 if (entry->key == dummy)
69 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000071 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000072 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
74 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000075 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +000076 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000077 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000078 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000079 }
80 else {
81 /* The compare did major nasty stuff to the
82 * set: start over.
83 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000084 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000085 }
86 }
87 freeslot = NULL;
88 }
89
90 /* In the loop, key == dummy is by far (factor of 100s) the
91 least likely outcome, so test for that last. */
92 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
93 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +000094 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000095 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000096 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000097 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000098 break;
99 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000100 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000102 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000103 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000104 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
105 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000106 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000107 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000108 if (cmp > 0)
109 break;
110 }
111 else {
112 /* The compare did major nasty stuff to the
113 * set: start over.
114 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000115 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000116 }
117 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000118 else if (entry->key == dummy && freeslot == NULL)
119 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000121 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122}
123
124/*
125 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000126 * This means we can always use _PyString_Eq directly and not have to check to
127 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000128 */
129static setentry *
130set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
131{
132 register int i;
133 register unsigned int perturb;
134 register setentry *freeslot;
135 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000136 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000137 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138
139 /* Make sure this function doesn't have to handle non-string keys,
140 including subclasses of str; e.g., one reason to subclass
141 strings is to override __eq__, and for speed we don't cater to
142 that here. */
143 if (!PyString_CheckExact(key)) {
144 so->lookup = set_lookkey;
145 return set_lookkey(so, key, hash);
146 }
147 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000148 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000149 if (entry->key == NULL || entry->key == key)
150 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000151 if (entry->key == dummy)
152 freeslot = entry;
153 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000154 if (entry->hash == hash && _PyString_Eq(entry->key, key))
155 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000156 freeslot = NULL;
157 }
158
159 /* In the loop, key == dummy is by far (factor of 100s) the
160 least likely outcome, so test for that last. */
161 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
162 i = (i << 2) + i + perturb + 1;
163 entry = &table[i & mask];
164 if (entry->key == NULL)
165 return freeslot == NULL ? entry : freeslot;
166 if (entry->key == key
167 || (entry->hash == hash
168 && entry->key != dummy
169 && _PyString_Eq(entry->key, key)))
170 return entry;
171 if (entry->key == dummy && freeslot == NULL)
172 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000173 }
174}
175
176/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000177Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000178Used both by the internal resize routine and by the public insert routine.
179Eats a reference to key.
180*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000181static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000182set_insert_key(register PySetObject *so, PyObject *key, long hash)
183{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000184 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000185 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
186
187 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000188 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000189 if (entry == NULL)
190 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000191 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000192 /* UNUSED */
193 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000194 entry->key = key;
195 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000196 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000197 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000198 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000199 entry->key = key;
200 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000201 so->used++;
202 Py_DECREF(dummy);
203 } else {
204 /* ACTIVE */
205 Py_DECREF(key);
206 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000207 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208}
209
210/*
211Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000212keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000213actually be smaller than the old one.
214*/
215static int
216set_table_resize(PySetObject *so, int minused)
217{
218 int newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000219 setentry *oldtable, *newtable, *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220 int i;
221 int is_oldtable_malloced;
222 setentry small_copy[PySet_MINSIZE];
223
224 assert(minused >= 0);
225
226 /* Find the smallest table size > minused. */
227 for (newsize = PySet_MINSIZE;
228 newsize <= minused && newsize > 0;
229 newsize <<= 1)
230 ;
231 if (newsize <= 0) {
232 PyErr_NoMemory();
233 return -1;
234 }
235
236 /* Get space for a new table. */
237 oldtable = so->table;
238 assert(oldtable != NULL);
239 is_oldtable_malloced = oldtable != so->smalltable;
240
241 if (newsize == PySet_MINSIZE) {
242 /* A large table is shrinking, or we can't get any smaller. */
243 newtable = so->smalltable;
244 if (newtable == oldtable) {
245 if (so->fill == so->used) {
246 /* No dummies, so no point doing anything. */
247 return 0;
248 }
249 /* We're not going to resize it, but rebuild the
250 table anyway to purge old dummy entries.
251 Subtle: This is *necessary* if fill==size,
252 as set_lookkey needs at least one virgin slot to
253 terminate failing searches. If fill < size, it's
254 merely desirable, as dummies slow searches. */
255 assert(so->fill > so->used);
256 memcpy(small_copy, oldtable, sizeof(small_copy));
257 oldtable = small_copy;
258 }
259 }
260 else {
261 newtable = PyMem_NEW(setentry, newsize);
262 if (newtable == NULL) {
263 PyErr_NoMemory();
264 return -1;
265 }
266 }
267
268 /* Make the set empty, using the new table. */
269 assert(newtable != oldtable);
270 so->table = newtable;
271 so->mask = newsize - 1;
272 memset(newtable, 0, sizeof(setentry) * newsize);
273 so->used = 0;
274 i = so->fill;
275 so->fill = 0;
276
277 /* Copy the data over; this is refcount-neutral for active entries;
278 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000279 for (entry = oldtable; i > 0; entry++) {
280 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000281 /* UNUSED */
282 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000283 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284 /* DUMMY */
285 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000286 assert(entry->key == dummy);
287 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000288 } else {
289 /* ACTIVE */
290 --i;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000291 if(set_insert_key(so, entry->key, entry->hash) == -1) {
292 if (is_oldtable_malloced)
293 PyMem_DEL(oldtable);
294 return -1;
295 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296 }
297 }
298
299 if (is_oldtable_malloced)
300 PyMem_DEL(oldtable);
301 return 0;
302}
303
Raymond Hettingerc991db22005-08-11 07:58:45 +0000304/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
305
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000306static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000307set_add_entry(register PySetObject *so, setentry *entry)
308{
309 register int n_used;
310
311 assert(so->fill <= so->mask); /* at least one empty slot */
312 n_used = so->used;
313 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000314 if (set_insert_key(so, entry->key, entry->hash) == -1)
315 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000316 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
317 return 0;
318 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
319}
320
321static int
322set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323{
324 register long hash;
325 register int n_used;
326
Raymond Hettingerc991db22005-08-11 07:58:45 +0000327 if (!PyString_CheckExact(key) ||
328 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000329 hash = PyObject_Hash(key);
330 if (hash == -1)
331 return -1;
332 }
333 assert(so->fill <= so->mask); /* at least one empty slot */
334 n_used = so->used;
335 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000336 if (set_insert_key(so, key, hash) == -1) {
337 Py_DECREF(key);
338 return -1;
339 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000340 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
341 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000342 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000343}
344
345#define DISCARD_NOTFOUND 0
346#define DISCARD_FOUND 1
347
348static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000349set_discard_entry(PySetObject *so, setentry *oldentry)
350{ register setentry *entry;
351 PyObject *old_key;
352
353 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000354 if (entry == NULL)
355 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356 if (entry->key == NULL || entry->key == dummy)
357 return DISCARD_NOTFOUND;
358 old_key = entry->key;
359 Py_INCREF(dummy);
360 entry->key = dummy;
361 so->used--;
362 Py_DECREF(old_key);
363 return DISCARD_FOUND;
364}
365
366static int
367set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000368{
369 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000370 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000371 PyObject *old_key;
372
373 assert (PyAnySet_Check(so));
374 if (!PyString_CheckExact(key) ||
375 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
376 hash = PyObject_Hash(key);
377 if (hash == -1)
378 return -1;
379 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000380 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000381 if (entry == NULL)
382 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000383 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000384 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000385 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000387 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000388 so->used--;
389 Py_DECREF(old_key);
390 return DISCARD_FOUND;
391}
392
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000393static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394set_clear_internal(PySetObject *so)
395{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000396 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397 int table_is_malloced;
398 int fill;
399 setentry small_copy[PySet_MINSIZE];
400#ifdef Py_DEBUG
401 int i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000403
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000404 n = so->mask + 1;
405 i = 0;
406#endif
407
408 table = so->table;
409 assert(table != NULL);
410 table_is_malloced = table != so->smalltable;
411
412 /* This is delicate. During the process of clearing the set,
413 * decrefs can cause the set to mutate. To avoid fatal confusion
414 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000415 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000416 * clearing.
417 */
418 fill = so->fill;
419 if (table_is_malloced)
420 EMPTY_TO_MINSIZE(so);
421
422 else if (fill > 0) {
423 /* It's a small table with something that needs to be cleared.
424 * Afraid the only safe way is to copy the set entries into
425 * another small table first.
426 */
427 memcpy(small_copy, table, sizeof(small_copy));
428 table = small_copy;
429 EMPTY_TO_MINSIZE(so);
430 }
431 /* else it's a small table that's already empty */
432
433 /* Now we can finally clear things. If C had refcounts, we could
434 * assert that the refcount on table is 1 now, i.e. that this function
435 * has unique access to it, so decref side-effects can't alter it.
436 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000437 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000438#ifdef Py_DEBUG
439 assert(i < n);
440 ++i;
441#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000442 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000444 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445 }
446#ifdef Py_DEBUG
447 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000448 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449#endif
450 }
451
452 if (table_is_malloced)
453 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000454 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455}
456
457/*
458 * Iterate over a set table. Use like so:
459 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000460 * int pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000461 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000462 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000463 * while (set_next(yourset, &pos, &entry)) {
464 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000465 * }
466 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000467 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000468 * mutates the table.
469 */
470static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000471set_next(PySetObject *so, int *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472{
473 register int i, mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000474 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000475
476 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000477 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000478 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000479 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000481 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000483 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484 if (i > mask)
485 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000486 assert(table[i].key != NULL);
487 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000488 return 1;
489}
490
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000491static void
492set_dealloc(PySetObject *so)
493{
494 register setentry *entry;
495 int fill = so->fill;
496 PyObject_GC_UnTrack(so);
497 Py_TRASHCAN_SAFE_BEGIN(so)
498 if (so->weakreflist != NULL)
499 PyObject_ClearWeakRefs((PyObject *) so);
500
501 for (entry = so->table; fill > 0; entry++) {
502 if (entry->key) {
503 --fill;
504 Py_DECREF(entry->key);
505 }
506 }
507 if (so->table != so->smalltable)
508 PyMem_DEL(so->table);
509 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
510 free_sets[num_free_sets++] = so;
511 else
512 so->ob_type->tp_free(so);
513 Py_TRASHCAN_SAFE_END(so)
514}
515
516static int
517set_tp_print(PySetObject *so, FILE *fp, int flags)
518{
519 setentry *entry;
520 int pos=0;
521 char *emit = ""; /* No separator emitted on first pass */
522 char *separator = ", ";
523
524 fprintf(fp, "%s([", so->ob_type->tp_name);
525 while (set_next(so, &pos, &entry)) {
526 fputs(emit, fp);
527 emit = separator;
528 if (PyObject_Print(entry->key, fp, 0) != 0)
529 return -1;
530 }
531 fputs("])", fp);
532 return 0;
533}
534
535static PyObject *
536set_repr(PySetObject *so)
537{
538 PyObject *keys, *result, *listrepr;
539
540 keys = PySequence_List((PyObject *)so);
541 if (keys == NULL)
542 return NULL;
543 listrepr = PyObject_Repr(keys);
544 Py_DECREF(keys);
545 if (listrepr == NULL)
546 return NULL;
547
548 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
549 PyString_AS_STRING(listrepr));
550 Py_DECREF(listrepr);
551 return result;
552}
553
554static int
555set_len(PyObject *so)
556{
557 return ((PySetObject *)so)->used;
558}
559
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000560static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000561set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000562{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000563 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000564 register int i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000565 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000566
567 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000568 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000569
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000570 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000571 if (other == so || other->used == 0)
572 /* a.update(a) or a.update({}); nothing to do */
573 return 0;
574 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000575 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000576 * that there will be no (or few) overlapping keys.
577 */
578 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
579 if (set_table_resize(so, (so->used + other->used)*2) != 0)
580 return -1;
581 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000582 for (i = 0; i <= other->mask; i++) {
583 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000584 if (entry->key != NULL &&
585 entry->key != dummy) {
586 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000587 if (set_insert_key(so, entry->key, entry->hash) == -1) {
588 Py_DECREF(entry->key);
589 return -1;
590 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000591 }
592 }
593 return 0;
594}
595
596static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000597set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000598{
599 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000600 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000601
602 if (!PyString_CheckExact(key) ||
603 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
604 hash = PyObject_Hash(key);
605 if (hash == -1)
606 return -1;
607 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000608 entry = (so->lookup)(so, key, hash);
609 if (entry == NULL)
610 return -1;
611 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000612 return key != NULL && key != dummy;
613}
614
Raymond Hettingerc991db22005-08-11 07:58:45 +0000615static int
616set_contains_entry(PySetObject *so, setentry *entry)
617{
618 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000619 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000620
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000621 lu_entry = (so->lookup)(so, entry->key, entry->hash);
622 if (lu_entry == NULL)
623 return -1;
624 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000625 return key != NULL && key != dummy;
626}
627
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000628static PyObject *
629set_pop(PySetObject *so)
630{
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000631 register int i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000632 register setentry *entry;
633 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000634
635 assert (PyAnySet_Check(so));
636 if (so->used == 0) {
637 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
638 return NULL;
639 }
640
641 /* Set entry to "the first" unused or dummy set entry. We abuse
642 * the hash field of slot 0 to hold a search finger:
643 * If slot 0 has a value, use slot 0.
644 * Else slot 0 is being used to hold a search finger,
645 * and we use its hash value as the first index to look.
646 */
647 entry = &so->table[0];
648 if (entry->key == NULL || entry->key == dummy) {
649 i = (int)entry->hash;
650 /* The hash field may be a real hash value, or it may be a
651 * legit search finger, or it may be a once-legit search
652 * finger that's out of bounds now because it wrapped around
653 * or the table shrunk -- simply make sure it's in bounds now.
654 */
655 if (i > so->mask || i < 1)
656 i = 1; /* skip slot 0 */
657 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
658 i++;
659 if (i > so->mask)
660 i = 1;
661 }
662 }
663 key = entry->key;
664 Py_INCREF(dummy);
665 entry->key = dummy;
666 so->used--;
667 so->table[0].hash = i + 1; /* next place to start */
668 return key;
669}
670
671PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
672
673static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000674set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000675{
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000676 int pos = 0;
677 setentry *entry;
678
679 while (set_next(so, &pos, &entry))
680 Py_VISIT(entry->key);
681 return 0;
682}
683
684static long
685frozenset_hash(PyObject *self)
686{
687 PySetObject *so = (PySetObject *)self;
688 long h, hash = 1927868237L;
689 setentry *entry;
690 int pos = 0;
691
692 if (so->hash != -1)
693 return so->hash;
694
695 hash *= PySet_GET_SIZE(self) + 1;
696 while (set_next(so, &pos, &entry)) {
697 /* Work to increase the bit dispersion for closely spaced hash
698 values. The is important because some use cases have many
699 combinations of a small number of elements with nearby
700 hashes so that many distinct combinations collapse to only
701 a handful of distinct hash values. */
702 h = entry->hash;
703 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
704 }
705 hash = hash * 69069L + 907133923L;
706 if (hash == -1)
707 hash = 590923713L;
708 so->hash = hash;
709 return hash;
710}
711
712static long
713set_nohash(PyObject *self)
714{
715 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
716 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000717}
718
Raymond Hettingera9d99362005-08-05 00:01:15 +0000719/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000720
Raymond Hettingerd7946662005-08-01 21:39:29 +0000721static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000722
723typedef struct {
724 PyObject_HEAD
725 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
726 int si_used;
727 int si_pos;
728 long len;
729} setiterobject;
730
731static PyObject *
732set_iter(PySetObject *so)
733{
734 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
735 if (si == NULL)
736 return NULL;
737 Py_INCREF(so);
738 si->si_set = so;
739 si->si_used = so->used;
740 si->si_pos = 0;
741 si->len = so->used;
742 return (PyObject *)si;
743}
744
745static void
746setiter_dealloc(setiterobject *si)
747{
748 Py_XDECREF(si->si_set);
749 PyObject_Del(si);
750}
751
752static int
753setiter_len(setiterobject *si)
754{
755 if (si->si_set != NULL && si->si_used == si->si_set->used)
756 return si->len;
757 return 0;
758}
759
760static PySequenceMethods setiter_as_sequence = {
761 (inquiry)setiter_len, /* sq_length */
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000762 0, /* sq_concat */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000763};
764
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000765static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000766{
767 PyObject *key;
768 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000769 register setentry *entry;
770 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000771
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000772 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000773 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000774 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000775
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000776 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777 PyErr_SetString(PyExc_RuntimeError,
778 "Set changed size during iteration");
779 si->si_used = -1; /* Make this state sticky */
780 return NULL;
781 }
782
783 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000784 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000785 entry = so->table;
786 mask = so->mask;
787 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788 i++;
789 si->si_pos = i+1;
790 if (i > mask)
791 goto fail;
792 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000793 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794 Py_INCREF(key);
795 return key;
796
797fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000798 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799 si->si_set = NULL;
800 return NULL;
801}
802
Hye-Shik Change2956762005-08-01 05:26:41 +0000803static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804 PyObject_HEAD_INIT(&PyType_Type)
805 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000806 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807 sizeof(setiterobject), /* tp_basicsize */
808 0, /* tp_itemsize */
809 /* methods */
810 (destructor)setiter_dealloc, /* tp_dealloc */
811 0, /* tp_print */
812 0, /* tp_getattr */
813 0, /* tp_setattr */
814 0, /* tp_compare */
815 0, /* tp_repr */
816 0, /* tp_as_number */
817 &setiter_as_sequence, /* tp_as_sequence */
818 0, /* tp_as_mapping */
819 0, /* tp_hash */
820 0, /* tp_call */
821 0, /* tp_str */
822 PyObject_GenericGetAttr, /* tp_getattro */
823 0, /* tp_setattro */
824 0, /* tp_as_buffer */
825 Py_TPFLAGS_DEFAULT, /* tp_flags */
826 0, /* tp_doc */
827 0, /* tp_traverse */
828 0, /* tp_clear */
829 0, /* tp_richcompare */
830 0, /* tp_weaklistoffset */
831 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000832 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833};
834
Raymond Hettingerd7946662005-08-01 21:39:29 +0000835static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000836set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000837{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000838 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000839
Raymond Hettingerd7946662005-08-01 21:39:29 +0000840 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000841 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000842
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000843 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000844 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000845 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000846 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000847 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000848 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000849 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000850 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000851 }
852
Raymond Hettingera38123e2003-11-24 22:18:49 +0000853 it = PyObject_GetIter(other);
854 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000855 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000856
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000857 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000858 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000859 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000860 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000861 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000862 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000863 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000864 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000865 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000866 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000867 return -1;
868 return 0;
869}
870
871static PyObject *
872set_update(PySetObject *so, PyObject *other)
873{
874 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000875 return NULL;
876 Py_RETURN_NONE;
877}
878
879PyDoc_STRVAR(update_doc,
880"Update a set with the union of itself and another.");
881
882static PyObject *
883make_new_set(PyTypeObject *type, PyObject *iterable)
884{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000885 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000886
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000887 if (dummy == NULL) { /* Auto-initialize dummy */
888 dummy = PyString_FromString("<dummy key>");
889 if (dummy == NULL)
890 return NULL;
891 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000892
893 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000894 if (num_free_sets &&
895 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
896 so = free_sets[--num_free_sets];
897 assert (so != NULL && PyAnySet_CheckExact(so));
898 so->ob_type = type;
899 _Py_NewReference((PyObject *)so);
900 EMPTY_TO_MINSIZE(so);
901 PyObject_GC_Track(so);
902 } else {
903 so = (PySetObject *)type->tp_alloc(type, 0);
904 if (so == NULL)
905 return NULL;
906 /* tp_alloc has already zeroed the structure */
907 assert(so->table == NULL && so->fill == 0 && so->used == 0);
908 INIT_NONZERO_SET_SLOTS(so);
909 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000910
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000911 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000912 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000913
Raymond Hettingera38123e2003-11-24 22:18:49 +0000914 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000915 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000916 Py_DECREF(so);
917 return NULL;
918 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000919 }
920
Raymond Hettingera690a992003-11-16 16:17:49 +0000921 return (PyObject *)so;
922}
923
Raymond Hettingerd7946662005-08-01 21:39:29 +0000924/* The empty frozenset is a singleton */
925static PyObject *emptyfrozenset = NULL;
926
Raymond Hettingera690a992003-11-16 16:17:49 +0000927static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000928frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000929{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000930 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000931
Georg Brandl02c42872005-08-26 06:42:30 +0000932 if (!_PyArg_NoKeywords("frozenset()", kwds))
933 return NULL;
934
Raymond Hettingera690a992003-11-16 16:17:49 +0000935 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
936 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000937
938 if (type != &PyFrozenSet_Type)
939 return make_new_set(type, iterable);
940
941 if (iterable != NULL) {
942 /* frozenset(f) is idempotent */
943 if (PyFrozenSet_CheckExact(iterable)) {
944 Py_INCREF(iterable);
945 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000946 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000947 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000948 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949 return result;
950 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000951 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952 /* The empty frozenset is a singleton */
953 if (emptyfrozenset == NULL)
954 emptyfrozenset = make_new_set(type, NULL);
955 Py_XINCREF(emptyfrozenset);
956 return emptyfrozenset;
957}
958
959void
960PySet_Fini(void)
961{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000962 PySetObject *so;
963
964 while (num_free_sets) {
965 num_free_sets--;
966 so = free_sets[num_free_sets];
967 PyObject_GC_Del(so);
968 }
Raymond Hettingera9d99362005-08-05 00:01:15 +0000969 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000970 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000971}
972
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000973static PyObject *
974set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
975{
Georg Brandl02c42872005-08-26 06:42:30 +0000976 if (!_PyArg_NoKeywords("set()", kwds))
977 return NULL;
978
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000979 return make_new_set(type, NULL);
980}
981
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000982/* set_swap_bodies() switches the contents of any two sets by moving their
983 internal data pointers and, if needed, copying the internal smalltables.
984 Semantically equivalent to:
985
986 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
987
988 The function always succeeds and it leaves both objects in a stable state.
989 Useful for creating temporary frozensets from sets for membership testing
990 in __contains__(), discard(), and remove(). Also useful for operations
991 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000992 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000993*/
994
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000995static void
996set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000997{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000998 int t;
999 setentry *u;
1000 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1001 setentry tab[PySet_MINSIZE];
1002 long h;
1003
1004 t = a->fill; a->fill = b->fill; b->fill = t;
1005 t = a->used; a->used = b->used; b->used = t;
1006 t = a->mask; a->mask = b->mask; b->mask = t;
1007
1008 u = a->table;
1009 if (a->table == a->smalltable)
1010 u = b->smalltable;
1011 a->table = b->table;
1012 if (b->table == b->smalltable)
1013 a->table = a->smalltable;
1014 b->table = u;
1015
1016 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1017
1018 if (a->table == a->smalltable || b->table == b->smalltable) {
1019 memcpy(tab, a->smalltable, sizeof(tab));
1020 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1021 memcpy(b->smalltable, tab, sizeof(tab));
1022 }
1023
Raymond Hettingera580c472005-08-05 17:19:54 +00001024 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1025 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1026 h = a->hash; a->hash = b->hash; b->hash = h;
1027 } else {
1028 a->hash = -1;
1029 b->hash = -1;
1030 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001031}
1032
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001033static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001034set_copy(PySetObject *so)
1035{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001036 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001037}
1038
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001039static PyObject *
1040frozenset_copy(PySetObject *so)
1041{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001042 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001043 Py_INCREF(so);
1044 return (PyObject *)so;
1045 }
1046 return set_copy(so);
1047}
1048
Raymond Hettingera690a992003-11-16 16:17:49 +00001049PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1050
1051static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001052set_clear(PySetObject *so)
1053{
1054 set_clear_internal(so);
1055 Py_RETURN_NONE;
1056}
1057
1058PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1059
1060static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001061set_union(PySetObject *so, PyObject *other)
1062{
1063 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001064
1065 result = (PySetObject *)set_copy(so);
1066 if (result == NULL)
1067 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001068 if ((PyObject *)so == other)
1069 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001070 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001071 Py_DECREF(result);
1072 return NULL;
1073 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001074 return (PyObject *)result;
1075}
1076
1077PyDoc_STRVAR(union_doc,
1078 "Return the union of two sets as a new set.\n\
1079\n\
1080(i.e. all elements that are in either set.)");
1081
1082static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001083set_or(PySetObject *so, PyObject *other)
1084{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001085 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001086 Py_INCREF(Py_NotImplemented);
1087 return Py_NotImplemented;
1088 }
1089 return set_union(so, other);
1090}
1091
1092static PyObject *
1093set_ior(PySetObject *so, PyObject *other)
1094{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001095 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001096 Py_INCREF(Py_NotImplemented);
1097 return Py_NotImplemented;
1098 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001099 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001100 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001101 Py_INCREF(so);
1102 return (PyObject *)so;
1103}
1104
1105static PyObject *
1106set_intersection(PySetObject *so, PyObject *other)
1107{
1108 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001109 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001110
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001111 if ((PyObject *)so == other)
1112 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001113
Raymond Hettingera690a992003-11-16 16:17:49 +00001114 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1115 if (result == NULL)
1116 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001117
Raymond Hettingerc991db22005-08-11 07:58:45 +00001118 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001119 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001120 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001121
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001122 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001123 tmp = (PyObject *)so;
1124 so = (PySetObject *)other;
1125 other = tmp;
1126 }
1127
Raymond Hettingerc991db22005-08-11 07:58:45 +00001128 while (set_next((PySetObject *)other, &pos, &entry)) {
1129 if (set_contains_entry(so, entry)) {
1130 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001131 Py_DECREF(result);
1132 return NULL;
1133 }
1134 }
1135 }
1136 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001137 }
1138
Raymond Hettingera690a992003-11-16 16:17:49 +00001139 it = PyObject_GetIter(other);
1140 if (it == NULL) {
1141 Py_DECREF(result);
1142 return NULL;
1143 }
1144
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001145 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001146 if (set_contains_key(so, key)) {
1147 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001148 Py_DECREF(it);
1149 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001150 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001151 return NULL;
1152 }
1153 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001154 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001155 }
1156 Py_DECREF(it);
1157 if (PyErr_Occurred()) {
1158 Py_DECREF(result);
1159 return NULL;
1160 }
1161 return (PyObject *)result;
1162}
1163
1164PyDoc_STRVAR(intersection_doc,
1165"Return the intersection of two sets as a new set.\n\
1166\n\
1167(i.e. all elements that are in both sets.)");
1168
1169static PyObject *
1170set_intersection_update(PySetObject *so, PyObject *other)
1171{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001172 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001173
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001174 tmp = set_intersection(so, other);
1175 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001176 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001177 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001178 Py_DECREF(tmp);
1179 Py_RETURN_NONE;
1180}
1181
1182PyDoc_STRVAR(intersection_update_doc,
1183"Update a set with the intersection of itself and another.");
1184
1185static PyObject *
1186set_and(PySetObject *so, PyObject *other)
1187{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001188 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001189 Py_INCREF(Py_NotImplemented);
1190 return Py_NotImplemented;
1191 }
1192 return set_intersection(so, other);
1193}
1194
1195static PyObject *
1196set_iand(PySetObject *so, PyObject *other)
1197{
1198 PyObject *result;
1199
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001200 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001201 Py_INCREF(Py_NotImplemented);
1202 return Py_NotImplemented;
1203 }
1204 result = set_intersection_update(so, other);
1205 if (result == NULL)
1206 return NULL;
1207 Py_DECREF(result);
1208 Py_INCREF(so);
1209 return (PyObject *)so;
1210}
1211
Raymond Hettingerc991db22005-08-11 07:58:45 +00001212int
1213set_difference_update_internal(PySetObject *so, PyObject *other)
1214{
1215 if ((PyObject *)so == other)
1216 return set_clear_internal(so);
1217
1218 if (PyAnySet_Check(other)) {
1219 setentry *entry;
1220 int pos = 0;
1221
1222 while (set_next((PySetObject *)other, &pos, &entry))
1223 set_discard_entry(so, entry);
1224 } else {
1225 PyObject *key, *it;
1226 it = PyObject_GetIter(other);
1227 if (it == NULL)
1228 return -1;
1229
1230 while ((key = PyIter_Next(it)) != NULL) {
1231 if (set_discard_key(so, key) == -1) {
1232 Py_DECREF(it);
1233 Py_DECREF(key);
1234 return -1;
1235 }
1236 Py_DECREF(key);
1237 }
1238 Py_DECREF(it);
1239 if (PyErr_Occurred())
1240 return -1;
1241 }
1242 /* If more than 1/5 are dummies, then resize them away. */
1243 if ((so->fill - so->used) * 5 < so->mask)
1244 return 0;
1245 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1246}
1247
Raymond Hettingera690a992003-11-16 16:17:49 +00001248static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001249set_difference_update(PySetObject *so, PyObject *other)
1250{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001251 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001252 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001253 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001254}
1255
1256PyDoc_STRVAR(difference_update_doc,
1257"Remove all elements of another set from this set.");
1258
1259static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001260set_difference(PySetObject *so, PyObject *other)
1261{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001262 PyObject *result;
1263 setentry *entry;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001264 int pos = 0;
1265
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001266 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001267 result = set_copy(so);
1268 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001269 return NULL;
1270 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001271 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001272 Py_DECREF(result);
1273 return NULL;
1274 }
1275
1276 result = make_new_set(so->ob_type, NULL);
1277 if (result == NULL)
1278 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001279
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001280 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001281 while (set_next(so, &pos, &entry)) {
1282 setentry entrycopy;
1283 entrycopy.hash = entry->hash;
1284 entrycopy.key = entry->key;
1285 if (!PyDict_Contains(other, entry->key)) {
1286 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001287 return NULL;
1288 }
1289 }
1290 return result;
1291 }
1292
Raymond Hettingerc991db22005-08-11 07:58:45 +00001293 while (set_next(so, &pos, &entry)) {
1294 if (!set_contains_entry((PySetObject *)other, entry)) {
1295 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001296 return NULL;
1297 }
1298 }
1299 return result;
1300}
1301
1302PyDoc_STRVAR(difference_doc,
1303"Return the difference of two sets as a new set.\n\
1304\n\
1305(i.e. all elements that are in this set but not the other.)");
1306static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001307set_sub(PySetObject *so, PyObject *other)
1308{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001309 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001310 Py_INCREF(Py_NotImplemented);
1311 return Py_NotImplemented;
1312 }
1313 return set_difference(so, other);
1314}
1315
1316static PyObject *
1317set_isub(PySetObject *so, PyObject *other)
1318{
1319 PyObject *result;
1320
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001321 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001322 Py_INCREF(Py_NotImplemented);
1323 return Py_NotImplemented;
1324 }
1325 result = set_difference_update(so, other);
1326 if (result == NULL)
1327 return NULL;
1328 Py_DECREF(result);
1329 Py_INCREF(so);
1330 return (PyObject *)so;
1331}
1332
1333static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001334set_symmetric_difference_update(PySetObject *so, PyObject *other)
1335{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001336 PySetObject *otherset;
1337 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001338 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001339 setentry *entry;
1340
1341 if ((PyObject *)so == other)
1342 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001343
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001344 if (PyDict_Check(other)) {
1345 PyObject *value;
1346 int rv;
1347 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001348 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001349 if (rv == -1)
1350 return NULL;
1351 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001352 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001353 return NULL;
1354 }
1355 }
1356 Py_RETURN_NONE;
1357 }
1358
1359 if (PyAnySet_Check(other)) {
1360 Py_INCREF(other);
1361 otherset = (PySetObject *)other;
1362 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001363 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1364 if (otherset == NULL)
1365 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001366 }
1367
Raymond Hettingerc991db22005-08-11 07:58:45 +00001368 while (set_next(otherset, &pos, &entry)) {
1369 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001370 if (rv == -1) {
1371 Py_XDECREF(otherset);
1372 return NULL;
1373 }
1374 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001375 if (set_add_entry(so, entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001376 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001377 return NULL;
1378 }
1379 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001380 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001381 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001382 Py_RETURN_NONE;
1383}
1384
1385PyDoc_STRVAR(symmetric_difference_update_doc,
1386"Update a set with the symmetric difference of itself and another.");
1387
1388static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001389set_symmetric_difference(PySetObject *so, PyObject *other)
1390{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001391 PyObject *rv;
1392 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001393
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001394 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1395 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001396 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001397 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1398 if (rv == NULL)
1399 return NULL;
1400 Py_DECREF(rv);
1401 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001402}
1403
1404PyDoc_STRVAR(symmetric_difference_doc,
1405"Return the symmetric difference of two sets as a new set.\n\
1406\n\
1407(i.e. all elements that are in exactly one of the sets.)");
1408
1409static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001410set_xor(PySetObject *so, PyObject *other)
1411{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001412 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001413 Py_INCREF(Py_NotImplemented);
1414 return Py_NotImplemented;
1415 }
1416 return set_symmetric_difference(so, other);
1417}
1418
1419static PyObject *
1420set_ixor(PySetObject *so, PyObject *other)
1421{
1422 PyObject *result;
1423
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001424 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001425 Py_INCREF(Py_NotImplemented);
1426 return Py_NotImplemented;
1427 }
1428 result = set_symmetric_difference_update(so, other);
1429 if (result == NULL)
1430 return NULL;
1431 Py_DECREF(result);
1432 Py_INCREF(so);
1433 return (PyObject *)so;
1434}
1435
1436static PyObject *
1437set_issubset(PySetObject *so, PyObject *other)
1438{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001439 setentry *entry;
1440 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001441
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001442 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001443 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001444 tmp = make_new_set(&PySet_Type, other);
1445 if (tmp == NULL)
1446 return NULL;
1447 result = set_issubset(so, tmp);
1448 Py_DECREF(tmp);
1449 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001450 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001451 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001452 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001453
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001454 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001455 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001456 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001457 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001458 Py_RETURN_TRUE;
1459}
1460
1461PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1462
1463static PyObject *
1464set_issuperset(PySetObject *so, PyObject *other)
1465{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001466 PyObject *tmp, *result;
1467
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001468 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001469 tmp = make_new_set(&PySet_Type, other);
1470 if (tmp == NULL)
1471 return NULL;
1472 result = set_issuperset(so, tmp);
1473 Py_DECREF(tmp);
1474 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001475 }
1476 return set_issubset((PySetObject *)other, (PyObject *)so);
1477}
1478
1479PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1480
Raymond Hettingera690a992003-11-16 16:17:49 +00001481static PyObject *
1482set_richcompare(PySetObject *v, PyObject *w, int op)
1483{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001484 PyObject *r1, *r2;
1485
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001486 if(!PyAnySet_Check(w)) {
1487 if (op == Py_EQ)
1488 Py_RETURN_FALSE;
1489 if (op == Py_NE)
1490 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001491 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1492 return NULL;
1493 }
1494 switch (op) {
1495 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001496 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001497 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001498 if (v->hash != -1 &&
1499 ((PySetObject *)w)->hash != -1 &&
1500 v->hash != ((PySetObject *)w)->hash)
1501 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001502 return set_issubset(v, w);
1503 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001504 r1 = set_richcompare(v, w, Py_EQ);
1505 if (r1 == NULL)
1506 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001507 r2 = PyBool_FromLong(PyObject_Not(r1));
1508 Py_DECREF(r1);
1509 return r2;
1510 case Py_LE:
1511 return set_issubset(v, w);
1512 case Py_GE:
1513 return set_issuperset(v, w);
1514 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001515 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001516 Py_RETURN_FALSE;
1517 return set_issubset(v, w);
1518 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001519 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001520 Py_RETURN_FALSE;
1521 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001522 }
1523 Py_INCREF(Py_NotImplemented);
1524 return Py_NotImplemented;
1525}
1526
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001527static int
1528set_nocmp(PyObject *self)
1529{
1530 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1531 return -1;
1532}
1533
Raymond Hettingera690a992003-11-16 16:17:49 +00001534static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001535set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001536{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001537 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001538 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001539 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001540}
1541
1542PyDoc_STRVAR(add_doc,
1543"Add an element to a set.\n\
1544\n\
1545This has no effect if the element is already present.");
1546
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001547static int
1548set_contains(PySetObject *so, PyObject *key)
1549{
1550 PyObject *tmpkey;
1551 int rv;
1552
1553 rv = set_contains_key(so, key);
1554 if (rv == -1) {
1555 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1556 return -1;
1557 PyErr_Clear();
1558 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1559 if (tmpkey == NULL)
1560 return -1;
1561 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1562 rv = set_contains(so, tmpkey);
1563 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1564 Py_DECREF(tmpkey);
1565 }
1566 return rv;
1567}
1568
1569static PyObject *
1570set_direct_contains(PySetObject *so, PyObject *key)
1571{
1572 long result;
1573
1574 result = set_contains(so, key);
1575 if (result == -1)
1576 return NULL;
1577 return PyBool_FromLong(result);
1578}
1579
1580PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1581
Raymond Hettingera690a992003-11-16 16:17:49 +00001582static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001583set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001584{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001585 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001586 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001587
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001588 rv = set_discard_key(so, key);
1589 if (rv == -1) {
1590 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1591 return NULL;
1592 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001593 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1594 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001595 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001596 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001597 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001598 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001599 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001600 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001601 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001602 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001603 return NULL;
1604 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001605 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001606}
1607
1608PyDoc_STRVAR(remove_doc,
1609"Remove an element from a set; it must be a member.\n\
1610\n\
1611If the element is not a member, raise a KeyError.");
1612
1613static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001614set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001615{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001616 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001617 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001618
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001619 rv = set_discard_key(so, key);
1620 if (rv == -1) {
1621 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1622 return NULL;
1623 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001624 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1625 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001626 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001627 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001628 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001629 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001630 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001631 return result;
1632 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001633 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001634}
1635
1636PyDoc_STRVAR(discard_doc,
1637"Remove an element from a set if it is a member.\n\
1638\n\
1639If the element is not a member, do nothing.");
1640
1641static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001642set_reduce(PySetObject *so)
1643{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001644 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001645
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001646 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001647 if (keys == NULL)
1648 goto done;
1649 args = PyTuple_Pack(1, keys);
1650 if (args == NULL)
1651 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001652 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1653 if (dict == NULL) {
1654 PyErr_Clear();
1655 dict = Py_None;
1656 Py_INCREF(dict);
1657 }
1658 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001659done:
1660 Py_XDECREF(args);
1661 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001662 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001663 return result;
1664}
1665
1666PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1667
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001668static int
1669set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1670{
1671 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001672
1673 if (!PyAnySet_Check(self))
1674 return -1;
1675 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1676 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001677 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001678 self->hash = -1;
1679 if (iterable == NULL)
1680 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001681 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001682}
1683
Raymond Hettingera690a992003-11-16 16:17:49 +00001684static PySequenceMethods set_as_sequence = {
1685 (inquiry)set_len, /* sq_length */
1686 0, /* sq_concat */
1687 0, /* sq_repeat */
1688 0, /* sq_item */
1689 0, /* sq_slice */
1690 0, /* sq_ass_item */
1691 0, /* sq_ass_slice */
1692 (objobjproc)set_contains, /* sq_contains */
1693};
1694
1695/* set object ********************************************************/
1696
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001697#ifdef Py_DEBUG
1698static PyObject *test_c_api(PySetObject *so);
1699
1700PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1701All is well if assertions don't fail.");
1702#endif
1703
Raymond Hettingera690a992003-11-16 16:17:49 +00001704static PyMethodDef set_methods[] = {
1705 {"add", (PyCFunction)set_add, METH_O,
1706 add_doc},
1707 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1708 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001709 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001710 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001711 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1712 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001713 {"discard", (PyCFunction)set_discard, METH_O,
1714 discard_doc},
1715 {"difference", (PyCFunction)set_difference, METH_O,
1716 difference_doc},
1717 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1718 difference_update_doc},
1719 {"intersection",(PyCFunction)set_intersection, METH_O,
1720 intersection_doc},
1721 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1722 intersection_update_doc},
1723 {"issubset", (PyCFunction)set_issubset, METH_O,
1724 issubset_doc},
1725 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1726 issuperset_doc},
1727 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1728 pop_doc},
1729 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1730 reduce_doc},
1731 {"remove", (PyCFunction)set_remove, METH_O,
1732 remove_doc},
1733 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1734 symmetric_difference_doc},
1735 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1736 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001737#ifdef Py_DEBUG
1738 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1739 test_c_api_doc},
1740#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001741 {"union", (PyCFunction)set_union, METH_O,
1742 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001743 {"update", (PyCFunction)set_update, METH_O,
1744 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001745 {NULL, NULL} /* sentinel */
1746};
1747
1748static PyNumberMethods set_as_number = {
1749 0, /*nb_add*/
1750 (binaryfunc)set_sub, /*nb_subtract*/
1751 0, /*nb_multiply*/
1752 0, /*nb_divide*/
1753 0, /*nb_remainder*/
1754 0, /*nb_divmod*/
1755 0, /*nb_power*/
1756 0, /*nb_negative*/
1757 0, /*nb_positive*/
1758 0, /*nb_absolute*/
1759 0, /*nb_nonzero*/
1760 0, /*nb_invert*/
1761 0, /*nb_lshift*/
1762 0, /*nb_rshift*/
1763 (binaryfunc)set_and, /*nb_and*/
1764 (binaryfunc)set_xor, /*nb_xor*/
1765 (binaryfunc)set_or, /*nb_or*/
1766 0, /*nb_coerce*/
1767 0, /*nb_int*/
1768 0, /*nb_long*/
1769 0, /*nb_float*/
1770 0, /*nb_oct*/
1771 0, /*nb_hex*/
1772 0, /*nb_inplace_add*/
1773 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1774 0, /*nb_inplace_multiply*/
1775 0, /*nb_inplace_divide*/
1776 0, /*nb_inplace_remainder*/
1777 0, /*nb_inplace_power*/
1778 0, /*nb_inplace_lshift*/
1779 0, /*nb_inplace_rshift*/
1780 (binaryfunc)set_iand, /*nb_inplace_and*/
1781 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1782 (binaryfunc)set_ior, /*nb_inplace_or*/
1783};
1784
1785PyDoc_STRVAR(set_doc,
1786"set(iterable) --> set object\n\
1787\n\
1788Build an unordered collection.");
1789
1790PyTypeObject PySet_Type = {
1791 PyObject_HEAD_INIT(&PyType_Type)
1792 0, /* ob_size */
1793 "set", /* tp_name */
1794 sizeof(PySetObject), /* tp_basicsize */
1795 0, /* tp_itemsize */
1796 /* methods */
1797 (destructor)set_dealloc, /* tp_dealloc */
1798 (printfunc)set_tp_print, /* tp_print */
1799 0, /* tp_getattr */
1800 0, /* tp_setattr */
1801 (cmpfunc)set_nocmp, /* tp_compare */
1802 (reprfunc)set_repr, /* tp_repr */
1803 &set_as_number, /* tp_as_number */
1804 &set_as_sequence, /* tp_as_sequence */
1805 0, /* tp_as_mapping */
1806 set_nohash, /* tp_hash */
1807 0, /* tp_call */
1808 0, /* tp_str */
1809 PyObject_GenericGetAttr, /* tp_getattro */
1810 0, /* tp_setattro */
1811 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001812 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001813 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001814 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001815 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001816 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001817 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001818 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001819 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001820 0, /* tp_iternext */
1821 set_methods, /* tp_methods */
1822 0, /* tp_members */
1823 0, /* tp_getset */
1824 0, /* tp_base */
1825 0, /* tp_dict */
1826 0, /* tp_descr_get */
1827 0, /* tp_descr_set */
1828 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001829 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001830 PyType_GenericAlloc, /* tp_alloc */
1831 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001832 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001833};
1834
1835/* frozenset object ********************************************************/
1836
1837
1838static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001839 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001840 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001841 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001842 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001843 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001844 difference_doc},
1845 {"intersection",(PyCFunction)set_intersection, METH_O,
1846 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001847 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001848 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001849 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001850 issuperset_doc},
1851 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1852 reduce_doc},
1853 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1854 symmetric_difference_doc},
1855 {"union", (PyCFunction)set_union, METH_O,
1856 union_doc},
1857 {NULL, NULL} /* sentinel */
1858};
1859
1860static PyNumberMethods frozenset_as_number = {
1861 0, /*nb_add*/
1862 (binaryfunc)set_sub, /*nb_subtract*/
1863 0, /*nb_multiply*/
1864 0, /*nb_divide*/
1865 0, /*nb_remainder*/
1866 0, /*nb_divmod*/
1867 0, /*nb_power*/
1868 0, /*nb_negative*/
1869 0, /*nb_positive*/
1870 0, /*nb_absolute*/
1871 0, /*nb_nonzero*/
1872 0, /*nb_invert*/
1873 0, /*nb_lshift*/
1874 0, /*nb_rshift*/
1875 (binaryfunc)set_and, /*nb_and*/
1876 (binaryfunc)set_xor, /*nb_xor*/
1877 (binaryfunc)set_or, /*nb_or*/
1878};
1879
1880PyDoc_STRVAR(frozenset_doc,
1881"frozenset(iterable) --> frozenset object\n\
1882\n\
1883Build an immutable unordered collection.");
1884
1885PyTypeObject PyFrozenSet_Type = {
1886 PyObject_HEAD_INIT(&PyType_Type)
1887 0, /* ob_size */
1888 "frozenset", /* tp_name */
1889 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001890 0, /* tp_itemsize */
1891 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001892 (destructor)set_dealloc, /* tp_dealloc */
1893 (printfunc)set_tp_print, /* tp_print */
1894 0, /* tp_getattr */
1895 0, /* tp_setattr */
1896 (cmpfunc)set_nocmp, /* tp_compare */
1897 (reprfunc)set_repr, /* tp_repr */
1898 &frozenset_as_number, /* tp_as_number */
1899 &set_as_sequence, /* tp_as_sequence */
1900 0, /* tp_as_mapping */
1901 frozenset_hash, /* tp_hash */
1902 0, /* tp_call */
1903 0, /* tp_str */
1904 PyObject_GenericGetAttr, /* tp_getattro */
1905 0, /* tp_setattro */
1906 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001907 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001908 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001909 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001910 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001911 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001912 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001913 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001914 (getiterfunc)set_iter, /* tp_iter */
1915 0, /* tp_iternext */
1916 frozenset_methods, /* tp_methods */
1917 0, /* tp_members */
1918 0, /* tp_getset */
1919 0, /* tp_base */
1920 0, /* tp_dict */
1921 0, /* tp_descr_get */
1922 0, /* tp_descr_set */
1923 0, /* tp_dictoffset */
1924 0, /* tp_init */
1925 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001926 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001927 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001928};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001929
1930
1931/***** C API functions *************************************************/
1932
1933PyObject *
1934PySet_New(PyObject *iterable)
1935{
1936 return make_new_set(&PySet_Type, iterable);
1937}
1938
1939PyObject *
1940PyFrozenSet_New(PyObject *iterable)
1941{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001942 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001943
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001944 if (iterable == NULL)
1945 args = PyTuple_New(0);
1946 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001947 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001948 if (args == NULL)
1949 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001950 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001951 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001952 return result;
1953}
1954
1955int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001956PySet_Size(PyObject *anyset)
1957{
1958 if (!PyAnySet_Check(anyset)) {
1959 PyErr_BadInternalCall();
1960 return -1;
1961 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001962 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001963}
1964
1965int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001966PySet_Contains(PyObject *anyset, PyObject *key)
1967{
1968 if (!PyAnySet_Check(anyset)) {
1969 PyErr_BadInternalCall();
1970 return -1;
1971 }
1972 return set_contains_key((PySetObject *)anyset, key);
1973}
1974
1975int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001976PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001977{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001978 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001979 PyErr_BadInternalCall();
1980 return -1;
1981 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001982 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001983}
1984
1985int
1986PySet_Add(PyObject *set, PyObject *key)
1987{
1988 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1989 PyErr_BadInternalCall();
1990 return -1;
1991 }
1992 return set_add_key((PySetObject *)set, key);
1993}
1994
1995PyObject *
1996PySet_Pop(PyObject *set)
1997{
1998 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1999 PyErr_BadInternalCall();
2000 return NULL;
2001 }
2002 return set_pop((PySetObject *)set);
2003}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002004
2005
2006#ifdef Py_DEBUG
2007
2008/* Test code to be called with any three element set.
2009 Returns True and original set is restored. */
2010
2011#define assertRaises(call_return_value, exception) \
2012 do { \
2013 assert(call_return_value); \
2014 assert(PyErr_ExceptionMatches(exception)); \
2015 PyErr_Clear(); \
2016 } while(0)
2017
2018static PyObject *
2019test_c_api(PySetObject *so)
2020{
2021 PyObject *elem, *dup, *t, *f, *ob = (PyObject *)so;
2022
2023 /* Verify preconditions and exercise type/size checks */
2024 assert(PyAnySet_Check(ob));
2025 assert(PyAnySet_CheckExact(ob));
2026 assert(!PyFrozenSet_CheckExact(ob));
2027 assert(PySet_Size(ob) == 3);
2028 assert(PySet_GET_SIZE(ob) == 3);
2029
2030 /* Raise TypeError for non-iterable constructor arguments */
2031 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2032 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2033
2034 /* Raise TypeError for unhashable key */
2035 dup = PySet_New(ob);
2036 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2037 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2038 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2039
2040 /* Exercise successful pop, contains, add, and discard */
2041 elem = PySet_Pop(ob);
2042 assert(PySet_Contains(ob, elem) == 0);
2043 assert(PySet_GET_SIZE(ob) == 2);
2044 assert(PySet_Add(ob, elem) == 0);
2045 assert(PySet_Contains(ob, elem) == 1);
2046 assert(PySet_GET_SIZE(ob) == 3);
2047 assert(PySet_Discard(ob, elem) == 1);
2048 assert(PySet_GET_SIZE(ob) == 2);
2049 assert(PySet_Discard(ob, elem) == 0);
2050 assert(PySet_GET_SIZE(ob) == 2);
2051
2052 /* Raise SystemError when self argument is not a set or frozenset. */
2053 t = PyTuple_New(0);
2054 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2055 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2056 Py_DECREF(t);
2057
2058 /* Raise SystemError when self argument is not a set. */
2059 f = PyFrozenSet_New(dup);
2060 assert(PySet_Size(f) == 3);
2061 assert(PyFrozenSet_CheckExact(f));
2062 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2063 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2064 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2065 Py_DECREF(f);
2066
2067 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002068 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2069 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002070 assert(PySet_GET_SIZE(ob) == 0);
2071 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2072
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002073 /* Restore the set from the copy using the PyNumber API */
2074 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2075 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002076
2077 /* Verify constructors accept NULL arguments */
2078 f = PySet_New(NULL);
2079 assert(f != NULL);
2080 assert(PySet_GET_SIZE(f) == 0);
2081 Py_DECREF(f);
2082 f = PyFrozenSet_New(NULL);
2083 assert(f != NULL);
2084 assert(PyFrozenSet_CheckExact(f));
2085 assert(PySet_GET_SIZE(f) == 0);
2086 Py_DECREF(f);
2087
2088 Py_DECREF(elem);
2089 Py_DECREF(dup);
2090 Py_RETURN_TRUE;
2091}
2092
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002093#undef assertRaises
2094
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002095#endif