blob: b35fef5ed6724541725ef2caaab5582943a5b8d7 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
Raymond Hettinger2212e522008-11-30 23:43:36 +00006 Copyright (c) 2003-2008 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"
Christian Heimes0ded5b52007-12-10 15:50:56 +000012#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
Thomas Wouters89f507f2006-12-13 04:49:30 +000026}
27
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028/* This must be >= 1. */
29#define PERTURB_SHIFT 5
30
31/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000032static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#ifdef Py_REF_DEBUG
35PyObject *
36_PySet_Dummy(void)
37{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039}
40#endif
41
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042#define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
45 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000046 } while(0)
47
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
51 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052 } while(0)
53
Raymond Hettingerbc841a12005-08-07 13:02:53 +000054/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000055#ifndef PySet_MAXFREELIST
56#define PySet_MAXFREELIST 80
57#endif
58static PySetObject *free_list[PySet_MAXFREELIST];
59static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060
Christian Heimes0ded5b52007-12-10 15:50:56 +000061
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062/*
63The basic lookup function used by all operations.
64This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65Open addressing is preferred over chaining since the link overhead for
66chaining would be substantial (100% with typical malloc overhead).
67
68The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000069probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070
71All arithmetic on hash should ignore overflow.
72
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000073Unlike the dictionary implementation, the lookkey functions can return
74NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075*/
76
77static setentry *
78set_lookkey(PySetObject *so, PyObject *key, register long hash)
79{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 register Py_ssize_t i;
81 register size_t perturb;
82 register setentry *freeslot;
83 register size_t mask = so->mask;
84 setentry *table = so->table;
85 register setentry *entry;
86 register int cmp;
87 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 i = hash & mask;
90 entry = &table[i];
91 if (entry->key == NULL || entry->key == key)
92 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 if (entry->key == dummy)
95 freeslot = entry;
96 else {
97 if (entry->hash == hash) {
98 startkey = entry->key;
99 Py_INCREF(startkey);
100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
101 Py_DECREF(startkey);
102 if (cmp < 0)
103 return NULL;
104 if (table == so->table && entry->key == startkey) {
105 if (cmp > 0)
106 return entry;
107 }
108 else {
109 /* The compare did major nasty stuff to the
110 * set: start over.
111 */
112 return set_lookkey(so, key, hash);
113 }
114 }
115 freeslot = NULL;
116 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /* In the loop, key == dummy is by far (factor of 100s) the
119 least likely outcome, so test for that last. */
120 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
121 i = (i << 2) + i + perturb + 1;
122 entry = &table[i & mask];
123 if (entry->key == NULL) {
124 if (freeslot != NULL)
125 entry = freeslot;
126 break;
127 }
128 if (entry->key == key)
129 break;
130 if (entry->hash == hash && entry->key != dummy) {
131 startkey = entry->key;
132 Py_INCREF(startkey);
133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
134 Py_DECREF(startkey);
135 if (cmp < 0)
136 return NULL;
137 if (table == so->table && entry->key == startkey) {
138 if (cmp > 0)
139 break;
140 }
141 else {
142 /* The compare did major nasty stuff to the
143 * set: start over.
144 */
145 return set_lookkey(so, key, hash);
146 }
147 }
148 else if (entry->key == dummy && freeslot == NULL)
149 freeslot = entry;
150 }
151 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152}
153
154/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000155 * Hacked up version of set_lookkey which can assume keys are always unicode;
156 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000157 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158 */
159static setentry *
Christian Heimes0ded5b52007-12-10 15:50:56 +0000160set_lookkey_unicode(PySetObject *so, PyObject *key, register long hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 register Py_ssize_t i;
163 register size_t perturb;
164 register setentry *freeslot;
165 register size_t mask = so->mask;
166 setentry *table = so->table;
167 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 /* Make sure this function doesn't have to handle non-unicode keys,
170 including subclasses of str; e.g., one reason to subclass
171 strings is to override __eq__, and for speed we don't cater to
172 that here. */
173 if (!PyUnicode_CheckExact(key)) {
174 so->lookup = set_lookkey;
175 return set_lookkey(so, key, hash);
176 }
177 i = hash & mask;
178 entry = &table[i];
179 if (entry->key == NULL || entry->key == key)
180 return entry;
181 if (entry->key == dummy)
182 freeslot = entry;
183 else {
184 if (entry->hash == hash && unicode_eq(entry->key, key))
185 return entry;
186 freeslot = NULL;
187 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 /* In the loop, key == dummy is by far (factor of 100s) the
190 least likely outcome, so test for that last. */
191 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
192 i = (i << 2) + i + perturb + 1;
193 entry = &table[i & mask];
194 if (entry->key == NULL)
195 return freeslot == NULL ? entry : freeslot;
196 if (entry->key == key
197 || (entry->hash == hash
198 && entry->key != dummy
199 && unicode_eq(entry->key, key)))
200 return entry;
201 if (entry->key == dummy && freeslot == NULL)
202 freeslot = entry;
203 }
204 assert(0); /* NOT REACHED */
205 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206}
207
208/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211Eats a reference to key.
212*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000214set_insert_key(register PySetObject *so, PyObject *key, long hash)
215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 register setentry *entry;
217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 assert(so->lookup != NULL);
220 entry = so->lookup(so, key, hash);
221 if (entry == NULL)
222 return -1;
223 if (entry->key == NULL) {
224 /* UNUSED */
225 so->fill++;
226 entry->key = key;
227 entry->hash = hash;
228 so->used++;
229 } else if (entry->key == dummy) {
230 /* DUMMY */
231 entry->key = key;
232 entry->hash = hash;
233 so->used++;
234 Py_DECREF(dummy);
235 } else {
236 /* ACTIVE */
237 Py_DECREF(key);
238 }
239 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000240}
241
242/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000243Internal routine used by set_table_resize() to insert an item which is
244known to be absent from the set. This routine also assumes that
245the set contains no deleted entries. Besides the performance benefit,
246using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
247Note that no refcounts are changed by this routine; if needed, the caller
248is responsible for incref'ing `key`.
249*/
250static void
251set_insert_clean(register PySetObject *so, PyObject *key, long hash)
252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 register size_t i;
254 register size_t perturb;
255 register size_t mask = (size_t)so->mask;
256 setentry *table = so->table;
257 register setentry *entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 i = hash & mask;
260 entry = &table[i];
261 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
262 i = (i << 2) + i + perturb + 1;
263 entry = &table[i & mask];
264 }
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000269}
270
271/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000273keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274actually be smaller than the old one.
275*/
276static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 Py_ssize_t newsize;
280 setentry *oldtable, *newtable, *entry;
281 Py_ssize_t i;
282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Find the smallest table size > minused. */
288 for (newsize = PySet_MINSIZE;
289 newsize <= minused && newsize > 0;
290 newsize <<= 1)
291 ;
292 if (newsize <= 0) {
293 PyErr_NoMemory();
294 return -1;
295 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL);
300 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (newsize == PySet_MINSIZE) {
303 /* A large table is shrinking, or we can't get any smaller. */
304 newtable = so->smalltable;
305 if (newtable == oldtable) {
306 if (so->fill == so->used) {
307 /* No dummies, so no point doing anything. */
308 return 0;
309 }
310 /* We're not going to resize it, but rebuild the
311 table anyway to purge old dummy entries.
312 Subtle: This is *necessary* if fill==size,
313 as set_lookkey needs at least one virgin slot to
314 terminate failing searches. If fill < size, it's
315 merely desirable, as dummies slow searches. */
316 assert(so->fill > so->used);
317 memcpy(small_copy, oldtable, sizeof(small_copy));
318 oldtable = small_copy;
319 }
320 }
321 else {
322 newtable = PyMem_NEW(setentry, newsize);
323 if (newtable == NULL) {
324 PyErr_NoMemory();
325 return -1;
326 }
327 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* Make the set empty, using the new table. */
330 assert(newtable != oldtable);
331 so->table = newtable;
332 so->mask = newsize - 1;
333 memset(newtable, 0, sizeof(setentry) * newsize);
334 so->used = 0;
335 i = so->fill;
336 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
340 for (entry = oldtable; i > 0; entry++) {
341 if (entry->key == NULL) {
342 /* UNUSED */
343 ;
344 } else if (entry->key == dummy) {
345 /* DUMMY */
346 --i;
347 assert(entry->key == dummy);
348 Py_DECREF(entry->key);
349 } else {
350 /* ACTIVE */
351 --i;
352 set_insert_clean(so, entry->key, entry->hash);
353 }
354 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (is_oldtable_malloced)
357 PyMem_DEL(oldtable);
358 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000359}
360
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
362
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000364set_add_entry(register PySetObject *so, setentry *entry)
365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 assert(so->fill <= so->mask); /* at least one empty slot */
369 n_used = so->used;
370 Py_INCREF(entry->key);
371 if (set_insert_key(so, entry->key, entry->hash) == -1) {
372 Py_DECREF(entry->key);
373 return -1;
374 }
375 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
376 return 0;
377 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000378}
379
380static int
381set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 register long hash;
384 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (!PyUnicode_CheckExact(key) ||
387 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
388 hash = PyObject_Hash(key);
389 if (hash == -1)
390 return -1;
391 }
392 assert(so->fill <= so->mask); /* at least one empty slot */
393 n_used = so->used;
394 Py_INCREF(key);
395 if (set_insert_key(so, key, hash) == -1) {
396 Py_DECREF(key);
397 return -1;
398 }
399 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
400 return 0;
401 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402}
403
404#define DISCARD_NOTFOUND 0
405#define DISCARD_FOUND 1
406
407static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000408set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409{ register setentry *entry;
410 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
413 if (entry == NULL)
414 return -1;
415 if (entry->key == NULL || entry->key == dummy)
416 return DISCARD_NOTFOUND;
417 old_key = entry->key;
418 Py_INCREF(dummy);
419 entry->key = dummy;
420 so->used--;
421 Py_DECREF(old_key);
422 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000423}
424
425static int
426set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 register long hash;
429 register setentry *entry;
430 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (!PyUnicode_CheckExact(key) ||
435 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
436 hash = PyObject_Hash(key);
437 if (hash == -1)
438 return -1;
439 }
440 entry = (so->lookup)(so, key, hash);
441 if (entry == NULL)
442 return -1;
443 if (entry->key == NULL || entry->key == dummy)
444 return DISCARD_NOTFOUND;
445 old_key = entry->key;
446 Py_INCREF(dummy);
447 entry->key = dummy;
448 so->used--;
449 Py_DECREF(old_key);
450 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451}
452
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000453static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454set_clear_internal(PySetObject *so)
455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 setentry *entry, *table;
457 int table_is_malloced;
458 Py_ssize_t fill;
459 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000460#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 Py_ssize_t i, n;
462 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 n = so->mask + 1;
465 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000466#endif
467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 table = so->table;
469 assert(table != NULL);
470 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 /* This is delicate. During the process of clearing the set,
473 * decrefs can cause the set to mutate. To avoid fatal confusion
474 * (voice of experience), we have to make the set empty before
475 * clearing the slots, and never refer to anything via so->ref while
476 * clearing.
477 */
478 fill = so->fill;
479 if (table_is_malloced)
480 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 else if (fill > 0) {
483 /* It's a small table with something that needs to be cleared.
484 * Afraid the only safe way is to copy the set entries into
485 * another small table first.
486 */
487 memcpy(small_copy, table, sizeof(small_copy));
488 table = small_copy;
489 EMPTY_TO_MINSIZE(so);
490 }
491 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 /* Now we can finally clear things. If C had refcounts, we could
494 * assert that the refcount on table is 1 now, i.e. that this function
495 * has unique access to it, so decref side-effects can't alter it.
496 */
497 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 assert(i < n);
500 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (entry->key) {
503 --fill;
504 Py_DECREF(entry->key);
505 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 else
508 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (table_is_malloced)
513 PyMem_DEL(table);
514 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515}
516
517/*
518 * Iterate over a set table. Use like so:
519 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000520 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000521 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000522 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000523 * while (set_next(yourset, &pos, &entry)) {
524 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525 * }
526 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000527 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529 */
530static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000531set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 Py_ssize_t i;
534 Py_ssize_t mask;
535 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 assert (PyAnySet_Check(so));
538 i = *pos_ptr;
539 assert(i >= 0);
540 table = so->table;
541 mask = so->mask;
542 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
543 i++;
544 *pos_ptr = i+1;
545 if (i > mask)
546 return 0;
547 assert(table[i].key != NULL);
548 *entry_ptr = &table[i];
549 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000550}
551
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000552static void
553set_dealloc(PySetObject *so)
554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 register setentry *entry;
556 Py_ssize_t fill = so->fill;
557 PyObject_GC_UnTrack(so);
558 Py_TRASHCAN_SAFE_BEGIN(so)
559 if (so->weakreflist != NULL)
560 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 for (entry = so->table; fill > 0; entry++) {
563 if (entry->key) {
564 --fill;
565 Py_DECREF(entry->key);
566 }
567 }
568 if (so->table != so->smalltable)
569 PyMem_DEL(so->table);
570 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
571 free_list[numfree++] = so;
572 else
573 Py_TYPE(so)->tp_free(so);
574 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000575}
576
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000577static PyObject *
578set_repr(PySetObject *so)
579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 PyObject *keys, *result=NULL;
581 Py_UNICODE *u;
582 int status = Py_ReprEnter((PyObject*)so);
583 PyObject *listrepr;
584 Py_ssize_t newsize;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (status != 0) {
587 if (status < 0)
588 return NULL;
589 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
590 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 /* shortcut for the empty set */
593 if (!so->used) {
594 Py_ReprLeave((PyObject*)so);
595 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
596 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 keys = PySequence_List((PyObject *)so);
599 if (keys == NULL)
600 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 listrepr = PyObject_Repr(keys);
603 Py_DECREF(keys);
604 if (listrepr == NULL)
605 goto done;
606 newsize = PyUnicode_GET_SIZE(listrepr);
607 result = PyUnicode_FromUnicode(NULL, newsize);
608 if (result) {
609 u = PyUnicode_AS_UNICODE(result);
610 *u++ = '{';
611 /* Omit the brackets from the listrepr */
612 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
613 PyUnicode_GET_SIZE(listrepr)-2);
614 u += newsize-2;
615 *u++ = '}';
616 }
617 Py_DECREF(listrepr);
618 if (Py_TYPE(so) != &PySet_Type) {
619 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
620 Py_TYPE(so)->tp_name,
621 result);
622 Py_DECREF(result);
623 result = tmp;
624 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000625done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 Py_ReprLeave((PyObject*)so);
627 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000628}
629
Martin v. Löwis18e16552006-02-15 17:27:45 +0000630static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000631set_len(PyObject *so)
632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000634}
635
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000636static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000637set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PySetObject *other;
640 register Py_ssize_t i;
641 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 assert (PyAnySet_Check(so));
644 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 other = (PySetObject*)otherset;
647 if (other == so || other->used == 0)
648 /* a.update(a) or a.update({}); nothing to do */
649 return 0;
650 /* Do one big resize at the start, rather than
651 * incrementally resizing as we insert new keys. Expect
652 * that there will be no (or few) overlapping keys.
653 */
654 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
655 if (set_table_resize(so, (so->used + other->used)*2) != 0)
656 return -1;
657 }
658 for (i = 0; i <= other->mask; i++) {
659 entry = &other->table[i];
660 if (entry->key != NULL &&
661 entry->key != dummy) {
662 Py_INCREF(entry->key);
663 if (set_insert_key(so, entry->key, entry->hash) == -1) {
664 Py_DECREF(entry->key);
665 return -1;
666 }
667 }
668 }
669 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670}
671
672static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000673set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 long hash;
676 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (!PyUnicode_CheckExact(key) ||
679 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
680 hash = PyObject_Hash(key);
681 if (hash == -1)
682 return -1;
683 }
684 entry = (so->lookup)(so, key, hash);
685 if (entry == NULL)
686 return -1;
687 key = entry->key;
688 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000689}
690
Raymond Hettingerc991db22005-08-11 07:58:45 +0000691static int
692set_contains_entry(PySetObject *so, setentry *entry)
693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 PyObject *key;
695 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 lu_entry = (so->lookup)(so, entry->key, entry->hash);
698 if (lu_entry == NULL)
699 return -1;
700 key = lu_entry->key;
701 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000702}
703
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000704static PyObject *
705set_pop(PySetObject *so)
706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 register Py_ssize_t i = 0;
708 register setentry *entry;
709 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 assert (PyAnySet_Check(so));
712 if (so->used == 0) {
713 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
714 return NULL;
715 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 /* Set entry to "the first" unused or dummy set entry. We abuse
718 * the hash field of slot 0 to hold a search finger:
719 * If slot 0 has a value, use slot 0.
720 * Else slot 0 is being used to hold a search finger,
721 * and we use its hash value as the first index to look.
722 */
723 entry = &so->table[0];
724 if (entry->key == NULL || entry->key == dummy) {
725 i = entry->hash;
726 /* The hash field may be a real hash value, or it may be a
727 * legit search finger, or it may be a once-legit search
728 * finger that's out of bounds now because it wrapped around
729 * or the table shrunk -- simply make sure it's in bounds now.
730 */
731 if (i > so->mask || i < 1)
732 i = 1; /* skip slot 0 */
733 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
734 i++;
735 if (i > so->mask)
736 i = 1;
737 }
738 }
739 key = entry->key;
740 Py_INCREF(dummy);
741 entry->key = dummy;
742 so->used--;
743 so->table[0].hash = i + 1; /* next place to start */
744 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000745}
746
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000747PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
748Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000749
750static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000751set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 Py_ssize_t pos = 0;
754 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 while (set_next(so, &pos, &entry))
757 Py_VISIT(entry->key);
758 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000759}
760
761static long
762frozenset_hash(PyObject *self)
763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 PySetObject *so = (PySetObject *)self;
765 long h, hash = 1927868237L;
766 setentry *entry;
767 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (so->hash != -1)
770 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 hash *= PySet_GET_SIZE(self) + 1;
773 while (set_next(so, &pos, &entry)) {
774 /* Work to increase the bit dispersion for closely spaced hash
775 values. The is important because some use cases have many
776 combinations of a small number of elements with nearby
777 hashes so that many distinct combinations collapse to only
778 a handful of distinct hash values. */
779 h = entry->hash;
780 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
781 }
782 hash = hash * 69069L + 907133923L;
783 if (hash == -1)
784 hash = 590923713L;
785 so->hash = hash;
786 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000787}
788
Raymond Hettingera9d99362005-08-05 00:01:15 +0000789/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PyObject_HEAD
793 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
794 Py_ssize_t si_used;
795 Py_ssize_t si_pos;
796 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797} setiterobject;
798
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799static void
800setiter_dealloc(setiterobject *si)
801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 Py_XDECREF(si->si_set);
803 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000804}
805
806static int
807setiter_traverse(setiterobject *si, visitproc visit, void *arg)
808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 Py_VISIT(si->si_set);
810 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811}
812
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000813static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000814setiter_len(setiterobject *si)
815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 Py_ssize_t len = 0;
817 if (si->si_set != NULL && si->si_used == si->si_set->used)
818 len = si->len;
819 return PyLong_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000820}
821
Armin Rigof5b3e362006-02-11 21:32:43 +0000822PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000823
824static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
826 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000827};
828
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000829static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 PyObject *key;
832 register Py_ssize_t i, mask;
833 register setentry *entry;
834 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 if (so == NULL)
837 return NULL;
838 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 if (si->si_used != so->used) {
841 PyErr_SetString(PyExc_RuntimeError,
842 "Set changed size during iteration");
843 si->si_used = -1; /* Make this state sticky */
844 return NULL;
845 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 i = si->si_pos;
848 assert(i>=0);
849 entry = so->table;
850 mask = so->mask;
851 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
852 i++;
853 si->si_pos = i+1;
854 if (i > mask)
855 goto fail;
856 si->len--;
857 key = entry[i].key;
858 Py_INCREF(key);
859 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000860
861fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 Py_DECREF(so);
863 si->si_set = NULL;
864 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000865}
866
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000867PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 PyVarObject_HEAD_INIT(&PyType_Type, 0)
869 "set_iterator", /* tp_name */
870 sizeof(setiterobject), /* tp_basicsize */
871 0, /* tp_itemsize */
872 /* methods */
873 (destructor)setiter_dealloc, /* tp_dealloc */
874 0, /* tp_print */
875 0, /* tp_getattr */
876 0, /* tp_setattr */
877 0, /* tp_reserved */
878 0, /* tp_repr */
879 0, /* tp_as_number */
880 0, /* tp_as_sequence */
881 0, /* tp_as_mapping */
882 0, /* tp_hash */
883 0, /* tp_call */
884 0, /* tp_str */
885 PyObject_GenericGetAttr, /* tp_getattro */
886 0, /* tp_setattro */
887 0, /* tp_as_buffer */
888 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
889 0, /* tp_doc */
890 (traverseproc)setiter_traverse, /* tp_traverse */
891 0, /* tp_clear */
892 0, /* tp_richcompare */
893 0, /* tp_weaklistoffset */
894 PyObject_SelfIter, /* tp_iter */
895 (iternextfunc)setiter_iternext, /* tp_iternext */
896 setiter_methods, /* tp_methods */
897 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000898};
899
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000900static PyObject *
901set_iter(PySetObject *so)
902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
904 if (si == NULL)
905 return NULL;
906 Py_INCREF(so);
907 si->si_set = so;
908 si->si_used = so->used;
909 si->si_pos = 0;
910 si->len = so->used;
911 _PyObject_GC_TRACK(si);
912 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000913}
914
Raymond Hettingerd7946662005-08-01 21:39:29 +0000915static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000916set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (PyAnySet_Check(other))
921 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 if (PyDict_CheckExact(other)) {
924 PyObject *value;
925 Py_ssize_t pos = 0;
926 long hash;
927 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 /* Do one big resize at the start, rather than
930 * incrementally resizing as we insert new keys. Expect
931 * that there will be no (or few) overlapping keys.
932 */
933 if (dictsize == -1)
934 return -1;
935 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
936 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
937 return -1;
938 }
939 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
940 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 an_entry.hash = hash;
943 an_entry.key = key;
944 if (set_add_entry(so, &an_entry) == -1)
945 return -1;
946 }
947 return 0;
948 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 it = PyObject_GetIter(other);
951 if (it == NULL)
952 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 while ((key = PyIter_Next(it)) != NULL) {
955 if (set_add_key(so, key) == -1) {
956 Py_DECREF(it);
957 Py_DECREF(key);
958 return -1;
959 }
960 Py_DECREF(key);
961 }
962 Py_DECREF(it);
963 if (PyErr_Occurred())
964 return -1;
965 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000966}
967
968static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000969set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
974 PyObject *other = PyTuple_GET_ITEM(args, i);
975 if (set_update_internal(so, other) == -1)
976 return NULL;
977 }
978 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000979}
980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000982"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000983
984static PyObject *
985make_new_set(PyTypeObject *type, PyObject *iterable)
986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 if (dummy == NULL) { /* Auto-initialize dummy */
990 dummy = PyUnicode_FromString("<dummy key>");
991 if (dummy == NULL)
992 return NULL;
993 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 /* create PySetObject structure */
996 if (numfree &&
997 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
998 so = free_list[--numfree];
999 assert (so != NULL && PyAnySet_CheckExact(so));
1000 Py_TYPE(so) = type;
1001 _Py_NewReference((PyObject *)so);
1002 EMPTY_TO_MINSIZE(so);
1003 PyObject_GC_Track(so);
1004 } else {
1005 so = (PySetObject *)type->tp_alloc(type, 0);
1006 if (so == NULL)
1007 return NULL;
1008 /* tp_alloc has already zeroed the structure */
1009 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1010 INIT_NONZERO_SET_SLOTS(so);
1011 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 so->lookup = set_lookkey_unicode;
1014 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 if (iterable != NULL) {
1017 if (set_update_internal(so, iterable) == -1) {
1018 Py_DECREF(so);
1019 return NULL;
1020 }
1021 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001024}
1025
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001026static PyObject *
1027make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1030 if (PyType_IsSubtype(type, &PySet_Type))
1031 type = &PySet_Type;
1032 else
1033 type = &PyFrozenSet_Type;
1034 }
1035 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001036}
1037
Raymond Hettingerd7946662005-08-01 21:39:29 +00001038/* The empty frozenset is a singleton */
1039static PyObject *emptyfrozenset = NULL;
1040
Raymond Hettingera690a992003-11-16 16:17:49 +00001041static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001042frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1047 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1050 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (type != &PyFrozenSet_Type)
1053 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 if (iterable != NULL) {
1056 /* frozenset(f) is idempotent */
1057 if (PyFrozenSet_CheckExact(iterable)) {
1058 Py_INCREF(iterable);
1059 return iterable;
1060 }
1061 result = make_new_set(type, iterable);
1062 if (result == NULL || PySet_GET_SIZE(result))
1063 return result;
1064 Py_DECREF(result);
1065 }
1066 /* The empty frozenset is a singleton */
1067 if (emptyfrozenset == NULL)
1068 emptyfrozenset = make_new_set(type, NULL);
1069 Py_XINCREF(emptyfrozenset);
1070 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001071}
1072
1073void
1074PySet_Fini(void)
1075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 while (numfree) {
1079 numfree--;
1080 so = free_list[numfree];
1081 PyObject_GC_Del(so);
1082 }
1083 Py_CLEAR(dummy);
1084 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001085}
1086
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001087static PyObject *
1088set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1091 return NULL;
1092
1093 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001094}
1095
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001096/* set_swap_bodies() switches the contents of any two sets by moving their
1097 internal data pointers and, if needed, copying the internal smalltables.
1098 Semantically equivalent to:
1099
1100 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1101
1102 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001104 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001106 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001107*/
1108
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001109static void
1110set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 Py_ssize_t t;
1113 setentry *u;
1114 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1115 setentry tab[PySet_MINSIZE];
1116 long h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 t = a->fill; a->fill = b->fill; b->fill = t;
1119 t = a->used; a->used = b->used; b->used = t;
1120 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 u = a->table;
1123 if (a->table == a->smalltable)
1124 u = b->smalltable;
1125 a->table = b->table;
1126 if (b->table == b->smalltable)
1127 a->table = a->smalltable;
1128 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 if (a->table == a->smalltable || b->table == b->smalltable) {
1133 memcpy(tab, a->smalltable, sizeof(tab));
1134 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1135 memcpy(b->smalltable, tab, sizeof(tab));
1136 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1139 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1140 h = a->hash; a->hash = b->hash; b->hash = h;
1141 } else {
1142 a->hash = -1;
1143 b->hash = -1;
1144 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001145}
1146
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001147static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001148set_copy(PySetObject *so)
1149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001151}
1152
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001153static PyObject *
1154frozenset_copy(PySetObject *so)
1155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (PyFrozenSet_CheckExact(so)) {
1157 Py_INCREF(so);
1158 return (PyObject *)so;
1159 }
1160 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001161}
1162
Raymond Hettingera690a992003-11-16 16:17:49 +00001163PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1164
1165static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001166set_clear(PySetObject *so)
1167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 set_clear_internal(so);
1169 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001170}
1171
1172PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1173
1174static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001175set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 PySetObject *result;
1178 PyObject *other;
1179 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 result = (PySetObject *)set_copy(so);
1182 if (result == NULL)
1183 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1186 other = PyTuple_GET_ITEM(args, i);
1187 if ((PyObject *)so == other)
1188 continue;
1189 if (set_update_internal(result, other) == -1) {
1190 Py_DECREF(result);
1191 return NULL;
1192 }
1193 }
1194 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001195}
1196
1197PyDoc_STRVAR(union_doc,
1198 "Return the union of sets as a new set.\n\
1199\n\
1200(i.e. all elements that are in either set.)");
1201
1202static PyObject *
1203set_or(PySetObject *so, PyObject *other)
1204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1208 Py_INCREF(Py_NotImplemented);
1209 return Py_NotImplemented;
1210 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 result = (PySetObject *)set_copy(so);
1213 if (result == NULL)
1214 return NULL;
1215 if ((PyObject *)so == other)
1216 return (PyObject *)result;
1217 if (set_update_internal(result, other) == -1) {
1218 Py_DECREF(result);
1219 return NULL;
1220 }
1221 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001222}
1223
Raymond Hettingera690a992003-11-16 16:17:49 +00001224static PyObject *
1225set_ior(PySetObject *so, PyObject *other)
1226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 if (!PyAnySet_Check(other)) {
1228 Py_INCREF(Py_NotImplemented);
1229 return Py_NotImplemented;
1230 }
1231 if (set_update_internal(so, other) == -1)
1232 return NULL;
1233 Py_INCREF(so);
1234 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001235}
1236
1237static PyObject *
1238set_intersection(PySetObject *so, PyObject *other)
1239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 PySetObject *result;
1241 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 if ((PyObject *)so == other)
1244 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1247 if (result == NULL)
1248 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (PyAnySet_Check(other)) {
1251 Py_ssize_t pos = 0;
1252 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1255 tmp = (PyObject *)so;
1256 so = (PySetObject *)other;
1257 other = tmp;
1258 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 while (set_next((PySetObject *)other, &pos, &entry)) {
1261 int rv = set_contains_entry(so, entry);
1262 if (rv == -1) {
1263 Py_DECREF(result);
1264 return NULL;
1265 }
1266 if (rv) {
1267 if (set_add_entry(result, entry) == -1) {
1268 Py_DECREF(result);
1269 return NULL;
1270 }
1271 }
1272 }
1273 return (PyObject *)result;
1274 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 it = PyObject_GetIter(other);
1277 if (it == NULL) {
1278 Py_DECREF(result);
1279 return NULL;
1280 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 while ((key = PyIter_Next(it)) != NULL) {
1283 int rv;
1284 setentry entry;
1285 long hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (hash == -1) {
1288 Py_DECREF(it);
1289 Py_DECREF(result);
1290 Py_DECREF(key);
1291 return NULL;
1292 }
1293 entry.hash = hash;
1294 entry.key = key;
1295 rv = set_contains_entry(so, &entry);
1296 if (rv == -1) {
1297 Py_DECREF(it);
1298 Py_DECREF(result);
1299 Py_DECREF(key);
1300 return NULL;
1301 }
1302 if (rv) {
1303 if (set_add_entry(result, &entry) == -1) {
1304 Py_DECREF(it);
1305 Py_DECREF(result);
1306 Py_DECREF(key);
1307 return NULL;
1308 }
1309 }
1310 Py_DECREF(key);
1311 }
1312 Py_DECREF(it);
1313 if (PyErr_Occurred()) {
1314 Py_DECREF(result);
1315 return NULL;
1316 }
1317 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001318}
1319
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001320static PyObject *
1321set_intersection_multi(PySetObject *so, PyObject *args)
1322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 Py_ssize_t i;
1324 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 if (PyTuple_GET_SIZE(args) == 0)
1327 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 Py_INCREF(so);
1330 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1331 PyObject *other = PyTuple_GET_ITEM(args, i);
1332 PyObject *newresult = set_intersection((PySetObject *)result, other);
1333 if (newresult == NULL) {
1334 Py_DECREF(result);
1335 return NULL;
1336 }
1337 Py_DECREF(result);
1338 result = newresult;
1339 }
1340 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001341}
1342
Raymond Hettingera690a992003-11-16 16:17:49 +00001343PyDoc_STRVAR(intersection_doc,
1344"Return the intersection of two sets as a new set.\n\
1345\n\
1346(i.e. all elements that are in both sets.)");
1347
1348static PyObject *
1349set_intersection_update(PySetObject *so, PyObject *other)
1350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 tmp = set_intersection(so, other);
1354 if (tmp == NULL)
1355 return NULL;
1356 set_swap_bodies(so, (PySetObject *)tmp);
1357 Py_DECREF(tmp);
1358 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001359}
1360
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001361static PyObject *
1362set_intersection_update_multi(PySetObject *so, PyObject *args)
1363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 tmp = set_intersection_multi(so, args);
1367 if (tmp == NULL)
1368 return NULL;
1369 set_swap_bodies(so, (PySetObject *)tmp);
1370 Py_DECREF(tmp);
1371 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001372}
1373
Raymond Hettingera690a992003-11-16 16:17:49 +00001374PyDoc_STRVAR(intersection_update_doc,
1375"Update a set with the intersection of itself and another.");
1376
1377static PyObject *
1378set_and(PySetObject *so, PyObject *other)
1379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1381 Py_INCREF(Py_NotImplemented);
1382 return Py_NotImplemented;
1383 }
1384 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001385}
1386
1387static PyObject *
1388set_iand(PySetObject *so, PyObject *other)
1389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 if (!PyAnySet_Check(other)) {
1393 Py_INCREF(Py_NotImplemented);
1394 return Py_NotImplemented;
1395 }
1396 result = set_intersection_update(so, other);
1397 if (result == NULL)
1398 return NULL;
1399 Py_DECREF(result);
1400 Py_INCREF(so);
1401 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001402}
1403
Guido van Rossum58da9312007-11-10 23:39:45 +00001404static PyObject *
1405set_isdisjoint(PySetObject *so, PyObject *other)
1406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 if ((PyObject *)so == other) {
1410 if (PySet_GET_SIZE(so) == 0)
1411 Py_RETURN_TRUE;
1412 else
1413 Py_RETURN_FALSE;
1414 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if (PyAnySet_CheckExact(other)) {
1417 Py_ssize_t pos = 0;
1418 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1421 tmp = (PyObject *)so;
1422 so = (PySetObject *)other;
1423 other = tmp;
1424 }
1425 while (set_next((PySetObject *)other, &pos, &entry)) {
1426 int rv = set_contains_entry(so, entry);
1427 if (rv == -1)
1428 return NULL;
1429 if (rv)
1430 Py_RETURN_FALSE;
1431 }
1432 Py_RETURN_TRUE;
1433 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 it = PyObject_GetIter(other);
1436 if (it == NULL)
1437 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 while ((key = PyIter_Next(it)) != NULL) {
1440 int rv;
1441 setentry entry;
1442 long hash = PyObject_Hash(key);;
Guido van Rossum58da9312007-11-10 23:39:45 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 if (hash == -1) {
1445 Py_DECREF(key);
1446 Py_DECREF(it);
1447 return NULL;
1448 }
1449 entry.hash = hash;
1450 entry.key = key;
1451 rv = set_contains_entry(so, &entry);
1452 Py_DECREF(key);
1453 if (rv == -1) {
1454 Py_DECREF(it);
1455 return NULL;
1456 }
1457 if (rv) {
1458 Py_DECREF(it);
1459 Py_RETURN_FALSE;
1460 }
1461 }
1462 Py_DECREF(it);
1463 if (PyErr_Occurred())
1464 return NULL;
1465 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001466}
1467
1468PyDoc_STRVAR(isdisjoint_doc,
1469"Return True if two sets have a null intersection.");
1470
Neal Norwitz6576bd82005-11-13 18:41:28 +00001471static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001472set_difference_update_internal(PySetObject *so, PyObject *other)
1473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 if ((PyObject *)so == other)
1475 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (PyAnySet_Check(other)) {
1478 setentry *entry;
1479 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 while (set_next((PySetObject *)other, &pos, &entry))
1482 if (set_discard_entry(so, entry) == -1)
1483 return -1;
1484 } else {
1485 PyObject *key, *it;
1486 it = PyObject_GetIter(other);
1487 if (it == NULL)
1488 return -1;
1489
1490 while ((key = PyIter_Next(it)) != NULL) {
1491 if (set_discard_key(so, key) == -1) {
1492 Py_DECREF(it);
1493 Py_DECREF(key);
1494 return -1;
1495 }
1496 Py_DECREF(key);
1497 }
1498 Py_DECREF(it);
1499 if (PyErr_Occurred())
1500 return -1;
1501 }
1502 /* If more than 1/5 are dummies, then resize them away. */
1503 if ((so->fill - so->used) * 5 < so->mask)
1504 return 0;
1505 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001506}
1507
Raymond Hettingera690a992003-11-16 16:17:49 +00001508static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001509set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1514 PyObject *other = PyTuple_GET_ITEM(args, i);
1515 if (set_difference_update_internal(so, other) == -1)
1516 return NULL;
1517 }
1518 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001519}
1520
1521PyDoc_STRVAR(difference_update_doc,
1522"Remove all elements of another set from this set.");
1523
1524static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001525set_difference(PySetObject *so, PyObject *other)
1526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 PyObject *result;
1528 setentry *entry;
1529 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
1532 result = set_copy(so);
1533 if (result == NULL)
1534 return NULL;
1535 if (set_difference_update_internal((PySetObject *)result, other) != -1)
1536 return result;
1537 Py_DECREF(result);
1538 return NULL;
1539 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 result = make_new_set_basetype(Py_TYPE(so), NULL);
1542 if (result == NULL)
1543 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 if (PyDict_CheckExact(other)) {
1546 while (set_next(so, &pos, &entry)) {
1547 setentry entrycopy;
1548 entrycopy.hash = entry->hash;
1549 entrycopy.key = entry->key;
1550 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
1551 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1552 Py_DECREF(result);
1553 return NULL;
1554 }
1555 }
1556 }
1557 return result;
1558 }
1559
1560 while (set_next(so, &pos, &entry)) {
1561 int rv = set_contains_entry((PySetObject *)other, entry);
1562 if (rv == -1) {
1563 Py_DECREF(result);
1564 return NULL;
1565 }
1566 if (!rv) {
1567 if (set_add_entry((PySetObject *)result, entry) == -1) {
1568 Py_DECREF(result);
1569 return NULL;
1570 }
1571 }
1572 }
1573 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001574}
1575
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001576static PyObject *
1577set_difference_multi(PySetObject *so, PyObject *args)
1578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 Py_ssize_t i;
1580 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (PyTuple_GET_SIZE(args) == 0)
1583 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 other = PyTuple_GET_ITEM(args, 0);
1586 result = set_difference(so, other);
1587 if (result == NULL)
1588 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1591 other = PyTuple_GET_ITEM(args, i);
1592 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1593 Py_DECREF(result);
1594 return NULL;
1595 }
1596 }
1597 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001598}
1599
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001600PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001601"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001602\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001603(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001604static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001605set_sub(PySetObject *so, PyObject *other)
1606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1608 Py_INCREF(Py_NotImplemented);
1609 return Py_NotImplemented;
1610 }
1611 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001612}
1613
1614static PyObject *
1615set_isub(PySetObject *so, PyObject *other)
1616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 if (!PyAnySet_Check(other)) {
1618 Py_INCREF(Py_NotImplemented);
1619 return Py_NotImplemented;
1620 }
1621 if (set_difference_update_internal(so, other) == -1)
1622 return NULL;
1623 Py_INCREF(so);
1624 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001625}
1626
1627static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001628set_symmetric_difference_update(PySetObject *so, PyObject *other)
1629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 PySetObject *otherset;
1631 PyObject *key;
1632 Py_ssize_t pos = 0;
1633 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 if ((PyObject *)so == other)
1636 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if (PyDict_CheckExact(other)) {
1639 PyObject *value;
1640 int rv;
1641 long hash;
1642 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1643 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 an_entry.hash = hash;
1646 an_entry.key = key;
1647 rv = set_discard_entry(so, &an_entry);
1648 if (rv == -1)
1649 return NULL;
1650 if (rv == DISCARD_NOTFOUND) {
1651 if (set_add_entry(so, &an_entry) == -1)
1652 return NULL;
1653 }
1654 }
1655 Py_RETURN_NONE;
1656 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if (PyAnySet_Check(other)) {
1659 Py_INCREF(other);
1660 otherset = (PySetObject *)other;
1661 } else {
1662 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1663 if (otherset == NULL)
1664 return NULL;
1665 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 while (set_next(otherset, &pos, &entry)) {
1668 int rv = set_discard_entry(so, entry);
1669 if (rv == -1) {
1670 Py_DECREF(otherset);
1671 return NULL;
1672 }
1673 if (rv == DISCARD_NOTFOUND) {
1674 if (set_add_entry(so, entry) == -1) {
1675 Py_DECREF(otherset);
1676 return NULL;
1677 }
1678 }
1679 }
1680 Py_DECREF(otherset);
1681 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001682}
1683
1684PyDoc_STRVAR(symmetric_difference_update_doc,
1685"Update a set with the symmetric difference of itself and another.");
1686
1687static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001688set_symmetric_difference(PySetObject *so, PyObject *other)
1689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 PyObject *rv;
1691 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1694 if (otherset == NULL)
1695 return NULL;
1696 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1697 if (rv == NULL)
1698 return NULL;
1699 Py_DECREF(rv);
1700 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001701}
1702
1703PyDoc_STRVAR(symmetric_difference_doc,
1704"Return the symmetric difference of two sets as a new set.\n\
1705\n\
1706(i.e. all elements that are in exactly one of the sets.)");
1707
1708static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001709set_xor(PySetObject *so, PyObject *other)
1710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1712 Py_INCREF(Py_NotImplemented);
1713 return Py_NotImplemented;
1714 }
1715 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001716}
1717
1718static PyObject *
1719set_ixor(PySetObject *so, PyObject *other)
1720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 if (!PyAnySet_Check(other)) {
1724 Py_INCREF(Py_NotImplemented);
1725 return Py_NotImplemented;
1726 }
1727 result = set_symmetric_difference_update(so, other);
1728 if (result == NULL)
1729 return NULL;
1730 Py_DECREF(result);
1731 Py_INCREF(so);
1732 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001733}
1734
1735static PyObject *
1736set_issubset(PySetObject *so, PyObject *other)
1737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 setentry *entry;
1739 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 if (!PyAnySet_Check(other)) {
1742 PyObject *tmp, *result;
1743 tmp = make_new_set(&PySet_Type, other);
1744 if (tmp == NULL)
1745 return NULL;
1746 result = set_issubset(so, tmp);
1747 Py_DECREF(tmp);
1748 return result;
1749 }
1750 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1751 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 while (set_next(so, &pos, &entry)) {
1754 int rv = set_contains_entry((PySetObject *)other, entry);
1755 if (rv == -1)
1756 return NULL;
1757 if (!rv)
1758 Py_RETURN_FALSE;
1759 }
1760 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001761}
1762
1763PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1764
1765static PyObject *
1766set_issuperset(PySetObject *so, PyObject *other)
1767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 if (!PyAnySet_Check(other)) {
1771 tmp = make_new_set(&PySet_Type, other);
1772 if (tmp == NULL)
1773 return NULL;
1774 result = set_issuperset(so, tmp);
1775 Py_DECREF(tmp);
1776 return result;
1777 }
1778 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001779}
1780
1781PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1782
Raymond Hettingera690a992003-11-16 16:17:49 +00001783static PyObject *
1784set_richcompare(PySetObject *v, PyObject *w, int op)
1785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 if(!PyAnySet_Check(w)) {
1789 Py_INCREF(Py_NotImplemented);
1790 return Py_NotImplemented;
1791 }
1792 switch (op) {
1793 case Py_EQ:
1794 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1795 Py_RETURN_FALSE;
1796 if (v->hash != -1 &&
1797 ((PySetObject *)w)->hash != -1 &&
1798 v->hash != ((PySetObject *)w)->hash)
1799 Py_RETURN_FALSE;
1800 return set_issubset(v, w);
1801 case Py_NE:
1802 r1 = set_richcompare(v, w, Py_EQ);
1803 if (r1 == NULL)
1804 return NULL;
1805 r2 = PyBool_FromLong(PyObject_Not(r1));
1806 Py_DECREF(r1);
1807 return r2;
1808 case Py_LE:
1809 return set_issubset(v, w);
1810 case Py_GE:
1811 return set_issuperset(v, w);
1812 case Py_LT:
1813 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1814 Py_RETURN_FALSE;
1815 return set_issubset(v, w);
1816 case Py_GT:
1817 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1818 Py_RETURN_FALSE;
1819 return set_issuperset(v, w);
1820 }
1821 Py_INCREF(Py_NotImplemented);
1822 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001823}
1824
Raymond Hettingera690a992003-11-16 16:17:49 +00001825static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001826set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 if (set_add_key(so, key) == -1)
1829 return NULL;
1830 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001831}
1832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001834"Add an element to a set.\n\
1835\n\
1836This has no effect if the element is already present.");
1837
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001838static int
1839set_contains(PySetObject *so, PyObject *key)
1840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 PyObject *tmpkey;
1842 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 rv = set_contains_key(so, key);
1845 if (rv == -1) {
1846 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1847 return -1;
1848 PyErr_Clear();
1849 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1850 if (tmpkey == NULL)
1851 return -1;
1852 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1853 rv = set_contains(so, tmpkey);
1854 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1855 Py_DECREF(tmpkey);
1856 }
1857 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001858}
1859
1860static PyObject *
1861set_direct_contains(PySetObject *so, PyObject *key)
1862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 result = set_contains(so, key);
1866 if (result == -1)
1867 return NULL;
1868 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001869}
1870
1871PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1872
Raymond Hettingera690a992003-11-16 16:17:49 +00001873static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001874set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 PyObject *tmpkey;
1877 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 rv = set_discard_key(so, key);
1880 if (rv == -1) {
1881 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1882 return NULL;
1883 PyErr_Clear();
1884 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1885 if (tmpkey == NULL)
1886 return NULL;
1887 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1888 rv = set_discard_key(so, tmpkey);
1889 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1890 Py_DECREF(tmpkey);
1891 if (rv == -1)
1892 return NULL;
1893 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 if (rv == DISCARD_NOTFOUND) {
1896 set_key_error(key);
1897 return NULL;
1898 }
1899 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001900}
1901
1902PyDoc_STRVAR(remove_doc,
1903"Remove an element from a set; it must be a member.\n\
1904\n\
1905If the element is not a member, raise a KeyError.");
1906
1907static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001908set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 PyObject *tmpkey, *result;
1911 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 rv = set_discard_key(so, key);
1914 if (rv == -1) {
1915 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1916 return NULL;
1917 PyErr_Clear();
1918 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1919 if (tmpkey == NULL)
1920 return NULL;
1921 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1922 result = set_discard(so, tmpkey);
1923 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1924 Py_DECREF(tmpkey);
1925 return result;
1926 }
1927 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001928}
1929
1930PyDoc_STRVAR(discard_doc,
1931"Remove an element from a set if it is a member.\n\
1932\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001934
1935static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001936set_reduce(PySetObject *so)
1937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 keys = PySequence_List((PyObject *)so);
1941 if (keys == NULL)
1942 goto done;
1943 args = PyTuple_Pack(1, keys);
1944 if (args == NULL)
1945 goto done;
1946 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1947 if (dict == NULL) {
1948 PyErr_Clear();
1949 dict = Py_None;
1950 Py_INCREF(dict);
1951 }
1952 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001953done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 Py_XDECREF(args);
1955 Py_XDECREF(keys);
1956 Py_XDECREF(dict);
1957 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001958}
1959
1960PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1961
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001962static PyObject *
1963set_sizeof(PySetObject *so)
1964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 res = sizeof(PySetObject);
1968 if (so->table != so->smalltable)
1969 res = res + (so->mask + 1) * sizeof(setentry);
1970 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001971}
1972
1973PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001974static int
1975set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 if (!PyAnySet_Check(self))
1980 return -1;
1981 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1982 return -1;
1983 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1984 return -1;
1985 set_clear_internal(self);
1986 self->hash = -1;
1987 if (iterable == NULL)
1988 return 0;
1989 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001990}
1991
Raymond Hettingera690a992003-11-16 16:17:49 +00001992static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 set_len, /* sq_length */
1994 0, /* sq_concat */
1995 0, /* sq_repeat */
1996 0, /* sq_item */
1997 0, /* sq_slice */
1998 0, /* sq_ass_item */
1999 0, /* sq_ass_slice */
2000 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002001};
2002
2003/* set object ********************************************************/
2004
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002005#ifdef Py_DEBUG
2006static PyObject *test_c_api(PySetObject *so);
2007
2008PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2009All is well if assertions don't fail.");
2010#endif
2011
Raymond Hettingera690a992003-11-16 16:17:49 +00002012static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 {"add", (PyCFunction)set_add, METH_O,
2014 add_doc},
2015 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2016 clear_doc},
2017 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2018 contains_doc},
2019 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2020 copy_doc},
2021 {"discard", (PyCFunction)set_discard, METH_O,
2022 discard_doc},
2023 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2024 difference_doc},
2025 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2026 difference_update_doc},
2027 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2028 intersection_doc},
2029 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2030 intersection_update_doc},
2031 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2032 isdisjoint_doc},
2033 {"issubset", (PyCFunction)set_issubset, METH_O,
2034 issubset_doc},
2035 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2036 issuperset_doc},
2037 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2038 pop_doc},
2039 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2040 reduce_doc},
2041 {"remove", (PyCFunction)set_remove, METH_O,
2042 remove_doc},
2043 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2044 sizeof_doc},
2045 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2046 symmetric_difference_doc},
2047 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2048 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002049#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2051 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002052#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 {"union", (PyCFunction)set_union, METH_VARARGS,
2054 union_doc},
2055 {"update", (PyCFunction)set_update, METH_VARARGS,
2056 update_doc},
2057 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002058};
2059
2060static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 0, /*nb_add*/
2062 (binaryfunc)set_sub, /*nb_subtract*/
2063 0, /*nb_multiply*/
2064 0, /*nb_remainder*/
2065 0, /*nb_divmod*/
2066 0, /*nb_power*/
2067 0, /*nb_negative*/
2068 0, /*nb_positive*/
2069 0, /*nb_absolute*/
2070 0, /*nb_bool*/
2071 0, /*nb_invert*/
2072 0, /*nb_lshift*/
2073 0, /*nb_rshift*/
2074 (binaryfunc)set_and, /*nb_and*/
2075 (binaryfunc)set_xor, /*nb_xor*/
2076 (binaryfunc)set_or, /*nb_or*/
2077 0, /*nb_int*/
2078 0, /*nb_reserved*/
2079 0, /*nb_float*/
2080 0, /*nb_inplace_add*/
2081 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2082 0, /*nb_inplace_multiply*/
2083 0, /*nb_inplace_remainder*/
2084 0, /*nb_inplace_power*/
2085 0, /*nb_inplace_lshift*/
2086 0, /*nb_inplace_rshift*/
2087 (binaryfunc)set_iand, /*nb_inplace_and*/
2088 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2089 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002090};
2091
2092PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002093"set() -> new empty set object\n\
2094set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002095\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002096Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002097
2098PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2100 "set", /* tp_name */
2101 sizeof(PySetObject), /* tp_basicsize */
2102 0, /* tp_itemsize */
2103 /* methods */
2104 (destructor)set_dealloc, /* tp_dealloc */
2105 0, /* tp_print */
2106 0, /* tp_getattr */
2107 0, /* tp_setattr */
2108 0, /* tp_reserved */
2109 (reprfunc)set_repr, /* tp_repr */
2110 &set_as_number, /* tp_as_number */
2111 &set_as_sequence, /* tp_as_sequence */
2112 0, /* tp_as_mapping */
2113 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2114 0, /* tp_call */
2115 0, /* tp_str */
2116 PyObject_GenericGetAttr, /* tp_getattro */
2117 0, /* tp_setattro */
2118 0, /* tp_as_buffer */
2119 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2120 Py_TPFLAGS_BASETYPE, /* tp_flags */
2121 set_doc, /* tp_doc */
2122 (traverseproc)set_traverse, /* tp_traverse */
2123 (inquiry)set_clear_internal, /* tp_clear */
2124 (richcmpfunc)set_richcompare, /* tp_richcompare */
2125 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2126 (getiterfunc)set_iter, /* tp_iter */
2127 0, /* tp_iternext */
2128 set_methods, /* tp_methods */
2129 0, /* tp_members */
2130 0, /* tp_getset */
2131 0, /* tp_base */
2132 0, /* tp_dict */
2133 0, /* tp_descr_get */
2134 0, /* tp_descr_set */
2135 0, /* tp_dictoffset */
2136 (initproc)set_init, /* tp_init */
2137 PyType_GenericAlloc, /* tp_alloc */
2138 set_new, /* tp_new */
2139 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002140};
2141
2142/* frozenset object ********************************************************/
2143
2144
2145static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2147 contains_doc},
2148 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2149 copy_doc},
2150 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2151 difference_doc},
2152 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2153 intersection_doc},
2154 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2155 isdisjoint_doc},
2156 {"issubset", (PyCFunction)set_issubset, METH_O,
2157 issubset_doc},
2158 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2159 issuperset_doc},
2160 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2161 reduce_doc},
2162 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2163 sizeof_doc},
2164 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2165 symmetric_difference_doc},
2166 {"union", (PyCFunction)set_union, METH_VARARGS,
2167 union_doc},
2168 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002169};
2170
2171static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 0, /*nb_add*/
2173 (binaryfunc)set_sub, /*nb_subtract*/
2174 0, /*nb_multiply*/
2175 0, /*nb_remainder*/
2176 0, /*nb_divmod*/
2177 0, /*nb_power*/
2178 0, /*nb_negative*/
2179 0, /*nb_positive*/
2180 0, /*nb_absolute*/
2181 0, /*nb_bool*/
2182 0, /*nb_invert*/
2183 0, /*nb_lshift*/
2184 0, /*nb_rshift*/
2185 (binaryfunc)set_and, /*nb_and*/
2186 (binaryfunc)set_xor, /*nb_xor*/
2187 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002188};
2189
2190PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002191"frozenset() -> empty frozenset object\n\
2192frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002193\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002194Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002195
2196PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2198 "frozenset", /* tp_name */
2199 sizeof(PySetObject), /* tp_basicsize */
2200 0, /* tp_itemsize */
2201 /* methods */
2202 (destructor)set_dealloc, /* tp_dealloc */
2203 0, /* tp_print */
2204 0, /* tp_getattr */
2205 0, /* tp_setattr */
2206 0, /* tp_reserved */
2207 (reprfunc)set_repr, /* tp_repr */
2208 &frozenset_as_number, /* tp_as_number */
2209 &set_as_sequence, /* tp_as_sequence */
2210 0, /* tp_as_mapping */
2211 frozenset_hash, /* tp_hash */
2212 0, /* tp_call */
2213 0, /* tp_str */
2214 PyObject_GenericGetAttr, /* tp_getattro */
2215 0, /* tp_setattro */
2216 0, /* tp_as_buffer */
2217 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2218 Py_TPFLAGS_BASETYPE, /* tp_flags */
2219 frozenset_doc, /* tp_doc */
2220 (traverseproc)set_traverse, /* tp_traverse */
2221 (inquiry)set_clear_internal, /* tp_clear */
2222 (richcmpfunc)set_richcompare, /* tp_richcompare */
2223 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2224 (getiterfunc)set_iter, /* tp_iter */
2225 0, /* tp_iternext */
2226 frozenset_methods, /* tp_methods */
2227 0, /* tp_members */
2228 0, /* tp_getset */
2229 0, /* tp_base */
2230 0, /* tp_dict */
2231 0, /* tp_descr_get */
2232 0, /* tp_descr_set */
2233 0, /* tp_dictoffset */
2234 0, /* tp_init */
2235 PyType_GenericAlloc, /* tp_alloc */
2236 frozenset_new, /* tp_new */
2237 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002238};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002239
2240
2241/***** C API functions *************************************************/
2242
2243PyObject *
2244PySet_New(PyObject *iterable)
2245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002247}
2248
2249PyObject *
2250PyFrozenSet_New(PyObject *iterable)
2251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002253}
2254
Neal Norwitz8c49c822006-03-04 18:41:19 +00002255Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002256PySet_Size(PyObject *anyset)
2257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 if (!PyAnySet_Check(anyset)) {
2259 PyErr_BadInternalCall();
2260 return -1;
2261 }
2262 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002263}
2264
2265int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002266PySet_Clear(PyObject *set)
2267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 if (!PySet_Check(set)) {
2269 PyErr_BadInternalCall();
2270 return -1;
2271 }
2272 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002273}
2274
2275int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002276PySet_Contains(PyObject *anyset, PyObject *key)
2277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 if (!PyAnySet_Check(anyset)) {
2279 PyErr_BadInternalCall();
2280 return -1;
2281 }
2282 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002283}
2284
2285int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002286PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 if (!PySet_Check(set)) {
2289 PyErr_BadInternalCall();
2290 return -1;
2291 }
2292 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002293}
2294
2295int
Christian Heimesfd66e512008-01-29 12:18:50 +00002296PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 if (!PySet_Check(anyset) &&
2299 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2300 PyErr_BadInternalCall();
2301 return -1;
2302 }
2303 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002304}
2305
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002306int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002307_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 if (!PyAnySet_Check(set)) {
2312 PyErr_BadInternalCall();
2313 return -1;
2314 }
2315 if (set_next((PySetObject *)set, pos, &entry) == 0)
2316 return 0;
2317 *key = entry->key;
2318 *hash = entry->hash;
2319 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002320}
2321
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002322PyObject *
2323PySet_Pop(PyObject *set)
2324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 if (!PySet_Check(set)) {
2326 PyErr_BadInternalCall();
2327 return NULL;
2328 }
2329 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002330}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002331
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002332int
2333_PySet_Update(PyObject *set, PyObject *iterable)
2334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 if (!PySet_Check(set)) {
2336 PyErr_BadInternalCall();
2337 return -1;
2338 }
2339 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002340}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002341
2342#ifdef Py_DEBUG
2343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002345 Returns True and original set is restored. */
2346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347#define assertRaises(call_return_value, exception) \
2348 do { \
2349 assert(call_return_value); \
2350 assert(PyErr_ExceptionMatches(exception)); \
2351 PyErr_Clear(); \
2352 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002353
2354static PyObject *
2355test_c_api(PySetObject *so)
2356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 Py_ssize_t count;
2358 char *s;
2359 Py_ssize_t i;
2360 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2361 PyObject *ob = (PyObject *)so;
2362 long hash;
2363 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* Verify preconditions */
2366 assert(PyAnySet_Check(ob));
2367 assert(PyAnySet_CheckExact(ob));
2368 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 /* so.clear(); so |= set("abc"); */
2371 str = PyUnicode_FromString("abc");
2372 if (str == NULL)
2373 return NULL;
2374 set_clear_internal(so);
2375 if (set_update_internal(so, str) == -1) {
2376 Py_DECREF(str);
2377 return NULL;
2378 }
2379 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 /* Exercise type/size checks */
2382 assert(PySet_Size(ob) == 3);
2383 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 /* Raise TypeError for non-iterable constructor arguments */
2386 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2387 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 /* Raise TypeError for unhashable key */
2390 dup = PySet_New(ob);
2391 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2392 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2393 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 /* Exercise successful pop, contains, add, and discard */
2396 elem = PySet_Pop(ob);
2397 assert(PySet_Contains(ob, elem) == 0);
2398 assert(PySet_GET_SIZE(ob) == 2);
2399 assert(PySet_Add(ob, elem) == 0);
2400 assert(PySet_Contains(ob, elem) == 1);
2401 assert(PySet_GET_SIZE(ob) == 3);
2402 assert(PySet_Discard(ob, elem) == 1);
2403 assert(PySet_GET_SIZE(ob) == 2);
2404 assert(PySet_Discard(ob, elem) == 0);
2405 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 /* Exercise clear */
2408 dup2 = PySet_New(dup);
2409 assert(PySet_Clear(dup2) == 0);
2410 assert(PySet_Size(dup2) == 0);
2411 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Raise SystemError on clear or update of frozen set */
2414 f = PyFrozenSet_New(dup);
2415 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2416 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2417 assert(PySet_Add(f, elem) == 0);
2418 Py_INCREF(f);
2419 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2420 Py_DECREF(f);
2421 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 /* Exercise direct iteration */
2424 i = 0, count = 0;
2425 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2426 s = _PyUnicode_AsString(x);
2427 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2428 count++;
2429 }
2430 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 /* Exercise updates */
2433 dup2 = PySet_New(NULL);
2434 assert(_PySet_Update(dup2, dup) == 0);
2435 assert(PySet_Size(dup2) == 3);
2436 assert(_PySet_Update(dup2, dup) == 0);
2437 assert(PySet_Size(dup2) == 3);
2438 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 /* Raise SystemError when self argument is not a set or frozenset. */
2441 t = PyTuple_New(0);
2442 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2443 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2444 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 /* Raise SystemError when self argument is not a set. */
2447 f = PyFrozenSet_New(dup);
2448 assert(PySet_Size(f) == 3);
2449 assert(PyFrozenSet_CheckExact(f));
2450 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2451 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2452 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 /* Raise KeyError when popping from an empty set */
2455 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2456 Py_DECREF(ob);
2457 assert(PySet_GET_SIZE(ob) == 0);
2458 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* Restore the set from the copy using the PyNumber API */
2461 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2462 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 /* Verify constructors accept NULL arguments */
2465 f = PySet_New(NULL);
2466 assert(f != NULL);
2467 assert(PySet_GET_SIZE(f) == 0);
2468 Py_DECREF(f);
2469 f = PyFrozenSet_New(NULL);
2470 assert(f != NULL);
2471 assert(PyFrozenSet_CheckExact(f));
2472 assert(PySet_GET_SIZE(f) == 0);
2473 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 Py_DECREF(elem);
2476 Py_DECREF(dup);
2477 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002478}
2479
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002480#undef assertRaises
2481
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002482#endif