blob: d39f26587f589080b96ca41c8baebf1e059b0920 [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 Hettinger9bda1d62005-09-16 07:14:21 +000047Unlike the dictionary implementation, the lookkey functions can return
48NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000049*/
50
51static setentry *
52set_lookkey(PySetObject *so, PyObject *key, register long hash)
53{
54 register int i;
55 register unsigned int perturb;
56 register setentry *freeslot;
57 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000058 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000059 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061 PyObject *startkey;
62
63 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000064 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000065 if (entry->key == NULL || entry->key == key)
66 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000067
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000068 if (entry->key == dummy)
69 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000071 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000072 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
74 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000075 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +000076 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000077 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000078 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000079 }
80 else {
81 /* The compare did major nasty stuff to the
82 * set: start over.
83 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000084 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000085 }
86 }
87 freeslot = NULL;
88 }
89
90 /* In the loop, key == dummy is by far (factor of 100s) the
91 least likely outcome, so test for that last. */
92 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
93 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +000094 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000095 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000096 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000097 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000098 break;
99 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000100 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000102 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000103 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000104 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
105 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000106 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000107 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000108 if (cmp > 0)
109 break;
110 }
111 else {
112 /* The compare did major nasty stuff to the
113 * set: start over.
114 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000115 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000116 }
117 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000118 else if (entry->key == dummy && freeslot == NULL)
119 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000121 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122}
123
124/*
125 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000126 * This means we can always use _PyString_Eq directly and not have to check to
127 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000128 */
129static setentry *
130set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
131{
132 register int i;
133 register unsigned int perturb;
134 register setentry *freeslot;
135 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000136 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000137 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138
139 /* Make sure this function doesn't have to handle non-string keys,
140 including subclasses of str; e.g., one reason to subclass
141 strings is to override __eq__, and for speed we don't cater to
142 that here. */
143 if (!PyString_CheckExact(key)) {
144 so->lookup = set_lookkey;
145 return set_lookkey(so, key, hash);
146 }
147 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000148 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000149 if (entry->key == NULL || entry->key == key)
150 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000151 if (entry->key == dummy)
152 freeslot = entry;
153 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000154 if (entry->hash == hash && _PyString_Eq(entry->key, key))
155 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000156 freeslot = NULL;
157 }
158
159 /* In the loop, key == dummy is by far (factor of 100s) the
160 least likely outcome, so test for that last. */
161 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
162 i = (i << 2) + i + perturb + 1;
163 entry = &table[i & mask];
164 if (entry->key == NULL)
165 return freeslot == NULL ? entry : freeslot;
166 if (entry->key == key
167 || (entry->hash == hash
168 && entry->key != dummy
169 && _PyString_Eq(entry->key, key)))
170 return entry;
171 if (entry->key == dummy && freeslot == NULL)
172 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000173 }
174}
175
176/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000177Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000178Used both by the internal resize routine and by the public insert routine.
179Eats a reference to key.
180*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000181static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000182set_insert_key(register PySetObject *so, PyObject *key, long hash)
183{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000184 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000185 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
186
187 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000188 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000189 if (entry == NULL)
190 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000191 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000192 /* UNUSED */
193 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000194 entry->key = key;
195 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000196 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000197 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000198 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000199 entry->key = key;
200 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000201 so->used++;
202 Py_DECREF(dummy);
203 } else {
204 /* ACTIVE */
205 Py_DECREF(key);
206 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000207 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208}
209
210/*
211Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000212keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000213actually be smaller than the old one.
214*/
215static int
216set_table_resize(PySetObject *so, int minused)
217{
218 int newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000219 setentry *oldtable, *newtable, *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220 int i;
221 int is_oldtable_malloced;
222 setentry small_copy[PySet_MINSIZE];
223
224 assert(minused >= 0);
225
226 /* Find the smallest table size > minused. */
227 for (newsize = PySet_MINSIZE;
228 newsize <= minused && newsize > 0;
229 newsize <<= 1)
230 ;
231 if (newsize <= 0) {
232 PyErr_NoMemory();
233 return -1;
234 }
235
236 /* Get space for a new table. */
237 oldtable = so->table;
238 assert(oldtable != NULL);
239 is_oldtable_malloced = oldtable != so->smalltable;
240
241 if (newsize == PySet_MINSIZE) {
242 /* A large table is shrinking, or we can't get any smaller. */
243 newtable = so->smalltable;
244 if (newtable == oldtable) {
245 if (so->fill == so->used) {
246 /* No dummies, so no point doing anything. */
247 return 0;
248 }
249 /* We're not going to resize it, but rebuild the
250 table anyway to purge old dummy entries.
251 Subtle: This is *necessary* if fill==size,
252 as set_lookkey needs at least one virgin slot to
253 terminate failing searches. If fill < size, it's
254 merely desirable, as dummies slow searches. */
255 assert(so->fill > so->used);
256 memcpy(small_copy, oldtable, sizeof(small_copy));
257 oldtable = small_copy;
258 }
259 }
260 else {
261 newtable = PyMem_NEW(setentry, newsize);
262 if (newtable == NULL) {
263 PyErr_NoMemory();
264 return -1;
265 }
266 }
267
268 /* Make the set empty, using the new table. */
269 assert(newtable != oldtable);
270 so->table = newtable;
271 so->mask = newsize - 1;
272 memset(newtable, 0, sizeof(setentry) * newsize);
273 so->used = 0;
274 i = so->fill;
275 so->fill = 0;
276
277 /* Copy the data over; this is refcount-neutral for active entries;
278 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000279 for (entry = oldtable; i > 0; entry++) {
280 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000281 /* UNUSED */
282 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000283 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284 /* DUMMY */
285 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000286 assert(entry->key == dummy);
287 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000288 } else {
289 /* ACTIVE */
290 --i;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000291 if(set_insert_key(so, entry->key, entry->hash) == -1) {
292 if (is_oldtable_malloced)
293 PyMem_DEL(oldtable);
294 return -1;
295 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296 }
297 }
298
299 if (is_oldtable_malloced)
300 PyMem_DEL(oldtable);
301 return 0;
302}
303
Raymond Hettingerc991db22005-08-11 07:58:45 +0000304/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
305
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000306static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000307set_add_entry(register PySetObject *so, setentry *entry)
308{
309 register int n_used;
310
311 assert(so->fill <= so->mask); /* at least one empty slot */
312 n_used = so->used;
313 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000314 if (set_insert_key(so, entry->key, entry->hash) == -1)
315 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000316 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
317 return 0;
318 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
319}
320
321static int
322set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323{
324 register long hash;
325 register int n_used;
326
Raymond Hettingerc991db22005-08-11 07:58:45 +0000327 if (!PyString_CheckExact(key) ||
328 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000329 hash = PyObject_Hash(key);
330 if (hash == -1)
331 return -1;
332 }
333 assert(so->fill <= so->mask); /* at least one empty slot */
334 n_used = so->used;
335 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000336 if (set_insert_key(so, key, hash) == -1) {
337 Py_DECREF(key);
338 return -1;
339 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000340 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
341 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000342 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000343}
344
345#define DISCARD_NOTFOUND 0
346#define DISCARD_FOUND 1
347
348static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000349set_discard_entry(PySetObject *so, setentry *oldentry)
350{ register setentry *entry;
351 PyObject *old_key;
352
353 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000354 if (entry == NULL)
355 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356 if (entry->key == NULL || entry->key == dummy)
357 return DISCARD_NOTFOUND;
358 old_key = entry->key;
359 Py_INCREF(dummy);
360 entry->key = dummy;
361 so->used--;
362 Py_DECREF(old_key);
363 return DISCARD_FOUND;
364}
365
366static int
367set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000368{
369 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000370 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000371 PyObject *old_key;
372
373 assert (PyAnySet_Check(so));
374 if (!PyString_CheckExact(key) ||
375 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
376 hash = PyObject_Hash(key);
377 if (hash == -1)
378 return -1;
379 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000380 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000381 if (entry == NULL)
382 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000383 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000384 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000385 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000387 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000388 so->used--;
389 Py_DECREF(old_key);
390 return DISCARD_FOUND;
391}
392
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000393static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394set_clear_internal(PySetObject *so)
395{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000396 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397 int table_is_malloced;
398 int fill;
399 setentry small_copy[PySet_MINSIZE];
400#ifdef Py_DEBUG
401 int i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000403
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000404 n = so->mask + 1;
405 i = 0;
406#endif
407
408 table = so->table;
409 assert(table != NULL);
410 table_is_malloced = table != so->smalltable;
411
412 /* This is delicate. During the process of clearing the set,
413 * decrefs can cause the set to mutate. To avoid fatal confusion
414 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000415 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000416 * clearing.
417 */
418 fill = so->fill;
419 if (table_is_malloced)
420 EMPTY_TO_MINSIZE(so);
421
422 else if (fill > 0) {
423 /* It's a small table with something that needs to be cleared.
424 * Afraid the only safe way is to copy the set entries into
425 * another small table first.
426 */
427 memcpy(small_copy, table, sizeof(small_copy));
428 table = small_copy;
429 EMPTY_TO_MINSIZE(so);
430 }
431 /* else it's a small table that's already empty */
432
433 /* Now we can finally clear things. If C had refcounts, we could
434 * assert that the refcount on table is 1 now, i.e. that this function
435 * has unique access to it, so decref side-effects can't alter it.
436 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000437 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000438#ifdef Py_DEBUG
439 assert(i < n);
440 ++i;
441#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000442 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000444 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445 }
446#ifdef Py_DEBUG
447 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000448 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449#endif
450 }
451
452 if (table_is_malloced)
453 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000454 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455}
456
457/*
458 * Iterate over a set table. Use like so:
459 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000460 * int pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000461 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000462 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000463 * while (set_next(yourset, &pos, &entry)) {
464 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000465 * }
466 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000467 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000468 * mutates the table.
469 */
470static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000471set_next(PySetObject *so, int *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472{
473 register int i, mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000474 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000475
476 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000477 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000478 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000479 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000481 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000483 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484 if (i > mask)
485 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000486 assert(table[i].key != NULL);
487 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000488 return 1;
489}
490
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000491static void
492set_dealloc(PySetObject *so)
493{
494 register setentry *entry;
495 int fill = so->fill;
496 PyObject_GC_UnTrack(so);
497 Py_TRASHCAN_SAFE_BEGIN(so)
498 if (so->weakreflist != NULL)
499 PyObject_ClearWeakRefs((PyObject *) so);
500
501 for (entry = so->table; fill > 0; entry++) {
502 if (entry->key) {
503 --fill;
504 Py_DECREF(entry->key);
505 }
506 }
507 if (so->table != so->smalltable)
508 PyMem_DEL(so->table);
509 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
510 free_sets[num_free_sets++] = so;
511 else
512 so->ob_type->tp_free(so);
513 Py_TRASHCAN_SAFE_END(so)
514}
515
516static int
517set_tp_print(PySetObject *so, FILE *fp, int flags)
518{
519 setentry *entry;
520 int pos=0;
521 char *emit = ""; /* No separator emitted on first pass */
522 char *separator = ", ";
523
524 fprintf(fp, "%s([", so->ob_type->tp_name);
525 while (set_next(so, &pos, &entry)) {
526 fputs(emit, fp);
527 emit = separator;
528 if (PyObject_Print(entry->key, fp, 0) != 0)
529 return -1;
530 }
531 fputs("])", fp);
532 return 0;
533}
534
535static PyObject *
536set_repr(PySetObject *so)
537{
538 PyObject *keys, *result, *listrepr;
539
540 keys = PySequence_List((PyObject *)so);
541 if (keys == NULL)
542 return NULL;
543 listrepr = PyObject_Repr(keys);
544 Py_DECREF(keys);
545 if (listrepr == NULL)
546 return NULL;
547
548 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
549 PyString_AS_STRING(listrepr));
550 Py_DECREF(listrepr);
551 return result;
552}
553
554static int
555set_len(PyObject *so)
556{
557 return ((PySetObject *)so)->used;
558}
559
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000560static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000561set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000562{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000563 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000564 register int i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000565 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000566
567 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000568 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000569
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000570 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000571 if (other == so || other->used == 0)
572 /* a.update(a) or a.update({}); nothing to do */
573 return 0;
574 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000575 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000576 * that there will be no (or few) overlapping keys.
577 */
578 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
579 if (set_table_resize(so, (so->used + other->used)*2) != 0)
580 return -1;
581 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000582 for (i = 0; i <= other->mask; i++) {
583 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000584 if (entry->key != NULL &&
585 entry->key != dummy) {
586 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000587 if (set_insert_key(so, entry->key, entry->hash) == -1) {
588 Py_DECREF(entry->key);
589 return -1;
590 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000591 }
592 }
593 return 0;
594}
595
596static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000597set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000598{
599 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000600 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000601
602 if (!PyString_CheckExact(key) ||
603 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
604 hash = PyObject_Hash(key);
605 if (hash == -1)
606 return -1;
607 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000608 entry = (so->lookup)(so, key, hash);
609 if (entry == NULL)
610 return -1;
611 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000612 return key != NULL && key != dummy;
613}
614
Raymond Hettingerc991db22005-08-11 07:58:45 +0000615static int
616set_contains_entry(PySetObject *so, setentry *entry)
617{
618 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000619 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000620
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000621 lu_entry = (so->lookup)(so, entry->key, entry->hash);
622 if (lu_entry == NULL)
623 return -1;
624 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000625 return key != NULL && key != dummy;
626}
627
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000628static PyObject *
629set_pop(PySetObject *so)
630{
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000631 register int i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000632 register setentry *entry;
633 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000634
635 assert (PyAnySet_Check(so));
636 if (so->used == 0) {
637 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
638 return NULL;
639 }
640
641 /* Set entry to "the first" unused or dummy set entry. We abuse
642 * the hash field of slot 0 to hold a search finger:
643 * If slot 0 has a value, use slot 0.
644 * Else slot 0 is being used to hold a search finger,
645 * and we use its hash value as the first index to look.
646 */
647 entry = &so->table[0];
648 if (entry->key == NULL || entry->key == dummy) {
649 i = (int)entry->hash;
650 /* The hash field may be a real hash value, or it may be a
651 * legit search finger, or it may be a once-legit search
652 * finger that's out of bounds now because it wrapped around
653 * or the table shrunk -- simply make sure it's in bounds now.
654 */
655 if (i > so->mask || i < 1)
656 i = 1; /* skip slot 0 */
657 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
658 i++;
659 if (i > so->mask)
660 i = 1;
661 }
662 }
663 key = entry->key;
664 Py_INCREF(dummy);
665 entry->key = dummy;
666 so->used--;
667 so->table[0].hash = i + 1; /* next place to start */
668 return key;
669}
670
671PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
672
673static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000674set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000675{
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000676 int pos = 0;
677 setentry *entry;
678
679 while (set_next(so, &pos, &entry))
680 Py_VISIT(entry->key);
681 return 0;
682}
683
684static long
685frozenset_hash(PyObject *self)
686{
687 PySetObject *so = (PySetObject *)self;
688 long h, hash = 1927868237L;
689 setentry *entry;
690 int pos = 0;
691
692 if (so->hash != -1)
693 return so->hash;
694
695 hash *= PySet_GET_SIZE(self) + 1;
696 while (set_next(so, &pos, &entry)) {
697 /* Work to increase the bit dispersion for closely spaced hash
698 values. The is important because some use cases have many
699 combinations of a small number of elements with nearby
700 hashes so that many distinct combinations collapse to only
701 a handful of distinct hash values. */
702 h = entry->hash;
703 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
704 }
705 hash = hash * 69069L + 907133923L;
706 if (hash == -1)
707 hash = 590923713L;
708 so->hash = hash;
709 return hash;
710}
711
712static long
713set_nohash(PyObject *self)
714{
715 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
716 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000717}
718
Raymond Hettingera9d99362005-08-05 00:01:15 +0000719/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000720
Raymond Hettingerd7946662005-08-01 21:39:29 +0000721static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000722
723typedef struct {
724 PyObject_HEAD
725 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
726 int si_used;
727 int si_pos;
728 long len;
729} setiterobject;
730
731static PyObject *
732set_iter(PySetObject *so)
733{
734 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
735 if (si == NULL)
736 return NULL;
737 Py_INCREF(so);
738 si->si_set = so;
739 si->si_used = so->used;
740 si->si_pos = 0;
741 si->len = so->used;
742 return (PyObject *)si;
743}
744
745static void
746setiter_dealloc(setiterobject *si)
747{
748 Py_XDECREF(si->si_set);
749 PyObject_Del(si);
750}
751
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000752static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000753setiter_len(setiterobject *si)
754{
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000755 int len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000756 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000757 len = si->len;
758 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000759}
760
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000761PyDoc_STRVAR(length_cue_doc, "Private method returning an estimate of len(list(it)).");
762
763static PyMethodDef setiter_methods[] = {
764 {"_length_cue", (PyCFunction)setiter_len, METH_NOARGS, length_cue_doc},
765 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000766};
767
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000768static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000769{
770 PyObject *key;
771 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000772 register setentry *entry;
773 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000774
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000775 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000776 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000777 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000778
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000779 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000780 PyErr_SetString(PyExc_RuntimeError,
781 "Set changed size during iteration");
782 si->si_used = -1; /* Make this state sticky */
783 return NULL;
784 }
785
786 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000787 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000788 entry = so->table;
789 mask = so->mask;
790 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791 i++;
792 si->si_pos = i+1;
793 if (i > mask)
794 goto fail;
795 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000796 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797 Py_INCREF(key);
798 return key;
799
800fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000801 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802 si->si_set = NULL;
803 return NULL;
804}
805
Hye-Shik Change2956762005-08-01 05:26:41 +0000806static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807 PyObject_HEAD_INIT(&PyType_Type)
808 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000809 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810 sizeof(setiterobject), /* tp_basicsize */
811 0, /* tp_itemsize */
812 /* methods */
813 (destructor)setiter_dealloc, /* tp_dealloc */
814 0, /* tp_print */
815 0, /* tp_getattr */
816 0, /* tp_setattr */
817 0, /* tp_compare */
818 0, /* tp_repr */
819 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000820 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000821 0, /* tp_as_mapping */
822 0, /* tp_hash */
823 0, /* tp_call */
824 0, /* tp_str */
825 PyObject_GenericGetAttr, /* tp_getattro */
826 0, /* tp_setattro */
827 0, /* tp_as_buffer */
828 Py_TPFLAGS_DEFAULT, /* tp_flags */
829 0, /* tp_doc */
830 0, /* tp_traverse */
831 0, /* tp_clear */
832 0, /* tp_richcompare */
833 0, /* tp_weaklistoffset */
834 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000835 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000836 setiter_methods, /* tp_methods */
837 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000838};
839
Raymond Hettingerd7946662005-08-01 21:39:29 +0000840static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000841set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000842{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000843 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000844
Raymond Hettingerd7946662005-08-01 21:39:29 +0000845 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000846 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000847
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000849 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000851 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000852 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000853 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000854 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000855 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856 }
857
Raymond Hettingera38123e2003-11-24 22:18:49 +0000858 it = PyObject_GetIter(other);
859 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000860 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000861
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000862 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000863 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000864 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000865 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000866 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000867 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000868 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000869 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000870 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000871 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000872 return -1;
873 return 0;
874}
875
876static PyObject *
877set_update(PySetObject *so, PyObject *other)
878{
879 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000880 return NULL;
881 Py_RETURN_NONE;
882}
883
884PyDoc_STRVAR(update_doc,
885"Update a set with the union of itself and another.");
886
887static PyObject *
888make_new_set(PyTypeObject *type, PyObject *iterable)
889{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000890 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000891
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000892 if (dummy == NULL) { /* Auto-initialize dummy */
893 dummy = PyString_FromString("<dummy key>");
894 if (dummy == NULL)
895 return NULL;
896 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000897
898 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000899 if (num_free_sets &&
900 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
901 so = free_sets[--num_free_sets];
902 assert (so != NULL && PyAnySet_CheckExact(so));
903 so->ob_type = type;
904 _Py_NewReference((PyObject *)so);
905 EMPTY_TO_MINSIZE(so);
906 PyObject_GC_Track(so);
907 } else {
908 so = (PySetObject *)type->tp_alloc(type, 0);
909 if (so == NULL)
910 return NULL;
911 /* tp_alloc has already zeroed the structure */
912 assert(so->table == NULL && so->fill == 0 && so->used == 0);
913 INIT_NONZERO_SET_SLOTS(so);
914 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000915
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000916 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000917 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000918
Raymond Hettingera38123e2003-11-24 22:18:49 +0000919 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000920 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000921 Py_DECREF(so);
922 return NULL;
923 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000924 }
925
Raymond Hettingera690a992003-11-16 16:17:49 +0000926 return (PyObject *)so;
927}
928
Raymond Hettingerd7946662005-08-01 21:39:29 +0000929/* The empty frozenset is a singleton */
930static PyObject *emptyfrozenset = NULL;
931
Raymond Hettingera690a992003-11-16 16:17:49 +0000932static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000933frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000934{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000935 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000936
Georg Brandl02c42872005-08-26 06:42:30 +0000937 if (!_PyArg_NoKeywords("frozenset()", kwds))
938 return NULL;
939
Raymond Hettingera690a992003-11-16 16:17:49 +0000940 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
941 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942
943 if (type != &PyFrozenSet_Type)
944 return make_new_set(type, iterable);
945
946 if (iterable != NULL) {
947 /* frozenset(f) is idempotent */
948 if (PyFrozenSet_CheckExact(iterable)) {
949 Py_INCREF(iterable);
950 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000951 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000953 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000954 return result;
955 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000956 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000957 /* The empty frozenset is a singleton */
958 if (emptyfrozenset == NULL)
959 emptyfrozenset = make_new_set(type, NULL);
960 Py_XINCREF(emptyfrozenset);
961 return emptyfrozenset;
962}
963
964void
965PySet_Fini(void)
966{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000967 PySetObject *so;
968
969 while (num_free_sets) {
970 num_free_sets--;
971 so = free_sets[num_free_sets];
972 PyObject_GC_Del(so);
973 }
Raymond Hettingera9d99362005-08-05 00:01:15 +0000974 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000975 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000976}
977
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000978static PyObject *
979set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
980{
Georg Brandl02c42872005-08-26 06:42:30 +0000981 if (!_PyArg_NoKeywords("set()", kwds))
982 return NULL;
983
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000984 return make_new_set(type, NULL);
985}
986
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000987/* set_swap_bodies() switches the contents of any two sets by moving their
988 internal data pointers and, if needed, copying the internal smalltables.
989 Semantically equivalent to:
990
991 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
992
993 The function always succeeds and it leaves both objects in a stable state.
994 Useful for creating temporary frozensets from sets for membership testing
995 in __contains__(), discard(), and remove(). Also useful for operations
996 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000997 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000998*/
999
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001000static void
1001set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001002{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001003 int t;
1004 setentry *u;
1005 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1006 setentry tab[PySet_MINSIZE];
1007 long h;
1008
1009 t = a->fill; a->fill = b->fill; b->fill = t;
1010 t = a->used; a->used = b->used; b->used = t;
1011 t = a->mask; a->mask = b->mask; b->mask = t;
1012
1013 u = a->table;
1014 if (a->table == a->smalltable)
1015 u = b->smalltable;
1016 a->table = b->table;
1017 if (b->table == b->smalltable)
1018 a->table = a->smalltable;
1019 b->table = u;
1020
1021 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1022
1023 if (a->table == a->smalltable || b->table == b->smalltable) {
1024 memcpy(tab, a->smalltable, sizeof(tab));
1025 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1026 memcpy(b->smalltable, tab, sizeof(tab));
1027 }
1028
Raymond Hettingera580c472005-08-05 17:19:54 +00001029 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1030 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1031 h = a->hash; a->hash = b->hash; b->hash = h;
1032 } else {
1033 a->hash = -1;
1034 b->hash = -1;
1035 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001036}
1037
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001038static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001039set_copy(PySetObject *so)
1040{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001041 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001042}
1043
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001044static PyObject *
1045frozenset_copy(PySetObject *so)
1046{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001047 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001048 Py_INCREF(so);
1049 return (PyObject *)so;
1050 }
1051 return set_copy(so);
1052}
1053
Raymond Hettingera690a992003-11-16 16:17:49 +00001054PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1055
1056static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001057set_clear(PySetObject *so)
1058{
1059 set_clear_internal(so);
1060 Py_RETURN_NONE;
1061}
1062
1063PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1064
1065static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001066set_union(PySetObject *so, PyObject *other)
1067{
1068 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001069
1070 result = (PySetObject *)set_copy(so);
1071 if (result == NULL)
1072 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001073 if ((PyObject *)so == other)
1074 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001075 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001076 Py_DECREF(result);
1077 return NULL;
1078 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001079 return (PyObject *)result;
1080}
1081
1082PyDoc_STRVAR(union_doc,
1083 "Return the union of two sets as a new set.\n\
1084\n\
1085(i.e. all elements that are in either set.)");
1086
1087static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001088set_or(PySetObject *so, PyObject *other)
1089{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001090 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001091 Py_INCREF(Py_NotImplemented);
1092 return Py_NotImplemented;
1093 }
1094 return set_union(so, other);
1095}
1096
1097static PyObject *
1098set_ior(PySetObject *so, PyObject *other)
1099{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001100 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001101 Py_INCREF(Py_NotImplemented);
1102 return Py_NotImplemented;
1103 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001104 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001105 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001106 Py_INCREF(so);
1107 return (PyObject *)so;
1108}
1109
1110static PyObject *
1111set_intersection(PySetObject *so, PyObject *other)
1112{
1113 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001114 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001115
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001116 if ((PyObject *)so == other)
1117 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001118
Raymond Hettingera690a992003-11-16 16:17:49 +00001119 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1120 if (result == NULL)
1121 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001122
Raymond Hettingerc991db22005-08-11 07:58:45 +00001123 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001124 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001125 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001126
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001127 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001128 tmp = (PyObject *)so;
1129 so = (PySetObject *)other;
1130 other = tmp;
1131 }
1132
Raymond Hettingerc991db22005-08-11 07:58:45 +00001133 while (set_next((PySetObject *)other, &pos, &entry)) {
1134 if (set_contains_entry(so, entry)) {
1135 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001136 Py_DECREF(result);
1137 return NULL;
1138 }
1139 }
1140 }
1141 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001142 }
1143
Raymond Hettingera690a992003-11-16 16:17:49 +00001144 it = PyObject_GetIter(other);
1145 if (it == NULL) {
1146 Py_DECREF(result);
1147 return NULL;
1148 }
1149
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001150 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001151 if (set_contains_key(so, key)) {
1152 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001153 Py_DECREF(it);
1154 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001155 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001156 return NULL;
1157 }
1158 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001159 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001160 }
1161 Py_DECREF(it);
1162 if (PyErr_Occurred()) {
1163 Py_DECREF(result);
1164 return NULL;
1165 }
1166 return (PyObject *)result;
1167}
1168
1169PyDoc_STRVAR(intersection_doc,
1170"Return the intersection of two sets as a new set.\n\
1171\n\
1172(i.e. all elements that are in both sets.)");
1173
1174static PyObject *
1175set_intersection_update(PySetObject *so, PyObject *other)
1176{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001177 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001178
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001179 tmp = set_intersection(so, other);
1180 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001181 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001182 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001183 Py_DECREF(tmp);
1184 Py_RETURN_NONE;
1185}
1186
1187PyDoc_STRVAR(intersection_update_doc,
1188"Update a set with the intersection of itself and another.");
1189
1190static PyObject *
1191set_and(PySetObject *so, PyObject *other)
1192{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001193 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001194 Py_INCREF(Py_NotImplemented);
1195 return Py_NotImplemented;
1196 }
1197 return set_intersection(so, other);
1198}
1199
1200static PyObject *
1201set_iand(PySetObject *so, PyObject *other)
1202{
1203 PyObject *result;
1204
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001205 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001206 Py_INCREF(Py_NotImplemented);
1207 return Py_NotImplemented;
1208 }
1209 result = set_intersection_update(so, other);
1210 if (result == NULL)
1211 return NULL;
1212 Py_DECREF(result);
1213 Py_INCREF(so);
1214 return (PyObject *)so;
1215}
1216
Neal Norwitz6576bd82005-11-13 18:41:28 +00001217static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001218set_difference_update_internal(PySetObject *so, PyObject *other)
1219{
1220 if ((PyObject *)so == other)
1221 return set_clear_internal(so);
1222
1223 if (PyAnySet_Check(other)) {
1224 setentry *entry;
1225 int pos = 0;
1226
1227 while (set_next((PySetObject *)other, &pos, &entry))
1228 set_discard_entry(so, entry);
1229 } else {
1230 PyObject *key, *it;
1231 it = PyObject_GetIter(other);
1232 if (it == NULL)
1233 return -1;
1234
1235 while ((key = PyIter_Next(it)) != NULL) {
1236 if (set_discard_key(so, key) == -1) {
1237 Py_DECREF(it);
1238 Py_DECREF(key);
1239 return -1;
1240 }
1241 Py_DECREF(key);
1242 }
1243 Py_DECREF(it);
1244 if (PyErr_Occurred())
1245 return -1;
1246 }
1247 /* If more than 1/5 are dummies, then resize them away. */
1248 if ((so->fill - so->used) * 5 < so->mask)
1249 return 0;
1250 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1251}
1252
Raymond Hettingera690a992003-11-16 16:17:49 +00001253static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001254set_difference_update(PySetObject *so, PyObject *other)
1255{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001256 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001257 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001258 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001259}
1260
1261PyDoc_STRVAR(difference_update_doc,
1262"Remove all elements of another set from this set.");
1263
1264static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001265set_difference(PySetObject *so, PyObject *other)
1266{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001267 PyObject *result;
1268 setentry *entry;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001269 int pos = 0;
1270
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001271 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001272 result = set_copy(so);
1273 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001274 return NULL;
1275 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001276 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001277 Py_DECREF(result);
1278 return NULL;
1279 }
1280
1281 result = make_new_set(so->ob_type, NULL);
1282 if (result == NULL)
1283 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001284
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001285 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001286 while (set_next(so, &pos, &entry)) {
1287 setentry entrycopy;
1288 entrycopy.hash = entry->hash;
1289 entrycopy.key = entry->key;
1290 if (!PyDict_Contains(other, entry->key)) {
1291 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001292 return NULL;
1293 }
1294 }
1295 return result;
1296 }
1297
Raymond Hettingerc991db22005-08-11 07:58:45 +00001298 while (set_next(so, &pos, &entry)) {
1299 if (!set_contains_entry((PySetObject *)other, entry)) {
1300 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001301 return NULL;
1302 }
1303 }
1304 return result;
1305}
1306
1307PyDoc_STRVAR(difference_doc,
1308"Return the difference of two sets as a new set.\n\
1309\n\
1310(i.e. all elements that are in this set but not the other.)");
1311static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001312set_sub(PySetObject *so, PyObject *other)
1313{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001314 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001315 Py_INCREF(Py_NotImplemented);
1316 return Py_NotImplemented;
1317 }
1318 return set_difference(so, other);
1319}
1320
1321static PyObject *
1322set_isub(PySetObject *so, PyObject *other)
1323{
1324 PyObject *result;
1325
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001326 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001327 Py_INCREF(Py_NotImplemented);
1328 return Py_NotImplemented;
1329 }
1330 result = set_difference_update(so, other);
1331 if (result == NULL)
1332 return NULL;
1333 Py_DECREF(result);
1334 Py_INCREF(so);
1335 return (PyObject *)so;
1336}
1337
1338static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001339set_symmetric_difference_update(PySetObject *so, PyObject *other)
1340{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001341 PySetObject *otherset;
1342 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001343 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001344 setentry *entry;
1345
1346 if ((PyObject *)so == other)
1347 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001348
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001349 if (PyDict_Check(other)) {
1350 PyObject *value;
1351 int rv;
1352 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001353 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001354 if (rv == -1)
1355 return NULL;
1356 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001357 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001358 return NULL;
1359 }
1360 }
1361 Py_RETURN_NONE;
1362 }
1363
1364 if (PyAnySet_Check(other)) {
1365 Py_INCREF(other);
1366 otherset = (PySetObject *)other;
1367 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001368 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1369 if (otherset == NULL)
1370 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001371 }
1372
Raymond Hettingerc991db22005-08-11 07:58:45 +00001373 while (set_next(otherset, &pos, &entry)) {
1374 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001375 if (rv == -1) {
1376 Py_XDECREF(otherset);
1377 return NULL;
1378 }
1379 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001380 if (set_add_entry(so, entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001381 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001382 return NULL;
1383 }
1384 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001385 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001386 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001387 Py_RETURN_NONE;
1388}
1389
1390PyDoc_STRVAR(symmetric_difference_update_doc,
1391"Update a set with the symmetric difference of itself and another.");
1392
1393static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001394set_symmetric_difference(PySetObject *so, PyObject *other)
1395{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001396 PyObject *rv;
1397 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001398
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001399 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1400 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001401 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001402 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1403 if (rv == NULL)
1404 return NULL;
1405 Py_DECREF(rv);
1406 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001407}
1408
1409PyDoc_STRVAR(symmetric_difference_doc,
1410"Return the symmetric difference of two sets as a new set.\n\
1411\n\
1412(i.e. all elements that are in exactly one of the sets.)");
1413
1414static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001415set_xor(PySetObject *so, PyObject *other)
1416{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001417 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001418 Py_INCREF(Py_NotImplemented);
1419 return Py_NotImplemented;
1420 }
1421 return set_symmetric_difference(so, other);
1422}
1423
1424static PyObject *
1425set_ixor(PySetObject *so, PyObject *other)
1426{
1427 PyObject *result;
1428
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001429 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001430 Py_INCREF(Py_NotImplemented);
1431 return Py_NotImplemented;
1432 }
1433 result = set_symmetric_difference_update(so, other);
1434 if (result == NULL)
1435 return NULL;
1436 Py_DECREF(result);
1437 Py_INCREF(so);
1438 return (PyObject *)so;
1439}
1440
1441static PyObject *
1442set_issubset(PySetObject *so, PyObject *other)
1443{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001444 setentry *entry;
1445 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001446
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001447 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001448 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001449 tmp = make_new_set(&PySet_Type, other);
1450 if (tmp == NULL)
1451 return NULL;
1452 result = set_issubset(so, tmp);
1453 Py_DECREF(tmp);
1454 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001455 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001456 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001457 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001458
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001459 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001460 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001461 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001462 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001463 Py_RETURN_TRUE;
1464}
1465
1466PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1467
1468static PyObject *
1469set_issuperset(PySetObject *so, PyObject *other)
1470{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001471 PyObject *tmp, *result;
1472
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001473 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001474 tmp = make_new_set(&PySet_Type, other);
1475 if (tmp == NULL)
1476 return NULL;
1477 result = set_issuperset(so, tmp);
1478 Py_DECREF(tmp);
1479 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001480 }
1481 return set_issubset((PySetObject *)other, (PyObject *)so);
1482}
1483
1484PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1485
Raymond Hettingera690a992003-11-16 16:17:49 +00001486static PyObject *
1487set_richcompare(PySetObject *v, PyObject *w, int op)
1488{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001489 PyObject *r1, *r2;
1490
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001491 if(!PyAnySet_Check(w)) {
1492 if (op == Py_EQ)
1493 Py_RETURN_FALSE;
1494 if (op == Py_NE)
1495 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001496 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1497 return NULL;
1498 }
1499 switch (op) {
1500 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001501 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001502 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001503 if (v->hash != -1 &&
1504 ((PySetObject *)w)->hash != -1 &&
1505 v->hash != ((PySetObject *)w)->hash)
1506 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001507 return set_issubset(v, w);
1508 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001509 r1 = set_richcompare(v, w, Py_EQ);
1510 if (r1 == NULL)
1511 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001512 r2 = PyBool_FromLong(PyObject_Not(r1));
1513 Py_DECREF(r1);
1514 return r2;
1515 case Py_LE:
1516 return set_issubset(v, w);
1517 case Py_GE:
1518 return set_issuperset(v, w);
1519 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001520 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001521 Py_RETURN_FALSE;
1522 return set_issubset(v, w);
1523 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001524 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001525 Py_RETURN_FALSE;
1526 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001527 }
1528 Py_INCREF(Py_NotImplemented);
1529 return Py_NotImplemented;
1530}
1531
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001532static int
1533set_nocmp(PyObject *self)
1534{
1535 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1536 return -1;
1537}
1538
Raymond Hettingera690a992003-11-16 16:17:49 +00001539static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001540set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001541{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001542 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001543 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001544 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001545}
1546
1547PyDoc_STRVAR(add_doc,
1548"Add an element to a set.\n\
1549\n\
1550This has no effect if the element is already present.");
1551
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001552static int
1553set_contains(PySetObject *so, PyObject *key)
1554{
1555 PyObject *tmpkey;
1556 int rv;
1557
1558 rv = set_contains_key(so, key);
1559 if (rv == -1) {
1560 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1561 return -1;
1562 PyErr_Clear();
1563 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1564 if (tmpkey == NULL)
1565 return -1;
1566 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1567 rv = set_contains(so, tmpkey);
1568 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1569 Py_DECREF(tmpkey);
1570 }
1571 return rv;
1572}
1573
1574static PyObject *
1575set_direct_contains(PySetObject *so, PyObject *key)
1576{
1577 long result;
1578
1579 result = set_contains(so, key);
1580 if (result == -1)
1581 return NULL;
1582 return PyBool_FromLong(result);
1583}
1584
1585PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1586
Raymond Hettingera690a992003-11-16 16:17:49 +00001587static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001588set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001589{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001590 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001591 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001592
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001593 rv = set_discard_key(so, key);
1594 if (rv == -1) {
1595 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1596 return NULL;
1597 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001598 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1599 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001600 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001601 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001602 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001603 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001604 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001605 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001606 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001607 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001608 return NULL;
1609 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001610 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001611}
1612
1613PyDoc_STRVAR(remove_doc,
1614"Remove an element from a set; it must be a member.\n\
1615\n\
1616If the element is not a member, raise a KeyError.");
1617
1618static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001619set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001620{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001621 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001622 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001623
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001624 rv = set_discard_key(so, key);
1625 if (rv == -1) {
1626 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1627 return NULL;
1628 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001629 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1630 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001631 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001632 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001633 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001634 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001635 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001636 return result;
1637 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001638 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001639}
1640
1641PyDoc_STRVAR(discard_doc,
1642"Remove an element from a set if it is a member.\n\
1643\n\
1644If the element is not a member, do nothing.");
1645
1646static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001647set_reduce(PySetObject *so)
1648{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001649 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001650
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001651 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001652 if (keys == NULL)
1653 goto done;
1654 args = PyTuple_Pack(1, keys);
1655 if (args == NULL)
1656 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001657 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1658 if (dict == NULL) {
1659 PyErr_Clear();
1660 dict = Py_None;
1661 Py_INCREF(dict);
1662 }
1663 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001664done:
1665 Py_XDECREF(args);
1666 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001667 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001668 return result;
1669}
1670
1671PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1672
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001673static int
1674set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1675{
1676 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001677
1678 if (!PyAnySet_Check(self))
1679 return -1;
1680 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1681 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001682 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001683 self->hash = -1;
1684 if (iterable == NULL)
1685 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001686 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001687}
1688
Raymond Hettingera690a992003-11-16 16:17:49 +00001689static PySequenceMethods set_as_sequence = {
1690 (inquiry)set_len, /* sq_length */
1691 0, /* sq_concat */
1692 0, /* sq_repeat */
1693 0, /* sq_item */
1694 0, /* sq_slice */
1695 0, /* sq_ass_item */
1696 0, /* sq_ass_slice */
1697 (objobjproc)set_contains, /* sq_contains */
1698};
1699
1700/* set object ********************************************************/
1701
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001702#ifdef Py_DEBUG
1703static PyObject *test_c_api(PySetObject *so);
1704
1705PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1706All is well if assertions don't fail.");
1707#endif
1708
Raymond Hettingera690a992003-11-16 16:17:49 +00001709static PyMethodDef set_methods[] = {
1710 {"add", (PyCFunction)set_add, METH_O,
1711 add_doc},
1712 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1713 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001714 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001715 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001716 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1717 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001718 {"discard", (PyCFunction)set_discard, METH_O,
1719 discard_doc},
1720 {"difference", (PyCFunction)set_difference, METH_O,
1721 difference_doc},
1722 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1723 difference_update_doc},
1724 {"intersection",(PyCFunction)set_intersection, METH_O,
1725 intersection_doc},
1726 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1727 intersection_update_doc},
1728 {"issubset", (PyCFunction)set_issubset, METH_O,
1729 issubset_doc},
1730 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1731 issuperset_doc},
1732 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1733 pop_doc},
1734 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1735 reduce_doc},
1736 {"remove", (PyCFunction)set_remove, METH_O,
1737 remove_doc},
1738 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1739 symmetric_difference_doc},
1740 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1741 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001742#ifdef Py_DEBUG
1743 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1744 test_c_api_doc},
1745#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001746 {"union", (PyCFunction)set_union, METH_O,
1747 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001748 {"update", (PyCFunction)set_update, METH_O,
1749 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001750 {NULL, NULL} /* sentinel */
1751};
1752
1753static PyNumberMethods set_as_number = {
1754 0, /*nb_add*/
1755 (binaryfunc)set_sub, /*nb_subtract*/
1756 0, /*nb_multiply*/
1757 0, /*nb_divide*/
1758 0, /*nb_remainder*/
1759 0, /*nb_divmod*/
1760 0, /*nb_power*/
1761 0, /*nb_negative*/
1762 0, /*nb_positive*/
1763 0, /*nb_absolute*/
1764 0, /*nb_nonzero*/
1765 0, /*nb_invert*/
1766 0, /*nb_lshift*/
1767 0, /*nb_rshift*/
1768 (binaryfunc)set_and, /*nb_and*/
1769 (binaryfunc)set_xor, /*nb_xor*/
1770 (binaryfunc)set_or, /*nb_or*/
1771 0, /*nb_coerce*/
1772 0, /*nb_int*/
1773 0, /*nb_long*/
1774 0, /*nb_float*/
1775 0, /*nb_oct*/
1776 0, /*nb_hex*/
1777 0, /*nb_inplace_add*/
1778 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1779 0, /*nb_inplace_multiply*/
1780 0, /*nb_inplace_divide*/
1781 0, /*nb_inplace_remainder*/
1782 0, /*nb_inplace_power*/
1783 0, /*nb_inplace_lshift*/
1784 0, /*nb_inplace_rshift*/
1785 (binaryfunc)set_iand, /*nb_inplace_and*/
1786 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1787 (binaryfunc)set_ior, /*nb_inplace_or*/
1788};
1789
1790PyDoc_STRVAR(set_doc,
1791"set(iterable) --> set object\n\
1792\n\
1793Build an unordered collection.");
1794
1795PyTypeObject PySet_Type = {
1796 PyObject_HEAD_INIT(&PyType_Type)
1797 0, /* ob_size */
1798 "set", /* tp_name */
1799 sizeof(PySetObject), /* tp_basicsize */
1800 0, /* tp_itemsize */
1801 /* methods */
1802 (destructor)set_dealloc, /* tp_dealloc */
1803 (printfunc)set_tp_print, /* tp_print */
1804 0, /* tp_getattr */
1805 0, /* tp_setattr */
1806 (cmpfunc)set_nocmp, /* tp_compare */
1807 (reprfunc)set_repr, /* tp_repr */
1808 &set_as_number, /* tp_as_number */
1809 &set_as_sequence, /* tp_as_sequence */
1810 0, /* tp_as_mapping */
1811 set_nohash, /* tp_hash */
1812 0, /* tp_call */
1813 0, /* tp_str */
1814 PyObject_GenericGetAttr, /* tp_getattro */
1815 0, /* tp_setattro */
1816 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001817 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001818 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001819 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001820 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001821 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001822 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001823 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001824 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001825 0, /* tp_iternext */
1826 set_methods, /* tp_methods */
1827 0, /* tp_members */
1828 0, /* tp_getset */
1829 0, /* tp_base */
1830 0, /* tp_dict */
1831 0, /* tp_descr_get */
1832 0, /* tp_descr_set */
1833 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001834 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001835 PyType_GenericAlloc, /* tp_alloc */
1836 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001837 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001838};
1839
1840/* frozenset object ********************************************************/
1841
1842
1843static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001844 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001845 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001846 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001847 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001848 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001849 difference_doc},
1850 {"intersection",(PyCFunction)set_intersection, METH_O,
1851 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001852 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001853 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001854 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001855 issuperset_doc},
1856 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1857 reduce_doc},
1858 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1859 symmetric_difference_doc},
1860 {"union", (PyCFunction)set_union, METH_O,
1861 union_doc},
1862 {NULL, NULL} /* sentinel */
1863};
1864
1865static PyNumberMethods frozenset_as_number = {
1866 0, /*nb_add*/
1867 (binaryfunc)set_sub, /*nb_subtract*/
1868 0, /*nb_multiply*/
1869 0, /*nb_divide*/
1870 0, /*nb_remainder*/
1871 0, /*nb_divmod*/
1872 0, /*nb_power*/
1873 0, /*nb_negative*/
1874 0, /*nb_positive*/
1875 0, /*nb_absolute*/
1876 0, /*nb_nonzero*/
1877 0, /*nb_invert*/
1878 0, /*nb_lshift*/
1879 0, /*nb_rshift*/
1880 (binaryfunc)set_and, /*nb_and*/
1881 (binaryfunc)set_xor, /*nb_xor*/
1882 (binaryfunc)set_or, /*nb_or*/
1883};
1884
1885PyDoc_STRVAR(frozenset_doc,
1886"frozenset(iterable) --> frozenset object\n\
1887\n\
1888Build an immutable unordered collection.");
1889
1890PyTypeObject PyFrozenSet_Type = {
1891 PyObject_HEAD_INIT(&PyType_Type)
1892 0, /* ob_size */
1893 "frozenset", /* tp_name */
1894 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001895 0, /* tp_itemsize */
1896 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001897 (destructor)set_dealloc, /* tp_dealloc */
1898 (printfunc)set_tp_print, /* tp_print */
1899 0, /* tp_getattr */
1900 0, /* tp_setattr */
1901 (cmpfunc)set_nocmp, /* tp_compare */
1902 (reprfunc)set_repr, /* tp_repr */
1903 &frozenset_as_number, /* tp_as_number */
1904 &set_as_sequence, /* tp_as_sequence */
1905 0, /* tp_as_mapping */
1906 frozenset_hash, /* tp_hash */
1907 0, /* tp_call */
1908 0, /* tp_str */
1909 PyObject_GenericGetAttr, /* tp_getattro */
1910 0, /* tp_setattro */
1911 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001912 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001913 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001914 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001915 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001916 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001917 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001918 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001919 (getiterfunc)set_iter, /* tp_iter */
1920 0, /* tp_iternext */
1921 frozenset_methods, /* tp_methods */
1922 0, /* tp_members */
1923 0, /* tp_getset */
1924 0, /* tp_base */
1925 0, /* tp_dict */
1926 0, /* tp_descr_get */
1927 0, /* tp_descr_set */
1928 0, /* tp_dictoffset */
1929 0, /* tp_init */
1930 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001931 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001932 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001933};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001934
1935
1936/***** C API functions *************************************************/
1937
1938PyObject *
1939PySet_New(PyObject *iterable)
1940{
1941 return make_new_set(&PySet_Type, iterable);
1942}
1943
1944PyObject *
1945PyFrozenSet_New(PyObject *iterable)
1946{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001947 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001948
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001949 if (iterable == NULL)
1950 args = PyTuple_New(0);
1951 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001952 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001953 if (args == NULL)
1954 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001955 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001956 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001957 return result;
1958}
1959
1960int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001961PySet_Size(PyObject *anyset)
1962{
1963 if (!PyAnySet_Check(anyset)) {
1964 PyErr_BadInternalCall();
1965 return -1;
1966 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001967 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001968}
1969
1970int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001971PySet_Contains(PyObject *anyset, PyObject *key)
1972{
1973 if (!PyAnySet_Check(anyset)) {
1974 PyErr_BadInternalCall();
1975 return -1;
1976 }
1977 return set_contains_key((PySetObject *)anyset, key);
1978}
1979
1980int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001981PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001982{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001983 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001984 PyErr_BadInternalCall();
1985 return -1;
1986 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001987 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001988}
1989
1990int
1991PySet_Add(PyObject *set, PyObject *key)
1992{
1993 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1994 PyErr_BadInternalCall();
1995 return -1;
1996 }
1997 return set_add_key((PySetObject *)set, key);
1998}
1999
2000PyObject *
2001PySet_Pop(PyObject *set)
2002{
2003 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2004 PyErr_BadInternalCall();
2005 return NULL;
2006 }
2007 return set_pop((PySetObject *)set);
2008}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002009
2010
2011#ifdef Py_DEBUG
2012
2013/* Test code to be called with any three element set.
2014 Returns True and original set is restored. */
2015
2016#define assertRaises(call_return_value, exception) \
2017 do { \
2018 assert(call_return_value); \
2019 assert(PyErr_ExceptionMatches(exception)); \
2020 PyErr_Clear(); \
2021 } while(0)
2022
2023static PyObject *
2024test_c_api(PySetObject *so)
2025{
2026 PyObject *elem, *dup, *t, *f, *ob = (PyObject *)so;
2027
2028 /* Verify preconditions and exercise type/size checks */
2029 assert(PyAnySet_Check(ob));
2030 assert(PyAnySet_CheckExact(ob));
2031 assert(!PyFrozenSet_CheckExact(ob));
2032 assert(PySet_Size(ob) == 3);
2033 assert(PySet_GET_SIZE(ob) == 3);
2034
2035 /* Raise TypeError for non-iterable constructor arguments */
2036 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2037 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2038
2039 /* Raise TypeError for unhashable key */
2040 dup = PySet_New(ob);
2041 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2042 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2043 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2044
2045 /* Exercise successful pop, contains, add, and discard */
2046 elem = PySet_Pop(ob);
2047 assert(PySet_Contains(ob, elem) == 0);
2048 assert(PySet_GET_SIZE(ob) == 2);
2049 assert(PySet_Add(ob, elem) == 0);
2050 assert(PySet_Contains(ob, elem) == 1);
2051 assert(PySet_GET_SIZE(ob) == 3);
2052 assert(PySet_Discard(ob, elem) == 1);
2053 assert(PySet_GET_SIZE(ob) == 2);
2054 assert(PySet_Discard(ob, elem) == 0);
2055 assert(PySet_GET_SIZE(ob) == 2);
2056
2057 /* Raise SystemError when self argument is not a set or frozenset. */
2058 t = PyTuple_New(0);
2059 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2060 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2061 Py_DECREF(t);
2062
2063 /* Raise SystemError when self argument is not a set. */
2064 f = PyFrozenSet_New(dup);
2065 assert(PySet_Size(f) == 3);
2066 assert(PyFrozenSet_CheckExact(f));
2067 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2068 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2069 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2070 Py_DECREF(f);
2071
2072 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002073 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2074 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002075 assert(PySet_GET_SIZE(ob) == 0);
2076 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2077
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002078 /* Restore the set from the copy using the PyNumber API */
2079 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2080 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002081
2082 /* Verify constructors accept NULL arguments */
2083 f = PySet_New(NULL);
2084 assert(f != NULL);
2085 assert(PySet_GET_SIZE(f) == 0);
2086 Py_DECREF(f);
2087 f = PyFrozenSet_New(NULL);
2088 assert(f != NULL);
2089 assert(PyFrozenSet_CheckExact(f));
2090 assert(PySet_GET_SIZE(f) == 0);
2091 Py_DECREF(f);
2092
2093 Py_DECREF(elem);
2094 Py_DECREF(dup);
2095 Py_RETURN_TRUE;
2096}
2097
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002098#undef assertRaises
2099
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002100#endif