blob: 4fd3672ad007eb3d4a3daabc9fb2c8b2639c680c [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 Hettinger19c2d772003-11-21 18:36:54 +0000935 int result;
936
Raymond Hettingerc991db22005-08-11 07:58:45 +0000937 result = set_contains_key(so, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000938 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000939 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000940 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
941 if (tmpkey == NULL)
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000942 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000943 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000944 result = set_contains_key(so, tmpkey);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000945 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
946 Py_DECREF(tmpkey);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000947 }
948 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000949}
950
951static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000952set_direct_contains(PySetObject *so, PyObject *key)
953{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000954 long result;
955
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000956 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000957 if (result == -1)
958 return NULL;
959 return PyBool_FromLong(result);
960}
961
962PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
963
964static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000965set_copy(PySetObject *so)
966{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000967 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000968}
969
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000970static PyObject *
971frozenset_copy(PySetObject *so)
972{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000973 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000974 Py_INCREF(so);
975 return (PyObject *)so;
976 }
977 return set_copy(so);
978}
979
Raymond Hettingera690a992003-11-16 16:17:49 +0000980PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
981
982static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000983set_clear(PySetObject *so)
984{
985 set_clear_internal(so);
986 Py_RETURN_NONE;
987}
988
989PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
990
991static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000992set_union(PySetObject *so, PyObject *other)
993{
994 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000995
996 result = (PySetObject *)set_copy(so);
997 if (result == NULL)
998 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000999 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001000 Py_DECREF(result);
1001 return NULL;
1002 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001003 return (PyObject *)result;
1004}
1005
1006PyDoc_STRVAR(union_doc,
1007 "Return the union of two sets as a new set.\n\
1008\n\
1009(i.e. all elements that are in either set.)");
1010
1011static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001012set_or(PySetObject *so, PyObject *other)
1013{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001014 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001015 Py_INCREF(Py_NotImplemented);
1016 return Py_NotImplemented;
1017 }
1018 return set_union(so, other);
1019}
1020
1021static PyObject *
1022set_ior(PySetObject *so, PyObject *other)
1023{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001024 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001025 Py_INCREF(Py_NotImplemented);
1026 return Py_NotImplemented;
1027 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001028 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001029 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001030 Py_INCREF(so);
1031 return (PyObject *)so;
1032}
1033
1034static PyObject *
1035set_intersection(PySetObject *so, PyObject *other)
1036{
1037 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001038 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001039
Raymond Hettingerc991db22005-08-11 07:58:45 +00001040 if ((PyObject *)so == other) {
1041 Py_INCREF(other);
1042 return other;
1043 }
1044
Raymond Hettingera690a992003-11-16 16:17:49 +00001045 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1046 if (result == NULL)
1047 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001048
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001049 if (PyAnySet_Check(other) && set_len(other) > set_len((PyObject *)so)) {
1050 tmp = (PyObject *)so;
1051 so = (PySetObject *)other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001052 other = tmp;
1053 }
1054
Raymond Hettingerc991db22005-08-11 07:58:45 +00001055 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001056 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001057 setentry *entry;
1058 while (set_next((PySetObject *)other, &pos, &entry)) {
1059 if (set_contains_entry(so, entry)) {
1060 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001061 Py_DECREF(result);
1062 return NULL;
1063 }
1064 }
1065 }
1066 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001067 }
1068
Raymond Hettingera690a992003-11-16 16:17:49 +00001069 it = PyObject_GetIter(other);
1070 if (it == NULL) {
1071 Py_DECREF(result);
1072 return NULL;
1073 }
1074
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001075 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001076 if (set_contains_key(so, key)) {
1077 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001078 Py_DECREF(it);
1079 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001080 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001081 return NULL;
1082 }
1083 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001084 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001085 }
1086 Py_DECREF(it);
1087 if (PyErr_Occurred()) {
1088 Py_DECREF(result);
1089 return NULL;
1090 }
1091 return (PyObject *)result;
1092}
1093
1094PyDoc_STRVAR(intersection_doc,
1095"Return the intersection of two sets as a new set.\n\
1096\n\
1097(i.e. all elements that are in both sets.)");
1098
1099static PyObject *
1100set_intersection_update(PySetObject *so, PyObject *other)
1101{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001102 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001103
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001104 tmp = set_intersection(so, other);
1105 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001106 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001107 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001108 Py_DECREF(tmp);
1109 Py_RETURN_NONE;
1110}
1111
1112PyDoc_STRVAR(intersection_update_doc,
1113"Update a set with the intersection of itself and another.");
1114
1115static PyObject *
1116set_and(PySetObject *so, PyObject *other)
1117{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001118 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001119 Py_INCREF(Py_NotImplemented);
1120 return Py_NotImplemented;
1121 }
1122 return set_intersection(so, other);
1123}
1124
1125static PyObject *
1126set_iand(PySetObject *so, PyObject *other)
1127{
1128 PyObject *result;
1129
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001130 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001131 Py_INCREF(Py_NotImplemented);
1132 return Py_NotImplemented;
1133 }
1134 result = set_intersection_update(so, other);
1135 if (result == NULL)
1136 return NULL;
1137 Py_DECREF(result);
1138 Py_INCREF(so);
1139 return (PyObject *)so;
1140}
1141
Raymond Hettingerc991db22005-08-11 07:58:45 +00001142int
1143set_difference_update_internal(PySetObject *so, PyObject *other)
1144{
1145 if ((PyObject *)so == other)
1146 return set_clear_internal(so);
1147
1148 if (PyAnySet_Check(other)) {
1149 setentry *entry;
1150 int pos = 0;
1151
1152 while (set_next((PySetObject *)other, &pos, &entry))
1153 set_discard_entry(so, entry);
1154 } else {
1155 PyObject *key, *it;
1156 it = PyObject_GetIter(other);
1157 if (it == NULL)
1158 return -1;
1159
1160 while ((key = PyIter_Next(it)) != NULL) {
1161 if (set_discard_key(so, key) == -1) {
1162 Py_DECREF(it);
1163 Py_DECREF(key);
1164 return -1;
1165 }
1166 Py_DECREF(key);
1167 }
1168 Py_DECREF(it);
1169 if (PyErr_Occurred())
1170 return -1;
1171 }
1172 /* If more than 1/5 are dummies, then resize them away. */
1173 if ((so->fill - so->used) * 5 < so->mask)
1174 return 0;
1175 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1176}
1177
Raymond Hettingera690a992003-11-16 16:17:49 +00001178static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001179set_difference_update(PySetObject *so, PyObject *other)
1180{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001181 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001182 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001183 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001184}
1185
1186PyDoc_STRVAR(difference_update_doc,
1187"Remove all elements of another set from this set.");
1188
1189static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001190set_difference(PySetObject *so, PyObject *other)
1191{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001192 PyObject *result;
1193 setentry *entry;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001194 int pos = 0;
1195
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001196 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001197 result = set_copy(so);
1198 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001199 return NULL;
1200 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001201 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001202 Py_DECREF(result);
1203 return NULL;
1204 }
1205
1206 result = make_new_set(so->ob_type, NULL);
1207 if (result == NULL)
1208 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001209
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001210 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001211 while (set_next(so, &pos, &entry)) {
1212 setentry entrycopy;
1213 entrycopy.hash = entry->hash;
1214 entrycopy.key = entry->key;
1215 if (!PyDict_Contains(other, entry->key)) {
1216 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001217 return NULL;
1218 }
1219 }
1220 return result;
1221 }
1222
Raymond Hettingerc991db22005-08-11 07:58:45 +00001223 while (set_next(so, &pos, &entry)) {
1224 if (!set_contains_entry((PySetObject *)other, entry)) {
1225 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001226 return NULL;
1227 }
1228 }
1229 return result;
1230}
1231
1232PyDoc_STRVAR(difference_doc,
1233"Return the difference of two sets as a new set.\n\
1234\n\
1235(i.e. all elements that are in this set but not the other.)");
1236static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001237set_sub(PySetObject *so, PyObject *other)
1238{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001239 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001240 Py_INCREF(Py_NotImplemented);
1241 return Py_NotImplemented;
1242 }
1243 return set_difference(so, other);
1244}
1245
1246static PyObject *
1247set_isub(PySetObject *so, PyObject *other)
1248{
1249 PyObject *result;
1250
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001251 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001252 Py_INCREF(Py_NotImplemented);
1253 return Py_NotImplemented;
1254 }
1255 result = set_difference_update(so, other);
1256 if (result == NULL)
1257 return NULL;
1258 Py_DECREF(result);
1259 Py_INCREF(so);
1260 return (PyObject *)so;
1261}
1262
1263static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001264set_symmetric_difference_update(PySetObject *so, PyObject *other)
1265{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001266 PySetObject *otherset;
1267 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001268 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001269 setentry *entry;
1270
1271 if ((PyObject *)so == other)
1272 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001273
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001274 if (PyDict_Check(other)) {
1275 PyObject *value;
1276 int rv;
1277 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001278 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001279 if (rv == -1)
1280 return NULL;
1281 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001282 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001283 return NULL;
1284 }
1285 }
1286 Py_RETURN_NONE;
1287 }
1288
1289 if (PyAnySet_Check(other)) {
1290 Py_INCREF(other);
1291 otherset = (PySetObject *)other;
1292 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001293 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1294 if (otherset == NULL)
1295 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001296 }
1297
Raymond Hettingerc991db22005-08-11 07:58:45 +00001298 while (set_next(otherset, &pos, &entry)) {
1299 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001300 if (rv == -1) {
1301 Py_XDECREF(otherset);
1302 return NULL;
1303 }
1304 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001305 if (set_add_entry(so, entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001306 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001307 return NULL;
1308 }
1309 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001310 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001311 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001312 Py_RETURN_NONE;
1313}
1314
1315PyDoc_STRVAR(symmetric_difference_update_doc,
1316"Update a set with the symmetric difference of itself and another.");
1317
1318static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001319set_symmetric_difference(PySetObject *so, PyObject *other)
1320{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001321 PyObject *rv;
1322 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001323
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001324 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1325 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001326 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001327 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1328 if (rv == NULL)
1329 return NULL;
1330 Py_DECREF(rv);
1331 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001332}
1333
1334PyDoc_STRVAR(symmetric_difference_doc,
1335"Return the symmetric difference of two sets as a new set.\n\
1336\n\
1337(i.e. all elements that are in exactly one of the sets.)");
1338
1339static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001340set_xor(PySetObject *so, PyObject *other)
1341{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001342 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001343 Py_INCREF(Py_NotImplemented);
1344 return Py_NotImplemented;
1345 }
1346 return set_symmetric_difference(so, other);
1347}
1348
1349static PyObject *
1350set_ixor(PySetObject *so, PyObject *other)
1351{
1352 PyObject *result;
1353
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001354 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001355 Py_INCREF(Py_NotImplemented);
1356 return Py_NotImplemented;
1357 }
1358 result = set_symmetric_difference_update(so, other);
1359 if (result == NULL)
1360 return NULL;
1361 Py_DECREF(result);
1362 Py_INCREF(so);
1363 return (PyObject *)so;
1364}
1365
1366static PyObject *
1367set_issubset(PySetObject *so, PyObject *other)
1368{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001369 PyObject *tmp, *result;
Raymond Hettinger99220fa2005-08-06 18:57:13 +00001370 register setentry *entry;
1371 register int i;
Raymond Hettingera690a992003-11-16 16:17:49 +00001372
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001373 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001374 tmp = make_new_set(&PySet_Type, other);
1375 if (tmp == NULL)
1376 return NULL;
1377 result = set_issubset(so, tmp);
1378 Py_DECREF(tmp);
1379 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001380 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001381 if (set_len((PyObject *)so) > set_len(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001382 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001383
Raymond Hettinger99220fa2005-08-06 18:57:13 +00001384 entry = &so->table[0];
1385 for (i=so->used ; i ; entry++, i--) {
1386 while (entry->key == NULL || entry->key==dummy)
1387 entry++;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001388 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001389 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001390 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001391 Py_RETURN_TRUE;
1392}
1393
1394PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1395
1396static PyObject *
1397set_issuperset(PySetObject *so, PyObject *other)
1398{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001399 PyObject *tmp, *result;
1400
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001401 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001402 tmp = make_new_set(&PySet_Type, other);
1403 if (tmp == NULL)
1404 return NULL;
1405 result = set_issuperset(so, tmp);
1406 Py_DECREF(tmp);
1407 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001408 }
1409 return set_issubset((PySetObject *)other, (PyObject *)so);
1410}
1411
1412PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1413
1414static long
1415set_nohash(PyObject *self)
1416{
1417 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1418 return -1;
1419}
1420
1421static int
1422set_nocmp(PyObject *self)
1423{
1424 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1425 return -1;
1426}
1427
1428static long
1429frozenset_hash(PyObject *self)
1430{
Raymond Hettingera690a992003-11-16 16:17:49 +00001431 PySetObject *so = (PySetObject *)self;
Raymond Hettingera580c472005-08-05 17:19:54 +00001432 long h, hash = 1927868237L;
1433 setentry *entry;
1434 int i;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001435
Raymond Hettingera690a992003-11-16 16:17:49 +00001436 if (so->hash != -1)
1437 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001438
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001439 hash *= set_len(self) + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +00001440 entry = &so->table[0];
1441 for (i=so->used ; i ; entry++, i--) {
1442 while (entry->key == NULL || entry->key==dummy)
1443 entry++;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001444 /* Work to increase the bit dispersion for closely spaced hash
1445 values. The is important because some use cases have many
1446 combinations of a small number of elements with nearby
1447 hashes so that many distinct combinations collapse to only
1448 a handful of distinct hash values. */
Raymond Hettingera9d99362005-08-05 00:01:15 +00001449 h = entry->hash;
Raymond Hettingerc9786332004-06-10 22:41:48 +00001450 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +00001451 }
Raymond Hettingerc9786332004-06-10 22:41:48 +00001452 hash = hash * 69069L + 907133923L;
Raymond Hettinger27e403e2004-06-10 21:38:41 +00001453 if (hash == -1)
1454 hash = 590923713L;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001455 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +00001456 return hash;
1457}
1458
1459static PyObject *
1460set_richcompare(PySetObject *v, PyObject *w, int op)
1461{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001462 PyObject *r1, *r2;
1463
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001464 if(!PyAnySet_Check(w)) {
1465 if (op == Py_EQ)
1466 Py_RETURN_FALSE;
1467 if (op == Py_NE)
1468 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001469 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1470 return NULL;
1471 }
1472 switch (op) {
1473 case Py_EQ:
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001474 if (set_len((PyObject *)v) != set_len(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001475 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001476 return set_issubset(v, w);
1477 case Py_NE:
1478 if (set_len((PyObject *)v) != set_len(w))
1479 Py_RETURN_TRUE;
1480 r1 = set_issubset(v, w);
1481 assert (r1 != NULL);
1482 r2 = PyBool_FromLong(PyObject_Not(r1));
1483 Py_DECREF(r1);
1484 return r2;
1485 case Py_LE:
1486 return set_issubset(v, w);
1487 case Py_GE:
1488 return set_issuperset(v, w);
1489 case Py_LT:
1490 if (set_len((PyObject *)v) >= set_len(w))
1491 Py_RETURN_FALSE;
1492 return set_issubset(v, w);
1493 case Py_GT:
1494 if (set_len((PyObject *)v) <= set_len(w))
1495 Py_RETURN_FALSE;
1496 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001497 }
1498 Py_INCREF(Py_NotImplemented);
1499 return Py_NotImplemented;
1500}
1501
1502static PyObject *
1503set_repr(PySetObject *so)
1504{
1505 PyObject *keys, *result, *listrepr;
1506
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001507 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001508 if (keys == NULL)
1509 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001510 listrepr = PyObject_Repr(keys);
1511 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001512 if (listrepr == NULL)
1513 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001514
1515 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1516 PyString_AS_STRING(listrepr));
1517 Py_DECREF(listrepr);
1518 return result;
1519}
1520
1521static int
1522set_tp_print(PySetObject *so, FILE *fp, int flags)
1523{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001524 setentry *entry;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001525 int pos=0;
1526 char *emit = ""; /* No separator emitted on first pass */
1527 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001528
Raymond Hettingera690a992003-11-16 16:17:49 +00001529 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001530 while (set_next(so, &pos, &entry)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001531 fputs(emit, fp);
1532 emit = separator;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001533 if (PyObject_Print(entry->key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001534 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001535 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001536 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001537 return 0;
1538}
1539
1540static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001541set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001542{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001543 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001544 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001545 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001546}
1547
1548PyDoc_STRVAR(add_doc,
1549"Add an element to a set.\n\
1550\n\
1551This has no effect if the element is already present.");
1552
1553static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001554set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001555{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001556 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001557 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001558
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001559 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1560 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1561 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001562 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001563 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1564 result = set_remove(so, tmpkey);
1565 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1566 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001567 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001568 }
Raymond Hettinger0deab622003-12-13 18:53:18 +00001569
Raymond Hettingerc991db22005-08-11 07:58:45 +00001570 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001571 if (rv == -1)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001572 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001573 else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001574 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001575 return NULL;
1576 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001577 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001578}
1579
1580PyDoc_STRVAR(remove_doc,
1581"Remove an element from a set; it must be a member.\n\
1582\n\
1583If the element is not a member, raise a KeyError.");
1584
1585static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001586set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001587{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001588 PyObject *tmpkey, *result;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001589
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001590 if (PyType_IsSubtype(key->ob_type, &PySet_Type)) {
1591 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1592 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001593 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001594 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1595 result = set_discard(so, tmpkey);
1596 set_swap_bodies((PySetObject *)key, (PySetObject *)tmpkey);
1597 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001598 return result;
1599 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001600
Raymond Hettingerc991db22005-08-11 07:58:45 +00001601 if (set_discard_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001602 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001603 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001604}
1605
1606PyDoc_STRVAR(discard_doc,
1607"Remove an element from a set if it is a member.\n\
1608\n\
1609If the element is not a member, do nothing.");
1610
1611static PyObject *
1612set_pop(PySetObject *so)
1613{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001614 PyObject *key;
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001615 register setentry *entry;
1616 register int i = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001617
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001618 assert (PyAnySet_Check(so));
1619 if (so->used == 0) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001620 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
1621 return NULL;
1622 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001623
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001624 /* Set entry to "the first" unused or dummy set entry. We abuse
1625 * the hash field of slot 0 to hold a search finger:
1626 * If slot 0 has a value, use slot 0.
1627 * Else slot 0 is being used to hold a search finger,
1628 * and we use its hash value as the first index to look.
1629 */
1630 entry = &so->table[0];
1631 if (entry->key == NULL || entry->key == dummy) {
1632 i = (int)entry->hash;
1633 /* The hash field may be a real hash value, or it may be a
1634 * legit search finger, or it may be a once-legit search
1635 * finger that's out of bounds now because it wrapped around
1636 * or the table shrunk -- simply make sure it's in bounds now.
1637 */
1638 if (i > so->mask || i < 1)
1639 i = 1; /* skip slot 0 */
1640 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
1641 i++;
1642 if (i > so->mask)
1643 i = 1;
1644 }
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001645 }
Raymond Hettinger67962ab2005-08-02 03:45:16 +00001646 key = entry->key;
1647 Py_INCREF(dummy);
1648 entry->key = dummy;
1649 so->used--;
1650 so->table[0].hash = i + 1; /* next place to start */
Raymond Hettingera690a992003-11-16 16:17:49 +00001651 return key;
1652}
1653
1654PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
1655
1656static PyObject *
1657set_reduce(PySetObject *so)
1658{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001659 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001660
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001661 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001662 if (keys == NULL)
1663 goto done;
1664 args = PyTuple_Pack(1, keys);
1665 if (args == NULL)
1666 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001667 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1668 if (dict == NULL) {
1669 PyErr_Clear();
1670 dict = Py_None;
1671 Py_INCREF(dict);
1672 }
1673 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001674done:
1675 Py_XDECREF(args);
1676 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001677 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001678 return result;
1679}
1680
1681PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1682
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001683static int
1684set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1685{
1686 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001687
1688 if (!PyAnySet_Check(self))
1689 return -1;
1690 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1691 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001692 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001693 self->hash = -1;
1694 if (iterable == NULL)
1695 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001696 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001697}
1698
Raymond Hettingera690a992003-11-16 16:17:49 +00001699static PySequenceMethods set_as_sequence = {
1700 (inquiry)set_len, /* sq_length */
1701 0, /* sq_concat */
1702 0, /* sq_repeat */
1703 0, /* sq_item */
1704 0, /* sq_slice */
1705 0, /* sq_ass_item */
1706 0, /* sq_ass_slice */
1707 (objobjproc)set_contains, /* sq_contains */
1708};
1709
1710/* set object ********************************************************/
1711
1712static PyMethodDef set_methods[] = {
1713 {"add", (PyCFunction)set_add, METH_O,
1714 add_doc},
1715 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1716 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001717 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001718 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001719 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1720 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001721 {"discard", (PyCFunction)set_discard, METH_O,
1722 discard_doc},
1723 {"difference", (PyCFunction)set_difference, METH_O,
1724 difference_doc},
1725 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1726 difference_update_doc},
1727 {"intersection",(PyCFunction)set_intersection, METH_O,
1728 intersection_doc},
1729 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1730 intersection_update_doc},
1731 {"issubset", (PyCFunction)set_issubset, METH_O,
1732 issubset_doc},
1733 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1734 issuperset_doc},
1735 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1736 pop_doc},
1737 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1738 reduce_doc},
1739 {"remove", (PyCFunction)set_remove, METH_O,
1740 remove_doc},
1741 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1742 symmetric_difference_doc},
1743 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1744 symmetric_difference_update_doc},
1745 {"union", (PyCFunction)set_union, METH_O,
1746 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001747 {"update", (PyCFunction)set_update, METH_O,
1748 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001749 {NULL, NULL} /* sentinel */
1750};
1751
1752static PyNumberMethods set_as_number = {
1753 0, /*nb_add*/
1754 (binaryfunc)set_sub, /*nb_subtract*/
1755 0, /*nb_multiply*/
1756 0, /*nb_divide*/
1757 0, /*nb_remainder*/
1758 0, /*nb_divmod*/
1759 0, /*nb_power*/
1760 0, /*nb_negative*/
1761 0, /*nb_positive*/
1762 0, /*nb_absolute*/
1763 0, /*nb_nonzero*/
1764 0, /*nb_invert*/
1765 0, /*nb_lshift*/
1766 0, /*nb_rshift*/
1767 (binaryfunc)set_and, /*nb_and*/
1768 (binaryfunc)set_xor, /*nb_xor*/
1769 (binaryfunc)set_or, /*nb_or*/
1770 0, /*nb_coerce*/
1771 0, /*nb_int*/
1772 0, /*nb_long*/
1773 0, /*nb_float*/
1774 0, /*nb_oct*/
1775 0, /*nb_hex*/
1776 0, /*nb_inplace_add*/
1777 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1778 0, /*nb_inplace_multiply*/
1779 0, /*nb_inplace_divide*/
1780 0, /*nb_inplace_remainder*/
1781 0, /*nb_inplace_power*/
1782 0, /*nb_inplace_lshift*/
1783 0, /*nb_inplace_rshift*/
1784 (binaryfunc)set_iand, /*nb_inplace_and*/
1785 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1786 (binaryfunc)set_ior, /*nb_inplace_or*/
1787};
1788
1789PyDoc_STRVAR(set_doc,
1790"set(iterable) --> set object\n\
1791\n\
1792Build an unordered collection.");
1793
1794PyTypeObject PySet_Type = {
1795 PyObject_HEAD_INIT(&PyType_Type)
1796 0, /* ob_size */
1797 "set", /* tp_name */
1798 sizeof(PySetObject), /* tp_basicsize */
1799 0, /* tp_itemsize */
1800 /* methods */
1801 (destructor)set_dealloc, /* tp_dealloc */
1802 (printfunc)set_tp_print, /* tp_print */
1803 0, /* tp_getattr */
1804 0, /* tp_setattr */
1805 (cmpfunc)set_nocmp, /* tp_compare */
1806 (reprfunc)set_repr, /* tp_repr */
1807 &set_as_number, /* tp_as_number */
1808 &set_as_sequence, /* tp_as_sequence */
1809 0, /* tp_as_mapping */
1810 set_nohash, /* tp_hash */
1811 0, /* tp_call */
1812 0, /* tp_str */
1813 PyObject_GenericGetAttr, /* tp_getattro */
1814 0, /* tp_setattro */
1815 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001816 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001817 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001818 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001819 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001820 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001821 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001822 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001823 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001824 0, /* tp_iternext */
1825 set_methods, /* tp_methods */
1826 0, /* tp_members */
1827 0, /* tp_getset */
1828 0, /* tp_base */
1829 0, /* tp_dict */
1830 0, /* tp_descr_get */
1831 0, /* tp_descr_set */
1832 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001833 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001834 PyType_GenericAlloc, /* tp_alloc */
1835 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001836 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001837};
1838
1839/* frozenset object ********************************************************/
1840
1841
1842static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001843 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001844 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001845 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001846 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001847 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001848 difference_doc},
1849 {"intersection",(PyCFunction)set_intersection, METH_O,
1850 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001851 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001852 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001853 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001854 issuperset_doc},
1855 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1856 reduce_doc},
1857 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1858 symmetric_difference_doc},
1859 {"union", (PyCFunction)set_union, METH_O,
1860 union_doc},
1861 {NULL, NULL} /* sentinel */
1862};
1863
1864static PyNumberMethods frozenset_as_number = {
1865 0, /*nb_add*/
1866 (binaryfunc)set_sub, /*nb_subtract*/
1867 0, /*nb_multiply*/
1868 0, /*nb_divide*/
1869 0, /*nb_remainder*/
1870 0, /*nb_divmod*/
1871 0, /*nb_power*/
1872 0, /*nb_negative*/
1873 0, /*nb_positive*/
1874 0, /*nb_absolute*/
1875 0, /*nb_nonzero*/
1876 0, /*nb_invert*/
1877 0, /*nb_lshift*/
1878 0, /*nb_rshift*/
1879 (binaryfunc)set_and, /*nb_and*/
1880 (binaryfunc)set_xor, /*nb_xor*/
1881 (binaryfunc)set_or, /*nb_or*/
1882};
1883
1884PyDoc_STRVAR(frozenset_doc,
1885"frozenset(iterable) --> frozenset object\n\
1886\n\
1887Build an immutable unordered collection.");
1888
1889PyTypeObject PyFrozenSet_Type = {
1890 PyObject_HEAD_INIT(&PyType_Type)
1891 0, /* ob_size */
1892 "frozenset", /* tp_name */
1893 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001894 0, /* tp_itemsize */
1895 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001896 (destructor)set_dealloc, /* tp_dealloc */
1897 (printfunc)set_tp_print, /* tp_print */
1898 0, /* tp_getattr */
1899 0, /* tp_setattr */
1900 (cmpfunc)set_nocmp, /* tp_compare */
1901 (reprfunc)set_repr, /* tp_repr */
1902 &frozenset_as_number, /* tp_as_number */
1903 &set_as_sequence, /* tp_as_sequence */
1904 0, /* tp_as_mapping */
1905 frozenset_hash, /* tp_hash */
1906 0, /* tp_call */
1907 0, /* tp_str */
1908 PyObject_GenericGetAttr, /* tp_getattro */
1909 0, /* tp_setattro */
1910 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001911 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001912 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001913 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001914 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001915 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001916 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001917 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001918 (getiterfunc)set_iter, /* tp_iter */
1919 0, /* tp_iternext */
1920 frozenset_methods, /* tp_methods */
1921 0, /* tp_members */
1922 0, /* tp_getset */
1923 0, /* tp_base */
1924 0, /* tp_dict */
1925 0, /* tp_descr_get */
1926 0, /* tp_descr_set */
1927 0, /* tp_dictoffset */
1928 0, /* tp_init */
1929 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001930 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001931 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001932};