blob: 54b1c28b05ab8924f5872c43dfff0e4a7dd9870b [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
Raymond Hettingerbc841a12005-08-07 13:02:53 +000018#define INIT_NONZERO_SET_SLOTS(so) do { \
19 (so)->table = (so)->smalltable; \
20 (so)->mask = PySet_MINSIZE - 1; \
21 (so)->hash = -1; \
22 } while(0)
23
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000024#define EMPTY_TO_MINSIZE(so) do { \
25 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
26 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000027 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028 } while(0)
29
Raymond Hettingerbc841a12005-08-07 13:02:53 +000030/* Reuse scheme to save calls to malloc, free, and memset */
31#define MAXFREESETS 80
32static PySetObject *free_sets[MAXFREESETS];
33static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034
35/*
36The basic lookup function used by all operations.
37This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
38Open addressing is preferred over chaining since the link overhead for
39chaining would be substantial (100% with typical malloc overhead).
40
41The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000042probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000043
44All arithmetic on hash should ignore overflow.
45
Raymond Hettingerbc841a12005-08-07 13:02:53 +000046The lookup function always succeeds and nevers return NULL. This simplifies
47and speeds client functions which do won't have to test for and handle
48errors. To meet that requirement, any errors generated by a user defined
49__cmp__() function are simply cleared and ignored.
50Previously outstanding exceptions are maintained.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051*/
52
53static setentry *
54set_lookkey(PySetObject *so, PyObject *key, register long hash)
55{
56 register int i;
57 register unsigned int perturb;
58 register setentry *freeslot;
59 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000060 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000061 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062 register int restore_error;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000063 register int cmp;
64 PyObject *err_type, *err_value, *err_tb;
65 PyObject *startkey;
66
67 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000068 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000069 if (entry->key == NULL || entry->key == key)
70 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000071
Raymond Hettinger99220fa2005-08-06 18:57:13 +000072 restore_error = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000073 if (entry->key == dummy)
74 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000076 if (entry->hash == hash) {
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +000077 if (_PyErr_OCCURRED()) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000078 restore_error = 1;
79 PyErr_Fetch(&err_type, &err_value, &err_tb);
80 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000081 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
83 if (cmp < 0)
84 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +000085 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086 if (cmp > 0)
87 goto Done;
88 }
89 else {
90 /* The compare did major nasty stuff to the
91 * set: start over.
92 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000093 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000094 goto Done;
95 }
96 }
97 freeslot = NULL;
98 }
99
100 /* In the loop, key == dummy is by far (factor of 100s) the
101 least likely outcome, so test for that last. */
102 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
103 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000104 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000105 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000106 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000107 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000108 break;
109 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000110 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000111 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000112 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger99220fa2005-08-06 18:57:13 +0000113 if (_PyErr_OCCURRED()) {
114 restore_error = 1;
115 PyErr_Fetch(&err_type, &err_value,
116 &err_tb);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000117 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000118 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000119 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
120 if (cmp < 0)
121 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +0000122 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000123 if (cmp > 0)
124 break;
125 }
126 else {
127 /* The compare did major nasty stuff to the
128 * set: start over.
129 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000130 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000131 break;
132 }
133 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000134 else if (entry->key == dummy && freeslot == NULL)
135 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000136 }
137
138Done:
139 if (restore_error)
140 PyErr_Restore(err_type, err_value, err_tb);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000141 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000142}
143
144/*
145 * Hacked up version of set_lookkey which can assume keys are always strings;
146 * this assumption allows testing for errors during PyObject_Compare() to
147 * be dropped; string-string comparisons never raise exceptions. This also
148 * means we don't need to go through PyObject_Compare(); we can always use
149 * _PyString_Eq directly.
150 *
151 * This is valuable because the general-case error handling in set_lookkey() is
152 * expensive, and sets with pure-string keys may be very common.
153 */
154static setentry *
155set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
156{
157 register int i;
158 register unsigned int perturb;
159 register setentry *freeslot;
160 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000161 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000162 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000163
164 /* Make sure this function doesn't have to handle non-string keys,
165 including subclasses of str; e.g., one reason to subclass
166 strings is to override __eq__, and for speed we don't cater to
167 that here. */
168 if (!PyString_CheckExact(key)) {
169 so->lookup = set_lookkey;
170 return set_lookkey(so, key, hash);
171 }
172 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000173 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000174 if (entry->key == NULL || entry->key == key)
175 return entry;
Raymond Hettingera580c472005-08-05 17:19:54 +0000176 if (so->fill != so->used) {
177 if (entry->key == dummy)
178 freeslot = entry;
179 else {
180 if (entry->hash == hash && _PyString_Eq(entry->key, key))
181 return entry;
182 freeslot = NULL;
183 }
184
185 /* In the loop, key == dummy is by far (factor of 100s) the
186 least likely outcome, so test for that last. */
187 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
188 i = (i << 2) + i + perturb + 1;
189 entry = &table[i & mask];
190 if (entry->key == NULL)
191 return freeslot == NULL ? entry : freeslot;
192 if (entry->key == key
193 || (entry->hash == hash
194 && entry->key != dummy
195 && _PyString_Eq(entry->key, key)))
196 return entry;
197 if (entry->key == dummy && freeslot == NULL)
198 freeslot = entry;
199 }
200 } else {
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000201 /* Simplified loop when there are no dummy entries. */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000202 if (entry->hash == hash && _PyString_Eq(entry->key, key))
203 return entry;
Raymond Hettingera580c472005-08-05 17:19:54 +0000204 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
205 i = (i << 2) + i + perturb + 1;
206 entry = &table[i & mask];
207 if (entry->key == NULL)
208 return entry;
209 if (entry->key == key
210 || (entry->hash == hash
211 && _PyString_Eq(entry->key, key)))
212 return entry;
213 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000214 }
215}
216
217/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000218Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000219Used both by the internal resize routine and by the public insert routine.
220Eats a reference to key.
221*/
222static void
223set_insert_key(register PySetObject *so, PyObject *key, long hash)
224{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000225 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000226 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
227
228 assert(so->lookup != NULL);
229
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000230 entry = so->lookup(so, key, hash);
231 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000232 /* UNUSED */
233 so->fill++;
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++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000237 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000238 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000239 entry->key = key;
240 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000241 so->used++;
242 Py_DECREF(dummy);
243 } else {
244 /* ACTIVE */
245 Py_DECREF(key);
246 }
247}
248
249/*
250Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000251keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000252actually be smaller than the old one.
253*/
254static int
255set_table_resize(PySetObject *so, int minused)
256{
257 int newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000258 setentry *oldtable, *newtable, *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000259 int i;
260 int is_oldtable_malloced;
261 setentry small_copy[PySet_MINSIZE];
262
263 assert(minused >= 0);
264
265 /* Find the smallest table size > minused. */
266 for (newsize = PySet_MINSIZE;
267 newsize <= minused && newsize > 0;
268 newsize <<= 1)
269 ;
270 if (newsize <= 0) {
271 PyErr_NoMemory();
272 return -1;
273 }
274
275 /* Get space for a new table. */
276 oldtable = so->table;
277 assert(oldtable != NULL);
278 is_oldtable_malloced = oldtable != so->smalltable;
279
280 if (newsize == PySet_MINSIZE) {
281 /* A large table is shrinking, or we can't get any smaller. */
282 newtable = so->smalltable;
283 if (newtable == oldtable) {
284 if (so->fill == so->used) {
285 /* No dummies, so no point doing anything. */
286 return 0;
287 }
288 /* We're not going to resize it, but rebuild the
289 table anyway to purge old dummy entries.
290 Subtle: This is *necessary* if fill==size,
291 as set_lookkey needs at least one virgin slot to
292 terminate failing searches. If fill < size, it's
293 merely desirable, as dummies slow searches. */
294 assert(so->fill > so->used);
295 memcpy(small_copy, oldtable, sizeof(small_copy));
296 oldtable = small_copy;
297 }
298 }
299 else {
300 newtable = PyMem_NEW(setentry, newsize);
301 if (newtable == NULL) {
302 PyErr_NoMemory();
303 return -1;
304 }
305 }
306
307 /* Make the set empty, using the new table. */
308 assert(newtable != oldtable);
309 so->table = newtable;
310 so->mask = newsize - 1;
311 memset(newtable, 0, sizeof(setentry) * newsize);
312 so->used = 0;
313 i = so->fill;
314 so->fill = 0;
315
316 /* Copy the data over; this is refcount-neutral for active entries;
317 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000318 for (entry = oldtable; i > 0; entry++) {
319 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000320 /* UNUSED */
321 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000322 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323 /* DUMMY */
324 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000325 assert(entry->key == dummy);
326 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000327 } else {
328 /* ACTIVE */
329 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000330 set_insert_key(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331 }
332 }
333
334 if (is_oldtable_malloced)
335 PyMem_DEL(oldtable);
336 return 0;
337}
338
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000339/* CAUTION: set_add_internal() must guarantee that it won't resize the table */
340static int
341set_add_internal(register PySetObject *so, PyObject *key)
342{
343 register long hash;
344 register int n_used;
345
346 if (PyString_CheckExact(key)) {
347 hash = ((PyStringObject *)key)->ob_shash;
348 if (hash == -1)
349 hash = PyObject_Hash(key);
350 } else {
351 hash = PyObject_Hash(key);
352 if (hash == -1)
353 return -1;
354 }
355 assert(so->fill <= so->mask); /* at least one empty slot */
356 n_used = so->used;
357 Py_INCREF(key);
358 set_insert_key(so, key, hash);
359 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
360 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000361 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000362}
363
364#define DISCARD_NOTFOUND 0
365#define DISCARD_FOUND 1
366
367static int
368set_discard_internal(PySetObject *so, PyObject *key)
369{
370 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000371 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000372 PyObject *old_key;
373
374 assert (PyAnySet_Check(so));
375 if (!PyString_CheckExact(key) ||
376 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
377 hash = PyObject_Hash(key);
378 if (hash == -1)
379 return -1;
380 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000381 entry = (so->lookup)(so, key, hash);
382 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000383 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000384 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000385 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000386 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000387 so->used--;
388 Py_DECREF(old_key);
389 return DISCARD_FOUND;
390}
391
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000392static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000393set_clear_internal(PySetObject *so)
394{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000395 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000396 int table_is_malloced;
397 int fill;
398 setentry small_copy[PySet_MINSIZE];
399#ifdef Py_DEBUG
400 int i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000401 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000402
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000403 n = so->mask + 1;
404 i = 0;
405#endif
406
407 table = so->table;
408 assert(table != NULL);
409 table_is_malloced = table != so->smalltable;
410
411 /* This is delicate. During the process of clearing the set,
412 * decrefs can cause the set to mutate. To avoid fatal confusion
413 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000414 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000415 * clearing.
416 */
417 fill = so->fill;
418 if (table_is_malloced)
419 EMPTY_TO_MINSIZE(so);
420
421 else if (fill > 0) {
422 /* It's a small table with something that needs to be cleared.
423 * Afraid the only safe way is to copy the set entries into
424 * another small table first.
425 */
426 memcpy(small_copy, table, sizeof(small_copy));
427 table = small_copy;
428 EMPTY_TO_MINSIZE(so);
429 }
430 /* else it's a small table that's already empty */
431
432 /* Now we can finally clear things. If C had refcounts, we could
433 * assert that the refcount on table is 1 now, i.e. that this function
434 * has unique access to it, so decref side-effects can't alter it.
435 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437#ifdef Py_DEBUG
438 assert(i < n);
439 ++i;
440#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000441 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000442 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000443 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000444 }
445#ifdef Py_DEBUG
446 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000447 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448#endif
449 }
450
451 if (table_is_malloced)
452 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000453 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454}
455
456/*
457 * Iterate over a set table. Use like so:
458 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000459 * int pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000460 * PyObject *key;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000461 * pos = 0; # important! pos should not otherwise be changed by you
462 * while (set_next_internal(yourset, &pos, &key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000463 * Refer to borrowed reference in key.
464 * }
465 *
466 * CAUTION: In general, it isn't safe to use set_next_internal in a loop that
467 * mutates the table.
468 */
469static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000470set_next_internal(PySetObject *so, int *pos, PyObject **key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000471{
472 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000473 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000474
475 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000476 i = *pos;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000477 if (i < 0)
478 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000479 entry = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480 mask = so->mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000481 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482 i++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000483 *pos = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484 if (i > mask)
485 return 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000486 if (key)
487 *key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000488 return 1;
489}
490
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491static int
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000492set_merge_internal(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000494 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495 register int i;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000496 register setentry *entry, *othertable;
497 register int othermask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498
499 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000500 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000502 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503 if (other == so || other->used == 0)
504 /* a.update(a) or a.update({}); nothing to do */
505 return 0;
506 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000507 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508 * that there will be no (or few) overlapping keys.
509 */
510 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
511 if (set_table_resize(so, (so->used + other->used)*2) != 0)
512 return -1;
513 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000514 othermask = other->mask;
515 othertable = other->table;
516 for (i = 0; i <= othermask; i++) {
517 entry = &othertable[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000518 if (entry->key != NULL &&
519 entry->key != dummy) {
520 Py_INCREF(entry->key);
521 set_insert_key(so, entry->key, entry->hash);
522 }
523 }
524 return 0;
525}
526
527static int
528set_contains_internal(PySetObject *so, PyObject *key)
529{
530 long hash;
531
532 if (!PyString_CheckExact(key) ||
533 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
534 hash = PyObject_Hash(key);
535 if (hash == -1)
536 return -1;
537 }
538 key = (so->lookup)(so, key, hash)->key;
539 return key != NULL && key != dummy;
540}
541
Raymond Hettingera9d99362005-08-05 00:01:15 +0000542/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543
Raymond Hettingerd7946662005-08-01 21:39:29 +0000544static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545
546typedef struct {
547 PyObject_HEAD
548 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
549 int si_used;
550 int si_pos;
551 long len;
552} setiterobject;
553
554static PyObject *
555set_iter(PySetObject *so)
556{
557 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
558 if (si == NULL)
559 return NULL;
560 Py_INCREF(so);
561 si->si_set = so;
562 si->si_used = so->used;
563 si->si_pos = 0;
564 si->len = so->used;
565 return (PyObject *)si;
566}
567
568static void
569setiter_dealloc(setiterobject *si)
570{
571 Py_XDECREF(si->si_set);
572 PyObject_Del(si);
573}
574
575static int
576setiter_len(setiterobject *si)
577{
578 if (si->si_set != NULL && si->si_used == si->si_set->used)
579 return si->len;
580 return 0;
581}
582
583static PySequenceMethods setiter_as_sequence = {
584 (inquiry)setiter_len, /* sq_length */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000585};
586
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000587static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000588{
589 PyObject *key;
590 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000591 register setentry *entry;
592 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000594 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000595 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000596 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000597
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000598 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000599 PyErr_SetString(PyExc_RuntimeError,
600 "Set changed size during iteration");
601 si->si_used = -1; /* Make this state sticky */
602 return NULL;
603 }
604
605 i = si->si_pos;
606 if (i < 0)
607 goto fail;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000608 entry = so->table;
609 mask = so->mask;
610 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000611 i++;
612 si->si_pos = i+1;
613 if (i > mask)
614 goto fail;
615 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000616 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000617 Py_INCREF(key);
618 return key;
619
620fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000621 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000622 si->si_set = NULL;
623 return NULL;
624}
625
Hye-Shik Change2956762005-08-01 05:26:41 +0000626static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000627 PyObject_HEAD_INIT(&PyType_Type)
628 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000629 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000630 sizeof(setiterobject), /* tp_basicsize */
631 0, /* tp_itemsize */
632 /* methods */
633 (destructor)setiter_dealloc, /* tp_dealloc */
634 0, /* tp_print */
635 0, /* tp_getattr */
636 0, /* tp_setattr */
637 0, /* tp_compare */
638 0, /* tp_repr */
639 0, /* tp_as_number */
640 &setiter_as_sequence, /* tp_as_sequence */
641 0, /* tp_as_mapping */
642 0, /* tp_hash */
643 0, /* tp_call */
644 0, /* tp_str */
645 PyObject_GenericGetAttr, /* tp_getattro */
646 0, /* tp_setattro */
647 0, /* tp_as_buffer */
648 Py_TPFLAGS_DEFAULT, /* tp_flags */
649 0, /* tp_doc */
650 0, /* tp_traverse */
651 0, /* tp_clear */
652 0, /* tp_richcompare */
653 0, /* tp_weaklistoffset */
654 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000655 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000656};
657
Raymond Hettingerd7946662005-08-01 21:39:29 +0000658static int
659set_len(PyObject *so)
660{
661 return ((PySetObject *)so)->used;
662}
663
664static int
665set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000666{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000667 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000668
Raymond Hettingerd7946662005-08-01 21:39:29 +0000669 if (PyAnySet_Check(other))
670 return set_merge_internal(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000671
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000672 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000673 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000674 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000675 while (PyDict_Next(other, &pos, &key, &value)) {
676 if (set_add_internal(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000677 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000678 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000679 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000680 }
681
Raymond Hettingera38123e2003-11-24 22:18:49 +0000682 it = PyObject_GetIter(other);
683 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000684 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000685
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000686 while ((key = PyIter_Next(it)) != NULL) {
687 if (set_add_internal(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000688 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000689 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000690 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000691 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000692 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000693 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000694 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000695 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000696 return -1;
697 return 0;
698}
699
700static PyObject *
701set_update(PySetObject *so, PyObject *other)
702{
703 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000704 return NULL;
705 Py_RETURN_NONE;
706}
707
708PyDoc_STRVAR(update_doc,
709"Update a set with the union of itself and another.");
710
711static PyObject *
712make_new_set(PyTypeObject *type, PyObject *iterable)
713{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000714 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000715
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000716 if (dummy == NULL) { /* Auto-initialize dummy */
717 dummy = PyString_FromString("<dummy key>");
718 if (dummy == NULL)
719 return NULL;
720 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000721
722 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000723 if (num_free_sets &&
724 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
725 so = free_sets[--num_free_sets];
726 assert (so != NULL && PyAnySet_CheckExact(so));
727 so->ob_type = type;
728 _Py_NewReference((PyObject *)so);
729 EMPTY_TO_MINSIZE(so);
730 PyObject_GC_Track(so);
731 } else {
732 so = (PySetObject *)type->tp_alloc(type, 0);
733 if (so == NULL)
734 return NULL;
735 /* tp_alloc has already zeroed the structure */
736 assert(so->table == NULL && so->fill == 0 && so->used == 0);
737 INIT_NONZERO_SET_SLOTS(so);
738 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000739
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000740 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000741 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000742
Raymond Hettingera38123e2003-11-24 22:18:49 +0000743 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000744 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000745 Py_DECREF(so);
746 return NULL;
747 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000748 }
749
Raymond Hettingera690a992003-11-16 16:17:49 +0000750 return (PyObject *)so;
751}
752
Raymond Hettingerd7946662005-08-01 21:39:29 +0000753/* The empty frozenset is a singleton */
754static PyObject *emptyfrozenset = NULL;
755
Raymond Hettingera690a992003-11-16 16:17:49 +0000756static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000757frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000758{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000759 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000760
761 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
762 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000763
764 if (type != &PyFrozenSet_Type)
765 return make_new_set(type, iterable);
766
767 if (iterable != NULL) {
768 /* frozenset(f) is idempotent */
769 if (PyFrozenSet_CheckExact(iterable)) {
770 Py_INCREF(iterable);
771 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000772 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000773 result = make_new_set(type, iterable);
774 if (result == NULL || set_len(result))
775 return result;
776 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000777 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000778 /* The empty frozenset is a singleton */
779 if (emptyfrozenset == NULL)
780 emptyfrozenset = make_new_set(type, NULL);
781 Py_XINCREF(emptyfrozenset);
782 return emptyfrozenset;
783}
784
785void
786PySet_Fini(void)
787{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000788 PySetObject *so;
789
790 while (num_free_sets) {
791 num_free_sets--;
792 so = free_sets[num_free_sets];
793 PyObject_GC_Del(so);
794 }
Raymond Hettingera9d99362005-08-05 00:01:15 +0000795 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000796 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000797}
798
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000799static PyObject *
800set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
801{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000802 return make_new_set(type, NULL);
803}
804
Raymond Hettingera690a992003-11-16 16:17:49 +0000805static void
806set_dealloc(PySetObject *so)
807{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000808 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809 int fill = so->fill;
810
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000811 PyObject_GC_UnTrack(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812 Py_TRASHCAN_SAFE_BEGIN(so)
Raymond Hettinger691d8052004-05-30 07:26:47 +0000813 if (so->weakreflist != NULL)
814 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000816 for (entry = so->table; fill > 0; entry++) {
817 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000819 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000820 }
821 }
822 if (so->table != so->smalltable)
823 PyMem_DEL(so->table);
824
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000825 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
826 free_sets[num_free_sets++] = so;
827 else
828 so->ob_type->tp_free(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000830}
831
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000832static int
833set_traverse(PySetObject *so, visitproc visit, void *arg)
834{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000835 int pos = 0;
836 PyObject *key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000838 while (set_next_internal(so, &pos, &key))
839 Py_VISIT(key);
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000840 return 0;
841}
842
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000843/* set_swap_bodies() switches the contents of any two sets by moving their
844 internal data pointers and, if needed, copying the internal smalltables.
845 Semantically equivalent to:
846
847 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
848
849 The function always succeeds and it leaves both objects in a stable state.
850 Useful for creating temporary frozensets from sets for membership testing
851 in __contains__(), discard(), and remove(). Also useful for operations
852 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000853 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000854*/
855
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856static void
857set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000858{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000859 int t;
860 setentry *u;
861 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
862 setentry tab[PySet_MINSIZE];
863 long h;
864
865 t = a->fill; a->fill = b->fill; b->fill = t;
866 t = a->used; a->used = b->used; b->used = t;
867 t = a->mask; a->mask = b->mask; b->mask = t;
868
869 u = a->table;
870 if (a->table == a->smalltable)
871 u = b->smalltable;
872 a->table = b->table;
873 if (b->table == b->smalltable)
874 a->table = a->smalltable;
875 b->table = u;
876
877 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
878
879 if (a->table == a->smalltable || b->table == b->smalltable) {
880 memcpy(tab, a->smalltable, sizeof(tab));
881 memcpy(a->smalltable, b->smalltable, sizeof(tab));
882 memcpy(b->smalltable, tab, sizeof(tab));
883 }
884
Raymond Hettingera580c472005-08-05 17:19:54 +0000885 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
886 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
887 h = a->hash; a->hash = b->hash; b->hash = h;
888 } else {
889 a->hash = -1;
890 b->hash = -1;
891 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000892}
893
894static int
895set_contains(PySetObject *so, PyObject *key)
896{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000897 PyObject *tmpkey;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000898 int result;
899
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000900 result = set_contains_internal(so, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000901 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000902 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000903 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
904 if (tmpkey == NULL)
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000905 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000906 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
907 result = set_contains_internal(so, tmpkey);
908 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
909 Py_DECREF(tmpkey);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000910 }
911 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000912}
913
914static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000915set_direct_contains(PySetObject *so, PyObject *key)
916{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000917 long result;
918
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000919 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000920 if (result == -1)
921 return NULL;
922 return PyBool_FromLong(result);
923}
924
925PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
926
927static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000928set_copy(PySetObject *so)
929{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000930 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000931}
932
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000933static PyObject *
934frozenset_copy(PySetObject *so)
935{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000936 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000937 Py_INCREF(so);
938 return (PyObject *)so;
939 }
940 return set_copy(so);
941}
942
Raymond Hettingera690a992003-11-16 16:17:49 +0000943PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
944
945static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000946set_union(PySetObject *so, PyObject *other)
947{
948 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000949
950 result = (PySetObject *)set_copy(so);
951 if (result == NULL)
952 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000953 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000954 Py_DECREF(result);
955 return NULL;
956 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000957 return (PyObject *)result;
958}
959
960PyDoc_STRVAR(union_doc,
961 "Return the union of two sets as a new set.\n\
962\n\
963(i.e. all elements that are in either set.)");
964
965static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000966set_or(PySetObject *so, PyObject *other)
967{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000968 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000969 Py_INCREF(Py_NotImplemented);
970 return Py_NotImplemented;
971 }
972 return set_union(so, other);
973}
974
975static PyObject *
976set_ior(PySetObject *so, PyObject *other)
977{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000978 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000979 Py_INCREF(Py_NotImplemented);
980 return Py_NotImplemented;
981 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000982 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +0000983 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000984 Py_INCREF(so);
985 return (PyObject *)so;
986}
987
988static PyObject *
989set_intersection(PySetObject *so, PyObject *other)
990{
991 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000992 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000993
994 result = (PySetObject *)make_new_set(so->ob_type, NULL);
995 if (result == NULL)
996 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000997
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000998 if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) {
999 tmp = (PyObject *)so;
1000 so = (PySetObject *)other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001001 other = tmp;
1002 }
1003
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001004 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001005 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001006 while (set_next_internal((PySetObject *)other, &pos, &key)) {
1007 if (set_contains_internal(so, key)) {
1008 if (set_add_internal(result, key) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001009 Py_DECREF(result);
1010 return NULL;
1011 }
1012 }
1013 }
1014 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001015 }
1016
Raymond Hettingera690a992003-11-16 16:17:49 +00001017 it = PyObject_GetIter(other);
1018 if (it == NULL) {
1019 Py_DECREF(result);
1020 return NULL;
1021 }
1022
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001023 while ((key = PyIter_Next(it)) != NULL) {
1024 if (set_contains_internal(so, key)) {
1025 if (set_add_internal(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001026 Py_DECREF(it);
1027 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001028 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001029 return NULL;
1030 }
1031 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001032 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001033 }
1034 Py_DECREF(it);
1035 if (PyErr_Occurred()) {
1036 Py_DECREF(result);
1037 return NULL;
1038 }
1039 return (PyObject *)result;
1040}
1041
1042PyDoc_STRVAR(intersection_doc,
1043"Return the intersection of two sets as a new set.\n\
1044\n\
1045(i.e. all elements that are in both sets.)");
1046
1047static PyObject *
1048set_intersection_update(PySetObject *so, PyObject *other)
1049{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001050 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001051
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001052 tmp = set_intersection(so, other);
1053 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001054 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001055 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001056 Py_DECREF(tmp);
1057 Py_RETURN_NONE;
1058}
1059
1060PyDoc_STRVAR(intersection_update_doc,
1061"Update a set with the intersection of itself and another.");
1062
1063static PyObject *
1064set_and(PySetObject *so, PyObject *other)
1065{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001066 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001067 Py_INCREF(Py_NotImplemented);
1068 return Py_NotImplemented;
1069 }
1070 return set_intersection(so, other);
1071}
1072
1073static PyObject *
1074set_iand(PySetObject *so, PyObject *other)
1075{
1076 PyObject *result;
1077
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001078 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001079 Py_INCREF(Py_NotImplemented);
1080 return Py_NotImplemented;
1081 }
1082 result = set_intersection_update(so, other);
1083 if (result == NULL)
1084 return NULL;
1085 Py_DECREF(result);
1086 Py_INCREF(so);
1087 return (PyObject *)so;
1088}
1089
1090static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001091set_difference_update(PySetObject *so, PyObject *other)
1092{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001093 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001094
1095 it = PyObject_GetIter(other);
1096 if (it == NULL)
1097 return NULL;
1098
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001099 while ((key = PyIter_Next(it)) != NULL) {
1100 if (set_discard_internal(so, key) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001101 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001102 Py_DECREF(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001103 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001104 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001105 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001106 }
1107 Py_DECREF(it);
1108 if (PyErr_Occurred())
1109 return NULL;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001110 /* If more than 1/6 are dummies, then resize them away. */
1111 if ((so->fill - so->used) * 6 < so->mask)
1112 Py_RETURN_NONE;
1113 if (set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4) == -1)
1114 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001115 Py_RETURN_NONE;
1116}
1117
1118PyDoc_STRVAR(difference_update_doc,
1119"Remove all elements of another set from this set.");
1120
1121static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001122set_difference(PySetObject *so, PyObject *other)
1123{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001124 PyObject *tmp, *key, *result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001125 int pos = 0;
1126
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001127 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001128 result = set_copy(so);
1129 if (result == NULL)
1130 return result;
1131 tmp = set_difference_update((PySetObject *)result, other);
1132 if (tmp != NULL) {
1133 Py_DECREF(tmp);
1134 return result;
1135 }
1136 Py_DECREF(result);
1137 return NULL;
1138 }
1139
1140 result = make_new_set(so->ob_type, NULL);
1141 if (result == NULL)
1142 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001143
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001144 if (PyDict_Check(other)) {
1145 while (set_next_internal(so, &pos, &key)) {
1146 if (!PyDict_Contains(other, key)) {
1147 if (set_add_internal((PySetObject *)result, key) == -1)
1148 return NULL;
1149 }
1150 }
1151 return result;
1152 }
1153
1154 while (set_next_internal(so, &pos, &key)) {
1155 if (!set_contains_internal((PySetObject *)other, key)) {
1156 if (set_add_internal((PySetObject *)result, key) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001157 return NULL;
1158 }
1159 }
1160 return result;
1161}
1162
1163PyDoc_STRVAR(difference_doc,
1164"Return the difference of two sets as a new set.\n\
1165\n\
1166(i.e. all elements that are in this set but not the other.)");
1167static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001168set_sub(PySetObject *so, PyObject *other)
1169{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001170 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001171 Py_INCREF(Py_NotImplemented);
1172 return Py_NotImplemented;
1173 }
1174 return set_difference(so, other);
1175}
1176
1177static PyObject *
1178set_isub(PySetObject *so, PyObject *other)
1179{
1180 PyObject *result;
1181
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001182 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001183 Py_INCREF(Py_NotImplemented);
1184 return Py_NotImplemented;
1185 }
1186 result = set_difference_update(so, other);
1187 if (result == NULL)
1188 return NULL;
1189 Py_DECREF(result);
1190 Py_INCREF(so);
1191 return (PyObject *)so;
1192}
1193
1194static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001195set_symmetric_difference_update(PySetObject *so, PyObject *other)
1196{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001197 PySetObject *otherset;
1198 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001199 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001200
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001201 if (PyDict_Check(other)) {
1202 PyObject *value;
1203 int rv;
1204 while (PyDict_Next(other, &pos, &key, &value)) {
1205 rv = set_discard_internal(so, key);
1206 if (rv == -1)
1207 return NULL;
1208 if (rv == DISCARD_NOTFOUND) {
1209 if (set_add_internal(so, key) == -1)
1210 return NULL;
1211 }
1212 }
1213 Py_RETURN_NONE;
1214 }
1215
1216 if (PyAnySet_Check(other)) {
1217 Py_INCREF(other);
1218 otherset = (PySetObject *)other;
1219 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001220 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1221 if (otherset == NULL)
1222 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001223 }
1224
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001225 while (set_next_internal(otherset, &pos, &key)) {
1226 int rv = set_discard_internal(so, key);
1227 if (rv == -1) {
1228 Py_XDECREF(otherset);
1229 return NULL;
1230 }
1231 if (rv == DISCARD_NOTFOUND) {
1232 if (set_add_internal(so, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001233 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001234 return NULL;
1235 }
1236 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001237 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001238 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001239 Py_RETURN_NONE;
1240}
1241
1242PyDoc_STRVAR(symmetric_difference_update_doc,
1243"Update a set with the symmetric difference of itself and another.");
1244
1245static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001246set_symmetric_difference(PySetObject *so, PyObject *other)
1247{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001248 PyObject *rv;
1249 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001250
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001251 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1252 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001253 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001254 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1255 if (rv == NULL)
1256 return NULL;
1257 Py_DECREF(rv);
1258 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001259}
1260
1261PyDoc_STRVAR(symmetric_difference_doc,
1262"Return the symmetric difference of two sets as a new set.\n\
1263\n\
1264(i.e. all elements that are in exactly one of the sets.)");
1265
1266static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001267set_xor(PySetObject *so, PyObject *other)
1268{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001269 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001270 Py_INCREF(Py_NotImplemented);
1271 return Py_NotImplemented;
1272 }
1273 return set_symmetric_difference(so, other);
1274}
1275
1276static PyObject *
1277set_ixor(PySetObject *so, PyObject *other)
1278{
1279 PyObject *result;
1280
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001281 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001282 Py_INCREF(Py_NotImplemented);
1283 return Py_NotImplemented;
1284 }
1285 result = set_symmetric_difference_update(so, other);
1286 if (result == NULL)
1287 return NULL;
1288 Py_DECREF(result);
1289 Py_INCREF(so);
1290 return (PyObject *)so;
1291}
1292
1293static PyObject *
1294set_issubset(PySetObject *so, PyObject *other)
1295{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001296 PyObject *tmp, *result;
Raymond Hettinger99220fa2005-08-06 18:57:13 +00001297 register setentry *entry;
1298 register int i;
Raymond Hettingera690a992003-11-16 16:17:49 +00001299
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001300 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001301 tmp = make_new_set(&PySet_Type, other);
1302 if (tmp == NULL)
1303 return NULL;
1304 result = set_issubset(so, tmp);
1305 Py_DECREF(tmp);
1306 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001307 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001308 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001309 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001310
Raymond Hettinger99220fa2005-08-06 18:57:13 +00001311 entry = &so->table[0];
1312 for (i=so->used ; i ; entry++, i--) {
1313 while (entry->key == NULL || entry->key==dummy)
1314 entry++;
1315 if (!set_contains_internal((PySetObject *)other, entry->key))
Raymond Hettingera690a992003-11-16 16:17:49 +00001316 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001317 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001318 Py_RETURN_TRUE;
1319}
1320
1321PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1322
1323static PyObject *
1324set_issuperset(PySetObject *so, PyObject *other)
1325{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001326 PyObject *tmp, *result;
1327
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001328 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001329 tmp = make_new_set(&PySet_Type, other);
1330 if (tmp == NULL)
1331 return NULL;
1332 result = set_issuperset(so, tmp);
1333 Py_DECREF(tmp);
1334 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001335 }
1336 return set_issubset((PySetObject *)other, (PyObject *)so);
1337}
1338
1339PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1340
1341static long
1342set_nohash(PyObject *self)
1343{
1344 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1345 return -1;
1346}
1347
1348static int
1349set_nocmp(PyObject *self)
1350{
1351 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1352 return -1;
1353}
1354
1355static long
1356frozenset_hash(PyObject *self)
1357{
Raymond Hettingera690a992003-11-16 16:17:49 +00001358 PySetObject *so = (PySetObject *)self;
Raymond Hettingera580c472005-08-05 17:19:54 +00001359 long h, hash = 1927868237L;
1360 setentry *entry;
1361 int i;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001362
Raymond Hettingera690a992003-11-16 16:17:49 +00001363 if (so->hash != -1)
1364 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001365
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001366 hash *= set_len(self) + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +00001367 entry = &so->table[0];
1368 for (i=so->used ; i ; entry++, i--) {
1369 while (entry->key == NULL || entry->key==dummy)
1370 entry++;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001371 /* Work to increase the bit dispersion for closely spaced hash
1372 values. The is important because some use cases have many
1373 combinations of a small number of elements with nearby
1374 hashes so that many distinct combinations collapse to only
1375 a handful of distinct hash values. */
Raymond Hettingera9d99362005-08-05 00:01:15 +00001376 h = entry->hash;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001377 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001378 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001379 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001380 if (hash == -1)
1381 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001382 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001383 return hash;
1384}
1385
1386static PyObject *
1387set_richcompare(PySetObject *v, PyObject *w, int op)
1388{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001389 PyObject *r1, *r2;
1390
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001391 if(!PyAnySet_Check(w)) {
1392 if (op == Py_EQ)
1393 Py_RETURN_FALSE;
1394 if (op == Py_NE)
1395 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001396 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1397 return NULL;
1398 }
1399 switch (op) {
1400 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001401 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001402 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001403 return set_issubset(v, w);
1404 case Py_NE:
1405 if (set_len((PyObject *)v) != set_len(w))
1406 Py_RETURN_TRUE;
1407 r1 = set_issubset(v, w);
1408 assert (r1 != NULL);
1409 r2 = PyBool_FromLong(PyObject_Not(r1));
1410 Py_DECREF(r1);
1411 return r2;
1412 case Py_LE:
1413 return set_issubset(v, w);
1414 case Py_GE:
1415 return set_issuperset(v, w);
1416 case Py_LT:
1417 if (set_len((PyObject *)v) >= set_len(w))
1418 Py_RETURN_FALSE;
1419 return set_issubset(v, w);
1420 case Py_GT:
1421 if (set_len((PyObject *)v) <= set_len(w))
1422 Py_RETURN_FALSE;
1423 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001424 }
1425 Py_INCREF(Py_NotImplemented);
1426 return Py_NotImplemented;
1427}
1428
1429static PyObject *
1430set_repr(PySetObject *so)
1431{
1432 PyObject *keys, *result, *listrepr;
1433
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001434 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001435 if (keys == NULL)
1436 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001437 listrepr = PyObject_Repr(keys);
1438 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001439 if (listrepr == NULL)
1440 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001441
1442 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1443 PyString_AS_STRING(listrepr));
1444 Py_DECREF(listrepr);
1445 return result;
1446}
1447
1448static int
1449set_tp_print(PySetObject *so, FILE *fp, int flags)
1450{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001451 PyObject *key;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001452 int pos=0;
1453 char *emit = ""; /* No separator emitted on first pass */
1454 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001455
Raymond Hettingera690a992003-11-16 16:17:49 +00001456 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001457 while (set_next_internal(so, &pos, &key)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001458 fputs(emit, fp);
1459 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001460 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001461 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001462 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001463 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001464 return 0;
1465}
1466
1467static PyObject *
1468set_clear(PySetObject *so)
1469{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001470 set_clear_internal(so);
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001471 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001472}
1473
1474PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1475
Raymond Hettingera690a992003-11-16 16:17:49 +00001476static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001477set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001478{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001479 if (set_add_internal(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001480 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001481 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001482}
1483
1484PyDoc_STRVAR(add_doc,
1485"Add an element to a set.\n\
1486\n\
1487This has no effect if the element is already present.");
1488
1489static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001490set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001491{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001492 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001493 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001494
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001495 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1496 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1497 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001498 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001499 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1500 result = set_remove(so, tmpkey);
1501 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1502 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001503 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001504 }
Raymond Hettinger0deab622003-12-13 18:53:18 +00001505
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001506 rv = set_discard_internal(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001507 if (rv == -1)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001508 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001509 else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001510 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001511 return NULL;
1512 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001513 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001514}
1515
1516PyDoc_STRVAR(remove_doc,
1517"Remove an element from a set; it must be a member.\n\
1518\n\
1519If the element is not a member, raise a KeyError.");
1520
1521static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001522set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001523{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001524 PyObject *tmpkey, *result;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001525
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001526 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1527 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1528 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001529 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001530 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1531 result = set_discard(so, tmpkey);
1532 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1533 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001534 return result;
1535 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001536
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001537 if (set_discard_internal(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001538 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001539 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001540}
1541
1542PyDoc_STRVAR(discard_doc,
1543"Remove an element from a set if it is a member.\n\
1544\n\
1545If the element is not a member, do nothing.");
1546
1547static PyObject *
1548set_pop(PySetObject *so)
1549{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001550 PyObject *key;
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001551 register setentry *entry;
1552 register int i = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001553
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001554 assert (PyAnySet_Check(so));
1555 if (so->used == 0) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001556 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1557 return NULL;
1558 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001559
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001560 /* Set entry to "the first" unused or dummy set entry. We abuse
1561 * the hash field of slot 0 to hold a search finger:
1562 * If slot 0 has a value, use slot 0.
1563 * Else slot 0 is being used to hold a search finger,
1564 * and we use its hash value as the first index to look.
1565 */
1566 entry = &so->table[0];
1567 if (entry->key == NULL || entry->key == dummy) {
1568 i = (int)entry->hash;
1569 /* The hash field may be a real hash value, or it may be a
1570 * legit search finger, or it may be a once-legit search
1571 * finger that's out of bounds now because it wrapped around
1572 * or the table shrunk -- simply make sure it's in bounds now.
1573 */
1574 if (i > so->mask || i < 1)
1575 i = 1; /* skip slot 0 */
1576 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
1577 i++;
1578 if (i > so->mask)
1579 i = 1;
1580 }
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001581 }
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001582 key = entry->key;
1583 Py_INCREF(dummy);
1584 entry->key = dummy;
1585 so->used--;
1586 so->table[0].hash = i + 1; /* next place to start */
Raymond Hettingera690a992003-11-16 16:17:49 +00001587 return key;
1588}
1589
1590PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1591
1592static PyObject *
1593set_reduce(PySetObject *so)
1594{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001595 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001596
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001597 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001598 if (keys == NULL)
1599 goto done;
1600 args = PyTuple_Pack(1, keys);
1601 if (args == NULL)
1602 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001603 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1604 if (dict == NULL) {
1605 PyErr_Clear();
1606 dict = Py_None;
1607 Py_INCREF(dict);
1608 }
1609 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001610done:
1611 Py_XDECREF(args);
1612 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001613 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001614 return result;
1615}
1616
1617PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1618
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001619static int
1620set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1621{
1622 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001623
1624 if (!PyAnySet_Check(self))
1625 return -1;
1626 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1627 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001628 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001629 self->hash = -1;
1630 if (iterable == NULL)
1631 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001632 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001633}
1634
Raymond Hettingera690a992003-11-16 16:17:49 +00001635static PySequenceMethods set_as_sequence = {
1636 (inquiry)set_len, /* sq_length */
1637 0, /* sq_concat */
1638 0, /* sq_repeat */
1639 0, /* sq_item */
1640 0, /* sq_slice */
1641 0, /* sq_ass_item */
1642 0, /* sq_ass_slice */
1643 (objobjproc)set_contains, /* sq_contains */
1644};
1645
1646/* set object ********************************************************/
1647
1648static PyMethodDef set_methods[] = {
1649 {"add", (PyCFunction)set_add, METH_O,
1650 add_doc},
1651 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1652 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001653 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001654 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001655 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1656 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001657 {"discard", (PyCFunction)set_discard, METH_O,
1658 discard_doc},
1659 {"difference", (PyCFunction)set_difference, METH_O,
1660 difference_doc},
1661 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1662 difference_update_doc},
1663 {"intersection",(PyCFunction)set_intersection, METH_O,
1664 intersection_doc},
1665 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1666 intersection_update_doc},
1667 {"issubset", (PyCFunction)set_issubset, METH_O,
1668 issubset_doc},
1669 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1670 issuperset_doc},
1671 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1672 pop_doc},
1673 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1674 reduce_doc},
1675 {"remove", (PyCFunction)set_remove, METH_O,
1676 remove_doc},
1677 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1678 symmetric_difference_doc},
1679 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1680 symmetric_difference_update_doc},
1681 {"union", (PyCFunction)set_union, METH_O,
1682 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001683 {"update", (PyCFunction)set_update, METH_O,
1684 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001685 {NULL, NULL} /* sentinel */
1686};
1687
1688static PyNumberMethods set_as_number = {
1689 0, /*nb_add*/
1690 (binaryfunc)set_sub, /*nb_subtract*/
1691 0, /*nb_multiply*/
1692 0, /*nb_divide*/
1693 0, /*nb_remainder*/
1694 0, /*nb_divmod*/
1695 0, /*nb_power*/
1696 0, /*nb_negative*/
1697 0, /*nb_positive*/
1698 0, /*nb_absolute*/
1699 0, /*nb_nonzero*/
1700 0, /*nb_invert*/
1701 0, /*nb_lshift*/
1702 0, /*nb_rshift*/
1703 (binaryfunc)set_and, /*nb_and*/
1704 (binaryfunc)set_xor, /*nb_xor*/
1705 (binaryfunc)set_or, /*nb_or*/
1706 0, /*nb_coerce*/
1707 0, /*nb_int*/
1708 0, /*nb_long*/
1709 0, /*nb_float*/
1710 0, /*nb_oct*/
1711 0, /*nb_hex*/
1712 0, /*nb_inplace_add*/
1713 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1714 0, /*nb_inplace_multiply*/
1715 0, /*nb_inplace_divide*/
1716 0, /*nb_inplace_remainder*/
1717 0, /*nb_inplace_power*/
1718 0, /*nb_inplace_lshift*/
1719 0, /*nb_inplace_rshift*/
1720 (binaryfunc)set_iand, /*nb_inplace_and*/
1721 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1722 (binaryfunc)set_ior, /*nb_inplace_or*/
1723};
1724
1725PyDoc_STRVAR(set_doc,
1726"set(iterable) --> set object\n\
1727\n\
1728Build an unordered collection.");
1729
1730PyTypeObject PySet_Type = {
1731 PyObject_HEAD_INIT(&PyType_Type)
1732 0, /* ob_size */
1733 "set", /* tp_name */
1734 sizeof(PySetObject), /* tp_basicsize */
1735 0, /* tp_itemsize */
1736 /* methods */
1737 (destructor)set_dealloc, /* tp_dealloc */
1738 (printfunc)set_tp_print, /* tp_print */
1739 0, /* tp_getattr */
1740 0, /* tp_setattr */
1741 (cmpfunc)set_nocmp, /* tp_compare */
1742 (reprfunc)set_repr, /* tp_repr */
1743 &set_as_number, /* tp_as_number */
1744 &set_as_sequence, /* tp_as_sequence */
1745 0, /* tp_as_mapping */
1746 set_nohash, /* tp_hash */
1747 0, /* tp_call */
1748 0, /* tp_str */
1749 PyObject_GenericGetAttr, /* tp_getattro */
1750 0, /* tp_setattro */
1751 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001752 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001753 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001754 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001755 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001756 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001757 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001758 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001759 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001760 0, /* tp_iternext */
1761 set_methods, /* tp_methods */
1762 0, /* tp_members */
1763 0, /* tp_getset */
1764 0, /* tp_base */
1765 0, /* tp_dict */
1766 0, /* tp_descr_get */
1767 0, /* tp_descr_set */
1768 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001769 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001770 PyType_GenericAlloc, /* tp_alloc */
1771 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001772 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001773};
1774
1775/* frozenset object ********************************************************/
1776
1777
1778static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001779 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001780 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001781 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001782 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001783 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001784 difference_doc},
1785 {"intersection",(PyCFunction)set_intersection, METH_O,
1786 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001787 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001788 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001789 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001790 issuperset_doc},
1791 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1792 reduce_doc},
1793 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1794 symmetric_difference_doc},
1795 {"union", (PyCFunction)set_union, METH_O,
1796 union_doc},
1797 {NULL, NULL} /* sentinel */
1798};
1799
1800static PyNumberMethods frozenset_as_number = {
1801 0, /*nb_add*/
1802 (binaryfunc)set_sub, /*nb_subtract*/
1803 0, /*nb_multiply*/
1804 0, /*nb_divide*/
1805 0, /*nb_remainder*/
1806 0, /*nb_divmod*/
1807 0, /*nb_power*/
1808 0, /*nb_negative*/
1809 0, /*nb_positive*/
1810 0, /*nb_absolute*/
1811 0, /*nb_nonzero*/
1812 0, /*nb_invert*/
1813 0, /*nb_lshift*/
1814 0, /*nb_rshift*/
1815 (binaryfunc)set_and, /*nb_and*/
1816 (binaryfunc)set_xor, /*nb_xor*/
1817 (binaryfunc)set_or, /*nb_or*/
1818};
1819
1820PyDoc_STRVAR(frozenset_doc,
1821"frozenset(iterable) --> frozenset object\n\
1822\n\
1823Build an immutable unordered collection.");
1824
1825PyTypeObject PyFrozenSet_Type = {
1826 PyObject_HEAD_INIT(&PyType_Type)
1827 0, /* ob_size */
1828 "frozenset", /* tp_name */
1829 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001830 0, /* tp_itemsize */
1831 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001832 (destructor)set_dealloc, /* tp_dealloc */
1833 (printfunc)set_tp_print, /* tp_print */
1834 0, /* tp_getattr */
1835 0, /* tp_setattr */
1836 (cmpfunc)set_nocmp, /* tp_compare */
1837 (reprfunc)set_repr, /* tp_repr */
1838 &frozenset_as_number, /* tp_as_number */
1839 &set_as_sequence, /* tp_as_sequence */
1840 0, /* tp_as_mapping */
1841 frozenset_hash, /* tp_hash */
1842 0, /* tp_call */
1843 0, /* tp_str */
1844 PyObject_GenericGetAttr, /* tp_getattro */
1845 0, /* tp_setattro */
1846 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001847 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001848 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001849 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001850 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001851 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001852 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001853 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001854 (getiterfunc)set_iter, /* tp_iter */
1855 0, /* tp_iternext */
1856 frozenset_methods, /* tp_methods */
1857 0, /* tp_members */
1858 0, /* tp_getset */
1859 0, /* tp_base */
1860 0, /* tp_dict */
1861 0, /* tp_descr_get */
1862 0, /* tp_descr_set */
1863 0, /* tp_dictoffset */
1864 0, /* tp_init */
1865 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001866 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001867 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001868};