blob: edc43df57469e4109c3d05b2210b4523bfed3f14 [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
Martin v. Löwis72d20672006-04-11 09:04:12 +00006 Copyright (c) 2003-6 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 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{
Martin v. Löwis18e16552006-02-15 17:27:45 +000054 register Py_ssize_t i;
55 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000056 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{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000132 register Py_ssize_t i;
133 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000134 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 Hettinger334b5b22006-03-26 03:11:29 +0000448 assert(entry->key == NULL);
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
Martin v. Löwis18e16552006-02-15 17:27:45 +0000471set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473 Py_ssize_t i;
474 int mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000475 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476
477 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000478 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000479 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000480 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000481 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000482 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000484 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000485 if (i > mask)
486 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000487 assert(table[i].key != NULL);
488 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489 return 1;
490}
491
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000492static void
493set_dealloc(PySetObject *so)
494{
495 register setentry *entry;
496 int fill = so->fill;
497 PyObject_GC_UnTrack(so);
498 Py_TRASHCAN_SAFE_BEGIN(so)
499 if (so->weakreflist != NULL)
500 PyObject_ClearWeakRefs((PyObject *) so);
501
502 for (entry = so->table; fill > 0; entry++) {
503 if (entry->key) {
504 --fill;
505 Py_DECREF(entry->key);
506 }
507 }
508 if (so->table != so->smalltable)
509 PyMem_DEL(so->table);
510 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
511 free_sets[num_free_sets++] = so;
512 else
513 so->ob_type->tp_free(so);
514 Py_TRASHCAN_SAFE_END(so)
515}
516
517static int
518set_tp_print(PySetObject *so, FILE *fp, int flags)
519{
520 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000521 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000522 char *emit = ""; /* No separator emitted on first pass */
523 char *separator = ", ";
524
525 fprintf(fp, "%s([", so->ob_type->tp_name);
526 while (set_next(so, &pos, &entry)) {
527 fputs(emit, fp);
528 emit = separator;
529 if (PyObject_Print(entry->key, fp, 0) != 0)
530 return -1;
531 }
532 fputs("])", fp);
533 return 0;
534}
535
536static PyObject *
537set_repr(PySetObject *so)
538{
539 PyObject *keys, *result, *listrepr;
540
541 keys = PySequence_List((PyObject *)so);
542 if (keys == NULL)
543 return NULL;
544 listrepr = PyObject_Repr(keys);
545 Py_DECREF(keys);
546 if (listrepr == NULL)
547 return NULL;
548
549 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
550 PyString_AS_STRING(listrepr));
551 Py_DECREF(listrepr);
552 return result;
553}
554
Martin v. Löwis18e16552006-02-15 17:27:45 +0000555static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000556set_len(PyObject *so)
557{
558 return ((PySetObject *)so)->used;
559}
560
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000561static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000562set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000563{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000564 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000565 register int i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000566 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000567
568 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000569 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000570
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000571 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000572 if (other == so || other->used == 0)
573 /* a.update(a) or a.update({}); nothing to do */
574 return 0;
575 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000576 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000577 * that there will be no (or few) overlapping keys.
578 */
579 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
580 if (set_table_resize(so, (so->used + other->used)*2) != 0)
581 return -1;
582 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000583 for (i = 0; i <= other->mask; i++) {
584 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000585 if (entry->key != NULL &&
586 entry->key != dummy) {
587 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000588 if (set_insert_key(so, entry->key, entry->hash) == -1) {
589 Py_DECREF(entry->key);
590 return -1;
591 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000592 }
593 }
594 return 0;
595}
596
597static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000598set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000599{
600 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000601 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000602
603 if (!PyString_CheckExact(key) ||
604 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
605 hash = PyObject_Hash(key);
606 if (hash == -1)
607 return -1;
608 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000609 entry = (so->lookup)(so, key, hash);
610 if (entry == NULL)
611 return -1;
612 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000613 return key != NULL && key != dummy;
614}
615
Raymond Hettingerc991db22005-08-11 07:58:45 +0000616static int
617set_contains_entry(PySetObject *so, setentry *entry)
618{
619 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000620 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000621
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000622 lu_entry = (so->lookup)(so, entry->key, entry->hash);
623 if (lu_entry == NULL)
624 return -1;
625 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000626 return key != NULL && key != dummy;
627}
628
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000629static PyObject *
630set_pop(PySetObject *so)
631{
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000632 register int i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000633 register setentry *entry;
634 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000635
636 assert (PyAnySet_Check(so));
637 if (so->used == 0) {
638 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
639 return NULL;
640 }
641
642 /* Set entry to "the first" unused or dummy set entry. We abuse
643 * the hash field of slot 0 to hold a search finger:
644 * If slot 0 has a value, use slot 0.
645 * Else slot 0 is being used to hold a search finger,
646 * and we use its hash value as the first index to look.
647 */
648 entry = &so->table[0];
649 if (entry->key == NULL || entry->key == dummy) {
650 i = (int)entry->hash;
651 /* The hash field may be a real hash value, or it may be a
652 * legit search finger, or it may be a once-legit search
653 * finger that's out of bounds now because it wrapped around
654 * or the table shrunk -- simply make sure it's in bounds now.
655 */
656 if (i > so->mask || i < 1)
657 i = 1; /* skip slot 0 */
658 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
659 i++;
660 if (i > so->mask)
661 i = 1;
662 }
663 }
664 key = entry->key;
665 Py_INCREF(dummy);
666 entry->key = dummy;
667 so->used--;
668 so->table[0].hash = i + 1; /* next place to start */
669 return key;
670}
671
672PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
673
674static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000675set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000676{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000677 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000678 setentry *entry;
679
680 while (set_next(so, &pos, &entry))
681 Py_VISIT(entry->key);
682 return 0;
683}
684
685static long
686frozenset_hash(PyObject *self)
687{
688 PySetObject *so = (PySetObject *)self;
689 long h, hash = 1927868237L;
690 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000691 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000692
693 if (so->hash != -1)
694 return so->hash;
695
696 hash *= PySet_GET_SIZE(self) + 1;
697 while (set_next(so, &pos, &entry)) {
698 /* Work to increase the bit dispersion for closely spaced hash
699 values. The is important because some use cases have many
700 combinations of a small number of elements with nearby
701 hashes so that many distinct combinations collapse to only
702 a handful of distinct hash values. */
703 h = entry->hash;
704 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
705 }
706 hash = hash * 69069L + 907133923L;
707 if (hash == -1)
708 hash = 590923713L;
709 so->hash = hash;
710 return hash;
711}
712
713static long
714set_nohash(PyObject *self)
715{
716 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
717 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000718}
719
Raymond Hettingera9d99362005-08-05 00:01:15 +0000720/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000721
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000722typedef struct {
723 PyObject_HEAD
724 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
725 int si_used;
726 int si_pos;
727 long len;
728} setiterobject;
729
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000730static void
731setiter_dealloc(setiterobject *si)
732{
733 Py_XDECREF(si->si_set);
734 PyObject_Del(si);
735}
736
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000737static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000738setiter_len(setiterobject *si)
739{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000740 long len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000741 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000742 len = si->len;
743 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000744}
745
Armin Rigof5b3e362006-02-11 21:32:43 +0000746PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000747
748static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000749 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000750 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000751};
752
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000753static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000754{
755 PyObject *key;
756 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000757 register setentry *entry;
758 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000759
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000760 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000761 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000762 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000763
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000764 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000765 PyErr_SetString(PyExc_RuntimeError,
766 "Set changed size during iteration");
767 si->si_used = -1; /* Make this state sticky */
768 return NULL;
769 }
770
771 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000772 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000773 entry = so->table;
774 mask = so->mask;
775 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000776 i++;
777 si->si_pos = i+1;
778 if (i > mask)
779 goto fail;
780 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000781 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000782 Py_INCREF(key);
783 return key;
784
785fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000786 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787 si->si_set = NULL;
788 return NULL;
789}
790
Hye-Shik Change2956762005-08-01 05:26:41 +0000791static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792 PyObject_HEAD_INIT(&PyType_Type)
793 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000794 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000795 sizeof(setiterobject), /* tp_basicsize */
796 0, /* tp_itemsize */
797 /* methods */
798 (destructor)setiter_dealloc, /* tp_dealloc */
799 0, /* tp_print */
800 0, /* tp_getattr */
801 0, /* tp_setattr */
802 0, /* tp_compare */
803 0, /* tp_repr */
804 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000805 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000806 0, /* tp_as_mapping */
807 0, /* tp_hash */
808 0, /* tp_call */
809 0, /* tp_str */
810 PyObject_GenericGetAttr, /* tp_getattro */
811 0, /* tp_setattro */
812 0, /* tp_as_buffer */
813 Py_TPFLAGS_DEFAULT, /* tp_flags */
814 0, /* tp_doc */
815 0, /* tp_traverse */
816 0, /* tp_clear */
817 0, /* tp_richcompare */
818 0, /* tp_weaklistoffset */
819 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000820 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000821 setiter_methods, /* tp_methods */
822 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000823};
824
Martin v. Löwis72d20672006-04-11 09:04:12 +0000825static PyObject *
826set_iter(PySetObject *so)
827{
828 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
829 if (si == NULL)
830 return NULL;
831 Py_INCREF(so);
832 si->si_set = so;
833 si->si_used = so->used;
834 si->si_pos = 0;
835 si->len = so->used;
836 return (PyObject *)si;
837}
838
Raymond Hettingerd7946662005-08-01 21:39:29 +0000839static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000840set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000841{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000842 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000843
Raymond Hettingerd7946662005-08-01 21:39:29 +0000844 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000845 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000846
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847 if (PyDict_Check(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000848 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000849 Py_ssize_t pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000850 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000851 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000852 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000854 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000855 }
856
Raymond Hettingera38123e2003-11-24 22:18:49 +0000857 it = PyObject_GetIter(other);
858 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000859 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000860
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000861 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000862 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000863 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000864 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000865 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000866 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000867 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000868 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000869 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000870 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000871 return -1;
872 return 0;
873}
874
875static PyObject *
876set_update(PySetObject *so, PyObject *other)
877{
878 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000879 return NULL;
880 Py_RETURN_NONE;
881}
882
883PyDoc_STRVAR(update_doc,
884"Update a set with the union of itself and another.");
885
886static PyObject *
887make_new_set(PyTypeObject *type, PyObject *iterable)
888{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000889 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000890
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891 if (dummy == NULL) { /* Auto-initialize dummy */
892 dummy = PyString_FromString("<dummy key>");
893 if (dummy == NULL)
894 return NULL;
895 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000896
897 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000898 if (num_free_sets &&
899 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
900 so = free_sets[--num_free_sets];
901 assert (so != NULL && PyAnySet_CheckExact(so));
902 so->ob_type = type;
903 _Py_NewReference((PyObject *)so);
904 EMPTY_TO_MINSIZE(so);
905 PyObject_GC_Track(so);
906 } else {
907 so = (PySetObject *)type->tp_alloc(type, 0);
908 if (so == NULL)
909 return NULL;
910 /* tp_alloc has already zeroed the structure */
911 assert(so->table == NULL && so->fill == 0 && so->used == 0);
912 INIT_NONZERO_SET_SLOTS(so);
913 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000914
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000915 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000916 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000917
Raymond Hettingera38123e2003-11-24 22:18:49 +0000918 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000919 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000920 Py_DECREF(so);
921 return NULL;
922 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000923 }
924
Raymond Hettingera690a992003-11-16 16:17:49 +0000925 return (PyObject *)so;
926}
927
Raymond Hettingerd7946662005-08-01 21:39:29 +0000928/* The empty frozenset is a singleton */
929static PyObject *emptyfrozenset = NULL;
930
Raymond Hettingera690a992003-11-16 16:17:49 +0000931static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000932frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000933{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000934 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000935
Georg Brandl02c42872005-08-26 06:42:30 +0000936 if (!_PyArg_NoKeywords("frozenset()", kwds))
937 return NULL;
938
Raymond Hettingera690a992003-11-16 16:17:49 +0000939 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
940 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000941
942 if (type != &PyFrozenSet_Type)
943 return make_new_set(type, iterable);
944
945 if (iterable != NULL) {
946 /* frozenset(f) is idempotent */
947 if (PyFrozenSet_CheckExact(iterable)) {
948 Py_INCREF(iterable);
949 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000950 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000951 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000952 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000953 return result;
954 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000955 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000956 /* The empty frozenset is a singleton */
957 if (emptyfrozenset == NULL)
958 emptyfrozenset = make_new_set(type, NULL);
959 Py_XINCREF(emptyfrozenset);
960 return emptyfrozenset;
961}
962
963void
964PySet_Fini(void)
965{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000966 PySetObject *so;
967
968 while (num_free_sets) {
969 num_free_sets--;
970 so = free_sets[num_free_sets];
971 PyObject_GC_Del(so);
972 }
Raymond Hettingera9d99362005-08-05 00:01:15 +0000973 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000974 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000975}
976
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000977static PyObject *
978set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
979{
Georg Brandl02c42872005-08-26 06:42:30 +0000980 if (!_PyArg_NoKeywords("set()", kwds))
981 return NULL;
982
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000983 return make_new_set(type, NULL);
984}
985
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000986/* set_swap_bodies() switches the contents of any two sets by moving their
987 internal data pointers and, if needed, copying the internal smalltables.
988 Semantically equivalent to:
989
990 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
991
992 The function always succeeds and it leaves both objects in a stable state.
993 Useful for creating temporary frozensets from sets for membership testing
994 in __contains__(), discard(), and remove(). Also useful for operations
995 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000996 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000997*/
998
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000999static void
1000set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001001{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001002 int t;
1003 setentry *u;
1004 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1005 setentry tab[PySet_MINSIZE];
1006 long h;
1007
1008 t = a->fill; a->fill = b->fill; b->fill = t;
1009 t = a->used; a->used = b->used; b->used = t;
1010 t = a->mask; a->mask = b->mask; b->mask = t;
1011
1012 u = a->table;
1013 if (a->table == a->smalltable)
1014 u = b->smalltable;
1015 a->table = b->table;
1016 if (b->table == b->smalltable)
1017 a->table = a->smalltable;
1018 b->table = u;
1019
1020 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1021
1022 if (a->table == a->smalltable || b->table == b->smalltable) {
1023 memcpy(tab, a->smalltable, sizeof(tab));
1024 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1025 memcpy(b->smalltable, tab, sizeof(tab));
1026 }
1027
Raymond Hettingera580c472005-08-05 17:19:54 +00001028 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1029 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1030 h = a->hash; a->hash = b->hash; b->hash = h;
1031 } else {
1032 a->hash = -1;
1033 b->hash = -1;
1034 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001035}
1036
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001037static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001038set_copy(PySetObject *so)
1039{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001040 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001041}
1042
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001043static PyObject *
1044frozenset_copy(PySetObject *so)
1045{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001046 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001047 Py_INCREF(so);
1048 return (PyObject *)so;
1049 }
1050 return set_copy(so);
1051}
1052
Raymond Hettingera690a992003-11-16 16:17:49 +00001053PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1054
1055static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001056set_clear(PySetObject *so)
1057{
1058 set_clear_internal(so);
1059 Py_RETURN_NONE;
1060}
1061
1062PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1063
1064static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001065set_union(PySetObject *so, PyObject *other)
1066{
1067 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001068
1069 result = (PySetObject *)set_copy(so);
1070 if (result == NULL)
1071 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001072 if ((PyObject *)so == other)
1073 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001074 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001075 Py_DECREF(result);
1076 return NULL;
1077 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001078 return (PyObject *)result;
1079}
1080
1081PyDoc_STRVAR(union_doc,
1082 "Return the union of two sets as a new set.\n\
1083\n\
1084(i.e. all elements that are in either set.)");
1085
1086static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001087set_or(PySetObject *so, PyObject *other)
1088{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001089 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001090 Py_INCREF(Py_NotImplemented);
1091 return Py_NotImplemented;
1092 }
1093 return set_union(so, other);
1094}
1095
1096static PyObject *
1097set_ior(PySetObject *so, PyObject *other)
1098{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001099 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001100 Py_INCREF(Py_NotImplemented);
1101 return Py_NotImplemented;
1102 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001103 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001104 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001105 Py_INCREF(so);
1106 return (PyObject *)so;
1107}
1108
1109static PyObject *
1110set_intersection(PySetObject *so, PyObject *other)
1111{
1112 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001113 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001114
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001115 if ((PyObject *)so == other)
1116 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001117
Raymond Hettingera690a992003-11-16 16:17:49 +00001118 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1119 if (result == NULL)
1120 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001121
Raymond Hettingerc991db22005-08-11 07:58:45 +00001122 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001123 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001124 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001125
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001126 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001127 tmp = (PyObject *)so;
1128 so = (PySetObject *)other;
1129 other = tmp;
1130 }
1131
Raymond Hettingerc991db22005-08-11 07:58:45 +00001132 while (set_next((PySetObject *)other, &pos, &entry)) {
1133 if (set_contains_entry(so, entry)) {
1134 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001135 Py_DECREF(result);
1136 return NULL;
1137 }
1138 }
1139 }
1140 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001141 }
1142
Raymond Hettingera690a992003-11-16 16:17:49 +00001143 it = PyObject_GetIter(other);
1144 if (it == NULL) {
1145 Py_DECREF(result);
1146 return NULL;
1147 }
1148
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001149 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001150 if (set_contains_key(so, key)) {
1151 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001152 Py_DECREF(it);
1153 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001154 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001155 return NULL;
1156 }
1157 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001158 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001159 }
1160 Py_DECREF(it);
1161 if (PyErr_Occurred()) {
1162 Py_DECREF(result);
1163 return NULL;
1164 }
1165 return (PyObject *)result;
1166}
1167
1168PyDoc_STRVAR(intersection_doc,
1169"Return the intersection of two sets as a new set.\n\
1170\n\
1171(i.e. all elements that are in both sets.)");
1172
1173static PyObject *
1174set_intersection_update(PySetObject *so, PyObject *other)
1175{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001176 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001177
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001178 tmp = set_intersection(so, other);
1179 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001180 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001181 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001182 Py_DECREF(tmp);
1183 Py_RETURN_NONE;
1184}
1185
1186PyDoc_STRVAR(intersection_update_doc,
1187"Update a set with the intersection of itself and another.");
1188
1189static PyObject *
1190set_and(PySetObject *so, PyObject *other)
1191{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001192 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001193 Py_INCREF(Py_NotImplemented);
1194 return Py_NotImplemented;
1195 }
1196 return set_intersection(so, other);
1197}
1198
1199static PyObject *
1200set_iand(PySetObject *so, PyObject *other)
1201{
1202 PyObject *result;
1203
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001204 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001205 Py_INCREF(Py_NotImplemented);
1206 return Py_NotImplemented;
1207 }
1208 result = set_intersection_update(so, other);
1209 if (result == NULL)
1210 return NULL;
1211 Py_DECREF(result);
1212 Py_INCREF(so);
1213 return (PyObject *)so;
1214}
1215
Neal Norwitz6576bd82005-11-13 18:41:28 +00001216static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001217set_difference_update_internal(PySetObject *so, PyObject *other)
1218{
1219 if ((PyObject *)so == other)
1220 return set_clear_internal(so);
1221
1222 if (PyAnySet_Check(other)) {
1223 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001224 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001225
1226 while (set_next((PySetObject *)other, &pos, &entry))
1227 set_discard_entry(so, entry);
1228 } else {
1229 PyObject *key, *it;
1230 it = PyObject_GetIter(other);
1231 if (it == NULL)
1232 return -1;
1233
1234 while ((key = PyIter_Next(it)) != NULL) {
1235 if (set_discard_key(so, key) == -1) {
1236 Py_DECREF(it);
1237 Py_DECREF(key);
1238 return -1;
1239 }
1240 Py_DECREF(key);
1241 }
1242 Py_DECREF(it);
1243 if (PyErr_Occurred())
1244 return -1;
1245 }
1246 /* If more than 1/5 are dummies, then resize them away. */
1247 if ((so->fill - so->used) * 5 < so->mask)
1248 return 0;
1249 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1250}
1251
Raymond Hettingera690a992003-11-16 16:17:49 +00001252static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001253set_difference_update(PySetObject *so, PyObject *other)
1254{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001255 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001256 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001257 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001258}
1259
1260PyDoc_STRVAR(difference_update_doc,
1261"Remove all elements of another set from this set.");
1262
1263static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001264set_difference(PySetObject *so, PyObject *other)
1265{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001266 PyObject *result;
1267 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001268 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001269
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001270 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001271 result = set_copy(so);
1272 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001273 return NULL;
1274 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001275 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001276 Py_DECREF(result);
1277 return NULL;
1278 }
1279
1280 result = make_new_set(so->ob_type, NULL);
1281 if (result == NULL)
1282 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001283
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001284 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001285 while (set_next(so, &pos, &entry)) {
1286 setentry entrycopy;
1287 entrycopy.hash = entry->hash;
1288 entrycopy.key = entry->key;
1289 if (!PyDict_Contains(other, entry->key)) {
1290 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001291 return NULL;
1292 }
1293 }
1294 return result;
1295 }
1296
Raymond Hettingerc991db22005-08-11 07:58:45 +00001297 while (set_next(so, &pos, &entry)) {
1298 if (!set_contains_entry((PySetObject *)other, entry)) {
1299 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001300 return NULL;
1301 }
1302 }
1303 return result;
1304}
1305
1306PyDoc_STRVAR(difference_doc,
1307"Return the difference of two sets as a new set.\n\
1308\n\
1309(i.e. all elements that are in this set but not the other.)");
1310static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001311set_sub(PySetObject *so, PyObject *other)
1312{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001313 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001314 Py_INCREF(Py_NotImplemented);
1315 return Py_NotImplemented;
1316 }
1317 return set_difference(so, other);
1318}
1319
1320static PyObject *
1321set_isub(PySetObject *so, PyObject *other)
1322{
1323 PyObject *result;
1324
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001325 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001326 Py_INCREF(Py_NotImplemented);
1327 return Py_NotImplemented;
1328 }
1329 result = set_difference_update(so, other);
1330 if (result == NULL)
1331 return NULL;
1332 Py_DECREF(result);
1333 Py_INCREF(so);
1334 return (PyObject *)so;
1335}
1336
1337static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001338set_symmetric_difference_update(PySetObject *so, PyObject *other)
1339{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001340 PySetObject *otherset;
1341 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001342 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001343 setentry *entry;
1344
1345 if ((PyObject *)so == other)
1346 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001347
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001348 if (PyDict_Check(other)) {
1349 PyObject *value;
1350 int rv;
1351 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001352 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001353 if (rv == -1)
1354 return NULL;
1355 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001356 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001357 return NULL;
1358 }
1359 }
1360 Py_RETURN_NONE;
1361 }
1362
1363 if (PyAnySet_Check(other)) {
1364 Py_INCREF(other);
1365 otherset = (PySetObject *)other;
1366 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001367 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1368 if (otherset == NULL)
1369 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001370 }
1371
Raymond Hettingerc991db22005-08-11 07:58:45 +00001372 while (set_next(otherset, &pos, &entry)) {
1373 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001374 if (rv == -1) {
1375 Py_XDECREF(otherset);
1376 return NULL;
1377 }
1378 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001379 if (set_add_entry(so, entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001380 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001381 return NULL;
1382 }
1383 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001384 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001385 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001386 Py_RETURN_NONE;
1387}
1388
1389PyDoc_STRVAR(symmetric_difference_update_doc,
1390"Update a set with the symmetric difference of itself and another.");
1391
1392static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001393set_symmetric_difference(PySetObject *so, PyObject *other)
1394{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001395 PyObject *rv;
1396 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001397
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001398 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1399 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001400 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001401 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1402 if (rv == NULL)
1403 return NULL;
1404 Py_DECREF(rv);
1405 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001406}
1407
1408PyDoc_STRVAR(symmetric_difference_doc,
1409"Return the symmetric difference of two sets as a new set.\n\
1410\n\
1411(i.e. all elements that are in exactly one of the sets.)");
1412
1413static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001414set_xor(PySetObject *so, PyObject *other)
1415{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001416 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001417 Py_INCREF(Py_NotImplemented);
1418 return Py_NotImplemented;
1419 }
1420 return set_symmetric_difference(so, other);
1421}
1422
1423static PyObject *
1424set_ixor(PySetObject *so, PyObject *other)
1425{
1426 PyObject *result;
1427
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001428 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001429 Py_INCREF(Py_NotImplemented);
1430 return Py_NotImplemented;
1431 }
1432 result = set_symmetric_difference_update(so, other);
1433 if (result == NULL)
1434 return NULL;
1435 Py_DECREF(result);
1436 Py_INCREF(so);
1437 return (PyObject *)so;
1438}
1439
1440static PyObject *
1441set_issubset(PySetObject *so, PyObject *other)
1442{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001443 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001444 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001445
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001446 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001447 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001448 tmp = make_new_set(&PySet_Type, other);
1449 if (tmp == NULL)
1450 return NULL;
1451 result = set_issubset(so, tmp);
1452 Py_DECREF(tmp);
1453 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001454 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001455 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001456 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001457
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001458 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001459 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001460 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001461 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001462 Py_RETURN_TRUE;
1463}
1464
1465PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1466
1467static PyObject *
1468set_issuperset(PySetObject *so, PyObject *other)
1469{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001470 PyObject *tmp, *result;
1471
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001472 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001473 tmp = make_new_set(&PySet_Type, other);
1474 if (tmp == NULL)
1475 return NULL;
1476 result = set_issuperset(so, tmp);
1477 Py_DECREF(tmp);
1478 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001479 }
1480 return set_issubset((PySetObject *)other, (PyObject *)so);
1481}
1482
1483PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1484
Raymond Hettingera690a992003-11-16 16:17:49 +00001485static PyObject *
1486set_richcompare(PySetObject *v, PyObject *w, int op)
1487{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001488 PyObject *r1, *r2;
1489
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001490 if(!PyAnySet_Check(w)) {
1491 if (op == Py_EQ)
1492 Py_RETURN_FALSE;
1493 if (op == Py_NE)
1494 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001495 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1496 return NULL;
1497 }
1498 switch (op) {
1499 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001500 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001501 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001502 if (v->hash != -1 &&
1503 ((PySetObject *)w)->hash != -1 &&
1504 v->hash != ((PySetObject *)w)->hash)
1505 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001506 return set_issubset(v, w);
1507 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001508 r1 = set_richcompare(v, w, Py_EQ);
1509 if (r1 == NULL)
1510 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001511 r2 = PyBool_FromLong(PyObject_Not(r1));
1512 Py_DECREF(r1);
1513 return r2;
1514 case Py_LE:
1515 return set_issubset(v, w);
1516 case Py_GE:
1517 return set_issuperset(v, w);
1518 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001519 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001520 Py_RETURN_FALSE;
1521 return set_issubset(v, w);
1522 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001523 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001524 Py_RETURN_FALSE;
1525 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001526 }
1527 Py_INCREF(Py_NotImplemented);
1528 return Py_NotImplemented;
1529}
1530
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001531static int
Georg Brandl347b3002006-03-30 11:57:00 +00001532set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001533{
1534 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1535 return -1;
1536}
1537
Raymond Hettingera690a992003-11-16 16:17:49 +00001538static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001539set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001540{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001541 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001542 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001543 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001544}
1545
1546PyDoc_STRVAR(add_doc,
1547"Add an element to a set.\n\
1548\n\
1549This has no effect if the element is already present.");
1550
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001551static int
1552set_contains(PySetObject *so, PyObject *key)
1553{
1554 PyObject *tmpkey;
1555 int rv;
1556
1557 rv = set_contains_key(so, key);
1558 if (rv == -1) {
1559 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1560 return -1;
1561 PyErr_Clear();
1562 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1563 if (tmpkey == NULL)
1564 return -1;
1565 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1566 rv = set_contains(so, tmpkey);
1567 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1568 Py_DECREF(tmpkey);
1569 }
1570 return rv;
1571}
1572
1573static PyObject *
1574set_direct_contains(PySetObject *so, PyObject *key)
1575{
1576 long result;
1577
1578 result = set_contains(so, key);
1579 if (result == -1)
1580 return NULL;
1581 return PyBool_FromLong(result);
1582}
1583
1584PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1585
Raymond Hettingera690a992003-11-16 16:17:49 +00001586static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001587set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001588{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001589 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001590 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001591
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001592 rv = set_discard_key(so, key);
1593 if (rv == -1) {
1594 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1595 return NULL;
1596 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001597 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1598 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001599 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001600 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001601 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001602 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001603 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001604 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001605 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001606 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001607 return NULL;
1608 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001609 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001610}
1611
1612PyDoc_STRVAR(remove_doc,
1613"Remove an element from a set; it must be a member.\n\
1614\n\
1615If the element is not a member, raise a KeyError.");
1616
1617static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001618set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001619{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001620 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001621 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001622
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001623 rv = set_discard_key(so, key);
1624 if (rv == -1) {
1625 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1626 return NULL;
1627 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001628 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1629 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001630 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001631 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001632 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001633 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001634 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001635 return result;
1636 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001637 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001638}
1639
1640PyDoc_STRVAR(discard_doc,
1641"Remove an element from a set if it is a member.\n\
1642\n\
1643If the element is not a member, do nothing.");
1644
1645static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001646set_reduce(PySetObject *so)
1647{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001648 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001649
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001650 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001651 if (keys == NULL)
1652 goto done;
1653 args = PyTuple_Pack(1, keys);
1654 if (args == NULL)
1655 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001656 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1657 if (dict == NULL) {
1658 PyErr_Clear();
1659 dict = Py_None;
1660 Py_INCREF(dict);
1661 }
1662 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001663done:
1664 Py_XDECREF(args);
1665 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001666 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001667 return result;
1668}
1669
1670PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1671
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001672static int
1673set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1674{
1675 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001676
1677 if (!PyAnySet_Check(self))
1678 return -1;
1679 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1680 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001681 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001682 self->hash = -1;
1683 if (iterable == NULL)
1684 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001685 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001686}
1687
Raymond Hettingera690a992003-11-16 16:17:49 +00001688static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001689 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001690 0, /* sq_concat */
1691 0, /* sq_repeat */
1692 0, /* sq_item */
1693 0, /* sq_slice */
1694 0, /* sq_ass_item */
1695 0, /* sq_ass_slice */
1696 (objobjproc)set_contains, /* sq_contains */
1697};
1698
1699/* set object ********************************************************/
1700
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001701#ifdef Py_DEBUG
1702static PyObject *test_c_api(PySetObject *so);
1703
1704PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1705All is well if assertions don't fail.");
1706#endif
1707
Raymond Hettingera690a992003-11-16 16:17:49 +00001708static PyMethodDef set_methods[] = {
1709 {"add", (PyCFunction)set_add, METH_O,
1710 add_doc},
1711 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1712 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001713 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001714 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001715 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1716 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001717 {"discard", (PyCFunction)set_discard, METH_O,
1718 discard_doc},
1719 {"difference", (PyCFunction)set_difference, METH_O,
1720 difference_doc},
1721 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1722 difference_update_doc},
1723 {"intersection",(PyCFunction)set_intersection, METH_O,
1724 intersection_doc},
1725 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1726 intersection_update_doc},
1727 {"issubset", (PyCFunction)set_issubset, METH_O,
1728 issubset_doc},
1729 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1730 issuperset_doc},
1731 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1732 pop_doc},
1733 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1734 reduce_doc},
1735 {"remove", (PyCFunction)set_remove, METH_O,
1736 remove_doc},
1737 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1738 symmetric_difference_doc},
1739 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1740 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001741#ifdef Py_DEBUG
1742 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1743 test_c_api_doc},
1744#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001745 {"union", (PyCFunction)set_union, METH_O,
1746 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001747 {"update", (PyCFunction)set_update, METH_O,
1748 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001749 {NULL, NULL} /* sentinel */
1750};
1751
1752static PyNumberMethods set_as_number = {
1753 0, /*nb_add*/
1754 (binaryfunc)set_sub, /*nb_subtract*/
1755 0, /*nb_multiply*/
1756 0, /*nb_divide*/
1757 0, /*nb_remainder*/
1758 0, /*nb_divmod*/
1759 0, /*nb_power*/
1760 0, /*nb_negative*/
1761 0, /*nb_positive*/
1762 0, /*nb_absolute*/
1763 0, /*nb_nonzero*/
1764 0, /*nb_invert*/
1765 0, /*nb_lshift*/
1766 0, /*nb_rshift*/
1767 (binaryfunc)set_and, /*nb_and*/
1768 (binaryfunc)set_xor, /*nb_xor*/
1769 (binaryfunc)set_or, /*nb_or*/
1770 0, /*nb_coerce*/
1771 0, /*nb_int*/
1772 0, /*nb_long*/
1773 0, /*nb_float*/
1774 0, /*nb_oct*/
1775 0, /*nb_hex*/
1776 0, /*nb_inplace_add*/
1777 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1778 0, /*nb_inplace_multiply*/
1779 0, /*nb_inplace_divide*/
1780 0, /*nb_inplace_remainder*/
1781 0, /*nb_inplace_power*/
1782 0, /*nb_inplace_lshift*/
1783 0, /*nb_inplace_rshift*/
1784 (binaryfunc)set_iand, /*nb_inplace_and*/
1785 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1786 (binaryfunc)set_ior, /*nb_inplace_or*/
1787};
1788
1789PyDoc_STRVAR(set_doc,
1790"set(iterable) --> set object\n\
1791\n\
1792Build an unordered collection.");
1793
1794PyTypeObject PySet_Type = {
1795 PyObject_HEAD_INIT(&PyType_Type)
1796 0, /* ob_size */
1797 "set", /* tp_name */
1798 sizeof(PySetObject), /* tp_basicsize */
1799 0, /* tp_itemsize */
1800 /* methods */
1801 (destructor)set_dealloc, /* tp_dealloc */
1802 (printfunc)set_tp_print, /* tp_print */
1803 0, /* tp_getattr */
1804 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001805 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001806 (reprfunc)set_repr, /* tp_repr */
1807 &set_as_number, /* tp_as_number */
1808 &set_as_sequence, /* tp_as_sequence */
1809 0, /* tp_as_mapping */
1810 set_nohash, /* tp_hash */
1811 0, /* tp_call */
1812 0, /* tp_str */
1813 PyObject_GenericGetAttr, /* tp_getattro */
1814 0, /* tp_setattro */
1815 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001816 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001817 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001818 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001819 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001820 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001821 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001822 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001823 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001824 0, /* tp_iternext */
1825 set_methods, /* tp_methods */
1826 0, /* tp_members */
1827 0, /* tp_getset */
1828 0, /* tp_base */
1829 0, /* tp_dict */
1830 0, /* tp_descr_get */
1831 0, /* tp_descr_set */
1832 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001833 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001834 PyType_GenericAlloc, /* tp_alloc */
1835 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001836 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001837};
1838
1839/* frozenset object ********************************************************/
1840
1841
1842static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001843 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001844 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001845 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001846 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001847 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001848 difference_doc},
1849 {"intersection",(PyCFunction)set_intersection, METH_O,
1850 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001851 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001852 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001853 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001854 issuperset_doc},
1855 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1856 reduce_doc},
1857 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1858 symmetric_difference_doc},
1859 {"union", (PyCFunction)set_union, METH_O,
1860 union_doc},
1861 {NULL, NULL} /* sentinel */
1862};
1863
1864static PyNumberMethods frozenset_as_number = {
1865 0, /*nb_add*/
1866 (binaryfunc)set_sub, /*nb_subtract*/
1867 0, /*nb_multiply*/
1868 0, /*nb_divide*/
1869 0, /*nb_remainder*/
1870 0, /*nb_divmod*/
1871 0, /*nb_power*/
1872 0, /*nb_negative*/
1873 0, /*nb_positive*/
1874 0, /*nb_absolute*/
1875 0, /*nb_nonzero*/
1876 0, /*nb_invert*/
1877 0, /*nb_lshift*/
1878 0, /*nb_rshift*/
1879 (binaryfunc)set_and, /*nb_and*/
1880 (binaryfunc)set_xor, /*nb_xor*/
1881 (binaryfunc)set_or, /*nb_or*/
1882};
1883
1884PyDoc_STRVAR(frozenset_doc,
1885"frozenset(iterable) --> frozenset object\n\
1886\n\
1887Build an immutable unordered collection.");
1888
1889PyTypeObject PyFrozenSet_Type = {
1890 PyObject_HEAD_INIT(&PyType_Type)
1891 0, /* ob_size */
1892 "frozenset", /* tp_name */
1893 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001894 0, /* tp_itemsize */
1895 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001896 (destructor)set_dealloc, /* tp_dealloc */
1897 (printfunc)set_tp_print, /* tp_print */
1898 0, /* tp_getattr */
1899 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001900 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001901 (reprfunc)set_repr, /* tp_repr */
1902 &frozenset_as_number, /* tp_as_number */
1903 &set_as_sequence, /* tp_as_sequence */
1904 0, /* tp_as_mapping */
1905 frozenset_hash, /* tp_hash */
1906 0, /* tp_call */
1907 0, /* tp_str */
1908 PyObject_GenericGetAttr, /* tp_getattro */
1909 0, /* tp_setattro */
1910 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001911 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001912 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001913 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001914 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001915 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001916 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001917 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001918 (getiterfunc)set_iter, /* tp_iter */
1919 0, /* tp_iternext */
1920 frozenset_methods, /* tp_methods */
1921 0, /* tp_members */
1922 0, /* tp_getset */
1923 0, /* tp_base */
1924 0, /* tp_dict */
1925 0, /* tp_descr_get */
1926 0, /* tp_descr_set */
1927 0, /* tp_dictoffset */
1928 0, /* tp_init */
1929 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001930 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001931 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001932};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001933
1934
1935/***** C API functions *************************************************/
1936
1937PyObject *
1938PySet_New(PyObject *iterable)
1939{
1940 return make_new_set(&PySet_Type, iterable);
1941}
1942
1943PyObject *
1944PyFrozenSet_New(PyObject *iterable)
1945{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001946 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001947
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001948 if (iterable == NULL)
1949 args = PyTuple_New(0);
1950 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001951 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001952 if (args == NULL)
1953 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001954 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001955 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001956 return result;
1957}
1958
Neal Norwitz8c49c822006-03-04 18:41:19 +00001959Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001960PySet_Size(PyObject *anyset)
1961{
1962 if (!PyAnySet_Check(anyset)) {
1963 PyErr_BadInternalCall();
1964 return -1;
1965 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001966 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001967}
1968
1969int
Barry Warsaw176014f2006-03-30 22:45:35 +00001970PySet_Clear(PyObject *set)
1971{
1972 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1973 PyErr_BadInternalCall();
1974 return -1;
1975 }
1976 return set_clear_internal((PySetObject *)set);
1977}
1978
1979int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001980PySet_Contains(PyObject *anyset, PyObject *key)
1981{
1982 if (!PyAnySet_Check(anyset)) {
1983 PyErr_BadInternalCall();
1984 return -1;
1985 }
1986 return set_contains_key((PySetObject *)anyset, key);
1987}
1988
1989int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001990PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001991{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001992 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001993 PyErr_BadInternalCall();
1994 return -1;
1995 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001996 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001997}
1998
1999int
2000PySet_Add(PyObject *set, PyObject *key)
2001{
2002 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2003 PyErr_BadInternalCall();
2004 return -1;
2005 }
2006 return set_add_key((PySetObject *)set, key);
2007}
2008
Barry Warsaw176014f2006-03-30 22:45:35 +00002009int
2010_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2011{
2012 setentry *entry_ptr;
2013
2014 if (!PyAnySet_Check(set)) {
2015 PyErr_BadInternalCall();
2016 return -1;
2017 }
2018 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2019 return 0;
2020 *entry = entry_ptr->key;
2021 return 1;
2022}
2023
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002024PyObject *
2025PySet_Pop(PyObject *set)
2026{
2027 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2028 PyErr_BadInternalCall();
2029 return NULL;
2030 }
2031 return set_pop((PySetObject *)set);
2032}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002033
Barry Warsaw176014f2006-03-30 22:45:35 +00002034int
2035_PySet_Update(PyObject *set, PyObject *iterable)
2036{
2037 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2038 PyErr_BadInternalCall();
2039 return -1;
2040 }
2041 return set_update_internal((PySetObject *)set, iterable);
2042}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002043
2044#ifdef Py_DEBUG
2045
2046/* Test code to be called with any three element set.
2047 Returns True and original set is restored. */
2048
2049#define assertRaises(call_return_value, exception) \
2050 do { \
2051 assert(call_return_value); \
2052 assert(PyErr_ExceptionMatches(exception)); \
2053 PyErr_Clear(); \
2054 } while(0)
2055
2056static PyObject *
2057test_c_api(PySetObject *so)
2058{
Barry Warsaw176014f2006-03-30 22:45:35 +00002059 int count;
2060 char *s;
2061 Py_ssize_t i;
2062 PyObject *elem, *dup, *t, *f, *dup2;
2063 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002064
2065 /* Verify preconditions and exercise type/size checks */
2066 assert(PyAnySet_Check(ob));
2067 assert(PyAnySet_CheckExact(ob));
2068 assert(!PyFrozenSet_CheckExact(ob));
2069 assert(PySet_Size(ob) == 3);
2070 assert(PySet_GET_SIZE(ob) == 3);
2071
2072 /* Raise TypeError for non-iterable constructor arguments */
2073 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2074 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2075
2076 /* Raise TypeError for unhashable key */
2077 dup = PySet_New(ob);
2078 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2079 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2080 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2081
2082 /* Exercise successful pop, contains, add, and discard */
2083 elem = PySet_Pop(ob);
2084 assert(PySet_Contains(ob, elem) == 0);
2085 assert(PySet_GET_SIZE(ob) == 2);
2086 assert(PySet_Add(ob, elem) == 0);
2087 assert(PySet_Contains(ob, elem) == 1);
2088 assert(PySet_GET_SIZE(ob) == 3);
2089 assert(PySet_Discard(ob, elem) == 1);
2090 assert(PySet_GET_SIZE(ob) == 2);
2091 assert(PySet_Discard(ob, elem) == 0);
2092 assert(PySet_GET_SIZE(ob) == 2);
2093
Barry Warsaw176014f2006-03-30 22:45:35 +00002094 /* Exercise clear */
2095 dup2 = PySet_New(dup);
2096 assert(PySet_Clear(dup2) == 0);
2097 assert(PySet_Size(dup2) == 0);
2098 Py_DECREF(dup2);
2099
2100 /* Raise SystemError on clear or update of frozen set */
2101 f = PyFrozenSet_New(dup);
2102 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2103 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2104 Py_DECREF(f);
2105
2106 /* Exercise direct iteration */
2107 i = 0, count = 0;
2108 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2109 s = PyString_AsString(elem);
2110 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2111 count++;
2112 }
2113 assert(count == 3);
2114
2115 /* Exercise updates */
2116 dup2 = PySet_New(NULL);
2117 assert(_PySet_Update(dup2, dup) == 0);
2118 assert(PySet_Size(dup2) == 3);
2119 assert(_PySet_Update(dup2, dup) == 0);
2120 assert(PySet_Size(dup2) == 3);
2121 Py_DECREF(dup2);
2122
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002123 /* Raise SystemError when self argument is not a set or frozenset. */
2124 t = PyTuple_New(0);
2125 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2126 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2127 Py_DECREF(t);
2128
2129 /* Raise SystemError when self argument is not a set. */
2130 f = PyFrozenSet_New(dup);
2131 assert(PySet_Size(f) == 3);
2132 assert(PyFrozenSet_CheckExact(f));
2133 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2134 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2135 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2136 Py_DECREF(f);
2137
2138 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002139 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2140 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002141 assert(PySet_GET_SIZE(ob) == 0);
2142 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2143
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002144 /* Restore the set from the copy using the PyNumber API */
2145 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2146 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002147
2148 /* Verify constructors accept NULL arguments */
2149 f = PySet_New(NULL);
2150 assert(f != NULL);
2151 assert(PySet_GET_SIZE(f) == 0);
2152 Py_DECREF(f);
2153 f = PyFrozenSet_New(NULL);
2154 assert(f != NULL);
2155 assert(PyFrozenSet_CheckExact(f));
2156 assert(PySet_GET_SIZE(f) == 0);
2157 Py_DECREF(f);
2158
2159 Py_DECREF(elem);
2160 Py_DECREF(dup);
2161 Py_RETURN_TRUE;
2162}
2163
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002164#undef assertRaises
2165
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002166#endif