blob: 771d512b580c34fdef4ea4545c78091ff17fdf86 [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;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052 register int cmp;
53 PyObject *err_type, *err_value, *err_tb;
54 PyObject *startkey;
55
56 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000057 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000058 if (entry->key == NULL || entry->key == key)
59 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060
Raymond Hettinger99220fa2005-08-06 18:57:13 +000061 restore_error = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000062 if (entry->key == dummy)
63 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000064 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000065 if (entry->hash == hash) {
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +000066 if (_PyErr_OCCURRED()) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000067 restore_error = 1;
68 PyErr_Fetch(&err_type, &err_value, &err_tb);
69 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000070 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000071 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
72 if (cmp < 0)
73 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +000074 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075 if (cmp > 0)
76 goto Done;
77 }
78 else {
79 /* The compare did major nasty stuff to the
80 * set: start over.
81 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000082 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000083 goto Done;
84 }
85 }
86 freeslot = NULL;
87 }
88
89 /* In the loop, key == dummy is by far (factor of 100s) the
90 least likely outcome, so test for that last. */
91 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
92 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +000093 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000095 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000096 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000097 break;
98 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000099 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000100 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000101 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger99220fa2005-08-06 18:57:13 +0000102 if (_PyErr_OCCURRED()) {
103 restore_error = 1;
104 PyErr_Fetch(&err_type, &err_value,
105 &err_tb);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000106 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000107 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000108 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
109 if (cmp < 0)
110 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +0000111 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000112 if (cmp > 0)
113 break;
114 }
115 else {
116 /* The compare did major nasty stuff to the
117 * set: start over.
118 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000119 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 break;
121 }
122 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000123 else if (entry->key == dummy && freeslot == NULL)
124 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000125 }
126
127Done:
128 if (restore_error)
129 PyErr_Restore(err_type, err_value, err_tb);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000130 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000131}
132
133/*
134 * Hacked up version of set_lookkey which can assume keys are always strings;
135 * this assumption allows testing for errors during PyObject_Compare() to
136 * be dropped; string-string comparisons never raise exceptions. This also
137 * means we don't need to go through PyObject_Compare(); we can always use
138 * _PyString_Eq directly.
139 *
140 * This is valuable because the general-case error handling in set_lookkey() is
141 * expensive, and sets with pure-string keys may be very common.
142 */
143static setentry *
144set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
145{
146 register int i;
147 register unsigned int perturb;
148 register setentry *freeslot;
149 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000150 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000151 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152
153 /* Make sure this function doesn't have to handle non-string keys,
154 including subclasses of str; e.g., one reason to subclass
155 strings is to override __eq__, and for speed we don't cater to
156 that here. */
157 if (!PyString_CheckExact(key)) {
158 so->lookup = set_lookkey;
159 return set_lookkey(so, key, hash);
160 }
161 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000162 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000163 if (entry->key == NULL || entry->key == key)
164 return entry;
Raymond Hettingera580c472005-08-05 17:19:54 +0000165 if (so->fill != so->used) {
166 if (entry->key == dummy)
167 freeslot = entry;
168 else {
169 if (entry->hash == hash && _PyString_Eq(entry->key, key))
170 return entry;
171 freeslot = NULL;
172 }
173
174 /* In the loop, key == dummy is by far (factor of 100s) the
175 least likely outcome, so test for that last. */
176 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
177 i = (i << 2) + i + perturb + 1;
178 entry = &table[i & mask];
179 if (entry->key == NULL)
180 return freeslot == NULL ? entry : freeslot;
181 if (entry->key == key
182 || (entry->hash == hash
183 && entry->key != dummy
184 && _PyString_Eq(entry->key, key)))
185 return entry;
186 if (entry->key == dummy && freeslot == NULL)
187 freeslot = entry;
188 }
189 } else {
190 /* Simplified loop that can assume are no dummy entries */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000191 if (entry->hash == hash && _PyString_Eq(entry->key, key))
192 return entry;
Raymond Hettingera580c472005-08-05 17:19:54 +0000193 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
194 i = (i << 2) + i + perturb + 1;
195 entry = &table[i & mask];
196 if (entry->key == NULL)
197 return entry;
198 if (entry->key == key
199 || (entry->hash == hash
200 && _PyString_Eq(entry->key, key)))
201 return entry;
202 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000203 }
204}
205
206/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000207Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208Used both by the internal resize routine and by the public insert routine.
209Eats a reference to key.
210*/
211static void
212set_insert_key(register PySetObject *so, PyObject *key, long hash)
213{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000214 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000215 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
216
217 assert(so->lookup != NULL);
218
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000219 entry = so->lookup(so, key, hash);
220 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000221 /* UNUSED */
222 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 entry->key = key;
224 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000225 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000226 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000227 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000228 entry->key = key;
229 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000230 so->used++;
231 Py_DECREF(dummy);
232 } else {
233 /* ACTIVE */
234 Py_DECREF(key);
235 }
236}
237
238/*
239Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000240keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000241actually be smaller than the old one.
242*/
243static int
244set_table_resize(PySetObject *so, int minused)
245{
246 int newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000247 setentry *oldtable, *newtable, *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000248 int i;
249 int is_oldtable_malloced;
250 setentry small_copy[PySet_MINSIZE];
251
252 assert(minused >= 0);
253
254 /* Find the smallest table size > minused. */
255 for (newsize = PySet_MINSIZE;
256 newsize <= minused && newsize > 0;
257 newsize <<= 1)
258 ;
259 if (newsize <= 0) {
260 PyErr_NoMemory();
261 return -1;
262 }
263
264 /* Get space for a new table. */
265 oldtable = so->table;
266 assert(oldtable != NULL);
267 is_oldtable_malloced = oldtable != so->smalltable;
268
269 if (newsize == PySet_MINSIZE) {
270 /* A large table is shrinking, or we can't get any smaller. */
271 newtable = so->smalltable;
272 if (newtable == oldtable) {
273 if (so->fill == so->used) {
274 /* No dummies, so no point doing anything. */
275 return 0;
276 }
277 /* We're not going to resize it, but rebuild the
278 table anyway to purge old dummy entries.
279 Subtle: This is *necessary* if fill==size,
280 as set_lookkey needs at least one virgin slot to
281 terminate failing searches. If fill < size, it's
282 merely desirable, as dummies slow searches. */
283 assert(so->fill > so->used);
284 memcpy(small_copy, oldtable, sizeof(small_copy));
285 oldtable = small_copy;
286 }
287 }
288 else {
289 newtable = PyMem_NEW(setentry, newsize);
290 if (newtable == NULL) {
291 PyErr_NoMemory();
292 return -1;
293 }
294 }
295
296 /* Make the set empty, using the new table. */
297 assert(newtable != oldtable);
298 so->table = newtable;
299 so->mask = newsize - 1;
300 memset(newtable, 0, sizeof(setentry) * newsize);
301 so->used = 0;
302 i = so->fill;
303 so->fill = 0;
304
305 /* Copy the data over; this is refcount-neutral for active entries;
306 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000307 for (entry = oldtable; i > 0; entry++) {
308 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000309 /* UNUSED */
310 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000311 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000312 /* DUMMY */
313 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000314 assert(entry->key == dummy);
315 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000316 } else {
317 /* ACTIVE */
318 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000319 set_insert_key(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000320 }
321 }
322
323 if (is_oldtable_malloced)
324 PyMem_DEL(oldtable);
325 return 0;
326}
327
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000328/* CAUTION: set_add_internal() must guarantee that it won't resize the table */
329static int
330set_add_internal(register PySetObject *so, PyObject *key)
331{
332 register long hash;
333 register int n_used;
334
335 if (PyString_CheckExact(key)) {
336 hash = ((PyStringObject *)key)->ob_shash;
337 if (hash == -1)
338 hash = PyObject_Hash(key);
339 } else {
340 hash = PyObject_Hash(key);
341 if (hash == -1)
342 return -1;
343 }
344 assert(so->fill <= so->mask); /* at least one empty slot */
345 n_used = so->used;
346 Py_INCREF(key);
347 set_insert_key(so, key, hash);
348 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
349 return 0;
350 return set_table_resize(so, so->used*(so->used>50000 ? 2 : 4));
351}
352
353#define DISCARD_NOTFOUND 0
354#define DISCARD_FOUND 1
355
356static int
357set_discard_internal(PySetObject *so, PyObject *key)
358{
359 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000360 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000361 PyObject *old_key;
362
363 assert (PyAnySet_Check(so));
364 if (!PyString_CheckExact(key) ||
365 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
366 hash = PyObject_Hash(key);
367 if (hash == -1)
368 return -1;
369 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000370 entry = (so->lookup)(so, key, hash);
371 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000372 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000373 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000374 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000375 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376 so->used--;
377 Py_DECREF(old_key);
378 return DISCARD_FOUND;
379}
380
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000381static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382set_clear_internal(PySetObject *so)
383{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000384 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000385 int table_is_malloced;
386 int fill;
387 setentry small_copy[PySet_MINSIZE];
388#ifdef Py_DEBUG
389 int i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000390 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000391
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000392 n = so->mask + 1;
393 i = 0;
394#endif
395
396 table = so->table;
397 assert(table != NULL);
398 table_is_malloced = table != so->smalltable;
399
400 /* This is delicate. During the process of clearing the set,
401 * decrefs can cause the set to mutate. To avoid fatal confusion
402 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000403 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000404 * clearing.
405 */
406 fill = so->fill;
407 if (table_is_malloced)
408 EMPTY_TO_MINSIZE(so);
409
410 else if (fill > 0) {
411 /* It's a small table with something that needs to be cleared.
412 * Afraid the only safe way is to copy the set entries into
413 * another small table first.
414 */
415 memcpy(small_copy, table, sizeof(small_copy));
416 table = small_copy;
417 EMPTY_TO_MINSIZE(so);
418 }
419 /* else it's a small table that's already empty */
420
421 /* Now we can finally clear things. If C had refcounts, we could
422 * assert that the refcount on table is 1 now, i.e. that this function
423 * has unique access to it, so decref side-effects can't alter it.
424 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000425 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000426#ifdef Py_DEBUG
427 assert(i < n);
428 ++i;
429#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000430 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000432 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000433 }
434#ifdef Py_DEBUG
435 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437#endif
438 }
439
440 if (table_is_malloced)
441 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000442 so->hash = -1;
443 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000444}
445
446/*
447 * Iterate over a set table. Use like so:
448 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000449 * int pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450 * PyObject *key;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000451 * pos = 0; # important! pos should not otherwise be changed by you
452 * while (set_next_internal(yourset, &pos, &key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 * Refer to borrowed reference in key.
454 * }
455 *
456 * CAUTION: In general, it isn't safe to use set_next_internal in a loop that
457 * mutates the table.
458 */
459static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000460set_next_internal(PySetObject *so, int *pos, PyObject **key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000461{
462 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000463 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464
465 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000466 i = *pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467 if (i < 0)
468 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000469 entry = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470 mask = so->mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000471 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472 i++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000473 *pos = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000474 if (i > mask)
475 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000476 if (key)
477 *key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000478 return 1;
479}
480
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000481static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000482set_merge_internal(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000484 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000485 register int i;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000486 register setentry *entry, *othertable;
487 register int othermask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000488
489 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000490 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000492 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493 if (other == so || other->used == 0)
494 /* a.update(a) or a.update({}); nothing to do */
495 return 0;
496 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000497 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498 * that there will be no (or few) overlapping keys.
499 */
500 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
501 if (set_table_resize(so, (so->used + other->used)*2) != 0)
502 return -1;
503 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000504 othermask = other->mask;
505 othertable = other->table;
506 for (i = 0; i <= othermask; i++) {
507 entry = &othertable[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508 if (entry->key != NULL &&
509 entry->key != dummy) {
510 Py_INCREF(entry->key);
511 set_insert_key(so, entry->key, entry->hash);
512 }
513 }
514 return 0;
515}
516
517static int
518set_contains_internal(PySetObject *so, PyObject *key)
519{
520 long hash;
521
522 if (!PyString_CheckExact(key) ||
523 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
524 hash = PyObject_Hash(key);
525 if (hash == -1)
526 return -1;
527 }
528 key = (so->lookup)(so, key, hash)->key;
529 return key != NULL && key != dummy;
530}
531
Raymond Hettingera9d99362005-08-05 00:01:15 +0000532/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000533
Raymond Hettingerd7946662005-08-01 21:39:29 +0000534static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000535
536typedef struct {
537 PyObject_HEAD
538 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
539 int si_used;
540 int si_pos;
541 long len;
542} setiterobject;
543
544static PyObject *
545set_iter(PySetObject *so)
546{
547 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
548 if (si == NULL)
549 return NULL;
550 Py_INCREF(so);
551 si->si_set = so;
552 si->si_used = so->used;
553 si->si_pos = 0;
554 si->len = so->used;
555 return (PyObject *)si;
556}
557
558static void
559setiter_dealloc(setiterobject *si)
560{
561 Py_XDECREF(si->si_set);
562 PyObject_Del(si);
563}
564
565static int
566setiter_len(setiterobject *si)
567{
568 if (si->si_set != NULL && si->si_used == si->si_set->used)
569 return si->len;
570 return 0;
571}
572
573static PySequenceMethods setiter_as_sequence = {
574 (inquiry)setiter_len, /* sq_length */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000575};
576
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000577static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000578{
579 PyObject *key;
580 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000581 register setentry *entry;
582 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000583
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000584 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000585 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000586 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000587
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000588 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000589 PyErr_SetString(PyExc_RuntimeError,
590 "Set changed size during iteration");
591 si->si_used = -1; /* Make this state sticky */
592 return NULL;
593 }
594
595 i = si->si_pos;
596 if (i < 0)
597 goto fail;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000598 entry = so->table;
599 mask = so->mask;
600 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000601 i++;
602 si->si_pos = i+1;
603 if (i > mask)
604 goto fail;
605 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000606 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000607 Py_INCREF(key);
608 return key;
609
610fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000611 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000612 si->si_set = NULL;
613 return NULL;
614}
615
Hye-Shik Change2956762005-08-01 05:26:41 +0000616static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000617 PyObject_HEAD_INIT(&PyType_Type)
618 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000619 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000620 sizeof(setiterobject), /* tp_basicsize */
621 0, /* tp_itemsize */
622 /* methods */
623 (destructor)setiter_dealloc, /* tp_dealloc */
624 0, /* tp_print */
625 0, /* tp_getattr */
626 0, /* tp_setattr */
627 0, /* tp_compare */
628 0, /* tp_repr */
629 0, /* tp_as_number */
630 &setiter_as_sequence, /* tp_as_sequence */
631 0, /* tp_as_mapping */
632 0, /* tp_hash */
633 0, /* tp_call */
634 0, /* tp_str */
635 PyObject_GenericGetAttr, /* tp_getattro */
636 0, /* tp_setattro */
637 0, /* tp_as_buffer */
638 Py_TPFLAGS_DEFAULT, /* tp_flags */
639 0, /* tp_doc */
640 0, /* tp_traverse */
641 0, /* tp_clear */
642 0, /* tp_richcompare */
643 0, /* tp_weaklistoffset */
644 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000645 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000646};
647
Raymond Hettingerd7946662005-08-01 21:39:29 +0000648static int
649set_len(PyObject *so)
650{
651 return ((PySetObject *)so)->used;
652}
653
654static int
655set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000656{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000657 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000658
Raymond Hettingerd7946662005-08-01 21:39:29 +0000659 if (PyAnySet_Check(other))
660 return set_merge_internal(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000661
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000662 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000663 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000664 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000665 while (PyDict_Next(other, &pos, &key, &value)) {
666 if (set_add_internal(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000667 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000669 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670 }
671
Raymond Hettingera38123e2003-11-24 22:18:49 +0000672 it = PyObject_GetIter(other);
673 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000674 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000675
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000676 while ((key = PyIter_Next(it)) != NULL) {
677 if (set_add_internal(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000678 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000679 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000680 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000681 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000682 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000683 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000684 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000685 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000686 return -1;
687 return 0;
688}
689
690static PyObject *
691set_update(PySetObject *so, PyObject *other)
692{
693 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000694 return NULL;
695 Py_RETURN_NONE;
696}
697
698PyDoc_STRVAR(update_doc,
699"Update a set with the union of itself and another.");
700
701static PyObject *
702make_new_set(PyTypeObject *type, PyObject *iterable)
703{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000704 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000705
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000706 if (dummy == NULL) { /* Auto-initialize dummy */
707 dummy = PyString_FromString("<dummy key>");
708 if (dummy == NULL)
709 return NULL;
710 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000711
712 /* create PySetObject structure */
713 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000714 if (so == NULL)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000715 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000716
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +0000717 /* tp_alloc has already zeroed the structure */
718 assert(so->table == NULL && so->fill == 0 && so->used == 0);
719 so->table = so->smalltable;
720 so->mask = PySet_MINSIZE - 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000721 so->lookup = set_lookkey_string;
Raymond Hettingera690a992003-11-16 16:17:49 +0000722 so->hash = -1;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000723 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000724
Raymond Hettingera38123e2003-11-24 22:18:49 +0000725 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000726 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000727 Py_DECREF(so);
728 return NULL;
729 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000730 }
731
Raymond Hettingera690a992003-11-16 16:17:49 +0000732 return (PyObject *)so;
733}
734
Raymond Hettingerd7946662005-08-01 21:39:29 +0000735/* The empty frozenset is a singleton */
736static PyObject *emptyfrozenset = NULL;
737
Raymond Hettingera690a992003-11-16 16:17:49 +0000738static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000739frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000740{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000741 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000742
743 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
744 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000745
746 if (type != &PyFrozenSet_Type)
747 return make_new_set(type, iterable);
748
749 if (iterable != NULL) {
750 /* frozenset(f) is idempotent */
751 if (PyFrozenSet_CheckExact(iterable)) {
752 Py_INCREF(iterable);
753 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000754 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000755 result = make_new_set(type, iterable);
756 if (result == NULL || set_len(result))
757 return result;
758 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000759 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000760 /* The empty frozenset is a singleton */
761 if (emptyfrozenset == NULL)
762 emptyfrozenset = make_new_set(type, NULL);
763 Py_XINCREF(emptyfrozenset);
764 return emptyfrozenset;
765}
766
767void
768PySet_Fini(void)
769{
Raymond Hettingera9d99362005-08-05 00:01:15 +0000770 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000771 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000772}
773
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000774static PyObject *
775set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
776{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000777 return make_new_set(type, NULL);
778}
779
Raymond Hettingera690a992003-11-16 16:17:49 +0000780static void
781set_dealloc(PySetObject *so)
782{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000783 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000784 int fill = so->fill;
785
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000786 PyObject_GC_UnTrack(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787 Py_TRASHCAN_SAFE_BEGIN(so)
Raymond Hettinger691d8052004-05-30 07:26:47 +0000788 if (so->weakreflist != NULL)
789 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000791 for (entry = so->table; fill > 0; entry++) {
792 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000794 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000795 }
796 }
797 if (so->table != so->smalltable)
798 PyMem_DEL(so->table);
799
Raymond Hettingera690a992003-11-16 16:17:49 +0000800 so->ob_type->tp_free(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000802}
803
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000804static int
805set_traverse(PySetObject *so, visitproc visit, void *arg)
806{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000807 int pos = 0;
808 PyObject *key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000810 while (set_next_internal(so, &pos, &key))
811 Py_VISIT(key);
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000812 return 0;
813}
814
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000815/* set_swap_bodies() switches the contents of any two sets by moving their
816 internal data pointers and, if needed, copying the internal smalltables.
817 Semantically equivalent to:
818
819 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
820
821 The function always succeeds and it leaves both objects in a stable state.
822 Useful for creating temporary frozensets from sets for membership testing
823 in __contains__(), discard(), and remove(). Also useful for operations
824 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000825 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000826*/
827
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828static void
829set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000830{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831 int t;
832 setentry *u;
833 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
834 setentry tab[PySet_MINSIZE];
835 long h;
836
837 t = a->fill; a->fill = b->fill; b->fill = t;
838 t = a->used; a->used = b->used; b->used = t;
839 t = a->mask; a->mask = b->mask; b->mask = t;
840
841 u = a->table;
842 if (a->table == a->smalltable)
843 u = b->smalltable;
844 a->table = b->table;
845 if (b->table == b->smalltable)
846 a->table = a->smalltable;
847 b->table = u;
848
849 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
850
851 if (a->table == a->smalltable || b->table == b->smalltable) {
852 memcpy(tab, a->smalltable, sizeof(tab));
853 memcpy(a->smalltable, b->smalltable, sizeof(tab));
854 memcpy(b->smalltable, tab, sizeof(tab));
855 }
856
Raymond Hettingera580c472005-08-05 17:19:54 +0000857 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
858 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
859 h = a->hash; a->hash = b->hash; b->hash = h;
860 } else {
861 a->hash = -1;
862 b->hash = -1;
863 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000864}
865
866static int
867set_contains(PySetObject *so, PyObject *key)
868{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000869 PyObject *tmpkey;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000870 int result;
871
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872 result = set_contains_internal(so, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000873 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000874 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000875 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
876 if (tmpkey == NULL)
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000877 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000878 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
879 result = set_contains_internal(so, tmpkey);
880 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
881 Py_DECREF(tmpkey);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000882 }
883 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000884}
885
886static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000887set_direct_contains(PySetObject *so, PyObject *key)
888{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000889 long result;
890
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000891 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000892 if (result == -1)
893 return NULL;
894 return PyBool_FromLong(result);
895}
896
897PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
898
899static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000900set_copy(PySetObject *so)
901{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000902 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000903}
904
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000905static PyObject *
906frozenset_copy(PySetObject *so)
907{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000908 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000909 Py_INCREF(so);
910 return (PyObject *)so;
911 }
912 return set_copy(so);
913}
914
Raymond Hettingera690a992003-11-16 16:17:49 +0000915PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
916
917static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000918set_union(PySetObject *so, PyObject *other)
919{
920 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000921
922 result = (PySetObject *)set_copy(so);
923 if (result == NULL)
924 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000925 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000926 Py_DECREF(result);
927 return NULL;
928 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000929 return (PyObject *)result;
930}
931
932PyDoc_STRVAR(union_doc,
933 "Return the union of two sets as a new set.\n\
934\n\
935(i.e. all elements that are in either set.)");
936
937static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000938set_or(PySetObject *so, PyObject *other)
939{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000940 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000941 Py_INCREF(Py_NotImplemented);
942 return Py_NotImplemented;
943 }
944 return set_union(so, other);
945}
946
947static PyObject *
948set_ior(PySetObject *so, PyObject *other)
949{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000950 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000951 Py_INCREF(Py_NotImplemented);
952 return Py_NotImplemented;
953 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000954 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +0000955 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000956 Py_INCREF(so);
957 return (PyObject *)so;
958}
959
960static PyObject *
961set_intersection(PySetObject *so, PyObject *other)
962{
963 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000964 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000965
966 result = (PySetObject *)make_new_set(so->ob_type, NULL);
967 if (result == NULL)
968 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000969
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000970 if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) {
971 tmp = (PyObject *)so;
972 so = (PySetObject *)other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000973 other = tmp;
974 }
975
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000976 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000977 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000978 while (set_next_internal((PySetObject *)other, &pos, &key)) {
979 if (set_contains_internal(so, key)) {
980 if (set_add_internal(result, key) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000981 Py_DECREF(result);
982 return NULL;
983 }
984 }
985 }
986 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000987 }
988
Raymond Hettingera690a992003-11-16 16:17:49 +0000989 it = PyObject_GetIter(other);
990 if (it == NULL) {
991 Py_DECREF(result);
992 return NULL;
993 }
994
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000995 while ((key = PyIter_Next(it)) != NULL) {
996 if (set_contains_internal(so, key)) {
997 if (set_add_internal(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000998 Py_DECREF(it);
999 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001000 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001001 return NULL;
1002 }
1003 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001004 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001005 }
1006 Py_DECREF(it);
1007 if (PyErr_Occurred()) {
1008 Py_DECREF(result);
1009 return NULL;
1010 }
1011 return (PyObject *)result;
1012}
1013
1014PyDoc_STRVAR(intersection_doc,
1015"Return the intersection of two sets as a new set.\n\
1016\n\
1017(i.e. all elements that are in both sets.)");
1018
1019static PyObject *
1020set_intersection_update(PySetObject *so, PyObject *other)
1021{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001022 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001023
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001024 tmp = set_intersection(so, other);
1025 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001026 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001027 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001028 Py_DECREF(tmp);
1029 Py_RETURN_NONE;
1030}
1031
1032PyDoc_STRVAR(intersection_update_doc,
1033"Update a set with the intersection of itself and another.");
1034
1035static PyObject *
1036set_and(PySetObject *so, PyObject *other)
1037{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001038 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001039 Py_INCREF(Py_NotImplemented);
1040 return Py_NotImplemented;
1041 }
1042 return set_intersection(so, other);
1043}
1044
1045static PyObject *
1046set_iand(PySetObject *so, PyObject *other)
1047{
1048 PyObject *result;
1049
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001050 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001051 Py_INCREF(Py_NotImplemented);
1052 return Py_NotImplemented;
1053 }
1054 result = set_intersection_update(so, other);
1055 if (result == NULL)
1056 return NULL;
1057 Py_DECREF(result);
1058 Py_INCREF(so);
1059 return (PyObject *)so;
1060}
1061
1062static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001063set_difference_update(PySetObject *so, PyObject *other)
1064{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001065 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001066
1067 it = PyObject_GetIter(other);
1068 if (it == NULL)
1069 return NULL;
1070
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001071 while ((key = PyIter_Next(it)) != NULL) {
1072 if (set_discard_internal(so, key) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001073 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001074 Py_DECREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001075 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001076 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001077 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001078 }
1079 Py_DECREF(it);
1080 if (PyErr_Occurred())
1081 return NULL;
1082 Py_RETURN_NONE;
1083}
1084
1085PyDoc_STRVAR(difference_update_doc,
1086"Remove all elements of another set from this set.");
1087
1088static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001089set_difference(PySetObject *so, PyObject *other)
1090{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001091 PyObject *tmp, *key, *result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001092 int pos = 0;
1093
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001094 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001095 result = set_copy(so);
1096 if (result == NULL)
1097 return result;
1098 tmp = set_difference_update((PySetObject *)result, other);
1099 if (tmp != NULL) {
1100 Py_DECREF(tmp);
1101 return result;
1102 }
1103 Py_DECREF(result);
1104 return NULL;
1105 }
1106
1107 result = make_new_set(so->ob_type, NULL);
1108 if (result == NULL)
1109 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001110
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001111 if (PyDict_Check(other)) {
1112 while (set_next_internal(so, &pos, &key)) {
1113 if (!PyDict_Contains(other, key)) {
1114 if (set_add_internal((PySetObject *)result, key) == -1)
1115 return NULL;
1116 }
1117 }
1118 return result;
1119 }
1120
1121 while (set_next_internal(so, &pos, &key)) {
1122 if (!set_contains_internal((PySetObject *)other, key)) {
1123 if (set_add_internal((PySetObject *)result, key) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001124 return NULL;
1125 }
1126 }
1127 return result;
1128}
1129
1130PyDoc_STRVAR(difference_doc,
1131"Return the difference of two sets as a new set.\n\
1132\n\
1133(i.e. all elements that are in this set but not the other.)");
1134static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001135set_sub(PySetObject *so, PyObject *other)
1136{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001137 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001138 Py_INCREF(Py_NotImplemented);
1139 return Py_NotImplemented;
1140 }
1141 return set_difference(so, other);
1142}
1143
1144static PyObject *
1145set_isub(PySetObject *so, PyObject *other)
1146{
1147 PyObject *result;
1148
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001149 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001150 Py_INCREF(Py_NotImplemented);
1151 return Py_NotImplemented;
1152 }
1153 result = set_difference_update(so, other);
1154 if (result == NULL)
1155 return NULL;
1156 Py_DECREF(result);
1157 Py_INCREF(so);
1158 return (PyObject *)so;
1159}
1160
1161static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001162set_symmetric_difference_update(PySetObject *so, PyObject *other)
1163{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001164 PySetObject *otherset;
1165 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001166 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001167
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001168 if (PyDict_Check(other)) {
1169 PyObject *value;
1170 int rv;
1171 while (PyDict_Next(other, &pos, &key, &value)) {
1172 rv = set_discard_internal(so, key);
1173 if (rv == -1)
1174 return NULL;
1175 if (rv == DISCARD_NOTFOUND) {
1176 if (set_add_internal(so, key) == -1)
1177 return NULL;
1178 }
1179 }
1180 Py_RETURN_NONE;
1181 }
1182
1183 if (PyAnySet_Check(other)) {
1184 Py_INCREF(other);
1185 otherset = (PySetObject *)other;
1186 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001187 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1188 if (otherset == NULL)
1189 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001190 }
1191
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001192 while (set_next_internal(otherset, &pos, &key)) {
1193 int rv = set_discard_internal(so, key);
1194 if (rv == -1) {
1195 Py_XDECREF(otherset);
1196 return NULL;
1197 }
1198 if (rv == DISCARD_NOTFOUND) {
1199 if (set_add_internal(so, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001200 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001201 return NULL;
1202 }
1203 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001204 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001205 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001206 Py_RETURN_NONE;
1207}
1208
1209PyDoc_STRVAR(symmetric_difference_update_doc,
1210"Update a set with the symmetric difference of itself and another.");
1211
1212static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001213set_symmetric_difference(PySetObject *so, PyObject *other)
1214{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001215 PyObject *rv;
1216 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001217
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001218 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1219 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001220 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001221 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1222 if (rv == NULL)
1223 return NULL;
1224 Py_DECREF(rv);
1225 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001226}
1227
1228PyDoc_STRVAR(symmetric_difference_doc,
1229"Return the symmetric difference of two sets as a new set.\n\
1230\n\
1231(i.e. all elements that are in exactly one of the sets.)");
1232
1233static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001234set_xor(PySetObject *so, PyObject *other)
1235{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001236 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001237 Py_INCREF(Py_NotImplemented);
1238 return Py_NotImplemented;
1239 }
1240 return set_symmetric_difference(so, other);
1241}
1242
1243static PyObject *
1244set_ixor(PySetObject *so, PyObject *other)
1245{
1246 PyObject *result;
1247
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001248 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001249 Py_INCREF(Py_NotImplemented);
1250 return Py_NotImplemented;
1251 }
1252 result = set_symmetric_difference_update(so, other);
1253 if (result == NULL)
1254 return NULL;
1255 Py_DECREF(result);
1256 Py_INCREF(so);
1257 return (PyObject *)so;
1258}
1259
1260static PyObject *
1261set_issubset(PySetObject *so, PyObject *other)
1262{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001263 PyObject *tmp, *result;
Raymond Hettinger99220fa2005-08-06 18:57:13 +00001264 register setentry *entry;
1265 register int i;
Raymond Hettingera690a992003-11-16 16:17:49 +00001266
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001267 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001268 tmp = make_new_set(&PySet_Type, other);
1269 if (tmp == NULL)
1270 return NULL;
1271 result = set_issubset(so, tmp);
1272 Py_DECREF(tmp);
1273 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001274 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001275 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001276 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001277
Raymond Hettinger99220fa2005-08-06 18:57:13 +00001278 entry = &so->table[0];
1279 for (i=so->used ; i ; entry++, i--) {
1280 while (entry->key == NULL || entry->key==dummy)
1281 entry++;
1282 if (!set_contains_internal((PySetObject *)other, entry->key))
Raymond Hettingera690a992003-11-16 16:17:49 +00001283 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001284 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001285 Py_RETURN_TRUE;
1286}
1287
1288PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1289
1290static PyObject *
1291set_issuperset(PySetObject *so, PyObject *other)
1292{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001293 PyObject *tmp, *result;
1294
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001295 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001296 tmp = make_new_set(&PySet_Type, other);
1297 if (tmp == NULL)
1298 return NULL;
1299 result = set_issuperset(so, tmp);
1300 Py_DECREF(tmp);
1301 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001302 }
1303 return set_issubset((PySetObject *)other, (PyObject *)so);
1304}
1305
1306PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1307
1308static long
1309set_nohash(PyObject *self)
1310{
1311 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1312 return -1;
1313}
1314
1315static int
1316set_nocmp(PyObject *self)
1317{
1318 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1319 return -1;
1320}
1321
1322static long
1323frozenset_hash(PyObject *self)
1324{
Raymond Hettingera690a992003-11-16 16:17:49 +00001325 PySetObject *so = (PySetObject *)self;
Raymond Hettingera580c472005-08-05 17:19:54 +00001326 long h, hash = 1927868237L;
1327 setentry *entry;
1328 int i;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001329
Raymond Hettingera690a992003-11-16 16:17:49 +00001330 if (so->hash != -1)
1331 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001332
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001333 hash *= set_len(self) + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +00001334 entry = &so->table[0];
1335 for (i=so->used ; i ; entry++, i--) {
1336 while (entry->key == NULL || entry->key==dummy)
1337 entry++;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001338 /* Work to increase the bit dispersion for closely spaced hash
1339 values. The is important because some use cases have many
1340 combinations of a small number of elements with nearby
1341 hashes so that many distinct combinations collapse to only
1342 a handful of distinct hash values. */
Raymond Hettingera9d99362005-08-05 00:01:15 +00001343 h = entry->hash;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001344 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001345 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001346 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001347 if (hash == -1)
1348 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001349 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001350 return hash;
1351}
1352
1353static PyObject *
1354set_richcompare(PySetObject *v, PyObject *w, int op)
1355{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001356 PyObject *r1, *r2;
1357
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001358 if(!PyAnySet_Check(w)) {
1359 if (op == Py_EQ)
1360 Py_RETURN_FALSE;
1361 if (op == Py_NE)
1362 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001363 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1364 return NULL;
1365 }
1366 switch (op) {
1367 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001368 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001369 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001370 return set_issubset(v, w);
1371 case Py_NE:
1372 if (set_len((PyObject *)v) != set_len(w))
1373 Py_RETURN_TRUE;
1374 r1 = set_issubset(v, w);
1375 assert (r1 != NULL);
1376 r2 = PyBool_FromLong(PyObject_Not(r1));
1377 Py_DECREF(r1);
1378 return r2;
1379 case Py_LE:
1380 return set_issubset(v, w);
1381 case Py_GE:
1382 return set_issuperset(v, w);
1383 case Py_LT:
1384 if (set_len((PyObject *)v) >= set_len(w))
1385 Py_RETURN_FALSE;
1386 return set_issubset(v, w);
1387 case Py_GT:
1388 if (set_len((PyObject *)v) <= set_len(w))
1389 Py_RETURN_FALSE;
1390 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001391 }
1392 Py_INCREF(Py_NotImplemented);
1393 return Py_NotImplemented;
1394}
1395
1396static PyObject *
1397set_repr(PySetObject *so)
1398{
1399 PyObject *keys, *result, *listrepr;
1400
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001401 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001402 if (keys == NULL)
1403 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001404 listrepr = PyObject_Repr(keys);
1405 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001406 if (listrepr == NULL)
1407 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001408
1409 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1410 PyString_AS_STRING(listrepr));
1411 Py_DECREF(listrepr);
1412 return result;
1413}
1414
1415static int
1416set_tp_print(PySetObject *so, FILE *fp, int flags)
1417{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001418 PyObject *key;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001419 int pos=0;
1420 char *emit = ""; /* No separator emitted on first pass */
1421 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001422
Raymond Hettingera690a992003-11-16 16:17:49 +00001423 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001424 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001425 fputs(emit, fp);
1426 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001427 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001428 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001429 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001430 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001431 return 0;
1432}
1433
1434static PyObject *
1435set_clear(PySetObject *so)
1436{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001437 set_clear_internal(so);
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001438 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001439}
1440
1441PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1442
Raymond Hettingera690a992003-11-16 16:17:49 +00001443static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001444set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001445{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001446 if (set_add_internal(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001447 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001448 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001449}
1450
1451PyDoc_STRVAR(add_doc,
1452"Add an element to a set.\n\
1453\n\
1454This has no effect if the element is already present.");
1455
1456static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001457set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001458{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001459 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001460 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001461
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001462 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1463 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1464 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001465 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001466 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1467 result = set_remove(so, tmpkey);
1468 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1469 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001470 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001471 }
Raymond Hettinger0deab622003-12-13 18:53:18 +00001472
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001473 rv = set_discard_internal(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001474 if (rv == -1)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001475 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001476 else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001477 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001478 return NULL;
1479 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001480 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001481}
1482
1483PyDoc_STRVAR(remove_doc,
1484"Remove an element from a set; it must be a member.\n\
1485\n\
1486If the element is not a member, raise a KeyError.");
1487
1488static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001489set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001490{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001491 PyObject *tmpkey, *result;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001492
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001493 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1494 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1495 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001496 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001497 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1498 result = set_discard(so, tmpkey);
1499 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1500 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001501 return result;
1502 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001503
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001504 if (set_discard_internal(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001505 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001506 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001507}
1508
1509PyDoc_STRVAR(discard_doc,
1510"Remove an element from a set if it is a member.\n\
1511\n\
1512If the element is not a member, do nothing.");
1513
1514static PyObject *
1515set_pop(PySetObject *so)
1516{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001517 PyObject *key;
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001518 register setentry *entry;
1519 register int i = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001520
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001521 assert (PyAnySet_Check(so));
1522 if (so->used == 0) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001523 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1524 return NULL;
1525 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001526
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001527 /* Set entry to "the first" unused or dummy set entry. We abuse
1528 * the hash field of slot 0 to hold a search finger:
1529 * If slot 0 has a value, use slot 0.
1530 * Else slot 0 is being used to hold a search finger,
1531 * and we use its hash value as the first index to look.
1532 */
1533 entry = &so->table[0];
1534 if (entry->key == NULL || entry->key == dummy) {
1535 i = (int)entry->hash;
1536 /* The hash field may be a real hash value, or it may be a
1537 * legit search finger, or it may be a once-legit search
1538 * finger that's out of bounds now because it wrapped around
1539 * or the table shrunk -- simply make sure it's in bounds now.
1540 */
1541 if (i > so->mask || i < 1)
1542 i = 1; /* skip slot 0 */
1543 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
1544 i++;
1545 if (i > so->mask)
1546 i = 1;
1547 }
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001548 }
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001549 key = entry->key;
1550 Py_INCREF(dummy);
1551 entry->key = dummy;
1552 so->used--;
1553 so->table[0].hash = i + 1; /* next place to start */
Raymond Hettingera690a992003-11-16 16:17:49 +00001554 return key;
1555}
1556
1557PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1558
1559static PyObject *
1560set_reduce(PySetObject *so)
1561{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001562 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001563
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001564 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001565 if (keys == NULL)
1566 goto done;
1567 args = PyTuple_Pack(1, keys);
1568 if (args == NULL)
1569 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001570 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1571 if (dict == NULL) {
1572 PyErr_Clear();
1573 dict = Py_None;
1574 Py_INCREF(dict);
1575 }
1576 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001577done:
1578 Py_XDECREF(args);
1579 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001580 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001581 return result;
1582}
1583
1584PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1585
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001586static int
1587set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1588{
1589 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001590
1591 if (!PyAnySet_Check(self))
1592 return -1;
1593 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1594 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001595 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001596 self->hash = -1;
1597 if (iterable == NULL)
1598 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001599 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001600}
1601
Raymond Hettingera690a992003-11-16 16:17:49 +00001602static PySequenceMethods set_as_sequence = {
1603 (inquiry)set_len, /* sq_length */
1604 0, /* sq_concat */
1605 0, /* sq_repeat */
1606 0, /* sq_item */
1607 0, /* sq_slice */
1608 0, /* sq_ass_item */
1609 0, /* sq_ass_slice */
1610 (objobjproc)set_contains, /* sq_contains */
1611};
1612
1613/* set object ********************************************************/
1614
1615static PyMethodDef set_methods[] = {
1616 {"add", (PyCFunction)set_add, METH_O,
1617 add_doc},
1618 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1619 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001620 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001621 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001622 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1623 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001624 {"discard", (PyCFunction)set_discard, METH_O,
1625 discard_doc},
1626 {"difference", (PyCFunction)set_difference, METH_O,
1627 difference_doc},
1628 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1629 difference_update_doc},
1630 {"intersection",(PyCFunction)set_intersection, METH_O,
1631 intersection_doc},
1632 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1633 intersection_update_doc},
1634 {"issubset", (PyCFunction)set_issubset, METH_O,
1635 issubset_doc},
1636 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1637 issuperset_doc},
1638 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1639 pop_doc},
1640 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1641 reduce_doc},
1642 {"remove", (PyCFunction)set_remove, METH_O,
1643 remove_doc},
1644 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1645 symmetric_difference_doc},
1646 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1647 symmetric_difference_update_doc},
1648 {"union", (PyCFunction)set_union, METH_O,
1649 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001650 {"update", (PyCFunction)set_update, METH_O,
1651 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001652 {NULL, NULL} /* sentinel */
1653};
1654
1655static PyNumberMethods set_as_number = {
1656 0, /*nb_add*/
1657 (binaryfunc)set_sub, /*nb_subtract*/
1658 0, /*nb_multiply*/
1659 0, /*nb_divide*/
1660 0, /*nb_remainder*/
1661 0, /*nb_divmod*/
1662 0, /*nb_power*/
1663 0, /*nb_negative*/
1664 0, /*nb_positive*/
1665 0, /*nb_absolute*/
1666 0, /*nb_nonzero*/
1667 0, /*nb_invert*/
1668 0, /*nb_lshift*/
1669 0, /*nb_rshift*/
1670 (binaryfunc)set_and, /*nb_and*/
1671 (binaryfunc)set_xor, /*nb_xor*/
1672 (binaryfunc)set_or, /*nb_or*/
1673 0, /*nb_coerce*/
1674 0, /*nb_int*/
1675 0, /*nb_long*/
1676 0, /*nb_float*/
1677 0, /*nb_oct*/
1678 0, /*nb_hex*/
1679 0, /*nb_inplace_add*/
1680 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1681 0, /*nb_inplace_multiply*/
1682 0, /*nb_inplace_divide*/
1683 0, /*nb_inplace_remainder*/
1684 0, /*nb_inplace_power*/
1685 0, /*nb_inplace_lshift*/
1686 0, /*nb_inplace_rshift*/
1687 (binaryfunc)set_iand, /*nb_inplace_and*/
1688 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1689 (binaryfunc)set_ior, /*nb_inplace_or*/
1690};
1691
1692PyDoc_STRVAR(set_doc,
1693"set(iterable) --> set object\n\
1694\n\
1695Build an unordered collection.");
1696
1697PyTypeObject PySet_Type = {
1698 PyObject_HEAD_INIT(&PyType_Type)
1699 0, /* ob_size */
1700 "set", /* tp_name */
1701 sizeof(PySetObject), /* tp_basicsize */
1702 0, /* tp_itemsize */
1703 /* methods */
1704 (destructor)set_dealloc, /* tp_dealloc */
1705 (printfunc)set_tp_print, /* tp_print */
1706 0, /* tp_getattr */
1707 0, /* tp_setattr */
1708 (cmpfunc)set_nocmp, /* tp_compare */
1709 (reprfunc)set_repr, /* tp_repr */
1710 &set_as_number, /* tp_as_number */
1711 &set_as_sequence, /* tp_as_sequence */
1712 0, /* tp_as_mapping */
1713 set_nohash, /* tp_hash */
1714 0, /* tp_call */
1715 0, /* tp_str */
1716 PyObject_GenericGetAttr, /* tp_getattro */
1717 0, /* tp_setattro */
1718 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001719 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001720 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001721 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001722 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001723 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001724 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001725 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001726 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001727 0, /* tp_iternext */
1728 set_methods, /* tp_methods */
1729 0, /* tp_members */
1730 0, /* tp_getset */
1731 0, /* tp_base */
1732 0, /* tp_dict */
1733 0, /* tp_descr_get */
1734 0, /* tp_descr_set */
1735 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001736 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001737 PyType_GenericAlloc, /* tp_alloc */
1738 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001739 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001740};
1741
1742/* frozenset object ********************************************************/
1743
1744
1745static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001746 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001747 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001748 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001749 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001750 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001751 difference_doc},
1752 {"intersection",(PyCFunction)set_intersection, METH_O,
1753 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001754 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001755 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001756 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001757 issuperset_doc},
1758 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1759 reduce_doc},
1760 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1761 symmetric_difference_doc},
1762 {"union", (PyCFunction)set_union, METH_O,
1763 union_doc},
1764 {NULL, NULL} /* sentinel */
1765};
1766
1767static PyNumberMethods frozenset_as_number = {
1768 0, /*nb_add*/
1769 (binaryfunc)set_sub, /*nb_subtract*/
1770 0, /*nb_multiply*/
1771 0, /*nb_divide*/
1772 0, /*nb_remainder*/
1773 0, /*nb_divmod*/
1774 0, /*nb_power*/
1775 0, /*nb_negative*/
1776 0, /*nb_positive*/
1777 0, /*nb_absolute*/
1778 0, /*nb_nonzero*/
1779 0, /*nb_invert*/
1780 0, /*nb_lshift*/
1781 0, /*nb_rshift*/
1782 (binaryfunc)set_and, /*nb_and*/
1783 (binaryfunc)set_xor, /*nb_xor*/
1784 (binaryfunc)set_or, /*nb_or*/
1785};
1786
1787PyDoc_STRVAR(frozenset_doc,
1788"frozenset(iterable) --> frozenset object\n\
1789\n\
1790Build an immutable unordered collection.");
1791
1792PyTypeObject PyFrozenSet_Type = {
1793 PyObject_HEAD_INIT(&PyType_Type)
1794 0, /* ob_size */
1795 "frozenset", /* tp_name */
1796 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001797 0, /* tp_itemsize */
1798 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001799 (destructor)set_dealloc, /* tp_dealloc */
1800 (printfunc)set_tp_print, /* tp_print */
1801 0, /* tp_getattr */
1802 0, /* tp_setattr */
1803 (cmpfunc)set_nocmp, /* tp_compare */
1804 (reprfunc)set_repr, /* tp_repr */
1805 &frozenset_as_number, /* tp_as_number */
1806 &set_as_sequence, /* tp_as_sequence */
1807 0, /* tp_as_mapping */
1808 frozenset_hash, /* tp_hash */
1809 0, /* tp_call */
1810 0, /* tp_str */
1811 PyObject_GenericGetAttr, /* tp_getattro */
1812 0, /* tp_setattro */
1813 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001814 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001815 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001816 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001817 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001818 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001819 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001820 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001821 (getiterfunc)set_iter, /* tp_iter */
1822 0, /* tp_iternext */
1823 frozenset_methods, /* tp_methods */
1824 0, /* tp_members */
1825 0, /* tp_getset */
1826 0, /* tp_base */
1827 0, /* tp_dict */
1828 0, /* tp_descr_get */
1829 0, /* tp_descr_set */
1830 0, /* tp_dictoffset */
1831 0, /* tp_init */
1832 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001833 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001834 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001835};