blob: 478b5cb651dec520e92ead08c79c8aedc785bad2 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingera9d99362005-08-05 00:01:15 +00003 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Raymond Hettinger2212e522008-11-30 23:43:36 +00006 Copyright (c) 2003-2008 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000012#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
Thomas Wouters89f507f2006-12-13 04:49:30 +000026}
27
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028/* This must be >= 1. */
29#define PERTURB_SHIFT 5
30
31/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000032static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#ifdef Py_REF_DEBUG
35PyObject *
36_PySet_Dummy(void)
37{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039}
40#endif
41
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042#define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
45 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000046 } while(0)
47
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
51 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052 } while(0)
53
Raymond Hettingerbc841a12005-08-07 13:02:53 +000054/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000055#ifndef PySet_MAXFREELIST
56#define PySet_MAXFREELIST 80
57#endif
58static PySetObject *free_list[PySet_MAXFREELIST];
59static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060
Christian Heimes0ded5b52007-12-10 15:50:56 +000061
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062/*
63The basic lookup function used by all operations.
64This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65Open addressing is preferred over chaining since the link overhead for
66chaining would be substantial (100% with typical malloc overhead).
67
68The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000069probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070
71All arithmetic on hash should ignore overflow.
72
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000073Unlike the dictionary implementation, the lookkey functions can return
74NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075*/
76
77static setentry *
Benjamin Peterson8f67d082010-10-17 20:54:53 +000078set_lookkey(PySetObject *so, PyObject *key, register Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 register Py_ssize_t i;
81 register size_t perturb;
82 register setentry *freeslot;
83 register size_t mask = so->mask;
84 setentry *table = so->table;
85 register setentry *entry;
86 register int cmp;
87 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 i = hash & mask;
90 entry = &table[i];
91 if (entry->key == NULL || entry->key == key)
92 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 if (entry->key == dummy)
95 freeslot = entry;
96 else {
97 if (entry->hash == hash) {
98 startkey = entry->key;
99 Py_INCREF(startkey);
100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
101 Py_DECREF(startkey);
102 if (cmp < 0)
103 return NULL;
104 if (table == so->table && entry->key == startkey) {
105 if (cmp > 0)
106 return entry;
107 }
108 else {
109 /* The compare did major nasty stuff to the
110 * set: start over.
111 */
112 return set_lookkey(so, key, hash);
113 }
114 }
115 freeslot = NULL;
116 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /* In the loop, key == dummy is by far (factor of 100s) the
119 least likely outcome, so test for that last. */
120 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
121 i = (i << 2) + i + perturb + 1;
122 entry = &table[i & mask];
123 if (entry->key == NULL) {
124 if (freeslot != NULL)
125 entry = freeslot;
126 break;
127 }
128 if (entry->key == key)
129 break;
130 if (entry->hash == hash && entry->key != dummy) {
131 startkey = entry->key;
132 Py_INCREF(startkey);
133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
134 Py_DECREF(startkey);
135 if (cmp < 0)
136 return NULL;
137 if (table == so->table && entry->key == startkey) {
138 if (cmp > 0)
139 break;
140 }
141 else {
142 /* The compare did major nasty stuff to the
143 * set: start over.
144 */
145 return set_lookkey(so, key, hash);
146 }
147 }
148 else if (entry->key == dummy && freeslot == NULL)
149 freeslot = entry;
150 }
151 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152}
153
154/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000155 * Hacked up version of set_lookkey which can assume keys are always unicode;
156 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000157 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158 */
159static setentry *
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000160set_lookkey_unicode(PySetObject *so, PyObject *key, register Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 register Py_ssize_t i;
163 register size_t perturb;
164 register setentry *freeslot;
165 register size_t mask = so->mask;
166 setentry *table = so->table;
167 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 /* Make sure this function doesn't have to handle non-unicode keys,
170 including subclasses of str; e.g., one reason to subclass
171 strings is to override __eq__, and for speed we don't cater to
172 that here. */
173 if (!PyUnicode_CheckExact(key)) {
174 so->lookup = set_lookkey;
175 return set_lookkey(so, key, hash);
176 }
177 i = hash & mask;
178 entry = &table[i];
179 if (entry->key == NULL || entry->key == key)
180 return entry;
181 if (entry->key == dummy)
182 freeslot = entry;
183 else {
184 if (entry->hash == hash && unicode_eq(entry->key, key))
185 return entry;
186 freeslot = NULL;
187 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 /* In the loop, key == dummy is by far (factor of 100s) the
190 least likely outcome, so test for that last. */
191 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
192 i = (i << 2) + i + perturb + 1;
193 entry = &table[i & mask];
194 if (entry->key == NULL)
195 return freeslot == NULL ? entry : freeslot;
196 if (entry->key == key
197 || (entry->hash == hash
198 && entry->key != dummy
199 && unicode_eq(entry->key, key)))
200 return entry;
201 if (entry->key == dummy && freeslot == NULL)
202 freeslot = entry;
203 }
204 assert(0); /* NOT REACHED */
205 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206}
207
208/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211Eats a reference to key.
212*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213static int
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000214set_insert_key(register PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 register setentry *entry;
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, Py_hash_t);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 assert(so->lookup != NULL);
220 entry = so->lookup(so, key, hash);
221 if (entry == NULL)
222 return -1;
223 if (entry->key == NULL) {
224 /* UNUSED */
225 so->fill++;
226 entry->key = key;
227 entry->hash = hash;
228 so->used++;
229 } else if (entry->key == dummy) {
230 /* DUMMY */
231 entry->key = key;
232 entry->hash = hash;
233 so->used++;
234 Py_DECREF(dummy);
235 } else {
236 /* ACTIVE */
237 Py_DECREF(key);
238 }
239 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000240}
241
242/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000243Internal routine used by set_table_resize() to insert an item which is
244known to be absent from the set. This routine also assumes that
245the set contains no deleted entries. Besides the performance benefit,
246using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
247Note that no refcounts are changed by this routine; if needed, the caller
248is responsible for incref'ing `key`.
249*/
250static void
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000251set_insert_clean(register PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 register size_t i;
254 register size_t perturb;
255 register size_t mask = (size_t)so->mask;
256 setentry *table = so->table;
257 register setentry *entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 i = hash & mask;
260 entry = &table[i];
261 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
262 i = (i << 2) + i + perturb + 1;
263 entry = &table[i & mask];
264 }
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000269}
270
271/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000273keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274actually be smaller than the old one.
275*/
276static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 Py_ssize_t newsize;
280 setentry *oldtable, *newtable, *entry;
281 Py_ssize_t i;
282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Find the smallest table size > minused. */
288 for (newsize = PySet_MINSIZE;
289 newsize <= minused && newsize > 0;
290 newsize <<= 1)
291 ;
292 if (newsize <= 0) {
293 PyErr_NoMemory();
294 return -1;
295 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL);
300 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (newsize == PySet_MINSIZE) {
303 /* A large table is shrinking, or we can't get any smaller. */
304 newtable = so->smalltable;
305 if (newtable == oldtable) {
306 if (so->fill == so->used) {
307 /* No dummies, so no point doing anything. */
308 return 0;
309 }
310 /* We're not going to resize it, but rebuild the
311 table anyway to purge old dummy entries.
312 Subtle: This is *necessary* if fill==size,
313 as set_lookkey needs at least one virgin slot to
314 terminate failing searches. If fill < size, it's
315 merely desirable, as dummies slow searches. */
316 assert(so->fill > so->used);
317 memcpy(small_copy, oldtable, sizeof(small_copy));
318 oldtable = small_copy;
319 }
320 }
321 else {
322 newtable = PyMem_NEW(setentry, newsize);
323 if (newtable == NULL) {
324 PyErr_NoMemory();
325 return -1;
326 }
327 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* Make the set empty, using the new table. */
330 assert(newtable != oldtable);
331 so->table = newtable;
332 so->mask = newsize - 1;
333 memset(newtable, 0, sizeof(setentry) * newsize);
334 so->used = 0;
335 i = so->fill;
336 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
340 for (entry = oldtable; i > 0; entry++) {
341 if (entry->key == NULL) {
342 /* UNUSED */
343 ;
344 } else if (entry->key == dummy) {
345 /* DUMMY */
346 --i;
347 assert(entry->key == dummy);
348 Py_DECREF(entry->key);
349 } else {
350 /* ACTIVE */
351 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000352 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 }
354 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (is_oldtable_malloced)
357 PyMem_DEL(oldtable);
358 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000359}
360
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
362
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000364set_add_entry(register PySetObject *so, setentry *entry)
365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 register Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000367 PyObject *key = entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 assert(so->fill <= so->mask); /* at least one empty slot */
370 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000371 Py_INCREF(key);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000372 if (set_insert_key(so, key, entry->hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000373 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return -1;
375 }
376 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
377 return 0;
378 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000379}
380
381static int
382set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000383{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000384 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 if (!PyUnicode_CheckExact(key) ||
388 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
389 hash = PyObject_Hash(key);
390 if (hash == -1)
391 return -1;
392 }
393 assert(so->fill <= so->mask); /* at least one empty slot */
394 n_used = so->used;
395 Py_INCREF(key);
396 if (set_insert_key(so, key, hash) == -1) {
397 Py_DECREF(key);
398 return -1;
399 }
400 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
401 return 0;
402 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000403}
404
405#define DISCARD_NOTFOUND 0
406#define DISCARD_FOUND 1
407
408static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000409set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410{ register setentry *entry;
411 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000412
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000413 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (entry == NULL)
415 return -1;
416 if (entry->key == NULL || entry->key == dummy)
417 return DISCARD_NOTFOUND;
418 old_key = entry->key;
419 Py_INCREF(dummy);
420 entry->key = dummy;
421 so->used--;
422 Py_DECREF(old_key);
423 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000424}
425
426static int
427set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000428{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000429 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 register setentry *entry;
431 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (!PyUnicode_CheckExact(key) ||
436 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
437 hash = PyObject_Hash(key);
438 if (hash == -1)
439 return -1;
440 }
441 entry = (so->lookup)(so, key, hash);
442 if (entry == NULL)
443 return -1;
444 if (entry->key == NULL || entry->key == dummy)
445 return DISCARD_NOTFOUND;
446 old_key = entry->key;
447 Py_INCREF(dummy);
448 entry->key = dummy;
449 so->used--;
450 Py_DECREF(old_key);
451 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000452}
453
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000454static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455set_clear_internal(PySetObject *so)
456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 setentry *entry, *table;
458 int table_is_malloced;
459 Py_ssize_t fill;
460 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000461#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 Py_ssize_t i, n;
463 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 n = so->mask + 1;
466 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467#endif
468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 table = so->table;
470 assert(table != NULL);
471 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 /* This is delicate. During the process of clearing the set,
474 * decrefs can cause the set to mutate. To avoid fatal confusion
475 * (voice of experience), we have to make the set empty before
476 * clearing the slots, and never refer to anything via so->ref while
477 * clearing.
478 */
479 fill = so->fill;
480 if (table_is_malloced)
481 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 else if (fill > 0) {
484 /* It's a small table with something that needs to be cleared.
485 * Afraid the only safe way is to copy the set entries into
486 * another small table first.
487 */
488 memcpy(small_copy, table, sizeof(small_copy));
489 table = small_copy;
490 EMPTY_TO_MINSIZE(so);
491 }
492 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 /* Now we can finally clear things. If C had refcounts, we could
495 * assert that the refcount on table is 1 now, i.e. that this function
496 * has unique access to it, so decref side-effects can't alter it.
497 */
498 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 assert(i < n);
501 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (entry->key) {
504 --fill;
505 Py_DECREF(entry->key);
506 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000507#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 else
509 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000510#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 if (table_is_malloced)
514 PyMem_DEL(table);
515 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516}
517
518/*
519 * Iterate over a set table. Use like so:
520 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000521 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000522 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000523 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000524 * while (set_next(yourset, &pos, &entry)) {
525 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000526 * }
527 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000528 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000530 */
531static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000532set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 Py_ssize_t i;
535 Py_ssize_t mask;
536 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 assert (PyAnySet_Check(so));
539 i = *pos_ptr;
540 assert(i >= 0);
541 table = so->table;
542 mask = so->mask;
543 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
544 i++;
545 *pos_ptr = i+1;
546 if (i > mask)
547 return 0;
548 assert(table[i].key != NULL);
549 *entry_ptr = &table[i];
550 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000551}
552
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000553static void
554set_dealloc(PySetObject *so)
555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 register setentry *entry;
557 Py_ssize_t fill = so->fill;
558 PyObject_GC_UnTrack(so);
559 Py_TRASHCAN_SAFE_BEGIN(so)
560 if (so->weakreflist != NULL)
561 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 for (entry = so->table; fill > 0; entry++) {
564 if (entry->key) {
565 --fill;
566 Py_DECREF(entry->key);
567 }
568 }
569 if (so->table != so->smalltable)
570 PyMem_DEL(so->table);
571 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
572 free_list[numfree++] = so;
573 else
574 Py_TYPE(so)->tp_free(so);
575 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000576}
577
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000578static PyObject *
579set_repr(PySetObject *so)
580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 PyObject *keys, *result=NULL;
582 Py_UNICODE *u;
583 int status = Py_ReprEnter((PyObject*)so);
584 PyObject *listrepr;
585 Py_ssize_t newsize;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if (status != 0) {
588 if (status < 0)
589 return NULL;
590 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
591 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 /* shortcut for the empty set */
594 if (!so->used) {
595 Py_ReprLeave((PyObject*)so);
596 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
597 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 keys = PySequence_List((PyObject *)so);
600 if (keys == NULL)
601 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 listrepr = PyObject_Repr(keys);
604 Py_DECREF(keys);
605 if (listrepr == NULL)
606 goto done;
607 newsize = PyUnicode_GET_SIZE(listrepr);
608 result = PyUnicode_FromUnicode(NULL, newsize);
609 if (result) {
610 u = PyUnicode_AS_UNICODE(result);
611 *u++ = '{';
612 /* Omit the brackets from the listrepr */
613 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
614 PyUnicode_GET_SIZE(listrepr)-2);
615 u += newsize-2;
616 *u++ = '}';
617 }
618 Py_DECREF(listrepr);
619 if (Py_TYPE(so) != &PySet_Type) {
620 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
621 Py_TYPE(so)->tp_name,
622 result);
623 Py_DECREF(result);
624 result = tmp;
625 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000626done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 Py_ReprLeave((PyObject*)so);
628 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000629}
630
Martin v. Löwis18e16552006-02-15 17:27:45 +0000631static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000632set_len(PyObject *so)
633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000635}
636
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000637static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000638set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000641 PyObject *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 register Py_ssize_t i;
643 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 assert (PyAnySet_Check(so));
646 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 other = (PySetObject*)otherset;
649 if (other == so || other->used == 0)
650 /* a.update(a) or a.update({}); nothing to do */
651 return 0;
652 /* Do one big resize at the start, rather than
653 * incrementally resizing as we insert new keys. Expect
654 * that there will be no (or few) overlapping keys.
655 */
656 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
657 if (set_table_resize(so, (so->used + other->used)*2) != 0)
658 return -1;
659 }
660 for (i = 0; i <= other->mask; i++) {
661 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000662 key = entry->key;
663 if (key != NULL &&
664 key != dummy) {
665 Py_INCREF(key);
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000666 if (set_insert_key(so, key, entry->hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000667 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 return -1;
669 }
670 }
671 }
672 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673}
674
675static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000676set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000677{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000678 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 if (!PyUnicode_CheckExact(key) ||
682 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
683 hash = PyObject_Hash(key);
684 if (hash == -1)
685 return -1;
686 }
687 entry = (so->lookup)(so, key, hash);
688 if (entry == NULL)
689 return -1;
690 key = entry->key;
691 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000692}
693
Raymond Hettingerc991db22005-08-11 07:58:45 +0000694static int
695set_contains_entry(PySetObject *so, setentry *entry)
696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 PyObject *key;
698 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000699
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000700 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (lu_entry == NULL)
702 return -1;
703 key = lu_entry->key;
704 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000705}
706
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000707static PyObject *
708set_pop(PySetObject *so)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 register Py_ssize_t i = 0;
711 register setentry *entry;
712 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 assert (PyAnySet_Check(so));
715 if (so->used == 0) {
716 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
717 return NULL;
718 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 /* Set entry to "the first" unused or dummy set entry. We abuse
721 * the hash field of slot 0 to hold a search finger:
722 * If slot 0 has a value, use slot 0.
723 * Else slot 0 is being used to hold a search finger,
724 * and we use its hash value as the first index to look.
725 */
726 entry = &so->table[0];
727 if (entry->key == NULL || entry->key == dummy) {
728 i = entry->hash;
729 /* The hash field may be a real hash value, or it may be a
730 * legit search finger, or it may be a once-legit search
731 * finger that's out of bounds now because it wrapped around
732 * or the table shrunk -- simply make sure it's in bounds now.
733 */
734 if (i > so->mask || i < 1)
735 i = 1; /* skip slot 0 */
736 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
737 i++;
738 if (i > so->mask)
739 i = 1;
740 }
741 }
742 key = entry->key;
743 Py_INCREF(dummy);
744 entry->key = dummy;
745 so->used--;
746 so->table[0].hash = i + 1; /* next place to start */
747 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000748}
749
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000750PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
751Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000752
753static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000754set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 Py_ssize_t pos = 0;
757 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 while (set_next(so, &pos, &entry))
760 Py_VISIT(entry->key);
761 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000762}
763
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000764static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000765frozenset_hash(PyObject *self)
766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 PySetObject *so = (PySetObject *)self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000768 Py_hash_t h, hash = 1927868237L;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 setentry *entry;
770 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 if (so->hash != -1)
773 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000774
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000775 hash *= PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 while (set_next(so, &pos, &entry)) {
777 /* Work to increase the bit dispersion for closely spaced hash
778 values. The is important because some use cases have many
779 combinations of a small number of elements with nearby
780 hashes so that many distinct combinations collapse to only
781 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000782 h = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
784 }
785 hash = hash * 69069L + 907133923L;
786 if (hash == -1)
787 hash = 590923713L;
788 so->hash = hash;
789 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000790}
791
Raymond Hettingera9d99362005-08-05 00:01:15 +0000792/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 PyObject_HEAD
796 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
797 Py_ssize_t si_used;
798 Py_ssize_t si_pos;
799 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800} setiterobject;
801
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802static void
803setiter_dealloc(setiterobject *si)
804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 Py_XDECREF(si->si_set);
806 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000807}
808
809static int
810setiter_traverse(setiterobject *si, visitproc visit, void *arg)
811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 Py_VISIT(si->si_set);
813 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000814}
815
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000816static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000817setiter_len(setiterobject *si)
818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 Py_ssize_t len = 0;
820 if (si->si_set != NULL && si->si_used == si->si_set->used)
821 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000822 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000823}
824
Armin Rigof5b3e362006-02-11 21:32:43 +0000825PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000826
827static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
829 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000830};
831
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000832static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 PyObject *key;
835 register Py_ssize_t i, mask;
836 register setentry *entry;
837 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 if (so == NULL)
840 return NULL;
841 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 if (si->si_used != so->used) {
844 PyErr_SetString(PyExc_RuntimeError,
845 "Set changed size during iteration");
846 si->si_used = -1; /* Make this state sticky */
847 return NULL;
848 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 i = si->si_pos;
851 assert(i>=0);
852 entry = so->table;
853 mask = so->mask;
854 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
855 i++;
856 si->si_pos = i+1;
857 if (i > mask)
858 goto fail;
859 si->len--;
860 key = entry[i].key;
861 Py_INCREF(key);
862 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000863
864fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 Py_DECREF(so);
866 si->si_set = NULL;
867 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868}
869
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000870PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 PyVarObject_HEAD_INIT(&PyType_Type, 0)
872 "set_iterator", /* tp_name */
873 sizeof(setiterobject), /* tp_basicsize */
874 0, /* tp_itemsize */
875 /* methods */
876 (destructor)setiter_dealloc, /* tp_dealloc */
877 0, /* tp_print */
878 0, /* tp_getattr */
879 0, /* tp_setattr */
880 0, /* tp_reserved */
881 0, /* tp_repr */
882 0, /* tp_as_number */
883 0, /* tp_as_sequence */
884 0, /* tp_as_mapping */
885 0, /* tp_hash */
886 0, /* tp_call */
887 0, /* tp_str */
888 PyObject_GenericGetAttr, /* tp_getattro */
889 0, /* tp_setattro */
890 0, /* tp_as_buffer */
891 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
892 0, /* tp_doc */
893 (traverseproc)setiter_traverse, /* tp_traverse */
894 0, /* tp_clear */
895 0, /* tp_richcompare */
896 0, /* tp_weaklistoffset */
897 PyObject_SelfIter, /* tp_iter */
898 (iternextfunc)setiter_iternext, /* tp_iternext */
899 setiter_methods, /* tp_methods */
900 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000901};
902
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000903static PyObject *
904set_iter(PySetObject *so)
905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
907 if (si == NULL)
908 return NULL;
909 Py_INCREF(so);
910 si->si_set = so;
911 si->si_used = so->used;
912 si->si_pos = 0;
913 si->len = so->used;
914 _PyObject_GC_TRACK(si);
915 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000916}
917
Raymond Hettingerd7946662005-08-01 21:39:29 +0000918static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000919set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 if (PyAnySet_Check(other))
924 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (PyDict_CheckExact(other)) {
927 PyObject *value;
928 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000929 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 /* Do one big resize at the start, rather than
933 * incrementally resizing as we insert new keys. Expect
934 * that there will be no (or few) overlapping keys.
935 */
936 if (dictsize == -1)
937 return -1;
938 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
939 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
940 return -1;
941 }
942 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
943 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 an_entry.hash = hash;
946 an_entry.key = key;
947 if (set_add_entry(so, &an_entry) == -1)
948 return -1;
949 }
950 return 0;
951 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 it = PyObject_GetIter(other);
954 if (it == NULL)
955 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 while ((key = PyIter_Next(it)) != NULL) {
958 if (set_add_key(so, key) == -1) {
959 Py_DECREF(it);
960 Py_DECREF(key);
961 return -1;
962 }
963 Py_DECREF(key);
964 }
965 Py_DECREF(it);
966 if (PyErr_Occurred())
967 return -1;
968 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000969}
970
971static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000972set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
977 PyObject *other = PyTuple_GET_ITEM(args, i);
978 if (set_update_internal(so, other) == -1)
979 return NULL;
980 }
981 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000982}
983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000985"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000986
987static PyObject *
988make_new_set(PyTypeObject *type, PyObject *iterable)
989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 if (dummy == NULL) { /* Auto-initialize dummy */
993 dummy = PyUnicode_FromString("<dummy key>");
994 if (dummy == NULL)
995 return NULL;
996 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 /* create PySetObject structure */
999 if (numfree &&
1000 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1001 so = free_list[--numfree];
1002 assert (so != NULL && PyAnySet_CheckExact(so));
1003 Py_TYPE(so) = type;
1004 _Py_NewReference((PyObject *)so);
1005 EMPTY_TO_MINSIZE(so);
1006 PyObject_GC_Track(so);
1007 } else {
1008 so = (PySetObject *)type->tp_alloc(type, 0);
1009 if (so == NULL)
1010 return NULL;
1011 /* tp_alloc has already zeroed the structure */
1012 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1013 INIT_NONZERO_SET_SLOTS(so);
1014 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 so->lookup = set_lookkey_unicode;
1017 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 if (iterable != NULL) {
1020 if (set_update_internal(so, iterable) == -1) {
1021 Py_DECREF(so);
1022 return NULL;
1023 }
1024 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001027}
1028
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001029static PyObject *
1030make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1033 if (PyType_IsSubtype(type, &PySet_Type))
1034 type = &PySet_Type;
1035 else
1036 type = &PyFrozenSet_Type;
1037 }
1038 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001039}
1040
Raymond Hettingerd7946662005-08-01 21:39:29 +00001041/* The empty frozenset is a singleton */
1042static PyObject *emptyfrozenset = NULL;
1043
Raymond Hettingera690a992003-11-16 16:17:49 +00001044static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001045frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1050 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1053 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 if (type != &PyFrozenSet_Type)
1056 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 if (iterable != NULL) {
1059 /* frozenset(f) is idempotent */
1060 if (PyFrozenSet_CheckExact(iterable)) {
1061 Py_INCREF(iterable);
1062 return iterable;
1063 }
1064 result = make_new_set(type, iterable);
1065 if (result == NULL || PySet_GET_SIZE(result))
1066 return result;
1067 Py_DECREF(result);
1068 }
1069 /* The empty frozenset is a singleton */
1070 if (emptyfrozenset == NULL)
1071 emptyfrozenset = make_new_set(type, NULL);
1072 Py_XINCREF(emptyfrozenset);
1073 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001074}
1075
1076void
1077PySet_Fini(void)
1078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 while (numfree) {
1082 numfree--;
1083 so = free_list[numfree];
1084 PyObject_GC_Del(so);
1085 }
1086 Py_CLEAR(dummy);
1087 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001088}
1089
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001090static PyObject *
1091set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1094 return NULL;
1095
1096 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001097}
1098
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001099/* set_swap_bodies() switches the contents of any two sets by moving their
1100 internal data pointers and, if needed, copying the internal smalltables.
1101 Semantically equivalent to:
1102
1103 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1104
1105 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001107 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001109 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001110*/
1111
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001112static void
1113set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 Py_ssize_t t;
1116 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001117 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001119 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 t = a->fill; a->fill = b->fill; b->fill = t;
1122 t = a->used; a->used = b->used; b->used = t;
1123 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 u = a->table;
1126 if (a->table == a->smalltable)
1127 u = b->smalltable;
1128 a->table = b->table;
1129 if (b->table == b->smalltable)
1130 a->table = a->smalltable;
1131 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 if (a->table == a->smalltable || b->table == b->smalltable) {
1136 memcpy(tab, a->smalltable, sizeof(tab));
1137 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1138 memcpy(b->smalltable, tab, sizeof(tab));
1139 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1142 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1143 h = a->hash; a->hash = b->hash; b->hash = h;
1144 } else {
1145 a->hash = -1;
1146 b->hash = -1;
1147 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001148}
1149
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001150static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001151set_copy(PySetObject *so)
1152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001154}
1155
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001156static PyObject *
1157frozenset_copy(PySetObject *so)
1158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (PyFrozenSet_CheckExact(so)) {
1160 Py_INCREF(so);
1161 return (PyObject *)so;
1162 }
1163 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001164}
1165
Raymond Hettingera690a992003-11-16 16:17:49 +00001166PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1167
1168static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001169set_clear(PySetObject *so)
1170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 set_clear_internal(so);
1172 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001173}
1174
1175PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1176
1177static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001178set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 PySetObject *result;
1181 PyObject *other;
1182 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 result = (PySetObject *)set_copy(so);
1185 if (result == NULL)
1186 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1189 other = PyTuple_GET_ITEM(args, i);
1190 if ((PyObject *)so == other)
1191 continue;
1192 if (set_update_internal(result, other) == -1) {
1193 Py_DECREF(result);
1194 return NULL;
1195 }
1196 }
1197 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001198}
1199
1200PyDoc_STRVAR(union_doc,
1201 "Return the union of sets as a new set.\n\
1202\n\
1203(i.e. all elements that are in either set.)");
1204
1205static PyObject *
1206set_or(PySetObject *so, PyObject *other)
1207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1211 Py_INCREF(Py_NotImplemented);
1212 return Py_NotImplemented;
1213 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 result = (PySetObject *)set_copy(so);
1216 if (result == NULL)
1217 return NULL;
1218 if ((PyObject *)so == other)
1219 return (PyObject *)result;
1220 if (set_update_internal(result, other) == -1) {
1221 Py_DECREF(result);
1222 return NULL;
1223 }
1224 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001225}
1226
Raymond Hettingera690a992003-11-16 16:17:49 +00001227static PyObject *
1228set_ior(PySetObject *so, PyObject *other)
1229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 if (!PyAnySet_Check(other)) {
1231 Py_INCREF(Py_NotImplemented);
1232 return Py_NotImplemented;
1233 }
1234 if (set_update_internal(so, other) == -1)
1235 return NULL;
1236 Py_INCREF(so);
1237 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001238}
1239
1240static PyObject *
1241set_intersection(PySetObject *so, PyObject *other)
1242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 PySetObject *result;
1244 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 if ((PyObject *)so == other)
1247 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1250 if (result == NULL)
1251 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (PyAnySet_Check(other)) {
1254 Py_ssize_t pos = 0;
1255 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1258 tmp = (PyObject *)so;
1259 so = (PySetObject *)other;
1260 other = tmp;
1261 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 while (set_next((PySetObject *)other, &pos, &entry)) {
1264 int rv = set_contains_entry(so, entry);
1265 if (rv == -1) {
1266 Py_DECREF(result);
1267 return NULL;
1268 }
1269 if (rv) {
1270 if (set_add_entry(result, entry) == -1) {
1271 Py_DECREF(result);
1272 return NULL;
1273 }
1274 }
1275 }
1276 return (PyObject *)result;
1277 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 it = PyObject_GetIter(other);
1280 if (it == NULL) {
1281 Py_DECREF(result);
1282 return NULL;
1283 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 while ((key = PyIter_Next(it)) != NULL) {
1286 int rv;
1287 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001288 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (hash == -1) {
1291 Py_DECREF(it);
1292 Py_DECREF(result);
1293 Py_DECREF(key);
1294 return NULL;
1295 }
1296 entry.hash = hash;
1297 entry.key = key;
1298 rv = set_contains_entry(so, &entry);
1299 if (rv == -1) {
1300 Py_DECREF(it);
1301 Py_DECREF(result);
1302 Py_DECREF(key);
1303 return NULL;
1304 }
1305 if (rv) {
1306 if (set_add_entry(result, &entry) == -1) {
1307 Py_DECREF(it);
1308 Py_DECREF(result);
1309 Py_DECREF(key);
1310 return NULL;
1311 }
1312 }
1313 Py_DECREF(key);
1314 }
1315 Py_DECREF(it);
1316 if (PyErr_Occurred()) {
1317 Py_DECREF(result);
1318 return NULL;
1319 }
1320 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001321}
1322
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001323static PyObject *
1324set_intersection_multi(PySetObject *so, PyObject *args)
1325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 Py_ssize_t i;
1327 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 if (PyTuple_GET_SIZE(args) == 0)
1330 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 Py_INCREF(so);
1333 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1334 PyObject *other = PyTuple_GET_ITEM(args, i);
1335 PyObject *newresult = set_intersection((PySetObject *)result, other);
1336 if (newresult == NULL) {
1337 Py_DECREF(result);
1338 return NULL;
1339 }
1340 Py_DECREF(result);
1341 result = newresult;
1342 }
1343 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001344}
1345
Raymond Hettingera690a992003-11-16 16:17:49 +00001346PyDoc_STRVAR(intersection_doc,
1347"Return the intersection of two sets as a new set.\n\
1348\n\
1349(i.e. all elements that are in both sets.)");
1350
1351static PyObject *
1352set_intersection_update(PySetObject *so, PyObject *other)
1353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 tmp = set_intersection(so, other);
1357 if (tmp == NULL)
1358 return NULL;
1359 set_swap_bodies(so, (PySetObject *)tmp);
1360 Py_DECREF(tmp);
1361 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001362}
1363
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001364static PyObject *
1365set_intersection_update_multi(PySetObject *so, PyObject *args)
1366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 tmp = set_intersection_multi(so, args);
1370 if (tmp == NULL)
1371 return NULL;
1372 set_swap_bodies(so, (PySetObject *)tmp);
1373 Py_DECREF(tmp);
1374 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001375}
1376
Raymond Hettingera690a992003-11-16 16:17:49 +00001377PyDoc_STRVAR(intersection_update_doc,
1378"Update a set with the intersection of itself and another.");
1379
1380static PyObject *
1381set_and(PySetObject *so, PyObject *other)
1382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1384 Py_INCREF(Py_NotImplemented);
1385 return Py_NotImplemented;
1386 }
1387 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001388}
1389
1390static PyObject *
1391set_iand(PySetObject *so, PyObject *other)
1392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 if (!PyAnySet_Check(other)) {
1396 Py_INCREF(Py_NotImplemented);
1397 return Py_NotImplemented;
1398 }
1399 result = set_intersection_update(so, other);
1400 if (result == NULL)
1401 return NULL;
1402 Py_DECREF(result);
1403 Py_INCREF(so);
1404 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001405}
1406
Guido van Rossum58da9312007-11-10 23:39:45 +00001407static PyObject *
1408set_isdisjoint(PySetObject *so, PyObject *other)
1409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if ((PyObject *)so == other) {
1413 if (PySet_GET_SIZE(so) == 0)
1414 Py_RETURN_TRUE;
1415 else
1416 Py_RETURN_FALSE;
1417 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (PyAnySet_CheckExact(other)) {
1420 Py_ssize_t pos = 0;
1421 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1424 tmp = (PyObject *)so;
1425 so = (PySetObject *)other;
1426 other = tmp;
1427 }
1428 while (set_next((PySetObject *)other, &pos, &entry)) {
1429 int rv = set_contains_entry(so, entry);
1430 if (rv == -1)
1431 return NULL;
1432 if (rv)
1433 Py_RETURN_FALSE;
1434 }
1435 Py_RETURN_TRUE;
1436 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 it = PyObject_GetIter(other);
1439 if (it == NULL)
1440 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 while ((key = PyIter_Next(it)) != NULL) {
1443 int rv;
1444 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001445 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (hash == -1) {
1448 Py_DECREF(key);
1449 Py_DECREF(it);
1450 return NULL;
1451 }
1452 entry.hash = hash;
1453 entry.key = key;
1454 rv = set_contains_entry(so, &entry);
1455 Py_DECREF(key);
1456 if (rv == -1) {
1457 Py_DECREF(it);
1458 return NULL;
1459 }
1460 if (rv) {
1461 Py_DECREF(it);
1462 Py_RETURN_FALSE;
1463 }
1464 }
1465 Py_DECREF(it);
1466 if (PyErr_Occurred())
1467 return NULL;
1468 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001469}
1470
1471PyDoc_STRVAR(isdisjoint_doc,
1472"Return True if two sets have a null intersection.");
1473
Neal Norwitz6576bd82005-11-13 18:41:28 +00001474static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001475set_difference_update_internal(PySetObject *so, PyObject *other)
1476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if ((PyObject *)so == other)
1478 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 if (PyAnySet_Check(other)) {
1481 setentry *entry;
1482 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 while (set_next((PySetObject *)other, &pos, &entry))
1485 if (set_discard_entry(so, entry) == -1)
1486 return -1;
1487 } else {
1488 PyObject *key, *it;
1489 it = PyObject_GetIter(other);
1490 if (it == NULL)
1491 return -1;
1492
1493 while ((key = PyIter_Next(it)) != NULL) {
1494 if (set_discard_key(so, key) == -1) {
1495 Py_DECREF(it);
1496 Py_DECREF(key);
1497 return -1;
1498 }
1499 Py_DECREF(key);
1500 }
1501 Py_DECREF(it);
1502 if (PyErr_Occurred())
1503 return -1;
1504 }
1505 /* If more than 1/5 are dummies, then resize them away. */
1506 if ((so->fill - so->used) * 5 < so->mask)
1507 return 0;
1508 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001509}
1510
Raymond Hettingera690a992003-11-16 16:17:49 +00001511static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001512set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1517 PyObject *other = PyTuple_GET_ITEM(args, i);
1518 if (set_difference_update_internal(so, other) == -1)
1519 return NULL;
1520 }
1521 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001522}
1523
1524PyDoc_STRVAR(difference_update_doc,
1525"Remove all elements of another set from this set.");
1526
1527static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001528set_copy_and_difference(PySetObject *so, PyObject *other)
1529{
1530 PyObject *result;
1531
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}
1540
1541static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001542set_difference(PySetObject *so, PyObject *other)
1543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 PyObject *result;
1545 setentry *entry;
1546 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001549 return set_copy_and_difference(so, other);
1550 }
1551
1552 /* If len(so) much more than len(other), it's more efficient to simply copy
1553 * so and then iterate other looking for common elements. */
1554 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1555 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 result = make_new_set_basetype(Py_TYPE(so), NULL);
1559 if (result == NULL)
1560 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (PyDict_CheckExact(other)) {
1563 while (set_next(so, &pos, &entry)) {
1564 setentry entrycopy;
1565 entrycopy.hash = entry->hash;
1566 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001567 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1569 Py_DECREF(result);
1570 return NULL;
1571 }
1572 }
1573 }
1574 return result;
1575 }
1576
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001577 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 while (set_next(so, &pos, &entry)) {
1579 int rv = set_contains_entry((PySetObject *)other, entry);
1580 if (rv == -1) {
1581 Py_DECREF(result);
1582 return NULL;
1583 }
1584 if (!rv) {
1585 if (set_add_entry((PySetObject *)result, entry) == -1) {
1586 Py_DECREF(result);
1587 return NULL;
1588 }
1589 }
1590 }
1591 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001592}
1593
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001594static PyObject *
1595set_difference_multi(PySetObject *so, PyObject *args)
1596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 Py_ssize_t i;
1598 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 if (PyTuple_GET_SIZE(args) == 0)
1601 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 other = PyTuple_GET_ITEM(args, 0);
1604 result = set_difference(so, other);
1605 if (result == NULL)
1606 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1609 other = PyTuple_GET_ITEM(args, i);
1610 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1611 Py_DECREF(result);
1612 return NULL;
1613 }
1614 }
1615 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001616}
1617
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001618PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001619"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001620\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001621(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001622static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001623set_sub(PySetObject *so, PyObject *other)
1624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1626 Py_INCREF(Py_NotImplemented);
1627 return Py_NotImplemented;
1628 }
1629 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001630}
1631
1632static PyObject *
1633set_isub(PySetObject *so, PyObject *other)
1634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 if (!PyAnySet_Check(other)) {
1636 Py_INCREF(Py_NotImplemented);
1637 return Py_NotImplemented;
1638 }
1639 if (set_difference_update_internal(so, other) == -1)
1640 return NULL;
1641 Py_INCREF(so);
1642 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001643}
1644
1645static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001646set_symmetric_difference_update(PySetObject *so, PyObject *other)
1647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 PySetObject *otherset;
1649 PyObject *key;
1650 Py_ssize_t pos = 0;
1651 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 if ((PyObject *)so == other)
1654 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 if (PyDict_CheckExact(other)) {
1657 PyObject *value;
1658 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001659 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1661 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001662
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001663 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 an_entry.hash = hash;
1665 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001668 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001669 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001672 if (rv == DISCARD_NOTFOUND) {
1673 if (set_add_entry(so, &an_entry) == -1) {
1674 Py_DECREF(key);
1675 return NULL;
1676 }
1677 }
Georg Brandl2d444492010-09-03 10:52:55 +00001678 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 }
1680 Py_RETURN_NONE;
1681 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 if (PyAnySet_Check(other)) {
1684 Py_INCREF(other);
1685 otherset = (PySetObject *)other;
1686 } else {
1687 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1688 if (otherset == NULL)
1689 return NULL;
1690 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 while (set_next(otherset, &pos, &entry)) {
1693 int rv = set_discard_entry(so, entry);
1694 if (rv == -1) {
1695 Py_DECREF(otherset);
1696 return NULL;
1697 }
1698 if (rv == DISCARD_NOTFOUND) {
1699 if (set_add_entry(so, entry) == -1) {
1700 Py_DECREF(otherset);
1701 return NULL;
1702 }
1703 }
1704 }
1705 Py_DECREF(otherset);
1706 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001707}
1708
1709PyDoc_STRVAR(symmetric_difference_update_doc,
1710"Update a set with the symmetric difference of itself and another.");
1711
1712static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001713set_symmetric_difference(PySetObject *so, PyObject *other)
1714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 PyObject *rv;
1716 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1719 if (otherset == NULL)
1720 return NULL;
1721 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1722 if (rv == NULL)
1723 return NULL;
1724 Py_DECREF(rv);
1725 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001726}
1727
1728PyDoc_STRVAR(symmetric_difference_doc,
1729"Return the symmetric difference of two sets as a new set.\n\
1730\n\
1731(i.e. all elements that are in exactly one of the sets.)");
1732
1733static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001734set_xor(PySetObject *so, PyObject *other)
1735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1737 Py_INCREF(Py_NotImplemented);
1738 return Py_NotImplemented;
1739 }
1740 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001741}
1742
1743static PyObject *
1744set_ixor(PySetObject *so, PyObject *other)
1745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 if (!PyAnySet_Check(other)) {
1749 Py_INCREF(Py_NotImplemented);
1750 return Py_NotImplemented;
1751 }
1752 result = set_symmetric_difference_update(so, other);
1753 if (result == NULL)
1754 return NULL;
1755 Py_DECREF(result);
1756 Py_INCREF(so);
1757 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001758}
1759
1760static PyObject *
1761set_issubset(PySetObject *so, PyObject *other)
1762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 setentry *entry;
1764 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 if (!PyAnySet_Check(other)) {
1767 PyObject *tmp, *result;
1768 tmp = make_new_set(&PySet_Type, other);
1769 if (tmp == NULL)
1770 return NULL;
1771 result = set_issubset(so, tmp);
1772 Py_DECREF(tmp);
1773 return result;
1774 }
1775 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1776 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 while (set_next(so, &pos, &entry)) {
1779 int rv = set_contains_entry((PySetObject *)other, entry);
1780 if (rv == -1)
1781 return NULL;
1782 if (!rv)
1783 Py_RETURN_FALSE;
1784 }
1785 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001786}
1787
1788PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1789
1790static PyObject *
1791set_issuperset(PySetObject *so, PyObject *other)
1792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 if (!PyAnySet_Check(other)) {
1796 tmp = make_new_set(&PySet_Type, other);
1797 if (tmp == NULL)
1798 return NULL;
1799 result = set_issuperset(so, tmp);
1800 Py_DECREF(tmp);
1801 return result;
1802 }
1803 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001804}
1805
1806PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1807
Raymond Hettingera690a992003-11-16 16:17:49 +00001808static PyObject *
1809set_richcompare(PySetObject *v, PyObject *w, int op)
1810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 if(!PyAnySet_Check(w)) {
1814 Py_INCREF(Py_NotImplemented);
1815 return Py_NotImplemented;
1816 }
1817 switch (op) {
1818 case Py_EQ:
1819 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1820 Py_RETURN_FALSE;
1821 if (v->hash != -1 &&
1822 ((PySetObject *)w)->hash != -1 &&
1823 v->hash != ((PySetObject *)w)->hash)
1824 Py_RETURN_FALSE;
1825 return set_issubset(v, w);
1826 case Py_NE:
1827 r1 = set_richcompare(v, w, Py_EQ);
1828 if (r1 == NULL)
1829 return NULL;
1830 r2 = PyBool_FromLong(PyObject_Not(r1));
1831 Py_DECREF(r1);
1832 return r2;
1833 case Py_LE:
1834 return set_issubset(v, w);
1835 case Py_GE:
1836 return set_issuperset(v, w);
1837 case Py_LT:
1838 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1839 Py_RETURN_FALSE;
1840 return set_issubset(v, w);
1841 case Py_GT:
1842 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1843 Py_RETURN_FALSE;
1844 return set_issuperset(v, w);
1845 }
1846 Py_INCREF(Py_NotImplemented);
1847 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001848}
1849
Raymond Hettingera690a992003-11-16 16:17:49 +00001850static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001851set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 if (set_add_key(so, key) == -1)
1854 return NULL;
1855 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001856}
1857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001859"Add an element to a set.\n\
1860\n\
1861This has no effect if the element is already present.");
1862
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001863static int
1864set_contains(PySetObject *so, PyObject *key)
1865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 PyObject *tmpkey;
1867 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 rv = set_contains_key(so, key);
1870 if (rv == -1) {
1871 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1872 return -1;
1873 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001874 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 if (tmpkey == NULL)
1876 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 rv = set_contains(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 Py_DECREF(tmpkey);
1879 }
1880 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001881}
1882
1883static PyObject *
1884set_direct_contains(PySetObject *so, PyObject *key)
1885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 result = set_contains(so, key);
1889 if (result == -1)
1890 return NULL;
1891 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001892}
1893
1894PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1895
Raymond Hettingera690a992003-11-16 16:17:49 +00001896static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001897set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 PyObject *tmpkey;
1900 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 rv = set_discard_key(so, key);
1903 if (rv == -1) {
1904 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1905 return NULL;
1906 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001907 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 if (tmpkey == NULL)
1909 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 Py_DECREF(tmpkey);
1912 if (rv == -1)
1913 return NULL;
1914 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 if (rv == DISCARD_NOTFOUND) {
1917 set_key_error(key);
1918 return NULL;
1919 }
1920 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001921}
1922
1923PyDoc_STRVAR(remove_doc,
1924"Remove an element from a set; it must be a member.\n\
1925\n\
1926If the element is not a member, raise a KeyError.");
1927
1928static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001929set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 PyObject *tmpkey, *result;
1932 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 rv = set_discard_key(so, key);
1935 if (rv == -1) {
1936 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1937 return NULL;
1938 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001939 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 if (tmpkey == NULL)
1941 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 result = set_discard(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 Py_DECREF(tmpkey);
1944 return result;
1945 }
1946 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001947}
1948
1949PyDoc_STRVAR(discard_doc,
1950"Remove an element from a set if it is a member.\n\
1951\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001953
1954static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001955set_reduce(PySetObject *so)
1956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 keys = PySequence_List((PyObject *)so);
1960 if (keys == NULL)
1961 goto done;
1962 args = PyTuple_Pack(1, keys);
1963 if (args == NULL)
1964 goto done;
1965 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1966 if (dict == NULL) {
1967 PyErr_Clear();
1968 dict = Py_None;
1969 Py_INCREF(dict);
1970 }
1971 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001972done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 Py_XDECREF(args);
1974 Py_XDECREF(keys);
1975 Py_XDECREF(dict);
1976 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001977}
1978
1979PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1980
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001981static PyObject *
1982set_sizeof(PySetObject *so)
1983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 res = sizeof(PySetObject);
1987 if (so->table != so->smalltable)
1988 res = res + (so->mask + 1) * sizeof(setentry);
1989 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001990}
1991
1992PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001993static int
1994set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 if (!PyAnySet_Check(self))
1999 return -1;
2000 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2001 return -1;
2002 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2003 return -1;
2004 set_clear_internal(self);
2005 self->hash = -1;
2006 if (iterable == NULL)
2007 return 0;
2008 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002009}
2010
Raymond Hettingera690a992003-11-16 16:17:49 +00002011static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 set_len, /* sq_length */
2013 0, /* sq_concat */
2014 0, /* sq_repeat */
2015 0, /* sq_item */
2016 0, /* sq_slice */
2017 0, /* sq_ass_item */
2018 0, /* sq_ass_slice */
2019 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002020};
2021
2022/* set object ********************************************************/
2023
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002024#ifdef Py_DEBUG
2025static PyObject *test_c_api(PySetObject *so);
2026
2027PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2028All is well if assertions don't fail.");
2029#endif
2030
Raymond Hettingera690a992003-11-16 16:17:49 +00002031static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 {"add", (PyCFunction)set_add, METH_O,
2033 add_doc},
2034 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2035 clear_doc},
2036 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2037 contains_doc},
2038 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2039 copy_doc},
2040 {"discard", (PyCFunction)set_discard, METH_O,
2041 discard_doc},
2042 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2043 difference_doc},
2044 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2045 difference_update_doc},
2046 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2047 intersection_doc},
2048 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2049 intersection_update_doc},
2050 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2051 isdisjoint_doc},
2052 {"issubset", (PyCFunction)set_issubset, METH_O,
2053 issubset_doc},
2054 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2055 issuperset_doc},
2056 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2057 pop_doc},
2058 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2059 reduce_doc},
2060 {"remove", (PyCFunction)set_remove, METH_O,
2061 remove_doc},
2062 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2063 sizeof_doc},
2064 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2065 symmetric_difference_doc},
2066 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2067 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002068#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2070 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002071#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 {"union", (PyCFunction)set_union, METH_VARARGS,
2073 union_doc},
2074 {"update", (PyCFunction)set_update, METH_VARARGS,
2075 update_doc},
2076 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002077};
2078
2079static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 0, /*nb_add*/
2081 (binaryfunc)set_sub, /*nb_subtract*/
2082 0, /*nb_multiply*/
2083 0, /*nb_remainder*/
2084 0, /*nb_divmod*/
2085 0, /*nb_power*/
2086 0, /*nb_negative*/
2087 0, /*nb_positive*/
2088 0, /*nb_absolute*/
2089 0, /*nb_bool*/
2090 0, /*nb_invert*/
2091 0, /*nb_lshift*/
2092 0, /*nb_rshift*/
2093 (binaryfunc)set_and, /*nb_and*/
2094 (binaryfunc)set_xor, /*nb_xor*/
2095 (binaryfunc)set_or, /*nb_or*/
2096 0, /*nb_int*/
2097 0, /*nb_reserved*/
2098 0, /*nb_float*/
2099 0, /*nb_inplace_add*/
2100 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2101 0, /*nb_inplace_multiply*/
2102 0, /*nb_inplace_remainder*/
2103 0, /*nb_inplace_power*/
2104 0, /*nb_inplace_lshift*/
2105 0, /*nb_inplace_rshift*/
2106 (binaryfunc)set_iand, /*nb_inplace_and*/
2107 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2108 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002109};
2110
2111PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002112"set() -> new empty set object\n\
2113set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002114\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002115Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002116
2117PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2119 "set", /* tp_name */
2120 sizeof(PySetObject), /* tp_basicsize */
2121 0, /* tp_itemsize */
2122 /* methods */
2123 (destructor)set_dealloc, /* tp_dealloc */
2124 0, /* tp_print */
2125 0, /* tp_getattr */
2126 0, /* tp_setattr */
2127 0, /* tp_reserved */
2128 (reprfunc)set_repr, /* tp_repr */
2129 &set_as_number, /* tp_as_number */
2130 &set_as_sequence, /* tp_as_sequence */
2131 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002132 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 0, /* tp_call */
2134 0, /* tp_str */
2135 PyObject_GenericGetAttr, /* tp_getattro */
2136 0, /* tp_setattro */
2137 0, /* tp_as_buffer */
2138 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2139 Py_TPFLAGS_BASETYPE, /* tp_flags */
2140 set_doc, /* tp_doc */
2141 (traverseproc)set_traverse, /* tp_traverse */
2142 (inquiry)set_clear_internal, /* tp_clear */
2143 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002144 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2145 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 0, /* tp_iternext */
2147 set_methods, /* tp_methods */
2148 0, /* tp_members */
2149 0, /* tp_getset */
2150 0, /* tp_base */
2151 0, /* tp_dict */
2152 0, /* tp_descr_get */
2153 0, /* tp_descr_set */
2154 0, /* tp_dictoffset */
2155 (initproc)set_init, /* tp_init */
2156 PyType_GenericAlloc, /* tp_alloc */
2157 set_new, /* tp_new */
2158 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002159};
2160
2161/* frozenset object ********************************************************/
2162
2163
2164static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2166 contains_doc},
2167 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2168 copy_doc},
2169 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2170 difference_doc},
2171 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2172 intersection_doc},
2173 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2174 isdisjoint_doc},
2175 {"issubset", (PyCFunction)set_issubset, METH_O,
2176 issubset_doc},
2177 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2178 issuperset_doc},
2179 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2180 reduce_doc},
2181 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2182 sizeof_doc},
2183 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2184 symmetric_difference_doc},
2185 {"union", (PyCFunction)set_union, METH_VARARGS,
2186 union_doc},
2187 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002188};
2189
2190static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 0, /*nb_add*/
2192 (binaryfunc)set_sub, /*nb_subtract*/
2193 0, /*nb_multiply*/
2194 0, /*nb_remainder*/
2195 0, /*nb_divmod*/
2196 0, /*nb_power*/
2197 0, /*nb_negative*/
2198 0, /*nb_positive*/
2199 0, /*nb_absolute*/
2200 0, /*nb_bool*/
2201 0, /*nb_invert*/
2202 0, /*nb_lshift*/
2203 0, /*nb_rshift*/
2204 (binaryfunc)set_and, /*nb_and*/
2205 (binaryfunc)set_xor, /*nb_xor*/
2206 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002207};
2208
2209PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002210"frozenset() -> empty frozenset object\n\
2211frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002212\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002213Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002214
2215PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2217 "frozenset", /* tp_name */
2218 sizeof(PySetObject), /* tp_basicsize */
2219 0, /* tp_itemsize */
2220 /* methods */
2221 (destructor)set_dealloc, /* tp_dealloc */
2222 0, /* tp_print */
2223 0, /* tp_getattr */
2224 0, /* tp_setattr */
2225 0, /* tp_reserved */
2226 (reprfunc)set_repr, /* tp_repr */
2227 &frozenset_as_number, /* tp_as_number */
2228 &set_as_sequence, /* tp_as_sequence */
2229 0, /* tp_as_mapping */
2230 frozenset_hash, /* tp_hash */
2231 0, /* tp_call */
2232 0, /* tp_str */
2233 PyObject_GenericGetAttr, /* tp_getattro */
2234 0, /* tp_setattro */
2235 0, /* tp_as_buffer */
2236 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2237 Py_TPFLAGS_BASETYPE, /* tp_flags */
2238 frozenset_doc, /* tp_doc */
2239 (traverseproc)set_traverse, /* tp_traverse */
2240 (inquiry)set_clear_internal, /* tp_clear */
2241 (richcmpfunc)set_richcompare, /* tp_richcompare */
2242 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2243 (getiterfunc)set_iter, /* tp_iter */
2244 0, /* tp_iternext */
2245 frozenset_methods, /* tp_methods */
2246 0, /* tp_members */
2247 0, /* tp_getset */
2248 0, /* tp_base */
2249 0, /* tp_dict */
2250 0, /* tp_descr_get */
2251 0, /* tp_descr_set */
2252 0, /* tp_dictoffset */
2253 0, /* tp_init */
2254 PyType_GenericAlloc, /* tp_alloc */
2255 frozenset_new, /* tp_new */
2256 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002257};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002258
2259
2260/***** C API functions *************************************************/
2261
2262PyObject *
2263PySet_New(PyObject *iterable)
2264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002266}
2267
2268PyObject *
2269PyFrozenSet_New(PyObject *iterable)
2270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002272}
2273
Neal Norwitz8c49c822006-03-04 18:41:19 +00002274Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002275PySet_Size(PyObject *anyset)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 if (!PyAnySet_Check(anyset)) {
2278 PyErr_BadInternalCall();
2279 return -1;
2280 }
2281 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002282}
2283
2284int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002285PySet_Clear(PyObject *set)
2286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 if (!PySet_Check(set)) {
2288 PyErr_BadInternalCall();
2289 return -1;
2290 }
2291 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002292}
2293
2294int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002295PySet_Contains(PyObject *anyset, PyObject *key)
2296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 if (!PyAnySet_Check(anyset)) {
2298 PyErr_BadInternalCall();
2299 return -1;
2300 }
2301 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002302}
2303
2304int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002305PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (!PySet_Check(set)) {
2308 PyErr_BadInternalCall();
2309 return -1;
2310 }
2311 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002312}
2313
2314int
Christian Heimesfd66e512008-01-29 12:18:50 +00002315PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!PySet_Check(anyset) &&
2318 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2319 PyErr_BadInternalCall();
2320 return -1;
2321 }
2322 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002323}
2324
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002325int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002326_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 if (!PyAnySet_Check(set)) {
2331 PyErr_BadInternalCall();
2332 return -1;
2333 }
2334 if (set_next((PySetObject *)set, pos, &entry) == 0)
2335 return 0;
2336 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002337 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002339}
2340
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002341PyObject *
2342PySet_Pop(PyObject *set)
2343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 if (!PySet_Check(set)) {
2345 PyErr_BadInternalCall();
2346 return NULL;
2347 }
2348 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002349}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002350
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002351int
2352_PySet_Update(PyObject *set, PyObject *iterable)
2353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (!PySet_Check(set)) {
2355 PyErr_BadInternalCall();
2356 return -1;
2357 }
2358 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002359}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002360
2361#ifdef Py_DEBUG
2362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002364 Returns True and original set is restored. */
2365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366#define assertRaises(call_return_value, exception) \
2367 do { \
2368 assert(call_return_value); \
2369 assert(PyErr_ExceptionMatches(exception)); \
2370 PyErr_Clear(); \
2371 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002372
2373static PyObject *
2374test_c_api(PySetObject *so)
2375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 Py_ssize_t count;
2377 char *s;
2378 Py_ssize_t i;
2379 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2380 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002381 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 /* Verify preconditions */
2385 assert(PyAnySet_Check(ob));
2386 assert(PyAnySet_CheckExact(ob));
2387 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 /* so.clear(); so |= set("abc"); */
2390 str = PyUnicode_FromString("abc");
2391 if (str == NULL)
2392 return NULL;
2393 set_clear_internal(so);
2394 if (set_update_internal(so, str) == -1) {
2395 Py_DECREF(str);
2396 return NULL;
2397 }
2398 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 /* Exercise type/size checks */
2401 assert(PySet_Size(ob) == 3);
2402 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 /* Raise TypeError for non-iterable constructor arguments */
2405 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2406 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 /* Raise TypeError for unhashable key */
2409 dup = PySet_New(ob);
2410 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2411 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2412 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 /* Exercise successful pop, contains, add, and discard */
2415 elem = PySet_Pop(ob);
2416 assert(PySet_Contains(ob, elem) == 0);
2417 assert(PySet_GET_SIZE(ob) == 2);
2418 assert(PySet_Add(ob, elem) == 0);
2419 assert(PySet_Contains(ob, elem) == 1);
2420 assert(PySet_GET_SIZE(ob) == 3);
2421 assert(PySet_Discard(ob, elem) == 1);
2422 assert(PySet_GET_SIZE(ob) == 2);
2423 assert(PySet_Discard(ob, elem) == 0);
2424 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 /* Exercise clear */
2427 dup2 = PySet_New(dup);
2428 assert(PySet_Clear(dup2) == 0);
2429 assert(PySet_Size(dup2) == 0);
2430 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 /* Raise SystemError on clear or update of frozen set */
2433 f = PyFrozenSet_New(dup);
2434 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2435 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2436 assert(PySet_Add(f, elem) == 0);
2437 Py_INCREF(f);
2438 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2439 Py_DECREF(f);
2440 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 /* Exercise direct iteration */
2443 i = 0, count = 0;
2444 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2445 s = _PyUnicode_AsString(x);
2446 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2447 count++;
2448 }
2449 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 /* Exercise updates */
2452 dup2 = PySet_New(NULL);
2453 assert(_PySet_Update(dup2, dup) == 0);
2454 assert(PySet_Size(dup2) == 3);
2455 assert(_PySet_Update(dup2, dup) == 0);
2456 assert(PySet_Size(dup2) == 3);
2457 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 /* Raise SystemError when self argument is not a set or frozenset. */
2460 t = PyTuple_New(0);
2461 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2462 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2463 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 /* Raise SystemError when self argument is not a set. */
2466 f = PyFrozenSet_New(dup);
2467 assert(PySet_Size(f) == 3);
2468 assert(PyFrozenSet_CheckExact(f));
2469 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2470 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2471 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 /* Raise KeyError when popping from an empty set */
2474 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2475 Py_DECREF(ob);
2476 assert(PySet_GET_SIZE(ob) == 0);
2477 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 /* Restore the set from the copy using the PyNumber API */
2480 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2481 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 /* Verify constructors accept NULL arguments */
2484 f = PySet_New(NULL);
2485 assert(f != NULL);
2486 assert(PySet_GET_SIZE(f) == 0);
2487 Py_DECREF(f);
2488 f = PyFrozenSet_New(NULL);
2489 assert(f != NULL);
2490 assert(PyFrozenSet_CheckExact(f));
2491 assert(PySet_GET_SIZE(f) == 0);
2492 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 Py_DECREF(elem);
2495 Py_DECREF(dup);
2496 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002497}
2498
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002499#undef assertRaises
2500
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002501#endif