blob: c46df83b2096bc455637d23020b38a8ea00729ef [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;
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +000069 if (_PyErr_OCCURRED()) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070 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;
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +0000107 if (_PyErr_OCCURRED()) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000108 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
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000387static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000388set_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
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000409 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000410 * 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);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000448 so->hash = -1;
449 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450}
451
452/*
453 * Iterate over a set table. Use like so:
454 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000455 * int pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456 * PyObject *key;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000457 * pos = 0; # important! pos should not otherwise be changed by you
458 * while (set_next_internal(yourset, &pos, &key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000459 * Refer to borrowed reference in key.
460 * }
461 *
462 * CAUTION: In general, it isn't safe to use set_next_internal in a loop that
463 * mutates the table.
464 */
465static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000466set_next_internal(PySetObject *so, int *pos, PyObject **key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467{
468 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000469 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470
471 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000472 i = *pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473 if (i < 0)
474 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000475 entry = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476 mask = so->mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000477 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000478 i++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000479 *pos = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480 if (i > mask)
481 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000482 if (key)
483 *key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484 return 1;
485}
486
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000488set_merge_internal(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000490 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491 register int i;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000492 register setentry *entry, *othertable;
493 register int othermask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494
495 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000496 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000497
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000498 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499 if (other == so || other->used == 0)
500 /* a.update(a) or a.update({}); nothing to do */
501 return 0;
502 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000503 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000504 * that there will be no (or few) overlapping keys.
505 */
506 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
507 if (set_table_resize(so, (so->used + other->used)*2) != 0)
508 return -1;
509 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000510 othermask = other->mask;
511 othertable = other->table;
512 for (i = 0; i <= othermask; i++) {
513 entry = &othertable[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000514 if (entry->key != NULL &&
515 entry->key != dummy) {
516 Py_INCREF(entry->key);
517 set_insert_key(so, entry->key, entry->hash);
518 }
519 }
520 return 0;
521}
522
523static int
524set_contains_internal(PySetObject *so, PyObject *key)
525{
526 long hash;
527
528 if (!PyString_CheckExact(key) ||
529 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
530 hash = PyObject_Hash(key);
531 if (hash == -1)
532 return -1;
533 }
534 key = (so->lookup)(so, key, hash)->key;
535 return key != NULL && key != dummy;
536}
537
Raymond Hettingera9d99362005-08-05 00:01:15 +0000538/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000539
Raymond Hettingerd7946662005-08-01 21:39:29 +0000540static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000541
542typedef struct {
543 PyObject_HEAD
544 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
545 int si_used;
546 int si_pos;
547 long len;
548} setiterobject;
549
550static PyObject *
551set_iter(PySetObject *so)
552{
553 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
554 if (si == NULL)
555 return NULL;
556 Py_INCREF(so);
557 si->si_set = so;
558 si->si_used = so->used;
559 si->si_pos = 0;
560 si->len = so->used;
561 return (PyObject *)si;
562}
563
564static void
565setiter_dealloc(setiterobject *si)
566{
567 Py_XDECREF(si->si_set);
568 PyObject_Del(si);
569}
570
571static int
572setiter_len(setiterobject *si)
573{
574 if (si->si_set != NULL && si->si_used == si->si_set->used)
575 return si->len;
576 return 0;
577}
578
579static PySequenceMethods setiter_as_sequence = {
580 (inquiry)setiter_len, /* sq_length */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000581};
582
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000583static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000584{
585 PyObject *key;
586 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000587 register setentry *entry;
588 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000589
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000590 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000591 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000592 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000594 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000595 PyErr_SetString(PyExc_RuntimeError,
596 "Set changed size during iteration");
597 si->si_used = -1; /* Make this state sticky */
598 return NULL;
599 }
600
601 i = si->si_pos;
602 if (i < 0)
603 goto fail;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000604 entry = so->table;
605 mask = so->mask;
606 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000607 i++;
608 si->si_pos = i+1;
609 if (i > mask)
610 goto fail;
611 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000612 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000613 Py_INCREF(key);
614 return key;
615
616fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000617 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000618 si->si_set = NULL;
619 return NULL;
620}
621
Hye-Shik Change2956762005-08-01 05:26:41 +0000622static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000623 PyObject_HEAD_INIT(&PyType_Type)
624 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000625 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000626 sizeof(setiterobject), /* tp_basicsize */
627 0, /* tp_itemsize */
628 /* methods */
629 (destructor)setiter_dealloc, /* tp_dealloc */
630 0, /* tp_print */
631 0, /* tp_getattr */
632 0, /* tp_setattr */
633 0, /* tp_compare */
634 0, /* tp_repr */
635 0, /* tp_as_number */
636 &setiter_as_sequence, /* tp_as_sequence */
637 0, /* tp_as_mapping */
638 0, /* tp_hash */
639 0, /* tp_call */
640 0, /* tp_str */
641 PyObject_GenericGetAttr, /* tp_getattro */
642 0, /* tp_setattro */
643 0, /* tp_as_buffer */
644 Py_TPFLAGS_DEFAULT, /* tp_flags */
645 0, /* tp_doc */
646 0, /* tp_traverse */
647 0, /* tp_clear */
648 0, /* tp_richcompare */
649 0, /* tp_weaklistoffset */
650 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000651 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000652};
653
Raymond Hettingerd7946662005-08-01 21:39:29 +0000654static int
655set_len(PyObject *so)
656{
657 return ((PySetObject *)so)->used;
658}
659
660static int
661set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000662{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000663 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000664
Raymond Hettingerd7946662005-08-01 21:39:29 +0000665 if (PyAnySet_Check(other))
666 return set_merge_internal(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000667
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000669 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000671 while (PyDict_Next(other, &pos, &key, &value)) {
672 if (set_add_internal(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000673 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000674 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000675 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000676 }
677
Raymond Hettingera38123e2003-11-24 22:18:49 +0000678 it = PyObject_GetIter(other);
679 if (it == NULL)
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 while ((key = PyIter_Next(it)) != NULL) {
683 if (set_add_internal(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000684 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000685 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000686 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000687 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000688 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000689 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000690 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000691 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000692 return -1;
693 return 0;
694}
695
696static PyObject *
697set_update(PySetObject *so, PyObject *other)
698{
699 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000700 return NULL;
701 Py_RETURN_NONE;
702}
703
704PyDoc_STRVAR(update_doc,
705"Update a set with the union of itself and another.");
706
707static PyObject *
708make_new_set(PyTypeObject *type, PyObject *iterable)
709{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000710 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000711
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000712 if (dummy == NULL) { /* Auto-initialize dummy */
713 dummy = PyString_FromString("<dummy key>");
714 if (dummy == NULL)
715 return NULL;
716 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000717
718 /* create PySetObject structure */
719 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000720 if (so == NULL)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000721 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000722
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +0000723 /* tp_alloc has already zeroed the structure */
724 assert(so->table == NULL && so->fill == 0 && so->used == 0);
725 so->table = so->smalltable;
726 so->mask = PySet_MINSIZE - 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000727 so->lookup = set_lookkey_string;
Raymond Hettingera690a992003-11-16 16:17:49 +0000728 so->hash = -1;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000729 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000730
Raymond Hettingera38123e2003-11-24 22:18:49 +0000731 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000732 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000733 Py_DECREF(so);
734 return NULL;
735 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000736 }
737
Raymond Hettingera690a992003-11-16 16:17:49 +0000738 return (PyObject *)so;
739}
740
Raymond Hettingerd7946662005-08-01 21:39:29 +0000741/* The empty frozenset is a singleton */
742static PyObject *emptyfrozenset = NULL;
743
Raymond Hettingera690a992003-11-16 16:17:49 +0000744static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000745frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000746{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000747 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000748
749 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
750 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000751
752 if (type != &PyFrozenSet_Type)
753 return make_new_set(type, iterable);
754
755 if (iterable != NULL) {
756 /* frozenset(f) is idempotent */
757 if (PyFrozenSet_CheckExact(iterable)) {
758 Py_INCREF(iterable);
759 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000760 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000761 result = make_new_set(type, iterable);
762 if (result == NULL || set_len(result))
763 return result;
764 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000765 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000766 /* The empty frozenset is a singleton */
767 if (emptyfrozenset == NULL)
768 emptyfrozenset = make_new_set(type, NULL);
769 Py_XINCREF(emptyfrozenset);
770 return emptyfrozenset;
771}
772
773void
774PySet_Fini(void)
775{
Raymond Hettingera9d99362005-08-05 00:01:15 +0000776 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000777 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000778}
779
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000780static PyObject *
781set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
782{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000783 return make_new_set(type, NULL);
784}
785
Raymond Hettingera690a992003-11-16 16:17:49 +0000786static void
787set_dealloc(PySetObject *so)
788{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000789 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790 int fill = so->fill;
791
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000792 PyObject_GC_UnTrack(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793 Py_TRASHCAN_SAFE_BEGIN(so)
Raymond Hettinger691d8052004-05-30 07:26:47 +0000794 if (so->weakreflist != NULL)
795 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000797 for (entry = so->table; fill > 0; entry++) {
798 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000800 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801 }
802 }
803 if (so->table != so->smalltable)
804 PyMem_DEL(so->table);
805
Raymond Hettingera690a992003-11-16 16:17:49 +0000806 so->ob_type->tp_free(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000808}
809
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000810static int
811set_traverse(PySetObject *so, visitproc visit, void *arg)
812{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000813 int pos = 0;
814 PyObject *key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000816 while (set_next_internal(so, &pos, &key))
817 Py_VISIT(key);
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000818 return 0;
819}
820
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000821/* set_swap_bodies() switches the contents of any two sets by moving their
822 internal data pointers and, if needed, copying the internal smalltables.
823 Semantically equivalent to:
824
825 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
826
827 The function always succeeds and it leaves both objects in a stable state.
828 Useful for creating temporary frozensets from sets for membership testing
829 in __contains__(), discard(), and remove(). Also useful for operations
830 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000831 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000832*/
833
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000834static void
835set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000836{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837 int t;
838 setentry *u;
839 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
840 setentry tab[PySet_MINSIZE];
841 long h;
842
843 t = a->fill; a->fill = b->fill; b->fill = t;
844 t = a->used; a->used = b->used; b->used = t;
845 t = a->mask; a->mask = b->mask; b->mask = t;
846
847 u = a->table;
848 if (a->table == a->smalltable)
849 u = b->smalltable;
850 a->table = b->table;
851 if (b->table == b->smalltable)
852 a->table = a->smalltable;
853 b->table = u;
854
855 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
856
857 if (a->table == a->smalltable || b->table == b->smalltable) {
858 memcpy(tab, a->smalltable, sizeof(tab));
859 memcpy(a->smalltable, b->smalltable, sizeof(tab));
860 memcpy(b->smalltable, tab, sizeof(tab));
861 }
862
Raymond Hettingera580c472005-08-05 17:19:54 +0000863 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
864 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
865 h = a->hash; a->hash = b->hash; b->hash = h;
866 } else {
867 a->hash = -1;
868 b->hash = -1;
869 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000870}
871
872static int
873set_contains(PySetObject *so, PyObject *key)
874{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000875 PyObject *tmpkey;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000876 int result;
877
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000878 result = set_contains_internal(so, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000879 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000880 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000881 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
882 if (tmpkey == NULL)
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000883 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000884 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
885 result = set_contains_internal(so, tmpkey);
886 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
887 Py_DECREF(tmpkey);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000888 }
889 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000890}
891
892static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000893set_direct_contains(PySetObject *so, PyObject *key)
894{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000895 long result;
896
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000897 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000898 if (result == -1)
899 return NULL;
900 return PyBool_FromLong(result);
901}
902
903PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
904
905static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000906set_copy(PySetObject *so)
907{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000908 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000909}
910
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000911static PyObject *
912frozenset_copy(PySetObject *so)
913{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000914 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000915 Py_INCREF(so);
916 return (PyObject *)so;
917 }
918 return set_copy(so);
919}
920
Raymond Hettingera690a992003-11-16 16:17:49 +0000921PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
922
923static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000924set_union(PySetObject *so, PyObject *other)
925{
926 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000927
928 result = (PySetObject *)set_copy(so);
929 if (result == NULL)
930 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000931 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000932 Py_DECREF(result);
933 return NULL;
934 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000935 return (PyObject *)result;
936}
937
938PyDoc_STRVAR(union_doc,
939 "Return the union of two sets as a new set.\n\
940\n\
941(i.e. all elements that are in either set.)");
942
943static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000944set_or(PySetObject *so, PyObject *other)
945{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000946 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000947 Py_INCREF(Py_NotImplemented);
948 return Py_NotImplemented;
949 }
950 return set_union(so, other);
951}
952
953static PyObject *
954set_ior(PySetObject *so, PyObject *other)
955{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000956 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000957 Py_INCREF(Py_NotImplemented);
958 return Py_NotImplemented;
959 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000960 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +0000961 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000962 Py_INCREF(so);
963 return (PyObject *)so;
964}
965
966static PyObject *
967set_intersection(PySetObject *so, PyObject *other)
968{
969 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000970 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000971
972 result = (PySetObject *)make_new_set(so->ob_type, NULL);
973 if (result == NULL)
974 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000975
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000976 if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) {
977 tmp = (PyObject *)so;
978 so = (PySetObject *)other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000979 other = tmp;
980 }
981
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000982 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000983 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000984 while (set_next_internal((PySetObject *)other, &pos, &key)) {
985 if (set_contains_internal(so, key)) {
986 if (set_add_internal(result, key) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000987 Py_DECREF(result);
988 return NULL;
989 }
990 }
991 }
992 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000993 }
994
Raymond Hettingera690a992003-11-16 16:17:49 +0000995 it = PyObject_GetIter(other);
996 if (it == NULL) {
997 Py_DECREF(result);
998 return NULL;
999 }
1000
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001001 while ((key = PyIter_Next(it)) != NULL) {
1002 if (set_contains_internal(so, key)) {
1003 if (set_add_internal(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001004 Py_DECREF(it);
1005 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001006 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001007 return NULL;
1008 }
1009 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001010 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001011 }
1012 Py_DECREF(it);
1013 if (PyErr_Occurred()) {
1014 Py_DECREF(result);
1015 return NULL;
1016 }
1017 return (PyObject *)result;
1018}
1019
1020PyDoc_STRVAR(intersection_doc,
1021"Return the intersection of two sets as a new set.\n\
1022\n\
1023(i.e. all elements that are in both sets.)");
1024
1025static PyObject *
1026set_intersection_update(PySetObject *so, PyObject *other)
1027{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001028 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001029
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001030 tmp = set_intersection(so, other);
1031 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001032 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001033 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001034 Py_DECREF(tmp);
1035 Py_RETURN_NONE;
1036}
1037
1038PyDoc_STRVAR(intersection_update_doc,
1039"Update a set with the intersection of itself and another.");
1040
1041static PyObject *
1042set_and(PySetObject *so, PyObject *other)
1043{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001044 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001045 Py_INCREF(Py_NotImplemented);
1046 return Py_NotImplemented;
1047 }
1048 return set_intersection(so, other);
1049}
1050
1051static PyObject *
1052set_iand(PySetObject *so, PyObject *other)
1053{
1054 PyObject *result;
1055
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001056 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001057 Py_INCREF(Py_NotImplemented);
1058 return Py_NotImplemented;
1059 }
1060 result = set_intersection_update(so, other);
1061 if (result == NULL)
1062 return NULL;
1063 Py_DECREF(result);
1064 Py_INCREF(so);
1065 return (PyObject *)so;
1066}
1067
1068static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001069set_difference_update(PySetObject *so, PyObject *other)
1070{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001071 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001072
1073 it = PyObject_GetIter(other);
1074 if (it == NULL)
1075 return NULL;
1076
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001077 while ((key = PyIter_Next(it)) != NULL) {
1078 if (set_discard_internal(so, key) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001079 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001080 Py_DECREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001081 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001082 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001083 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001084 }
1085 Py_DECREF(it);
1086 if (PyErr_Occurred())
1087 return NULL;
1088 Py_RETURN_NONE;
1089}
1090
1091PyDoc_STRVAR(difference_update_doc,
1092"Remove all elements of another set from this set.");
1093
1094static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001095set_difference(PySetObject *so, PyObject *other)
1096{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001097 PyObject *tmp, *key, *result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001098 int pos = 0;
1099
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001100 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001101 result = set_copy(so);
1102 if (result == NULL)
1103 return result;
1104 tmp = set_difference_update((PySetObject *)result, other);
1105 if (tmp != NULL) {
1106 Py_DECREF(tmp);
1107 return result;
1108 }
1109 Py_DECREF(result);
1110 return NULL;
1111 }
1112
1113 result = make_new_set(so->ob_type, NULL);
1114 if (result == NULL)
1115 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001116
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001117 if (PyDict_Check(other)) {
1118 while (set_next_internal(so, &pos, &key)) {
1119 if (!PyDict_Contains(other, key)) {
1120 if (set_add_internal((PySetObject *)result, key) == -1)
1121 return NULL;
1122 }
1123 }
1124 return result;
1125 }
1126
1127 while (set_next_internal(so, &pos, &key)) {
1128 if (!set_contains_internal((PySetObject *)other, key)) {
1129 if (set_add_internal((PySetObject *)result, key) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001130 return NULL;
1131 }
1132 }
1133 return result;
1134}
1135
1136PyDoc_STRVAR(difference_doc,
1137"Return the difference of two sets as a new set.\n\
1138\n\
1139(i.e. all elements that are in this set but not the other.)");
1140static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001141set_sub(PySetObject *so, PyObject *other)
1142{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001143 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001144 Py_INCREF(Py_NotImplemented);
1145 return Py_NotImplemented;
1146 }
1147 return set_difference(so, other);
1148}
1149
1150static PyObject *
1151set_isub(PySetObject *so, PyObject *other)
1152{
1153 PyObject *result;
1154
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001155 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001156 Py_INCREF(Py_NotImplemented);
1157 return Py_NotImplemented;
1158 }
1159 result = set_difference_update(so, other);
1160 if (result == NULL)
1161 return NULL;
1162 Py_DECREF(result);
1163 Py_INCREF(so);
1164 return (PyObject *)so;
1165}
1166
1167static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001168set_symmetric_difference_update(PySetObject *so, PyObject *other)
1169{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001170 PySetObject *otherset;
1171 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001172 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001173
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001174 if (PyDict_Check(other)) {
1175 PyObject *value;
1176 int rv;
1177 while (PyDict_Next(other, &pos, &key, &value)) {
1178 rv = set_discard_internal(so, key);
1179 if (rv == -1)
1180 return NULL;
1181 if (rv == DISCARD_NOTFOUND) {
1182 if (set_add_internal(so, key) == -1)
1183 return NULL;
1184 }
1185 }
1186 Py_RETURN_NONE;
1187 }
1188
1189 if (PyAnySet_Check(other)) {
1190 Py_INCREF(other);
1191 otherset = (PySetObject *)other;
1192 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001193 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1194 if (otherset == NULL)
1195 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001196 }
1197
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001198 while (set_next_internal(otherset, &pos, &key)) {
1199 int rv = set_discard_internal(so, key);
1200 if (rv == -1) {
1201 Py_XDECREF(otherset);
1202 return NULL;
1203 }
1204 if (rv == DISCARD_NOTFOUND) {
1205 if (set_add_internal(so, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001206 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001207 return NULL;
1208 }
1209 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001210 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001211 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001212 Py_RETURN_NONE;
1213}
1214
1215PyDoc_STRVAR(symmetric_difference_update_doc,
1216"Update a set with the symmetric difference of itself and another.");
1217
1218static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001219set_symmetric_difference(PySetObject *so, PyObject *other)
1220{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001221 PyObject *rv;
1222 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001223
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001224 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1225 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001226 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001227 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1228 if (rv == NULL)
1229 return NULL;
1230 Py_DECREF(rv);
1231 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001232}
1233
1234PyDoc_STRVAR(symmetric_difference_doc,
1235"Return the symmetric difference of two sets as a new set.\n\
1236\n\
1237(i.e. all elements that are in exactly one of the sets.)");
1238
1239static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001240set_xor(PySetObject *so, PyObject *other)
1241{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001242 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001243 Py_INCREF(Py_NotImplemented);
1244 return Py_NotImplemented;
1245 }
1246 return set_symmetric_difference(so, other);
1247}
1248
1249static PyObject *
1250set_ixor(PySetObject *so, PyObject *other)
1251{
1252 PyObject *result;
1253
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001254 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001255 Py_INCREF(Py_NotImplemented);
1256 return Py_NotImplemented;
1257 }
1258 result = set_symmetric_difference_update(so, other);
1259 if (result == NULL)
1260 return NULL;
1261 Py_DECREF(result);
1262 Py_INCREF(so);
1263 return (PyObject *)so;
1264}
1265
1266static PyObject *
1267set_issubset(PySetObject *so, PyObject *other)
1268{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001269 PyObject *tmp, *result;
1270 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001271 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001272
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001273 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001274 tmp = make_new_set(&PySet_Type, other);
1275 if (tmp == NULL)
1276 return NULL;
1277 result = set_issubset(so, tmp);
1278 Py_DECREF(tmp);
1279 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001280 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001281 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001282 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001283
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001284 while (set_next_internal(so, &pos, &key)) {
1285 if (!set_contains_internal((PySetObject *)other, key))
Raymond Hettingera690a992003-11-16 16:17:49 +00001286 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001287 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001288 Py_RETURN_TRUE;
1289}
1290
1291PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1292
1293static PyObject *
1294set_issuperset(PySetObject *so, PyObject *other)
1295{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001296 PyObject *tmp, *result;
1297
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001298 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001299 tmp = make_new_set(&PySet_Type, other);
1300 if (tmp == NULL)
1301 return NULL;
1302 result = set_issuperset(so, tmp);
1303 Py_DECREF(tmp);
1304 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001305 }
1306 return set_issubset((PySetObject *)other, (PyObject *)so);
1307}
1308
1309PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1310
1311static long
1312set_nohash(PyObject *self)
1313{
1314 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1315 return -1;
1316}
1317
1318static int
1319set_nocmp(PyObject *self)
1320{
1321 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1322 return -1;
1323}
1324
1325static long
1326frozenset_hash(PyObject *self)
1327{
Raymond Hettingera690a992003-11-16 16:17:49 +00001328 PySetObject *so = (PySetObject *)self;
Raymond Hettingera580c472005-08-05 17:19:54 +00001329 long h, hash = 1927868237L;
1330 setentry *entry;
1331 int i;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001332
Raymond Hettingera690a992003-11-16 16:17:49 +00001333 if (so->hash != -1)
1334 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001335
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001336 hash *= set_len(self) + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +00001337 entry = &so->table[0];
1338 for (i=so->used ; i ; entry++, i--) {
1339 while (entry->key == NULL || entry->key==dummy)
1340 entry++;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001341 /* Work to increase the bit dispersion for closely spaced hash
1342 values. The is important because some use cases have many
1343 combinations of a small number of elements with nearby
1344 hashes so that many distinct combinations collapse to only
1345 a handful of distinct hash values. */
Raymond Hettingera9d99362005-08-05 00:01:15 +00001346 h = entry->hash;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001347 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001348 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001349 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001350 if (hash == -1)
1351 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001352 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001353 return hash;
1354}
1355
1356static PyObject *
1357set_richcompare(PySetObject *v, PyObject *w, int op)
1358{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001359 PyObject *r1, *r2;
1360
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001361 if(!PyAnySet_Check(w)) {
1362 if (op == Py_EQ)
1363 Py_RETURN_FALSE;
1364 if (op == Py_NE)
1365 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001366 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1367 return NULL;
1368 }
1369 switch (op) {
1370 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001371 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001372 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001373 return set_issubset(v, w);
1374 case Py_NE:
1375 if (set_len((PyObject *)v) != set_len(w))
1376 Py_RETURN_TRUE;
1377 r1 = set_issubset(v, w);
1378 assert (r1 != NULL);
1379 r2 = PyBool_FromLong(PyObject_Not(r1));
1380 Py_DECREF(r1);
1381 return r2;
1382 case Py_LE:
1383 return set_issubset(v, w);
1384 case Py_GE:
1385 return set_issuperset(v, w);
1386 case Py_LT:
1387 if (set_len((PyObject *)v) >= set_len(w))
1388 Py_RETURN_FALSE;
1389 return set_issubset(v, w);
1390 case Py_GT:
1391 if (set_len((PyObject *)v) <= set_len(w))
1392 Py_RETURN_FALSE;
1393 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001394 }
1395 Py_INCREF(Py_NotImplemented);
1396 return Py_NotImplemented;
1397}
1398
1399static PyObject *
1400set_repr(PySetObject *so)
1401{
1402 PyObject *keys, *result, *listrepr;
1403
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001404 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001405 if (keys == NULL)
1406 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001407 listrepr = PyObject_Repr(keys);
1408 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001409 if (listrepr == NULL)
1410 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001411
1412 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1413 PyString_AS_STRING(listrepr));
1414 Py_DECREF(listrepr);
1415 return result;
1416}
1417
1418static int
1419set_tp_print(PySetObject *so, FILE *fp, int flags)
1420{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001421 PyObject *key;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001422 int pos=0;
1423 char *emit = ""; /* No separator emitted on first pass */
1424 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001425
Raymond Hettingera690a992003-11-16 16:17:49 +00001426 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001427 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001428 fputs(emit, fp);
1429 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001430 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001431 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001432 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001433 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001434 return 0;
1435}
1436
1437static PyObject *
1438set_clear(PySetObject *so)
1439{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001440 set_clear_internal(so);
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001441 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001442}
1443
1444PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1445
Raymond Hettingera690a992003-11-16 16:17:49 +00001446static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001447set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001448{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001449 if (set_add_internal(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001450 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001451 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001452}
1453
1454PyDoc_STRVAR(add_doc,
1455"Add an element to a set.\n\
1456\n\
1457This has no effect if the element is already present.");
1458
1459static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001460set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001461{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001462 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001463 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001464
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001465 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1466 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1467 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001468 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001469 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1470 result = set_remove(so, tmpkey);
1471 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1472 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001473 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001474 }
Raymond Hettinger0deab622003-12-13 18:53:18 +00001475
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001476 rv = set_discard_internal(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001477 if (rv == -1)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001478 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001479 else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001480 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001481 return NULL;
1482 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001483 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001484}
1485
1486PyDoc_STRVAR(remove_doc,
1487"Remove an element from a set; it must be a member.\n\
1488\n\
1489If the element is not a member, raise a KeyError.");
1490
1491static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001492set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001493{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001494 PyObject *tmpkey, *result;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001495
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001496 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1497 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1498 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001499 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001500 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1501 result = set_discard(so, tmpkey);
1502 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1503 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001504 return result;
1505 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001506
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001507 if (set_discard_internal(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001508 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001509 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001510}
1511
1512PyDoc_STRVAR(discard_doc,
1513"Remove an element from a set if it is a member.\n\
1514\n\
1515If the element is not a member, do nothing.");
1516
1517static PyObject *
1518set_pop(PySetObject *so)
1519{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001520 PyObject *key;
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001521 register setentry *entry;
1522 register int i = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001523
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001524 assert (PyAnySet_Check(so));
1525 if (so->used == 0) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001526 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1527 return NULL;
1528 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001529
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001530 /* Set entry to "the first" unused or dummy set entry. We abuse
1531 * the hash field of slot 0 to hold a search finger:
1532 * If slot 0 has a value, use slot 0.
1533 * Else slot 0 is being used to hold a search finger,
1534 * and we use its hash value as the first index to look.
1535 */
1536 entry = &so->table[0];
1537 if (entry->key == NULL || entry->key == dummy) {
1538 i = (int)entry->hash;
1539 /* The hash field may be a real hash value, or it may be a
1540 * legit search finger, or it may be a once-legit search
1541 * finger that's out of bounds now because it wrapped around
1542 * or the table shrunk -- simply make sure it's in bounds now.
1543 */
1544 if (i > so->mask || i < 1)
1545 i = 1; /* skip slot 0 */
1546 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
1547 i++;
1548 if (i > so->mask)
1549 i = 1;
1550 }
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001551 }
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001552 key = entry->key;
1553 Py_INCREF(dummy);
1554 entry->key = dummy;
1555 so->used--;
1556 so->table[0].hash = i + 1; /* next place to start */
Raymond Hettingera690a992003-11-16 16:17:49 +00001557 return key;
1558}
1559
1560PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1561
1562static PyObject *
1563set_reduce(PySetObject *so)
1564{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001565 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001566
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001567 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001568 if (keys == NULL)
1569 goto done;
1570 args = PyTuple_Pack(1, keys);
1571 if (args == NULL)
1572 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001573 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1574 if (dict == NULL) {
1575 PyErr_Clear();
1576 dict = Py_None;
1577 Py_INCREF(dict);
1578 }
1579 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001580done:
1581 Py_XDECREF(args);
1582 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001583 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001584 return result;
1585}
1586
1587PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1588
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001589static int
1590set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1591{
1592 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001593
1594 if (!PyAnySet_Check(self))
1595 return -1;
1596 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1597 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001598 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001599 self->hash = -1;
1600 if (iterable == NULL)
1601 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001602 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001603}
1604
Raymond Hettingera690a992003-11-16 16:17:49 +00001605static PySequenceMethods set_as_sequence = {
1606 (inquiry)set_len, /* sq_length */
1607 0, /* sq_concat */
1608 0, /* sq_repeat */
1609 0, /* sq_item */
1610 0, /* sq_slice */
1611 0, /* sq_ass_item */
1612 0, /* sq_ass_slice */
1613 (objobjproc)set_contains, /* sq_contains */
1614};
1615
1616/* set object ********************************************************/
1617
1618static PyMethodDef set_methods[] = {
1619 {"add", (PyCFunction)set_add, METH_O,
1620 add_doc},
1621 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1622 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001623 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001624 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001625 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1626 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001627 {"discard", (PyCFunction)set_discard, METH_O,
1628 discard_doc},
1629 {"difference", (PyCFunction)set_difference, METH_O,
1630 difference_doc},
1631 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1632 difference_update_doc},
1633 {"intersection",(PyCFunction)set_intersection, METH_O,
1634 intersection_doc},
1635 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1636 intersection_update_doc},
1637 {"issubset", (PyCFunction)set_issubset, METH_O,
1638 issubset_doc},
1639 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1640 issuperset_doc},
1641 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1642 pop_doc},
1643 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1644 reduce_doc},
1645 {"remove", (PyCFunction)set_remove, METH_O,
1646 remove_doc},
1647 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1648 symmetric_difference_doc},
1649 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1650 symmetric_difference_update_doc},
1651 {"union", (PyCFunction)set_union, METH_O,
1652 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001653 {"update", (PyCFunction)set_update, METH_O,
1654 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001655 {NULL, NULL} /* sentinel */
1656};
1657
1658static PyNumberMethods set_as_number = {
1659 0, /*nb_add*/
1660 (binaryfunc)set_sub, /*nb_subtract*/
1661 0, /*nb_multiply*/
1662 0, /*nb_divide*/
1663 0, /*nb_remainder*/
1664 0, /*nb_divmod*/
1665 0, /*nb_power*/
1666 0, /*nb_negative*/
1667 0, /*nb_positive*/
1668 0, /*nb_absolute*/
1669 0, /*nb_nonzero*/
1670 0, /*nb_invert*/
1671 0, /*nb_lshift*/
1672 0, /*nb_rshift*/
1673 (binaryfunc)set_and, /*nb_and*/
1674 (binaryfunc)set_xor, /*nb_xor*/
1675 (binaryfunc)set_or, /*nb_or*/
1676 0, /*nb_coerce*/
1677 0, /*nb_int*/
1678 0, /*nb_long*/
1679 0, /*nb_float*/
1680 0, /*nb_oct*/
1681 0, /*nb_hex*/
1682 0, /*nb_inplace_add*/
1683 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1684 0, /*nb_inplace_multiply*/
1685 0, /*nb_inplace_divide*/
1686 0, /*nb_inplace_remainder*/
1687 0, /*nb_inplace_power*/
1688 0, /*nb_inplace_lshift*/
1689 0, /*nb_inplace_rshift*/
1690 (binaryfunc)set_iand, /*nb_inplace_and*/
1691 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1692 (binaryfunc)set_ior, /*nb_inplace_or*/
1693};
1694
1695PyDoc_STRVAR(set_doc,
1696"set(iterable) --> set object\n\
1697\n\
1698Build an unordered collection.");
1699
1700PyTypeObject PySet_Type = {
1701 PyObject_HEAD_INIT(&PyType_Type)
1702 0, /* ob_size */
1703 "set", /* tp_name */
1704 sizeof(PySetObject), /* tp_basicsize */
1705 0, /* tp_itemsize */
1706 /* methods */
1707 (destructor)set_dealloc, /* tp_dealloc */
1708 (printfunc)set_tp_print, /* tp_print */
1709 0, /* tp_getattr */
1710 0, /* tp_setattr */
1711 (cmpfunc)set_nocmp, /* tp_compare */
1712 (reprfunc)set_repr, /* tp_repr */
1713 &set_as_number, /* tp_as_number */
1714 &set_as_sequence, /* tp_as_sequence */
1715 0, /* tp_as_mapping */
1716 set_nohash, /* tp_hash */
1717 0, /* tp_call */
1718 0, /* tp_str */
1719 PyObject_GenericGetAttr, /* tp_getattro */
1720 0, /* tp_setattro */
1721 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001722 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001723 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001724 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001725 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001726 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001727 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001728 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001729 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001730 0, /* tp_iternext */
1731 set_methods, /* tp_methods */
1732 0, /* tp_members */
1733 0, /* tp_getset */
1734 0, /* tp_base */
1735 0, /* tp_dict */
1736 0, /* tp_descr_get */
1737 0, /* tp_descr_set */
1738 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001739 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001740 PyType_GenericAlloc, /* tp_alloc */
1741 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001742 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001743};
1744
1745/* frozenset object ********************************************************/
1746
1747
1748static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001749 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001750 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001751 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001752 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001753 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001754 difference_doc},
1755 {"intersection",(PyCFunction)set_intersection, METH_O,
1756 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001757 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001758 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001759 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001760 issuperset_doc},
1761 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1762 reduce_doc},
1763 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1764 symmetric_difference_doc},
1765 {"union", (PyCFunction)set_union, METH_O,
1766 union_doc},
1767 {NULL, NULL} /* sentinel */
1768};
1769
1770static PyNumberMethods frozenset_as_number = {
1771 0, /*nb_add*/
1772 (binaryfunc)set_sub, /*nb_subtract*/
1773 0, /*nb_multiply*/
1774 0, /*nb_divide*/
1775 0, /*nb_remainder*/
1776 0, /*nb_divmod*/
1777 0, /*nb_power*/
1778 0, /*nb_negative*/
1779 0, /*nb_positive*/
1780 0, /*nb_absolute*/
1781 0, /*nb_nonzero*/
1782 0, /*nb_invert*/
1783 0, /*nb_lshift*/
1784 0, /*nb_rshift*/
1785 (binaryfunc)set_and, /*nb_and*/
1786 (binaryfunc)set_xor, /*nb_xor*/
1787 (binaryfunc)set_or, /*nb_or*/
1788};
1789
1790PyDoc_STRVAR(frozenset_doc,
1791"frozenset(iterable) --> frozenset object\n\
1792\n\
1793Build an immutable unordered collection.");
1794
1795PyTypeObject PyFrozenSet_Type = {
1796 PyObject_HEAD_INIT(&PyType_Type)
1797 0, /* ob_size */
1798 "frozenset", /* tp_name */
1799 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001800 0, /* tp_itemsize */
1801 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001802 (destructor)set_dealloc, /* tp_dealloc */
1803 (printfunc)set_tp_print, /* tp_print */
1804 0, /* tp_getattr */
1805 0, /* tp_setattr */
1806 (cmpfunc)set_nocmp, /* tp_compare */
1807 (reprfunc)set_repr, /* tp_repr */
1808 &frozenset_as_number, /* tp_as_number */
1809 &set_as_sequence, /* tp_as_sequence */
1810 0, /* tp_as_mapping */
1811 frozenset_hash, /* tp_hash */
1812 0, /* tp_call */
1813 0, /* tp_str */
1814 PyObject_GenericGetAttr, /* tp_getattro */
1815 0, /* tp_setattro */
1816 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001817 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001818 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001819 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001820 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001821 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001822 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001823 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001824 (getiterfunc)set_iter, /* tp_iter */
1825 0, /* tp_iternext */
1826 frozenset_methods, /* tp_methods */
1827 0, /* tp_members */
1828 0, /* tp_getset */
1829 0, /* tp_base */
1830 0, /* tp_dict */
1831 0, /* tp_descr_get */
1832 0, /* tp_descr_set */
1833 0, /* tp_dictoffset */
1834 0, /* tp_init */
1835 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001836 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001837 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001838};