blob: 30afc7c1eefc16188f70f8b7c28bcea58613f94a [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +000038 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039}
40#endif
41
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000216 register setentry *entry;
217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000218
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000285 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000286
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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;
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000352 set_insert_clean(so, entry->key, (long) entry->hash);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000353 }
354 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000366 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000367
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000368 assert(so->fill <= so->mask); /* at least one empty slot */
369 n_used = so->used;
370 Py_INCREF(entry->key);
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000371 if (set_insert_key(so, entry->key, (long) entry->hash) == -1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000372 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 Pitrou7f14f0d2010-05-09 16:14:21 +0000383 register long hash;
384 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000385
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000409{ register setentry *entry;
410 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000411
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000412 entry = (so->lookup)(so, oldentry->key, (long) oldentry->hash);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000413 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 Pitrou7f14f0d2010-05-09 16:14:21 +0000428 register long hash;
429 register setentry *entry;
430 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000432 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000433
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000461 Py_ssize_t i, n;
462 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000463
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 n = so->mask + 1;
465 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000466#endif
467
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000468 table = so->table;
469 assert(table != NULL);
470 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000471
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000499 assert(i < n);
500 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000502 if (entry->key) {
503 --fill;
504 Py_DECREF(entry->key);
505 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506#ifdef Py_DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000507 else
508 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000510 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000511
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000533 Py_ssize_t i;
534 Py_ssize_t mask;
535 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000598 keys = PySequence_List((PyObject *)so);
599 if (keys == NULL)
600 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000639 PySetObject *other;
640 register Py_ssize_t i;
641 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000642
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000643 assert (PyAnySet_Check(so));
644 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000645
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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);
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000663 if (set_insert_key(so, entry->key, (long) entry->hash) == -1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000664 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 Pitrou7f14f0d2010-05-09 16:14:21 +0000675 long hash;
676 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000677
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000694 PyObject *key;
695 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000696
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000697 lu_entry = (so->lookup)(so, entry->key, (long) entry->hash);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000698 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 Pitrou7f14f0d2010-05-09 16:14:21 +0000707 register Py_ssize_t i = 0;
708 register setentry *entry;
709 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000710
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000753 Py_ssize_t pos = 0;
754 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000755
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000769 if (so->hash != -1)
770 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000771
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000772 hash *= (long) PySet_GET_SIZE(self) + 1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000773 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. */
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000779 h = (long) entry->hash;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000780 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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000816 Py_ssize_t len = 0;
817 if (si->si_set != NULL && si->si_used == si->si_set->used)
818 len = si->len;
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000819 return PyLong_FromSsize_t(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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000836 if (so == NULL)
837 return NULL;
838 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000918 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000919
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000920 if (PyAnySet_Check(other))
921 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000922
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000950 it = PyObject_GetIter(other);
951 if (it == NULL)
952 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000953
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000971 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000972
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000987 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000988
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001013 so->lookup = set_lookkey_unicode;
1014 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001015
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001044 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001045
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001046 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1047 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001048
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001049 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1050 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001051
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001052 if (type != &PyFrozenSet_Type)
1053 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001054
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001076 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001077
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001130 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001131
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001177 PySetObject *result;
1178 PyObject *other;
1179 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001180
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001181 result = (PySetObject *)set_copy(so);
1182 if (result == NULL)
1183 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001184
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001205 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001206
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001240 PySetObject *result;
1241 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001242
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001243 if ((PyObject *)so == other)
1244 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001245
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001250 if (PyAnySet_Check(other)) {
1251 Py_ssize_t pos = 0;
1252 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001253
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001323 Py_ssize_t i;
1324 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001325
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001326 if (PyTuple_GET_SIZE(args) == 0)
1327 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001328
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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,
Raymond Hettingerc566df32009-11-19 00:01:54 +00001344"Return the intersection of two or more sets as a new set.\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00001345\n\
Raymond Hettingerc566df32009-11-19 00:01:54 +00001346(i.e. elements that are common to all of the sets.)");
Raymond Hettingera690a992003-11-16 16:17:49 +00001347
1348static PyObject *
1349set_intersection_update(PySetObject *so, PyObject *other)
1350{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001351 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001352
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001364 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001365
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001390 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001391
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001407 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001408
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001416 if (PyAnySet_CheckExact(other)) {
1417 Py_ssize_t pos = 0;
1418 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001419
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001435 it = PyObject_GetIter(other);
1436 if (it == NULL)
1437 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001438
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001474 if ((PyObject *)so == other)
1475 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001476
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001477 if (PyAnySet_Check(other)) {
1478 setentry *entry;
1479 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001480
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001511 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001512
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001527 PyObject *result;
1528 setentry *entry;
1529 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001530
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00001550 if (!_PyDict_Contains(other, entry->key, (long) entry->hash)) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001551 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 Pitrou7f14f0d2010-05-09 16:14:21 +00001579 Py_ssize_t i;
1580 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001581
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001582 if (PyTuple_GET_SIZE(args) == 0)
1583 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001584
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001630 PySetObject *otherset;
1631 PyObject *key;
1632 Py_ssize_t pos = 0;
1633 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001634
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001635 if ((PyObject *)so == other)
1636 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001637
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001690 PyObject *rv;
1691 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001692
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001721 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001722
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001738 setentry *entry;
1739 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001740
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001768 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001769
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001786 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001787
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +00001841 PyObject *tmpkey;
1842 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001843
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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();
Raymond Hettinger51ced7a2010-08-06 09:57:49 +00001849 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001850 if (tmpkey == NULL)
1851 return -1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001852 rv = set_contains(so, tmpkey);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001853 Py_DECREF(tmpkey);
1854 }
1855 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001856}
1857
1858static PyObject *
1859set_direct_contains(PySetObject *so, PyObject *key)
1860{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001861 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001862
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001863 result = set_contains(so, key);
1864 if (result == -1)
1865 return NULL;
1866 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001867}
1868
1869PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1870
Raymond Hettingera690a992003-11-16 16:17:49 +00001871static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001872set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001873{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001874 PyObject *tmpkey;
1875 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001876
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001877 rv = set_discard_key(so, key);
1878 if (rv == -1) {
1879 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1880 return NULL;
1881 PyErr_Clear();
Raymond Hettinger51ced7a2010-08-06 09:57:49 +00001882 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001883 if (tmpkey == NULL)
1884 return NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001885 rv = set_discard_key(so, tmpkey);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001886 Py_DECREF(tmpkey);
1887 if (rv == -1)
1888 return NULL;
1889 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001890
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001891 if (rv == DISCARD_NOTFOUND) {
1892 set_key_error(key);
1893 return NULL;
1894 }
1895 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001896}
1897
1898PyDoc_STRVAR(remove_doc,
1899"Remove an element from a set; it must be a member.\n\
1900\n\
1901If the element is not a member, raise a KeyError.");
1902
1903static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001904set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001905{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001906 PyObject *tmpkey, *result;
1907 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001908
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001909 rv = set_discard_key(so, key);
1910 if (rv == -1) {
1911 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1912 return NULL;
1913 PyErr_Clear();
Raymond Hettinger51ced7a2010-08-06 09:57:49 +00001914 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001915 if (tmpkey == NULL)
1916 return NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001917 result = set_discard(so, tmpkey);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001918 Py_DECREF(tmpkey);
1919 return result;
1920 }
1921 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001922}
1923
1924PyDoc_STRVAR(discard_doc,
1925"Remove an element from a set if it is a member.\n\
1926\n\
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001927If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001928
1929static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001930set_reduce(PySetObject *so)
1931{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001932 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001933
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001934 keys = PySequence_List((PyObject *)so);
1935 if (keys == NULL)
1936 goto done;
1937 args = PyTuple_Pack(1, keys);
1938 if (args == NULL)
1939 goto done;
1940 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1941 if (dict == NULL) {
1942 PyErr_Clear();
1943 dict = Py_None;
1944 Py_INCREF(dict);
1945 }
1946 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001947done:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001948 Py_XDECREF(args);
1949 Py_XDECREF(keys);
1950 Py_XDECREF(dict);
1951 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001952}
1953
1954PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1955
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001956static PyObject *
1957set_sizeof(PySetObject *so)
1958{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001959 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001960
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001961 res = sizeof(PySetObject);
1962 if (so->table != so->smalltable)
1963 res = res + (so->mask + 1) * sizeof(setentry);
1964 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001965}
1966
1967PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001968static int
1969set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1970{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001971 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001972
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001973 if (!PyAnySet_Check(self))
1974 return -1;
1975 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1976 return -1;
1977 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1978 return -1;
1979 set_clear_internal(self);
1980 self->hash = -1;
1981 if (iterable == NULL)
1982 return 0;
1983 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001984}
1985
Raymond Hettingera690a992003-11-16 16:17:49 +00001986static PySequenceMethods set_as_sequence = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001987 set_len, /* sq_length */
1988 0, /* sq_concat */
1989 0, /* sq_repeat */
1990 0, /* sq_item */
1991 0, /* sq_slice */
1992 0, /* sq_ass_item */
1993 0, /* sq_ass_slice */
1994 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001995};
1996
1997/* set object ********************************************************/
1998
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001999#ifdef Py_DEBUG
2000static PyObject *test_c_api(PySetObject *so);
2001
2002PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2003All is well if assertions don't fail.");
2004#endif
2005
Raymond Hettingera690a992003-11-16 16:17:49 +00002006static PyMethodDef set_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002007 {"add", (PyCFunction)set_add, METH_O,
2008 add_doc},
2009 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2010 clear_doc},
2011 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2012 contains_doc},
2013 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2014 copy_doc},
2015 {"discard", (PyCFunction)set_discard, METH_O,
2016 discard_doc},
2017 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2018 difference_doc},
2019 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2020 difference_update_doc},
2021 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2022 intersection_doc},
2023 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2024 intersection_update_doc},
2025 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2026 isdisjoint_doc},
2027 {"issubset", (PyCFunction)set_issubset, METH_O,
2028 issubset_doc},
2029 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2030 issuperset_doc},
2031 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2032 pop_doc},
2033 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2034 reduce_doc},
2035 {"remove", (PyCFunction)set_remove, METH_O,
2036 remove_doc},
2037 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2038 sizeof_doc},
2039 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2040 symmetric_difference_doc},
2041 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2042 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002043#ifdef Py_DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002044 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2045 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002046#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002047 {"union", (PyCFunction)set_union, METH_VARARGS,
2048 union_doc},
2049 {"update", (PyCFunction)set_update, METH_VARARGS,
2050 update_doc},
2051 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002052};
2053
2054static PyNumberMethods set_as_number = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002055 0, /*nb_add*/
2056 (binaryfunc)set_sub, /*nb_subtract*/
2057 0, /*nb_multiply*/
2058 0, /*nb_remainder*/
2059 0, /*nb_divmod*/
2060 0, /*nb_power*/
2061 0, /*nb_negative*/
2062 0, /*nb_positive*/
2063 0, /*nb_absolute*/
2064 0, /*nb_bool*/
2065 0, /*nb_invert*/
2066 0, /*nb_lshift*/
2067 0, /*nb_rshift*/
2068 (binaryfunc)set_and, /*nb_and*/
2069 (binaryfunc)set_xor, /*nb_xor*/
2070 (binaryfunc)set_or, /*nb_or*/
2071 0, /*nb_int*/
2072 0, /*nb_reserved*/
2073 0, /*nb_float*/
2074 0, /*nb_inplace_add*/
2075 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2076 0, /*nb_inplace_multiply*/
2077 0, /*nb_inplace_remainder*/
2078 0, /*nb_inplace_power*/
2079 0, /*nb_inplace_lshift*/
2080 0, /*nb_inplace_rshift*/
2081 (binaryfunc)set_iand, /*nb_inplace_and*/
2082 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2083 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002084};
2085
2086PyDoc_STRVAR(set_doc,
Ezio Melotti807e98e2010-03-01 04:10:55 +00002087"set() -> new empty set object\n\
2088set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002089\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002090Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002091
2092PyTypeObject PySet_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002093 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2094 "set", /* tp_name */
2095 sizeof(PySetObject), /* tp_basicsize */
2096 0, /* tp_itemsize */
2097 /* methods */
2098 (destructor)set_dealloc, /* tp_dealloc */
2099 0, /* tp_print */
2100 0, /* tp_getattr */
2101 0, /* tp_setattr */
2102 0, /* tp_reserved */
2103 (reprfunc)set_repr, /* tp_repr */
2104 &set_as_number, /* tp_as_number */
2105 &set_as_sequence, /* tp_as_sequence */
2106 0, /* tp_as_mapping */
2107 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2108 0, /* tp_call */
2109 0, /* tp_str */
2110 PyObject_GenericGetAttr, /* tp_getattro */
2111 0, /* tp_setattro */
2112 0, /* tp_as_buffer */
2113 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2114 Py_TPFLAGS_BASETYPE, /* tp_flags */
2115 set_doc, /* tp_doc */
2116 (traverseproc)set_traverse, /* tp_traverse */
2117 (inquiry)set_clear_internal, /* tp_clear */
2118 (richcmpfunc)set_richcompare, /* tp_richcompare */
2119 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2120 (getiterfunc)set_iter, /* tp_iter */
2121 0, /* tp_iternext */
2122 set_methods, /* tp_methods */
2123 0, /* tp_members */
2124 0, /* tp_getset */
2125 0, /* tp_base */
2126 0, /* tp_dict */
2127 0, /* tp_descr_get */
2128 0, /* tp_descr_set */
2129 0, /* tp_dictoffset */
2130 (initproc)set_init, /* tp_init */
2131 PyType_GenericAlloc, /* tp_alloc */
2132 set_new, /* tp_new */
2133 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002134};
2135
2136/* frozenset object ********************************************************/
2137
2138
2139static PyMethodDef frozenset_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002140 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2141 contains_doc},
2142 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2143 copy_doc},
2144 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2145 difference_doc},
2146 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2147 intersection_doc},
2148 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2149 isdisjoint_doc},
2150 {"issubset", (PyCFunction)set_issubset, METH_O,
2151 issubset_doc},
2152 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2153 issuperset_doc},
2154 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2155 reduce_doc},
2156 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2157 sizeof_doc},
2158 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2159 symmetric_difference_doc},
2160 {"union", (PyCFunction)set_union, METH_VARARGS,
2161 union_doc},
2162 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002163};
2164
2165static PyNumberMethods frozenset_as_number = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002166 0, /*nb_add*/
2167 (binaryfunc)set_sub, /*nb_subtract*/
2168 0, /*nb_multiply*/
2169 0, /*nb_remainder*/
2170 0, /*nb_divmod*/
2171 0, /*nb_power*/
2172 0, /*nb_negative*/
2173 0, /*nb_positive*/
2174 0, /*nb_absolute*/
2175 0, /*nb_bool*/
2176 0, /*nb_invert*/
2177 0, /*nb_lshift*/
2178 0, /*nb_rshift*/
2179 (binaryfunc)set_and, /*nb_and*/
2180 (binaryfunc)set_xor, /*nb_xor*/
2181 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002182};
2183
2184PyDoc_STRVAR(frozenset_doc,
Ezio Melotti807e98e2010-03-01 04:10:55 +00002185"frozenset() -> empty frozenset object\n\
2186frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002187\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002188Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002189
2190PyTypeObject PyFrozenSet_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002191 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2192 "frozenset", /* tp_name */
2193 sizeof(PySetObject), /* tp_basicsize */
2194 0, /* tp_itemsize */
2195 /* methods */
2196 (destructor)set_dealloc, /* tp_dealloc */
2197 0, /* tp_print */
2198 0, /* tp_getattr */
2199 0, /* tp_setattr */
2200 0, /* tp_reserved */
2201 (reprfunc)set_repr, /* tp_repr */
2202 &frozenset_as_number, /* tp_as_number */
2203 &set_as_sequence, /* tp_as_sequence */
2204 0, /* tp_as_mapping */
2205 frozenset_hash, /* tp_hash */
2206 0, /* tp_call */
2207 0, /* tp_str */
2208 PyObject_GenericGetAttr, /* tp_getattro */
2209 0, /* tp_setattro */
2210 0, /* tp_as_buffer */
2211 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2212 Py_TPFLAGS_BASETYPE, /* tp_flags */
2213 frozenset_doc, /* tp_doc */
2214 (traverseproc)set_traverse, /* tp_traverse */
2215 (inquiry)set_clear_internal, /* tp_clear */
2216 (richcmpfunc)set_richcompare, /* tp_richcompare */
2217 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2218 (getiterfunc)set_iter, /* tp_iter */
2219 0, /* tp_iternext */
2220 frozenset_methods, /* tp_methods */
2221 0, /* tp_members */
2222 0, /* tp_getset */
2223 0, /* tp_base */
2224 0, /* tp_dict */
2225 0, /* tp_descr_get */
2226 0, /* tp_descr_set */
2227 0, /* tp_dictoffset */
2228 0, /* tp_init */
2229 PyType_GenericAlloc, /* tp_alloc */
2230 frozenset_new, /* tp_new */
2231 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002232};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002233
2234
2235/***** C API functions *************************************************/
2236
2237PyObject *
2238PySet_New(PyObject *iterable)
2239{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002240 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002241}
2242
2243PyObject *
2244PyFrozenSet_New(PyObject *iterable)
2245{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002246 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002247}
2248
Neal Norwitz8c49c822006-03-04 18:41:19 +00002249Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002250PySet_Size(PyObject *anyset)
2251{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002252 if (!PyAnySet_Check(anyset)) {
2253 PyErr_BadInternalCall();
2254 return -1;
2255 }
2256 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002257}
2258
2259int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002260PySet_Clear(PyObject *set)
2261{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002262 if (!PySet_Check(set)) {
2263 PyErr_BadInternalCall();
2264 return -1;
2265 }
2266 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002267}
2268
2269int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002270PySet_Contains(PyObject *anyset, PyObject *key)
2271{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002272 if (!PyAnySet_Check(anyset)) {
2273 PyErr_BadInternalCall();
2274 return -1;
2275 }
2276 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002277}
2278
2279int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002280PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002281{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002282 if (!PySet_Check(set)) {
2283 PyErr_BadInternalCall();
2284 return -1;
2285 }
2286 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002287}
2288
2289int
Christian Heimesfd66e512008-01-29 12:18:50 +00002290PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002291{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002292 if (!PySet_Check(anyset) &&
2293 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2294 PyErr_BadInternalCall();
2295 return -1;
2296 }
2297 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002298}
2299
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002300int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002301_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2302{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002303 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002304
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002305 if (!PyAnySet_Check(set)) {
2306 PyErr_BadInternalCall();
2307 return -1;
2308 }
2309 if (set_next((PySetObject *)set, pos, &entry) == 0)
2310 return 0;
2311 *key = entry->key;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002312 *hash = (long) entry->hash;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002313 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002314}
2315
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002316PyObject *
2317PySet_Pop(PyObject *set)
2318{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002319 if (!PySet_Check(set)) {
2320 PyErr_BadInternalCall();
2321 return NULL;
2322 }
2323 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002324}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002325
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002326int
2327_PySet_Update(PyObject *set, PyObject *iterable)
2328{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002329 if (!PySet_Check(set)) {
2330 PyErr_BadInternalCall();
2331 return -1;
2332 }
2333 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002334}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002335
2336#ifdef Py_DEBUG
2337
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002338/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002339 Returns True and original set is restored. */
2340
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002341#define assertRaises(call_return_value, exception) \
2342 do { \
2343 assert(call_return_value); \
2344 assert(PyErr_ExceptionMatches(exception)); \
2345 PyErr_Clear(); \
2346 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002347
2348static PyObject *
2349test_c_api(PySetObject *so)
2350{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002351 Py_ssize_t count;
2352 char *s;
2353 Py_ssize_t i;
2354 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2355 PyObject *ob = (PyObject *)so;
2356 long hash;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002357
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002358 /* Verify preconditions and exercise type/size checks */
2359 assert(PyAnySet_Check(ob));
2360 assert(PyAnySet_CheckExact(ob));
2361 assert(!PyFrozenSet_CheckExact(ob));
2362 assert(PySet_Size(ob) == 3);
2363 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002364
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002365 /* Raise TypeError for non-iterable constructor arguments */
2366 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2367 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002368
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002369 /* Raise TypeError for unhashable key */
2370 dup = PySet_New(ob);
2371 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2372 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2373 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002374
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002375 /* Exercise successful pop, contains, add, and discard */
2376 elem = PySet_Pop(ob);
2377 assert(PySet_Contains(ob, elem) == 0);
2378 assert(PySet_GET_SIZE(ob) == 2);
2379 assert(PySet_Add(ob, elem) == 0);
2380 assert(PySet_Contains(ob, elem) == 1);
2381 assert(PySet_GET_SIZE(ob) == 3);
2382 assert(PySet_Discard(ob, elem) == 1);
2383 assert(PySet_GET_SIZE(ob) == 2);
2384 assert(PySet_Discard(ob, elem) == 0);
2385 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002386
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002387 /* Exercise clear */
2388 dup2 = PySet_New(dup);
2389 assert(PySet_Clear(dup2) == 0);
2390 assert(PySet_Size(dup2) == 0);
2391 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002392
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002393 /* Raise SystemError on clear or update of frozen set */
2394 f = PyFrozenSet_New(dup);
2395 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2396 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2397 assert(PySet_Add(f, elem) == 0);
2398 Py_INCREF(f);
2399 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2400 Py_DECREF(f);
2401 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002402
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002403 /* Exercise direct iteration */
2404 i = 0, count = 0;
2405 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2406 s = _PyUnicode_AsString(x);
2407 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2408 count++;
2409 }
2410 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002411
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002412 /* Exercise updates */
2413 dup2 = PySet_New(NULL);
2414 assert(_PySet_Update(dup2, dup) == 0);
2415 assert(PySet_Size(dup2) == 3);
2416 assert(_PySet_Update(dup2, dup) == 0);
2417 assert(PySet_Size(dup2) == 3);
2418 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002419
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002420 /* Raise SystemError when self argument is not a set or frozenset. */
2421 t = PyTuple_New(0);
2422 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2423 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2424 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002425
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002426 /* Raise SystemError when self argument is not a set. */
2427 f = PyFrozenSet_New(dup);
2428 assert(PySet_Size(f) == 3);
2429 assert(PyFrozenSet_CheckExact(f));
2430 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2431 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2432 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002433
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002434 /* Raise KeyError when popping from an empty set */
2435 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2436 Py_DECREF(ob);
2437 assert(PySet_GET_SIZE(ob) == 0);
2438 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002439
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002440 /* Restore the set from the copy using the PyNumber API */
2441 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2442 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002443
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002444 /* Verify constructors accept NULL arguments */
2445 f = PySet_New(NULL);
2446 assert(f != NULL);
2447 assert(PySet_GET_SIZE(f) == 0);
2448 Py_DECREF(f);
2449 f = PyFrozenSet_New(NULL);
2450 assert(f != NULL);
2451 assert(PyFrozenSet_CheckExact(f));
2452 assert(PySet_GET_SIZE(f) == 0);
2453 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002454
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002455 Py_DECREF(elem);
2456 Py_DECREF(dup);
2457 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002458}
2459
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002460#undef assertRaises
2461
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002462#endif