blob: ba06cb6e6148a1b875f4775f822b6eaba31b3c95 [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 Hettingered6c1ef2005-08-13 08:28:03 +000064 register int checked_error;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000065 register int cmp;
66 PyObject *err_type, *err_value, *err_tb;
67 PyObject *startkey;
68
69 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000070 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000071 if (entry->key == NULL || entry->key == key)
72 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073
Raymond Hettingered6c1ef2005-08-13 08:28:03 +000074 restore_error = checked_error = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000075 if (entry->key == dummy)
76 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000077 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000078 if (entry->hash == hash) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +000079 /* error can't have been checked yet */
80 checked_error = 1;
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +000081 if (_PyErr_OCCURRED()) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 restore_error = 1;
83 PyErr_Fetch(&err_type, &err_value, &err_tb);
84 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000085 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
87 if (cmp < 0)
88 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +000089 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000090 if (cmp > 0)
91 goto Done;
92 }
93 else {
94 /* The compare did major nasty stuff to the
95 * set: start over.
96 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000097 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000098 goto Done;
99 }
100 }
101 freeslot = NULL;
102 }
103
104 /* In the loop, key == dummy is by far (factor of 100s) the
105 least likely outcome, so test for that last. */
106 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
107 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000108 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000109 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000110 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000111 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000112 break;
113 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000114 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000115 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000116 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000117 if (!checked_error) {
118 checked_error = 1;
119 if (_PyErr_OCCURRED()) {
120 restore_error = 1;
121 PyErr_Fetch(&err_type, &err_value,
122 &err_tb);
123 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
127 if (cmp < 0)
128 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +0000129 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130 if (cmp > 0)
131 break;
132 }
133 else {
134 /* The compare did major nasty stuff to the
135 * set: start over.
136 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000137 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138 break;
139 }
140 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000141 else if (entry->key == dummy && freeslot == NULL)
142 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000143 }
144
145Done:
146 if (restore_error)
147 PyErr_Restore(err_type, err_value, err_tb);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000148 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000149}
150
151/*
152 * Hacked up version of set_lookkey which can assume keys are always strings;
153 * this assumption allows testing for errors during PyObject_Compare() to
154 * be dropped; string-string comparisons never raise exceptions. This also
155 * means we don't need to go through PyObject_Compare(); we can always use
156 * _PyString_Eq directly.
157 *
158 * This is valuable because the general-case error handling in set_lookkey() is
159 * expensive, and sets with pure-string keys may be very common.
160 */
161static setentry *
162set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
163{
164 register int i;
165 register unsigned int perturb;
166 register setentry *freeslot;
167 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000168 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000169 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000170
171 /* Make sure this function doesn't have to handle non-string keys,
172 including subclasses of str; e.g., one reason to subclass
173 strings is to override __eq__, and for speed we don't cater to
174 that here. */
175 if (!PyString_CheckExact(key)) {
176 so->lookup = set_lookkey;
177 return set_lookkey(so, key, hash);
178 }
179 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000180 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000181 if (entry->key == NULL || entry->key == key)
182 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000183 if (entry->key == dummy)
184 freeslot = entry;
185 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000186 if (entry->hash == hash && _PyString_Eq(entry->key, key))
187 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000188 freeslot = NULL;
189 }
190
191 /* In the loop, key == dummy is by far (factor of 100s) the
192 least likely outcome, so test for that last. */
193 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
194 i = (i << 2) + i + perturb + 1;
195 entry = &table[i & mask];
196 if (entry->key == NULL)
197 return freeslot == NULL ? entry : freeslot;
198 if (entry->key == key
199 || (entry->hash == hash
200 && entry->key != dummy
201 && _PyString_Eq(entry->key, key)))
202 return entry;
203 if (entry->key == dummy && freeslot == NULL)
204 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000205 }
206}
207
208/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000210Used both by the internal resize routine and by the public insert routine.
211Eats a reference to key.
212*/
213static void
214set_insert_key(register PySetObject *so, PyObject *key, long hash)
215{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000216 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
218
219 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220 entry = so->lookup(so, key, hash);
221 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 /* UNUSED */
223 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000224 entry->key = key;
225 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000226 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000227 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000228 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000229 entry->key = key;
230 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000231 so->used++;
232 Py_DECREF(dummy);
233 } else {
234 /* ACTIVE */
235 Py_DECREF(key);
236 }
237}
238
239/*
240Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000241keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000242actually be smaller than the old one.
243*/
244static int
245set_table_resize(PySetObject *so, int minused)
246{
247 int newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000248 setentry *oldtable, *newtable, *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000249 int i;
250 int is_oldtable_malloced;
251 setentry small_copy[PySet_MINSIZE];
252
253 assert(minused >= 0);
254
255 /* Find the smallest table size > minused. */
256 for (newsize = PySet_MINSIZE;
257 newsize <= minused && newsize > 0;
258 newsize <<= 1)
259 ;
260 if (newsize <= 0) {
261 PyErr_NoMemory();
262 return -1;
263 }
264
265 /* Get space for a new table. */
266 oldtable = so->table;
267 assert(oldtable != NULL);
268 is_oldtable_malloced = oldtable != so->smalltable;
269
270 if (newsize == PySet_MINSIZE) {
271 /* A large table is shrinking, or we can't get any smaller. */
272 newtable = so->smalltable;
273 if (newtable == oldtable) {
274 if (so->fill == so->used) {
275 /* No dummies, so no point doing anything. */
276 return 0;
277 }
278 /* We're not going to resize it, but rebuild the
279 table anyway to purge old dummy entries.
280 Subtle: This is *necessary* if fill==size,
281 as set_lookkey needs at least one virgin slot to
282 terminate failing searches. If fill < size, it's
283 merely desirable, as dummies slow searches. */
284 assert(so->fill > so->used);
285 memcpy(small_copy, oldtable, sizeof(small_copy));
286 oldtable = small_copy;
287 }
288 }
289 else {
290 newtable = PyMem_NEW(setentry, newsize);
291 if (newtable == NULL) {
292 PyErr_NoMemory();
293 return -1;
294 }
295 }
296
297 /* Make the set empty, using the new table. */
298 assert(newtable != oldtable);
299 so->table = newtable;
300 so->mask = newsize - 1;
301 memset(newtable, 0, sizeof(setentry) * newsize);
302 so->used = 0;
303 i = so->fill;
304 so->fill = 0;
305
306 /* Copy the data over; this is refcount-neutral for active entries;
307 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000308 for (entry = oldtable; i > 0; entry++) {
309 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000310 /* UNUSED */
311 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000312 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000313 /* DUMMY */
314 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000315 assert(entry->key == dummy);
316 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000317 } else {
318 /* ACTIVE */
319 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000320 set_insert_key(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000321 }
322 }
323
324 if (is_oldtable_malloced)
325 PyMem_DEL(oldtable);
326 return 0;
327}
328
Raymond Hettingerc991db22005-08-11 07:58:45 +0000329/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
330
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000332set_add_entry(register PySetObject *so, setentry *entry)
333{
334 register int n_used;
335
336 assert(so->fill <= so->mask); /* at least one empty slot */
337 n_used = so->used;
338 Py_INCREF(entry->key);
339 set_insert_key(so, entry->key, entry->hash);
340 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
341 return 0;
342 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
343}
344
345static int
346set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347{
348 register long hash;
349 register int n_used;
350
Raymond Hettingerc991db22005-08-11 07:58:45 +0000351 if (!PyString_CheckExact(key) ||
352 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000353 hash = PyObject_Hash(key);
354 if (hash == -1)
355 return -1;
356 }
357 assert(so->fill <= so->mask); /* at least one empty slot */
358 n_used = so->used;
359 Py_INCREF(key);
360 set_insert_key(so, key, hash);
361 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
362 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000363 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000364}
365
366#define DISCARD_NOTFOUND 0
367#define DISCARD_FOUND 1
368
369static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000370set_discard_entry(PySetObject *so, setentry *oldentry)
371{ register setentry *entry;
372 PyObject *old_key;
373
374 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
375 if (entry->key == NULL || entry->key == dummy)
376 return DISCARD_NOTFOUND;
377 old_key = entry->key;
378 Py_INCREF(dummy);
379 entry->key = dummy;
380 so->used--;
381 Py_DECREF(old_key);
382 return DISCARD_FOUND;
383}
384
385static int
386set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000387{
388 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000389 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000390 PyObject *old_key;
391
392 assert (PyAnySet_Check(so));
393 if (!PyString_CheckExact(key) ||
394 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
395 hash = PyObject_Hash(key);
396 if (hash == -1)
397 return -1;
398 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000399 entry = (so->lookup)(so, key, hash);
400 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000401 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000402 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000403 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000404 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405 so->used--;
406 Py_DECREF(old_key);
407 return DISCARD_FOUND;
408}
409
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000410static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000411set_clear_internal(PySetObject *so)
412{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000413 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000414 int table_is_malloced;
415 int fill;
416 setentry small_copy[PySet_MINSIZE];
417#ifdef Py_DEBUG
418 int i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000419 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000420
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421 n = so->mask + 1;
422 i = 0;
423#endif
424
425 table = so->table;
426 assert(table != NULL);
427 table_is_malloced = table != so->smalltable;
428
429 /* This is delicate. During the process of clearing the set,
430 * decrefs can cause the set to mutate. To avoid fatal confusion
431 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000432 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000433 * clearing.
434 */
435 fill = so->fill;
436 if (table_is_malloced)
437 EMPTY_TO_MINSIZE(so);
438
439 else if (fill > 0) {
440 /* It's a small table with something that needs to be cleared.
441 * Afraid the only safe way is to copy the set entries into
442 * another small table first.
443 */
444 memcpy(small_copy, table, sizeof(small_copy));
445 table = small_copy;
446 EMPTY_TO_MINSIZE(so);
447 }
448 /* else it's a small table that's already empty */
449
450 /* Now we can finally clear things. If C had refcounts, we could
451 * assert that the refcount on table is 1 now, i.e. that this function
452 * has unique access to it, so decref side-effects can't alter it.
453 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000454 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455#ifdef Py_DEBUG
456 assert(i < n);
457 ++i;
458#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000459 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000460 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000461 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462 }
463#ifdef Py_DEBUG
464 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000465 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000466#endif
467 }
468
469 if (table_is_malloced)
470 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000471 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472}
473
474/*
475 * Iterate over a set table. Use like so:
476 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000477 * int pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000478 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000479 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000480 * while (set_next(yourset, &pos, &entry)) {
481 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482 * }
483 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000484 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000485 * mutates the table.
486 */
487static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000488set_next(PySetObject *so, int *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489{
490 register int i, mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000491 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000492
493 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000494 i = *pos_ptr;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495 if (i < 0)
496 return 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000497 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000499 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000501 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502 if (i > mask)
503 return 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000504 if (table[i].key)
505 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506 return 1;
507}
508
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000510set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000511{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000512 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513 register int i;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000514 register setentry *entry, *othertable;
515 register int othermask;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516
517 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000518 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000520 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000521 if (other == so || other->used == 0)
522 /* a.update(a) or a.update({}); nothing to do */
523 return 0;
524 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000525 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000526 * that there will be no (or few) overlapping keys.
527 */
528 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
529 if (set_table_resize(so, (so->used + other->used)*2) != 0)
530 return -1;
531 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000532 othermask = other->mask;
533 othertable = other->table;
534 for (i = 0; i <= othermask; i++) {
535 entry = &othertable[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536 if (entry->key != NULL &&
537 entry->key != dummy) {
538 Py_INCREF(entry->key);
539 set_insert_key(so, entry->key, entry->hash);
540 }
541 }
542 return 0;
543}
544
545static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000546set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000547{
548 long hash;
549
550 if (!PyString_CheckExact(key) ||
551 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
552 hash = PyObject_Hash(key);
553 if (hash == -1)
554 return -1;
555 }
556 key = (so->lookup)(so, key, hash)->key;
557 return key != NULL && key != dummy;
558}
559
Raymond Hettingerc991db22005-08-11 07:58:45 +0000560static int
561set_contains_entry(PySetObject *so, setentry *entry)
562{
563 PyObject *key;
564
565 key = (so->lookup)(so, entry->key, entry->hash)->key;
566 return key != NULL && key != dummy;
567}
568
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000569static PyObject *
570set_pop(PySetObject *so)
571{
572 PyObject *key;
573 register setentry *entry;
574 register int i = 0;
575
576 assert (PyAnySet_Check(so));
577 if (so->used == 0) {
578 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
579 return NULL;
580 }
581
582 /* Set entry to "the first" unused or dummy set entry. We abuse
583 * the hash field of slot 0 to hold a search finger:
584 * If slot 0 has a value, use slot 0.
585 * Else slot 0 is being used to hold a search finger,
586 * and we use its hash value as the first index to look.
587 */
588 entry = &so->table[0];
589 if (entry->key == NULL || entry->key == dummy) {
590 i = (int)entry->hash;
591 /* The hash field may be a real hash value, or it may be a
592 * legit search finger, or it may be a once-legit search
593 * finger that's out of bounds now because it wrapped around
594 * or the table shrunk -- simply make sure it's in bounds now.
595 */
596 if (i > so->mask || i < 1)
597 i = 1; /* skip slot 0 */
598 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
599 i++;
600 if (i > so->mask)
601 i = 1;
602 }
603 }
604 key = entry->key;
605 Py_INCREF(dummy);
606 entry->key = dummy;
607 so->used--;
608 so->table[0].hash = i + 1; /* next place to start */
609 return key;
610}
611
612PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
613
614static int
615set_len(PyObject *so)
616{
617 return ((PySetObject *)so)->used;
618}
619
Raymond Hettingera9d99362005-08-05 00:01:15 +0000620/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621
Raymond Hettingerd7946662005-08-01 21:39:29 +0000622static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000623
624typedef struct {
625 PyObject_HEAD
626 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
627 int si_used;
628 int si_pos;
629 long len;
630} setiterobject;
631
632static PyObject *
633set_iter(PySetObject *so)
634{
635 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
636 if (si == NULL)
637 return NULL;
638 Py_INCREF(so);
639 si->si_set = so;
640 si->si_used = so->used;
641 si->si_pos = 0;
642 si->len = so->used;
643 return (PyObject *)si;
644}
645
646static void
647setiter_dealloc(setiterobject *si)
648{
649 Py_XDECREF(si->si_set);
650 PyObject_Del(si);
651}
652
653static int
654setiter_len(setiterobject *si)
655{
656 if (si->si_set != NULL && si->si_used == si->si_set->used)
657 return si->len;
658 return 0;
659}
660
661static PySequenceMethods setiter_as_sequence = {
662 (inquiry)setiter_len, /* sq_length */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000663};
664
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000665static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000666{
667 PyObject *key;
668 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000669 register setentry *entry;
670 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000671
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000672 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000674 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000675
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000676 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000677 PyErr_SetString(PyExc_RuntimeError,
678 "Set changed size during iteration");
679 si->si_used = -1; /* Make this state sticky */
680 return NULL;
681 }
682
683 i = si->si_pos;
684 if (i < 0)
685 goto fail;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000686 entry = so->table;
687 mask = so->mask;
688 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000689 i++;
690 si->si_pos = i+1;
691 if (i > mask)
692 goto fail;
693 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000694 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000695 Py_INCREF(key);
696 return key;
697
698fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000699 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000700 si->si_set = NULL;
701 return NULL;
702}
703
Hye-Shik Change2956762005-08-01 05:26:41 +0000704static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000705 PyObject_HEAD_INIT(&PyType_Type)
706 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000707 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000708 sizeof(setiterobject), /* tp_basicsize */
709 0, /* tp_itemsize */
710 /* methods */
711 (destructor)setiter_dealloc, /* tp_dealloc */
712 0, /* tp_print */
713 0, /* tp_getattr */
714 0, /* tp_setattr */
715 0, /* tp_compare */
716 0, /* tp_repr */
717 0, /* tp_as_number */
718 &setiter_as_sequence, /* tp_as_sequence */
719 0, /* tp_as_mapping */
720 0, /* tp_hash */
721 0, /* tp_call */
722 0, /* tp_str */
723 PyObject_GenericGetAttr, /* tp_getattro */
724 0, /* tp_setattro */
725 0, /* tp_as_buffer */
726 Py_TPFLAGS_DEFAULT, /* tp_flags */
727 0, /* tp_doc */
728 0, /* tp_traverse */
729 0, /* tp_clear */
730 0, /* tp_richcompare */
731 0, /* tp_weaklistoffset */
732 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000733 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000734};
735
Raymond Hettingerd7946662005-08-01 21:39:29 +0000736static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000737set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000738{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000739 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000740
Raymond Hettingerd7946662005-08-01 21:39:29 +0000741 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000742 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000743
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000744 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000745 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000746 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000747 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000748 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000749 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000750 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000751 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000752 }
753
Raymond Hettingera38123e2003-11-24 22:18:49 +0000754 it = PyObject_GetIter(other);
755 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000756 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000757
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000758 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000759 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000760 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000761 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000762 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000763 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000764 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000765 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000766 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000767 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000768 return -1;
769 return 0;
770}
771
772static PyObject *
773set_update(PySetObject *so, PyObject *other)
774{
775 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000776 return NULL;
777 Py_RETURN_NONE;
778}
779
780PyDoc_STRVAR(update_doc,
781"Update a set with the union of itself and another.");
782
783static PyObject *
784make_new_set(PyTypeObject *type, PyObject *iterable)
785{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000786 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000787
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788 if (dummy == NULL) { /* Auto-initialize dummy */
789 dummy = PyString_FromString("<dummy key>");
790 if (dummy == NULL)
791 return NULL;
792 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000793
794 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000795 if (num_free_sets &&
796 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
797 so = free_sets[--num_free_sets];
798 assert (so != NULL && PyAnySet_CheckExact(so));
799 so->ob_type = type;
800 _Py_NewReference((PyObject *)so);
801 EMPTY_TO_MINSIZE(so);
802 PyObject_GC_Track(so);
803 } else {
804 so = (PySetObject *)type->tp_alloc(type, 0);
805 if (so == NULL)
806 return NULL;
807 /* tp_alloc has already zeroed the structure */
808 assert(so->table == NULL && so->fill == 0 && so->used == 0);
809 INIT_NONZERO_SET_SLOTS(so);
810 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000813 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000814
Raymond Hettingera38123e2003-11-24 22:18:49 +0000815 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000816 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000817 Py_DECREF(so);
818 return NULL;
819 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000820 }
821
Raymond Hettingera690a992003-11-16 16:17:49 +0000822 return (PyObject *)so;
823}
824
Raymond Hettingerd7946662005-08-01 21:39:29 +0000825/* The empty frozenset is a singleton */
826static PyObject *emptyfrozenset = NULL;
827
Raymond Hettingera690a992003-11-16 16:17:49 +0000828static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000829frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000830{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000831 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000832
833 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
834 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000835
836 if (type != &PyFrozenSet_Type)
837 return make_new_set(type, iterable);
838
839 if (iterable != NULL) {
840 /* frozenset(f) is idempotent */
841 if (PyFrozenSet_CheckExact(iterable)) {
842 Py_INCREF(iterable);
843 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000844 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000845 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000846 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000847 return result;
848 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000849 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000850 /* The empty frozenset is a singleton */
851 if (emptyfrozenset == NULL)
852 emptyfrozenset = make_new_set(type, NULL);
853 Py_XINCREF(emptyfrozenset);
854 return emptyfrozenset;
855}
856
857void
858PySet_Fini(void)
859{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000860 PySetObject *so;
861
862 while (num_free_sets) {
863 num_free_sets--;
864 so = free_sets[num_free_sets];
865 PyObject_GC_Del(so);
866 }
Raymond Hettingera9d99362005-08-05 00:01:15 +0000867 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000868 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000869}
870
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000871static PyObject *
872set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
873{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000874 return make_new_set(type, NULL);
875}
876
Raymond Hettingera690a992003-11-16 16:17:49 +0000877static void
878set_dealloc(PySetObject *so)
879{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000880 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000881 int fill = so->fill;
882
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000883 PyObject_GC_UnTrack(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000884 Py_TRASHCAN_SAFE_BEGIN(so)
Raymond Hettinger691d8052004-05-30 07:26:47 +0000885 if (so->weakreflist != NULL)
886 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000887
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000888 for (entry = so->table; fill > 0; entry++) {
889 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000890 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000891 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000892 }
893 }
894 if (so->table != so->smalltable)
895 PyMem_DEL(so->table);
896
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000897 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
898 free_sets[num_free_sets++] = so;
899 else
900 so->ob_type->tp_free(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000901 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingera690a992003-11-16 16:17:49 +0000902}
903
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000904static int
905set_traverse(PySetObject *so, visitproc visit, void *arg)
906{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000907 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000908 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000909
Raymond Hettingerc991db22005-08-11 07:58:45 +0000910 while (set_next(so, &pos, &entry))
911 Py_VISIT(entry->key);
Raymond Hettingerbb999b52005-06-18 21:00:26 +0000912 return 0;
913}
914
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000915/* set_swap_bodies() switches the contents of any two sets by moving their
916 internal data pointers and, if needed, copying the internal smalltables.
917 Semantically equivalent to:
918
919 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
920
921 The function always succeeds and it leaves both objects in a stable state.
922 Useful for creating temporary frozensets from sets for membership testing
923 in __contains__(), discard(), and remove(). Also useful for operations
924 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000925 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000926*/
927
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000928static void
929set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000930{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000931 int t;
932 setentry *u;
933 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
934 setentry tab[PySet_MINSIZE];
935 long h;
936
937 t = a->fill; a->fill = b->fill; b->fill = t;
938 t = a->used; a->used = b->used; b->used = t;
939 t = a->mask; a->mask = b->mask; b->mask = t;
940
941 u = a->table;
942 if (a->table == a->smalltable)
943 u = b->smalltable;
944 a->table = b->table;
945 if (b->table == b->smalltable)
946 a->table = a->smalltable;
947 b->table = u;
948
949 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
950
951 if (a->table == a->smalltable || b->table == b->smalltable) {
952 memcpy(tab, a->smalltable, sizeof(tab));
953 memcpy(a->smalltable, b->smalltable, sizeof(tab));
954 memcpy(b->smalltable, tab, sizeof(tab));
955 }
956
Raymond Hettingera580c472005-08-05 17:19:54 +0000957 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
958 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
959 h = a->hash; a->hash = b->hash; b->hash = h;
960 } else {
961 a->hash = -1;
962 b->hash = -1;
963 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000964}
965
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000966static 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
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001055 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001056 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 Hettingered6c1ef2005-08-13 08:28:03 +00001372 setentry *entry;
1373 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001374
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001375 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001376 PyObject *tmp, *result;
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 Hettingerbeb31012005-08-16 03:47:52 +00001384 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001385 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001386
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001387 while (set_next(so, &pos, &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
Raymond Hettingera690a992003-11-16 16:17:49 +00001414static PyObject *
1415set_richcompare(PySetObject *v, PyObject *w, int op)
1416{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001417 PyObject *r1, *r2;
1418
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001419 if(!PyAnySet_Check(w)) {
1420 if (op == Py_EQ)
1421 Py_RETURN_FALSE;
1422 if (op == Py_NE)
1423 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001424 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1425 return NULL;
1426 }
1427 switch (op) {
1428 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001429 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001430 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001431 return set_issubset(v, w);
1432 case Py_NE:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001433 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001434 Py_RETURN_TRUE;
1435 r1 = set_issubset(v, w);
1436 assert (r1 != NULL);
1437 r2 = PyBool_FromLong(PyObject_Not(r1));
1438 Py_DECREF(r1);
1439 return r2;
1440 case Py_LE:
1441 return set_issubset(v, w);
1442 case Py_GE:
1443 return set_issuperset(v, w);
1444 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001445 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001446 Py_RETURN_FALSE;
1447 return set_issubset(v, w);
1448 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001449 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001450 Py_RETURN_FALSE;
1451 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001452 }
1453 Py_INCREF(Py_NotImplemented);
1454 return Py_NotImplemented;
1455}
1456
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001457static int
1458set_nocmp(PyObject *self)
1459{
1460 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1461 return -1;
1462}
1463
1464static long
1465frozenset_hash(PyObject *self)
1466{
1467 PySetObject *so = (PySetObject *)self;
1468 long h, hash = 1927868237L;
1469 setentry *entry;
1470 int pos = 0;
1471
1472 if (so->hash != -1)
1473 return so->hash;
1474
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001475 hash *= PySet_GET_SIZE(self) + 1;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001476 while (set_next(so, &pos, &entry)) {
1477 /* Work to increase the bit dispersion for closely spaced hash
1478 values. The is important because some use cases have many
1479 combinations of a small number of elements with nearby
1480 hashes so that many distinct combinations collapse to only
1481 a handful of distinct hash values. */
1482 h = entry->hash;
1483 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
1484 }
1485 hash = hash * 69069L + 907133923L;
1486 if (hash == -1)
1487 hash = 590923713L;
1488 so->hash = hash;
1489 return hash;
1490}
1491
1492static long
1493set_nohash(PyObject *self)
1494{
1495 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
1496 return -1;
1497}
1498
Raymond Hettingera690a992003-11-16 16:17:49 +00001499static PyObject *
1500set_repr(PySetObject *so)
1501{
1502 PyObject *keys, *result, *listrepr;
1503
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001504 keys = PySequence_List((PyObject *)so);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001505 if (keys == NULL)
1506 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001507 listrepr = PyObject_Repr(keys);
1508 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001509 if (listrepr == NULL)
1510 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001511
1512 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
1513 PyString_AS_STRING(listrepr));
1514 Py_DECREF(listrepr);
1515 return result;
1516}
1517
1518static int
1519set_tp_print(PySetObject *so, FILE *fp, int flags)
1520{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001521 setentry *entry;
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001522 int pos=0;
1523 char *emit = ""; /* No separator emitted on first pass */
1524 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +00001525
Raymond Hettingera690a992003-11-16 16:17:49 +00001526 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001527 while (set_next(so, &pos, &entry)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001528 fputs(emit, fp);
1529 emit = separator;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001530 if (PyObject_Print(entry->key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001531 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001532 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001533 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001534 return 0;
1535}
1536
1537static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001538set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001539{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001540 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001541 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001542 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001543}
1544
1545PyDoc_STRVAR(add_doc,
1546"Add an element to a set.\n\
1547\n\
1548This has no effect if the element is already present.");
1549
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001550static int
1551set_contains(PySetObject *so, PyObject *key)
1552{
1553 PyObject *tmpkey;
1554 int rv;
1555
1556 rv = set_contains_key(so, key);
1557 if (rv == -1) {
1558 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1559 return -1;
1560 PyErr_Clear();
1561 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1562 if (tmpkey == NULL)
1563 return -1;
1564 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1565 rv = set_contains(so, tmpkey);
1566 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1567 Py_DECREF(tmpkey);
1568 }
1569 return rv;
1570}
1571
1572static PyObject *
1573set_direct_contains(PySetObject *so, PyObject *key)
1574{
1575 long result;
1576
1577 result = set_contains(so, key);
1578 if (result == -1)
1579 return NULL;
1580 return PyBool_FromLong(result);
1581}
1582
1583PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1584
Raymond Hettingera690a992003-11-16 16:17:49 +00001585static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001586set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001587{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001588 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001589 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001590
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001591 rv = set_discard_key(so, key);
1592 if (rv == -1) {
1593 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1594 return NULL;
1595 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001596 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1597 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001598 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001599 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001600 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001601 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001602 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001603 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001604 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001605 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001606 return NULL;
1607 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001608 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001609}
1610
1611PyDoc_STRVAR(remove_doc,
1612"Remove an element from a set; it must be a member.\n\
1613\n\
1614If the element is not a member, raise a KeyError.");
1615
1616static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001617set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001618{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001619 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001620 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001621
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001622 rv = set_discard_key(so, key);
1623 if (rv == -1) {
1624 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1625 return NULL;
1626 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001627 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1628 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001629 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001630 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001631 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001632 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001633 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001634 return result;
1635 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001636 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001637}
1638
1639PyDoc_STRVAR(discard_doc,
1640"Remove an element from a set if it is a member.\n\
1641\n\
1642If the element is not a member, do nothing.");
1643
1644static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001645set_reduce(PySetObject *so)
1646{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001647 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001648
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001649 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001650 if (keys == NULL)
1651 goto done;
1652 args = PyTuple_Pack(1, keys);
1653 if (args == NULL)
1654 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001655 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1656 if (dict == NULL) {
1657 PyErr_Clear();
1658 dict = Py_None;
1659 Py_INCREF(dict);
1660 }
1661 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001662done:
1663 Py_XDECREF(args);
1664 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001665 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001666 return result;
1667}
1668
1669PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1670
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001671static int
1672set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1673{
1674 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001675
1676 if (!PyAnySet_Check(self))
1677 return -1;
1678 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1679 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001680 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001681 self->hash = -1;
1682 if (iterable == NULL)
1683 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001684 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001685}
1686
Raymond Hettingera690a992003-11-16 16:17:49 +00001687static PySequenceMethods set_as_sequence = {
1688 (inquiry)set_len, /* sq_length */
1689 0, /* sq_concat */
1690 0, /* sq_repeat */
1691 0, /* sq_item */
1692 0, /* sq_slice */
1693 0, /* sq_ass_item */
1694 0, /* sq_ass_slice */
1695 (objobjproc)set_contains, /* sq_contains */
1696};
1697
1698/* set object ********************************************************/
1699
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001700#ifdef Py_DEBUG
1701static PyObject *test_c_api(PySetObject *so);
1702
1703PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1704All is well if assertions don't fail.");
1705#endif
1706
Raymond Hettingera690a992003-11-16 16:17:49 +00001707static PyMethodDef set_methods[] = {
1708 {"add", (PyCFunction)set_add, METH_O,
1709 add_doc},
1710 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1711 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001712 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001713 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001714 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1715 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001716 {"discard", (PyCFunction)set_discard, METH_O,
1717 discard_doc},
1718 {"difference", (PyCFunction)set_difference, METH_O,
1719 difference_doc},
1720 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1721 difference_update_doc},
1722 {"intersection",(PyCFunction)set_intersection, METH_O,
1723 intersection_doc},
1724 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1725 intersection_update_doc},
1726 {"issubset", (PyCFunction)set_issubset, METH_O,
1727 issubset_doc},
1728 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1729 issuperset_doc},
1730 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1731 pop_doc},
1732 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1733 reduce_doc},
1734 {"remove", (PyCFunction)set_remove, METH_O,
1735 remove_doc},
1736 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1737 symmetric_difference_doc},
1738 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1739 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001740#ifdef Py_DEBUG
1741 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1742 test_c_api_doc},
1743#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001744 {"union", (PyCFunction)set_union, METH_O,
1745 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001746 {"update", (PyCFunction)set_update, METH_O,
1747 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001748 {NULL, NULL} /* sentinel */
1749};
1750
1751static PyNumberMethods set_as_number = {
1752 0, /*nb_add*/
1753 (binaryfunc)set_sub, /*nb_subtract*/
1754 0, /*nb_multiply*/
1755 0, /*nb_divide*/
1756 0, /*nb_remainder*/
1757 0, /*nb_divmod*/
1758 0, /*nb_power*/
1759 0, /*nb_negative*/
1760 0, /*nb_positive*/
1761 0, /*nb_absolute*/
1762 0, /*nb_nonzero*/
1763 0, /*nb_invert*/
1764 0, /*nb_lshift*/
1765 0, /*nb_rshift*/
1766 (binaryfunc)set_and, /*nb_and*/
1767 (binaryfunc)set_xor, /*nb_xor*/
1768 (binaryfunc)set_or, /*nb_or*/
1769 0, /*nb_coerce*/
1770 0, /*nb_int*/
1771 0, /*nb_long*/
1772 0, /*nb_float*/
1773 0, /*nb_oct*/
1774 0, /*nb_hex*/
1775 0, /*nb_inplace_add*/
1776 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1777 0, /*nb_inplace_multiply*/
1778 0, /*nb_inplace_divide*/
1779 0, /*nb_inplace_remainder*/
1780 0, /*nb_inplace_power*/
1781 0, /*nb_inplace_lshift*/
1782 0, /*nb_inplace_rshift*/
1783 (binaryfunc)set_iand, /*nb_inplace_and*/
1784 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1785 (binaryfunc)set_ior, /*nb_inplace_or*/
1786};
1787
1788PyDoc_STRVAR(set_doc,
1789"set(iterable) --> set object\n\
1790\n\
1791Build an unordered collection.");
1792
1793PyTypeObject PySet_Type = {
1794 PyObject_HEAD_INIT(&PyType_Type)
1795 0, /* ob_size */
1796 "set", /* tp_name */
1797 sizeof(PySetObject), /* tp_basicsize */
1798 0, /* tp_itemsize */
1799 /* methods */
1800 (destructor)set_dealloc, /* tp_dealloc */
1801 (printfunc)set_tp_print, /* tp_print */
1802 0, /* tp_getattr */
1803 0, /* tp_setattr */
1804 (cmpfunc)set_nocmp, /* tp_compare */
1805 (reprfunc)set_repr, /* tp_repr */
1806 &set_as_number, /* tp_as_number */
1807 &set_as_sequence, /* tp_as_sequence */
1808 0, /* tp_as_mapping */
1809 set_nohash, /* tp_hash */
1810 0, /* tp_call */
1811 0, /* tp_str */
1812 PyObject_GenericGetAttr, /* tp_getattro */
1813 0, /* tp_setattro */
1814 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001815 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001816 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001817 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001818 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001819 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001820 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001821 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001822 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001823 0, /* tp_iternext */
1824 set_methods, /* tp_methods */
1825 0, /* tp_members */
1826 0, /* tp_getset */
1827 0, /* tp_base */
1828 0, /* tp_dict */
1829 0, /* tp_descr_get */
1830 0, /* tp_descr_set */
1831 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001832 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001833 PyType_GenericAlloc, /* tp_alloc */
1834 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001835 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001836};
1837
1838/* frozenset object ********************************************************/
1839
1840
1841static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001842 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001843 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001844 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001845 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001846 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001847 difference_doc},
1848 {"intersection",(PyCFunction)set_intersection, METH_O,
1849 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001850 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001851 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001852 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001853 issuperset_doc},
1854 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1855 reduce_doc},
1856 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1857 symmetric_difference_doc},
1858 {"union", (PyCFunction)set_union, METH_O,
1859 union_doc},
1860 {NULL, NULL} /* sentinel */
1861};
1862
1863static PyNumberMethods frozenset_as_number = {
1864 0, /*nb_add*/
1865 (binaryfunc)set_sub, /*nb_subtract*/
1866 0, /*nb_multiply*/
1867 0, /*nb_divide*/
1868 0, /*nb_remainder*/
1869 0, /*nb_divmod*/
1870 0, /*nb_power*/
1871 0, /*nb_negative*/
1872 0, /*nb_positive*/
1873 0, /*nb_absolute*/
1874 0, /*nb_nonzero*/
1875 0, /*nb_invert*/
1876 0, /*nb_lshift*/
1877 0, /*nb_rshift*/
1878 (binaryfunc)set_and, /*nb_and*/
1879 (binaryfunc)set_xor, /*nb_xor*/
1880 (binaryfunc)set_or, /*nb_or*/
1881};
1882
1883PyDoc_STRVAR(frozenset_doc,
1884"frozenset(iterable) --> frozenset object\n\
1885\n\
1886Build an immutable unordered collection.");
1887
1888PyTypeObject PyFrozenSet_Type = {
1889 PyObject_HEAD_INIT(&PyType_Type)
1890 0, /* ob_size */
1891 "frozenset", /* tp_name */
1892 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001893 0, /* tp_itemsize */
1894 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001895 (destructor)set_dealloc, /* tp_dealloc */
1896 (printfunc)set_tp_print, /* tp_print */
1897 0, /* tp_getattr */
1898 0, /* tp_setattr */
1899 (cmpfunc)set_nocmp, /* tp_compare */
1900 (reprfunc)set_repr, /* tp_repr */
1901 &frozenset_as_number, /* tp_as_number */
1902 &set_as_sequence, /* tp_as_sequence */
1903 0, /* tp_as_mapping */
1904 frozenset_hash, /* tp_hash */
1905 0, /* tp_call */
1906 0, /* tp_str */
1907 PyObject_GenericGetAttr, /* tp_getattro */
1908 0, /* tp_setattro */
1909 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001910 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001911 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001912 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001913 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001914 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001915 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001916 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001917 (getiterfunc)set_iter, /* tp_iter */
1918 0, /* tp_iternext */
1919 frozenset_methods, /* tp_methods */
1920 0, /* tp_members */
1921 0, /* tp_getset */
1922 0, /* tp_base */
1923 0, /* tp_dict */
1924 0, /* tp_descr_get */
1925 0, /* tp_descr_set */
1926 0, /* tp_dictoffset */
1927 0, /* tp_init */
1928 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001929 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001930 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001931};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001932
1933
1934/***** C API functions *************************************************/
1935
1936PyObject *
1937PySet_New(PyObject *iterable)
1938{
1939 return make_new_set(&PySet_Type, iterable);
1940}
1941
1942PyObject *
1943PyFrozenSet_New(PyObject *iterable)
1944{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001945 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001946
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001947 if (iterable == NULL)
1948 args = PyTuple_New(0);
1949 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001950 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001951 if (args == NULL)
1952 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001953 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001954 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001955 return result;
1956}
1957
1958int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001959PySet_Size(PyObject *anyset)
1960{
1961 if (!PyAnySet_Check(anyset)) {
1962 PyErr_BadInternalCall();
1963 return -1;
1964 }
1965 return ((PySetObject *)anyset)->used;
1966}
1967
1968int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001969PySet_Contains(PyObject *anyset, PyObject *key)
1970{
1971 if (!PyAnySet_Check(anyset)) {
1972 PyErr_BadInternalCall();
1973 return -1;
1974 }
1975 return set_contains_key((PySetObject *)anyset, key);
1976}
1977
1978int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001979PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001980{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001981 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001982 PyErr_BadInternalCall();
1983 return -1;
1984 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001985 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001986}
1987
1988int
1989PySet_Add(PyObject *set, PyObject *key)
1990{
1991 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1992 PyErr_BadInternalCall();
1993 return -1;
1994 }
1995 return set_add_key((PySetObject *)set, key);
1996}
1997
1998PyObject *
1999PySet_Pop(PyObject *set)
2000{
2001 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2002 PyErr_BadInternalCall();
2003 return NULL;
2004 }
2005 return set_pop((PySetObject *)set);
2006}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002007
2008
2009#ifdef Py_DEBUG
2010
2011/* Test code to be called with any three element set.
2012 Returns True and original set is restored. */
2013
2014#define assertRaises(call_return_value, exception) \
2015 do { \
2016 assert(call_return_value); \
2017 assert(PyErr_ExceptionMatches(exception)); \
2018 PyErr_Clear(); \
2019 } while(0)
2020
2021static PyObject *
2022test_c_api(PySetObject *so)
2023{
2024 PyObject *elem, *dup, *t, *f, *ob = (PyObject *)so;
2025
2026 /* Verify preconditions and exercise type/size checks */
2027 assert(PyAnySet_Check(ob));
2028 assert(PyAnySet_CheckExact(ob));
2029 assert(!PyFrozenSet_CheckExact(ob));
2030 assert(PySet_Size(ob) == 3);
2031 assert(PySet_GET_SIZE(ob) == 3);
2032
2033 /* Raise TypeError for non-iterable constructor arguments */
2034 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2035 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2036
2037 /* Raise TypeError for unhashable key */
2038 dup = PySet_New(ob);
2039 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2040 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2041 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2042
2043 /* Exercise successful pop, contains, add, and discard */
2044 elem = PySet_Pop(ob);
2045 assert(PySet_Contains(ob, elem) == 0);
2046 assert(PySet_GET_SIZE(ob) == 2);
2047 assert(PySet_Add(ob, elem) == 0);
2048 assert(PySet_Contains(ob, elem) == 1);
2049 assert(PySet_GET_SIZE(ob) == 3);
2050 assert(PySet_Discard(ob, elem) == 1);
2051 assert(PySet_GET_SIZE(ob) == 2);
2052 assert(PySet_Discard(ob, elem) == 0);
2053 assert(PySet_GET_SIZE(ob) == 2);
2054
2055 /* Raise SystemError when self argument is not a set or frozenset. */
2056 t = PyTuple_New(0);
2057 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2058 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2059 Py_DECREF(t);
2060
2061 /* Raise SystemError when self argument is not a set. */
2062 f = PyFrozenSet_New(dup);
2063 assert(PySet_Size(f) == 3);
2064 assert(PyFrozenSet_CheckExact(f));
2065 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2066 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2067 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2068 Py_DECREF(f);
2069
2070 /* Raise KeyError when popping from an empty set */
2071 set_clear_internal(so);
2072 assert(PySet_GET_SIZE(ob) == 0);
2073 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2074
2075 /* Restore the set from the copy and use the abstract API */
2076 assert(PyObject_CallMethod(ob, "update", "O", dup) == Py_None);
2077 Py_DECREF(Py_None);
2078
2079 /* Verify constructors accept NULL arguments */
2080 f = PySet_New(NULL);
2081 assert(f != NULL);
2082 assert(PySet_GET_SIZE(f) == 0);
2083 Py_DECREF(f);
2084 f = PyFrozenSet_New(NULL);
2085 assert(f != NULL);
2086 assert(PyFrozenSet_CheckExact(f));
2087 assert(PySet_GET_SIZE(f) == 0);
2088 Py_DECREF(f);
2089
2090 Py_DECREF(elem);
2091 Py_DECREF(dup);
2092 Py_RETURN_TRUE;
2093}
2094
2095#endif