blob: 7cfd2a96c4bf4304e2c97fbb397d026b7aa7976e [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Raymond Hettingera9d99362005-08-05 00:01:15 +00006 Copyright (c) 2003-5 Python Software Foundation.
7 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
13/* This must be >= 1. */
14#define PERTURB_SHIFT 5
15
16/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000017static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000018
Raymond Hettingerbc841a12005-08-07 13:02:53 +000019#define INIT_NONZERO_SET_SLOTS(so) do { \
20 (so)->table = (so)->smalltable; \
21 (so)->mask = PySet_MINSIZE - 1; \
22 (so)->hash = -1; \
23 } while(0)
24
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000025#define EMPTY_TO_MINSIZE(so) do { \
26 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
27 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000028 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000029 } while(0)
30
Raymond Hettingerbc841a12005-08-07 13:02:53 +000031/* Reuse scheme to save calls to malloc, free, and memset */
32#define MAXFREESETS 80
33static PySetObject *free_sets[MAXFREESETS];
34static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035
36/*
37The basic lookup function used by all operations.
38This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
39Open addressing is preferred over chaining since the link overhead for
40chaining would be substantial (100% with typical malloc overhead).
41
42The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000043probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000044
45All arithmetic on hash should ignore overflow.
46
Raymond Hettingerbc841a12005-08-07 13:02:53 +000047The lookup function always succeeds and nevers return NULL. This simplifies
48and speeds client functions which do won't have to test for and handle
49errors. To meet that requirement, any errors generated by a user defined
50__cmp__() function are simply cleared and ignored.
51Previously outstanding exceptions are maintained.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052*/
53
54static setentry *
55set_lookkey(PySetObject *so, PyObject *key, register long hash)
56{
57 register int i;
58 register unsigned int perturb;
59 register setentry *freeslot;
60 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000061 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000062 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000063 register int restore_error;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000064 register int cmp;
65 PyObject *err_type, *err_value, *err_tb;
66 PyObject *startkey;
67
68 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000069 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000070 if (entry->key == NULL || entry->key == key)
71 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000072
Raymond Hettinger99220fa2005-08-06 18:57:13 +000073 restore_error = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000074 if (entry->key == dummy)
75 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000076 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000077 if (entry->hash == hash) {
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +000078 if (_PyErr_OCCURRED()) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000079 restore_error = 1;
80 PyErr_Fetch(&err_type, &err_value, &err_tb);
81 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000082 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000083 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
84 if (cmp < 0)
85 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +000086 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000087 if (cmp > 0)
88 goto Done;
89 }
90 else {
91 /* The compare did major nasty stuff to the
92 * set: start over.
93 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000095 goto Done;
96 }
97 }
98 freeslot = NULL;
99 }
100
101 /* In the loop, key == dummy is by far (factor of 100s) the
102 least likely outcome, so test for that last. */
103 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
104 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000105 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000106 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000107 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000108 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000109 break;
110 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000111 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000112 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000113 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger99220fa2005-08-06 18:57:13 +0000114 if (_PyErr_OCCURRED()) {
115 restore_error = 1;
116 PyErr_Fetch(&err_type, &err_value,
117 &err_tb);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000118 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000119 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
121 if (cmp < 0)
122 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +0000123 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 if (cmp > 0)
125 break;
126 }
127 else {
128 /* The compare did major nasty stuff to the
129 * set: start over.
130 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000131 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000132 break;
133 }
134 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000135 else if (entry->key == dummy && freeslot == NULL)
136 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000137 }
138
139Done:
140 if (restore_error)
141 PyErr_Restore(err_type, err_value, err_tb);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000142 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000143}
144
145/*
146 * Hacked up version of set_lookkey which can assume keys are always strings;
147 * this assumption allows testing for errors during PyObject_Compare() to
148 * be dropped; string-string comparisons never raise exceptions. This also
149 * means we don't need to go through PyObject_Compare(); we can always use
150 * _PyString_Eq directly.
151 *
152 * This is valuable because the general-case error handling in set_lookkey() is
153 * expensive, and sets with pure-string keys may be very common.
154 */
155static setentry *
156set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
157{
158 register int i;
159 register unsigned int perturb;
160 register setentry *freeslot;
161 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000162 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000163 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000164
165 /* Make sure this function doesn't have to handle non-string keys,
166 including subclasses of str; e.g., one reason to subclass
167 strings is to override __eq__, and for speed we don't cater to
168 that here. */
169 if (!PyString_CheckExact(key)) {
170 so->lookup = set_lookkey;
171 return set_lookkey(so, key, hash);
172 }
173 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000174 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000175 if (entry->key == NULL || entry->key == key)
176 return entry;
Raymond Hettingera580c472005-08-05 17:19:54 +0000177 if (so->fill != so->used) {
178 if (entry->key == dummy)
179 freeslot = entry;
180 else {
181 if (entry->hash == hash && _PyString_Eq(entry->key, key))
182 return entry;
183 freeslot = NULL;
184 }
185
186 /* In the loop, key == dummy is by far (factor of 100s) the
187 least likely outcome, so test for that last. */
188 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
189 i = (i << 2) + i + perturb + 1;
190 entry = &table[i & mask];
191 if (entry->key == NULL)
192 return freeslot == NULL ? entry : freeslot;
193 if (entry->key == key
194 || (entry->hash == hash
195 && entry->key != dummy
196 && _PyString_Eq(entry->key, key)))
197 return entry;
198 if (entry->key == dummy && freeslot == NULL)
199 freeslot = entry;
200 }
201 } else {
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000202 /* Simplified loop when there are no dummy entries. */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000203 if (entry->hash == hash && _PyString_Eq(entry->key, key))
204 return entry;
Raymond Hettingera580c472005-08-05 17:19:54 +0000205 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
206 i = (i << 2) + i + perturb + 1;
207 entry = &table[i & mask];
208 if (entry->key == NULL)
209 return entry;
210 if (entry->key == key
211 || (entry->hash == hash
212 && _PyString_Eq(entry->key, key)))
213 return entry;
214 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000215 }
216}
217
218/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000219Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220Used both by the internal resize routine and by the public insert routine.
221Eats a reference to key.
222*/
223static void
224set_insert_key(register PySetObject *so, PyObject *key, long hash)
225{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000226 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000227 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
228
229 assert(so->lookup != NULL);
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 Hettingerc991db22005-08-11 07:58:45 +0000339/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
340
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000342set_add_entry(register PySetObject *so, setentry *entry)
343{
344 register int n_used;
345
346 assert(so->fill <= so->mask); /* at least one empty slot */
347 n_used = so->used;
348 Py_INCREF(entry->key);
349 set_insert_key(so, entry->key, entry->hash);
350 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
351 return 0;
352 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
353}
354
355static int
356set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000357{
358 register long hash;
359 register int n_used;
360
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361 if (!PyString_CheckExact(key) ||
362 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363 hash = PyObject_Hash(key);
364 if (hash == -1)
365 return -1;
366 }
367 assert(so->fill <= so->mask); /* at least one empty slot */
368 n_used = so->used;
369 Py_INCREF(key);
370 set_insert_key(so, key, hash);
371 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
372 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000373 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000374}
375
376#define DISCARD_NOTFOUND 0
377#define DISCARD_FOUND 1
378
379static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000380set_discard_entry(PySetObject *so, setentry *oldentry)
381{ register setentry *entry;
382 PyObject *old_key;
383
384 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
385 if (entry->key == NULL || entry->key == dummy)
386 return DISCARD_NOTFOUND;
387 old_key = entry->key;
388 Py_INCREF(dummy);
389 entry->key = dummy;
390 so->used--;
391 Py_DECREF(old_key);
392 return DISCARD_FOUND;
393}
394
395static int
396set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397{
398 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000399 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000400 PyObject *old_key;
401
402 assert (PyAnySet_Check(so));
403 if (!PyString_CheckExact(key) ||
404 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
405 hash = PyObject_Hash(key);
406 if (hash == -1)
407 return -1;
408 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000409 entry = (so->lookup)(so, key, hash);
410 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000411 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000412 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000413 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000414 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000415 so->used--;
416 Py_DECREF(old_key);
417 return DISCARD_FOUND;
418}
419
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000420static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421set_clear_internal(PySetObject *so)
422{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000423 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424 int table_is_malloced;
425 int fill;
426 setentry small_copy[PySet_MINSIZE];
427#ifdef Py_DEBUG
428 int i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000429 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000430
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431 n = so->mask + 1;
432 i = 0;
433#endif
434
435 table = so->table;
436 assert(table != NULL);
437 table_is_malloced = table != so->smalltable;
438
439 /* This is delicate. During the process of clearing the set,
440 * decrefs can cause the set to mutate. To avoid fatal confusion
441 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000442 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443 * clearing.
444 */
445 fill = so->fill;
446 if (table_is_malloced)
447 EMPTY_TO_MINSIZE(so);
448
449 else if (fill > 0) {
450 /* It's a small table with something that needs to be cleared.
451 * Afraid the only safe way is to copy the set entries into
452 * another small table first.
453 */
454 memcpy(small_copy, table, sizeof(small_copy));
455 table = small_copy;
456 EMPTY_TO_MINSIZE(so);
457 }
458 /* else it's a small table that's already empty */
459
460 /* Now we can finally clear things. If C had refcounts, we could
461 * assert that the refcount on table is 1 now, i.e. that this function
462 * has unique access to it, so decref side-effects can't alter it.
463 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000464 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000465#ifdef Py_DEBUG
466 assert(i < n);
467 ++i;
468#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000469 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000471 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472 }
473#ifdef Py_DEBUG
474 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000475 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476#endif
477 }
478
479 if (table_is_malloced)
480 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000481 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482}
483
484/*
485 * Iterate over a set table. Use like so:
486 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000487 * int pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000488 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000489 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000490 * while (set_next(yourset, &pos, &entry)) {
491 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000492 * }
493 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000494 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495 * mutates the table.
496 */
497static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000498set_next(PySetObject *so, int *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499{
500 register int i, mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000501 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502
503 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000504 i = *pos_ptr;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505 if (i < 0)
506 return 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000507 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000509 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000510 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000511 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512 if (i > mask)
513 return 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000514 if (table[i].key)
515 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516 return 1;
517}
518
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000520set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000521{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000522 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523 register int i;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000524 register setentry *entry, *othertable;
525 register int othermask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000526
527 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000528 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000530 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000531 if (other == so || other->used == 0)
532 /* a.update(a) or a.update({}); nothing to do */
533 return 0;
534 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000535 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536 * that there will be no (or few) overlapping keys.
537 */
538 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
539 if (set_table_resize(so, (so->used + other->used)*2) != 0)
540 return -1;
541 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000542 othermask = other->mask;
543 othertable = other->table;
544 for (i = 0; i <= othermask; i++) {
545 entry = &othertable[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000546 if (entry->key != NULL &&
547 entry->key != dummy) {
548 Py_INCREF(entry->key);
549 set_insert_key(so, entry->key, entry->hash);
550 }
551 }
552 return 0;
553}
554
555static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000556set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000557{
558 long hash;
559
560 if (!PyString_CheckExact(key) ||
561 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
562 hash = PyObject_Hash(key);
563 if (hash == -1)
564 return -1;
565 }
566 key = (so->lookup)(so, key, hash)->key;
567 return key != NULL && key != dummy;
568}
569
Raymond Hettingerc991db22005-08-11 07:58:45 +0000570static int
571set_contains_entry(PySetObject *so, setentry *entry)
572{
573 PyObject *key;
574
575 key = (so->lookup)(so, entry->key, entry->hash)->key;
576 return key != NULL && key != dummy;
577}
578
Raymond Hettingera9d99362005-08-05 00:01:15 +0000579/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000580
Raymond Hettingerd7946662005-08-01 21:39:29 +0000581static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000582
583typedef struct {
584 PyObject_HEAD
585 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
586 int si_used;
587 int si_pos;
588 long len;
589} setiterobject;
590
591static PyObject *
592set_iter(PySetObject *so)
593{
594 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
595 if (si == NULL)
596 return NULL;
597 Py_INCREF(so);
598 si->si_set = so;
599 si->si_used = so->used;
600 si->si_pos = 0;
601 si->len = so->used;
602 return (PyObject *)si;
603}
604
605static void
606setiter_dealloc(setiterobject *si)
607{
608 Py_XDECREF(si->si_set);
609 PyObject_Del(si);
610}
611
612static int
613setiter_len(setiterobject *si)
614{
615 if (si->si_set != NULL && si->si_used == si->si_set->used)
616 return si->len;
617 return 0;
618}
619
620static PySequenceMethods setiter_as_sequence = {
621 (inquiry)setiter_len, /* sq_length */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000622};
623
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000624static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000625{
626 PyObject *key;
627 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000628 register setentry *entry;
629 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000630
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000631 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000632 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000633 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000634
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000635 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000636 PyErr_SetString(PyExc_RuntimeError,
637 "Set changed size during iteration");
638 si->si_used = -1; /* Make this state sticky */
639 return NULL;
640 }
641
642 i = si->si_pos;
643 if (i < 0)
644 goto fail;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000645 entry = so->table;
646 mask = so->mask;
647 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648 i++;
649 si->si_pos = i+1;
650 if (i > mask)
651 goto fail;
652 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000653 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000654 Py_INCREF(key);
655 return key;
656
657fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000658 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000659 si->si_set = NULL;
660 return NULL;
661}
662
Hye-Shik Change2956762005-08-01 05:26:41 +0000663static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000664 PyObject_HEAD_INIT(&PyType_Type)
665 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000666 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000667 sizeof(setiterobject), /* tp_basicsize */
668 0, /* tp_itemsize */
669 /* methods */
670 (destructor)setiter_dealloc, /* tp_dealloc */
671 0, /* tp_print */
672 0, /* tp_getattr */
673 0, /* tp_setattr */
674 0, /* tp_compare */
675 0, /* tp_repr */
676 0, /* tp_as_number */
677 &setiter_as_sequence, /* tp_as_sequence */
678 0, /* tp_as_mapping */
679 0, /* tp_hash */
680 0, /* tp_call */
681 0, /* tp_str */
682 PyObject_GenericGetAttr, /* tp_getattro */
683 0, /* tp_setattro */
684 0, /* tp_as_buffer */
685 Py_TPFLAGS_DEFAULT, /* tp_flags */
686 0, /* tp_doc */
687 0, /* tp_traverse */
688 0, /* tp_clear */
689 0, /* tp_richcompare */
690 0, /* tp_weaklistoffset */
691 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000692 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000693};
694
Raymond Hettingerd7946662005-08-01 21:39:29 +0000695static int
696set_len(PyObject *so)
697{
698 return ((PySetObject *)so)->used;
699}
700
701static int
702set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000703{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000704 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000705
Raymond Hettingerd7946662005-08-01 21:39:29 +0000706 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000707 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000708
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000709 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000710 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000711 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000712 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000713 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000714 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000715 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000716 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000717 }
718
Raymond Hettingera38123e2003-11-24 22:18:49 +0000719 it = PyObject_GetIter(other);
720 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000721 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000722
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000723 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000724 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000725 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000726 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000727 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000728 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000729 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000730 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000731 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000732 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000733 return -1;
734 return 0;
735}
736
737static PyObject *
738set_update(PySetObject *so, PyObject *other)
739{
740 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000741 return NULL;
742 Py_RETURN_NONE;
743}
744
745PyDoc_STRVAR(update_doc,
746"Update a set with the union of itself and another.");
747
748static PyObject *
749make_new_set(PyTypeObject *type, PyObject *iterable)
750{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000751 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000752
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000753 if (dummy == NULL) { /* Auto-initialize dummy */
754 dummy = PyString_FromString("<dummy key>");
755 if (dummy == NULL)
756 return NULL;
757 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000758
759 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000760 if (num_free_sets &&
761 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
762 so = free_sets[--num_free_sets];
763 assert (so != NULL && PyAnySet_CheckExact(so));
764 so->ob_type = type;
765 _Py_NewReference((PyObject *)so);
766 EMPTY_TO_MINSIZE(so);
767 PyObject_GC_Track(so);
768 } else {
769 so = (PySetObject *)type->tp_alloc(type, 0);
770 if (so == NULL)
771 return NULL;
772 /* tp_alloc has already zeroed the structure */
773 assert(so->table == NULL && so->fill == 0 && so->used == 0);
774 INIT_NONZERO_SET_SLOTS(so);
775 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000776
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000778 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000779
Raymond Hettingera38123e2003-11-24 22:18:49 +0000780 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000781 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000782 Py_DECREF(so);
783 return NULL;
784 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000785 }
786
Raymond Hettingera690a992003-11-16 16:17:49 +0000787 return (PyObject *)so;
788}
789
Raymond Hettingerd7946662005-08-01 21:39:29 +0000790/* The empty frozenset is a singleton */
791static PyObject *emptyfrozenset = NULL;
792
Raymond Hettingera690a992003-11-16 16:17:49 +0000793static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000794frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000795{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000796 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000797
798 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
799 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000800
801 if (type != &PyFrozenSet_Type)
802 return make_new_set(type, iterable);
803
804 if (iterable != NULL) {
805 /* frozenset(f) is idempotent */
806 if (PyFrozenSet_CheckExact(iterable)) {
807 Py_INCREF(iterable);
808 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000810 result = make_new_set(type, iterable);
811 if (result == NULL || set_len(result))
812 return result;
813 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000814 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000815 /* The empty frozenset is a singleton */
816 if (emptyfrozenset == NULL)
817 emptyfrozenset = make_new_set(type, NULL);
818 Py_XINCREF(emptyfrozenset);
819 return emptyfrozenset;
820}
821
822void
823PySet_Fini(void)
824{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000825 PySetObject *so;
826
827 while (num_free_sets) {
828 num_free_sets--;
829 so = free_sets[num_free_sets];
830 PyObject_GC_Del(so);
831 }
Raymond Hettingera9d99362005-08-05 00:01:15 +0000832 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000833 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000834}
835
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000836static PyObject *
837set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
838{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000839 return make_new_set(type, NULL);
840}
841
Raymond Hettingera690a992003-11-16 16:17:49 +0000842static void
843set_dealloc(PySetObject *so)
844{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000845 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000846 int fill = so->fill;
847
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000848 PyObject_GC_UnTrack(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000849 Py_TRASHCAN_SAFE_BEGIN(so)
Raymond Hettinger691d8052004-05-30 07:26:47 +0000850 if (so->weakreflist != NULL)
851 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000852
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000853 for (entry = so->table; fill > 0; entry++) {
854 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000855 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000856 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000857 }
858 }
859 if (so->table != so->smalltable)
860 PyMem_DEL(so->table);
861
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000862 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
863 free_sets[num_free_sets++] = so;
864 else
865 so->ob_type->tp_free(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000866 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000867}
868
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000869static int
870set_traverse(PySetObject *so, visitproc visit, void *arg)
871{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000872 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000873 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000874
Raymond Hettingerc991db22005-08-11 07:58:45 +0000875 while (set_next(so, &pos, &entry))
876 Py_VISIT(entry->key);
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000877 return 0;
878}
879
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000880/* set_swap_bodies() switches the contents of any two sets by moving their
881 internal data pointers and, if needed, copying the internal smalltables.
882 Semantically equivalent to:
883
884 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
885
886 The function always succeeds and it leaves both objects in a stable state.
887 Useful for creating temporary frozensets from sets for membership testing
888 in __contains__(), discard(), and remove(). Also useful for operations
889 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000890 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000891*/
892
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000893static void
894set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000895{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000896 int t;
897 setentry *u;
898 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
899 setentry tab[PySet_MINSIZE];
900 long h;
901
902 t = a->fill; a->fill = b->fill; b->fill = t;
903 t = a->used; a->used = b->used; b->used = t;
904 t = a->mask; a->mask = b->mask; b->mask = t;
905
906 u = a->table;
907 if (a->table == a->smalltable)
908 u = b->smalltable;
909 a->table = b->table;
910 if (b->table == b->smalltable)
911 a->table = a->smalltable;
912 b->table = u;
913
914 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
915
916 if (a->table == a->smalltable || b->table == b->smalltable) {
917 memcpy(tab, a->smalltable, sizeof(tab));
918 memcpy(a->smalltable, b->smalltable, sizeof(tab));
919 memcpy(b->smalltable, tab, sizeof(tab));
920 }
921
Raymond Hettingera580c472005-08-05 17:19:54 +0000922 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
923 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
924 h = a->hash; a->hash = b->hash; b->hash = h;
925 } else {
926 a->hash = -1;
927 b->hash = -1;
928 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000929}
930
931static int
932set_contains(PySetObject *so, PyObject *key)
933{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000934 PyObject *tmpkey;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +0000935 int rv;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000936
Raymond Hettingerb02c35e2005-08-12 20:48:39 +0000937 rv = set_contains_key(so, key);
938 if (rv == -1) {
939 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
940 return -1;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000941 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000942 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
943 if (tmpkey == NULL)
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000944 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000945 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +0000946 rv = set_contains(so, tmpkey);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000947 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
948 Py_DECREF(tmpkey);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000949 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +0000950 return rv;
Raymond Hettingera690a992003-11-16 16:17:49 +0000951}
952
953static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000954set_direct_contains(PySetObject *so, PyObject *key)
955{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000956 long result;
957
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000958 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000959 if (result == -1)
960 return NULL;
961 return PyBool_FromLong(result);
962}
963
964PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
965
966static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000967set_copy(PySetObject *so)
968{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000969 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000970}
971
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000972static PyObject *
973frozenset_copy(PySetObject *so)
974{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000975 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000976 Py_INCREF(so);
977 return (PyObject *)so;
978 }
979 return set_copy(so);
980}
981
Raymond Hettingera690a992003-11-16 16:17:49 +0000982PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
983
984static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000985set_clear(PySetObject *so)
986{
987 set_clear_internal(so);
988 Py_RETURN_NONE;
989}
990
991PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
992
993static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000994set_union(PySetObject *so, PyObject *other)
995{
996 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000997
998 result = (PySetObject *)set_copy(so);
999 if (result == NULL)
1000 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001001 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001002 Py_DECREF(result);
1003 return NULL;
1004 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001005 return (PyObject *)result;
1006}
1007
1008PyDoc_STRVAR(union_doc,
1009 "Return the union of two sets as a new set.\n\
1010\n\
1011(i.e. all elements that are in either set.)");
1012
1013static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001014set_or(PySetObject *so, PyObject *other)
1015{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001016 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001017 Py_INCREF(Py_NotImplemented);
1018 return Py_NotImplemented;
1019 }
1020 return set_union(so, other);
1021}
1022
1023static PyObject *
1024set_ior(PySetObject *so, PyObject *other)
1025{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001026 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001027 Py_INCREF(Py_NotImplemented);
1028 return Py_NotImplemented;
1029 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001030 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001031 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001032 Py_INCREF(so);
1033 return (PyObject *)so;
1034}
1035
1036static PyObject *
1037set_intersection(PySetObject *so, PyObject *other)
1038{
1039 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001040 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001041
Raymond Hettingerc991db22005-08-11 07:58:45 +00001042 if ((PyObject *)so == other) {
1043 Py_INCREF(other);
1044 return other;
1045 }
1046
Raymond Hettingera690a992003-11-16 16:17:49 +00001047 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1048 if (result == NULL)
1049 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001050
Raymond Hettingerc991db22005-08-11 07:58:45 +00001051 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001052 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001053 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001054
1055 if (set_len(other) > set_len((PyObject *)so)) {
1056 tmp = (PyObject *)so;
1057 so = (PySetObject *)other;
1058 other = tmp;
1059 }
1060
Raymond Hettingerc991db22005-08-11 07:58:45 +00001061 while (set_next((PySetObject *)other, &pos, &entry)) {
1062 if (set_contains_entry(so, entry)) {
1063 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001064 Py_DECREF(result);
1065 return NULL;
1066 }
1067 }
1068 }
1069 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001070 }
1071
Raymond Hettingera690a992003-11-16 16:17:49 +00001072 it = PyObject_GetIter(other);
1073 if (it == NULL) {
1074 Py_DECREF(result);
1075 return NULL;
1076 }
1077
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001078 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001079 if (set_contains_key(so, key)) {
1080 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001081 Py_DECREF(it);
1082 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001083 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001084 return NULL;
1085 }
1086 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001087 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001088 }
1089 Py_DECREF(it);
1090 if (PyErr_Occurred()) {
1091 Py_DECREF(result);
1092 return NULL;
1093 }
1094 return (PyObject *)result;
1095}
1096
1097PyDoc_STRVAR(intersection_doc,
1098"Return the intersection of two sets as a new set.\n\
1099\n\
1100(i.e. all elements that are in both sets.)");
1101
1102static PyObject *
1103set_intersection_update(PySetObject *so, PyObject *other)
1104{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001105 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001106
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001107 tmp = set_intersection(so, other);
1108 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001109 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001110 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001111 Py_DECREF(tmp);
1112 Py_RETURN_NONE;
1113}
1114
1115PyDoc_STRVAR(intersection_update_doc,
1116"Update a set with the intersection of itself and another.");
1117
1118static PyObject *
1119set_and(PySetObject *so, PyObject *other)
1120{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001121 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001122 Py_INCREF(Py_NotImplemented);
1123 return Py_NotImplemented;
1124 }
1125 return set_intersection(so, other);
1126}
1127
1128static PyObject *
1129set_iand(PySetObject *so, PyObject *other)
1130{
1131 PyObject *result;
1132
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001133 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001134 Py_INCREF(Py_NotImplemented);
1135 return Py_NotImplemented;
1136 }
1137 result = set_intersection_update(so, other);
1138 if (result == NULL)
1139 return NULL;
1140 Py_DECREF(result);
1141 Py_INCREF(so);
1142 return (PyObject *)so;
1143}
1144
Raymond Hettingerc991db22005-08-11 07:58:45 +00001145int
1146set_difference_update_internal(PySetObject *so, PyObject *other)
1147{
1148 if ((PyObject *)so == other)
1149 return set_clear_internal(so);
1150
1151 if (PyAnySet_Check(other)) {
1152 setentry *entry;
1153 int pos = 0;
1154
1155 while (set_next((PySetObject *)other, &pos, &entry))
1156 set_discard_entry(so, entry);
1157 } else {
1158 PyObject *key, *it;
1159 it = PyObject_GetIter(other);
1160 if (it == NULL)
1161 return -1;
1162
1163 while ((key = PyIter_Next(it)) != NULL) {
1164 if (set_discard_key(so, key) == -1) {
1165 Py_DECREF(it);
1166 Py_DECREF(key);
1167 return -1;
1168 }
1169 Py_DECREF(key);
1170 }
1171 Py_DECREF(it);
1172 if (PyErr_Occurred())
1173 return -1;
1174 }
1175 /* If more than 1/5 are dummies, then resize them away. */
1176 if ((so->fill - so->used) * 5 < so->mask)
1177 return 0;
1178 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1179}
1180
Raymond Hettingera690a992003-11-16 16:17:49 +00001181static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001182set_difference_update(PySetObject *so, PyObject *other)
1183{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001184 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001185 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001186 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001187}
1188
1189PyDoc_STRVAR(difference_update_doc,
1190"Remove all elements of another set from this set.");
1191
1192static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001193set_difference(PySetObject *so, PyObject *other)
1194{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001195 PyObject *result;
1196 setentry *entry;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001197 int pos = 0;
1198
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001199 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001200 result = set_copy(so);
1201 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001202 return NULL;
1203 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001204 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001205 Py_DECREF(result);
1206 return NULL;
1207 }
1208
1209 result = make_new_set(so->ob_type, NULL);
1210 if (result == NULL)
1211 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001212
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001213 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001214 while (set_next(so, &pos, &entry)) {
1215 setentry entrycopy;
1216 entrycopy.hash = entry->hash;
1217 entrycopy.key = entry->key;
1218 if (!PyDict_Contains(other, entry->key)) {
1219 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001220 return NULL;
1221 }
1222 }
1223 return result;
1224 }
1225
Raymond Hettingerc991db22005-08-11 07:58:45 +00001226 while (set_next(so, &pos, &entry)) {
1227 if (!set_contains_entry((PySetObject *)other, entry)) {
1228 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001229 return NULL;
1230 }
1231 }
1232 return result;
1233}
1234
1235PyDoc_STRVAR(difference_doc,
1236"Return the difference of two sets as a new set.\n\
1237\n\
1238(i.e. all elements that are in this set but not the other.)");
1239static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001240set_sub(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_difference(so, other);
1247}
1248
1249static PyObject *
1250set_isub(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_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 *
Raymond Hettingera690a992003-11-16 16:17:49 +00001267set_symmetric_difference_update(PySetObject *so, PyObject *other)
1268{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001269 PySetObject *otherset;
1270 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001271 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001272 setentry *entry;
1273
1274 if ((PyObject *)so == other)
1275 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001276
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001277 if (PyDict_Check(other)) {
1278 PyObject *value;
1279 int rv;
1280 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001281 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001282 if (rv == -1)
1283 return NULL;
1284 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001285 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001286 return NULL;
1287 }
1288 }
1289 Py_RETURN_NONE;
1290 }
1291
1292 if (PyAnySet_Check(other)) {
1293 Py_INCREF(other);
1294 otherset = (PySetObject *)other;
1295 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001296 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1297 if (otherset == NULL)
1298 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001299 }
1300
Raymond Hettingerc991db22005-08-11 07:58:45 +00001301 while (set_next(otherset, &pos, &entry)) {
1302 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001303 if (rv == -1) {
1304 Py_XDECREF(otherset);
1305 return NULL;
1306 }
1307 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001308 if (set_add_entry(so, entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001309 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001310 return NULL;
1311 }
1312 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001313 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001314 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001315 Py_RETURN_NONE;
1316}
1317
1318PyDoc_STRVAR(symmetric_difference_update_doc,
1319"Update a set with the symmetric difference of itself and another.");
1320
1321static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001322set_symmetric_difference(PySetObject *so, PyObject *other)
1323{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001324 PyObject *rv;
1325 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001326
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001327 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1328 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001329 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001330 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1331 if (rv == NULL)
1332 return NULL;
1333 Py_DECREF(rv);
1334 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001335}
1336
1337PyDoc_STRVAR(symmetric_difference_doc,
1338"Return the symmetric difference of two sets as a new set.\n\
1339\n\
1340(i.e. all elements that are in exactly one of the sets.)");
1341
1342static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001343set_xor(PySetObject *so, PyObject *other)
1344{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001345 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001346 Py_INCREF(Py_NotImplemented);
1347 return Py_NotImplemented;
1348 }
1349 return set_symmetric_difference(so, other);
1350}
1351
1352static PyObject *
1353set_ixor(PySetObject *so, PyObject *other)
1354{
1355 PyObject *result;
1356
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001357 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001358 Py_INCREF(Py_NotImplemented);
1359 return Py_NotImplemented;
1360 }
1361 result = set_symmetric_difference_update(so, other);
1362 if (result == NULL)
1363 return NULL;
1364 Py_DECREF(result);
1365 Py_INCREF(so);
1366 return (PyObject *)so;
1367}
1368
1369static PyObject *
1370set_issubset(PySetObject *so, PyObject *other)
1371{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001372 PyObject *tmp, *result;
Raymond Hettinger99220fa2005-08-06 18:57:13 +00001373 register setentry *entry;
1374 register int i;
Raymond Hettingera690a992003-11-16 16:17:49 +00001375
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001376 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001377 tmp = make_new_set(&PySet_Type, other);
1378 if (tmp == NULL)
1379 return NULL;
1380 result = set_issubset(so, tmp);
1381 Py_DECREF(tmp);
1382 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001383 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001384 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001385 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001386
Raymond Hettinger99220fa2005-08-06 18:57:13 +00001387 entry = &so->table[0];
1388 for (i=so->used ; i ; entry++, i--) {
1389 while (entry->key == NULL || entry->key==dummy)
1390 entry++;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001391 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001392 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001393 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001394 Py_RETURN_TRUE;
1395}
1396
1397PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1398
1399static PyObject *
1400set_issuperset(PySetObject *so, PyObject *other)
1401{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001402 PyObject *tmp, *result;
1403
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001404 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001405 tmp = make_new_set(&PySet_Type, other);
1406 if (tmp == NULL)
1407 return NULL;
1408 result = set_issuperset(so, tmp);
1409 Py_DECREF(tmp);
1410 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001411 }
1412 return set_issubset((PySetObject *)other, (PyObject *)so);
1413}
1414
1415PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1416
1417static long
1418set_nohash(PyObject *self)
1419{
1420 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1421 return -1;
1422}
1423
1424static int
1425set_nocmp(PyObject *self)
1426{
1427 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1428 return -1;
1429}
1430
1431static long
1432frozenset_hash(PyObject *self)
1433{
Raymond Hettingera690a992003-11-16 16:17:49 +00001434 PySetObject *so = (PySetObject *)self;
Raymond Hettingera580c472005-08-05 17:19:54 +00001435 long h, hash = 1927868237L;
1436 setentry *entry;
1437 int i;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001438
Raymond Hettingera690a992003-11-16 16:17:49 +00001439 if (so->hash != -1)
1440 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001441
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001442 hash *= set_len(self) + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +00001443 entry = &so->table[0];
1444 for (i=so->used ; i ; entry++, i--) {
1445 while (entry->key == NULL || entry->key==dummy)
1446 entry++;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001447 /* Work to increase the bit dispersion for closely spaced hash
1448 values. The is important because some use cases have many
1449 combinations of a small number of elements with nearby
1450 hashes so that many distinct combinations collapse to only
1451 a handful of distinct hash values. */
Raymond Hettingera9d99362005-08-05 00:01:15 +00001452 h = entry->hash;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001453 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001454 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001455 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001456 if (hash == -1)
1457 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001458 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001459 return hash;
1460}
1461
1462static PyObject *
1463set_richcompare(PySetObject *v, PyObject *w, int op)
1464{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001465 PyObject *r1, *r2;
1466
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001467 if(!PyAnySet_Check(w)) {
1468 if (op == Py_EQ)
1469 Py_RETURN_FALSE;
1470 if (op == Py_NE)
1471 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001472 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1473 return NULL;
1474 }
1475 switch (op) {
1476 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001477 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001478 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001479 return set_issubset(v, w);
1480 case Py_NE:
1481 if (set_len((PyObject *)v) != set_len(w))
1482 Py_RETURN_TRUE;
1483 r1 = set_issubset(v, w);
1484 assert (r1 != NULL);
1485 r2 = PyBool_FromLong(PyObject_Not(r1));
1486 Py_DECREF(r1);
1487 return r2;
1488 case Py_LE:
1489 return set_issubset(v, w);
1490 case Py_GE:
1491 return set_issuperset(v, w);
1492 case Py_LT:
1493 if (set_len((PyObject *)v) >= set_len(w))
1494 Py_RETURN_FALSE;
1495 return set_issubset(v, w);
1496 case Py_GT:
1497 if (set_len((PyObject *)v) <= set_len(w))
1498 Py_RETURN_FALSE;
1499 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001500 }
1501 Py_INCREF(Py_NotImplemented);
1502 return Py_NotImplemented;
1503}
1504
1505static PyObject *
1506set_repr(PySetObject *so)
1507{
1508 PyObject *keys, *result, *listrepr;
1509
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001510 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001511 if (keys == NULL)
1512 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001513 listrepr = PyObject_Repr(keys);
1514 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001515 if (listrepr == NULL)
1516 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001517
1518 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1519 PyString_AS_STRING(listrepr));
1520 Py_DECREF(listrepr);
1521 return result;
1522}
1523
1524static int
1525set_tp_print(PySetObject *so, FILE *fp, int flags)
1526{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001527 setentry *entry;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001528 int pos=0;
1529 char *emit = ""; /* No separator emitted on first pass */
1530 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001531
Raymond Hettingera690a992003-11-16 16:17:49 +00001532 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001533 while (set_next(so, &pos, &entry)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001534 fputs(emit, fp);
1535 emit = separator;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001536 if (PyObject_Print(entry->key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001537 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001538 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001539 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001540 return 0;
1541}
1542
1543static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001544set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001545{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001546 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001547 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001548 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001549}
1550
1551PyDoc_STRVAR(add_doc,
1552"Add an element to a set.\n\
1553\n\
1554This has no effect if the element is already present.");
1555
1556static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001557set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001558{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001559 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001560 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001561
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001562 rv = set_discard_key(so, key);
1563 if (rv == -1) {
1564 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1565 return NULL;
1566 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001567 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1568 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001569 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001570 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001571 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001572 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001573 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001574 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001575 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001576 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001577 return NULL;
1578 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001579 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001580}
1581
1582PyDoc_STRVAR(remove_doc,
1583"Remove an element from a set; it must be a member.\n\
1584\n\
1585If the element is not a member, raise a KeyError.");
1586
1587static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001588set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001589{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001590 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001591 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001592
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001593 rv = set_discard_key(so, key);
1594 if (rv == -1) {
1595 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1596 return NULL;
1597 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001598 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1599 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001600 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001601 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001602 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001603 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001604 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001605 return result;
1606 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001607 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001608}
1609
1610PyDoc_STRVAR(discard_doc,
1611"Remove an element from a set if it is a member.\n\
1612\n\
1613If the element is not a member, do nothing.");
1614
1615static PyObject *
1616set_pop(PySetObject *so)
1617{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001618 PyObject *key;
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001619 register setentry *entry;
1620 register int i = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001621
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001622 assert (PyAnySet_Check(so));
1623 if (so->used == 0) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001624 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1625 return NULL;
1626 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001627
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001628 /* Set entry to "the first" unused or dummy set entry. We abuse
1629 * the hash field of slot 0 to hold a search finger:
1630 * If slot 0 has a value, use slot 0.
1631 * Else slot 0 is being used to hold a search finger,
1632 * and we use its hash value as the first index to look.
1633 */
1634 entry = &so->table[0];
1635 if (entry->key == NULL || entry->key == dummy) {
1636 i = (int)entry->hash;
1637 /* The hash field may be a real hash value, or it may be a
1638 * legit search finger, or it may be a once-legit search
1639 * finger that's out of bounds now because it wrapped around
1640 * or the table shrunk -- simply make sure it's in bounds now.
1641 */
1642 if (i > so->mask || i < 1)
1643 i = 1; /* skip slot 0 */
1644 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
1645 i++;
1646 if (i > so->mask)
1647 i = 1;
1648 }
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001649 }
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001650 key = entry->key;
1651 Py_INCREF(dummy);
1652 entry->key = dummy;
1653 so->used--;
1654 so->table[0].hash = i + 1; /* next place to start */
Raymond Hettingera690a992003-11-16 16:17:49 +00001655 return key;
1656}
1657
1658PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1659
1660static PyObject *
1661set_reduce(PySetObject *so)
1662{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001663 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001664
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001665 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001666 if (keys == NULL)
1667 goto done;
1668 args = PyTuple_Pack(1, keys);
1669 if (args == NULL)
1670 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001671 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1672 if (dict == NULL) {
1673 PyErr_Clear();
1674 dict = Py_None;
1675 Py_INCREF(dict);
1676 }
1677 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001678done:
1679 Py_XDECREF(args);
1680 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001681 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001682 return result;
1683}
1684
1685PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1686
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001687static int
1688set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1689{
1690 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001691
1692 if (!PyAnySet_Check(self))
1693 return -1;
1694 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1695 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001696 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001697 self->hash = -1;
1698 if (iterable == NULL)
1699 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001700 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001701}
1702
Raymond Hettingera690a992003-11-16 16:17:49 +00001703static PySequenceMethods set_as_sequence = {
1704 (inquiry)set_len, /* sq_length */
1705 0, /* sq_concat */
1706 0, /* sq_repeat */
1707 0, /* sq_item */
1708 0, /* sq_slice */
1709 0, /* sq_ass_item */
1710 0, /* sq_ass_slice */
1711 (objobjproc)set_contains, /* sq_contains */
1712};
1713
1714/* set object ********************************************************/
1715
1716static PyMethodDef set_methods[] = {
1717 {"add", (PyCFunction)set_add, METH_O,
1718 add_doc},
1719 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1720 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001721 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001722 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001723 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1724 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001725 {"discard", (PyCFunction)set_discard, METH_O,
1726 discard_doc},
1727 {"difference", (PyCFunction)set_difference, METH_O,
1728 difference_doc},
1729 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1730 difference_update_doc},
1731 {"intersection",(PyCFunction)set_intersection, METH_O,
1732 intersection_doc},
1733 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1734 intersection_update_doc},
1735 {"issubset", (PyCFunction)set_issubset, METH_O,
1736 issubset_doc},
1737 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1738 issuperset_doc},
1739 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1740 pop_doc},
1741 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1742 reduce_doc},
1743 {"remove", (PyCFunction)set_remove, METH_O,
1744 remove_doc},
1745 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1746 symmetric_difference_doc},
1747 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1748 symmetric_difference_update_doc},
1749 {"union", (PyCFunction)set_union, METH_O,
1750 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001751 {"update", (PyCFunction)set_update, METH_O,
1752 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001753 {NULL, NULL} /* sentinel */
1754};
1755
1756static PyNumberMethods set_as_number = {
1757 0, /*nb_add*/
1758 (binaryfunc)set_sub, /*nb_subtract*/
1759 0, /*nb_multiply*/
1760 0, /*nb_divide*/
1761 0, /*nb_remainder*/
1762 0, /*nb_divmod*/
1763 0, /*nb_power*/
1764 0, /*nb_negative*/
1765 0, /*nb_positive*/
1766 0, /*nb_absolute*/
1767 0, /*nb_nonzero*/
1768 0, /*nb_invert*/
1769 0, /*nb_lshift*/
1770 0, /*nb_rshift*/
1771 (binaryfunc)set_and, /*nb_and*/
1772 (binaryfunc)set_xor, /*nb_xor*/
1773 (binaryfunc)set_or, /*nb_or*/
1774 0, /*nb_coerce*/
1775 0, /*nb_int*/
1776 0, /*nb_long*/
1777 0, /*nb_float*/
1778 0, /*nb_oct*/
1779 0, /*nb_hex*/
1780 0, /*nb_inplace_add*/
1781 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1782 0, /*nb_inplace_multiply*/
1783 0, /*nb_inplace_divide*/
1784 0, /*nb_inplace_remainder*/
1785 0, /*nb_inplace_power*/
1786 0, /*nb_inplace_lshift*/
1787 0, /*nb_inplace_rshift*/
1788 (binaryfunc)set_iand, /*nb_inplace_and*/
1789 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1790 (binaryfunc)set_ior, /*nb_inplace_or*/
1791};
1792
1793PyDoc_STRVAR(set_doc,
1794"set(iterable) --> set object\n\
1795\n\
1796Build an unordered collection.");
1797
1798PyTypeObject PySet_Type = {
1799 PyObject_HEAD_INIT(&PyType_Type)
1800 0, /* ob_size */
1801 "set", /* tp_name */
1802 sizeof(PySetObject), /* tp_basicsize */
1803 0, /* tp_itemsize */
1804 /* methods */
1805 (destructor)set_dealloc, /* tp_dealloc */
1806 (printfunc)set_tp_print, /* tp_print */
1807 0, /* tp_getattr */
1808 0, /* tp_setattr */
1809 (cmpfunc)set_nocmp, /* tp_compare */
1810 (reprfunc)set_repr, /* tp_repr */
1811 &set_as_number, /* tp_as_number */
1812 &set_as_sequence, /* tp_as_sequence */
1813 0, /* tp_as_mapping */
1814 set_nohash, /* tp_hash */
1815 0, /* tp_call */
1816 0, /* tp_str */
1817 PyObject_GenericGetAttr, /* tp_getattro */
1818 0, /* tp_setattro */
1819 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001820 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001821 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001822 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001823 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001824 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001825 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001826 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001827 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001828 0, /* tp_iternext */
1829 set_methods, /* tp_methods */
1830 0, /* tp_members */
1831 0, /* tp_getset */
1832 0, /* tp_base */
1833 0, /* tp_dict */
1834 0, /* tp_descr_get */
1835 0, /* tp_descr_set */
1836 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001837 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001838 PyType_GenericAlloc, /* tp_alloc */
1839 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001840 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001841};
1842
1843/* frozenset object ********************************************************/
1844
1845
1846static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001847 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001848 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001849 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001850 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001851 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001852 difference_doc},
1853 {"intersection",(PyCFunction)set_intersection, METH_O,
1854 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001855 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001856 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001857 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001858 issuperset_doc},
1859 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1860 reduce_doc},
1861 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1862 symmetric_difference_doc},
1863 {"union", (PyCFunction)set_union, METH_O,
1864 union_doc},
1865 {NULL, NULL} /* sentinel */
1866};
1867
1868static PyNumberMethods frozenset_as_number = {
1869 0, /*nb_add*/
1870 (binaryfunc)set_sub, /*nb_subtract*/
1871 0, /*nb_multiply*/
1872 0, /*nb_divide*/
1873 0, /*nb_remainder*/
1874 0, /*nb_divmod*/
1875 0, /*nb_power*/
1876 0, /*nb_negative*/
1877 0, /*nb_positive*/
1878 0, /*nb_absolute*/
1879 0, /*nb_nonzero*/
1880 0, /*nb_invert*/
1881 0, /*nb_lshift*/
1882 0, /*nb_rshift*/
1883 (binaryfunc)set_and, /*nb_and*/
1884 (binaryfunc)set_xor, /*nb_xor*/
1885 (binaryfunc)set_or, /*nb_or*/
1886};
1887
1888PyDoc_STRVAR(frozenset_doc,
1889"frozenset(iterable) --> frozenset object\n\
1890\n\
1891Build an immutable unordered collection.");
1892
1893PyTypeObject PyFrozenSet_Type = {
1894 PyObject_HEAD_INIT(&PyType_Type)
1895 0, /* ob_size */
1896 "frozenset", /* tp_name */
1897 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001898 0, /* tp_itemsize */
1899 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001900 (destructor)set_dealloc, /* tp_dealloc */
1901 (printfunc)set_tp_print, /* tp_print */
1902 0, /* tp_getattr */
1903 0, /* tp_setattr */
1904 (cmpfunc)set_nocmp, /* tp_compare */
1905 (reprfunc)set_repr, /* tp_repr */
1906 &frozenset_as_number, /* tp_as_number */
1907 &set_as_sequence, /* tp_as_sequence */
1908 0, /* tp_as_mapping */
1909 frozenset_hash, /* tp_hash */
1910 0, /* tp_call */
1911 0, /* tp_str */
1912 PyObject_GenericGetAttr, /* tp_getattro */
1913 0, /* tp_setattro */
1914 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001915 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001916 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001917 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001918 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001919 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001920 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001921 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001922 (getiterfunc)set_iter, /* tp_iter */
1923 0, /* tp_iternext */
1924 frozenset_methods, /* tp_methods */
1925 0, /* tp_members */
1926 0, /* tp_getset */
1927 0, /* tp_base */
1928 0, /* tp_dict */
1929 0, /* tp_descr_get */
1930 0, /* tp_descr_set */
1931 0, /* tp_dictoffset */
1932 0, /* tp_init */
1933 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001934 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001935 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001936};