blob: 356b6be3f40f91225f9cac1f9e097e075ac6c4fb [file] [log] [blame]
Raymond Hettingera9d99362005-08-05 00:01:15 +00001/* set object implementation
2 Written and maintained by Raymond D. Hettinger <python@rcn.com>
3 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00004
Raymond Hettingera9d99362005-08-05 00:01:15 +00005 Copyright (c) 2003-5 Python Software Foundation.
6 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00007*/
8
Raymond Hettingera690a992003-11-16 16:17:49 +00009#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000010#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000011
12/* This must be >= 1. */
13#define PERTURB_SHIFT 5
14
15/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000016static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000017
18#define EMPTY_TO_MINSIZE(so) do { \
19 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
20 (so)->used = (so)->fill = 0; \
21 (so)->table = (so)->smalltable; \
22 (so)->mask = PySet_MINSIZE - 1; \
23 } while(0)
24
25
26/*
27The basic lookup function used by all operations.
28This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
29Open addressing is preferred over chaining since the link overhead for
30chaining would be substantial (100% with typical malloc overhead).
31
32The initial probe index is computed as hash mod the table size. Subsequent
33probe indices are computed as explained earlier.
34
35All arithmetic on hash should ignore overflow.
36
37This function must never return NULL; failures are indicated by returning
38a setentry* for which the value field is NULL. Exceptions are never
39reported by this function, and outstanding exceptions are maintained.
40*/
41
42static setentry *
43set_lookkey(PySetObject *so, PyObject *key, register long hash)
44{
45 register int i;
46 register unsigned int perturb;
47 register setentry *freeslot;
48 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000049 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000050 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051 register int restore_error;
52 register int checked_error;
53 register int cmp;
54 PyObject *err_type, *err_value, *err_tb;
55 PyObject *startkey;
56
57 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000058 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000059 if (entry->key == NULL || entry->key == key)
60 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061
62 restore_error = checked_error = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000063 if (entry->key == dummy)
64 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000065 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000066 if (entry->hash == hash) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000067 /* error can't have been checked yet */
68 checked_error = 1;
69 if (PyErr_Occurred()) {
70 restore_error = 1;
71 PyErr_Fetch(&err_type, &err_value, &err_tb);
72 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000073 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000074 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
75 if (cmp < 0)
76 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +000077 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000078 if (cmp > 0)
79 goto Done;
80 }
81 else {
82 /* The compare did major nasty stuff to the
83 * set: start over.
84 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000085 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086 goto Done;
87 }
88 }
89 freeslot = NULL;
90 }
91
92 /* In the loop, key == dummy is by far (factor of 100s) the
93 least likely outcome, so test for that last. */
94 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
95 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +000096 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000097 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000098 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000099 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000100 break;
101 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000102 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000103 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000104 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000105 if (!checked_error) {
106 checked_error = 1;
107 if (PyErr_Occurred()) {
108 restore_error = 1;
109 PyErr_Fetch(&err_type, &err_value,
110 &err_tb);
111 }
112 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000113 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000114 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
115 if (cmp < 0)
116 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +0000117 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000118 if (cmp > 0)
119 break;
120 }
121 else {
122 /* The compare did major nasty stuff to the
123 * set: start over.
124 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 break;
127 }
128 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000129 else if (entry->key == dummy && freeslot == NULL)
130 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000131 }
132
133Done:
134 if (restore_error)
135 PyErr_Restore(err_type, err_value, err_tb);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000136 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000137}
138
139/*
140 * Hacked up version of set_lookkey which can assume keys are always strings;
141 * this assumption allows testing for errors during PyObject_Compare() to
142 * be dropped; string-string comparisons never raise exceptions. This also
143 * means we don't need to go through PyObject_Compare(); we can always use
144 * _PyString_Eq directly.
145 *
146 * This is valuable because the general-case error handling in set_lookkey() is
147 * expensive, and sets with pure-string keys may be very common.
148 */
149static setentry *
150set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
151{
152 register int i;
153 register unsigned int perturb;
154 register setentry *freeslot;
155 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000156 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000157 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158
159 /* Make sure this function doesn't have to handle non-string keys,
160 including subclasses of str; e.g., one reason to subclass
161 strings is to override __eq__, and for speed we don't cater to
162 that here. */
163 if (!PyString_CheckExact(key)) {
164 so->lookup = set_lookkey;
165 return set_lookkey(so, key, hash);
166 }
167 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000168 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000169 if (entry->key == NULL || entry->key == key)
170 return entry;
Raymond Hettingera580c472005-08-05 17:19:54 +0000171 if (so->fill != so->used) {
172 if (entry->key == dummy)
173 freeslot = entry;
174 else {
175 if (entry->hash == hash && _PyString_Eq(entry->key, key))
176 return entry;
177 freeslot = NULL;
178 }
179
180 /* In the loop, key == dummy is by far (factor of 100s) the
181 least likely outcome, so test for that last. */
182 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
183 i = (i << 2) + i + perturb + 1;
184 entry = &table[i & mask];
185 if (entry->key == NULL)
186 return freeslot == NULL ? entry : freeslot;
187 if (entry->key == key
188 || (entry->hash == hash
189 && entry->key != dummy
190 && _PyString_Eq(entry->key, key)))
191 return entry;
192 if (entry->key == dummy && freeslot == NULL)
193 freeslot = entry;
194 }
195 } else {
196 /* Simplified loop that can assume are no dummy entries */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000197 if (entry->hash == hash && _PyString_Eq(entry->key, key))
198 return entry;
Raymond Hettingera580c472005-08-05 17:19:54 +0000199 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
200 i = (i << 2) + i + perturb + 1;
201 entry = &table[i & mask];
202 if (entry->key == NULL)
203 return entry;
204 if (entry->key == key
205 || (entry->hash == hash
206 && _PyString_Eq(entry->key, key)))
207 return entry;
208 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209 }
210}
211
212/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000213Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000214Used both by the internal resize routine and by the public insert routine.
215Eats a reference to key.
216*/
217static void
218set_insert_key(register PySetObject *so, PyObject *key, long hash)
219{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000221 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
222
223 assert(so->lookup != NULL);
224
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000225 entry = so->lookup(so, key, hash);
226 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000227 /* UNUSED */
228 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000229 entry->key = key;
230 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000231 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000232 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000233 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000234 entry->key = key;
235 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000236 so->used++;
237 Py_DECREF(dummy);
238 } else {
239 /* ACTIVE */
240 Py_DECREF(key);
241 }
242}
243
244/*
245Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000246keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000247actually be smaller than the old one.
248*/
249static int
250set_table_resize(PySetObject *so, int minused)
251{
252 int newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000253 setentry *oldtable, *newtable, *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000254 int i;
255 int is_oldtable_malloced;
256 setentry small_copy[PySet_MINSIZE];
257
258 assert(minused >= 0);
259
260 /* Find the smallest table size > minused. */
261 for (newsize = PySet_MINSIZE;
262 newsize <= minused && newsize > 0;
263 newsize <<= 1)
264 ;
265 if (newsize <= 0) {
266 PyErr_NoMemory();
267 return -1;
268 }
269
270 /* Get space for a new table. */
271 oldtable = so->table;
272 assert(oldtable != NULL);
273 is_oldtable_malloced = oldtable != so->smalltable;
274
275 if (newsize == PySet_MINSIZE) {
276 /* A large table is shrinking, or we can't get any smaller. */
277 newtable = so->smalltable;
278 if (newtable == oldtable) {
279 if (so->fill == so->used) {
280 /* No dummies, so no point doing anything. */
281 return 0;
282 }
283 /* We're not going to resize it, but rebuild the
284 table anyway to purge old dummy entries.
285 Subtle: This is *necessary* if fill==size,
286 as set_lookkey needs at least one virgin slot to
287 terminate failing searches. If fill < size, it's
288 merely desirable, as dummies slow searches. */
289 assert(so->fill > so->used);
290 memcpy(small_copy, oldtable, sizeof(small_copy));
291 oldtable = small_copy;
292 }
293 }
294 else {
295 newtable = PyMem_NEW(setentry, newsize);
296 if (newtable == NULL) {
297 PyErr_NoMemory();
298 return -1;
299 }
300 }
301
302 /* Make the set empty, using the new table. */
303 assert(newtable != oldtable);
304 so->table = newtable;
305 so->mask = newsize - 1;
306 memset(newtable, 0, sizeof(setentry) * newsize);
307 so->used = 0;
308 i = so->fill;
309 so->fill = 0;
310
311 /* Copy the data over; this is refcount-neutral for active entries;
312 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000313 for (entry = oldtable; i > 0; entry++) {
314 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000315 /* UNUSED */
316 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000317 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000318 /* DUMMY */
319 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000320 assert(entry->key == dummy);
321 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000322 } else {
323 /* ACTIVE */
324 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000325 set_insert_key(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000326 }
327 }
328
329 if (is_oldtable_malloced)
330 PyMem_DEL(oldtable);
331 return 0;
332}
333
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334/* CAUTION: set_add_internal() must guarantee that it won't resize the table */
335static int
336set_add_internal(register PySetObject *so, PyObject *key)
337{
338 register long hash;
339 register int n_used;
340
341 if (PyString_CheckExact(key)) {
342 hash = ((PyStringObject *)key)->ob_shash;
343 if (hash == -1)
344 hash = PyObject_Hash(key);
345 } else {
346 hash = PyObject_Hash(key);
347 if (hash == -1)
348 return -1;
349 }
350 assert(so->fill <= so->mask); /* at least one empty slot */
351 n_used = so->used;
352 Py_INCREF(key);
353 set_insert_key(so, key, hash);
354 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
355 return 0;
356 return set_table_resize(so, so->used*(so->used>50000 ? 2 : 4));
357}
358
359#define DISCARD_NOTFOUND 0
360#define DISCARD_FOUND 1
361
362static int
363set_discard_internal(PySetObject *so, PyObject *key)
364{
365 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000366 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000367 PyObject *old_key;
368
369 assert (PyAnySet_Check(so));
370 if (!PyString_CheckExact(key) ||
371 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
372 hash = PyObject_Hash(key);
373 if (hash == -1)
374 return -1;
375 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000376 entry = (so->lookup)(so, key, hash);
377 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000378 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000379 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000381 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382 so->used--;
383 Py_DECREF(old_key);
384 return DISCARD_FOUND;
385}
386
387static void
388set_clear_internal(PySetObject *so)
389{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000390 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000391 int table_is_malloced;
392 int fill;
393 setentry small_copy[PySet_MINSIZE];
394#ifdef Py_DEBUG
395 int i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000396 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000397
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000398 n = so->mask + 1;
399 i = 0;
400#endif
401
402 table = so->table;
403 assert(table != NULL);
404 table_is_malloced = table != so->smalltable;
405
406 /* This is delicate. During the process of clearing the set,
407 * decrefs can cause the set to mutate. To avoid fatal confusion
408 * (voice of experience), we have to make the set empty before
409 * clearing the slots, and never refer to anything via mp->ref while
410 * clearing.
411 */
412 fill = so->fill;
413 if (table_is_malloced)
414 EMPTY_TO_MINSIZE(so);
415
416 else if (fill > 0) {
417 /* It's a small table with something that needs to be cleared.
418 * Afraid the only safe way is to copy the set entries into
419 * another small table first.
420 */
421 memcpy(small_copy, table, sizeof(small_copy));
422 table = small_copy;
423 EMPTY_TO_MINSIZE(so);
424 }
425 /* else it's a small table that's already empty */
426
427 /* Now we can finally clear things. If C had refcounts, we could
428 * assert that the refcount on table is 1 now, i.e. that this function
429 * has unique access to it, so decref side-effects can't alter it.
430 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000431 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000432#ifdef Py_DEBUG
433 assert(i < n);
434 ++i;
435#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000438 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439 }
440#ifdef Py_DEBUG
441 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000442 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443#endif
444 }
445
446 if (table_is_malloced)
447 PyMem_DEL(table);
448}
449
450/*
451 * Iterate over a set table. Use like so:
452 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000453 * int pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454 * PyObject *key;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000455 * pos = 0; # important! pos should not otherwise be changed by you
456 * while (set_next_internal(yourset, &pos, &key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457 * Refer to borrowed reference in key.
458 * }
459 *
460 * CAUTION: In general, it isn't safe to use set_next_internal in a loop that
461 * mutates the table.
462 */
463static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000464set_next_internal(PySetObject *so, int *pos, PyObject **key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000465{
466 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000467 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000468
469 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000470 i = *pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000471 if (i < 0)
472 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000473 entry = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000474 mask = so->mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000475 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476 i++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000477 *pos = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000478 if (i > mask)
479 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000480 if (key)
481 *key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482 return 1;
483}
484
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000485static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000486set_merge_internal(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000488 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489 register int i;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000490 register setentry *entry, *othertable;
491 register int othermask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000492
493 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000494 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000496 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000497 if (other == so || other->used == 0)
498 /* a.update(a) or a.update({}); nothing to do */
499 return 0;
500 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000501 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502 * that there will be no (or few) overlapping keys.
503 */
504 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
505 if (set_table_resize(so, (so->used + other->used)*2) != 0)
506 return -1;
507 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000508 othermask = other->mask;
509 othertable = other->table;
510 for (i = 0; i <= othermask; i++) {
511 entry = &othertable[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512 if (entry->key != NULL &&
513 entry->key != dummy) {
514 Py_INCREF(entry->key);
515 set_insert_key(so, entry->key, entry->hash);
516 }
517 }
518 return 0;
519}
520
521static int
522set_contains_internal(PySetObject *so, PyObject *key)
523{
524 long hash;
525
526 if (!PyString_CheckExact(key) ||
527 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
528 hash = PyObject_Hash(key);
529 if (hash == -1)
530 return -1;
531 }
532 key = (so->lookup)(so, key, hash)->key;
533 return key != NULL && key != dummy;
534}
535
Raymond Hettingera9d99362005-08-05 00:01:15 +0000536/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000537
Raymond Hettingerd7946662005-08-01 21:39:29 +0000538static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000539
540typedef struct {
541 PyObject_HEAD
542 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
543 int si_used;
544 int si_pos;
545 long len;
546} setiterobject;
547
548static PyObject *
549set_iter(PySetObject *so)
550{
551 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
552 if (si == NULL)
553 return NULL;
554 Py_INCREF(so);
555 si->si_set = so;
556 si->si_used = so->used;
557 si->si_pos = 0;
558 si->len = so->used;
559 return (PyObject *)si;
560}
561
562static void
563setiter_dealloc(setiterobject *si)
564{
565 Py_XDECREF(si->si_set);
566 PyObject_Del(si);
567}
568
569static int
570setiter_len(setiterobject *si)
571{
572 if (si->si_set != NULL && si->si_used == si->si_set->used)
573 return si->len;
574 return 0;
575}
576
577static PySequenceMethods setiter_as_sequence = {
578 (inquiry)setiter_len, /* sq_length */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000579};
580
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000581static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000582{
583 PyObject *key;
584 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000585 register setentry *entry;
586 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000587
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000588 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000589 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000590 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000591
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000592 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593 PyErr_SetString(PyExc_RuntimeError,
594 "Set changed size during iteration");
595 si->si_used = -1; /* Make this state sticky */
596 return NULL;
597 }
598
599 i = si->si_pos;
600 if (i < 0)
601 goto fail;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000602 entry = so->table;
603 mask = so->mask;
604 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000605 i++;
606 si->si_pos = i+1;
607 if (i > mask)
608 goto fail;
609 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000610 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000611 Py_INCREF(key);
612 return key;
613
614fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000615 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000616 si->si_set = NULL;
617 return NULL;
618}
619
Hye-Shik Change2956762005-08-01 05:26:41 +0000620static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621 PyObject_HEAD_INIT(&PyType_Type)
622 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000623 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000624 sizeof(setiterobject), /* tp_basicsize */
625 0, /* tp_itemsize */
626 /* methods */
627 (destructor)setiter_dealloc, /* tp_dealloc */
628 0, /* tp_print */
629 0, /* tp_getattr */
630 0, /* tp_setattr */
631 0, /* tp_compare */
632 0, /* tp_repr */
633 0, /* tp_as_number */
634 &setiter_as_sequence, /* tp_as_sequence */
635 0, /* tp_as_mapping */
636 0, /* tp_hash */
637 0, /* tp_call */
638 0, /* tp_str */
639 PyObject_GenericGetAttr, /* tp_getattro */
640 0, /* tp_setattro */
641 0, /* tp_as_buffer */
642 Py_TPFLAGS_DEFAULT, /* tp_flags */
643 0, /* tp_doc */
644 0, /* tp_traverse */
645 0, /* tp_clear */
646 0, /* tp_richcompare */
647 0, /* tp_weaklistoffset */
648 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000649 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000650};
651
Raymond Hettingerd7946662005-08-01 21:39:29 +0000652static int
653set_len(PyObject *so)
654{
655 return ((PySetObject *)so)->used;
656}
657
658static int
659set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000660{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000661 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000662
Raymond Hettingerd7946662005-08-01 21:39:29 +0000663 if (PyAnySet_Check(other))
664 return set_merge_internal(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000665
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000666 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000667 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000669 while (PyDict_Next(other, &pos, &key, &value)) {
670 if (set_add_internal(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000671 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000672 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000673 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000674 }
675
Raymond Hettingera38123e2003-11-24 22:18:49 +0000676 it = PyObject_GetIter(other);
677 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000678 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000679
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000680 while ((key = PyIter_Next(it)) != NULL) {
681 if (set_add_internal(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000682 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000683 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000684 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000685 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000686 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000687 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000688 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000689 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000690 return -1;
691 return 0;
692}
693
694static PyObject *
695set_update(PySetObject *so, PyObject *other)
696{
697 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000698 return NULL;
699 Py_RETURN_NONE;
700}
701
702PyDoc_STRVAR(update_doc,
703"Update a set with the union of itself and another.");
704
705static PyObject *
706make_new_set(PyTypeObject *type, PyObject *iterable)
707{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000708 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000709
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000710 if (dummy == NULL) { /* Auto-initialize dummy */
711 dummy = PyString_FromString("<dummy key>");
712 if (dummy == NULL)
713 return NULL;
714 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000715
716 /* create PySetObject structure */
717 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000718 if (so == NULL)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000719 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000720
721 EMPTY_TO_MINSIZE(so);
722 so->lookup = set_lookkey_string;
Raymond Hettingera690a992003-11-16 16:17:49 +0000723 so->hash = -1;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000724 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000725
Raymond Hettingera38123e2003-11-24 22:18:49 +0000726 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000727 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000728 Py_DECREF(so);
729 return NULL;
730 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000731 }
732
Raymond Hettingera690a992003-11-16 16:17:49 +0000733 return (PyObject *)so;
734}
735
Raymond Hettingerd7946662005-08-01 21:39:29 +0000736/* The empty frozenset is a singleton */
737static PyObject *emptyfrozenset = NULL;
738
Raymond Hettingera690a992003-11-16 16:17:49 +0000739static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000740frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000741{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000742 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000743
744 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
745 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000746
747 if (type != &PyFrozenSet_Type)
748 return make_new_set(type, iterable);
749
750 if (iterable != NULL) {
751 /* frozenset(f) is idempotent */
752 if (PyFrozenSet_CheckExact(iterable)) {
753 Py_INCREF(iterable);
754 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000755 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000756 result = make_new_set(type, iterable);
757 if (result == NULL || set_len(result))
758 return result;
759 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000760 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000761 /* The empty frozenset is a singleton */
762 if (emptyfrozenset == NULL)
763 emptyfrozenset = make_new_set(type, NULL);
764 Py_XINCREF(emptyfrozenset);
765 return emptyfrozenset;
766}
767
768void
769PySet_Fini(void)
770{
Raymond Hettingera9d99362005-08-05 00:01:15 +0000771 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000772 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000773}
774
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000775static PyObject *
776set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
777{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000778 return make_new_set(type, NULL);
779}
780
Raymond Hettingera690a992003-11-16 16:17:49 +0000781static void
782set_dealloc(PySetObject *so)
783{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000784 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000785 int fill = so->fill;
786
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000787 PyObject_GC_UnTrack(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788 Py_TRASHCAN_SAFE_BEGIN(so)
Raymond Hettinger691d8052004-05-30 07:26:47 +0000789 if (so->weakreflist != NULL)
790 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000792 for (entry = so->table; fill > 0; entry++) {
793 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000795 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796 }
797 }
798 if (so->table != so->smalltable)
799 PyMem_DEL(so->table);
800
Raymond Hettingera690a992003-11-16 16:17:49 +0000801 so->ob_type->tp_free(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000803}
804
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000805static int
806set_traverse(PySetObject *so, visitproc visit, void *arg)
807{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000808 int pos = 0;
809 PyObject *key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000811 while (set_next_internal(so, &pos, &key))
812 Py_VISIT(key);
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000813 return 0;
814}
815
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000816/* set_swap_bodies() switches the contents of any two sets by moving their
817 internal data pointers and, if needed, copying the internal smalltables.
818 Semantically equivalent to:
819
820 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
821
822 The function always succeeds and it leaves both objects in a stable state.
823 Useful for creating temporary frozensets from sets for membership testing
824 in __contains__(), discard(), and remove(). Also useful for operations
825 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000826 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000827*/
828
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829static void
830set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000831{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832 int t;
833 setentry *u;
834 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
835 setentry tab[PySet_MINSIZE];
836 long h;
837
838 t = a->fill; a->fill = b->fill; b->fill = t;
839 t = a->used; a->used = b->used; b->used = t;
840 t = a->mask; a->mask = b->mask; b->mask = t;
841
842 u = a->table;
843 if (a->table == a->smalltable)
844 u = b->smalltable;
845 a->table = b->table;
846 if (b->table == b->smalltable)
847 a->table = a->smalltable;
848 b->table = u;
849
850 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
851
852 if (a->table == a->smalltable || b->table == b->smalltable) {
853 memcpy(tab, a->smalltable, sizeof(tab));
854 memcpy(a->smalltable, b->smalltable, sizeof(tab));
855 memcpy(b->smalltable, tab, sizeof(tab));
856 }
857
Raymond Hettingera580c472005-08-05 17:19:54 +0000858 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
859 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
860 h = a->hash; a->hash = b->hash; b->hash = h;
861 } else {
862 a->hash = -1;
863 b->hash = -1;
864 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000865}
866
867static int
868set_contains(PySetObject *so, PyObject *key)
869{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000870 PyObject *tmpkey;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000871 int result;
872
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000873 result = set_contains_internal(so, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000874 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000875 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000876 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
877 if (tmpkey == NULL)
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000878 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000879 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
880 result = set_contains_internal(so, tmpkey);
881 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
882 Py_DECREF(tmpkey);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000883 }
884 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000885}
886
887static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000888set_direct_contains(PySetObject *so, PyObject *key)
889{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000890 long result;
891
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000892 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000893 if (result == -1)
894 return NULL;
895 return PyBool_FromLong(result);
896}
897
898PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
899
900static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000901set_copy(PySetObject *so)
902{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000903 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000904}
905
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000906static PyObject *
907frozenset_copy(PySetObject *so)
908{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000909 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000910 Py_INCREF(so);
911 return (PyObject *)so;
912 }
913 return set_copy(so);
914}
915
Raymond Hettingera690a992003-11-16 16:17:49 +0000916PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
917
918static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000919set_union(PySetObject *so, PyObject *other)
920{
921 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000922
923 result = (PySetObject *)set_copy(so);
924 if (result == NULL)
925 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000926 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000927 Py_DECREF(result);
928 return NULL;
929 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000930 return (PyObject *)result;
931}
932
933PyDoc_STRVAR(union_doc,
934 "Return the union of two sets as a new set.\n\
935\n\
936(i.e. all elements that are in either set.)");
937
938static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000939set_or(PySetObject *so, PyObject *other)
940{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000941 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000942 Py_INCREF(Py_NotImplemented);
943 return Py_NotImplemented;
944 }
945 return set_union(so, other);
946}
947
948static PyObject *
949set_ior(PySetObject *so, PyObject *other)
950{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000951 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000952 Py_INCREF(Py_NotImplemented);
953 return Py_NotImplemented;
954 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000955 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +0000956 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000957 Py_INCREF(so);
958 return (PyObject *)so;
959}
960
961static PyObject *
962set_intersection(PySetObject *so, PyObject *other)
963{
964 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000965 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000966
967 result = (PySetObject *)make_new_set(so->ob_type, NULL);
968 if (result == NULL)
969 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000970
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000971 if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) {
972 tmp = (PyObject *)so;
973 so = (PySetObject *)other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000974 other = tmp;
975 }
976
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000977 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000978 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000979 while (set_next_internal((PySetObject *)other, &pos, &key)) {
980 if (set_contains_internal(so, key)) {
981 if (set_add_internal(result, key) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000982 Py_DECREF(result);
983 return NULL;
984 }
985 }
986 }
987 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000988 }
989
Raymond Hettingera690a992003-11-16 16:17:49 +0000990 it = PyObject_GetIter(other);
991 if (it == NULL) {
992 Py_DECREF(result);
993 return NULL;
994 }
995
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000996 while ((key = PyIter_Next(it)) != NULL) {
997 if (set_contains_internal(so, key)) {
998 if (set_add_internal(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000999 Py_DECREF(it);
1000 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001001 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001002 return NULL;
1003 }
1004 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001005 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001006 }
1007 Py_DECREF(it);
1008 if (PyErr_Occurred()) {
1009 Py_DECREF(result);
1010 return NULL;
1011 }
1012 return (PyObject *)result;
1013}
1014
1015PyDoc_STRVAR(intersection_doc,
1016"Return the intersection of two sets as a new set.\n\
1017\n\
1018(i.e. all elements that are in both sets.)");
1019
1020static PyObject *
1021set_intersection_update(PySetObject *so, PyObject *other)
1022{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001023 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001024
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001025 tmp = set_intersection(so, other);
1026 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001027 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001028 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001029 Py_DECREF(tmp);
1030 Py_RETURN_NONE;
1031}
1032
1033PyDoc_STRVAR(intersection_update_doc,
1034"Update a set with the intersection of itself and another.");
1035
1036static PyObject *
1037set_and(PySetObject *so, PyObject *other)
1038{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001039 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001040 Py_INCREF(Py_NotImplemented);
1041 return Py_NotImplemented;
1042 }
1043 return set_intersection(so, other);
1044}
1045
1046static PyObject *
1047set_iand(PySetObject *so, PyObject *other)
1048{
1049 PyObject *result;
1050
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001051 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001052 Py_INCREF(Py_NotImplemented);
1053 return Py_NotImplemented;
1054 }
1055 result = set_intersection_update(so, other);
1056 if (result == NULL)
1057 return NULL;
1058 Py_DECREF(result);
1059 Py_INCREF(so);
1060 return (PyObject *)so;
1061}
1062
1063static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001064set_difference_update(PySetObject *so, PyObject *other)
1065{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001066 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001067
1068 it = PyObject_GetIter(other);
1069 if (it == NULL)
1070 return NULL;
1071
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001072 while ((key = PyIter_Next(it)) != NULL) {
1073 if (set_discard_internal(so, key) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001074 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001075 Py_DECREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001076 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001077 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001078 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001079 }
1080 Py_DECREF(it);
1081 if (PyErr_Occurred())
1082 return NULL;
1083 Py_RETURN_NONE;
1084}
1085
1086PyDoc_STRVAR(difference_update_doc,
1087"Remove all elements of another set from this set.");
1088
1089static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001090set_difference(PySetObject *so, PyObject *other)
1091{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001092 PyObject *tmp, *key, *result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001093 int pos = 0;
1094
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001095 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001096 result = set_copy(so);
1097 if (result == NULL)
1098 return result;
1099 tmp = set_difference_update((PySetObject *)result, other);
1100 if (tmp != NULL) {
1101 Py_DECREF(tmp);
1102 return result;
1103 }
1104 Py_DECREF(result);
1105 return NULL;
1106 }
1107
1108 result = make_new_set(so->ob_type, NULL);
1109 if (result == NULL)
1110 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001111
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001112 if (PyDict_Check(other)) {
1113 while (set_next_internal(so, &pos, &key)) {
1114 if (!PyDict_Contains(other, key)) {
1115 if (set_add_internal((PySetObject *)result, key) == -1)
1116 return NULL;
1117 }
1118 }
1119 return result;
1120 }
1121
1122 while (set_next_internal(so, &pos, &key)) {
1123 if (!set_contains_internal((PySetObject *)other, key)) {
1124 if (set_add_internal((PySetObject *)result, key) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001125 return NULL;
1126 }
1127 }
1128 return result;
1129}
1130
1131PyDoc_STRVAR(difference_doc,
1132"Return the difference of two sets as a new set.\n\
1133\n\
1134(i.e. all elements that are in this set but not the other.)");
1135static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001136set_sub(PySetObject *so, PyObject *other)
1137{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001138 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001139 Py_INCREF(Py_NotImplemented);
1140 return Py_NotImplemented;
1141 }
1142 return set_difference(so, other);
1143}
1144
1145static PyObject *
1146set_isub(PySetObject *so, PyObject *other)
1147{
1148 PyObject *result;
1149
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001150 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001151 Py_INCREF(Py_NotImplemented);
1152 return Py_NotImplemented;
1153 }
1154 result = set_difference_update(so, other);
1155 if (result == NULL)
1156 return NULL;
1157 Py_DECREF(result);
1158 Py_INCREF(so);
1159 return (PyObject *)so;
1160}
1161
1162static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001163set_symmetric_difference_update(PySetObject *so, PyObject *other)
1164{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001165 PySetObject *otherset;
1166 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001167 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001168
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001169 if (PyDict_Check(other)) {
1170 PyObject *value;
1171 int rv;
1172 while (PyDict_Next(other, &pos, &key, &value)) {
1173 rv = set_discard_internal(so, key);
1174 if (rv == -1)
1175 return NULL;
1176 if (rv == DISCARD_NOTFOUND) {
1177 if (set_add_internal(so, key) == -1)
1178 return NULL;
1179 }
1180 }
1181 Py_RETURN_NONE;
1182 }
1183
1184 if (PyAnySet_Check(other)) {
1185 Py_INCREF(other);
1186 otherset = (PySetObject *)other;
1187 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001188 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1189 if (otherset == NULL)
1190 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001191 }
1192
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001193 while (set_next_internal(otherset, &pos, &key)) {
1194 int rv = set_discard_internal(so, key);
1195 if (rv == -1) {
1196 Py_XDECREF(otherset);
1197 return NULL;
1198 }
1199 if (rv == DISCARD_NOTFOUND) {
1200 if (set_add_internal(so, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001201 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001202 return NULL;
1203 }
1204 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001205 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001206 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001207 Py_RETURN_NONE;
1208}
1209
1210PyDoc_STRVAR(symmetric_difference_update_doc,
1211"Update a set with the symmetric difference of itself and another.");
1212
1213static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001214set_symmetric_difference(PySetObject *so, PyObject *other)
1215{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001216 PyObject *rv;
1217 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001218
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001219 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1220 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001221 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001222 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1223 if (rv == NULL)
1224 return NULL;
1225 Py_DECREF(rv);
1226 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001227}
1228
1229PyDoc_STRVAR(symmetric_difference_doc,
1230"Return the symmetric difference of two sets as a new set.\n\
1231\n\
1232(i.e. all elements that are in exactly one of the sets.)");
1233
1234static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001235set_xor(PySetObject *so, PyObject *other)
1236{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001237 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001238 Py_INCREF(Py_NotImplemented);
1239 return Py_NotImplemented;
1240 }
1241 return set_symmetric_difference(so, other);
1242}
1243
1244static PyObject *
1245set_ixor(PySetObject *so, PyObject *other)
1246{
1247 PyObject *result;
1248
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001249 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001250 Py_INCREF(Py_NotImplemented);
1251 return Py_NotImplemented;
1252 }
1253 result = set_symmetric_difference_update(so, other);
1254 if (result == NULL)
1255 return NULL;
1256 Py_DECREF(result);
1257 Py_INCREF(so);
1258 return (PyObject *)so;
1259}
1260
1261static PyObject *
1262set_issubset(PySetObject *so, PyObject *other)
1263{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001264 PyObject *tmp, *result;
1265 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001266 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001267
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001268 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001269 tmp = make_new_set(&PySet_Type, other);
1270 if (tmp == NULL)
1271 return NULL;
1272 result = set_issubset(so, tmp);
1273 Py_DECREF(tmp);
1274 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001275 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001276 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001277 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001278
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001279 while (set_next_internal(so, &pos, &key)) {
1280 if (!set_contains_internal((PySetObject *)other, key))
Raymond Hettingera690a992003-11-16 16:17:49 +00001281 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001282 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001283 Py_RETURN_TRUE;
1284}
1285
1286PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1287
1288static PyObject *
1289set_issuperset(PySetObject *so, PyObject *other)
1290{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001291 PyObject *tmp, *result;
1292
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001293 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001294 tmp = make_new_set(&PySet_Type, other);
1295 if (tmp == NULL)
1296 return NULL;
1297 result = set_issuperset(so, tmp);
1298 Py_DECREF(tmp);
1299 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001300 }
1301 return set_issubset((PySetObject *)other, (PyObject *)so);
1302}
1303
1304PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1305
1306static long
1307set_nohash(PyObject *self)
1308{
1309 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1310 return -1;
1311}
1312
1313static int
1314set_nocmp(PyObject *self)
1315{
1316 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1317 return -1;
1318}
1319
1320static long
1321frozenset_hash(PyObject *self)
1322{
Raymond Hettingera690a992003-11-16 16:17:49 +00001323 PySetObject *so = (PySetObject *)self;
Raymond Hettingera580c472005-08-05 17:19:54 +00001324 long h, hash = 1927868237L;
1325 setentry *entry;
1326 int i;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001327
Raymond Hettingera690a992003-11-16 16:17:49 +00001328 if (so->hash != -1)
1329 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001330
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001331 hash *= set_len(self) + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +00001332 entry = &so->table[0];
1333 for (i=so->used ; i ; entry++, i--) {
1334 while (entry->key == NULL || entry->key==dummy)
1335 entry++;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001336 /* Work to increase the bit dispersion for closely spaced hash
1337 values. The is important because some use cases have many
1338 combinations of a small number of elements with nearby
1339 hashes so that many distinct combinations collapse to only
1340 a handful of distinct hash values. */
Raymond Hettingera9d99362005-08-05 00:01:15 +00001341 h = entry->hash;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001342 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001343 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001344 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001345 if (hash == -1)
1346 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001347 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001348 return hash;
1349}
1350
1351static PyObject *
1352set_richcompare(PySetObject *v, PyObject *w, int op)
1353{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001354 PyObject *r1, *r2;
1355
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001356 if(!PyAnySet_Check(w)) {
1357 if (op == Py_EQ)
1358 Py_RETURN_FALSE;
1359 if (op == Py_NE)
1360 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001361 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1362 return NULL;
1363 }
1364 switch (op) {
1365 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001366 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001367 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001368 return set_issubset(v, w);
1369 case Py_NE:
1370 if (set_len((PyObject *)v) != set_len(w))
1371 Py_RETURN_TRUE;
1372 r1 = set_issubset(v, w);
1373 assert (r1 != NULL);
1374 r2 = PyBool_FromLong(PyObject_Not(r1));
1375 Py_DECREF(r1);
1376 return r2;
1377 case Py_LE:
1378 return set_issubset(v, w);
1379 case Py_GE:
1380 return set_issuperset(v, w);
1381 case Py_LT:
1382 if (set_len((PyObject *)v) >= set_len(w))
1383 Py_RETURN_FALSE;
1384 return set_issubset(v, w);
1385 case Py_GT:
1386 if (set_len((PyObject *)v) <= set_len(w))
1387 Py_RETURN_FALSE;
1388 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001389 }
1390 Py_INCREF(Py_NotImplemented);
1391 return Py_NotImplemented;
1392}
1393
1394static PyObject *
1395set_repr(PySetObject *so)
1396{
1397 PyObject *keys, *result, *listrepr;
1398
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001399 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001400 if (keys == NULL)
1401 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001402 listrepr = PyObject_Repr(keys);
1403 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001404 if (listrepr == NULL)
1405 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001406
1407 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1408 PyString_AS_STRING(listrepr));
1409 Py_DECREF(listrepr);
1410 return result;
1411}
1412
1413static int
1414set_tp_print(PySetObject *so, FILE *fp, int flags)
1415{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001416 PyObject *key;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001417 int pos=0;
1418 char *emit = ""; /* No separator emitted on first pass */
1419 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001420
Raymond Hettingera690a992003-11-16 16:17:49 +00001421 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001422 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001423 fputs(emit, fp);
1424 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001425 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001426 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001427 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001428 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001429 return 0;
1430}
1431
1432static PyObject *
1433set_clear(PySetObject *so)
1434{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001435 set_clear_internal(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001436 so->hash = -1;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001437 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001438}
1439
1440PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1441
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001442static int
1443set_tp_clear(PySetObject *so)
1444{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001445 set_clear_internal(so);
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001446 so->hash = -1;
1447 return 0;
1448}
1449
Raymond Hettingera690a992003-11-16 16:17:49 +00001450static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001451set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001452{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001453 if (set_add_internal(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001454 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001455 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001456}
1457
1458PyDoc_STRVAR(add_doc,
1459"Add an element to a set.\n\
1460\n\
1461This has no effect if the element is already present.");
1462
1463static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001464set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001465{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001466 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001467 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001468
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001469 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1470 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1471 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001472 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001473 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1474 result = set_remove(so, tmpkey);
1475 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1476 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001477 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001478 }
Raymond Hettinger0deab622003-12-13 18:53:18 +00001479
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001480 rv = set_discard_internal(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001481 if (rv == -1)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001482 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001483 else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001484 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001485 return NULL;
1486 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001487 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001488}
1489
1490PyDoc_STRVAR(remove_doc,
1491"Remove an element from a set; it must be a member.\n\
1492\n\
1493If the element is not a member, raise a KeyError.");
1494
1495static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001496set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001497{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001498 PyObject *tmpkey, *result;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001499
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001500 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1501 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1502 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001503 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001504 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1505 result = set_discard(so, tmpkey);
1506 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1507 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001508 return result;
1509 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001510
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001511 if (set_discard_internal(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001512 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001513 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001514}
1515
1516PyDoc_STRVAR(discard_doc,
1517"Remove an element from a set if it is a member.\n\
1518\n\
1519If the element is not a member, do nothing.");
1520
1521static PyObject *
1522set_pop(PySetObject *so)
1523{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001524 PyObject *key;
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001525 register setentry *entry;
1526 register int i = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001527
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001528 assert (PyAnySet_Check(so));
1529 if (so->used == 0) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001530 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1531 return NULL;
1532 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001533
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001534 /* Set entry to "the first" unused or dummy set entry. We abuse
1535 * the hash field of slot 0 to hold a search finger:
1536 * If slot 0 has a value, use slot 0.
1537 * Else slot 0 is being used to hold a search finger,
1538 * and we use its hash value as the first index to look.
1539 */
1540 entry = &so->table[0];
1541 if (entry->key == NULL || entry->key == dummy) {
1542 i = (int)entry->hash;
1543 /* The hash field may be a real hash value, or it may be a
1544 * legit search finger, or it may be a once-legit search
1545 * finger that's out of bounds now because it wrapped around
1546 * or the table shrunk -- simply make sure it's in bounds now.
1547 */
1548 if (i > so->mask || i < 1)
1549 i = 1; /* skip slot 0 */
1550 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
1551 i++;
1552 if (i > so->mask)
1553 i = 1;
1554 }
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001555 }
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001556 key = entry->key;
1557 Py_INCREF(dummy);
1558 entry->key = dummy;
1559 so->used--;
1560 so->table[0].hash = i + 1; /* next place to start */
Raymond Hettingera690a992003-11-16 16:17:49 +00001561 return key;
1562}
1563
1564PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1565
1566static PyObject *
1567set_reduce(PySetObject *so)
1568{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001569 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001570
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001571 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001572 if (keys == NULL)
1573 goto done;
1574 args = PyTuple_Pack(1, keys);
1575 if (args == NULL)
1576 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001577 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1578 if (dict == NULL) {
1579 PyErr_Clear();
1580 dict = Py_None;
1581 Py_INCREF(dict);
1582 }
1583 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001584done:
1585 Py_XDECREF(args);
1586 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001587 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001588 return result;
1589}
1590
1591PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1592
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001593static int
1594set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1595{
1596 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001597
1598 if (!PyAnySet_Check(self))
1599 return -1;
1600 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1601 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001602 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001603 self->hash = -1;
1604 if (iterable == NULL)
1605 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001606 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001607}
1608
Raymond Hettingera690a992003-11-16 16:17:49 +00001609static PySequenceMethods set_as_sequence = {
1610 (inquiry)set_len, /* sq_length */
1611 0, /* sq_concat */
1612 0, /* sq_repeat */
1613 0, /* sq_item */
1614 0, /* sq_slice */
1615 0, /* sq_ass_item */
1616 0, /* sq_ass_slice */
1617 (objobjproc)set_contains, /* sq_contains */
1618};
1619
1620/* set object ********************************************************/
1621
1622static PyMethodDef set_methods[] = {
1623 {"add", (PyCFunction)set_add, METH_O,
1624 add_doc},
1625 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1626 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001627 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001628 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001629 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1630 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001631 {"discard", (PyCFunction)set_discard, METH_O,
1632 discard_doc},
1633 {"difference", (PyCFunction)set_difference, METH_O,
1634 difference_doc},
1635 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1636 difference_update_doc},
1637 {"intersection",(PyCFunction)set_intersection, METH_O,
1638 intersection_doc},
1639 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1640 intersection_update_doc},
1641 {"issubset", (PyCFunction)set_issubset, METH_O,
1642 issubset_doc},
1643 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1644 issuperset_doc},
1645 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1646 pop_doc},
1647 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1648 reduce_doc},
1649 {"remove", (PyCFunction)set_remove, METH_O,
1650 remove_doc},
1651 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1652 symmetric_difference_doc},
1653 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1654 symmetric_difference_update_doc},
1655 {"union", (PyCFunction)set_union, METH_O,
1656 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001657 {"update", (PyCFunction)set_update, METH_O,
1658 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001659 {NULL, NULL} /* sentinel */
1660};
1661
1662static PyNumberMethods set_as_number = {
1663 0, /*nb_add*/
1664 (binaryfunc)set_sub, /*nb_subtract*/
1665 0, /*nb_multiply*/
1666 0, /*nb_divide*/
1667 0, /*nb_remainder*/
1668 0, /*nb_divmod*/
1669 0, /*nb_power*/
1670 0, /*nb_negative*/
1671 0, /*nb_positive*/
1672 0, /*nb_absolute*/
1673 0, /*nb_nonzero*/
1674 0, /*nb_invert*/
1675 0, /*nb_lshift*/
1676 0, /*nb_rshift*/
1677 (binaryfunc)set_and, /*nb_and*/
1678 (binaryfunc)set_xor, /*nb_xor*/
1679 (binaryfunc)set_or, /*nb_or*/
1680 0, /*nb_coerce*/
1681 0, /*nb_int*/
1682 0, /*nb_long*/
1683 0, /*nb_float*/
1684 0, /*nb_oct*/
1685 0, /*nb_hex*/
1686 0, /*nb_inplace_add*/
1687 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1688 0, /*nb_inplace_multiply*/
1689 0, /*nb_inplace_divide*/
1690 0, /*nb_inplace_remainder*/
1691 0, /*nb_inplace_power*/
1692 0, /*nb_inplace_lshift*/
1693 0, /*nb_inplace_rshift*/
1694 (binaryfunc)set_iand, /*nb_inplace_and*/
1695 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1696 (binaryfunc)set_ior, /*nb_inplace_or*/
1697};
1698
1699PyDoc_STRVAR(set_doc,
1700"set(iterable) --> set object\n\
1701\n\
1702Build an unordered collection.");
1703
1704PyTypeObject PySet_Type = {
1705 PyObject_HEAD_INIT(&PyType_Type)
1706 0, /* ob_size */
1707 "set", /* tp_name */
1708 sizeof(PySetObject), /* tp_basicsize */
1709 0, /* tp_itemsize */
1710 /* methods */
1711 (destructor)set_dealloc, /* tp_dealloc */
1712 (printfunc)set_tp_print, /* tp_print */
1713 0, /* tp_getattr */
1714 0, /* tp_setattr */
1715 (cmpfunc)set_nocmp, /* tp_compare */
1716 (reprfunc)set_repr, /* tp_repr */
1717 &set_as_number, /* tp_as_number */
1718 &set_as_sequence, /* tp_as_sequence */
1719 0, /* tp_as_mapping */
1720 set_nohash, /* tp_hash */
1721 0, /* tp_call */
1722 0, /* tp_str */
1723 PyObject_GenericGetAttr, /* tp_getattro */
1724 0, /* tp_setattro */
1725 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001726 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001727 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001728 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001729 (traverseproc)set_traverse, /* tp_traverse */
1730 (inquiry)set_tp_clear, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001731 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001732 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001733 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001734 0, /* tp_iternext */
1735 set_methods, /* tp_methods */
1736 0, /* tp_members */
1737 0, /* tp_getset */
1738 0, /* tp_base */
1739 0, /* tp_dict */
1740 0, /* tp_descr_get */
1741 0, /* tp_descr_set */
1742 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001743 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001744 PyType_GenericAlloc, /* tp_alloc */
1745 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001746 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001747};
1748
1749/* frozenset object ********************************************************/
1750
1751
1752static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001753 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001754 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001755 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001756 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001757 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001758 difference_doc},
1759 {"intersection",(PyCFunction)set_intersection, METH_O,
1760 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001761 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001762 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001763 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001764 issuperset_doc},
1765 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1766 reduce_doc},
1767 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1768 symmetric_difference_doc},
1769 {"union", (PyCFunction)set_union, METH_O,
1770 union_doc},
1771 {NULL, NULL} /* sentinel */
1772};
1773
1774static PyNumberMethods frozenset_as_number = {
1775 0, /*nb_add*/
1776 (binaryfunc)set_sub, /*nb_subtract*/
1777 0, /*nb_multiply*/
1778 0, /*nb_divide*/
1779 0, /*nb_remainder*/
1780 0, /*nb_divmod*/
1781 0, /*nb_power*/
1782 0, /*nb_negative*/
1783 0, /*nb_positive*/
1784 0, /*nb_absolute*/
1785 0, /*nb_nonzero*/
1786 0, /*nb_invert*/
1787 0, /*nb_lshift*/
1788 0, /*nb_rshift*/
1789 (binaryfunc)set_and, /*nb_and*/
1790 (binaryfunc)set_xor, /*nb_xor*/
1791 (binaryfunc)set_or, /*nb_or*/
1792};
1793
1794PyDoc_STRVAR(frozenset_doc,
1795"frozenset(iterable) --> frozenset object\n\
1796\n\
1797Build an immutable unordered collection.");
1798
1799PyTypeObject PyFrozenSet_Type = {
1800 PyObject_HEAD_INIT(&PyType_Type)
1801 0, /* ob_size */
1802 "frozenset", /* tp_name */
1803 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001804 0, /* tp_itemsize */
1805 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001806 (destructor)set_dealloc, /* tp_dealloc */
1807 (printfunc)set_tp_print, /* tp_print */
1808 0, /* tp_getattr */
1809 0, /* tp_setattr */
1810 (cmpfunc)set_nocmp, /* tp_compare */
1811 (reprfunc)set_repr, /* tp_repr */
1812 &frozenset_as_number, /* tp_as_number */
1813 &set_as_sequence, /* tp_as_sequence */
1814 0, /* tp_as_mapping */
1815 frozenset_hash, /* tp_hash */
1816 0, /* tp_call */
1817 0, /* tp_str */
1818 PyObject_GenericGetAttr, /* tp_getattro */
1819 0, /* tp_setattro */
1820 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001821 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001822 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001823 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001824 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingera690a992003-11-16 16:17:49 +00001825 0, /* tp_clear */
1826 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001827 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001828 (getiterfunc)set_iter, /* tp_iter */
1829 0, /* tp_iternext */
1830 frozenset_methods, /* tp_methods */
1831 0, /* tp_members */
1832 0, /* tp_getset */
1833 0, /* tp_base */
1834 0, /* tp_dict */
1835 0, /* tp_descr_get */
1836 0, /* tp_descr_set */
1837 0, /* tp_dictoffset */
1838 0, /* tp_init */
1839 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001840 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001841 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001842};