blob: aac42537c41020d7c30a61461d4d964b80d4e9d0 [file] [log] [blame]
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001
2/* Set object implementation using a hash table
3 Functions adapted from dictobject.c
4*/
5
Raymond Hettingera690a992003-11-16 16:17:49 +00006#include "Python.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00007
8/* This must be >= 1. */
9#define PERTURB_SHIFT 5
10
11/* Object used as dummy key to fill deleted entries */
12static PyObject *dummy; /* Initialized by first call to make_new_set() */
13
14#define EMPTY_TO_MINSIZE(so) do { \
15 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
16 (so)->used = (so)->fill = 0; \
17 (so)->table = (so)->smalltable; \
18 (so)->mask = PySet_MINSIZE - 1; \
19 } while(0)
20
21
22/*
23The basic lookup function used by all operations.
24This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
25Open addressing is preferred over chaining since the link overhead for
26chaining would be substantial (100% with typical malloc overhead).
27
28The initial probe index is computed as hash mod the table size. Subsequent
29probe indices are computed as explained earlier.
30
31All arithmetic on hash should ignore overflow.
32
33This function must never return NULL; failures are indicated by returning
34a setentry* for which the value field is NULL. Exceptions are never
35reported by this function, and outstanding exceptions are maintained.
36*/
37
38static setentry *
39set_lookkey(PySetObject *so, PyObject *key, register long hash)
40{
41 register int i;
42 register unsigned int perturb;
43 register setentry *freeslot;
44 register unsigned int mask = so->mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000045 setentry *entry0 = so->table;
46 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047 register int restore_error;
48 register int checked_error;
49 register int cmp;
50 PyObject *err_type, *err_value, *err_tb;
51 PyObject *startkey;
52
53 i = hash & mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000054 entry = &entry0[i];
55 if (entry->key == NULL || entry->key == key)
56 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057
58 restore_error = checked_error = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000059 if (entry->key == dummy)
60 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000062 if (entry->hash == hash) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000063 /* error can't have been checked yet */
64 checked_error = 1;
65 if (PyErr_Occurred()) {
66 restore_error = 1;
67 PyErr_Fetch(&err_type, &err_value, &err_tb);
68 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000069 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
71 if (cmp < 0)
72 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000073 if (entry0 == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000074 if (cmp > 0)
75 goto Done;
76 }
77 else {
78 /* The compare did major nasty stuff to the
79 * set: start over.
80 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000081 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 goto Done;
83 }
84 }
85 freeslot = NULL;
86 }
87
88 /* In the loop, key == dummy is by far (factor of 100s) the
89 least likely outcome, so test for that last. */
90 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
91 i = (i << 2) + i + perturb + 1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000092 entry = &entry0[i & mask];
93 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000094 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000095 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000096 break;
97 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000098 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000099 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000100 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 if (!checked_error) {
102 checked_error = 1;
103 if (PyErr_Occurred()) {
104 restore_error = 1;
105 PyErr_Fetch(&err_type, &err_value,
106 &err_tb);
107 }
108 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000109 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000110 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
111 if (cmp < 0)
112 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000113 if (entry0 == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000114 if (cmp > 0)
115 break;
116 }
117 else {
118 /* The compare did major nasty stuff to the
119 * set: start over.
120 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000121 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 break;
123 }
124 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 else if (entry->key == dummy && freeslot == NULL)
126 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000127 }
128
129Done:
130 if (restore_error)
131 PyErr_Restore(err_type, err_value, err_tb);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000132 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000133}
134
135/*
136 * Hacked up version of set_lookkey which can assume keys are always strings;
137 * this assumption allows testing for errors during PyObject_Compare() to
138 * be dropped; string-string comparisons never raise exceptions. This also
139 * means we don't need to go through PyObject_Compare(); we can always use
140 * _PyString_Eq directly.
141 *
142 * This is valuable because the general-case error handling in set_lookkey() is
143 * expensive, and sets with pure-string keys may be very common.
144 */
145static setentry *
146set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
147{
148 register int i;
149 register unsigned int perturb;
150 register setentry *freeslot;
151 register unsigned int mask = so->mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000152 setentry *entry0 = so->table;
153 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000154
155 /* Make sure this function doesn't have to handle non-string keys,
156 including subclasses of str; e.g., one reason to subclass
157 strings is to override __eq__, and for speed we don't cater to
158 that here. */
159 if (!PyString_CheckExact(key)) {
160 so->lookup = set_lookkey;
161 return set_lookkey(so, key, hash);
162 }
163 i = hash & mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000164 entry = &entry0[i];
165 if (entry->key == NULL || entry->key == key)
166 return entry;
167 if (entry->key == dummy)
168 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000169 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000170 if (entry->hash == hash && _PyString_Eq(entry->key, key))
171 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000172 freeslot = NULL;
173 }
174
175 /* In the loop, key == dummy is by far (factor of 100s) the
176 least likely outcome, so test for that last. */
177 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
178 i = (i << 2) + i + perturb + 1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000179 entry = &entry0[i & mask];
180 if (entry->key == NULL)
181 return freeslot == NULL ? entry : freeslot;
182 if (entry->key == key
183 || (entry->hash == hash
184 && entry->key != dummy
185 && _PyString_Eq(entry->key, key)))
186 return entry;
187 if (entry->key == dummy && freeslot == NULL)
188 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000189 }
190}
191
192/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000193Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000194Used both by the internal resize routine and by the public insert routine.
195Eats a reference to key.
196*/
197static void
198set_insert_key(register PySetObject *so, PyObject *key, long hash)
199{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000200 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000201 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
202
203 assert(so->lookup != NULL);
204
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000205 entry = so->lookup(so, key, hash);
206 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000207 /* UNUSED */
208 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209 entry->key = key;
210 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000212 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000213 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000214 entry->key = key;
215 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000216 so->used++;
217 Py_DECREF(dummy);
218 } else {
219 /* ACTIVE */
220 Py_DECREF(key);
221 }
222}
223
224/*
225Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000226keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000227actually be smaller than the old one.
228*/
229static int
230set_table_resize(PySetObject *so, int minused)
231{
232 int newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000233 setentry *oldtable, *newtable, *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234 int i;
235 int is_oldtable_malloced;
236 setentry small_copy[PySet_MINSIZE];
237
238 assert(minused >= 0);
239
240 /* Find the smallest table size > minused. */
241 for (newsize = PySet_MINSIZE;
242 newsize <= minused && newsize > 0;
243 newsize <<= 1)
244 ;
245 if (newsize <= 0) {
246 PyErr_NoMemory();
247 return -1;
248 }
249
250 /* Get space for a new table. */
251 oldtable = so->table;
252 assert(oldtable != NULL);
253 is_oldtable_malloced = oldtable != so->smalltable;
254
255 if (newsize == PySet_MINSIZE) {
256 /* A large table is shrinking, or we can't get any smaller. */
257 newtable = so->smalltable;
258 if (newtable == oldtable) {
259 if (so->fill == so->used) {
260 /* No dummies, so no point doing anything. */
261 return 0;
262 }
263 /* We're not going to resize it, but rebuild the
264 table anyway to purge old dummy entries.
265 Subtle: This is *necessary* if fill==size,
266 as set_lookkey needs at least one virgin slot to
267 terminate failing searches. If fill < size, it's
268 merely desirable, as dummies slow searches. */
269 assert(so->fill > so->used);
270 memcpy(small_copy, oldtable, sizeof(small_copy));
271 oldtable = small_copy;
272 }
273 }
274 else {
275 newtable = PyMem_NEW(setentry, newsize);
276 if (newtable == NULL) {
277 PyErr_NoMemory();
278 return -1;
279 }
280 }
281
282 /* Make the set empty, using the new table. */
283 assert(newtable != oldtable);
284 so->table = newtable;
285 so->mask = newsize - 1;
286 memset(newtable, 0, sizeof(setentry) * newsize);
287 so->used = 0;
288 i = so->fill;
289 so->fill = 0;
290
291 /* Copy the data over; this is refcount-neutral for active entries;
292 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000293 for (entry = oldtable; i > 0; entry++) {
294 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000295 /* UNUSED */
296 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000297 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000298 /* DUMMY */
299 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000300 assert(entry->key == dummy);
301 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000302 } else {
303 /* ACTIVE */
304 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000305 set_insert_key(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000306 }
307 }
308
309 if (is_oldtable_malloced)
310 PyMem_DEL(oldtable);
311 return 0;
312}
313
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000314/* CAUTION: set_add_internal() must guarantee that it won't resize the table */
315static int
316set_add_internal(register PySetObject *so, PyObject *key)
317{
318 register long hash;
319 register int n_used;
320
321 if (PyString_CheckExact(key)) {
322 hash = ((PyStringObject *)key)->ob_shash;
323 if (hash == -1)
324 hash = PyObject_Hash(key);
325 } else {
326 hash = PyObject_Hash(key);
327 if (hash == -1)
328 return -1;
329 }
330 assert(so->fill <= so->mask); /* at least one empty slot */
331 n_used = so->used;
332 Py_INCREF(key);
333 set_insert_key(so, key, hash);
334 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
335 return 0;
336 return set_table_resize(so, so->used*(so->used>50000 ? 2 : 4));
337}
338
339#define DISCARD_NOTFOUND 0
340#define DISCARD_FOUND 1
341
342static int
343set_discard_internal(PySetObject *so, PyObject *key)
344{
345 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000346 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347 PyObject *old_key;
348
349 assert (PyAnySet_Check(so));
350 if (!PyString_CheckExact(key) ||
351 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
352 hash = PyObject_Hash(key);
353 if (hash == -1)
354 return -1;
355 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000356 entry = (so->lookup)(so, key, hash);
357 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000358 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000359 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000361 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000362 so->used--;
363 Py_DECREF(old_key);
364 return DISCARD_FOUND;
365}
366
367static void
368set_clear_internal(PySetObject *so)
369{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000370 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000371 int table_is_malloced;
372 int fill;
373 setentry small_copy[PySet_MINSIZE];
374#ifdef Py_DEBUG
375 int i, n;
376#endif
377
378 assert (PyAnySet_Check(so));
379#ifdef Py_DEBUG
380 n = so->mask + 1;
381 i = 0;
382#endif
383
384 table = so->table;
385 assert(table != NULL);
386 table_is_malloced = table != so->smalltable;
387
388 /* This is delicate. During the process of clearing the set,
389 * decrefs can cause the set to mutate. To avoid fatal confusion
390 * (voice of experience), we have to make the set empty before
391 * clearing the slots, and never refer to anything via mp->ref while
392 * clearing.
393 */
394 fill = so->fill;
395 if (table_is_malloced)
396 EMPTY_TO_MINSIZE(so);
397
398 else if (fill > 0) {
399 /* It's a small table with something that needs to be cleared.
400 * Afraid the only safe way is to copy the set entries into
401 * another small table first.
402 */
403 memcpy(small_copy, table, sizeof(small_copy));
404 table = small_copy;
405 EMPTY_TO_MINSIZE(so);
406 }
407 /* else it's a small table that's already empty */
408
409 /* Now we can finally clear things. If C had refcounts, we could
410 * assert that the refcount on table is 1 now, i.e. that this function
411 * has unique access to it, so decref side-effects can't alter it.
412 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000413 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000414#ifdef Py_DEBUG
415 assert(i < n);
416 ++i;
417#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000418 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000419 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000420 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421 }
422#ifdef Py_DEBUG
423 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000424 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000425#endif
426 }
427
428 if (table_is_malloced)
429 PyMem_DEL(table);
430}
431
432/*
433 * Iterate over a set table. Use like so:
434 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000435 * int pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000436 * PyObject *key;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000437 * pos = 0; # important! pos should not otherwise be changed by you
438 * while (set_next_internal(yourset, &pos, &key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439 * Refer to borrowed reference in key.
440 * }
441 *
442 * CAUTION: In general, it isn't safe to use set_next_internal in a loop that
443 * mutates the table.
444 */
445static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000446set_next_internal(PySetObject *so, int *pos, PyObject **key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000447{
448 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000449 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450
451 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000452 i = *pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 if (i < 0)
454 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000455 entry = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456 mask = so->mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000457 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000458 i++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000459 *pos = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000460 if (i > mask)
461 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000462 if (key)
463 *key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464 return 1;
465}
466
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000468set_merge_internal(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000469{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000470 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000471 register int i;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000472 register setentry *entry, *othertable;
473 register int othermask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000474
475 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000476 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000477
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000478 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000479 if (other == so || other->used == 0)
480 /* a.update(a) or a.update({}); nothing to do */
481 return 0;
482 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000483 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484 * that there will be no (or few) overlapping keys.
485 */
486 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
487 if (set_table_resize(so, (so->used + other->used)*2) != 0)
488 return -1;
489 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000490 othermask = other->mask;
491 othertable = other->table;
492 for (i = 0; i <= othermask; i++) {
493 entry = &othertable[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494 if (entry->key != NULL &&
495 entry->key != dummy) {
496 Py_INCREF(entry->key);
497 set_insert_key(so, entry->key, entry->hash);
498 }
499 }
500 return 0;
501}
502
503static int
504set_contains_internal(PySetObject *so, PyObject *key)
505{
506 long hash;
507
508 if (!PyString_CheckExact(key) ||
509 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
510 hash = PyObject_Hash(key);
511 if (hash == -1)
512 return -1;
513 }
514 key = (so->lookup)(so, key, hash)->key;
515 return key != NULL && key != dummy;
516}
517
Raymond Hettingerd7946662005-08-01 21:39:29 +0000518/***** Set iterator types **********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519
Raymond Hettingerd7946662005-08-01 21:39:29 +0000520static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000521
522typedef struct {
523 PyObject_HEAD
524 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
525 int si_used;
526 int si_pos;
527 long len;
528} setiterobject;
529
530static PyObject *
531set_iter(PySetObject *so)
532{
533 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
534 if (si == NULL)
535 return NULL;
536 Py_INCREF(so);
537 si->si_set = so;
538 si->si_used = so->used;
539 si->si_pos = 0;
540 si->len = so->used;
541 return (PyObject *)si;
542}
543
544static void
545setiter_dealloc(setiterobject *si)
546{
547 Py_XDECREF(si->si_set);
548 PyObject_Del(si);
549}
550
551static int
552setiter_len(setiterobject *si)
553{
554 if (si->si_set != NULL && si->si_used == si->si_set->used)
555 return si->len;
556 return 0;
557}
558
559static PySequenceMethods setiter_as_sequence = {
560 (inquiry)setiter_len, /* sq_length */
561 0, /* sq_concat */
562};
563
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000564static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000565{
566 PyObject *key;
567 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000568 register setentry *entry;
569 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000570
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000571 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000572 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000573 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000574
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000575 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000576 PyErr_SetString(PyExc_RuntimeError,
577 "Set changed size during iteration");
578 si->si_used = -1; /* Make this state sticky */
579 return NULL;
580 }
581
582 i = si->si_pos;
583 if (i < 0)
584 goto fail;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000585 entry = so->table;
586 mask = so->mask;
587 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000588 i++;
589 si->si_pos = i+1;
590 if (i > mask)
591 goto fail;
592 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000593 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000594 Py_INCREF(key);
595 return key;
596
597fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000598 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000599 si->si_set = NULL;
600 return NULL;
601}
602
Hye-Shik Change2956762005-08-01 05:26:41 +0000603static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000604 PyObject_HEAD_INIT(&PyType_Type)
605 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000606 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000607 sizeof(setiterobject), /* tp_basicsize */
608 0, /* tp_itemsize */
609 /* methods */
610 (destructor)setiter_dealloc, /* tp_dealloc */
611 0, /* tp_print */
612 0, /* tp_getattr */
613 0, /* tp_setattr */
614 0, /* tp_compare */
615 0, /* tp_repr */
616 0, /* tp_as_number */
617 &setiter_as_sequence, /* tp_as_sequence */
618 0, /* tp_as_mapping */
619 0, /* tp_hash */
620 0, /* tp_call */
621 0, /* tp_str */
622 PyObject_GenericGetAttr, /* tp_getattro */
623 0, /* tp_setattro */
624 0, /* tp_as_buffer */
625 Py_TPFLAGS_DEFAULT, /* tp_flags */
626 0, /* tp_doc */
627 0, /* tp_traverse */
628 0, /* tp_clear */
629 0, /* tp_richcompare */
630 0, /* tp_weaklistoffset */
631 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000632 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000633};
634
635/***** Derived functions (table accesses only done with above primitives *****/
636
Raymond Hettinger691d8052004-05-30 07:26:47 +0000637#include "structmember.h"
Raymond Hettingera690a992003-11-16 16:17:49 +0000638
639/* set object implementation
640 written and maintained by Raymond D. Hettinger <python@rcn.com>
641 derived from sets.py written by Greg V. Wilson, Alex Martelli,
642 Guido van Rossum, Raymond Hettinger, and Tim Peters.
643
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000644 Copyright (c) 2003-5 Python Software Foundation.
Raymond Hettingera690a992003-11-16 16:17:49 +0000645 All rights reserved.
646*/
647
Raymond Hettingerd7946662005-08-01 21:39:29 +0000648static int
649set_len(PyObject *so)
650{
651 return ((PySetObject *)so)->used;
652}
653
654static int
655set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000656{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000657 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000658
Raymond Hettingerd7946662005-08-01 21:39:29 +0000659 if (PyAnySet_Check(other))
660 return set_merge_internal(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000661
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000662 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000663 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000664 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000665 while (PyDict_Next(other, &pos, &key, &value)) {
666 if (set_add_internal(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000667 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000669 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670 }
671
Raymond Hettingera38123e2003-11-24 22:18:49 +0000672 it = PyObject_GetIter(other);
673 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000674 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000675
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000676 while ((key = PyIter_Next(it)) != NULL) {
677 if (set_add_internal(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000678 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000679 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000680 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000681 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000682 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000683 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000684 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000685 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000686 return -1;
687 return 0;
688}
689
690static PyObject *
691set_update(PySetObject *so, PyObject *other)
692{
693 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000694 return NULL;
695 Py_RETURN_NONE;
696}
697
698PyDoc_STRVAR(update_doc,
699"Update a set with the union of itself and another.");
700
701static PyObject *
702make_new_set(PyTypeObject *type, PyObject *iterable)
703{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000704 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000705
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000706 if (dummy == NULL) { /* Auto-initialize dummy */
707 dummy = PyString_FromString("<dummy key>");
708 if (dummy == NULL)
709 return NULL;
710 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000711
712 /* create PySetObject structure */
713 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000714 if (so == NULL)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000715 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000716
717 EMPTY_TO_MINSIZE(so);
718 so->lookup = set_lookkey_string;
Raymond Hettingera690a992003-11-16 16:17:49 +0000719 so->hash = -1;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000720 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000721
Raymond Hettingera38123e2003-11-24 22:18:49 +0000722 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000723 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000724 Py_DECREF(so);
725 return NULL;
726 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000727 }
728
Raymond Hettingera690a992003-11-16 16:17:49 +0000729 return (PyObject *)so;
730}
731
Raymond Hettingerd7946662005-08-01 21:39:29 +0000732/* The empty frozenset is a singleton */
733static PyObject *emptyfrozenset = NULL;
734
Raymond Hettingera690a992003-11-16 16:17:49 +0000735static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000736frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000737{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000738 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000739
740 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
741 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000742
743 if (type != &PyFrozenSet_Type)
744 return make_new_set(type, iterable);
745
746 if (iterable != NULL) {
747 /* frozenset(f) is idempotent */
748 if (PyFrozenSet_CheckExact(iterable)) {
749 Py_INCREF(iterable);
750 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000751 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000752 result = make_new_set(type, iterable);
753 if (result == NULL || set_len(result))
754 return result;
755 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000756 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000757 /* The empty frozenset is a singleton */
758 if (emptyfrozenset == NULL)
759 emptyfrozenset = make_new_set(type, NULL);
760 Py_XINCREF(emptyfrozenset);
761 return emptyfrozenset;
762}
763
764void
765PySet_Fini(void)
766{
767 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000768}
769
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000770static PyObject *
771set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
772{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000773 return make_new_set(type, NULL);
774}
775
Raymond Hettingera690a992003-11-16 16:17:49 +0000776static void
777set_dealloc(PySetObject *so)
778{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000779 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000780 int fill = so->fill;
781
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000782 PyObject_GC_UnTrack(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000783 Py_TRASHCAN_SAFE_BEGIN(so)
Raymond Hettinger691d8052004-05-30 07:26:47 +0000784 if (so->weakreflist != NULL)
785 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000786
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000787 for (entry = so->table; fill > 0; entry++) {
788 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000790 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791 }
792 }
793 if (so->table != so->smalltable)
794 PyMem_DEL(so->table);
795
Raymond Hettingera690a992003-11-16 16:17:49 +0000796 so->ob_type->tp_free(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000798}
799
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000800static int
801set_traverse(PySetObject *so, visitproc visit, void *arg)
802{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000803 int pos = 0;
804 PyObject *key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000806 while (set_next_internal(so, &pos, &key))
807 Py_VISIT(key);
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000808 return 0;
809}
810
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000811/* set_swap_bodies() switches the contents of any two sets by moving their
812 internal data pointers and, if needed, copying the internal smalltables.
813 Semantically equivalent to:
814
815 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
816
817 The function always succeeds and it leaves both objects in a stable state.
818 Useful for creating temporary frozensets from sets for membership testing
819 in __contains__(), discard(), and remove(). Also useful for operations
820 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000821 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000822*/
823
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824static void
825set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000826{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000827 int t;
828 setentry *u;
829 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
830 setentry tab[PySet_MINSIZE];
831 long h;
832
833 t = a->fill; a->fill = b->fill; b->fill = t;
834 t = a->used; a->used = b->used; b->used = t;
835 t = a->mask; a->mask = b->mask; b->mask = t;
836
837 u = a->table;
838 if (a->table == a->smalltable)
839 u = b->smalltable;
840 a->table = b->table;
841 if (b->table == b->smalltable)
842 a->table = a->smalltable;
843 b->table = u;
844
845 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
846
847 if (a->table == a->smalltable || b->table == b->smalltable) {
848 memcpy(tab, a->smalltable, sizeof(tab));
849 memcpy(a->smalltable, b->smalltable, sizeof(tab));
850 memcpy(b->smalltable, tab, sizeof(tab));
851 }
852
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000853 h = a->hash; a->hash = b->hash; b->hash = h;
Raymond Hettingera690a992003-11-16 16:17:49 +0000854}
855
856static int
857set_contains(PySetObject *so, PyObject *key)
858{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000859 PyObject *tmpkey;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000860 int result;
861
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000862 result = set_contains_internal(so, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000863 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000864 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000865 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
866 if (tmpkey == NULL)
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000867 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000868 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
869 result = set_contains_internal(so, tmpkey);
870 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
871 Py_DECREF(tmpkey);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000872 }
873 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000874}
875
876static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000877set_direct_contains(PySetObject *so, PyObject *key)
878{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000879 long result;
880
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000881 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000882 if (result == -1)
883 return NULL;
884 return PyBool_FromLong(result);
885}
886
887PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
888
889static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000890set_copy(PySetObject *so)
891{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000892 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000893}
894
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000895static PyObject *
896frozenset_copy(PySetObject *so)
897{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000898 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000899 Py_INCREF(so);
900 return (PyObject *)so;
901 }
902 return set_copy(so);
903}
904
Raymond Hettingera690a992003-11-16 16:17:49 +0000905PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
906
907static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000908set_union(PySetObject *so, PyObject *other)
909{
910 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000911
912 result = (PySetObject *)set_copy(so);
913 if (result == NULL)
914 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000915 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000916 Py_DECREF(result);
917 return NULL;
918 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000919 return (PyObject *)result;
920}
921
922PyDoc_STRVAR(union_doc,
923 "Return the union of two sets as a new set.\n\
924\n\
925(i.e. all elements that are in either set.)");
926
927static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000928set_or(PySetObject *so, PyObject *other)
929{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000930 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000931 Py_INCREF(Py_NotImplemented);
932 return Py_NotImplemented;
933 }
934 return set_union(so, other);
935}
936
937static PyObject *
938set_ior(PySetObject *so, PyObject *other)
939{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000940 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000941 Py_INCREF(Py_NotImplemented);
942 return Py_NotImplemented;
943 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000944 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +0000945 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000946 Py_INCREF(so);
947 return (PyObject *)so;
948}
949
950static PyObject *
951set_intersection(PySetObject *so, PyObject *other)
952{
953 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000954 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000955
956 result = (PySetObject *)make_new_set(so->ob_type, NULL);
957 if (result == NULL)
958 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000959
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000960 if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) {
961 tmp = (PyObject *)so;
962 so = (PySetObject *)other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000963 other = tmp;
964 }
965
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000966 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000967 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000968 while (set_next_internal((PySetObject *)other, &pos, &key)) {
969 if (set_contains_internal(so, key)) {
970 if (set_add_internal(result, key) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000971 Py_DECREF(result);
972 return NULL;
973 }
974 }
975 }
976 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000977 }
978
Raymond Hettingera690a992003-11-16 16:17:49 +0000979 it = PyObject_GetIter(other);
980 if (it == NULL) {
981 Py_DECREF(result);
982 return NULL;
983 }
984
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000985 while ((key = PyIter_Next(it)) != NULL) {
986 if (set_contains_internal(so, key)) {
987 if (set_add_internal(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000988 Py_DECREF(it);
989 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000990 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000991 return NULL;
992 }
993 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000994 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000995 }
996 Py_DECREF(it);
997 if (PyErr_Occurred()) {
998 Py_DECREF(result);
999 return NULL;
1000 }
1001 return (PyObject *)result;
1002}
1003
1004PyDoc_STRVAR(intersection_doc,
1005"Return the intersection of two sets as a new set.\n\
1006\n\
1007(i.e. all elements that are in both sets.)");
1008
1009static PyObject *
1010set_intersection_update(PySetObject *so, PyObject *other)
1011{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001012 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001013
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001014 tmp = set_intersection(so, other);
1015 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001016 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001017 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001018 Py_DECREF(tmp);
1019 Py_RETURN_NONE;
1020}
1021
1022PyDoc_STRVAR(intersection_update_doc,
1023"Update a set with the intersection of itself and another.");
1024
1025static PyObject *
1026set_and(PySetObject *so, PyObject *other)
1027{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001028 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001029 Py_INCREF(Py_NotImplemented);
1030 return Py_NotImplemented;
1031 }
1032 return set_intersection(so, other);
1033}
1034
1035static PyObject *
1036set_iand(PySetObject *so, PyObject *other)
1037{
1038 PyObject *result;
1039
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001040 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001041 Py_INCREF(Py_NotImplemented);
1042 return Py_NotImplemented;
1043 }
1044 result = set_intersection_update(so, other);
1045 if (result == NULL)
1046 return NULL;
1047 Py_DECREF(result);
1048 Py_INCREF(so);
1049 return (PyObject *)so;
1050}
1051
1052static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001053set_difference_update(PySetObject *so, PyObject *other)
1054{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001055 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001056
1057 it = PyObject_GetIter(other);
1058 if (it == NULL)
1059 return NULL;
1060
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001061 while ((key = PyIter_Next(it)) != NULL) {
1062 if (set_discard_internal(so, key) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001063 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001064 Py_DECREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001065 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001066 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001067 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001068 }
1069 Py_DECREF(it);
1070 if (PyErr_Occurred())
1071 return NULL;
1072 Py_RETURN_NONE;
1073}
1074
1075PyDoc_STRVAR(difference_update_doc,
1076"Remove all elements of another set from this set.");
1077
1078static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001079set_difference(PySetObject *so, PyObject *other)
1080{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001081 PyObject *tmp, *key, *result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001082 int pos = 0;
1083
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001084 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001085 result = set_copy(so);
1086 if (result == NULL)
1087 return result;
1088 tmp = set_difference_update((PySetObject *)result, other);
1089 if (tmp != NULL) {
1090 Py_DECREF(tmp);
1091 return result;
1092 }
1093 Py_DECREF(result);
1094 return NULL;
1095 }
1096
1097 result = make_new_set(so->ob_type, NULL);
1098 if (result == NULL)
1099 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001100
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001101 if (PyDict_Check(other)) {
1102 while (set_next_internal(so, &pos, &key)) {
1103 if (!PyDict_Contains(other, key)) {
1104 if (set_add_internal((PySetObject *)result, key) == -1)
1105 return NULL;
1106 }
1107 }
1108 return result;
1109 }
1110
1111 while (set_next_internal(so, &pos, &key)) {
1112 if (!set_contains_internal((PySetObject *)other, key)) {
1113 if (set_add_internal((PySetObject *)result, key) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001114 return NULL;
1115 }
1116 }
1117 return result;
1118}
1119
1120PyDoc_STRVAR(difference_doc,
1121"Return the difference of two sets as a new set.\n\
1122\n\
1123(i.e. all elements that are in this set but not the other.)");
1124static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001125set_sub(PySetObject *so, PyObject *other)
1126{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001127 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001128 Py_INCREF(Py_NotImplemented);
1129 return Py_NotImplemented;
1130 }
1131 return set_difference(so, other);
1132}
1133
1134static PyObject *
1135set_isub(PySetObject *so, PyObject *other)
1136{
1137 PyObject *result;
1138
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001139 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001140 Py_INCREF(Py_NotImplemented);
1141 return Py_NotImplemented;
1142 }
1143 result = set_difference_update(so, other);
1144 if (result == NULL)
1145 return NULL;
1146 Py_DECREF(result);
1147 Py_INCREF(so);
1148 return (PyObject *)so;
1149}
1150
1151static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001152set_symmetric_difference_update(PySetObject *so, PyObject *other)
1153{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001154 PySetObject *otherset;
1155 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001156 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001157
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001158 if (PyDict_Check(other)) {
1159 PyObject *value;
1160 int rv;
1161 while (PyDict_Next(other, &pos, &key, &value)) {
1162 rv = set_discard_internal(so, key);
1163 if (rv == -1)
1164 return NULL;
1165 if (rv == DISCARD_NOTFOUND) {
1166 if (set_add_internal(so, key) == -1)
1167 return NULL;
1168 }
1169 }
1170 Py_RETURN_NONE;
1171 }
1172
1173 if (PyAnySet_Check(other)) {
1174 Py_INCREF(other);
1175 otherset = (PySetObject *)other;
1176 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001177 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1178 if (otherset == NULL)
1179 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001180 }
1181
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001182 while (set_next_internal(otherset, &pos, &key)) {
1183 int rv = set_discard_internal(so, key);
1184 if (rv == -1) {
1185 Py_XDECREF(otherset);
1186 return NULL;
1187 }
1188 if (rv == DISCARD_NOTFOUND) {
1189 if (set_add_internal(so, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001190 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001191 return NULL;
1192 }
1193 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001194 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001195 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001196 Py_RETURN_NONE;
1197}
1198
1199PyDoc_STRVAR(symmetric_difference_update_doc,
1200"Update a set with the symmetric difference of itself and another.");
1201
1202static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001203set_symmetric_difference(PySetObject *so, PyObject *other)
1204{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001205 PyObject *rv;
1206 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001207
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001208 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1209 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001210 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001211 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1212 if (rv == NULL)
1213 return NULL;
1214 Py_DECREF(rv);
1215 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001216}
1217
1218PyDoc_STRVAR(symmetric_difference_doc,
1219"Return the symmetric difference of two sets as a new set.\n\
1220\n\
1221(i.e. all elements that are in exactly one of the sets.)");
1222
1223static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001224set_xor(PySetObject *so, PyObject *other)
1225{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001226 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001227 Py_INCREF(Py_NotImplemented);
1228 return Py_NotImplemented;
1229 }
1230 return set_symmetric_difference(so, other);
1231}
1232
1233static PyObject *
1234set_ixor(PySetObject *so, PyObject *other)
1235{
1236 PyObject *result;
1237
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001238 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001239 Py_INCREF(Py_NotImplemented);
1240 return Py_NotImplemented;
1241 }
1242 result = set_symmetric_difference_update(so, other);
1243 if (result == NULL)
1244 return NULL;
1245 Py_DECREF(result);
1246 Py_INCREF(so);
1247 return (PyObject *)so;
1248}
1249
1250static PyObject *
1251set_issubset(PySetObject *so, PyObject *other)
1252{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001253 PyObject *tmp, *result;
1254 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001255 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001256
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001257 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001258 tmp = make_new_set(&PySet_Type, other);
1259 if (tmp == NULL)
1260 return NULL;
1261 result = set_issubset(so, tmp);
1262 Py_DECREF(tmp);
1263 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001264 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001265 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001266 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001267
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001268 while (set_next_internal(so, &pos, &key)) {
1269 if (!set_contains_internal((PySetObject *)other, key))
Raymond Hettingera690a992003-11-16 16:17:49 +00001270 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001271 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001272 Py_RETURN_TRUE;
1273}
1274
1275PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1276
1277static PyObject *
1278set_issuperset(PySetObject *so, PyObject *other)
1279{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001280 PyObject *tmp, *result;
1281
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001282 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001283 tmp = make_new_set(&PySet_Type, other);
1284 if (tmp == NULL)
1285 return NULL;
1286 result = set_issuperset(so, tmp);
1287 Py_DECREF(tmp);
1288 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001289 }
1290 return set_issubset((PySetObject *)other, (PyObject *)so);
1291}
1292
1293PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1294
1295static long
1296set_nohash(PyObject *self)
1297{
1298 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1299 return -1;
1300}
1301
1302static int
1303set_nocmp(PyObject *self)
1304{
1305 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1306 return -1;
1307}
1308
1309static long
1310frozenset_hash(PyObject *self)
1311{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001312 PyObject *key;
Raymond Hettingera690a992003-11-16 16:17:49 +00001313 PySetObject *so = (PySetObject *)self;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001314 int pos = 0;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001315 long hash = 1927868237L;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001316
Raymond Hettingera690a992003-11-16 16:17:49 +00001317 if (so->hash != -1)
1318 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001319
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001320 hash *= set_len(self) + 1;
1321 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingerc9786332004-06-10 22:41:48 +00001322 /* Work to increase the bit dispersion for closely spaced hash
1323 values. The is important because some use cases have many
1324 combinations of a small number of elements with nearby
1325 hashes so that many distinct combinations collapse to only
1326 a handful of distinct hash values. */
1327 long h = PyObject_Hash(key);
1328 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001329 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001330 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001331 if (hash == -1)
1332 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001333 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001334 return hash;
1335}
1336
1337static PyObject *
1338set_richcompare(PySetObject *v, PyObject *w, int op)
1339{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001340 PyObject *r1, *r2;
1341
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001342 if(!PyAnySet_Check(w)) {
1343 if (op == Py_EQ)
1344 Py_RETURN_FALSE;
1345 if (op == Py_NE)
1346 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001347 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1348 return NULL;
1349 }
1350 switch (op) {
1351 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001352 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001353 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001354 return set_issubset(v, w);
1355 case Py_NE:
1356 if (set_len((PyObject *)v) != set_len(w))
1357 Py_RETURN_TRUE;
1358 r1 = set_issubset(v, w);
1359 assert (r1 != NULL);
1360 r2 = PyBool_FromLong(PyObject_Not(r1));
1361 Py_DECREF(r1);
1362 return r2;
1363 case Py_LE:
1364 return set_issubset(v, w);
1365 case Py_GE:
1366 return set_issuperset(v, w);
1367 case Py_LT:
1368 if (set_len((PyObject *)v) >= set_len(w))
1369 Py_RETURN_FALSE;
1370 return set_issubset(v, w);
1371 case Py_GT:
1372 if (set_len((PyObject *)v) <= set_len(w))
1373 Py_RETURN_FALSE;
1374 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001375 }
1376 Py_INCREF(Py_NotImplemented);
1377 return Py_NotImplemented;
1378}
1379
1380static PyObject *
1381set_repr(PySetObject *so)
1382{
1383 PyObject *keys, *result, *listrepr;
1384
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001385 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001386 if (keys == NULL)
1387 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001388 listrepr = PyObject_Repr(keys);
1389 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001390 if (listrepr == NULL)
1391 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001392
1393 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1394 PyString_AS_STRING(listrepr));
1395 Py_DECREF(listrepr);
1396 return result;
1397}
1398
1399static int
1400set_tp_print(PySetObject *so, FILE *fp, int flags)
1401{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001402 PyObject *key;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001403 int pos=0;
1404 char *emit = ""; /* No separator emitted on first pass */
1405 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001406
Raymond Hettingera690a992003-11-16 16:17:49 +00001407 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001408 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001409 fputs(emit, fp);
1410 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001411 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001412 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001413 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001414 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001415 return 0;
1416}
1417
1418static PyObject *
1419set_clear(PySetObject *so)
1420{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001421 set_clear_internal(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001422 so->hash = -1;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001423 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001424}
1425
1426PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1427
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001428static int
1429set_tp_clear(PySetObject *so)
1430{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001431 set_clear_internal(so);
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001432 so->hash = -1;
1433 return 0;
1434}
1435
Raymond Hettingera690a992003-11-16 16:17:49 +00001436static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001437set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001438{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001439 if (set_add_internal(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001440 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001441 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001442}
1443
1444PyDoc_STRVAR(add_doc,
1445"Add an element to a set.\n\
1446\n\
1447This has no effect if the element is already present.");
1448
1449static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001450set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001451{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001452 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001453 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001454
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001455 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1456 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1457 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001458 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001459 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1460 result = set_remove(so, tmpkey);
1461 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1462 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001463 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001464 }
Raymond Hettinger0deab622003-12-13 18:53:18 +00001465
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001466 rv = set_discard_internal(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001467 if (rv == -1)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001468 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001469 else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001470 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001471 return NULL;
1472 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001473 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001474}
1475
1476PyDoc_STRVAR(remove_doc,
1477"Remove an element from a set; it must be a member.\n\
1478\n\
1479If the element is not a member, raise a KeyError.");
1480
1481static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001482set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001483{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001484 PyObject *tmpkey, *result;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001485
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001486 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1487 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1488 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001489 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001490 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1491 result = set_discard(so, tmpkey);
1492 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1493 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001494 return result;
1495 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001496
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001497 if (set_discard_internal(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001498 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001499 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001500}
1501
1502PyDoc_STRVAR(discard_doc,
1503"Remove an element from a set if it is a member.\n\
1504\n\
1505If the element is not a member, do nothing.");
1506
1507static PyObject *
1508set_pop(PySetObject *so)
1509{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001510 PyObject *key;
Raymond Hettingera690a992003-11-16 16:17:49 +00001511 int pos = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001512 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001513
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001514 if (!set_next_internal(so, &pos, &key)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001515 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1516 return NULL;
1517 }
1518 Py_INCREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001519
1520 rv = set_discard_internal(so, key);
1521 if (rv == -1) {
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001522 Py_DECREF(key);
1523 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001524 } else if (rv == DISCARD_NOTFOUND) {
1525 Py_DECREF(key);
1526 PyErr_SetObject(PyExc_KeyError, key);
1527 return NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001528 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001529 return key;
1530}
1531
1532PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1533
1534static PyObject *
1535set_reduce(PySetObject *so)
1536{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001537 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001538
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001539 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001540 if (keys == NULL)
1541 goto done;
1542 args = PyTuple_Pack(1, keys);
1543 if (args == NULL)
1544 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001545 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1546 if (dict == NULL) {
1547 PyErr_Clear();
1548 dict = Py_None;
1549 Py_INCREF(dict);
1550 }
1551 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001552done:
1553 Py_XDECREF(args);
1554 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001555 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001556 return result;
1557}
1558
1559PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1560
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001561static int
1562set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1563{
1564 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001565
1566 if (!PyAnySet_Check(self))
1567 return -1;
1568 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1569 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001570 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001571 self->hash = -1;
1572 if (iterable == NULL)
1573 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001574 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001575}
1576
Raymond Hettingera690a992003-11-16 16:17:49 +00001577static PySequenceMethods set_as_sequence = {
1578 (inquiry)set_len, /* sq_length */
1579 0, /* sq_concat */
1580 0, /* sq_repeat */
1581 0, /* sq_item */
1582 0, /* sq_slice */
1583 0, /* sq_ass_item */
1584 0, /* sq_ass_slice */
1585 (objobjproc)set_contains, /* sq_contains */
1586};
1587
1588/* set object ********************************************************/
1589
1590static PyMethodDef set_methods[] = {
1591 {"add", (PyCFunction)set_add, METH_O,
1592 add_doc},
1593 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1594 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001595 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001596 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001597 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1598 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001599 {"discard", (PyCFunction)set_discard, METH_O,
1600 discard_doc},
1601 {"difference", (PyCFunction)set_difference, METH_O,
1602 difference_doc},
1603 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1604 difference_update_doc},
1605 {"intersection",(PyCFunction)set_intersection, METH_O,
1606 intersection_doc},
1607 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1608 intersection_update_doc},
1609 {"issubset", (PyCFunction)set_issubset, METH_O,
1610 issubset_doc},
1611 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1612 issuperset_doc},
1613 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1614 pop_doc},
1615 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1616 reduce_doc},
1617 {"remove", (PyCFunction)set_remove, METH_O,
1618 remove_doc},
1619 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1620 symmetric_difference_doc},
1621 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1622 symmetric_difference_update_doc},
1623 {"union", (PyCFunction)set_union, METH_O,
1624 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001625 {"update", (PyCFunction)set_update, METH_O,
1626 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001627 {NULL, NULL} /* sentinel */
1628};
1629
1630static PyNumberMethods set_as_number = {
1631 0, /*nb_add*/
1632 (binaryfunc)set_sub, /*nb_subtract*/
1633 0, /*nb_multiply*/
1634 0, /*nb_divide*/
1635 0, /*nb_remainder*/
1636 0, /*nb_divmod*/
1637 0, /*nb_power*/
1638 0, /*nb_negative*/
1639 0, /*nb_positive*/
1640 0, /*nb_absolute*/
1641 0, /*nb_nonzero*/
1642 0, /*nb_invert*/
1643 0, /*nb_lshift*/
1644 0, /*nb_rshift*/
1645 (binaryfunc)set_and, /*nb_and*/
1646 (binaryfunc)set_xor, /*nb_xor*/
1647 (binaryfunc)set_or, /*nb_or*/
1648 0, /*nb_coerce*/
1649 0, /*nb_int*/
1650 0, /*nb_long*/
1651 0, /*nb_float*/
1652 0, /*nb_oct*/
1653 0, /*nb_hex*/
1654 0, /*nb_inplace_add*/
1655 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1656 0, /*nb_inplace_multiply*/
1657 0, /*nb_inplace_divide*/
1658 0, /*nb_inplace_remainder*/
1659 0, /*nb_inplace_power*/
1660 0, /*nb_inplace_lshift*/
1661 0, /*nb_inplace_rshift*/
1662 (binaryfunc)set_iand, /*nb_inplace_and*/
1663 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1664 (binaryfunc)set_ior, /*nb_inplace_or*/
1665};
1666
1667PyDoc_STRVAR(set_doc,
1668"set(iterable) --> set object\n\
1669\n\
1670Build an unordered collection.");
1671
1672PyTypeObject PySet_Type = {
1673 PyObject_HEAD_INIT(&PyType_Type)
1674 0, /* ob_size */
1675 "set", /* tp_name */
1676 sizeof(PySetObject), /* tp_basicsize */
1677 0, /* tp_itemsize */
1678 /* methods */
1679 (destructor)set_dealloc, /* tp_dealloc */
1680 (printfunc)set_tp_print, /* tp_print */
1681 0, /* tp_getattr */
1682 0, /* tp_setattr */
1683 (cmpfunc)set_nocmp, /* tp_compare */
1684 (reprfunc)set_repr, /* tp_repr */
1685 &set_as_number, /* tp_as_number */
1686 &set_as_sequence, /* tp_as_sequence */
1687 0, /* tp_as_mapping */
1688 set_nohash, /* tp_hash */
1689 0, /* tp_call */
1690 0, /* tp_str */
1691 PyObject_GenericGetAttr, /* tp_getattro */
1692 0, /* tp_setattro */
1693 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001694 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001695 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001696 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001697 (traverseproc)set_traverse, /* tp_traverse */
1698 (inquiry)set_tp_clear, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001699 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001700 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001701 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001702 0, /* tp_iternext */
1703 set_methods, /* tp_methods */
1704 0, /* tp_members */
1705 0, /* tp_getset */
1706 0, /* tp_base */
1707 0, /* tp_dict */
1708 0, /* tp_descr_get */
1709 0, /* tp_descr_set */
1710 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001711 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001712 PyType_GenericAlloc, /* tp_alloc */
1713 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001714 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001715};
1716
1717/* frozenset object ********************************************************/
1718
1719
1720static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001721 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001722 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001723 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001724 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001725 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001726 difference_doc},
1727 {"intersection",(PyCFunction)set_intersection, METH_O,
1728 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001729 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001730 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001731 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001732 issuperset_doc},
1733 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1734 reduce_doc},
1735 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1736 symmetric_difference_doc},
1737 {"union", (PyCFunction)set_union, METH_O,
1738 union_doc},
1739 {NULL, NULL} /* sentinel */
1740};
1741
1742static PyNumberMethods frozenset_as_number = {
1743 0, /*nb_add*/
1744 (binaryfunc)set_sub, /*nb_subtract*/
1745 0, /*nb_multiply*/
1746 0, /*nb_divide*/
1747 0, /*nb_remainder*/
1748 0, /*nb_divmod*/
1749 0, /*nb_power*/
1750 0, /*nb_negative*/
1751 0, /*nb_positive*/
1752 0, /*nb_absolute*/
1753 0, /*nb_nonzero*/
1754 0, /*nb_invert*/
1755 0, /*nb_lshift*/
1756 0, /*nb_rshift*/
1757 (binaryfunc)set_and, /*nb_and*/
1758 (binaryfunc)set_xor, /*nb_xor*/
1759 (binaryfunc)set_or, /*nb_or*/
1760};
1761
1762PyDoc_STRVAR(frozenset_doc,
1763"frozenset(iterable) --> frozenset object\n\
1764\n\
1765Build an immutable unordered collection.");
1766
1767PyTypeObject PyFrozenSet_Type = {
1768 PyObject_HEAD_INIT(&PyType_Type)
1769 0, /* ob_size */
1770 "frozenset", /* tp_name */
1771 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001772 0, /* tp_itemsize */
1773 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001774 (destructor)set_dealloc, /* tp_dealloc */
1775 (printfunc)set_tp_print, /* tp_print */
1776 0, /* tp_getattr */
1777 0, /* tp_setattr */
1778 (cmpfunc)set_nocmp, /* tp_compare */
1779 (reprfunc)set_repr, /* tp_repr */
1780 &frozenset_as_number, /* tp_as_number */
1781 &set_as_sequence, /* tp_as_sequence */
1782 0, /* tp_as_mapping */
1783 frozenset_hash, /* tp_hash */
1784 0, /* tp_call */
1785 0, /* tp_str */
1786 PyObject_GenericGetAttr, /* tp_getattro */
1787 0, /* tp_setattro */
1788 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001789 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001790 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001791 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001792 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingera690a992003-11-16 16:17:49 +00001793 0, /* tp_clear */
1794 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001795 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001796 (getiterfunc)set_iter, /* tp_iter */
1797 0, /* tp_iternext */
1798 frozenset_methods, /* tp_methods */
1799 0, /* tp_members */
1800 0, /* tp_getset */
1801 0, /* tp_base */
1802 0, /* tp_dict */
1803 0, /* tp_descr_get */
1804 0, /* tp_descr_set */
1805 0, /* tp_dictoffset */
1806 0, /* tp_init */
1807 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001808 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001809 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001810};