blob: 3958a6cac88683cc414fd2c91b4bdf2359b5dceb [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002/* set object implementation
Raymond Hettingera9d99362005-08-05 00:01:15 +00003 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*/
6
Raymond Hettingera690a992003-11-16 16:17:49 +00007#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +00008#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00009
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +000010/* Set a key error with the specified argument, wrapping it in a
11 * tuple automatically so that tuple keys are not unpacked as the
12 * exception arguments. */
13static void
14set_key_error(PyObject *arg)
15{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000016 PyObject *tup;
17 tup = PyTuple_Pack(1, arg);
18 if (!tup)
19 return; /* caller will expect error to be set anyway */
20 PyErr_SetObject(PyExc_KeyError, tup);
21 Py_DECREF(tup);
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +000022}
23
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000024/* This must be >= 1. */
25#define PERTURB_SHIFT 5
26
27/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000028static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000029
Armin Rigoe1709372006-04-12 17:06:05 +000030#ifdef Py_REF_DEBUG
31PyObject *
32_PySet_Dummy(void)
33{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000034 return dummy;
Armin Rigoe1709372006-04-12 17:06:05 +000035}
36#endif
37
Antoine Pitrouc83ea132010-05-09 14:46:46 +000038#define INIT_NONZERO_SET_SLOTS(so) do { \
39 (so)->table = (so)->smalltable; \
40 (so)->mask = PySet_MINSIZE - 1; \
41 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000042 } while(0)
43
Antoine Pitrouc83ea132010-05-09 14:46:46 +000044#define EMPTY_TO_MINSIZE(so) do { \
45 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
46 (so)->used = (so)->fill = 0; \
47 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000048 } while(0)
49
Raymond Hettingerbc841a12005-08-07 13:02:53 +000050/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes5b970ad2008-02-06 13:33:44 +000051#ifndef PySet_MAXFREELIST
52#define PySet_MAXFREELIST 80
53#endif
54static PySetObject *free_list[PySet_MAXFREELIST];
55static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000056
57/*
58The basic lookup function used by all operations.
59This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
60Open addressing is preferred over chaining since the link overhead for
61chaining would be substantial (100% with typical malloc overhead).
62
63The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000064probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000065
66All arithmetic on hash should ignore overflow.
67
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000068Unlike the dictionary implementation, the lookkey functions can return
69NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070*/
71
72static setentry *
73set_lookkey(PySetObject *so, PyObject *key, register long hash)
74{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000075 register Py_ssize_t i;
76 register size_t perturb;
77 register setentry *freeslot;
78 register size_t mask = so->mask;
79 setentry *table = so->table;
80 register setentry *entry;
81 register int cmp;
82 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000083
Antoine Pitrouc83ea132010-05-09 14:46:46 +000084 i = hash & mask;
85 entry = &table[i];
86 if (entry->key == NULL || entry->key == key)
87 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000088
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089 if (entry->key == dummy)
90 freeslot = entry;
91 else {
92 if (entry->hash == hash) {
93 startkey = entry->key;
94 Py_INCREF(startkey);
95 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
96 Py_DECREF(startkey);
97 if (cmp < 0)
98 return NULL;
99 if (table == so->table && entry->key == startkey) {
100 if (cmp > 0)
101 return entry;
102 }
103 else {
104 /* The compare did major nasty stuff to the
105 * set: start over.
106 */
107 return set_lookkey(so, key, hash);
108 }
109 }
110 freeslot = NULL;
111 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000112
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000113 /* In the loop, key == dummy is by far (factor of 100s) the
114 least likely outcome, so test for that last. */
115 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
116 i = (i << 2) + i + perturb + 1;
117 entry = &table[i & mask];
118 if (entry->key == NULL) {
119 if (freeslot != NULL)
120 entry = freeslot;
121 break;
122 }
123 if (entry->key == key)
124 break;
125 if (entry->hash == hash && entry->key != dummy) {
126 startkey = entry->key;
127 Py_INCREF(startkey);
128 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
129 Py_DECREF(startkey);
130 if (cmp < 0)
131 return NULL;
132 if (table == so->table && entry->key == startkey) {
133 if (cmp > 0)
134 break;
135 }
136 else {
137 /* The compare did major nasty stuff to the
138 * set: start over.
139 */
140 return set_lookkey(so, key, hash);
141 }
142 }
143 else if (entry->key == dummy && freeslot == NULL)
144 freeslot = entry;
145 }
146 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000147}
148
149/*
150 * Hacked up version of set_lookkey which can assume keys are always strings;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000151 * This means we can always use _PyString_Eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000152 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000153 */
154static setentry *
155set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
156{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000157 register Py_ssize_t i;
158 register size_t perturb;
159 register setentry *freeslot;
160 register size_t mask = so->mask;
161 setentry *table = so->table;
162 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000163
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000164 /* Make sure this function doesn't have to handle non-string keys,
165 including subclasses of str; e.g., one reason to subclass
166 strings is to override __eq__, and for speed we don't cater to
167 that here. */
168 if (!PyString_CheckExact(key)) {
169 so->lookup = set_lookkey;
170 return set_lookkey(so, key, hash);
171 }
172 i = hash & mask;
173 entry = &table[i];
174 if (entry->key == NULL || entry->key == key)
175 return entry;
176 if (entry->key == dummy)
177 freeslot = entry;
178 else {
179 if (entry->hash == hash && _PyString_Eq(entry->key, key))
180 return entry;
181 freeslot = NULL;
182 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000183
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000184 /* In the loop, key == dummy is by far (factor of 100s) the
185 least likely outcome, so test for that last. */
186 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
187 i = (i << 2) + i + perturb + 1;
188 entry = &table[i & mask];
189 if (entry->key == NULL)
190 return freeslot == NULL ? entry : freeslot;
191 if (entry->key == key
192 || (entry->hash == hash
193 && entry->key != dummy
194 && _PyString_Eq(entry->key, key)))
195 return entry;
196 if (entry->key == dummy && freeslot == NULL)
197 freeslot = entry;
198 }
199 assert(0); /* NOT REACHED */
200 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000201}
202
203/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000204Internal routine to insert a new key into the table.
Raymond Hettinger0c850862006-12-08 04:24:33 +0000205Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206Eats a reference to key.
207*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000208static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209set_insert_key(register PySetObject *so, PyObject *key, long hash)
210{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000211 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000212
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000213 assert(so->lookup != NULL);
214 entry = so->lookup(so, key, hash);
215 if (entry == NULL)
216 return -1;
217 if (entry->key == NULL) {
218 /* UNUSED */
219 so->fill++;
220 entry->key = key;
221 entry->hash = hash;
222 so->used++;
223 } else if (entry->key == dummy) {
224 /* DUMMY */
225 entry->key = key;
226 entry->hash = hash;
227 so->used++;
228 Py_DECREF(dummy);
229 } else {
230 /* ACTIVE */
231 Py_DECREF(key);
232 }
233 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234}
235
236/*
Raymond Hettinger0c850862006-12-08 04:24:33 +0000237Internal routine used by set_table_resize() to insert an item which is
238known to be absent from the set. This routine also assumes that
239the set contains no deleted entries. Besides the performance benefit,
240using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
241Note that no refcounts are changed by this routine; if needed, the caller
242is responsible for incref'ing `key`.
243*/
244static void
245set_insert_clean(register PySetObject *so, PyObject *key, long hash)
246{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 register size_t i;
248 register size_t perturb;
249 register size_t mask = (size_t)so->mask;
250 setentry *table = so->table;
251 register setentry *entry;
Raymond Hettinger0c850862006-12-08 04:24:33 +0000252
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000253 i = hash & mask;
254 entry = &table[i];
255 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
256 i = (i << 2) + i + perturb + 1;
257 entry = &table[i & mask];
258 }
259 so->fill++;
260 entry->key = key;
261 entry->hash = hash;
262 so->used++;
Raymond Hettinger0c850862006-12-08 04:24:33 +0000263}
264
265/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000267keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000268actually be smaller than the old one.
269*/
270static int
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000271set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000273 Py_ssize_t newsize;
274 setentry *oldtable, *newtable, *entry;
275 Py_ssize_t i;
276 int is_oldtable_malloced;
277 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000279 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000280
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000281 /* Find the smallest table size > minused. */
282 for (newsize = PySet_MINSIZE;
283 newsize <= minused && newsize > 0;
284 newsize <<= 1)
285 ;
286 if (newsize <= 0) {
287 PyErr_NoMemory();
288 return -1;
289 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000290
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000291 /* Get space for a new table. */
292 oldtable = so->table;
293 assert(oldtable != NULL);
294 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000295
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000296 if (newsize == PySet_MINSIZE) {
297 /* A large table is shrinking, or we can't get any smaller. */
298 newtable = so->smalltable;
299 if (newtable == oldtable) {
300 if (so->fill == so->used) {
301 /* No dummies, so no point doing anything. */
302 return 0;
303 }
304 /* We're not going to resize it, but rebuild the
305 table anyway to purge old dummy entries.
306 Subtle: This is *necessary* if fill==size,
307 as set_lookkey needs at least one virgin slot to
308 terminate failing searches. If fill < size, it's
309 merely desirable, as dummies slow searches. */
310 assert(so->fill > so->used);
311 memcpy(small_copy, oldtable, sizeof(small_copy));
312 oldtable = small_copy;
313 }
314 }
315 else {
316 newtable = PyMem_NEW(setentry, newsize);
317 if (newtable == NULL) {
318 PyErr_NoMemory();
319 return -1;
320 }
321 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000322
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000323 /* Make the set empty, using the new table. */
324 assert(newtable != oldtable);
325 so->table = newtable;
326 so->mask = newsize - 1;
327 memset(newtable, 0, sizeof(setentry) * newsize);
328 so->used = 0;
329 i = so->fill;
330 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000332 /* Copy the data over; this is refcount-neutral for active entries;
333 dummy entries aren't copied over, of course */
334 for (entry = oldtable; i > 0; entry++) {
335 if (entry->key == NULL) {
336 /* UNUSED */
337 ;
338 } else if (entry->key == dummy) {
339 /* DUMMY */
340 --i;
341 assert(entry->key == dummy);
342 Py_DECREF(entry->key);
343 } else {
344 /* ACTIVE */
345 --i;
346 set_insert_clean(so, entry->key, entry->hash);
347 }
348 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000349
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000350 if (is_oldtable_malloced)
351 PyMem_DEL(oldtable);
352 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000353}
354
Raymond Hettingerc991db22005-08-11 07:58:45 +0000355/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
356
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000357static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000358set_add_entry(register PySetObject *so, setentry *entry)
359{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000360 register Py_ssize_t n_used;
Éric Araujof079c9b2011-03-22 23:47:32 +0100361 PyObject *key = entry->key;
362 long hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000363
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000364 assert(so->fill <= so->mask); /* at least one empty slot */
365 n_used = so->used;
Éric Araujof079c9b2011-03-22 23:47:32 +0100366 Py_INCREF(key);
367 if (set_insert_key(so, key, hash) == -1) {
368 Py_DECREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000369 return -1;
370 }
371 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
372 return 0;
373 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000374}
375
376static int
377set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000378{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000379 register long hash;
380 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000381
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000382 if (!PyString_CheckExact(key) ||
383 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
384 hash = PyObject_Hash(key);
385 if (hash == -1)
386 return -1;
387 }
388 assert(so->fill <= so->mask); /* at least one empty slot */
389 n_used = so->used;
390 Py_INCREF(key);
391 if (set_insert_key(so, key, hash) == -1) {
392 Py_DECREF(key);
393 return -1;
394 }
395 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
396 return 0;
397 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000398}
399
400#define DISCARD_NOTFOUND 0
401#define DISCARD_FOUND 1
402
403static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000404set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000405{ register setentry *entry;
406 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000407
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000408 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
409 if (entry == NULL)
410 return -1;
411 if (entry->key == NULL || entry->key == dummy)
412 return DISCARD_NOTFOUND;
413 old_key = entry->key;
414 Py_INCREF(dummy);
415 entry->key = dummy;
416 so->used--;
417 Py_DECREF(old_key);
418 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000419}
420
421static int
422set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000423{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000424 register long hash;
425 register setentry *entry;
426 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000427
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000428 assert (PyAnySet_Check(so));
429 if (!PyString_CheckExact(key) ||
430 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
431 hash = PyObject_Hash(key);
432 if (hash == -1)
433 return -1;
434 }
435 entry = (so->lookup)(so, key, hash);
436 if (entry == NULL)
437 return -1;
438 if (entry->key == NULL || entry->key == dummy)
439 return DISCARD_NOTFOUND;
440 old_key = entry->key;
441 Py_INCREF(dummy);
442 entry->key = dummy;
443 so->used--;
444 Py_DECREF(old_key);
445 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000446}
447
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000448static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449set_clear_internal(PySetObject *so)
450{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000451 setentry *entry, *table;
452 int table_is_malloced;
453 Py_ssize_t fill;
454 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000456 Py_ssize_t i, n;
457 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000458
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000459 n = so->mask + 1;
460 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000461#endif
462
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000463 table = so->table;
464 assert(table != NULL);
465 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000466
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000467 /* This is delicate. During the process of clearing the set,
468 * decrefs can cause the set to mutate. To avoid fatal confusion
469 * (voice of experience), we have to make the set empty before
470 * clearing the slots, and never refer to anything via so->ref while
471 * clearing.
472 */
473 fill = so->fill;
474 if (table_is_malloced)
475 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000477 else if (fill > 0) {
478 /* It's a small table with something that needs to be cleared.
479 * Afraid the only safe way is to copy the set entries into
480 * another small table first.
481 */
482 memcpy(small_copy, table, sizeof(small_copy));
483 table = small_copy;
484 EMPTY_TO_MINSIZE(so);
485 }
486 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000488 /* Now we can finally clear things. If C had refcounts, we could
489 * assert that the refcount on table is 1 now, i.e. that this function
490 * has unique access to it, so decref side-effects can't alter it.
491 */
492 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000494 assert(i < n);
495 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000497 if (entry->key) {
498 --fill;
499 Py_DECREF(entry->key);
500 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000502 else
503 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000504#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000505 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000507 if (table_is_malloced)
508 PyMem_DEL(table);
509 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000510}
511
512/*
513 * Iterate over a set table. Use like so:
514 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000515 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000516 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000517 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000518 * while (set_next(yourset, &pos, &entry)) {
519 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000520 * }
521 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000522 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000523 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000524 */
525static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000526set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000527{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000528 Py_ssize_t i;
529 Py_ssize_t mask;
530 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000531
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000532 assert (PyAnySet_Check(so));
533 i = *pos_ptr;
534 assert(i >= 0);
535 table = so->table;
536 mask = so->mask;
537 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
538 i++;
539 *pos_ptr = i+1;
540 if (i > mask)
541 return 0;
542 assert(table[i].key != NULL);
543 *entry_ptr = &table[i];
544 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545}
546
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000547static void
548set_dealloc(PySetObject *so)
549{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000550 register setentry *entry;
551 Py_ssize_t fill = so->fill;
552 PyObject_GC_UnTrack(so);
553 Py_TRASHCAN_SAFE_BEGIN(so)
554 if (so->weakreflist != NULL)
555 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000556
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000557 for (entry = so->table; fill > 0; entry++) {
558 if (entry->key) {
559 --fill;
560 Py_DECREF(entry->key);
561 }
562 }
563 if (so->table != so->smalltable)
564 PyMem_DEL(so->table);
565 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
566 free_list[numfree++] = so;
567 else
568 Py_TYPE(so)->tp_free(so);
569 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000570}
571
572static int
573set_tp_print(PySetObject *so, FILE *fp, int flags)
574{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000575 setentry *entry;
576 Py_ssize_t pos=0;
577 char *emit = ""; /* No separator emitted on first pass */
578 char *separator = ", ";
579 int status = Py_ReprEnter((PyObject*)so);
Raymond Hettinger53999102006-12-30 04:01:17 +0000580
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000581 if (status != 0) {
582 if (status < 0)
583 return status;
584 Py_BEGIN_ALLOW_THREADS
585 fprintf(fp, "%s(...)", so->ob_type->tp_name);
586 Py_END_ALLOW_THREADS
587 return 0;
588 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000589
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000590 Py_BEGIN_ALLOW_THREADS
591 fprintf(fp, "%s([", so->ob_type->tp_name);
592 Py_END_ALLOW_THREADS
593 while (set_next(so, &pos, &entry)) {
594 Py_BEGIN_ALLOW_THREADS
595 fputs(emit, fp);
596 Py_END_ALLOW_THREADS
597 emit = separator;
598 if (PyObject_Print(entry->key, fp, 0) != 0) {
599 Py_ReprLeave((PyObject*)so);
600 return -1;
601 }
602 }
603 Py_BEGIN_ALLOW_THREADS
604 fputs("])", fp);
605 Py_END_ALLOW_THREADS
606 Py_ReprLeave((PyObject*)so);
607 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000608}
609
610static PyObject *
611set_repr(PySetObject *so)
612{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000613 PyObject *keys, *result=NULL, *listrepr;
614 int status = Py_ReprEnter((PyObject*)so);
Raymond Hettinger53999102006-12-30 04:01:17 +0000615
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000616 if (status != 0) {
617 if (status < 0)
618 return NULL;
619 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
620 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000621
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 keys = PySequence_List((PyObject *)so);
623 if (keys == NULL)
624 goto done;
625 listrepr = PyObject_Repr(keys);
626 Py_DECREF(keys);
627 if (listrepr == NULL)
628 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000629
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000630 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
631 PyString_AS_STRING(listrepr));
632 Py_DECREF(listrepr);
Raymond Hettinger53999102006-12-30 04:01:17 +0000633done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000634 Py_ReprLeave((PyObject*)so);
635 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000636}
637
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000639set_len(PyObject *so)
640{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000641 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000642}
643
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000644static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000645set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000646{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000647 PySetObject *other;
Éric Araujof079c9b2011-03-22 23:47:32 +0100648 PyObject *key;
649 long hash;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000650 register Py_ssize_t i;
651 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000652
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000653 assert (PyAnySet_Check(so));
654 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000655
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000656 other = (PySetObject*)otherset;
657 if (other == so || other->used == 0)
658 /* a.update(a) or a.update({}); nothing to do */
659 return 0;
660 /* Do one big resize at the start, rather than
661 * incrementally resizing as we insert new keys. Expect
662 * that there will be no (or few) overlapping keys.
663 */
664 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
665 if (set_table_resize(so, (so->used + other->used)*2) != 0)
666 return -1;
667 }
668 for (i = 0; i <= other->mask; i++) {
669 entry = &other->table[i];
Éric Araujof079c9b2011-03-22 23:47:32 +0100670 key = entry->key;
671 hash = entry->hash;
672 if (key != NULL &&
673 key != dummy) {
674 Py_INCREF(key);
675 if (set_insert_key(so, key, hash) == -1) {
676 Py_DECREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000677 return -1;
678 }
679 }
680 }
681 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000682}
683
684static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000685set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000686{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000687 long hash;
688 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000689
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000690 if (!PyString_CheckExact(key) ||
691 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
692 hash = PyObject_Hash(key);
693 if (hash == -1)
694 return -1;
695 }
696 entry = (so->lookup)(so, key, hash);
697 if (entry == NULL)
698 return -1;
699 key = entry->key;
700 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000701}
702
Raymond Hettingerc991db22005-08-11 07:58:45 +0000703static int
704set_contains_entry(PySetObject *so, setentry *entry)
705{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000706 PyObject *key;
707 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000708
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 lu_entry = (so->lookup)(so, entry->key, entry->hash);
710 if (lu_entry == NULL)
711 return -1;
712 key = lu_entry->key;
713 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000714}
715
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000716static PyObject *
717set_pop(PySetObject *so)
718{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 register Py_ssize_t i = 0;
720 register setentry *entry;
721 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000722
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000723 assert (PyAnySet_Check(so));
724 if (so->used == 0) {
725 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
726 return NULL;
727 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000728
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000729 /* Set entry to "the first" unused or dummy set entry. We abuse
730 * the hash field of slot 0 to hold a search finger:
731 * If slot 0 has a value, use slot 0.
732 * Else slot 0 is being used to hold a search finger,
733 * and we use its hash value as the first index to look.
734 */
735 entry = &so->table[0];
736 if (entry->key == NULL || entry->key == dummy) {
737 i = entry->hash;
738 /* The hash field may be a real hash value, or it may be a
739 * legit search finger, or it may be a once-legit search
740 * finger that's out of bounds now because it wrapped around
741 * or the table shrunk -- simply make sure it's in bounds now.
742 */
743 if (i > so->mask || i < 1)
744 i = 1; /* skip slot 0 */
745 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
746 i++;
747 if (i > so->mask)
748 i = 1;
749 }
750 }
751 key = entry->key;
752 Py_INCREF(dummy);
753 entry->key = dummy;
754 so->used--;
755 so->table[0].hash = i + 1; /* next place to start */
756 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000757}
758
Andrew M. Kuchlingd7b7dde2008-10-03 16:29:19 +0000759PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
760Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000761
762static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000763set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000764{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000765 Py_ssize_t pos = 0;
766 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000767
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000768 while (set_next(so, &pos, &entry))
769 Py_VISIT(entry->key);
770 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000771}
772
773static long
774frozenset_hash(PyObject *self)
775{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000776 PySetObject *so = (PySetObject *)self;
777 long h, hash = 1927868237L;
778 setentry *entry;
779 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000780
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000781 if (so->hash != -1)
782 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000783
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000784 hash *= PySet_GET_SIZE(self) + 1;
785 while (set_next(so, &pos, &entry)) {
786 /* Work to increase the bit dispersion for closely spaced hash
787 values. The is important because some use cases have many
788 combinations of a small number of elements with nearby
789 hashes so that many distinct combinations collapse to only
790 a handful of distinct hash values. */
791 h = entry->hash;
792 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
793 }
794 hash = hash * 69069L + 907133923L;
795 if (hash == -1)
796 hash = 590923713L;
797 so->hash = hash;
798 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000799}
800
Raymond Hettingera9d99362005-08-05 00:01:15 +0000801/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000803typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000804 PyObject_HEAD
805 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
806 Py_ssize_t si_used;
807 Py_ssize_t si_pos;
808 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809} setiterobject;
810
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811static void
812setiter_dealloc(setiterobject *si)
813{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000814 Py_XDECREF(si->si_set);
815 PyObject_GC_Del(si);
Antoine Pitrouaa687902009-01-01 14:11:22 +0000816}
817
818static int
819setiter_traverse(setiterobject *si, visitproc visit, void *arg)
820{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 Py_VISIT(si->si_set);
822 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000823}
824
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000825static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000826setiter_len(setiterobject *si)
827{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000828 Py_ssize_t len = 0;
829 if (si->si_set != NULL && si->si_used == si->si_set->used)
830 len = si->len;
831 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832}
833
Armin Rigof5b3e362006-02-11 21:32:43 +0000834PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000835
836static PyMethodDef setiter_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000837 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
838 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839};
840
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000841static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000842{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000843 PyObject *key;
844 register Py_ssize_t i, mask;
845 register setentry *entry;
846 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000848 if (so == NULL)
849 return NULL;
850 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000851
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000852 if (si->si_used != so->used) {
853 PyErr_SetString(PyExc_RuntimeError,
854 "Set changed size during iteration");
855 si->si_used = -1; /* Make this state sticky */
856 return NULL;
857 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000858
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000859 i = si->si_pos;
860 assert(i>=0);
861 entry = so->table;
862 mask = so->mask;
863 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
864 i++;
865 si->si_pos = i+1;
866 if (i > mask)
867 goto fail;
868 si->len--;
869 key = entry[i].key;
870 Py_INCREF(key);
871 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872
873fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000874 si->si_set = NULL;
Serhiy Storchaka14a7d632016-03-30 20:43:06 +0300875 Py_DECREF(so);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000876 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000877}
878
Hye-Shik Change2956762005-08-01 05:26:41 +0000879static PyTypeObject PySetIter_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000880 PyVarObject_HEAD_INIT(&PyType_Type, 0)
881 "setiterator", /* tp_name */
882 sizeof(setiterobject), /* tp_basicsize */
883 0, /* tp_itemsize */
884 /* methods */
885 (destructor)setiter_dealloc, /* tp_dealloc */
886 0, /* tp_print */
887 0, /* tp_getattr */
888 0, /* tp_setattr */
889 0, /* tp_compare */
890 0, /* tp_repr */
891 0, /* tp_as_number */
892 0, /* tp_as_sequence */
893 0, /* tp_as_mapping */
894 0, /* tp_hash */
895 0, /* tp_call */
896 0, /* tp_str */
897 PyObject_GenericGetAttr, /* tp_getattro */
898 0, /* tp_setattro */
899 0, /* tp_as_buffer */
900 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
901 0, /* tp_doc */
902 (traverseproc)setiter_traverse, /* tp_traverse */
903 0, /* tp_clear */
904 0, /* tp_richcompare */
905 0, /* tp_weaklistoffset */
906 PyObject_SelfIter, /* tp_iter */
907 (iternextfunc)setiter_iternext, /* tp_iternext */
908 setiter_methods, /* tp_methods */
909 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000910};
911
Martin v. Löwis72d20672006-04-11 09:04:12 +0000912static PyObject *
913set_iter(PySetObject *so)
914{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000915 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
916 if (si == NULL)
917 return NULL;
918 Py_INCREF(so);
919 si->si_set = so;
920 si->si_used = so->used;
921 si->si_pos = 0;
922 si->len = so->used;
923 _PyObject_GC_TRACK(si);
924 return (PyObject *)si;
Martin v. Löwis72d20672006-04-11 09:04:12 +0000925}
926
Raymond Hettingerd7946662005-08-01 21:39:29 +0000927static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000928set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000929{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000930 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000931
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000932 if (PyAnySet_Check(other))
933 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000934
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000935 if (PyDict_CheckExact(other)) {
936 PyObject *value;
937 Py_ssize_t pos = 0;
938 long hash;
939 Py_ssize_t dictsize = PyDict_Size(other);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000940
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000941 /* Do one big resize at the start, rather than
942 * incrementally resizing as we insert new keys. Expect
943 * that there will be no (or few) overlapping keys.
944 */
945 if (dictsize == -1)
946 return -1;
947 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
948 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
949 return -1;
950 }
951 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
952 setentry an_entry;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000953
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000954 an_entry.hash = hash;
955 an_entry.key = key;
956 if (set_add_entry(so, &an_entry) == -1)
957 return -1;
958 }
959 return 0;
960 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000961
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000962 it = PyObject_GetIter(other);
963 if (it == NULL)
964 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000965
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000966 while ((key = PyIter_Next(it)) != NULL) {
967 if (set_add_key(so, key) == -1) {
968 Py_DECREF(it);
969 Py_DECREF(key);
970 return -1;
971 }
972 Py_DECREF(key);
973 }
974 Py_DECREF(it);
975 if (PyErr_Occurred())
976 return -1;
977 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000978}
979
980static PyObject *
Raymond Hettingeree4bcad2008-06-09 08:33:37 +0000981set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000982{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000983 Py_ssize_t i;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +0000984
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000985 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
986 PyObject *other = PyTuple_GET_ITEM(args, i);
987 if (set_update_internal(so, other) == -1)
988 return NULL;
989 }
990 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000991}
992
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993PyDoc_STRVAR(update_doc,
Raymond Hettingeree4bcad2008-06-09 08:33:37 +0000994"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000995
996static PyObject *
997make_new_set(PyTypeObject *type, PyObject *iterable)
998{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000999 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001001 if (dummy == NULL) { /* Auto-initialize dummy */
1002 dummy = PyString_FromString("<dummy key>");
1003 if (dummy == NULL)
1004 return NULL;
1005 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001006
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001007 /* create PySetObject structure */
1008 if (numfree &&
1009 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1010 so = free_list[--numfree];
1011 assert (so != NULL && PyAnySet_CheckExact(so));
1012 Py_TYPE(so) = type;
1013 _Py_NewReference((PyObject *)so);
1014 EMPTY_TO_MINSIZE(so);
1015 PyObject_GC_Track(so);
1016 } else {
1017 so = (PySetObject *)type->tp_alloc(type, 0);
1018 if (so == NULL)
1019 return NULL;
1020 /* tp_alloc has already zeroed the structure */
1021 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1022 INIT_NONZERO_SET_SLOTS(so);
1023 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001024
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001025 so->lookup = set_lookkey_string;
1026 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001028 if (iterable != NULL) {
1029 if (set_update_internal(so, iterable) == -1) {
1030 Py_DECREF(so);
1031 return NULL;
1032 }
1033 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001034
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001035 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001036}
1037
Raymond Hettingerd7946662005-08-01 21:39:29 +00001038/* The empty frozenset is a singleton */
1039static PyObject *emptyfrozenset = NULL;
1040
Raymond Hettingera690a992003-11-16 16:17:49 +00001041static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001042frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001043{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001044 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001045
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001046 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1047 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001048
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1050 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001052 if (type != &PyFrozenSet_Type)
1053 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001054
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001055 if (iterable != NULL) {
1056 /* frozenset(f) is idempotent */
1057 if (PyFrozenSet_CheckExact(iterable)) {
1058 Py_INCREF(iterable);
1059 return iterable;
1060 }
1061 result = make_new_set(type, iterable);
1062 if (result == NULL || PySet_GET_SIZE(result))
1063 return result;
1064 Py_DECREF(result);
1065 }
1066 /* The empty frozenset is a singleton */
1067 if (emptyfrozenset == NULL)
1068 emptyfrozenset = make_new_set(type, NULL);
1069 Py_XINCREF(emptyfrozenset);
1070 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001071}
1072
1073void
1074PySet_Fini(void)
1075{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001076 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001077
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001078 while (numfree) {
1079 numfree--;
1080 so = free_list[numfree];
1081 PyObject_GC_Del(so);
1082 }
1083 Py_CLEAR(dummy);
1084 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001085}
1086
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001087static PyObject *
1088set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1089{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001090 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1091 return NULL;
1092
1093 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001094}
1095
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001096/* set_swap_bodies() switches the contents of any two sets by moving their
1097 internal data pointers and, if needed, copying the internal smalltables.
1098 Semantically equivalent to:
1099
1100 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1101
1102 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001103 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001104 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001105 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001106 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001107*/
1108
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001109static void
1110set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001111{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001112 Py_ssize_t t;
1113 setentry *u;
1114 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1115 setentry tab[PySet_MINSIZE];
1116 long h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001117
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001118 t = a->fill; a->fill = b->fill; b->fill = t;
1119 t = a->used; a->used = b->used; b->used = t;
1120 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001121
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001122 u = a->table;
1123 if (a->table == a->smalltable)
1124 u = b->smalltable;
1125 a->table = b->table;
1126 if (b->table == b->smalltable)
1127 a->table = a->smalltable;
1128 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001129
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001130 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001131
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001132 if (a->table == a->smalltable || b->table == b->smalltable) {
1133 memcpy(tab, a->smalltable, sizeof(tab));
1134 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1135 memcpy(b->smalltable, tab, sizeof(tab));
1136 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001137
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001138 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1139 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1140 h = a->hash; a->hash = b->hash; b->hash = h;
1141 } else {
1142 a->hash = -1;
1143 b->hash = -1;
1144 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001145}
1146
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001147static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001148set_copy(PySetObject *so)
1149{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001150 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001151}
1152
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001153static PyObject *
1154frozenset_copy(PySetObject *so)
1155{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001156 if (PyFrozenSet_CheckExact(so)) {
1157 Py_INCREF(so);
1158 return (PyObject *)so;
1159 }
1160 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001161}
1162
Raymond Hettingera690a992003-11-16 16:17:49 +00001163PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1164
1165static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001166set_clear(PySetObject *so)
1167{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001168 set_clear_internal(so);
1169 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001170}
1171
1172PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1173
1174static PyObject *
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001175set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001176{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001177 PySetObject *result;
1178 PyObject *other;
1179 Py_ssize_t i;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001180
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001181 result = (PySetObject *)set_copy(so);
1182 if (result == NULL)
1183 return NULL;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001184
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001185 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1186 other = PyTuple_GET_ITEM(args, i);
1187 if ((PyObject *)so == other)
1188 continue;
1189 if (set_update_internal(result, other) == -1) {
1190 Py_DECREF(result);
1191 return NULL;
1192 }
1193 }
1194 return (PyObject *)result;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001195}
1196
1197PyDoc_STRVAR(union_doc,
1198 "Return the union of sets as a new set.\n\
1199\n\
1200(i.e. all elements that are in either set.)");
1201
1202static PyObject *
1203set_or(PySetObject *so, PyObject *other)
1204{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001205 PySetObject *result;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001207 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1208 Py_INCREF(Py_NotImplemented);
1209 return Py_NotImplemented;
1210 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001211
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001212 result = (PySetObject *)set_copy(so);
1213 if (result == NULL)
1214 return NULL;
1215 if ((PyObject *)so == other)
1216 return (PyObject *)result;
1217 if (set_update_internal(result, other) == -1) {
1218 Py_DECREF(result);
1219 return NULL;
1220 }
1221 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001222}
1223
Raymond Hettingera690a992003-11-16 16:17:49 +00001224static PyObject *
1225set_ior(PySetObject *so, PyObject *other)
1226{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001227 if (!PyAnySet_Check(other)) {
1228 Py_INCREF(Py_NotImplemented);
1229 return Py_NotImplemented;
1230 }
1231 if (set_update_internal(so, other) == -1)
1232 return NULL;
1233 Py_INCREF(so);
1234 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001235}
1236
1237static PyObject *
1238set_intersection(PySetObject *so, PyObject *other)
1239{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001240 PySetObject *result;
1241 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001242
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001243 if ((PyObject *)so == other)
1244 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001245
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001246 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
1247 if (result == NULL)
1248 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001249
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001250 if (PyAnySet_Check(other)) {
1251 Py_ssize_t pos = 0;
1252 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001253
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001254 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1255 tmp = (PyObject *)so;
1256 so = (PySetObject *)other;
1257 other = tmp;
1258 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001259
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001260 while (set_next((PySetObject *)other, &pos, &entry)) {
1261 int rv = set_contains_entry(so, entry);
1262 if (rv == -1) {
1263 Py_DECREF(result);
1264 return NULL;
1265 }
1266 if (rv) {
1267 if (set_add_entry(result, entry) == -1) {
1268 Py_DECREF(result);
1269 return NULL;
1270 }
1271 }
1272 }
1273 return (PyObject *)result;
1274 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001275
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001276 it = PyObject_GetIter(other);
1277 if (it == NULL) {
1278 Py_DECREF(result);
1279 return NULL;
1280 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001281
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001282 while ((key = PyIter_Next(it)) != NULL) {
1283 int rv;
1284 setentry entry;
1285 long hash = PyObject_Hash(key);
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001286
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001287 if (hash == -1) {
1288 Py_DECREF(it);
1289 Py_DECREF(result);
1290 Py_DECREF(key);
1291 return NULL;
1292 }
1293 entry.hash = hash;
1294 entry.key = key;
1295 rv = set_contains_entry(so, &entry);
1296 if (rv == -1) {
1297 Py_DECREF(it);
1298 Py_DECREF(result);
1299 Py_DECREF(key);
1300 return NULL;
1301 }
1302 if (rv) {
1303 if (set_add_entry(result, &entry) == -1) {
1304 Py_DECREF(it);
1305 Py_DECREF(result);
1306 Py_DECREF(key);
1307 return NULL;
1308 }
1309 }
1310 Py_DECREF(key);
1311 }
1312 Py_DECREF(it);
1313 if (PyErr_Occurred()) {
1314 Py_DECREF(result);
1315 return NULL;
1316 }
1317 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001318}
1319
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001320static PyObject *
1321set_intersection_multi(PySetObject *so, PyObject *args)
1322{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001323 Py_ssize_t i;
1324 PyObject *result = (PyObject *)so;
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001325
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001326 if (PyTuple_GET_SIZE(args) == 0)
1327 return set_copy(so);
Raymond Hettinger610a93e2008-06-11 00:44:47 +00001328
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001329 Py_INCREF(so);
1330 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1331 PyObject *other = PyTuple_GET_ITEM(args, i);
1332 PyObject *newresult = set_intersection((PySetObject *)result, other);
1333 if (newresult == NULL) {
1334 Py_DECREF(result);
1335 return NULL;
1336 }
1337 Py_DECREF(result);
1338 result = newresult;
1339 }
1340 return result;
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001341}
1342
Raymond Hettingera690a992003-11-16 16:17:49 +00001343PyDoc_STRVAR(intersection_doc,
Raymond Hettinger79628d32009-11-18 20:28:22 +00001344"Return the intersection of two or more sets as a new set.\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00001345\n\
Raymond Hettinger79628d32009-11-18 20:28:22 +00001346(i.e. elements that are common to all of the sets.)");
Raymond Hettingera690a992003-11-16 16:17:49 +00001347
1348static PyObject *
1349set_intersection_update(PySetObject *so, PyObject *other)
1350{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001351 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001352
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001353 tmp = set_intersection(so, other);
1354 if (tmp == NULL)
1355 return NULL;
1356 set_swap_bodies(so, (PySetObject *)tmp);
1357 Py_DECREF(tmp);
1358 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001359}
1360
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001361static PyObject *
1362set_intersection_update_multi(PySetObject *so, PyObject *args)
1363{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001364 PyObject *tmp;
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001365
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001366 tmp = set_intersection_multi(so, args);
1367 if (tmp == NULL)
1368 return NULL;
1369 set_swap_bodies(so, (PySetObject *)tmp);
1370 Py_DECREF(tmp);
1371 Py_RETURN_NONE;
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001372}
1373
Raymond Hettingera690a992003-11-16 16:17:49 +00001374PyDoc_STRVAR(intersection_update_doc,
1375"Update a set with the intersection of itself and another.");
1376
1377static PyObject *
1378set_and(PySetObject *so, PyObject *other)
1379{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001380 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1381 Py_INCREF(Py_NotImplemented);
1382 return Py_NotImplemented;
1383 }
1384 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001385}
1386
1387static PyObject *
1388set_iand(PySetObject *so, PyObject *other)
1389{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001390 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001391
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001392 if (!PyAnySet_Check(other)) {
1393 Py_INCREF(Py_NotImplemented);
1394 return Py_NotImplemented;
1395 }
1396 result = set_intersection_update(so, other);
1397 if (result == NULL)
1398 return NULL;
1399 Py_DECREF(result);
1400 Py_INCREF(so);
1401 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001402}
1403
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001404static PyObject *
1405set_isdisjoint(PySetObject *so, PyObject *other)
1406{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001407 PyObject *key, *it, *tmp;
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001408
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001409 if ((PyObject *)so == other) {
1410 if (PySet_GET_SIZE(so) == 0)
1411 Py_RETURN_TRUE;
1412 else
1413 Py_RETURN_FALSE;
1414 }
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001415
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001416 if (PyAnySet_CheckExact(other)) {
1417 Py_ssize_t pos = 0;
1418 setentry *entry;
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001419
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001420 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1421 tmp = (PyObject *)so;
1422 so = (PySetObject *)other;
1423 other = tmp;
1424 }
1425 while (set_next((PySetObject *)other, &pos, &entry)) {
1426 int rv = set_contains_entry(so, entry);
1427 if (rv == -1)
1428 return NULL;
1429 if (rv)
1430 Py_RETURN_FALSE;
1431 }
1432 Py_RETURN_TRUE;
1433 }
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001434
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001435 it = PyObject_GetIter(other);
1436 if (it == NULL)
1437 return NULL;
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001438
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001439 while ((key = PyIter_Next(it)) != NULL) {
1440 int rv;
1441 setentry entry;
1442 long hash = PyObject_Hash(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001443
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001444 if (hash == -1) {
1445 Py_DECREF(key);
1446 Py_DECREF(it);
1447 return NULL;
1448 }
1449 entry.hash = hash;
1450 entry.key = key;
1451 rv = set_contains_entry(so, &entry);
1452 Py_DECREF(key);
1453 if (rv == -1) {
1454 Py_DECREF(it);
1455 return NULL;
1456 }
1457 if (rv) {
1458 Py_DECREF(it);
1459 Py_RETURN_FALSE;
1460 }
1461 }
1462 Py_DECREF(it);
1463 if (PyErr_Occurred())
1464 return NULL;
1465 Py_RETURN_TRUE;
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001466}
1467
1468PyDoc_STRVAR(isdisjoint_doc,
1469"Return True if two sets have a null intersection.");
1470
Neal Norwitz6576bd82005-11-13 18:41:28 +00001471static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001472set_difference_update_internal(PySetObject *so, PyObject *other)
1473{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001474 if ((PyObject *)so == other)
1475 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001476
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001477 if (PyAnySet_Check(other)) {
1478 setentry *entry;
1479 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001480
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001481 while (set_next((PySetObject *)other, &pos, &entry))
1482 if (set_discard_entry(so, entry) == -1)
1483 return -1;
1484 } else {
1485 PyObject *key, *it;
1486 it = PyObject_GetIter(other);
1487 if (it == NULL)
1488 return -1;
1489
1490 while ((key = PyIter_Next(it)) != NULL) {
1491 if (set_discard_key(so, key) == -1) {
1492 Py_DECREF(it);
1493 Py_DECREF(key);
1494 return -1;
1495 }
1496 Py_DECREF(key);
1497 }
1498 Py_DECREF(it);
1499 if (PyErr_Occurred())
1500 return -1;
1501 }
1502 /* If more than 1/5 are dummies, then resize them away. */
1503 if ((so->fill - so->used) * 5 < so->mask)
1504 return 0;
1505 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001506}
1507
Raymond Hettingera690a992003-11-16 16:17:49 +00001508static PyObject *
Raymond Hettinger4267be62008-06-11 10:30:54 +00001509set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001510{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001511 Py_ssize_t i;
Raymond Hettinger4267be62008-06-11 10:30:54 +00001512
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001513 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1514 PyObject *other = PyTuple_GET_ITEM(args, i);
1515 if (set_difference_update_internal(so, other) == -1)
1516 return NULL;
1517 }
1518 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001519}
1520
1521PyDoc_STRVAR(difference_update_doc,
1522"Remove all elements of another set from this set.");
1523
1524static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001525set_difference(PySetObject *so, PyObject *other)
1526{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001527 PyObject *result;
1528 setentry *entry;
1529 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001530
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001531 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
1532 result = set_copy(so);
1533 if (result == NULL)
1534 return NULL;
1535 if (set_difference_update_internal((PySetObject *)result, other) != -1)
1536 return result;
1537 Py_DECREF(result);
1538 return NULL;
1539 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001540
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001541 result = make_new_set(Py_TYPE(so), NULL);
1542 if (result == NULL)
1543 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001544
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001545 if (PyDict_CheckExact(other)) {
1546 while (set_next(so, &pos, &entry)) {
1547 setentry entrycopy;
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001548 int rv;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001549 entrycopy.hash = entry->hash;
1550 entrycopy.key = entry->key;
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001551 rv = _PyDict_Contains(other, entry->key, entry->hash);
1552 if (rv < 0) {
1553 Py_DECREF(result);
1554 return NULL;
1555 }
1556 if (!rv) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001557 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1558 Py_DECREF(result);
1559 return NULL;
1560 }
1561 }
1562 }
1563 return result;
1564 }
1565
1566 while (set_next(so, &pos, &entry)) {
1567 int rv = set_contains_entry((PySetObject *)other, entry);
1568 if (rv == -1) {
1569 Py_DECREF(result);
1570 return NULL;
1571 }
1572 if (!rv) {
1573 if (set_add_entry((PySetObject *)result, entry) == -1) {
1574 Py_DECREF(result);
1575 return NULL;
1576 }
1577 }
1578 }
1579 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001580}
1581
Raymond Hettinger4267be62008-06-11 10:30:54 +00001582static PyObject *
1583set_difference_multi(PySetObject *so, PyObject *args)
1584{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001585 Py_ssize_t i;
1586 PyObject *result, *other;
Raymond Hettinger4267be62008-06-11 10:30:54 +00001587
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001588 if (PyTuple_GET_SIZE(args) == 0)
1589 return set_copy(so);
Raymond Hettinger4267be62008-06-11 10:30:54 +00001590
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001591 other = PyTuple_GET_ITEM(args, 0);
1592 result = set_difference(so, other);
1593 if (result == NULL)
1594 return NULL;
Raymond Hettinger4267be62008-06-11 10:30:54 +00001595
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001596 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1597 other = PyTuple_GET_ITEM(args, i);
1598 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1599 Py_DECREF(result);
1600 return NULL;
1601 }
1602 }
1603 return result;
Raymond Hettinger4267be62008-06-11 10:30:54 +00001604}
1605
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001606PyDoc_STRVAR(difference_doc,
Raymond Hettinger4267be62008-06-11 10:30:54 +00001607"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001608\n\
Raymond Hettinger4267be62008-06-11 10:30:54 +00001609(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001610static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001611set_sub(PySetObject *so, PyObject *other)
1612{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001613 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1614 Py_INCREF(Py_NotImplemented);
1615 return Py_NotImplemented;
1616 }
1617 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001618}
1619
1620static PyObject *
1621set_isub(PySetObject *so, PyObject *other)
1622{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001623 if (!PyAnySet_Check(other)) {
1624 Py_INCREF(Py_NotImplemented);
1625 return Py_NotImplemented;
1626 }
1627 if (set_difference_update_internal(so, other) == -1)
1628 return NULL;
1629 Py_INCREF(so);
1630 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001631}
1632
1633static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001634set_symmetric_difference_update(PySetObject *so, PyObject *other)
1635{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001636 PySetObject *otherset;
1637 PyObject *key;
1638 Py_ssize_t pos = 0;
1639 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001640
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001641 if ((PyObject *)so == other)
1642 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001643
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001644 if (PyDict_CheckExact(other)) {
1645 PyObject *value;
1646 int rv;
1647 long hash;
1648 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1649 setentry an_entry;
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001650
Éric Araujof079c9b2011-03-22 23:47:32 +01001651 Py_INCREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001652 an_entry.hash = hash;
1653 an_entry.key = key;
Éric Araujof079c9b2011-03-22 23:47:32 +01001654
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001655 rv = set_discard_entry(so, &an_entry);
Éric Araujof079c9b2011-03-22 23:47:32 +01001656 if (rv == -1) {
1657 Py_DECREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001658 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001659 }
Éric Araujof079c9b2011-03-22 23:47:32 +01001660 if (rv == DISCARD_NOTFOUND) {
1661 if (set_add_entry(so, &an_entry) == -1) {
1662 Py_DECREF(key);
1663 return NULL;
1664 }
1665 }
1666 Py_DECREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001667 }
1668 Py_RETURN_NONE;
1669 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001670
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001671 if (PyAnySet_Check(other)) {
1672 Py_INCREF(other);
1673 otherset = (PySetObject *)other;
1674 } else {
1675 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
1676 if (otherset == NULL)
1677 return NULL;
1678 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001679
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001680 while (set_next(otherset, &pos, &entry)) {
1681 int rv = set_discard_entry(so, entry);
1682 if (rv == -1) {
1683 Py_DECREF(otherset);
1684 return NULL;
1685 }
1686 if (rv == DISCARD_NOTFOUND) {
1687 if (set_add_entry(so, entry) == -1) {
1688 Py_DECREF(otherset);
1689 return NULL;
1690 }
1691 }
1692 }
1693 Py_DECREF(otherset);
1694 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001695}
1696
1697PyDoc_STRVAR(symmetric_difference_update_doc,
1698"Update a set with the symmetric difference of itself and another.");
1699
1700static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001701set_symmetric_difference(PySetObject *so, PyObject *other)
1702{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001703 PyObject *rv;
1704 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001705
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001706 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
1707 if (otherset == NULL)
1708 return NULL;
1709 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1710 if (rv == NULL)
1711 return NULL;
1712 Py_DECREF(rv);
1713 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001714}
1715
1716PyDoc_STRVAR(symmetric_difference_doc,
1717"Return the symmetric difference of two sets as a new set.\n\
1718\n\
1719(i.e. all elements that are in exactly one of the sets.)");
1720
1721static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001722set_xor(PySetObject *so, PyObject *other)
1723{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001724 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1725 Py_INCREF(Py_NotImplemented);
1726 return Py_NotImplemented;
1727 }
1728 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001729}
1730
1731static PyObject *
1732set_ixor(PySetObject *so, PyObject *other)
1733{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001734 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001735
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001736 if (!PyAnySet_Check(other)) {
1737 Py_INCREF(Py_NotImplemented);
1738 return Py_NotImplemented;
1739 }
1740 result = set_symmetric_difference_update(so, other);
1741 if (result == NULL)
1742 return NULL;
1743 Py_DECREF(result);
1744 Py_INCREF(so);
1745 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001746}
1747
1748static PyObject *
1749set_issubset(PySetObject *so, PyObject *other)
1750{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001751 setentry *entry;
1752 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001753
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001754 if (!PyAnySet_Check(other)) {
1755 PyObject *tmp, *result;
1756 tmp = make_new_set(&PySet_Type, other);
1757 if (tmp == NULL)
1758 return NULL;
1759 result = set_issubset(so, tmp);
1760 Py_DECREF(tmp);
1761 return result;
1762 }
1763 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1764 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001765
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001766 while (set_next(so, &pos, &entry)) {
1767 int rv = set_contains_entry((PySetObject *)other, entry);
1768 if (rv == -1)
1769 return NULL;
1770 if (!rv)
1771 Py_RETURN_FALSE;
1772 }
1773 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001774}
1775
1776PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1777
1778static PyObject *
1779set_issuperset(PySetObject *so, PyObject *other)
1780{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001781 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001782
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001783 if (!PyAnySet_Check(other)) {
1784 tmp = make_new_set(&PySet_Type, other);
1785 if (tmp == NULL)
1786 return NULL;
1787 result = set_issuperset(so, tmp);
1788 Py_DECREF(tmp);
1789 return result;
1790 }
1791 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001792}
1793
1794PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1795
Raymond Hettingera690a992003-11-16 16:17:49 +00001796static PyObject *
1797set_richcompare(PySetObject *v, PyObject *w, int op)
1798{
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001799 PyObject *r1;
1800 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001801
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001802 if(!PyAnySet_Check(w)) {
Raymond Hettingerf643b9a2014-05-25 22:13:41 -07001803 Py_INCREF(Py_NotImplemented);
1804 return Py_NotImplemented;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001805 }
1806 switch (op) {
1807 case Py_EQ:
1808 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1809 Py_RETURN_FALSE;
1810 if (v->hash != -1 &&
1811 ((PySetObject *)w)->hash != -1 &&
1812 v->hash != ((PySetObject *)w)->hash)
1813 Py_RETURN_FALSE;
1814 return set_issubset(v, w);
1815 case Py_NE:
1816 r1 = set_richcompare(v, w, Py_EQ);
1817 if (r1 == NULL)
1818 return NULL;
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001819 r2 = PyObject_IsTrue(r1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001820 Py_DECREF(r1);
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001821 if (r2 < 0)
1822 return NULL;
1823 return PyBool_FromLong(!r2);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001824 case Py_LE:
1825 return set_issubset(v, w);
1826 case Py_GE:
1827 return set_issuperset(v, w);
1828 case Py_LT:
1829 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1830 Py_RETURN_FALSE;
1831 return set_issubset(v, w);
1832 case Py_GT:
1833 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1834 Py_RETURN_FALSE;
1835 return set_issuperset(v, w);
1836 }
1837 Py_INCREF(Py_NotImplemented);
1838 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001839}
1840
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001841static int
Georg Brandl347b3002006-03-30 11:57:00 +00001842set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001843{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001844 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1845 return -1;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001846}
1847
Raymond Hettingera690a992003-11-16 16:17:49 +00001848static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001849set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001850{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001851 if (set_add_key(so, key) == -1)
1852 return NULL;
1853 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001854}
1855
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001856PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001857"Add an element to a set.\n\
1858\n\
1859This has no effect if the element is already present.");
1860
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001861static int
1862set_contains(PySetObject *so, PyObject *key)
1863{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001864 PyObject *tmpkey;
1865 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001866
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001867 rv = set_contains_key(so, key);
1868 if (rv == -1) {
1869 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1870 return -1;
1871 PyErr_Clear();
Raymond Hettinger3ad323e2010-08-06 10:18:56 +00001872 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001873 if (tmpkey == NULL)
1874 return -1;
Petri Lehtinen5f4d8702011-10-30 13:55:02 +02001875 rv = set_contains_key(so, tmpkey);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001876 Py_DECREF(tmpkey);
1877 }
1878 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001879}
1880
1881static PyObject *
1882set_direct_contains(PySetObject *so, PyObject *key)
1883{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001884 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001885
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001886 result = set_contains(so, key);
1887 if (result == -1)
1888 return NULL;
1889 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001890}
1891
1892PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1893
Raymond Hettingera690a992003-11-16 16:17:49 +00001894static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001895set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001896{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001897 PyObject *tmpkey;
1898 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001899
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001900 rv = set_discard_key(so, key);
1901 if (rv == -1) {
1902 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1903 return NULL;
1904 PyErr_Clear();
Raymond Hettinger3ad323e2010-08-06 10:18:56 +00001905 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001906 if (tmpkey == NULL)
1907 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001908 rv = set_discard_key(so, tmpkey);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001909 Py_DECREF(tmpkey);
1910 if (rv == -1)
1911 return NULL;
1912 }
Amaury Forgeot d'Arcd78b9dc2008-10-07 20:32:10 +00001913
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001914 if (rv == DISCARD_NOTFOUND) {
1915 set_key_error(key);
1916 return NULL;
1917 }
1918 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001919}
1920
1921PyDoc_STRVAR(remove_doc,
1922"Remove an element from a set; it must be a member.\n\
1923\n\
1924If the element is not a member, raise a KeyError.");
1925
1926static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001927set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001928{
Benjamin Petersone3b5eda2011-10-30 14:24:44 -04001929 PyObject *tmpkey;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001930 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001931
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001932 rv = set_discard_key(so, key);
1933 if (rv == -1) {
1934 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1935 return NULL;
1936 PyErr_Clear();
Raymond Hettinger3ad323e2010-08-06 10:18:56 +00001937 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001938 if (tmpkey == NULL)
1939 return NULL;
Petri Lehtinena39de112011-10-30 14:31:27 +02001940 rv = set_discard_key(so, tmpkey);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001941 Py_DECREF(tmpkey);
Petri Lehtinena39de112011-10-30 14:31:27 +02001942 if (rv == -1)
1943 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001944 }
1945 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001946}
1947
1948PyDoc_STRVAR(discard_doc,
1949"Remove an element from a set if it is a member.\n\
1950\n\
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001951If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001952
1953static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001954set_reduce(PySetObject *so)
1955{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001956 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001957
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001958 keys = PySequence_List((PyObject *)so);
1959 if (keys == NULL)
1960 goto done;
1961 args = PyTuple_Pack(1, keys);
1962 if (args == NULL)
1963 goto done;
1964 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1965 if (dict == NULL) {
1966 PyErr_Clear();
1967 dict = Py_None;
1968 Py_INCREF(dict);
1969 }
1970 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001971done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001972 Py_XDECREF(args);
1973 Py_XDECREF(keys);
1974 Py_XDECREF(dict);
1975 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001976}
1977
1978PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1979
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00001980static PyObject *
1981set_sizeof(PySetObject *so)
1982{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001983 Py_ssize_t res;
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00001984
Serhiy Storchakac06a6d02015-12-19 20:07:48 +02001985 res = _PyObject_SIZE(Py_TYPE(so));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001986 if (so->table != so->smalltable)
1987 res = res + (so->mask + 1) * sizeof(setentry);
1988 return PyInt_FromSsize_t(res);
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00001989}
1990
1991PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001992static int
1993set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1994{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001995 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001996
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001997 if (!PyAnySet_Check(self))
1998 return -1;
1999 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2000 return -1;
2001 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2002 return -1;
2003 set_clear_internal(self);
2004 self->hash = -1;
2005 if (iterable == NULL)
2006 return 0;
2007 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002008}
2009
Raymond Hettingera690a992003-11-16 16:17:49 +00002010static PySequenceMethods set_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002011 set_len, /* sq_length */
2012 0, /* sq_concat */
2013 0, /* sq_repeat */
2014 0, /* sq_item */
2015 0, /* sq_slice */
2016 0, /* sq_ass_item */
2017 0, /* sq_ass_slice */
2018 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002019};
2020
2021/* set object ********************************************************/
2022
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002023#ifdef Py_DEBUG
2024static PyObject *test_c_api(PySetObject *so);
2025
2026PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2027All is well if assertions don't fail.");
2028#endif
2029
Raymond Hettingera690a992003-11-16 16:17:49 +00002030static PyMethodDef set_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002031 {"add", (PyCFunction)set_add, METH_O,
2032 add_doc},
2033 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2034 clear_doc},
2035 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2036 contains_doc},
2037 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2038 copy_doc},
2039 {"discard", (PyCFunction)set_discard, METH_O,
2040 discard_doc},
2041 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2042 difference_doc},
2043 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2044 difference_update_doc},
2045 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2046 intersection_doc},
2047 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2048 intersection_update_doc},
2049 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2050 isdisjoint_doc},
2051 {"issubset", (PyCFunction)set_issubset, METH_O,
2052 issubset_doc},
2053 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2054 issuperset_doc},
2055 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2056 pop_doc},
2057 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2058 reduce_doc},
2059 {"remove", (PyCFunction)set_remove, METH_O,
2060 remove_doc},
2061 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2062 sizeof_doc},
2063 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2064 symmetric_difference_doc},
2065 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2066 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002067#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002068 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2069 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002070#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002071 {"union", (PyCFunction)set_union, METH_VARARGS,
2072 union_doc},
2073 {"update", (PyCFunction)set_update, METH_VARARGS,
2074 update_doc},
2075 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002076};
2077
2078static PyNumberMethods set_as_number = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002079 0, /*nb_add*/
2080 (binaryfunc)set_sub, /*nb_subtract*/
2081 0, /*nb_multiply*/
2082 0, /*nb_divide*/
2083 0, /*nb_remainder*/
2084 0, /*nb_divmod*/
2085 0, /*nb_power*/
2086 0, /*nb_negative*/
2087 0, /*nb_positive*/
2088 0, /*nb_absolute*/
2089 0, /*nb_nonzero*/
2090 0, /*nb_invert*/
2091 0, /*nb_lshift*/
2092 0, /*nb_rshift*/
2093 (binaryfunc)set_and, /*nb_and*/
2094 (binaryfunc)set_xor, /*nb_xor*/
2095 (binaryfunc)set_or, /*nb_or*/
2096 0, /*nb_coerce*/
2097 0, /*nb_int*/
2098 0, /*nb_long*/
2099 0, /*nb_float*/
2100 0, /*nb_oct*/
2101 0, /*nb_hex*/
2102 0, /*nb_inplace_add*/
2103 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2104 0, /*nb_inplace_multiply*/
2105 0, /*nb_inplace_divide*/
2106 0, /*nb_inplace_remainder*/
2107 0, /*nb_inplace_power*/
2108 0, /*nb_inplace_lshift*/
2109 0, /*nb_inplace_rshift*/
2110 (binaryfunc)set_iand, /*nb_inplace_and*/
2111 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2112 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002113};
2114
2115PyDoc_STRVAR(set_doc,
Georg Brandlb36e63a2010-02-28 18:26:37 +00002116"set() -> new empty set object\n\
2117set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002118\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002119Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002120
2121PyTypeObject PySet_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002122 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2123 "set", /* tp_name */
2124 sizeof(PySetObject), /* tp_basicsize */
2125 0, /* tp_itemsize */
2126 /* methods */
2127 (destructor)set_dealloc, /* tp_dealloc */
2128 (printfunc)set_tp_print, /* tp_print */
2129 0, /* tp_getattr */
2130 0, /* tp_setattr */
2131 set_nocmp, /* tp_compare */
2132 (reprfunc)set_repr, /* tp_repr */
2133 &set_as_number, /* tp_as_number */
2134 &set_as_sequence, /* tp_as_sequence */
2135 0, /* tp_as_mapping */
2136 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2137 0, /* tp_call */
2138 0, /* tp_str */
2139 PyObject_GenericGetAttr, /* tp_getattro */
2140 0, /* tp_setattro */
2141 0, /* tp_as_buffer */
2142 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
2143 Py_TPFLAGS_BASETYPE, /* tp_flags */
2144 set_doc, /* tp_doc */
2145 (traverseproc)set_traverse, /* tp_traverse */
2146 (inquiry)set_clear_internal, /* tp_clear */
2147 (richcmpfunc)set_richcompare, /* tp_richcompare */
2148 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2149 (getiterfunc)set_iter, /* tp_iter */
2150 0, /* tp_iternext */
2151 set_methods, /* tp_methods */
2152 0, /* tp_members */
2153 0, /* tp_getset */
2154 0, /* tp_base */
2155 0, /* tp_dict */
2156 0, /* tp_descr_get */
2157 0, /* tp_descr_set */
2158 0, /* tp_dictoffset */
2159 (initproc)set_init, /* tp_init */
2160 PyType_GenericAlloc, /* tp_alloc */
2161 set_new, /* tp_new */
2162 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002163};
2164
2165/* frozenset object ********************************************************/
2166
2167
2168static PyMethodDef frozenset_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002169 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2170 contains_doc},
2171 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2172 copy_doc},
2173 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2174 difference_doc},
2175 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2176 intersection_doc},
2177 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2178 isdisjoint_doc},
2179 {"issubset", (PyCFunction)set_issubset, METH_O,
2180 issubset_doc},
2181 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2182 issuperset_doc},
2183 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2184 reduce_doc},
2185 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2186 sizeof_doc},
2187 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2188 symmetric_difference_doc},
2189 {"union", (PyCFunction)set_union, METH_VARARGS,
2190 union_doc},
2191 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002192};
2193
2194static PyNumberMethods frozenset_as_number = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002195 0, /*nb_add*/
2196 (binaryfunc)set_sub, /*nb_subtract*/
2197 0, /*nb_multiply*/
2198 0, /*nb_divide*/
2199 0, /*nb_remainder*/
2200 0, /*nb_divmod*/
2201 0, /*nb_power*/
2202 0, /*nb_negative*/
2203 0, /*nb_positive*/
2204 0, /*nb_absolute*/
2205 0, /*nb_nonzero*/
2206 0, /*nb_invert*/
2207 0, /*nb_lshift*/
2208 0, /*nb_rshift*/
2209 (binaryfunc)set_and, /*nb_and*/
2210 (binaryfunc)set_xor, /*nb_xor*/
2211 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002212};
2213
2214PyDoc_STRVAR(frozenset_doc,
Georg Brandlb36e63a2010-02-28 18:26:37 +00002215"frozenset() -> empty frozenset object\n\
2216frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002217\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002218Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002219
2220PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002221 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2222 "frozenset", /* tp_name */
2223 sizeof(PySetObject), /* tp_basicsize */
2224 0, /* tp_itemsize */
2225 /* methods */
2226 (destructor)set_dealloc, /* tp_dealloc */
2227 (printfunc)set_tp_print, /* tp_print */
2228 0, /* tp_getattr */
2229 0, /* tp_setattr */
2230 set_nocmp, /* tp_compare */
2231 (reprfunc)set_repr, /* tp_repr */
2232 &frozenset_as_number, /* tp_as_number */
2233 &set_as_sequence, /* tp_as_sequence */
2234 0, /* tp_as_mapping */
2235 frozenset_hash, /* tp_hash */
2236 0, /* tp_call */
2237 0, /* tp_str */
2238 PyObject_GenericGetAttr, /* tp_getattro */
2239 0, /* tp_setattro */
2240 0, /* tp_as_buffer */
2241 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
2242 Py_TPFLAGS_BASETYPE, /* tp_flags */
2243 frozenset_doc, /* tp_doc */
2244 (traverseproc)set_traverse, /* tp_traverse */
2245 (inquiry)set_clear_internal, /* tp_clear */
2246 (richcmpfunc)set_richcompare, /* tp_richcompare */
2247 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2248 (getiterfunc)set_iter, /* tp_iter */
2249 0, /* tp_iternext */
2250 frozenset_methods, /* tp_methods */
2251 0, /* tp_members */
2252 0, /* tp_getset */
2253 0, /* tp_base */
2254 0, /* tp_dict */
2255 0, /* tp_descr_get */
2256 0, /* tp_descr_set */
2257 0, /* tp_dictoffset */
2258 0, /* tp_init */
2259 PyType_GenericAlloc, /* tp_alloc */
2260 frozenset_new, /* tp_new */
2261 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002262};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002263
2264
2265/***** C API functions *************************************************/
2266
2267PyObject *
2268PySet_New(PyObject *iterable)
2269{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002270 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002271}
2272
2273PyObject *
2274PyFrozenSet_New(PyObject *iterable)
2275{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002276 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002277}
2278
Neal Norwitz8c49c822006-03-04 18:41:19 +00002279Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002280PySet_Size(PyObject *anyset)
2281{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002282 if (!PyAnySet_Check(anyset)) {
2283 PyErr_BadInternalCall();
2284 return -1;
2285 }
2286 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002287}
2288
2289int
Barry Warsaw176014f2006-03-30 22:45:35 +00002290PySet_Clear(PyObject *set)
2291{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002292 if (!PySet_Check(set)) {
2293 PyErr_BadInternalCall();
2294 return -1;
2295 }
2296 return set_clear_internal((PySetObject *)set);
Barry Warsaw176014f2006-03-30 22:45:35 +00002297}
2298
2299int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002300PySet_Contains(PyObject *anyset, PyObject *key)
2301{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002302 if (!PyAnySet_Check(anyset)) {
2303 PyErr_BadInternalCall();
2304 return -1;
2305 }
2306 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002307}
2308
2309int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002310PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002311{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002312 if (!PySet_Check(set)) {
2313 PyErr_BadInternalCall();
2314 return -1;
2315 }
2316 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002317}
2318
2319int
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002320PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002321{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002322 if (!PySet_Check(anyset) &&
2323 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2324 PyErr_BadInternalCall();
2325 return -1;
2326 }
2327 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002328}
2329
Barry Warsaw176014f2006-03-30 22:45:35 +00002330int
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002331_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Barry Warsaw176014f2006-03-30 22:45:35 +00002332{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002333 setentry *entry_ptr;
Barry Warsaw176014f2006-03-30 22:45:35 +00002334
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002335 if (!PyAnySet_Check(set)) {
2336 PyErr_BadInternalCall();
2337 return -1;
2338 }
2339 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2340 return 0;
2341 *key = entry_ptr->key;
2342 return 1;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002343}
2344
2345int
2346_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2347{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002348 setentry *entry;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002349
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002350 if (!PyAnySet_Check(set)) {
2351 PyErr_BadInternalCall();
2352 return -1;
2353 }
2354 if (set_next((PySetObject *)set, pos, &entry) == 0)
2355 return 0;
2356 *key = entry->key;
2357 *hash = entry->hash;
2358 return 1;
Barry Warsaw176014f2006-03-30 22:45:35 +00002359}
2360
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002361PyObject *
2362PySet_Pop(PyObject *set)
2363{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002364 if (!PySet_Check(set)) {
2365 PyErr_BadInternalCall();
2366 return NULL;
2367 }
2368 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002369}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002370
Barry Warsaw176014f2006-03-30 22:45:35 +00002371int
2372_PySet_Update(PyObject *set, PyObject *iterable)
2373{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002374 if (!PySet_Check(set)) {
2375 PyErr_BadInternalCall();
2376 return -1;
2377 }
2378 return set_update_internal((PySetObject *)set, iterable);
Barry Warsaw176014f2006-03-30 22:45:35 +00002379}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002380
2381#ifdef Py_DEBUG
2382
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002383/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002384 Returns True and original set is restored. */
2385
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002386#define assertRaises(call_return_value, exception) \
2387 do { \
2388 assert(call_return_value); \
2389 assert(PyErr_ExceptionMatches(exception)); \
2390 PyErr_Clear(); \
2391 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002392
2393static PyObject *
2394test_c_api(PySetObject *so)
2395{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002396 Py_ssize_t count;
2397 char *s;
2398 Py_ssize_t i;
2399 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2400 PyObject *ob = (PyObject *)so;
2401 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002402
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002403 /* Verify preconditions */
2404 assert(PyAnySet_Check(ob));
2405 assert(PyAnySet_CheckExact(ob));
2406 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner17d90542010-03-13 00:13:22 +00002407
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002408 /* so.clear(); so |= set("abc"); */
2409 str = PyString_FromString("abc");
2410 if (str == NULL)
2411 return NULL;
2412 set_clear_internal(so);
2413 if (set_update_internal(so, str) == -1) {
2414 Py_DECREF(str);
2415 return NULL;
2416 }
2417 Py_DECREF(str);
Victor Stinner17d90542010-03-13 00:13:22 +00002418
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002419 /* Exercise type/size checks */
2420 assert(PySet_Size(ob) == 3);
2421 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002422
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002423 /* Raise TypeError for non-iterable constructor arguments */
2424 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2425 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002426
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002427 /* Raise TypeError for unhashable key */
2428 dup = PySet_New(ob);
2429 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2430 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2431 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002432
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002433 /* Exercise successful pop, contains, add, and discard */
2434 elem = PySet_Pop(ob);
2435 assert(PySet_Contains(ob, elem) == 0);
2436 assert(PySet_GET_SIZE(ob) == 2);
2437 assert(PySet_Add(ob, elem) == 0);
2438 assert(PySet_Contains(ob, elem) == 1);
2439 assert(PySet_GET_SIZE(ob) == 3);
2440 assert(PySet_Discard(ob, elem) == 1);
2441 assert(PySet_GET_SIZE(ob) == 2);
2442 assert(PySet_Discard(ob, elem) == 0);
2443 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002444
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002445 /* Exercise clear */
2446 dup2 = PySet_New(dup);
2447 assert(PySet_Clear(dup2) == 0);
2448 assert(PySet_Size(dup2) == 0);
2449 Py_DECREF(dup2);
Barry Warsaw176014f2006-03-30 22:45:35 +00002450
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002451 /* Raise SystemError on clear or update of frozen set */
2452 f = PyFrozenSet_New(dup);
2453 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2454 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2455 assert(PySet_Add(f, elem) == 0);
2456 Py_INCREF(f);
2457 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2458 Py_DECREF(f);
2459 Py_DECREF(f);
Barry Warsaw176014f2006-03-30 22:45:35 +00002460
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002461 /* Exercise direct iteration */
2462 i = 0, count = 0;
2463 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2464 s = PyString_AsString(x);
2465 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2466 count++;
2467 }
2468 assert(count == 3);
Barry Warsaw176014f2006-03-30 22:45:35 +00002469
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002470 /* Exercise updates */
2471 dup2 = PySet_New(NULL);
2472 assert(_PySet_Update(dup2, dup) == 0);
2473 assert(PySet_Size(dup2) == 3);
2474 assert(_PySet_Update(dup2, dup) == 0);
2475 assert(PySet_Size(dup2) == 3);
2476 Py_DECREF(dup2);
Barry Warsaw176014f2006-03-30 22:45:35 +00002477
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002478 /* Raise SystemError when self argument is not a set or frozenset. */
2479 t = PyTuple_New(0);
2480 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2481 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2482 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002483
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002484 /* Raise SystemError when self argument is not a set. */
2485 f = PyFrozenSet_New(dup);
2486 assert(PySet_Size(f) == 3);
2487 assert(PyFrozenSet_CheckExact(f));
2488 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2489 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2490 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002491
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002492 /* Raise KeyError when popping from an empty set */
2493 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2494 Py_DECREF(ob);
2495 assert(PySet_GET_SIZE(ob) == 0);
2496 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002497
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002498 /* Restore the set from the copy using the PyNumber API */
2499 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2500 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002501
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002502 /* Verify constructors accept NULL arguments */
2503 f = PySet_New(NULL);
2504 assert(f != NULL);
2505 assert(PySet_GET_SIZE(f) == 0);
2506 Py_DECREF(f);
2507 f = PyFrozenSet_New(NULL);
2508 assert(f != NULL);
2509 assert(PyFrozenSet_CheckExact(f));
2510 assert(PySet_GET_SIZE(f) == 0);
2511 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002512
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002513 Py_DECREF(elem);
2514 Py_DECREF(dup);
2515 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002516}
2517
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002518#undef assertRaises
2519
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002520#endif