blob: 8a58d65d37ddc21b9b847fe618427b6890913a07 [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
Martin v. Löwis68192102007-07-21 06:55:02 +00006 Copyright (c) 2003-2007 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000019 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +000025}
26
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000027/* This must be >= 1. */
28#define PERTURB_SHIFT 5
29
30/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000031static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032
Armin Rigoe1709372006-04-12 17:06:05 +000033#ifdef Py_REF_DEBUG
34PyObject *
35_PySet_Dummy(void)
36{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000037 return dummy;
Armin Rigoe1709372006-04-12 17:06:05 +000038}
39#endif
40
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041#define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
44 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000045 } while(0)
46
Antoine Pitrouc83ea132010-05-09 14:46:46 +000047#define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
50 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051 } while(0)
52
Raymond Hettingerbc841a12005-08-07 13:02:53 +000053/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes5b970ad2008-02-06 13:33:44 +000054#ifndef PySet_MAXFREELIST
55#define PySet_MAXFREELIST 80
56#endif
57static PySetObject *free_list[PySet_MAXFREELIST];
58static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000059
60/*
61The basic lookup function used by all operations.
62This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
63Open addressing is preferred over chaining since the link overhead for
64chaining would be substantial (100% with typical malloc overhead).
65
66The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000067probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000068
69All arithmetic on hash should ignore overflow.
70
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000071Unlike the dictionary implementation, the lookkey functions can return
72NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073*/
74
75static setentry *
76set_lookkey(PySetObject *so, PyObject *key, register long hash)
77{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000078 register Py_ssize_t i;
79 register size_t perturb;
80 register setentry *freeslot;
81 register size_t mask = so->mask;
82 setentry *table = so->table;
83 register setentry *entry;
84 register int cmp;
85 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086
Antoine Pitrouc83ea132010-05-09 14:46:46 +000087 i = hash & mask;
88 entry = &table[i];
89 if (entry->key == NULL || entry->key == key)
90 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000091
Antoine Pitrouc83ea132010-05-09 14:46:46 +000092 if (entry->key == dummy)
93 freeslot = entry;
94 else {
95 if (entry->hash == hash) {
96 startkey = entry->key;
97 Py_INCREF(startkey);
98 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
99 Py_DECREF(startkey);
100 if (cmp < 0)
101 return NULL;
102 if (table == so->table && entry->key == startkey) {
103 if (cmp > 0)
104 return entry;
105 }
106 else {
107 /* The compare did major nasty stuff to the
108 * set: start over.
109 */
110 return set_lookkey(so, key, hash);
111 }
112 }
113 freeslot = NULL;
114 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000115
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000116 /* In the loop, key == dummy is by far (factor of 100s) the
117 least likely outcome, so test for that last. */
118 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
119 i = (i << 2) + i + perturb + 1;
120 entry = &table[i & mask];
121 if (entry->key == NULL) {
122 if (freeslot != NULL)
123 entry = freeslot;
124 break;
125 }
126 if (entry->key == key)
127 break;
128 if (entry->hash == hash && entry->key != dummy) {
129 startkey = entry->key;
130 Py_INCREF(startkey);
131 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
132 Py_DECREF(startkey);
133 if (cmp < 0)
134 return NULL;
135 if (table == so->table && entry->key == startkey) {
136 if (cmp > 0)
137 break;
138 }
139 else {
140 /* The compare did major nasty stuff to the
141 * set: start over.
142 */
143 return set_lookkey(so, key, hash);
144 }
145 }
146 else if (entry->key == dummy && freeslot == NULL)
147 freeslot = entry;
148 }
149 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150}
151
152/*
153 * Hacked up version of set_lookkey which can assume keys are always strings;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000154 * This means we can always use _PyString_Eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000155 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000156 */
157static setentry *
158set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
159{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000160 register Py_ssize_t i;
161 register size_t perturb;
162 register setentry *freeslot;
163 register size_t mask = so->mask;
164 setentry *table = so->table;
165 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000166
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 /* Make sure this function doesn't have to handle non-string keys,
168 including subclasses of str; e.g., one reason to subclass
169 strings is to override __eq__, and for speed we don't cater to
170 that here. */
171 if (!PyString_CheckExact(key)) {
172 so->lookup = set_lookkey;
173 return set_lookkey(so, key, hash);
174 }
175 i = hash & mask;
176 entry = &table[i];
177 if (entry->key == NULL || entry->key == key)
178 return entry;
179 if (entry->key == dummy)
180 freeslot = entry;
181 else {
182 if (entry->hash == hash && _PyString_Eq(entry->key, key))
183 return entry;
184 freeslot = NULL;
185 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000186
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 /* In the loop, key == dummy is by far (factor of 100s) the
188 least likely outcome, so test for that last. */
189 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
190 i = (i << 2) + i + perturb + 1;
191 entry = &table[i & mask];
192 if (entry->key == NULL)
193 return freeslot == NULL ? entry : freeslot;
194 if (entry->key == key
195 || (entry->hash == hash
196 && entry->key != dummy
197 && _PyString_Eq(entry->key, key)))
198 return entry;
199 if (entry->key == dummy && freeslot == NULL)
200 freeslot = entry;
201 }
202 assert(0); /* NOT REACHED */
203 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000204}
205
206/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000207Internal routine to insert a new key into the table.
Raymond Hettinger0c850862006-12-08 04:24:33 +0000208Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209Eats a reference to key.
210*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000211static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000212set_insert_key(register PySetObject *so, PyObject *key, long hash)
213{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000214 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000215
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000216 assert(so->lookup != NULL);
217 entry = so->lookup(so, key, hash);
218 if (entry == NULL)
219 return -1;
220 if (entry->key == NULL) {
221 /* UNUSED */
222 so->fill++;
223 entry->key = key;
224 entry->hash = hash;
225 so->used++;
226 } else if (entry->key == dummy) {
227 /* DUMMY */
228 entry->key = key;
229 entry->hash = hash;
230 so->used++;
231 Py_DECREF(dummy);
232 } else {
233 /* ACTIVE */
234 Py_DECREF(key);
235 }
236 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000237}
238
239/*
Raymond Hettinger0c850862006-12-08 04:24:33 +0000240Internal routine used by set_table_resize() to insert an item which is
241known to be absent from the set. This routine also assumes that
242the set contains no deleted entries. Besides the performance benefit,
243using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
244Note that no refcounts are changed by this routine; if needed, the caller
245is responsible for incref'ing `key`.
246*/
247static void
248set_insert_clean(register PySetObject *so, PyObject *key, long hash)
249{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000250 register size_t i;
251 register size_t perturb;
252 register size_t mask = (size_t)so->mask;
253 setentry *table = so->table;
254 register setentry *entry;
Raymond Hettinger0c850862006-12-08 04:24:33 +0000255
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000256 i = hash & mask;
257 entry = &table[i];
258 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
259 i = (i << 2) + i + perturb + 1;
260 entry = &table[i & mask];
261 }
262 so->fill++;
263 entry->key = key;
264 entry->hash = hash;
265 so->used++;
Raymond Hettinger0c850862006-12-08 04:24:33 +0000266}
267
268/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000269Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000270keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000271actually be smaller than the old one.
272*/
273static int
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000274set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000275{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000276 Py_ssize_t newsize;
277 setentry *oldtable, *newtable, *entry;
278 Py_ssize_t i;
279 int is_oldtable_malloced;
280 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000281
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000282 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000283
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 /* Find the smallest table size > minused. */
285 for (newsize = PySet_MINSIZE;
286 newsize <= minused && newsize > 0;
287 newsize <<= 1)
288 ;
289 if (newsize <= 0) {
290 PyErr_NoMemory();
291 return -1;
292 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000293
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 /* Get space for a new table. */
295 oldtable = so->table;
296 assert(oldtable != NULL);
297 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000298
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000299 if (newsize == PySet_MINSIZE) {
300 /* A large table is shrinking, or we can't get any smaller. */
301 newtable = so->smalltable;
302 if (newtable == oldtable) {
303 if (so->fill == so->used) {
304 /* No dummies, so no point doing anything. */
305 return 0;
306 }
307 /* We're not going to resize it, but rebuild the
308 table anyway to purge old dummy entries.
309 Subtle: This is *necessary* if fill==size,
310 as set_lookkey needs at least one virgin slot to
311 terminate failing searches. If fill < size, it's
312 merely desirable, as dummies slow searches. */
313 assert(so->fill > so->used);
314 memcpy(small_copy, oldtable, sizeof(small_copy));
315 oldtable = small_copy;
316 }
317 }
318 else {
319 newtable = PyMem_NEW(setentry, newsize);
320 if (newtable == NULL) {
321 PyErr_NoMemory();
322 return -1;
323 }
324 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000325
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000326 /* Make the set empty, using the new table. */
327 assert(newtable != oldtable);
328 so->table = newtable;
329 so->mask = newsize - 1;
330 memset(newtable, 0, sizeof(setentry) * newsize);
331 so->used = 0;
332 i = so->fill;
333 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 /* Copy the data over; this is refcount-neutral for active entries;
336 dummy entries aren't copied over, of course */
337 for (entry = oldtable; i > 0; entry++) {
338 if (entry->key == NULL) {
339 /* UNUSED */
340 ;
341 } else if (entry->key == dummy) {
342 /* DUMMY */
343 --i;
344 assert(entry->key == dummy);
345 Py_DECREF(entry->key);
346 } else {
347 /* ACTIVE */
348 --i;
349 set_insert_clean(so, entry->key, entry->hash);
350 }
351 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000352
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000353 if (is_oldtable_malloced)
354 PyMem_DEL(oldtable);
355 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000356}
357
Raymond Hettingerc991db22005-08-11 07:58:45 +0000358/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
359
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361set_add_entry(register PySetObject *so, setentry *entry)
362{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 register Py_ssize_t n_used;
Éric Araujof079c9b2011-03-22 23:47:32 +0100364 PyObject *key = entry->key;
365 long hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000366
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000367 assert(so->fill <= so->mask); /* at least one empty slot */
368 n_used = so->used;
Éric Araujof079c9b2011-03-22 23:47:32 +0100369 Py_INCREF(key);
370 if (set_insert_key(so, key, hash) == -1) {
371 Py_DECREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000372 return -1;
373 }
374 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
375 return 0;
376 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000377}
378
379static int
380set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000381{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000382 register long hash;
383 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000384
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000385 if (!PyString_CheckExact(key) ||
386 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
387 hash = PyObject_Hash(key);
388 if (hash == -1)
389 return -1;
390 }
391 assert(so->fill <= so->mask); /* at least one empty slot */
392 n_used = so->used;
393 Py_INCREF(key);
394 if (set_insert_key(so, key, hash) == -1) {
395 Py_DECREF(key);
396 return -1;
397 }
398 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
399 return 0;
400 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000401}
402
403#define DISCARD_NOTFOUND 0
404#define DISCARD_FOUND 1
405
406static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000407set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000408{ register setentry *entry;
409 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000410
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000411 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
412 if (entry == NULL)
413 return -1;
414 if (entry->key == NULL || entry->key == dummy)
415 return DISCARD_NOTFOUND;
416 old_key = entry->key;
417 Py_INCREF(dummy);
418 entry->key = dummy;
419 so->used--;
420 Py_DECREF(old_key);
421 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000422}
423
424static int
425set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000426{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000427 register long hash;
428 register setentry *entry;
429 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000430
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000431 assert (PyAnySet_Check(so));
432 if (!PyString_CheckExact(key) ||
433 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
434 hash = PyObject_Hash(key);
435 if (hash == -1)
436 return -1;
437 }
438 entry = (so->lookup)(so, key, hash);
439 if (entry == NULL)
440 return -1;
441 if (entry->key == NULL || entry->key == dummy)
442 return DISCARD_NOTFOUND;
443 old_key = entry->key;
444 Py_INCREF(dummy);
445 entry->key = dummy;
446 so->used--;
447 Py_DECREF(old_key);
448 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449}
450
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000451static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000452set_clear_internal(PySetObject *so)
453{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000454 setentry *entry, *table;
455 int table_is_malloced;
456 Py_ssize_t fill;
457 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000458#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000459 Py_ssize_t i, n;
460 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000461
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000462 n = so->mask + 1;
463 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464#endif
465
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000466 table = so->table;
467 assert(table != NULL);
468 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000469
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000470 /* This is delicate. During the process of clearing the set,
471 * decrefs can cause the set to mutate. To avoid fatal confusion
472 * (voice of experience), we have to make the set empty before
473 * clearing the slots, and never refer to anything via so->ref while
474 * clearing.
475 */
476 fill = so->fill;
477 if (table_is_malloced)
478 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000479
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000480 else if (fill > 0) {
481 /* It's a small table with something that needs to be cleared.
482 * Afraid the only safe way is to copy the set entries into
483 * another small table first.
484 */
485 memcpy(small_copy, table, sizeof(small_copy));
486 table = small_copy;
487 EMPTY_TO_MINSIZE(so);
488 }
489 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000490
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 /* Now we can finally clear things. If C had refcounts, we could
492 * assert that the refcount on table is 1 now, i.e. that this function
493 * has unique access to it, so decref side-effects can't alter it.
494 */
495 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000497 assert(i < n);
498 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000500 if (entry->key) {
501 --fill;
502 Py_DECREF(entry->key);
503 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000504#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000505 else
506 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000507#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000508 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000510 if (table_is_malloced)
511 PyMem_DEL(table);
512 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513}
514
515/*
516 * Iterate over a set table. Use like so:
517 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000518 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000519 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000520 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000521 * while (set_next(yourset, &pos, &entry)) {
522 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523 * }
524 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000525 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000527 */
528static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000529set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000530{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000531 Py_ssize_t i;
532 Py_ssize_t mask;
533 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000535 assert (PyAnySet_Check(so));
536 i = *pos_ptr;
537 assert(i >= 0);
538 table = so->table;
539 mask = so->mask;
540 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
541 i++;
542 *pos_ptr = i+1;
543 if (i > mask)
544 return 0;
545 assert(table[i].key != NULL);
546 *entry_ptr = &table[i];
547 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000548}
549
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000550static void
551set_dealloc(PySetObject *so)
552{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000553 register setentry *entry;
554 Py_ssize_t fill = so->fill;
555 PyObject_GC_UnTrack(so);
556 Py_TRASHCAN_SAFE_BEGIN(so)
557 if (so->weakreflist != NULL)
558 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000559
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000560 for (entry = so->table; fill > 0; entry++) {
561 if (entry->key) {
562 --fill;
563 Py_DECREF(entry->key);
564 }
565 }
566 if (so->table != so->smalltable)
567 PyMem_DEL(so->table);
568 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
569 free_list[numfree++] = so;
570 else
571 Py_TYPE(so)->tp_free(so);
572 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000573}
574
575static int
576set_tp_print(PySetObject *so, FILE *fp, int flags)
577{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000578 setentry *entry;
579 Py_ssize_t pos=0;
580 char *emit = ""; /* No separator emitted on first pass */
581 char *separator = ", ";
582 int status = Py_ReprEnter((PyObject*)so);
Raymond Hettinger53999102006-12-30 04:01:17 +0000583
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000584 if (status != 0) {
585 if (status < 0)
586 return status;
587 Py_BEGIN_ALLOW_THREADS
588 fprintf(fp, "%s(...)", so->ob_type->tp_name);
589 Py_END_ALLOW_THREADS
590 return 0;
591 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000592
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000593 Py_BEGIN_ALLOW_THREADS
594 fprintf(fp, "%s([", so->ob_type->tp_name);
595 Py_END_ALLOW_THREADS
596 while (set_next(so, &pos, &entry)) {
597 Py_BEGIN_ALLOW_THREADS
598 fputs(emit, fp);
599 Py_END_ALLOW_THREADS
600 emit = separator;
601 if (PyObject_Print(entry->key, fp, 0) != 0) {
602 Py_ReprLeave((PyObject*)so);
603 return -1;
604 }
605 }
606 Py_BEGIN_ALLOW_THREADS
607 fputs("])", fp);
608 Py_END_ALLOW_THREADS
609 Py_ReprLeave((PyObject*)so);
610 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000611}
612
613static PyObject *
614set_repr(PySetObject *so)
615{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000616 PyObject *keys, *result=NULL, *listrepr;
617 int status = Py_ReprEnter((PyObject*)so);
Raymond Hettinger53999102006-12-30 04:01:17 +0000618
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000619 if (status != 0) {
620 if (status < 0)
621 return NULL;
622 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
623 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000624
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000625 keys = PySequence_List((PyObject *)so);
626 if (keys == NULL)
627 goto done;
628 listrepr = PyObject_Repr(keys);
629 Py_DECREF(keys);
630 if (listrepr == NULL)
631 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000632
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000633 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
634 PyString_AS_STRING(listrepr));
635 Py_DECREF(listrepr);
Raymond Hettinger53999102006-12-30 04:01:17 +0000636done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000637 Py_ReprLeave((PyObject*)so);
638 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000639}
640
Martin v. Löwis18e16552006-02-15 17:27:45 +0000641static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000642set_len(PyObject *so)
643{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000644 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000645}
646
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000647static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000648set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000649{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000650 PySetObject *other;
Éric Araujof079c9b2011-03-22 23:47:32 +0100651 PyObject *key;
652 long hash;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000653 register Py_ssize_t i;
654 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000655
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000656 assert (PyAnySet_Check(so));
657 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000658
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000659 other = (PySetObject*)otherset;
660 if (other == so || other->used == 0)
661 /* a.update(a) or a.update({}); nothing to do */
662 return 0;
663 /* Do one big resize at the start, rather than
664 * incrementally resizing as we insert new keys. Expect
665 * that there will be no (or few) overlapping keys.
666 */
667 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
668 if (set_table_resize(so, (so->used + other->used)*2) != 0)
669 return -1;
670 }
671 for (i = 0; i <= other->mask; i++) {
672 entry = &other->table[i];
Éric Araujof079c9b2011-03-22 23:47:32 +0100673 key = entry->key;
674 hash = entry->hash;
675 if (key != NULL &&
676 key != dummy) {
677 Py_INCREF(key);
678 if (set_insert_key(so, key, hash) == -1) {
679 Py_DECREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000680 return -1;
681 }
682 }
683 }
684 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000685}
686
687static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000688set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000689{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000690 long hash;
691 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000692
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000693 if (!PyString_CheckExact(key) ||
694 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
695 hash = PyObject_Hash(key);
696 if (hash == -1)
697 return -1;
698 }
699 entry = (so->lookup)(so, key, hash);
700 if (entry == NULL)
701 return -1;
702 key = entry->key;
703 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000704}
705
Raymond Hettingerc991db22005-08-11 07:58:45 +0000706static int
707set_contains_entry(PySetObject *so, setentry *entry)
708{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 PyObject *key;
710 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000711
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000712 lu_entry = (so->lookup)(so, entry->key, entry->hash);
713 if (lu_entry == NULL)
714 return -1;
715 key = lu_entry->key;
716 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000717}
718
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000719static PyObject *
720set_pop(PySetObject *so)
721{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 register Py_ssize_t i = 0;
723 register setentry *entry;
724 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000725
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000726 assert (PyAnySet_Check(so));
727 if (so->used == 0) {
728 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
729 return NULL;
730 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000731
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000732 /* Set entry to "the first" unused or dummy set entry. We abuse
733 * the hash field of slot 0 to hold a search finger:
734 * If slot 0 has a value, use slot 0.
735 * Else slot 0 is being used to hold a search finger,
736 * and we use its hash value as the first index to look.
737 */
738 entry = &so->table[0];
739 if (entry->key == NULL || entry->key == dummy) {
740 i = entry->hash;
741 /* The hash field may be a real hash value, or it may be a
742 * legit search finger, or it may be a once-legit search
743 * finger that's out of bounds now because it wrapped around
744 * or the table shrunk -- simply make sure it's in bounds now.
745 */
746 if (i > so->mask || i < 1)
747 i = 1; /* skip slot 0 */
748 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
749 i++;
750 if (i > so->mask)
751 i = 1;
752 }
753 }
754 key = entry->key;
755 Py_INCREF(dummy);
756 entry->key = dummy;
757 so->used--;
758 so->table[0].hash = i + 1; /* next place to start */
759 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000760}
761
Andrew M. Kuchlingd7b7dde2008-10-03 16:29:19 +0000762PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
763Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000764
765static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000766set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000767{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000768 Py_ssize_t pos = 0;
769 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000770
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000771 while (set_next(so, &pos, &entry))
772 Py_VISIT(entry->key);
773 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000774}
775
776static long
777frozenset_hash(PyObject *self)
778{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000779 PySetObject *so = (PySetObject *)self;
780 long h, hash = 1927868237L;
781 setentry *entry;
782 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000783
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000784 if (so->hash != -1)
785 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000786
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000787 hash *= PySet_GET_SIZE(self) + 1;
788 while (set_next(so, &pos, &entry)) {
789 /* Work to increase the bit dispersion for closely spaced hash
790 values. The is important because some use cases have many
791 combinations of a small number of elements with nearby
792 hashes so that many distinct combinations collapse to only
793 a handful of distinct hash values. */
794 h = entry->hash;
795 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
796 }
797 hash = hash * 69069L + 907133923L;
798 if (hash == -1)
799 hash = 590923713L;
800 so->hash = hash;
801 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000802}
803
Raymond Hettingera9d99362005-08-05 00:01:15 +0000804/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000806typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000807 PyObject_HEAD
808 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
809 Py_ssize_t si_used;
810 Py_ssize_t si_pos;
811 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812} setiterobject;
813
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000814static void
815setiter_dealloc(setiterobject *si)
816{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000817 Py_XDECREF(si->si_set);
818 PyObject_GC_Del(si);
Antoine Pitrouaa687902009-01-01 14:11:22 +0000819}
820
821static int
822setiter_traverse(setiterobject *si, visitproc visit, void *arg)
823{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000824 Py_VISIT(si->si_set);
825 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000826}
827
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000828static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829setiter_len(setiterobject *si)
830{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000831 Py_ssize_t len = 0;
832 if (si->si_set != NULL && si->si_used == si->si_set->used)
833 len = si->len;
834 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835}
836
Armin Rigof5b3e362006-02-11 21:32:43 +0000837PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000838
839static PyMethodDef setiter_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000840 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
841 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000842};
843
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000844static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000845{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000846 PyObject *key;
847 register Py_ssize_t i, mask;
848 register setentry *entry;
849 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000851 if (so == NULL)
852 return NULL;
853 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000854
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000855 if (si->si_used != so->used) {
856 PyErr_SetString(PyExc_RuntimeError,
857 "Set changed size during iteration");
858 si->si_used = -1; /* Make this state sticky */
859 return NULL;
860 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000862 i = si->si_pos;
863 assert(i>=0);
864 entry = so->table;
865 mask = so->mask;
866 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
867 i++;
868 si->si_pos = i+1;
869 if (i > mask)
870 goto fail;
871 si->len--;
872 key = entry[i].key;
873 Py_INCREF(key);
874 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000875
876fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000877 Py_DECREF(so);
878 si->si_set = NULL;
879 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880}
881
Hye-Shik Change2956762005-08-01 05:26:41 +0000882static PyTypeObject PySetIter_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000883 PyVarObject_HEAD_INIT(&PyType_Type, 0)
884 "setiterator", /* tp_name */
885 sizeof(setiterobject), /* tp_basicsize */
886 0, /* tp_itemsize */
887 /* methods */
888 (destructor)setiter_dealloc, /* tp_dealloc */
889 0, /* tp_print */
890 0, /* tp_getattr */
891 0, /* tp_setattr */
892 0, /* tp_compare */
893 0, /* tp_repr */
894 0, /* tp_as_number */
895 0, /* tp_as_sequence */
896 0, /* tp_as_mapping */
897 0, /* tp_hash */
898 0, /* tp_call */
899 0, /* tp_str */
900 PyObject_GenericGetAttr, /* tp_getattro */
901 0, /* tp_setattro */
902 0, /* tp_as_buffer */
903 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
904 0, /* tp_doc */
905 (traverseproc)setiter_traverse, /* tp_traverse */
906 0, /* tp_clear */
907 0, /* tp_richcompare */
908 0, /* tp_weaklistoffset */
909 PyObject_SelfIter, /* tp_iter */
910 (iternextfunc)setiter_iternext, /* tp_iternext */
911 setiter_methods, /* tp_methods */
912 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000913};
914
Martin v. Löwis72d20672006-04-11 09:04:12 +0000915static PyObject *
916set_iter(PySetObject *so)
917{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000918 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
919 if (si == NULL)
920 return NULL;
921 Py_INCREF(so);
922 si->si_set = so;
923 si->si_used = so->used;
924 si->si_pos = 0;
925 si->len = so->used;
926 _PyObject_GC_TRACK(si);
927 return (PyObject *)si;
Martin v. Löwis72d20672006-04-11 09:04:12 +0000928}
929
Raymond Hettingerd7946662005-08-01 21:39:29 +0000930static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000931set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000932{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000933 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000934
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000935 if (PyAnySet_Check(other))
936 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000937
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000938 if (PyDict_CheckExact(other)) {
939 PyObject *value;
940 Py_ssize_t pos = 0;
941 long hash;
942 Py_ssize_t dictsize = PyDict_Size(other);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000943
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000944 /* Do one big resize at the start, rather than
945 * incrementally resizing as we insert new keys. Expect
946 * that there will be no (or few) overlapping keys.
947 */
948 if (dictsize == -1)
949 return -1;
950 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
951 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
952 return -1;
953 }
954 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
955 setentry an_entry;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000956
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000957 an_entry.hash = hash;
958 an_entry.key = key;
959 if (set_add_entry(so, &an_entry) == -1)
960 return -1;
961 }
962 return 0;
963 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000964
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000965 it = PyObject_GetIter(other);
966 if (it == NULL)
967 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000968
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000969 while ((key = PyIter_Next(it)) != NULL) {
970 if (set_add_key(so, key) == -1) {
971 Py_DECREF(it);
972 Py_DECREF(key);
973 return -1;
974 }
975 Py_DECREF(key);
976 }
977 Py_DECREF(it);
978 if (PyErr_Occurred())
979 return -1;
980 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000981}
982
983static PyObject *
Raymond Hettingeree4bcad2008-06-09 08:33:37 +0000984set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000985{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000986 Py_ssize_t i;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +0000987
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000988 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
989 PyObject *other = PyTuple_GET_ITEM(args, i);
990 if (set_update_internal(so, other) == -1)
991 return NULL;
992 }
993 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000994}
995
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000996PyDoc_STRVAR(update_doc,
Raymond Hettingeree4bcad2008-06-09 08:33:37 +0000997"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000998
999static PyObject *
1000make_new_set(PyTypeObject *type, PyObject *iterable)
1001{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001002 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001003
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001004 if (dummy == NULL) { /* Auto-initialize dummy */
1005 dummy = PyString_FromString("<dummy key>");
1006 if (dummy == NULL)
1007 return NULL;
1008 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001009
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001010 /* create PySetObject structure */
1011 if (numfree &&
1012 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1013 so = free_list[--numfree];
1014 assert (so != NULL && PyAnySet_CheckExact(so));
1015 Py_TYPE(so) = type;
1016 _Py_NewReference((PyObject *)so);
1017 EMPTY_TO_MINSIZE(so);
1018 PyObject_GC_Track(so);
1019 } else {
1020 so = (PySetObject *)type->tp_alloc(type, 0);
1021 if (so == NULL)
1022 return NULL;
1023 /* tp_alloc has already zeroed the structure */
1024 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1025 INIT_NONZERO_SET_SLOTS(so);
1026 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001028 so->lookup = set_lookkey_string;
1029 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001031 if (iterable != NULL) {
1032 if (set_update_internal(so, iterable) == -1) {
1033 Py_DECREF(so);
1034 return NULL;
1035 }
1036 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001037
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001038 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001039}
1040
Raymond Hettingerd7946662005-08-01 21:39:29 +00001041/* The empty frozenset is a singleton */
1042static PyObject *emptyfrozenset = NULL;
1043
Raymond Hettingera690a992003-11-16 16:17:49 +00001044static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001045frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001046{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001047 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001048
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1050 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001052 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1053 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001054
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001055 if (type != &PyFrozenSet_Type)
1056 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001057
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001058 if (iterable != NULL) {
1059 /* frozenset(f) is idempotent */
1060 if (PyFrozenSet_CheckExact(iterable)) {
1061 Py_INCREF(iterable);
1062 return iterable;
1063 }
1064 result = make_new_set(type, iterable);
1065 if (result == NULL || PySet_GET_SIZE(result))
1066 return result;
1067 Py_DECREF(result);
1068 }
1069 /* The empty frozenset is a singleton */
1070 if (emptyfrozenset == NULL)
1071 emptyfrozenset = make_new_set(type, NULL);
1072 Py_XINCREF(emptyfrozenset);
1073 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001074}
1075
1076void
1077PySet_Fini(void)
1078{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001079 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001080
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001081 while (numfree) {
1082 numfree--;
1083 so = free_list[numfree];
1084 PyObject_GC_Del(so);
1085 }
1086 Py_CLEAR(dummy);
1087 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001088}
1089
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001090static PyObject *
1091set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1092{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001093 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1094 return NULL;
1095
1096 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001097}
1098
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001099/* set_swap_bodies() switches the contents of any two sets by moving their
1100 internal data pointers and, if needed, copying the internal smalltables.
1101 Semantically equivalent to:
1102
1103 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1104
1105 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001106 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001107 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001108 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001109 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001110*/
1111
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001112static void
1113set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001114{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001115 Py_ssize_t t;
1116 setentry *u;
1117 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1118 setentry tab[PySet_MINSIZE];
1119 long h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001120
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001121 t = a->fill; a->fill = b->fill; b->fill = t;
1122 t = a->used; a->used = b->used; b->used = t;
1123 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001124
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001125 u = a->table;
1126 if (a->table == a->smalltable)
1127 u = b->smalltable;
1128 a->table = b->table;
1129 if (b->table == b->smalltable)
1130 a->table = a->smalltable;
1131 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001132
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001133 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001134
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001135 if (a->table == a->smalltable || b->table == b->smalltable) {
1136 memcpy(tab, a->smalltable, sizeof(tab));
1137 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1138 memcpy(b->smalltable, tab, sizeof(tab));
1139 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001140
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001141 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1142 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1143 h = a->hash; a->hash = b->hash; b->hash = h;
1144 } else {
1145 a->hash = -1;
1146 b->hash = -1;
1147 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001148}
1149
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001150static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001151set_copy(PySetObject *so)
1152{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001153 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001154}
1155
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001156static PyObject *
1157frozenset_copy(PySetObject *so)
1158{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001159 if (PyFrozenSet_CheckExact(so)) {
1160 Py_INCREF(so);
1161 return (PyObject *)so;
1162 }
1163 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001164}
1165
Raymond Hettingera690a992003-11-16 16:17:49 +00001166PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1167
1168static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001169set_clear(PySetObject *so)
1170{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001171 set_clear_internal(so);
1172 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001173}
1174
1175PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1176
1177static PyObject *
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001178set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001179{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001180 PySetObject *result;
1181 PyObject *other;
1182 Py_ssize_t i;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001183
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001184 result = (PySetObject *)set_copy(so);
1185 if (result == NULL)
1186 return NULL;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001187
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001188 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1189 other = PyTuple_GET_ITEM(args, i);
1190 if ((PyObject *)so == other)
1191 continue;
1192 if (set_update_internal(result, other) == -1) {
1193 Py_DECREF(result);
1194 return NULL;
1195 }
1196 }
1197 return (PyObject *)result;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001198}
1199
1200PyDoc_STRVAR(union_doc,
1201 "Return the union of sets as a new set.\n\
1202\n\
1203(i.e. all elements that are in either set.)");
1204
1205static PyObject *
1206set_or(PySetObject *so, PyObject *other)
1207{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001208 PySetObject *result;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001209
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001210 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1211 Py_INCREF(Py_NotImplemented);
1212 return Py_NotImplemented;
1213 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001214
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001215 result = (PySetObject *)set_copy(so);
1216 if (result == NULL)
1217 return NULL;
1218 if ((PyObject *)so == other)
1219 return (PyObject *)result;
1220 if (set_update_internal(result, other) == -1) {
1221 Py_DECREF(result);
1222 return NULL;
1223 }
1224 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001225}
1226
Raymond Hettingera690a992003-11-16 16:17:49 +00001227static PyObject *
1228set_ior(PySetObject *so, PyObject *other)
1229{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001230 if (!PyAnySet_Check(other)) {
1231 Py_INCREF(Py_NotImplemented);
1232 return Py_NotImplemented;
1233 }
1234 if (set_update_internal(so, other) == -1)
1235 return NULL;
1236 Py_INCREF(so);
1237 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001238}
1239
1240static PyObject *
1241set_intersection(PySetObject *so, PyObject *other)
1242{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001243 PySetObject *result;
1244 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001245
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001246 if ((PyObject *)so == other)
1247 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001248
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001249 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
1250 if (result == NULL)
1251 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001252
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001253 if (PyAnySet_Check(other)) {
1254 Py_ssize_t pos = 0;
1255 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001256
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001257 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1258 tmp = (PyObject *)so;
1259 so = (PySetObject *)other;
1260 other = tmp;
1261 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001262
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001263 while (set_next((PySetObject *)other, &pos, &entry)) {
1264 int rv = set_contains_entry(so, entry);
1265 if (rv == -1) {
1266 Py_DECREF(result);
1267 return NULL;
1268 }
1269 if (rv) {
1270 if (set_add_entry(result, entry) == -1) {
1271 Py_DECREF(result);
1272 return NULL;
1273 }
1274 }
1275 }
1276 return (PyObject *)result;
1277 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001278
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001279 it = PyObject_GetIter(other);
1280 if (it == NULL) {
1281 Py_DECREF(result);
1282 return NULL;
1283 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001284
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001285 while ((key = PyIter_Next(it)) != NULL) {
1286 int rv;
1287 setentry entry;
1288 long hash = PyObject_Hash(key);
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001289
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001290 if (hash == -1) {
1291 Py_DECREF(it);
1292 Py_DECREF(result);
1293 Py_DECREF(key);
1294 return NULL;
1295 }
1296 entry.hash = hash;
1297 entry.key = key;
1298 rv = set_contains_entry(so, &entry);
1299 if (rv == -1) {
1300 Py_DECREF(it);
1301 Py_DECREF(result);
1302 Py_DECREF(key);
1303 return NULL;
1304 }
1305 if (rv) {
1306 if (set_add_entry(result, &entry) == -1) {
1307 Py_DECREF(it);
1308 Py_DECREF(result);
1309 Py_DECREF(key);
1310 return NULL;
1311 }
1312 }
1313 Py_DECREF(key);
1314 }
1315 Py_DECREF(it);
1316 if (PyErr_Occurred()) {
1317 Py_DECREF(result);
1318 return NULL;
1319 }
1320 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001321}
1322
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001323static PyObject *
1324set_intersection_multi(PySetObject *so, PyObject *args)
1325{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001326 Py_ssize_t i;
1327 PyObject *result = (PyObject *)so;
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001328
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001329 if (PyTuple_GET_SIZE(args) == 0)
1330 return set_copy(so);
Raymond Hettinger610a93e2008-06-11 00:44:47 +00001331
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001332 Py_INCREF(so);
1333 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1334 PyObject *other = PyTuple_GET_ITEM(args, i);
1335 PyObject *newresult = set_intersection((PySetObject *)result, other);
1336 if (newresult == NULL) {
1337 Py_DECREF(result);
1338 return NULL;
1339 }
1340 Py_DECREF(result);
1341 result = newresult;
1342 }
1343 return result;
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001344}
1345
Raymond Hettingera690a992003-11-16 16:17:49 +00001346PyDoc_STRVAR(intersection_doc,
Raymond Hettinger79628d32009-11-18 20:28:22 +00001347"Return the intersection of two or more sets as a new set.\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00001348\n\
Raymond Hettinger79628d32009-11-18 20:28:22 +00001349(i.e. elements that are common to all of the sets.)");
Raymond Hettingera690a992003-11-16 16:17:49 +00001350
1351static PyObject *
1352set_intersection_update(PySetObject *so, PyObject *other)
1353{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001354 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001355
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001356 tmp = set_intersection(so, other);
1357 if (tmp == NULL)
1358 return NULL;
1359 set_swap_bodies(so, (PySetObject *)tmp);
1360 Py_DECREF(tmp);
1361 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001362}
1363
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001364static PyObject *
1365set_intersection_update_multi(PySetObject *so, PyObject *args)
1366{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001367 PyObject *tmp;
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001368
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001369 tmp = set_intersection_multi(so, args);
1370 if (tmp == NULL)
1371 return NULL;
1372 set_swap_bodies(so, (PySetObject *)tmp);
1373 Py_DECREF(tmp);
1374 Py_RETURN_NONE;
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001375}
1376
Raymond Hettingera690a992003-11-16 16:17:49 +00001377PyDoc_STRVAR(intersection_update_doc,
1378"Update a set with the intersection of itself and another.");
1379
1380static PyObject *
1381set_and(PySetObject *so, PyObject *other)
1382{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001383 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1384 Py_INCREF(Py_NotImplemented);
1385 return Py_NotImplemented;
1386 }
1387 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001388}
1389
1390static PyObject *
1391set_iand(PySetObject *so, PyObject *other)
1392{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001393 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001394
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001395 if (!PyAnySet_Check(other)) {
1396 Py_INCREF(Py_NotImplemented);
1397 return Py_NotImplemented;
1398 }
1399 result = set_intersection_update(so, other);
1400 if (result == NULL)
1401 return NULL;
1402 Py_DECREF(result);
1403 Py_INCREF(so);
1404 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001405}
1406
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001407static PyObject *
1408set_isdisjoint(PySetObject *so, PyObject *other)
1409{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001410 PyObject *key, *it, *tmp;
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001411
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001412 if ((PyObject *)so == other) {
1413 if (PySet_GET_SIZE(so) == 0)
1414 Py_RETURN_TRUE;
1415 else
1416 Py_RETURN_FALSE;
1417 }
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001418
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001419 if (PyAnySet_CheckExact(other)) {
1420 Py_ssize_t pos = 0;
1421 setentry *entry;
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001422
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001423 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1424 tmp = (PyObject *)so;
1425 so = (PySetObject *)other;
1426 other = tmp;
1427 }
1428 while (set_next((PySetObject *)other, &pos, &entry)) {
1429 int rv = set_contains_entry(so, entry);
1430 if (rv == -1)
1431 return NULL;
1432 if (rv)
1433 Py_RETURN_FALSE;
1434 }
1435 Py_RETURN_TRUE;
1436 }
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001437
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001438 it = PyObject_GetIter(other);
1439 if (it == NULL)
1440 return NULL;
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001441
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001442 while ((key = PyIter_Next(it)) != NULL) {
1443 int rv;
1444 setentry entry;
1445 long hash = PyObject_Hash(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001446
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001447 if (hash == -1) {
1448 Py_DECREF(key);
1449 Py_DECREF(it);
1450 return NULL;
1451 }
1452 entry.hash = hash;
1453 entry.key = key;
1454 rv = set_contains_entry(so, &entry);
1455 Py_DECREF(key);
1456 if (rv == -1) {
1457 Py_DECREF(it);
1458 return NULL;
1459 }
1460 if (rv) {
1461 Py_DECREF(it);
1462 Py_RETURN_FALSE;
1463 }
1464 }
1465 Py_DECREF(it);
1466 if (PyErr_Occurred())
1467 return NULL;
1468 Py_RETURN_TRUE;
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001469}
1470
1471PyDoc_STRVAR(isdisjoint_doc,
1472"Return True if two sets have a null intersection.");
1473
Neal Norwitz6576bd82005-11-13 18:41:28 +00001474static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001475set_difference_update_internal(PySetObject *so, PyObject *other)
1476{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001477 if ((PyObject *)so == other)
1478 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001479
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001480 if (PyAnySet_Check(other)) {
1481 setentry *entry;
1482 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001483
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001484 while (set_next((PySetObject *)other, &pos, &entry))
1485 if (set_discard_entry(so, entry) == -1)
1486 return -1;
1487 } else {
1488 PyObject *key, *it;
1489 it = PyObject_GetIter(other);
1490 if (it == NULL)
1491 return -1;
1492
1493 while ((key = PyIter_Next(it)) != NULL) {
1494 if (set_discard_key(so, key) == -1) {
1495 Py_DECREF(it);
1496 Py_DECREF(key);
1497 return -1;
1498 }
1499 Py_DECREF(key);
1500 }
1501 Py_DECREF(it);
1502 if (PyErr_Occurred())
1503 return -1;
1504 }
1505 /* If more than 1/5 are dummies, then resize them away. */
1506 if ((so->fill - so->used) * 5 < so->mask)
1507 return 0;
1508 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001509}
1510
Raymond Hettingera690a992003-11-16 16:17:49 +00001511static PyObject *
Raymond Hettinger4267be62008-06-11 10:30:54 +00001512set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001513{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001514 Py_ssize_t i;
Raymond Hettinger4267be62008-06-11 10:30:54 +00001515
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001516 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1517 PyObject *other = PyTuple_GET_ITEM(args, i);
1518 if (set_difference_update_internal(so, other) == -1)
1519 return NULL;
1520 }
1521 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001522}
1523
1524PyDoc_STRVAR(difference_update_doc,
1525"Remove all elements of another set from this set.");
1526
1527static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001528set_difference(PySetObject *so, PyObject *other)
1529{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001530 PyObject *result;
1531 setentry *entry;
1532 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001533
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001534 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
1535 result = set_copy(so);
1536 if (result == NULL)
1537 return NULL;
1538 if (set_difference_update_internal((PySetObject *)result, other) != -1)
1539 return result;
1540 Py_DECREF(result);
1541 return NULL;
1542 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001543
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001544 result = make_new_set(Py_TYPE(so), NULL);
1545 if (result == NULL)
1546 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001547
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001548 if (PyDict_CheckExact(other)) {
1549 while (set_next(so, &pos, &entry)) {
1550 setentry entrycopy;
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001551 int rv;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001552 entrycopy.hash = entry->hash;
1553 entrycopy.key = entry->key;
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001554 rv = _PyDict_Contains(other, entry->key, entry->hash);
1555 if (rv < 0) {
1556 Py_DECREF(result);
1557 return NULL;
1558 }
1559 if (!rv) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001560 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1561 Py_DECREF(result);
1562 return NULL;
1563 }
1564 }
1565 }
1566 return result;
1567 }
1568
1569 while (set_next(so, &pos, &entry)) {
1570 int rv = set_contains_entry((PySetObject *)other, entry);
1571 if (rv == -1) {
1572 Py_DECREF(result);
1573 return NULL;
1574 }
1575 if (!rv) {
1576 if (set_add_entry((PySetObject *)result, entry) == -1) {
1577 Py_DECREF(result);
1578 return NULL;
1579 }
1580 }
1581 }
1582 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001583}
1584
Raymond Hettinger4267be62008-06-11 10:30:54 +00001585static PyObject *
1586set_difference_multi(PySetObject *so, PyObject *args)
1587{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001588 Py_ssize_t i;
1589 PyObject *result, *other;
Raymond Hettinger4267be62008-06-11 10:30:54 +00001590
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001591 if (PyTuple_GET_SIZE(args) == 0)
1592 return set_copy(so);
Raymond Hettinger4267be62008-06-11 10:30:54 +00001593
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001594 other = PyTuple_GET_ITEM(args, 0);
1595 result = set_difference(so, other);
1596 if (result == NULL)
1597 return NULL;
Raymond Hettinger4267be62008-06-11 10:30:54 +00001598
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001599 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1600 other = PyTuple_GET_ITEM(args, i);
1601 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1602 Py_DECREF(result);
1603 return NULL;
1604 }
1605 }
1606 return result;
Raymond Hettinger4267be62008-06-11 10:30:54 +00001607}
1608
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001609PyDoc_STRVAR(difference_doc,
Raymond Hettinger4267be62008-06-11 10:30:54 +00001610"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001611\n\
Raymond Hettinger4267be62008-06-11 10:30:54 +00001612(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001613static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001614set_sub(PySetObject *so, PyObject *other)
1615{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001616 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1617 Py_INCREF(Py_NotImplemented);
1618 return Py_NotImplemented;
1619 }
1620 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001621}
1622
1623static PyObject *
1624set_isub(PySetObject *so, PyObject *other)
1625{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001626 if (!PyAnySet_Check(other)) {
1627 Py_INCREF(Py_NotImplemented);
1628 return Py_NotImplemented;
1629 }
1630 if (set_difference_update_internal(so, other) == -1)
1631 return NULL;
1632 Py_INCREF(so);
1633 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001634}
1635
1636static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001637set_symmetric_difference_update(PySetObject *so, PyObject *other)
1638{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001639 PySetObject *otherset;
1640 PyObject *key;
1641 Py_ssize_t pos = 0;
1642 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001643
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001644 if ((PyObject *)so == other)
1645 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001646
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001647 if (PyDict_CheckExact(other)) {
1648 PyObject *value;
1649 int rv;
1650 long hash;
1651 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1652 setentry an_entry;
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001653
Éric Araujof079c9b2011-03-22 23:47:32 +01001654 Py_INCREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001655 an_entry.hash = hash;
1656 an_entry.key = key;
Éric Araujof079c9b2011-03-22 23:47:32 +01001657
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001658 rv = set_discard_entry(so, &an_entry);
Éric Araujof079c9b2011-03-22 23:47:32 +01001659 if (rv == -1) {
1660 Py_DECREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001661 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001662 }
Éric Araujof079c9b2011-03-22 23:47:32 +01001663 if (rv == DISCARD_NOTFOUND) {
1664 if (set_add_entry(so, &an_entry) == -1) {
1665 Py_DECREF(key);
1666 return NULL;
1667 }
1668 }
1669 Py_DECREF(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001670 }
1671 Py_RETURN_NONE;
1672 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001673
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001674 if (PyAnySet_Check(other)) {
1675 Py_INCREF(other);
1676 otherset = (PySetObject *)other;
1677 } else {
1678 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
1679 if (otherset == NULL)
1680 return NULL;
1681 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001682
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001683 while (set_next(otherset, &pos, &entry)) {
1684 int rv = set_discard_entry(so, entry);
1685 if (rv == -1) {
1686 Py_DECREF(otherset);
1687 return NULL;
1688 }
1689 if (rv == DISCARD_NOTFOUND) {
1690 if (set_add_entry(so, entry) == -1) {
1691 Py_DECREF(otherset);
1692 return NULL;
1693 }
1694 }
1695 }
1696 Py_DECREF(otherset);
1697 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001698}
1699
1700PyDoc_STRVAR(symmetric_difference_update_doc,
1701"Update a set with the symmetric difference of itself and another.");
1702
1703static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001704set_symmetric_difference(PySetObject *so, PyObject *other)
1705{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001706 PyObject *rv;
1707 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001708
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001709 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
1710 if (otherset == NULL)
1711 return NULL;
1712 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1713 if (rv == NULL)
1714 return NULL;
1715 Py_DECREF(rv);
1716 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001717}
1718
1719PyDoc_STRVAR(symmetric_difference_doc,
1720"Return the symmetric difference of two sets as a new set.\n\
1721\n\
1722(i.e. all elements that are in exactly one of the sets.)");
1723
1724static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001725set_xor(PySetObject *so, PyObject *other)
1726{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001727 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1728 Py_INCREF(Py_NotImplemented);
1729 return Py_NotImplemented;
1730 }
1731 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001732}
1733
1734static PyObject *
1735set_ixor(PySetObject *so, PyObject *other)
1736{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001737 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001738
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001739 if (!PyAnySet_Check(other)) {
1740 Py_INCREF(Py_NotImplemented);
1741 return Py_NotImplemented;
1742 }
1743 result = set_symmetric_difference_update(so, other);
1744 if (result == NULL)
1745 return NULL;
1746 Py_DECREF(result);
1747 Py_INCREF(so);
1748 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001749}
1750
1751static PyObject *
1752set_issubset(PySetObject *so, PyObject *other)
1753{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001754 setentry *entry;
1755 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001757 if (!PyAnySet_Check(other)) {
1758 PyObject *tmp, *result;
1759 tmp = make_new_set(&PySet_Type, other);
1760 if (tmp == NULL)
1761 return NULL;
1762 result = set_issubset(so, tmp);
1763 Py_DECREF(tmp);
1764 return result;
1765 }
1766 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1767 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001768
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001769 while (set_next(so, &pos, &entry)) {
1770 int rv = set_contains_entry((PySetObject *)other, entry);
1771 if (rv == -1)
1772 return NULL;
1773 if (!rv)
1774 Py_RETURN_FALSE;
1775 }
1776 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001777}
1778
1779PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1780
1781static PyObject *
1782set_issuperset(PySetObject *so, PyObject *other)
1783{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001784 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001785
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001786 if (!PyAnySet_Check(other)) {
1787 tmp = make_new_set(&PySet_Type, other);
1788 if (tmp == NULL)
1789 return NULL;
1790 result = set_issuperset(so, tmp);
1791 Py_DECREF(tmp);
1792 return result;
1793 }
1794 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001795}
1796
1797PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1798
Raymond Hettingera690a992003-11-16 16:17:49 +00001799static PyObject *
1800set_richcompare(PySetObject *v, PyObject *w, int op)
1801{
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001802 PyObject *r1;
1803 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001804
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001805 if(!PyAnySet_Check(w)) {
Raymond Hettingerf643b9a2014-05-25 22:13:41 -07001806 Py_INCREF(Py_NotImplemented);
1807 return Py_NotImplemented;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001808 }
1809 switch (op) {
1810 case Py_EQ:
1811 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1812 Py_RETURN_FALSE;
1813 if (v->hash != -1 &&
1814 ((PySetObject *)w)->hash != -1 &&
1815 v->hash != ((PySetObject *)w)->hash)
1816 Py_RETURN_FALSE;
1817 return set_issubset(v, w);
1818 case Py_NE:
1819 r1 = set_richcompare(v, w, Py_EQ);
1820 if (r1 == NULL)
1821 return NULL;
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001822 r2 = PyObject_IsTrue(r1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001823 Py_DECREF(r1);
Serhiy Storchaka5127ed72015-05-30 17:45:12 +03001824 if (r2 < 0)
1825 return NULL;
1826 return PyBool_FromLong(!r2);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001827 case Py_LE:
1828 return set_issubset(v, w);
1829 case Py_GE:
1830 return set_issuperset(v, w);
1831 case Py_LT:
1832 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1833 Py_RETURN_FALSE;
1834 return set_issubset(v, w);
1835 case Py_GT:
1836 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1837 Py_RETURN_FALSE;
1838 return set_issuperset(v, w);
1839 }
1840 Py_INCREF(Py_NotImplemented);
1841 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001842}
1843
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001844static int
Georg Brandl347b3002006-03-30 11:57:00 +00001845set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001846{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001847 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1848 return -1;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001849}
1850
Raymond Hettingera690a992003-11-16 16:17:49 +00001851static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001852set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001853{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001854 if (set_add_key(so, key) == -1)
1855 return NULL;
1856 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001857}
1858
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001859PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001860"Add an element to a set.\n\
1861\n\
1862This has no effect if the element is already present.");
1863
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001864static int
1865set_contains(PySetObject *so, PyObject *key)
1866{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001867 PyObject *tmpkey;
1868 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001869
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001870 rv = set_contains_key(so, key);
1871 if (rv == -1) {
1872 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1873 return -1;
1874 PyErr_Clear();
Raymond Hettinger3ad323e2010-08-06 10:18:56 +00001875 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001876 if (tmpkey == NULL)
1877 return -1;
Petri Lehtinen5f4d8702011-10-30 13:55:02 +02001878 rv = set_contains_key(so, tmpkey);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001879 Py_DECREF(tmpkey);
1880 }
1881 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001882}
1883
1884static PyObject *
1885set_direct_contains(PySetObject *so, PyObject *key)
1886{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001887 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001889 result = set_contains(so, key);
1890 if (result == -1)
1891 return NULL;
1892 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001893}
1894
1895PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1896
Raymond Hettingera690a992003-11-16 16:17:49 +00001897static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001898set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001899{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001900 PyObject *tmpkey;
1901 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001902
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001903 rv = set_discard_key(so, key);
1904 if (rv == -1) {
1905 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1906 return NULL;
1907 PyErr_Clear();
Raymond Hettinger3ad323e2010-08-06 10:18:56 +00001908 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001909 if (tmpkey == NULL)
1910 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001911 rv = set_discard_key(so, tmpkey);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001912 Py_DECREF(tmpkey);
1913 if (rv == -1)
1914 return NULL;
1915 }
Amaury Forgeot d'Arcd78b9dc2008-10-07 20:32:10 +00001916
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001917 if (rv == DISCARD_NOTFOUND) {
1918 set_key_error(key);
1919 return NULL;
1920 }
1921 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001922}
1923
1924PyDoc_STRVAR(remove_doc,
1925"Remove an element from a set; it must be a member.\n\
1926\n\
1927If the element is not a member, raise a KeyError.");
1928
1929static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001930set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001931{
Benjamin Petersone3b5eda2011-10-30 14:24:44 -04001932 PyObject *tmpkey;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001933 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001935 rv = set_discard_key(so, key);
1936 if (rv == -1) {
1937 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1938 return NULL;
1939 PyErr_Clear();
Raymond Hettinger3ad323e2010-08-06 10:18:56 +00001940 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001941 if (tmpkey == NULL)
1942 return NULL;
Petri Lehtinena39de112011-10-30 14:31:27 +02001943 rv = set_discard_key(so, tmpkey);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001944 Py_DECREF(tmpkey);
Petri Lehtinena39de112011-10-30 14:31:27 +02001945 if (rv == -1)
1946 return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001947 }
1948 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001949}
1950
1951PyDoc_STRVAR(discard_doc,
1952"Remove an element from a set if it is a member.\n\
1953\n\
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001954If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001955
1956static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001957set_reduce(PySetObject *so)
1958{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001959 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001960
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001961 keys = PySequence_List((PyObject *)so);
1962 if (keys == NULL)
1963 goto done;
1964 args = PyTuple_Pack(1, keys);
1965 if (args == NULL)
1966 goto done;
1967 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1968 if (dict == NULL) {
1969 PyErr_Clear();
1970 dict = Py_None;
1971 Py_INCREF(dict);
1972 }
1973 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001974done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001975 Py_XDECREF(args);
1976 Py_XDECREF(keys);
1977 Py_XDECREF(dict);
1978 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001979}
1980
1981PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1982
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00001983static PyObject *
1984set_sizeof(PySetObject *so)
1985{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001986 Py_ssize_t res;
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00001987
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001988 res = sizeof(PySetObject);
1989 if (so->table != so->smalltable)
1990 res = res + (so->mask + 1) * sizeof(setentry);
1991 return PyInt_FromSsize_t(res);
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00001992}
1993
1994PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001995static int
1996set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1997{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001998 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001999
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002000 if (!PyAnySet_Check(self))
2001 return -1;
2002 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2003 return -1;
2004 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2005 return -1;
2006 set_clear_internal(self);
2007 self->hash = -1;
2008 if (iterable == NULL)
2009 return 0;
2010 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002011}
2012
Raymond Hettingera690a992003-11-16 16:17:49 +00002013static PySequenceMethods set_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002014 set_len, /* sq_length */
2015 0, /* sq_concat */
2016 0, /* sq_repeat */
2017 0, /* sq_item */
2018 0, /* sq_slice */
2019 0, /* sq_ass_item */
2020 0, /* sq_ass_slice */
2021 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002022};
2023
2024/* set object ********************************************************/
2025
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002026#ifdef Py_DEBUG
2027static PyObject *test_c_api(PySetObject *so);
2028
2029PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2030All is well if assertions don't fail.");
2031#endif
2032
Raymond Hettingera690a992003-11-16 16:17:49 +00002033static PyMethodDef set_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002034 {"add", (PyCFunction)set_add, METH_O,
2035 add_doc},
2036 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2037 clear_doc},
2038 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2039 contains_doc},
2040 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2041 copy_doc},
2042 {"discard", (PyCFunction)set_discard, METH_O,
2043 discard_doc},
2044 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2045 difference_doc},
2046 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2047 difference_update_doc},
2048 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2049 intersection_doc},
2050 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2051 intersection_update_doc},
2052 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2053 isdisjoint_doc},
2054 {"issubset", (PyCFunction)set_issubset, METH_O,
2055 issubset_doc},
2056 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2057 issuperset_doc},
2058 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2059 pop_doc},
2060 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2061 reduce_doc},
2062 {"remove", (PyCFunction)set_remove, METH_O,
2063 remove_doc},
2064 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2065 sizeof_doc},
2066 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2067 symmetric_difference_doc},
2068 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2069 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002070#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002071 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2072 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002073#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002074 {"union", (PyCFunction)set_union, METH_VARARGS,
2075 union_doc},
2076 {"update", (PyCFunction)set_update, METH_VARARGS,
2077 update_doc},
2078 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002079};
2080
2081static PyNumberMethods set_as_number = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002082 0, /*nb_add*/
2083 (binaryfunc)set_sub, /*nb_subtract*/
2084 0, /*nb_multiply*/
2085 0, /*nb_divide*/
2086 0, /*nb_remainder*/
2087 0, /*nb_divmod*/
2088 0, /*nb_power*/
2089 0, /*nb_negative*/
2090 0, /*nb_positive*/
2091 0, /*nb_absolute*/
2092 0, /*nb_nonzero*/
2093 0, /*nb_invert*/
2094 0, /*nb_lshift*/
2095 0, /*nb_rshift*/
2096 (binaryfunc)set_and, /*nb_and*/
2097 (binaryfunc)set_xor, /*nb_xor*/
2098 (binaryfunc)set_or, /*nb_or*/
2099 0, /*nb_coerce*/
2100 0, /*nb_int*/
2101 0, /*nb_long*/
2102 0, /*nb_float*/
2103 0, /*nb_oct*/
2104 0, /*nb_hex*/
2105 0, /*nb_inplace_add*/
2106 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2107 0, /*nb_inplace_multiply*/
2108 0, /*nb_inplace_divide*/
2109 0, /*nb_inplace_remainder*/
2110 0, /*nb_inplace_power*/
2111 0, /*nb_inplace_lshift*/
2112 0, /*nb_inplace_rshift*/
2113 (binaryfunc)set_iand, /*nb_inplace_and*/
2114 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2115 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002116};
2117
2118PyDoc_STRVAR(set_doc,
Georg Brandlb36e63a2010-02-28 18:26:37 +00002119"set() -> new empty set object\n\
2120set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002121\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002122Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002123
2124PyTypeObject PySet_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002125 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2126 "set", /* tp_name */
2127 sizeof(PySetObject), /* tp_basicsize */
2128 0, /* tp_itemsize */
2129 /* methods */
2130 (destructor)set_dealloc, /* tp_dealloc */
2131 (printfunc)set_tp_print, /* tp_print */
2132 0, /* tp_getattr */
2133 0, /* tp_setattr */
2134 set_nocmp, /* tp_compare */
2135 (reprfunc)set_repr, /* tp_repr */
2136 &set_as_number, /* tp_as_number */
2137 &set_as_sequence, /* tp_as_sequence */
2138 0, /* tp_as_mapping */
2139 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2140 0, /* tp_call */
2141 0, /* tp_str */
2142 PyObject_GenericGetAttr, /* tp_getattro */
2143 0, /* tp_setattro */
2144 0, /* tp_as_buffer */
2145 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
2146 Py_TPFLAGS_BASETYPE, /* tp_flags */
2147 set_doc, /* tp_doc */
2148 (traverseproc)set_traverse, /* tp_traverse */
2149 (inquiry)set_clear_internal, /* tp_clear */
2150 (richcmpfunc)set_richcompare, /* tp_richcompare */
2151 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2152 (getiterfunc)set_iter, /* tp_iter */
2153 0, /* tp_iternext */
2154 set_methods, /* tp_methods */
2155 0, /* tp_members */
2156 0, /* tp_getset */
2157 0, /* tp_base */
2158 0, /* tp_dict */
2159 0, /* tp_descr_get */
2160 0, /* tp_descr_set */
2161 0, /* tp_dictoffset */
2162 (initproc)set_init, /* tp_init */
2163 PyType_GenericAlloc, /* tp_alloc */
2164 set_new, /* tp_new */
2165 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002166};
2167
2168/* frozenset object ********************************************************/
2169
2170
2171static PyMethodDef frozenset_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002172 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2173 contains_doc},
2174 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2175 copy_doc},
2176 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2177 difference_doc},
2178 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2179 intersection_doc},
2180 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2181 isdisjoint_doc},
2182 {"issubset", (PyCFunction)set_issubset, METH_O,
2183 issubset_doc},
2184 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2185 issuperset_doc},
2186 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2187 reduce_doc},
2188 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2189 sizeof_doc},
2190 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2191 symmetric_difference_doc},
2192 {"union", (PyCFunction)set_union, METH_VARARGS,
2193 union_doc},
2194 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002195};
2196
2197static PyNumberMethods frozenset_as_number = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002198 0, /*nb_add*/
2199 (binaryfunc)set_sub, /*nb_subtract*/
2200 0, /*nb_multiply*/
2201 0, /*nb_divide*/
2202 0, /*nb_remainder*/
2203 0, /*nb_divmod*/
2204 0, /*nb_power*/
2205 0, /*nb_negative*/
2206 0, /*nb_positive*/
2207 0, /*nb_absolute*/
2208 0, /*nb_nonzero*/
2209 0, /*nb_invert*/
2210 0, /*nb_lshift*/
2211 0, /*nb_rshift*/
2212 (binaryfunc)set_and, /*nb_and*/
2213 (binaryfunc)set_xor, /*nb_xor*/
2214 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002215};
2216
2217PyDoc_STRVAR(frozenset_doc,
Georg Brandlb36e63a2010-02-28 18:26:37 +00002218"frozenset() -> empty frozenset object\n\
2219frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002220\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002221Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002222
2223PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002224 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2225 "frozenset", /* tp_name */
2226 sizeof(PySetObject), /* tp_basicsize */
2227 0, /* tp_itemsize */
2228 /* methods */
2229 (destructor)set_dealloc, /* tp_dealloc */
2230 (printfunc)set_tp_print, /* tp_print */
2231 0, /* tp_getattr */
2232 0, /* tp_setattr */
2233 set_nocmp, /* tp_compare */
2234 (reprfunc)set_repr, /* tp_repr */
2235 &frozenset_as_number, /* tp_as_number */
2236 &set_as_sequence, /* tp_as_sequence */
2237 0, /* tp_as_mapping */
2238 frozenset_hash, /* tp_hash */
2239 0, /* tp_call */
2240 0, /* tp_str */
2241 PyObject_GenericGetAttr, /* tp_getattro */
2242 0, /* tp_setattro */
2243 0, /* tp_as_buffer */
2244 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
2245 Py_TPFLAGS_BASETYPE, /* tp_flags */
2246 frozenset_doc, /* tp_doc */
2247 (traverseproc)set_traverse, /* tp_traverse */
2248 (inquiry)set_clear_internal, /* tp_clear */
2249 (richcmpfunc)set_richcompare, /* tp_richcompare */
2250 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2251 (getiterfunc)set_iter, /* tp_iter */
2252 0, /* tp_iternext */
2253 frozenset_methods, /* tp_methods */
2254 0, /* tp_members */
2255 0, /* tp_getset */
2256 0, /* tp_base */
2257 0, /* tp_dict */
2258 0, /* tp_descr_get */
2259 0, /* tp_descr_set */
2260 0, /* tp_dictoffset */
2261 0, /* tp_init */
2262 PyType_GenericAlloc, /* tp_alloc */
2263 frozenset_new, /* tp_new */
2264 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002265};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002266
2267
2268/***** C API functions *************************************************/
2269
2270PyObject *
2271PySet_New(PyObject *iterable)
2272{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002273 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002274}
2275
2276PyObject *
2277PyFrozenSet_New(PyObject *iterable)
2278{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002279 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002280}
2281
Neal Norwitz8c49c822006-03-04 18:41:19 +00002282Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002283PySet_Size(PyObject *anyset)
2284{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002285 if (!PyAnySet_Check(anyset)) {
2286 PyErr_BadInternalCall();
2287 return -1;
2288 }
2289 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002290}
2291
2292int
Barry Warsaw176014f2006-03-30 22:45:35 +00002293PySet_Clear(PyObject *set)
2294{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002295 if (!PySet_Check(set)) {
2296 PyErr_BadInternalCall();
2297 return -1;
2298 }
2299 return set_clear_internal((PySetObject *)set);
Barry Warsaw176014f2006-03-30 22:45:35 +00002300}
2301
2302int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002303PySet_Contains(PyObject *anyset, PyObject *key)
2304{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002305 if (!PyAnySet_Check(anyset)) {
2306 PyErr_BadInternalCall();
2307 return -1;
2308 }
2309 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002310}
2311
2312int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002313PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002314{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002315 if (!PySet_Check(set)) {
2316 PyErr_BadInternalCall();
2317 return -1;
2318 }
2319 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002320}
2321
2322int
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002323PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002324{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002325 if (!PySet_Check(anyset) &&
2326 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2327 PyErr_BadInternalCall();
2328 return -1;
2329 }
2330 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002331}
2332
Barry Warsaw176014f2006-03-30 22:45:35 +00002333int
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002334_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Barry Warsaw176014f2006-03-30 22:45:35 +00002335{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002336 setentry *entry_ptr;
Barry Warsaw176014f2006-03-30 22:45:35 +00002337
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002338 if (!PyAnySet_Check(set)) {
2339 PyErr_BadInternalCall();
2340 return -1;
2341 }
2342 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2343 return 0;
2344 *key = entry_ptr->key;
2345 return 1;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002346}
2347
2348int
2349_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2350{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002351 setentry *entry;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002352
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002353 if (!PyAnySet_Check(set)) {
2354 PyErr_BadInternalCall();
2355 return -1;
2356 }
2357 if (set_next((PySetObject *)set, pos, &entry) == 0)
2358 return 0;
2359 *key = entry->key;
2360 *hash = entry->hash;
2361 return 1;
Barry Warsaw176014f2006-03-30 22:45:35 +00002362}
2363
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002364PyObject *
2365PySet_Pop(PyObject *set)
2366{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002367 if (!PySet_Check(set)) {
2368 PyErr_BadInternalCall();
2369 return NULL;
2370 }
2371 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002372}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002373
Barry Warsaw176014f2006-03-30 22:45:35 +00002374int
2375_PySet_Update(PyObject *set, PyObject *iterable)
2376{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002377 if (!PySet_Check(set)) {
2378 PyErr_BadInternalCall();
2379 return -1;
2380 }
2381 return set_update_internal((PySetObject *)set, iterable);
Barry Warsaw176014f2006-03-30 22:45:35 +00002382}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002383
2384#ifdef Py_DEBUG
2385
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002386/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002387 Returns True and original set is restored. */
2388
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002389#define assertRaises(call_return_value, exception) \
2390 do { \
2391 assert(call_return_value); \
2392 assert(PyErr_ExceptionMatches(exception)); \
2393 PyErr_Clear(); \
2394 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002395
2396static PyObject *
2397test_c_api(PySetObject *so)
2398{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002399 Py_ssize_t count;
2400 char *s;
2401 Py_ssize_t i;
2402 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2403 PyObject *ob = (PyObject *)so;
2404 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002405
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002406 /* Verify preconditions */
2407 assert(PyAnySet_Check(ob));
2408 assert(PyAnySet_CheckExact(ob));
2409 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner17d90542010-03-13 00:13:22 +00002410
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002411 /* so.clear(); so |= set("abc"); */
2412 str = PyString_FromString("abc");
2413 if (str == NULL)
2414 return NULL;
2415 set_clear_internal(so);
2416 if (set_update_internal(so, str) == -1) {
2417 Py_DECREF(str);
2418 return NULL;
2419 }
2420 Py_DECREF(str);
Victor Stinner17d90542010-03-13 00:13:22 +00002421
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002422 /* Exercise type/size checks */
2423 assert(PySet_Size(ob) == 3);
2424 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002425
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002426 /* Raise TypeError for non-iterable constructor arguments */
2427 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2428 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002429
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002430 /* Raise TypeError for unhashable key */
2431 dup = PySet_New(ob);
2432 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2433 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2434 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002435
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002436 /* Exercise successful pop, contains, add, and discard */
2437 elem = PySet_Pop(ob);
2438 assert(PySet_Contains(ob, elem) == 0);
2439 assert(PySet_GET_SIZE(ob) == 2);
2440 assert(PySet_Add(ob, elem) == 0);
2441 assert(PySet_Contains(ob, elem) == 1);
2442 assert(PySet_GET_SIZE(ob) == 3);
2443 assert(PySet_Discard(ob, elem) == 1);
2444 assert(PySet_GET_SIZE(ob) == 2);
2445 assert(PySet_Discard(ob, elem) == 0);
2446 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002447
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002448 /* Exercise clear */
2449 dup2 = PySet_New(dup);
2450 assert(PySet_Clear(dup2) == 0);
2451 assert(PySet_Size(dup2) == 0);
2452 Py_DECREF(dup2);
Barry Warsaw176014f2006-03-30 22:45:35 +00002453
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002454 /* Raise SystemError on clear or update of frozen set */
2455 f = PyFrozenSet_New(dup);
2456 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2457 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2458 assert(PySet_Add(f, elem) == 0);
2459 Py_INCREF(f);
2460 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2461 Py_DECREF(f);
2462 Py_DECREF(f);
Barry Warsaw176014f2006-03-30 22:45:35 +00002463
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002464 /* Exercise direct iteration */
2465 i = 0, count = 0;
2466 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2467 s = PyString_AsString(x);
2468 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2469 count++;
2470 }
2471 assert(count == 3);
Barry Warsaw176014f2006-03-30 22:45:35 +00002472
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002473 /* Exercise updates */
2474 dup2 = PySet_New(NULL);
2475 assert(_PySet_Update(dup2, dup) == 0);
2476 assert(PySet_Size(dup2) == 3);
2477 assert(_PySet_Update(dup2, dup) == 0);
2478 assert(PySet_Size(dup2) == 3);
2479 Py_DECREF(dup2);
Barry Warsaw176014f2006-03-30 22:45:35 +00002480
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002481 /* Raise SystemError when self argument is not a set or frozenset. */
2482 t = PyTuple_New(0);
2483 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2484 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2485 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002486
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002487 /* Raise SystemError when self argument is not a set. */
2488 f = PyFrozenSet_New(dup);
2489 assert(PySet_Size(f) == 3);
2490 assert(PyFrozenSet_CheckExact(f));
2491 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2492 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2493 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002494
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002495 /* Raise KeyError when popping from an empty set */
2496 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2497 Py_DECREF(ob);
2498 assert(PySet_GET_SIZE(ob) == 0);
2499 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002500
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002501 /* Restore the set from the copy using the PyNumber API */
2502 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2503 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002504
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002505 /* Verify constructors accept NULL arguments */
2506 f = PySet_New(NULL);
2507 assert(f != NULL);
2508 assert(PySet_GET_SIZE(f) == 0);
2509 Py_DECREF(f);
2510 f = PyFrozenSet_New(NULL);
2511 assert(f != NULL);
2512 assert(PyFrozenSet_CheckExact(f));
2513 assert(PySet_GET_SIZE(f) == 0);
2514 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002515
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002516 Py_DECREF(elem);
2517 Py_DECREF(dup);
2518 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002519}
2520
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002521#undef assertRaises
2522
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002523#endif