blob: 34e64e49111836c6d792657933e47ce15ab2d430 [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
314/*** Internal functions (derived from public dictionary api functions ) ***/
315
316
317/* CAUTION: set_add_internal() must guarantee that it won't resize the table */
318static int
319set_add_internal(register PySetObject *so, PyObject *key)
320{
321 register long hash;
322 register int n_used;
323
324 if (PyString_CheckExact(key)) {
325 hash = ((PyStringObject *)key)->ob_shash;
326 if (hash == -1)
327 hash = PyObject_Hash(key);
328 } else {
329 hash = PyObject_Hash(key);
330 if (hash == -1)
331 return -1;
332 }
333 assert(so->fill <= so->mask); /* at least one empty slot */
334 n_used = so->used;
335 Py_INCREF(key);
336 set_insert_key(so, key, hash);
337 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
338 return 0;
339 return set_table_resize(so, so->used*(so->used>50000 ? 2 : 4));
340}
341
342#define DISCARD_NOTFOUND 0
343#define DISCARD_FOUND 1
344
345static int
346set_discard_internal(PySetObject *so, PyObject *key)
347{
348 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000349 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000350 PyObject *old_key;
351
352 assert (PyAnySet_Check(so));
353 if (!PyString_CheckExact(key) ||
354 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
355 hash = PyObject_Hash(key);
356 if (hash == -1)
357 return -1;
358 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000359 entry = (so->lookup)(so, key, hash);
360 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000361 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000362 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000364 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000365 so->used--;
366 Py_DECREF(old_key);
367 return DISCARD_FOUND;
368}
369
370static void
371set_clear_internal(PySetObject *so)
372{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000373 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000374 int table_is_malloced;
375 int fill;
376 setentry small_copy[PySet_MINSIZE];
377#ifdef Py_DEBUG
378 int i, n;
379#endif
380
381 assert (PyAnySet_Check(so));
382#ifdef Py_DEBUG
383 n = so->mask + 1;
384 i = 0;
385#endif
386
387 table = so->table;
388 assert(table != NULL);
389 table_is_malloced = table != so->smalltable;
390
391 /* This is delicate. During the process of clearing the set,
392 * decrefs can cause the set to mutate. To avoid fatal confusion
393 * (voice of experience), we have to make the set empty before
394 * clearing the slots, and never refer to anything via mp->ref while
395 * clearing.
396 */
397 fill = so->fill;
398 if (table_is_malloced)
399 EMPTY_TO_MINSIZE(so);
400
401 else if (fill > 0) {
402 /* It's a small table with something that needs to be cleared.
403 * Afraid the only safe way is to copy the set entries into
404 * another small table first.
405 */
406 memcpy(small_copy, table, sizeof(small_copy));
407 table = small_copy;
408 EMPTY_TO_MINSIZE(so);
409 }
410 /* else it's a small table that's already empty */
411
412 /* Now we can finally clear things. If C had refcounts, we could
413 * assert that the refcount on table is 1 now, i.e. that this function
414 * has unique access to it, so decref side-effects can't alter it.
415 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000416 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000417#ifdef Py_DEBUG
418 assert(i < n);
419 ++i;
420#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000421 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000422 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000423 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424 }
425#ifdef Py_DEBUG
426 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000427 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000428#endif
429 }
430
431 if (table_is_malloced)
432 PyMem_DEL(table);
433}
434
435/*
436 * Iterate over a set table. Use like so:
437 *
438 * int i;
439 * PyObject *key;
440 * i = 0; # important! i should not otherwise be changed by you
441 * while (set_next_internal(yourset, &i, &key)) {
442 * Refer to borrowed reference in key.
443 * }
444 *
445 * CAUTION: In general, it isn't safe to use set_next_internal in a loop that
446 * mutates the table.
447 */
448static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000449set_next_internal(PySetObject *so, int *pos, PyObject **key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450{
451 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000452 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453
454 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000455 i = *pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456 if (i < 0)
457 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000458 entry = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000459 mask = so->mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000460 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000461 i++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000462 *pos = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000463 if (i > mask)
464 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000465 if (key)
466 *key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467 return 1;
468}
469
470/* Methods */
471
472static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000473set_merge_internal(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000474{
475 register PySetObject *other;
476 register int i;
477 setentry *entry;
478
479 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000480 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000481
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000482 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483 if (other == so || other->used == 0)
484 /* a.update(a) or a.update({}); nothing to do */
485 return 0;
486 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000487 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000488 * that there will be no (or few) overlapping keys.
489 */
490 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
491 if (set_table_resize(so, (so->used + other->used)*2) != 0)
492 return -1;
493 }
494 for (i = 0; i <= other->mask; i++) {
495 entry = &other->table[i];
496 if (entry->key != NULL &&
497 entry->key != dummy) {
498 Py_INCREF(entry->key);
499 set_insert_key(so, entry->key, entry->hash);
500 }
501 }
502 return 0;
503}
504
505static int
506set_contains_internal(PySetObject *so, PyObject *key)
507{
508 long hash;
509
510 if (!PyString_CheckExact(key) ||
511 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
512 hash = PyObject_Hash(key);
513 if (hash == -1)
514 return -1;
515 }
516 key = (so->lookup)(so, key, hash)->key;
517 return key != NULL && key != dummy;
518}
519
520static PyTypeObject PySetIter_Type; /* Forward */
521
522/* Set iterator types */
523
524typedef struct {
525 PyObject_HEAD
526 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
527 int si_used;
528 int si_pos;
529 long len;
530} setiterobject;
531
532static PyObject *
533set_iter(PySetObject *so)
534{
535 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
536 if (si == NULL)
537 return NULL;
538 Py_INCREF(so);
539 si->si_set = so;
540 si->si_used = so->used;
541 si->si_pos = 0;
542 si->len = so->used;
543 return (PyObject *)si;
544}
545
546static void
547setiter_dealloc(setiterobject *si)
548{
549 Py_XDECREF(si->si_set);
550 PyObject_Del(si);
551}
552
553static int
554setiter_len(setiterobject *si)
555{
556 if (si->si_set != NULL && si->si_used == si->si_set->used)
557 return si->len;
558 return 0;
559}
560
561static PySequenceMethods setiter_as_sequence = {
562 (inquiry)setiter_len, /* sq_length */
563 0, /* sq_concat */
564};
565
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000566static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000567{
568 PyObject *key;
569 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000570 register setentry *entry;
571 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000572
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000573 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000574 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000575 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000576
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000577 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000578 PyErr_SetString(PyExc_RuntimeError,
579 "Set changed size during iteration");
580 si->si_used = -1; /* Make this state sticky */
581 return NULL;
582 }
583
584 i = si->si_pos;
585 if (i < 0)
586 goto fail;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000587 entry = so->table;
588 mask = so->mask;
589 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000590 i++;
591 si->si_pos = i+1;
592 if (i > mask)
593 goto fail;
594 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000595 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000596 Py_INCREF(key);
597 return key;
598
599fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000600 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000601 si->si_set = NULL;
602 return NULL;
603}
604
605PyTypeObject PySetIter_Type = {
606 PyObject_HEAD_INIT(&PyType_Type)
607 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000608 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000609 sizeof(setiterobject), /* tp_basicsize */
610 0, /* tp_itemsize */
611 /* methods */
612 (destructor)setiter_dealloc, /* tp_dealloc */
613 0, /* tp_print */
614 0, /* tp_getattr */
615 0, /* tp_setattr */
616 0, /* tp_compare */
617 0, /* tp_repr */
618 0, /* tp_as_number */
619 &setiter_as_sequence, /* tp_as_sequence */
620 0, /* tp_as_mapping */
621 0, /* tp_hash */
622 0, /* tp_call */
623 0, /* tp_str */
624 PyObject_GenericGetAttr, /* tp_getattro */
625 0, /* tp_setattro */
626 0, /* tp_as_buffer */
627 Py_TPFLAGS_DEFAULT, /* tp_flags */
628 0, /* tp_doc */
629 0, /* tp_traverse */
630 0, /* tp_clear */
631 0, /* tp_richcompare */
632 0, /* tp_weaklistoffset */
633 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000634 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000635};
636
637/***** Derived functions (table accesses only done with above primitives *****/
638
Raymond Hettinger691d8052004-05-30 07:26:47 +0000639#include "structmember.h"
Raymond Hettingera690a992003-11-16 16:17:49 +0000640
641/* set object implementation
642 written and maintained by Raymond D. Hettinger <python@rcn.com>
643 derived from sets.py written by Greg V. Wilson, Alex Martelli,
644 Guido van Rossum, Raymond Hettinger, and Tim Peters.
645
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000646 Copyright (c) 2003-5 Python Software Foundation.
Raymond Hettingera690a992003-11-16 16:17:49 +0000647 All rights reserved.
648*/
649
Raymond Hettingera690a992003-11-16 16:17:49 +0000650static PyObject *
Raymond Hettingera38123e2003-11-24 22:18:49 +0000651set_update(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000652{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000653 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000654
Raymond Hettingera38123e2003-11-24 22:18:49 +0000655 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000656 if (set_merge_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000657 return NULL;
658 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000659 }
660
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000661 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000662 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000663 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000664 while (PyDict_Next(other, &pos, &key, &value)) {
665 if (set_add_internal(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000666 return NULL;
667 }
668 Py_RETURN_NONE;
669 }
670
Raymond Hettingera38123e2003-11-24 22:18:49 +0000671 it = PyObject_GetIter(other);
672 if (it == NULL)
673 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000674
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000675 while ((key = PyIter_Next(it)) != NULL) {
676 if (set_add_internal(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000677 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000678 Py_DECREF(key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000679 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000680 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000681 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000682 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000683 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000684 if (PyErr_Occurred())
Raymond Hettingera38123e2003-11-24 22:18:49 +0000685 return NULL;
686 Py_RETURN_NONE;
687}
688
689PyDoc_STRVAR(update_doc,
690"Update a set with the union of itself and another.");
691
692static PyObject *
693make_new_set(PyTypeObject *type, PyObject *iterable)
694{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000695 PyObject *tmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000696 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000697
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000698 if (dummy == NULL) { /* Auto-initialize dummy */
699 dummy = PyString_FromString("<dummy key>");
700 if (dummy == NULL)
701 return NULL;
702 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000703
704 /* create PySetObject structure */
705 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000706 if (so == NULL)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000707 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000708
709 EMPTY_TO_MINSIZE(so);
710 so->lookup = set_lookkey_string;
Raymond Hettingera690a992003-11-16 16:17:49 +0000711 so->hash = -1;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000712 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000713
Raymond Hettingera38123e2003-11-24 22:18:49 +0000714 if (iterable != NULL) {
715 tmp = set_update(so, iterable);
716 if (tmp == NULL) {
717 Py_DECREF(so);
718 return NULL;
719 }
720 Py_DECREF(tmp);
721 }
722
Raymond Hettingera690a992003-11-16 16:17:49 +0000723 return (PyObject *)so;
724}
725
726static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000727frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000728{
729 PyObject *iterable = NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000730 static PyObject *emptyfrozenset = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000731
732 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
733 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000734 if (iterable == NULL) {
735 if (type == &PyFrozenSet_Type) {
736 if (emptyfrozenset == NULL)
737 emptyfrozenset = make_new_set(type, NULL);
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000738 Py_INCREF(emptyfrozenset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000739 return emptyfrozenset;
740 }
741 } else if (PyFrozenSet_CheckExact(iterable)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000742 Py_INCREF(iterable);
743 return iterable;
744 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000745 return make_new_set(type, iterable);
746}
747
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000748static PyObject *
749set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
750{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000751 return make_new_set(type, NULL);
752}
753
Raymond Hettingera690a992003-11-16 16:17:49 +0000754static void
755set_dealloc(PySetObject *so)
756{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000757 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000758 int fill = so->fill;
759
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000760 PyObject_GC_UnTrack(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000761 Py_TRASHCAN_SAFE_BEGIN(so)
Raymond Hettinger691d8052004-05-30 07:26:47 +0000762 if (so->weakreflist != NULL)
763 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000764
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000765 for (entry = so->table; fill > 0; entry++) {
766 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000767 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000768 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000769 }
770 }
771 if (so->table != so->smalltable)
772 PyMem_DEL(so->table);
773
Raymond Hettingera690a992003-11-16 16:17:49 +0000774 so->ob_type->tp_free(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000775 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000776}
777
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000778static int
779set_traverse(PySetObject *so, visitproc visit, void *arg)
780{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000781 int pos = 0;
782 PyObject *key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000783
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000784 while (set_next_internal(so, &pos, &key))
785 Py_VISIT(key);
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000786 return 0;
787}
788
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789static int
790set_len(PyObject *so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000791{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792 return ((PySetObject *)so)->used;
Raymond Hettingera690a992003-11-16 16:17:49 +0000793}
794
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000795/* set_swap_bodies() switches the contents of any two sets by moving their
796 internal data pointers and, if needed, copying the internal smalltables.
797 Semantically equivalent to:
798
799 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
800
801 The function always succeeds and it leaves both objects in a stable state.
802 Useful for creating temporary frozensets from sets for membership testing
803 in __contains__(), discard(), and remove(). Also useful for operations
804 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000805 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000806*/
807
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000808static void
809set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000810{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811 int t;
812 setentry *u;
813 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
814 setentry tab[PySet_MINSIZE];
815 long h;
816
817 t = a->fill; a->fill = b->fill; b->fill = t;
818 t = a->used; a->used = b->used; b->used = t;
819 t = a->mask; a->mask = b->mask; b->mask = t;
820
821 u = a->table;
822 if (a->table == a->smalltable)
823 u = b->smalltable;
824 a->table = b->table;
825 if (b->table == b->smalltable)
826 a->table = a->smalltable;
827 b->table = u;
828
829 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
830
831 if (a->table == a->smalltable || b->table == b->smalltable) {
832 memcpy(tab, a->smalltable, sizeof(tab));
833 memcpy(a->smalltable, b->smalltable, sizeof(tab));
834 memcpy(b->smalltable, tab, sizeof(tab));
835 }
836
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000837 h = a->hash; a->hash = b->hash; b->hash = h;
Raymond Hettingera690a992003-11-16 16:17:49 +0000838}
839
840static int
841set_contains(PySetObject *so, PyObject *key)
842{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000843 PyObject *tmpkey;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000844 int result;
845
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000846 result = set_contains_internal(so, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000847 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000848 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000849 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
850 if (tmpkey == NULL)
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000851 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000852 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
853 result = set_contains_internal(so, tmpkey);
854 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
855 Py_DECREF(tmpkey);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000856 }
857 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000858}
859
860static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000861set_direct_contains(PySetObject *so, PyObject *key)
862{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000863 long result;
864
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000865 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000866 if (result == -1)
867 return NULL;
868 return PyBool_FromLong(result);
869}
870
871PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
872
873static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000874set_copy(PySetObject *so)
875{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000876 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000877}
878
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000879static PyObject *
880frozenset_copy(PySetObject *so)
881{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000882 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000883 Py_INCREF(so);
884 return (PyObject *)so;
885 }
886 return set_copy(so);
887}
888
Raymond Hettingera690a992003-11-16 16:17:49 +0000889PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
890
891static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000892set_union(PySetObject *so, PyObject *other)
893{
894 PySetObject *result;
895 PyObject *rv;
896
897 result = (PySetObject *)set_copy(so);
898 if (result == NULL)
899 return NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000900 rv = set_update(result, other);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000901 if (rv == NULL) {
902 Py_DECREF(result);
903 return NULL;
904 }
905 Py_DECREF(rv);
906 return (PyObject *)result;
907}
908
909PyDoc_STRVAR(union_doc,
910 "Return the union of two sets as a new set.\n\
911\n\
912(i.e. all elements that are in either set.)");
913
914static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000915set_or(PySetObject *so, PyObject *other)
916{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000917 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000918 Py_INCREF(Py_NotImplemented);
919 return Py_NotImplemented;
920 }
921 return set_union(so, other);
922}
923
924static PyObject *
925set_ior(PySetObject *so, PyObject *other)
926{
927 PyObject *result;
928
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000929 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000930 Py_INCREF(Py_NotImplemented);
931 return Py_NotImplemented;
932 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000933 result = set_update(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000934 if (result == NULL)
935 return NULL;
936 Py_DECREF(result);
937 Py_INCREF(so);
938 return (PyObject *)so;
939}
940
941static PyObject *
942set_intersection(PySetObject *so, PyObject *other)
943{
944 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000945 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000946
947 result = (PySetObject *)make_new_set(so->ob_type, NULL);
948 if (result == NULL)
949 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000950
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000951 if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) {
952 tmp = (PyObject *)so;
953 so = (PySetObject *)other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000954 other = tmp;
955 }
956
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000957 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000958 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000959 while (set_next_internal((PySetObject *)other, &pos, &key)) {
960 if (set_contains_internal(so, key)) {
961 if (set_add_internal(result, key) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000962 Py_DECREF(result);
963 return NULL;
964 }
965 }
966 }
967 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000968 }
969
Raymond Hettingera690a992003-11-16 16:17:49 +0000970 it = PyObject_GetIter(other);
971 if (it == NULL) {
972 Py_DECREF(result);
973 return NULL;
974 }
975
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000976 while ((key = PyIter_Next(it)) != NULL) {
977 if (set_contains_internal(so, key)) {
978 if (set_add_internal(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000979 Py_DECREF(it);
980 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000981 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000982 return NULL;
983 }
984 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000985 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000986 }
987 Py_DECREF(it);
988 if (PyErr_Occurred()) {
989 Py_DECREF(result);
990 return NULL;
991 }
992 return (PyObject *)result;
993}
994
995PyDoc_STRVAR(intersection_doc,
996"Return the intersection of two sets as a new set.\n\
997\n\
998(i.e. all elements that are in both sets.)");
999
1000static PyObject *
1001set_intersection_update(PySetObject *so, PyObject *other)
1002{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001003 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001004
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001005 tmp = set_intersection(so, other);
1006 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001007 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001008 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001009 Py_DECREF(tmp);
1010 Py_RETURN_NONE;
1011}
1012
1013PyDoc_STRVAR(intersection_update_doc,
1014"Update a set with the intersection of itself and another.");
1015
1016static PyObject *
1017set_and(PySetObject *so, PyObject *other)
1018{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001019 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001020 Py_INCREF(Py_NotImplemented);
1021 return Py_NotImplemented;
1022 }
1023 return set_intersection(so, other);
1024}
1025
1026static PyObject *
1027set_iand(PySetObject *so, PyObject *other)
1028{
1029 PyObject *result;
1030
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001031 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001032 Py_INCREF(Py_NotImplemented);
1033 return Py_NotImplemented;
1034 }
1035 result = set_intersection_update(so, other);
1036 if (result == NULL)
1037 return NULL;
1038 Py_DECREF(result);
1039 Py_INCREF(so);
1040 return (PyObject *)so;
1041}
1042
1043static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001044set_difference_update(PySetObject *so, PyObject *other)
1045{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001046 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001047
1048 it = PyObject_GetIter(other);
1049 if (it == NULL)
1050 return NULL;
1051
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001052 while ((key = PyIter_Next(it)) != NULL) {
1053 if (set_discard_internal(so, key) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001054 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001055 Py_DECREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001056 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001057 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001058 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001059 }
1060 Py_DECREF(it);
1061 if (PyErr_Occurred())
1062 return NULL;
1063 Py_RETURN_NONE;
1064}
1065
1066PyDoc_STRVAR(difference_update_doc,
1067"Remove all elements of another set from this set.");
1068
1069static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001070set_difference(PySetObject *so, PyObject *other)
1071{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001072 PyObject *tmp, *key, *result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001073 int pos = 0;
1074
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001075 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001076 result = set_copy(so);
1077 if (result == NULL)
1078 return result;
1079 tmp = set_difference_update((PySetObject *)result, other);
1080 if (tmp != NULL) {
1081 Py_DECREF(tmp);
1082 return result;
1083 }
1084 Py_DECREF(result);
1085 return NULL;
1086 }
1087
1088 result = make_new_set(so->ob_type, NULL);
1089 if (result == NULL)
1090 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001091
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001092 if (PyDict_Check(other)) {
1093 while (set_next_internal(so, &pos, &key)) {
1094 if (!PyDict_Contains(other, key)) {
1095 if (set_add_internal((PySetObject *)result, key) == -1)
1096 return NULL;
1097 }
1098 }
1099 return result;
1100 }
1101
1102 while (set_next_internal(so, &pos, &key)) {
1103 if (!set_contains_internal((PySetObject *)other, key)) {
1104 if (set_add_internal((PySetObject *)result, key) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001105 return NULL;
1106 }
1107 }
1108 return result;
1109}
1110
1111PyDoc_STRVAR(difference_doc,
1112"Return the difference of two sets as a new set.\n\
1113\n\
1114(i.e. all elements that are in this set but not the other.)");
1115static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001116set_sub(PySetObject *so, PyObject *other)
1117{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001118 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001119 Py_INCREF(Py_NotImplemented);
1120 return Py_NotImplemented;
1121 }
1122 return set_difference(so, other);
1123}
1124
1125static PyObject *
1126set_isub(PySetObject *so, PyObject *other)
1127{
1128 PyObject *result;
1129
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001130 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001131 Py_INCREF(Py_NotImplemented);
1132 return Py_NotImplemented;
1133 }
1134 result = set_difference_update(so, other);
1135 if (result == NULL)
1136 return NULL;
1137 Py_DECREF(result);
1138 Py_INCREF(so);
1139 return (PyObject *)so;
1140}
1141
1142static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001143set_symmetric_difference_update(PySetObject *so, PyObject *other)
1144{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001145 PySetObject *otherset;
1146 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001147 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001148
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001149 if (PyDict_Check(other)) {
1150 PyObject *value;
1151 int rv;
1152 while (PyDict_Next(other, &pos, &key, &value)) {
1153 rv = set_discard_internal(so, key);
1154 if (rv == -1)
1155 return NULL;
1156 if (rv == DISCARD_NOTFOUND) {
1157 if (set_add_internal(so, key) == -1)
1158 return NULL;
1159 }
1160 }
1161 Py_RETURN_NONE;
1162 }
1163
1164 if (PyAnySet_Check(other)) {
1165 Py_INCREF(other);
1166 otherset = (PySetObject *)other;
1167 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001168 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1169 if (otherset == NULL)
1170 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001171 }
1172
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001173 while (set_next_internal(otherset, &pos, &key)) {
1174 int rv = set_discard_internal(so, key);
1175 if (rv == -1) {
1176 Py_XDECREF(otherset);
1177 return NULL;
1178 }
1179 if (rv == DISCARD_NOTFOUND) {
1180 if (set_add_internal(so, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001181 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001182 return NULL;
1183 }
1184 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001185 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001186 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001187 Py_RETURN_NONE;
1188}
1189
1190PyDoc_STRVAR(symmetric_difference_update_doc,
1191"Update a set with the symmetric difference of itself and another.");
1192
1193static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001194set_symmetric_difference(PySetObject *so, PyObject *other)
1195{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001196 PyObject *rv;
1197 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001198
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001199 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1200 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001201 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001202 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1203 if (rv == NULL)
1204 return NULL;
1205 Py_DECREF(rv);
1206 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001207}
1208
1209PyDoc_STRVAR(symmetric_difference_doc,
1210"Return the symmetric difference of two sets as a new set.\n\
1211\n\
1212(i.e. all elements that are in exactly one of the sets.)");
1213
1214static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001215set_xor(PySetObject *so, PyObject *other)
1216{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001217 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001218 Py_INCREF(Py_NotImplemented);
1219 return Py_NotImplemented;
1220 }
1221 return set_symmetric_difference(so, other);
1222}
1223
1224static PyObject *
1225set_ixor(PySetObject *so, PyObject *other)
1226{
1227 PyObject *result;
1228
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001229 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001230 Py_INCREF(Py_NotImplemented);
1231 return Py_NotImplemented;
1232 }
1233 result = set_symmetric_difference_update(so, other);
1234 if (result == NULL)
1235 return NULL;
1236 Py_DECREF(result);
1237 Py_INCREF(so);
1238 return (PyObject *)so;
1239}
1240
1241static PyObject *
1242set_issubset(PySetObject *so, PyObject *other)
1243{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001244 PyObject *tmp, *result;
1245 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001246 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001247
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001248 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001249 tmp = make_new_set(&PySet_Type, other);
1250 if (tmp == NULL)
1251 return NULL;
1252 result = set_issubset(so, tmp);
1253 Py_DECREF(tmp);
1254 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001255 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001256 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001257 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001258
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001259 while (set_next_internal(so, &pos, &key)) {
1260 if (!set_contains_internal((PySetObject *)other, key))
Raymond Hettingera690a992003-11-16 16:17:49 +00001261 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001262 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001263 Py_RETURN_TRUE;
1264}
1265
1266PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1267
1268static PyObject *
1269set_issuperset(PySetObject *so, PyObject *other)
1270{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001271 PyObject *tmp, *result;
1272
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001273 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001274 tmp = make_new_set(&PySet_Type, other);
1275 if (tmp == NULL)
1276 return NULL;
1277 result = set_issuperset(so, tmp);
1278 Py_DECREF(tmp);
1279 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001280 }
1281 return set_issubset((PySetObject *)other, (PyObject *)so);
1282}
1283
1284PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1285
1286static long
1287set_nohash(PyObject *self)
1288{
1289 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1290 return -1;
1291}
1292
1293static int
1294set_nocmp(PyObject *self)
1295{
1296 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1297 return -1;
1298}
1299
1300static long
1301frozenset_hash(PyObject *self)
1302{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001303 PyObject *key;
Raymond Hettingera690a992003-11-16 16:17:49 +00001304 PySetObject *so = (PySetObject *)self;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001305 int pos = 0;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001306 long hash = 1927868237L;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001307
Raymond Hettingera690a992003-11-16 16:17:49 +00001308 if (so->hash != -1)
1309 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001310
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001311 hash *= set_len(self) + 1;
1312 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingerc9786332004-06-10 22:41:48 +00001313 /* Work to increase the bit dispersion for closely spaced hash
1314 values. The is important because some use cases have many
1315 combinations of a small number of elements with nearby
1316 hashes so that many distinct combinations collapse to only
1317 a handful of distinct hash values. */
1318 long h = PyObject_Hash(key);
1319 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001320 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001321 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001322 if (hash == -1)
1323 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001324 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001325 return hash;
1326}
1327
1328static PyObject *
1329set_richcompare(PySetObject *v, PyObject *w, int op)
1330{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001331 PyObject *r1, *r2;
1332
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001333 if(!PyAnySet_Check(w)) {
1334 if (op == Py_EQ)
1335 Py_RETURN_FALSE;
1336 if (op == Py_NE)
1337 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001338 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1339 return NULL;
1340 }
1341 switch (op) {
1342 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001343 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001344 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001345 return set_issubset(v, w);
1346 case Py_NE:
1347 if (set_len((PyObject *)v) != set_len(w))
1348 Py_RETURN_TRUE;
1349 r1 = set_issubset(v, w);
1350 assert (r1 != NULL);
1351 r2 = PyBool_FromLong(PyObject_Not(r1));
1352 Py_DECREF(r1);
1353 return r2;
1354 case Py_LE:
1355 return set_issubset(v, w);
1356 case Py_GE:
1357 return set_issuperset(v, w);
1358 case Py_LT:
1359 if (set_len((PyObject *)v) >= set_len(w))
1360 Py_RETURN_FALSE;
1361 return set_issubset(v, w);
1362 case Py_GT:
1363 if (set_len((PyObject *)v) <= set_len(w))
1364 Py_RETURN_FALSE;
1365 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001366 }
1367 Py_INCREF(Py_NotImplemented);
1368 return Py_NotImplemented;
1369}
1370
1371static PyObject *
1372set_repr(PySetObject *so)
1373{
1374 PyObject *keys, *result, *listrepr;
1375
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001376 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001377 if (keys == NULL)
1378 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001379 listrepr = PyObject_Repr(keys);
1380 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001381 if (listrepr == NULL)
1382 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001383
1384 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1385 PyString_AS_STRING(listrepr));
1386 Py_DECREF(listrepr);
1387 return result;
1388}
1389
1390static int
1391set_tp_print(PySetObject *so, FILE *fp, int flags)
1392{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001393 PyObject *key;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001394 int pos=0;
1395 char *emit = ""; /* No separator emitted on first pass */
1396 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001397
Raymond Hettingera690a992003-11-16 16:17:49 +00001398 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001399 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001400 fputs(emit, fp);
1401 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001402 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001403 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001404 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001405 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001406 return 0;
1407}
1408
1409static PyObject *
1410set_clear(PySetObject *so)
1411{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001412 set_clear_internal(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001413 so->hash = -1;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001414 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001415}
1416
1417PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1418
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001419static int
1420set_tp_clear(PySetObject *so)
1421{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001422 set_clear_internal(so);
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001423 so->hash = -1;
1424 return 0;
1425}
1426
Raymond Hettingera690a992003-11-16 16:17:49 +00001427static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001428set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001429{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001430 if (set_add_internal(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001431 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001432 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001433}
1434
1435PyDoc_STRVAR(add_doc,
1436"Add an element to a set.\n\
1437\n\
1438This has no effect if the element is already present.");
1439
1440static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001441set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001442{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001443 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001444 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001445
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001446 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1447 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1448 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001449 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001450 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1451 result = set_remove(so, tmpkey);
1452 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1453 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001454 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001455 }
Raymond Hettinger0deab622003-12-13 18:53:18 +00001456
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001457 rv = set_discard_internal(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001458 if (rv == -1)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001459 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001460 else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001461 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001462 return NULL;
1463 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001464 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001465}
1466
1467PyDoc_STRVAR(remove_doc,
1468"Remove an element from a set; it must be a member.\n\
1469\n\
1470If the element is not a member, raise a KeyError.");
1471
1472static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001473set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001474{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001475 PyObject *tmpkey, *result;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001476
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001477 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1478 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1479 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001480 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001481 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1482 result = set_discard(so, tmpkey);
1483 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1484 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001485 return result;
1486 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001487
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001488 if (set_discard_internal(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001489 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001490 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001491}
1492
1493PyDoc_STRVAR(discard_doc,
1494"Remove an element from a set if it is a member.\n\
1495\n\
1496If the element is not a member, do nothing.");
1497
1498static PyObject *
1499set_pop(PySetObject *so)
1500{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001501 PyObject *key;
Raymond Hettingera690a992003-11-16 16:17:49 +00001502 int pos = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001503 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001504
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001505 if (!set_next_internal(so, &pos, &key)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001506 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1507 return NULL;
1508 }
1509 Py_INCREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001510
1511 rv = set_discard_internal(so, key);
1512 if (rv == -1) {
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001513 Py_DECREF(key);
1514 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001515 } else if (rv == DISCARD_NOTFOUND) {
1516 Py_DECREF(key);
1517 PyErr_SetObject(PyExc_KeyError, key);
1518 return NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001519 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001520 return key;
1521}
1522
1523PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1524
1525static PyObject *
1526set_reduce(PySetObject *so)
1527{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001528 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001529
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001530 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001531 if (keys == NULL)
1532 goto done;
1533 args = PyTuple_Pack(1, keys);
1534 if (args == NULL)
1535 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001536 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1537 if (dict == NULL) {
1538 PyErr_Clear();
1539 dict = Py_None;
1540 Py_INCREF(dict);
1541 }
1542 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001543done:
1544 Py_XDECREF(args);
1545 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001546 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001547 return result;
1548}
1549
1550PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1551
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001552static int
1553set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1554{
1555 PyObject *iterable = NULL;
1556 PyObject *result;
1557
1558 if (!PyAnySet_Check(self))
1559 return -1;
1560 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1561 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001562 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001563 self->hash = -1;
1564 if (iterable == NULL)
1565 return 0;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001566 result = set_update(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001567 if (result != NULL) {
1568 Py_DECREF(result);
1569 return 0;
1570 }
1571 return -1;
1572}
1573
Raymond Hettingera690a992003-11-16 16:17:49 +00001574static PySequenceMethods set_as_sequence = {
1575 (inquiry)set_len, /* sq_length */
1576 0, /* sq_concat */
1577 0, /* sq_repeat */
1578 0, /* sq_item */
1579 0, /* sq_slice */
1580 0, /* sq_ass_item */
1581 0, /* sq_ass_slice */
1582 (objobjproc)set_contains, /* sq_contains */
1583};
1584
1585/* set object ********************************************************/
1586
1587static PyMethodDef set_methods[] = {
1588 {"add", (PyCFunction)set_add, METH_O,
1589 add_doc},
1590 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1591 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001592 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001593 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001594 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1595 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001596 {"discard", (PyCFunction)set_discard, METH_O,
1597 discard_doc},
1598 {"difference", (PyCFunction)set_difference, METH_O,
1599 difference_doc},
1600 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1601 difference_update_doc},
1602 {"intersection",(PyCFunction)set_intersection, METH_O,
1603 intersection_doc},
1604 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1605 intersection_update_doc},
1606 {"issubset", (PyCFunction)set_issubset, METH_O,
1607 issubset_doc},
1608 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1609 issuperset_doc},
1610 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1611 pop_doc},
1612 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1613 reduce_doc},
1614 {"remove", (PyCFunction)set_remove, METH_O,
1615 remove_doc},
1616 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1617 symmetric_difference_doc},
1618 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1619 symmetric_difference_update_doc},
1620 {"union", (PyCFunction)set_union, METH_O,
1621 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001622 {"update", (PyCFunction)set_update, METH_O,
1623 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001624 {NULL, NULL} /* sentinel */
1625};
1626
1627static PyNumberMethods set_as_number = {
1628 0, /*nb_add*/
1629 (binaryfunc)set_sub, /*nb_subtract*/
1630 0, /*nb_multiply*/
1631 0, /*nb_divide*/
1632 0, /*nb_remainder*/
1633 0, /*nb_divmod*/
1634 0, /*nb_power*/
1635 0, /*nb_negative*/
1636 0, /*nb_positive*/
1637 0, /*nb_absolute*/
1638 0, /*nb_nonzero*/
1639 0, /*nb_invert*/
1640 0, /*nb_lshift*/
1641 0, /*nb_rshift*/
1642 (binaryfunc)set_and, /*nb_and*/
1643 (binaryfunc)set_xor, /*nb_xor*/
1644 (binaryfunc)set_or, /*nb_or*/
1645 0, /*nb_coerce*/
1646 0, /*nb_int*/
1647 0, /*nb_long*/
1648 0, /*nb_float*/
1649 0, /*nb_oct*/
1650 0, /*nb_hex*/
1651 0, /*nb_inplace_add*/
1652 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1653 0, /*nb_inplace_multiply*/
1654 0, /*nb_inplace_divide*/
1655 0, /*nb_inplace_remainder*/
1656 0, /*nb_inplace_power*/
1657 0, /*nb_inplace_lshift*/
1658 0, /*nb_inplace_rshift*/
1659 (binaryfunc)set_iand, /*nb_inplace_and*/
1660 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1661 (binaryfunc)set_ior, /*nb_inplace_or*/
1662};
1663
1664PyDoc_STRVAR(set_doc,
1665"set(iterable) --> set object\n\
1666\n\
1667Build an unordered collection.");
1668
1669PyTypeObject PySet_Type = {
1670 PyObject_HEAD_INIT(&PyType_Type)
1671 0, /* ob_size */
1672 "set", /* tp_name */
1673 sizeof(PySetObject), /* tp_basicsize */
1674 0, /* tp_itemsize */
1675 /* methods */
1676 (destructor)set_dealloc, /* tp_dealloc */
1677 (printfunc)set_tp_print, /* tp_print */
1678 0, /* tp_getattr */
1679 0, /* tp_setattr */
1680 (cmpfunc)set_nocmp, /* tp_compare */
1681 (reprfunc)set_repr, /* tp_repr */
1682 &set_as_number, /* tp_as_number */
1683 &set_as_sequence, /* tp_as_sequence */
1684 0, /* tp_as_mapping */
1685 set_nohash, /* tp_hash */
1686 0, /* tp_call */
1687 0, /* tp_str */
1688 PyObject_GenericGetAttr, /* tp_getattro */
1689 0, /* tp_setattro */
1690 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001691 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001692 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001693 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001694 (traverseproc)set_traverse, /* tp_traverse */
1695 (inquiry)set_tp_clear, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001696 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001697 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001698 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001699 0, /* tp_iternext */
1700 set_methods, /* tp_methods */
1701 0, /* tp_members */
1702 0, /* tp_getset */
1703 0, /* tp_base */
1704 0, /* tp_dict */
1705 0, /* tp_descr_get */
1706 0, /* tp_descr_set */
1707 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001708 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001709 PyType_GenericAlloc, /* tp_alloc */
1710 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001711 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001712};
1713
1714/* frozenset object ********************************************************/
1715
1716
1717static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001718 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001719 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001720 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001721 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001722 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001723 difference_doc},
1724 {"intersection",(PyCFunction)set_intersection, METH_O,
1725 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001726 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001727 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001728 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001729 issuperset_doc},
1730 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1731 reduce_doc},
1732 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1733 symmetric_difference_doc},
1734 {"union", (PyCFunction)set_union, METH_O,
1735 union_doc},
1736 {NULL, NULL} /* sentinel */
1737};
1738
1739static PyNumberMethods frozenset_as_number = {
1740 0, /*nb_add*/
1741 (binaryfunc)set_sub, /*nb_subtract*/
1742 0, /*nb_multiply*/
1743 0, /*nb_divide*/
1744 0, /*nb_remainder*/
1745 0, /*nb_divmod*/
1746 0, /*nb_power*/
1747 0, /*nb_negative*/
1748 0, /*nb_positive*/
1749 0, /*nb_absolute*/
1750 0, /*nb_nonzero*/
1751 0, /*nb_invert*/
1752 0, /*nb_lshift*/
1753 0, /*nb_rshift*/
1754 (binaryfunc)set_and, /*nb_and*/
1755 (binaryfunc)set_xor, /*nb_xor*/
1756 (binaryfunc)set_or, /*nb_or*/
1757};
1758
1759PyDoc_STRVAR(frozenset_doc,
1760"frozenset(iterable) --> frozenset object\n\
1761\n\
1762Build an immutable unordered collection.");
1763
1764PyTypeObject PyFrozenSet_Type = {
1765 PyObject_HEAD_INIT(&PyType_Type)
1766 0, /* ob_size */
1767 "frozenset", /* tp_name */
1768 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001769 0, /* tp_itemsize */
1770 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001771 (destructor)set_dealloc, /* tp_dealloc */
1772 (printfunc)set_tp_print, /* tp_print */
1773 0, /* tp_getattr */
1774 0, /* tp_setattr */
1775 (cmpfunc)set_nocmp, /* tp_compare */
1776 (reprfunc)set_repr, /* tp_repr */
1777 &frozenset_as_number, /* tp_as_number */
1778 &set_as_sequence, /* tp_as_sequence */
1779 0, /* tp_as_mapping */
1780 frozenset_hash, /* tp_hash */
1781 0, /* tp_call */
1782 0, /* tp_str */
1783 PyObject_GenericGetAttr, /* tp_getattro */
1784 0, /* tp_setattro */
1785 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001786 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001787 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001788 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001789 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingera690a992003-11-16 16:17:49 +00001790 0, /* tp_clear */
1791 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001792 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001793 (getiterfunc)set_iter, /* tp_iter */
1794 0, /* tp_iternext */
1795 frozenset_methods, /* tp_methods */
1796 0, /* tp_members */
1797 0, /* tp_getset */
1798 0, /* tp_base */
1799 0, /* tp_dict */
1800 0, /* tp_descr_get */
1801 0, /* tp_descr_set */
1802 0, /* tp_dictoffset */
1803 0, /* tp_init */
1804 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001805 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001806 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001807};