blob: 795efc59298451c08cab78c202f55e09b387ad0b [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Wouters89f507f2006-12-13 04:49:30 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
19 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
25}
26
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000027/* This must be >= 1. */
28#define PERTURB_SHIFT 5
29
30/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000031static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033#ifdef Py_REF_DEBUG
34PyObject *
35_PySet_Dummy(void)
36{
37 return dummy;
38}
39#endif
40
Raymond Hettingerbc841a12005-08-07 13:02:53 +000041#define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
44 (so)->hash = -1; \
45 } while(0)
46
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047#define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000050 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051 } while(0)
52
Raymond Hettingerbc841a12005-08-07 13:02:53 +000053/* Reuse scheme to save calls to malloc, free, and memset */
54#define MAXFREESETS 80
55static PySetObject *free_sets[MAXFREESETS];
56static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057
58/*
59The basic lookup function used by all operations.
60This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
61Open addressing is preferred over chaining since the link overhead for
62chaining would be substantial (100% with typical malloc overhead).
63
64The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000065probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000066
67All arithmetic on hash should ignore overflow.
68
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000069Unlike the dictionary implementation, the lookkey functions can return
70NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000071*/
72
73static setentry *
74set_lookkey(PySetObject *so, PyObject *key, register long hash)
75{
Martin v. Löwis18e16552006-02-15 17:27:45 +000076 register Py_ssize_t i;
77 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000078 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000080 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000081 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000083 PyObject *startkey;
84
85 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000086 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000087 if (entry->key == NULL || entry->key == key)
88 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000089
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000090 if (entry->key == dummy)
91 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000092 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000093 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000095 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
96 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000097 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +000098 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000099 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000100 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 }
102 else {
103 /* The compare did major nasty stuff to the
104 * set: start over.
105 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000106 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000107 }
108 }
109 freeslot = NULL;
110 }
111
112 /* In the loop, key == dummy is by far (factor of 100s) the
113 least likely outcome, so test for that last. */
114 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
115 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000116 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000117 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000118 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000119 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 break;
121 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000122 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000123 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000124 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
127 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000128 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000129 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130 if (cmp > 0)
131 break;
132 }
133 else {
134 /* The compare did major nasty stuff to the
135 * set: start over.
136 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000137 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138 }
139 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000140 else if (entry->key == dummy && freeslot == NULL)
141 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000142 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000143 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000144}
145
146/*
147 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000148 * This means we can always use _PyString_Eq directly and not have to check to
149 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150 */
151static setentry *
152set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
153{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000154 register Py_ssize_t i;
155 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000156 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000158 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000159 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000160
161 /* Make sure this function doesn't have to handle non-string keys,
162 including subclasses of str; e.g., one reason to subclass
163 strings is to override __eq__, and for speed we don't cater to
164 that here. */
165 if (!PyString_CheckExact(key)) {
166 so->lookup = set_lookkey;
167 return set_lookkey(so, key, hash);
168 }
169 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000170 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000171 if (entry->key == NULL || entry->key == key)
172 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000173 if (entry->key == dummy)
174 freeslot = entry;
175 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000176 if (entry->hash == hash && _PyString_Eq(entry->key, key))
177 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000178 freeslot = NULL;
179 }
180
181 /* In the loop, key == dummy is by far (factor of 100s) the
182 least likely outcome, so test for that last. */
183 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
184 i = (i << 2) + i + perturb + 1;
185 entry = &table[i & mask];
186 if (entry->key == NULL)
187 return freeslot == NULL ? entry : freeslot;
188 if (entry->key == key
189 || (entry->hash == hash
190 && entry->key != dummy
191 && _PyString_Eq(entry->key, key)))
192 return entry;
193 if (entry->key == dummy && freeslot == NULL)
194 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000195 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000196 assert(0); /* NOT REACHED */
197 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000198}
199
200/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000201Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000202Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000203Eats a reference to key.
204*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000205static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206set_insert_key(register PySetObject *so, PyObject *key, long hash)
207{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000208 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
210
211 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000212 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213 if (entry == NULL)
214 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000215 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000216 /* UNUSED */
217 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000218 entry->key = key;
219 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000221 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 entry->key = key;
224 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000225 so->used++;
226 Py_DECREF(dummy);
227 } else {
228 /* ACTIVE */
229 Py_DECREF(key);
230 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000231 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000232}
233
234/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000235Internal routine used by set_table_resize() to insert an item which is
236known to be absent from the set. This routine also assumes that
237the set contains no deleted entries. Besides the performance benefit,
238using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
239Note that no refcounts are changed by this routine; if needed, the caller
240is responsible for incref'ing `key`.
241*/
242static void
243set_insert_clean(register PySetObject *so, PyObject *key, long hash)
244{
245 register size_t i;
246 register size_t perturb;
247 register size_t mask = (size_t)so->mask;
248 setentry *table = so->table;
249 register setentry *entry;
250
251 i = hash & mask;
252 entry = &table[i];
253 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
254 i = (i << 2) + i + perturb + 1;
255 entry = &table[i & mask];
256 }
257 so->fill++;
258 entry->key = key;
259 entry->hash = hash;
260 so->used++;
261}
262
263/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000264Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000265keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266actually be smaller than the old one.
267*/
268static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000269set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000272 setentry *oldtable, *newtable, *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274 int is_oldtable_malloced;
275 setentry small_copy[PySet_MINSIZE];
276
277 assert(minused >= 0);
278
279 /* Find the smallest table size > minused. */
280 for (newsize = PySet_MINSIZE;
281 newsize <= minused && newsize > 0;
282 newsize <<= 1)
283 ;
284 if (newsize <= 0) {
285 PyErr_NoMemory();
286 return -1;
287 }
288
289 /* Get space for a new table. */
290 oldtable = so->table;
291 assert(oldtable != NULL);
292 is_oldtable_malloced = oldtable != so->smalltable;
293
294 if (newsize == PySet_MINSIZE) {
295 /* A large table is shrinking, or we can't get any smaller. */
296 newtable = so->smalltable;
297 if (newtable == oldtable) {
298 if (so->fill == so->used) {
299 /* No dummies, so no point doing anything. */
300 return 0;
301 }
302 /* We're not going to resize it, but rebuild the
303 table anyway to purge old dummy entries.
304 Subtle: This is *necessary* if fill==size,
305 as set_lookkey needs at least one virgin slot to
306 terminate failing searches. If fill < size, it's
307 merely desirable, as dummies slow searches. */
308 assert(so->fill > so->used);
309 memcpy(small_copy, oldtable, sizeof(small_copy));
310 oldtable = small_copy;
311 }
312 }
313 else {
314 newtable = PyMem_NEW(setentry, newsize);
315 if (newtable == NULL) {
316 PyErr_NoMemory();
317 return -1;
318 }
319 }
320
321 /* Make the set empty, using the new table. */
322 assert(newtable != oldtable);
323 so->table = newtable;
324 so->mask = newsize - 1;
325 memset(newtable, 0, sizeof(setentry) * newsize);
326 so->used = 0;
327 i = so->fill;
328 so->fill = 0;
329
330 /* Copy the data over; this is refcount-neutral for active entries;
331 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000332 for (entry = oldtable; i > 0; entry++) {
333 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334 /* UNUSED */
335 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000336 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337 /* DUMMY */
338 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000339 assert(entry->key == dummy);
340 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341 } else {
342 /* ACTIVE */
343 --i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000344 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345 }
346 }
347
348 if (is_oldtable_malloced)
349 PyMem_DEL(oldtable);
350 return 0;
351}
352
Raymond Hettingerc991db22005-08-11 07:58:45 +0000353/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
354
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356set_add_entry(register PySetObject *so, setentry *entry)
357{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000358 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000359
360 assert(so->fill <= so->mask); /* at least one empty slot */
361 n_used = so->used;
362 Py_INCREF(entry->key);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000363 if (set_insert_key(so, entry->key, entry->hash) == -1) {
364 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000365 return -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000367 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
368 return 0;
369 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
370}
371
372static int
373set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000374{
375 register long hash;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000376 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377
Raymond Hettingerc991db22005-08-11 07:58:45 +0000378 if (!PyString_CheckExact(key) ||
379 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380 hash = PyObject_Hash(key);
381 if (hash == -1)
382 return -1;
383 }
384 assert(so->fill <= so->mask); /* at least one empty slot */
385 n_used = so->used;
386 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000387 if (set_insert_key(so, key, hash) == -1) {
388 Py_DECREF(key);
389 return -1;
390 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000391 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
392 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000393 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394}
395
396#define DISCARD_NOTFOUND 0
397#define DISCARD_FOUND 1
398
399static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000400set_discard_entry(PySetObject *so, setentry *oldentry)
401{ register setentry *entry;
402 PyObject *old_key;
403
404 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000405 if (entry == NULL)
406 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000407 if (entry->key == NULL || entry->key == dummy)
408 return DISCARD_NOTFOUND;
409 old_key = entry->key;
410 Py_INCREF(dummy);
411 entry->key = dummy;
412 so->used--;
413 Py_DECREF(old_key);
414 return DISCARD_FOUND;
415}
416
417static int
418set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000419{
420 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000421 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000422 PyObject *old_key;
423
424 assert (PyAnySet_Check(so));
425 if (!PyString_CheckExact(key) ||
426 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
427 hash = PyObject_Hash(key);
428 if (hash == -1)
429 return -1;
430 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000431 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000432 if (entry == NULL)
433 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000434 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000435 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000438 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439 so->used--;
440 Py_DECREF(old_key);
441 return DISCARD_FOUND;
442}
443
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000444static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445set_clear_internal(PySetObject *so)
446{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000447 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448 int table_is_malloced;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450 setentry small_copy[PySet_MINSIZE];
451#ifdef Py_DEBUG
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000452 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000454
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455 n = so->mask + 1;
456 i = 0;
457#endif
458
459 table = so->table;
460 assert(table != NULL);
461 table_is_malloced = table != so->smalltable;
462
463 /* This is delicate. During the process of clearing the set,
464 * decrefs can cause the set to mutate. To avoid fatal confusion
465 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000466 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467 * clearing.
468 */
469 fill = so->fill;
470 if (table_is_malloced)
471 EMPTY_TO_MINSIZE(so);
472
473 else if (fill > 0) {
474 /* It's a small table with something that needs to be cleared.
475 * Afraid the only safe way is to copy the set entries into
476 * another small table first.
477 */
478 memcpy(small_copy, table, sizeof(small_copy));
479 table = small_copy;
480 EMPTY_TO_MINSIZE(so);
481 }
482 /* else it's a small table that's already empty */
483
484 /* Now we can finally clear things. If C had refcounts, we could
485 * assert that the refcount on table is 1 now, i.e. that this function
486 * has unique access to it, so decref side-effects can't alter it.
487 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000488 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489#ifdef Py_DEBUG
490 assert(i < n);
491 ++i;
492#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000493 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000495 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496 }
497#ifdef Py_DEBUG
498 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500#endif
501 }
502
503 if (table_is_malloced)
504 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000505 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506}
507
508/*
509 * Iterate over a set table. Use like so:
510 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000512 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000513 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000514 * while (set_next(yourset, &pos, &entry)) {
515 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516 * }
517 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000518 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519 * mutates the table.
520 */
521static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000522set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000526 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000527
528 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000529 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000530 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000531 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000533 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536 if (i > mask)
537 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000538 assert(table[i].key != NULL);
539 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540 return 1;
541}
542
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000543static void
544set_dealloc(PySetObject *so)
545{
546 register setentry *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000547 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000548 PyObject_GC_UnTrack(so);
549 Py_TRASHCAN_SAFE_BEGIN(so)
550 if (so->weakreflist != NULL)
551 PyObject_ClearWeakRefs((PyObject *) so);
552
553 for (entry = so->table; fill > 0; entry++) {
554 if (entry->key) {
555 --fill;
556 Py_DECREF(entry->key);
557 }
558 }
559 if (so->table != so->smalltable)
560 PyMem_DEL(so->table);
561 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
562 free_sets[num_free_sets++] = so;
563 else
564 so->ob_type->tp_free(so);
565 Py_TRASHCAN_SAFE_END(so)
566}
567
568static int
569set_tp_print(PySetObject *so, FILE *fp, int flags)
570{
571 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000572 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000573 char *emit = ""; /* No separator emitted on first pass */
574 char *separator = ", ";
Georg Brandlc4996ba2006-08-28 19:37:11 +0000575 int literalform = 0;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000576 int status = Py_ReprEnter((PyObject*)so);
577
578 if (status != 0) {
579 if (status < 0)
580 return status;
581 fprintf(fp, "%s(...)", so->ob_type->tp_name);
582 return 0;
583 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000584
Georg Brandlc4996ba2006-08-28 19:37:11 +0000585 if (!so->used) {
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000586 Py_ReprLeave((PyObject*)so);
Georg Brandlc4996ba2006-08-28 19:37:11 +0000587 fprintf(fp, "%s()", so->ob_type->tp_name);
588 return 0;
589 }
590
591 if (so->ob_type == &PySet_Type) {
592 literalform = 1;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000593 fprintf(fp, "{");
Georg Brandlc4996ba2006-08-28 19:37:11 +0000594 } else
Guido van Rossum86e58e22006-08-28 15:27:34 +0000595 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000596 while (set_next(so, &pos, &entry)) {
597 fputs(emit, fp);
598 emit = separator;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000599 if (PyObject_Print(entry->key, fp, 0) != 0) {
600 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601 return -1;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000602 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000603 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000604 if (literalform)
Guido van Rossum86e58e22006-08-28 15:27:34 +0000605 fputs("}", fp);
606 else
607 fputs("])", fp);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000608 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000609 return 0;
610}
611
612static PyObject *
613set_repr(PySetObject *so)
614{
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000615 PyObject *keys, *result=NULL, *listrepr;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000616 int newsize;
617 Py_UNICODE *u;
618 const char *s;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000619 int status = Py_ReprEnter((PyObject*)so);
620
621 if (status != 0) {
622 if (status < 0)
623 return NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000624 return PyUnicode_FromFormat("%s(...)", so->ob_type->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000625 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000626
Georg Brandlc4996ba2006-08-28 19:37:11 +0000627 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000628 if (!so->used) {
629 Py_ReprLeave((PyObject*)so);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000630 return PyUnicode_FromFormat("%s()", so->ob_type->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000631 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000632
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000633 keys = PySequence_List((PyObject *)so);
634 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000635 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000636 listrepr = PyObject_Repr(keys);
637 Py_DECREF(keys);
638 if (listrepr == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000639 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000640
Walter Dörwald1ab83302007-05-18 17:15:44 +0000641 newsize = PyUnicode_GET_SIZE(listrepr);
642 if (so->ob_type != &PySet_Type)
643 newsize += strlen(so->ob_type->tp_name)+2;
644 result = PyUnicode_FromUnicode(NULL, newsize);
645 if (result) {
646 u = PyUnicode_AS_UNICODE(result);
647 if (so->ob_type != &PySet_Type) {
648 for (s = so->ob_type->tp_name; *s;)
649 *u++ = *s++;
650 *u++ = '(';
651 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr),
652 PyUnicode_GET_SIZE(listrepr));
653 u += PyUnicode_GET_SIZE(listrepr);
654 *u++ = ')';
655 } else {
656 *u++ = '{';
657 /* Omit the brackets from the listrepr */
658 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
659 PyUnicode_GET_SIZE(listrepr)-2);
660 u += PyUnicode_GET_SIZE(listrepr)-2;
661 *u++ = '}';
662 }
Guido van Rossum86e58e22006-08-28 15:27:34 +0000663 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000664 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000665done:
666 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000667 return result;
668}
669
Martin v. Löwis18e16552006-02-15 17:27:45 +0000670static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000671set_len(PyObject *so)
672{
673 return ((PySetObject *)so)->used;
674}
675
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000676static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000677set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000678{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000679 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000680 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000681 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000682
683 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000684 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000685
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000686 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000687 if (other == so || other->used == 0)
688 /* a.update(a) or a.update({}); nothing to do */
689 return 0;
690 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000691 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000692 * that there will be no (or few) overlapping keys.
693 */
694 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
695 if (set_table_resize(so, (so->used + other->used)*2) != 0)
696 return -1;
697 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000698 for (i = 0; i <= other->mask; i++) {
699 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000700 if (entry->key != NULL &&
701 entry->key != dummy) {
702 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000703 if (set_insert_key(so, entry->key, entry->hash) == -1) {
704 Py_DECREF(entry->key);
705 return -1;
706 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000707 }
708 }
709 return 0;
710}
711
712static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000713set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000714{
715 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000716 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000717
718 if (!PyString_CheckExact(key) ||
719 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
720 hash = PyObject_Hash(key);
721 if (hash == -1)
722 return -1;
723 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000724 entry = (so->lookup)(so, key, hash);
725 if (entry == NULL)
726 return -1;
727 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000728 return key != NULL && key != dummy;
729}
730
Raymond Hettingerc991db22005-08-11 07:58:45 +0000731static int
732set_contains_entry(PySetObject *so, setentry *entry)
733{
734 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000735 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000736
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000737 lu_entry = (so->lookup)(so, entry->key, entry->hash);
738 if (lu_entry == NULL)
739 return -1;
740 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000741 return key != NULL && key != dummy;
742}
743
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000744static PyObject *
745set_pop(PySetObject *so)
746{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000747 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000748 register setentry *entry;
749 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000750
751 assert (PyAnySet_Check(so));
752 if (so->used == 0) {
753 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
754 return NULL;
755 }
756
757 /* Set entry to "the first" unused or dummy set entry. We abuse
758 * the hash field of slot 0 to hold a search finger:
759 * If slot 0 has a value, use slot 0.
760 * Else slot 0 is being used to hold a search finger,
761 * and we use its hash value as the first index to look.
762 */
763 entry = &so->table[0];
764 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000765 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000766 /* The hash field may be a real hash value, or it may be a
767 * legit search finger, or it may be a once-legit search
768 * finger that's out of bounds now because it wrapped around
769 * or the table shrunk -- simply make sure it's in bounds now.
770 */
771 if (i > so->mask || i < 1)
772 i = 1; /* skip slot 0 */
773 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
774 i++;
775 if (i > so->mask)
776 i = 1;
777 }
778 }
779 key = entry->key;
780 Py_INCREF(dummy);
781 entry->key = dummy;
782 so->used--;
783 so->table[0].hash = i + 1; /* next place to start */
784 return key;
785}
786
787PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
788
789static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000790set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000791{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000792 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000793 setentry *entry;
794
795 while (set_next(so, &pos, &entry))
796 Py_VISIT(entry->key);
797 return 0;
798}
799
800static long
801frozenset_hash(PyObject *self)
802{
803 PySetObject *so = (PySetObject *)self;
804 long h, hash = 1927868237L;
805 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000806 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000807
808 if (so->hash != -1)
809 return so->hash;
810
811 hash *= PySet_GET_SIZE(self) + 1;
812 while (set_next(so, &pos, &entry)) {
813 /* Work to increase the bit dispersion for closely spaced hash
814 values. The is important because some use cases have many
815 combinations of a small number of elements with nearby
816 hashes so that many distinct combinations collapse to only
817 a handful of distinct hash values. */
818 h = entry->hash;
819 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
820 }
821 hash = hash * 69069L + 907133923L;
822 if (hash == -1)
823 hash = 590923713L;
824 so->hash = hash;
825 return hash;
826}
827
Raymond Hettingera9d99362005-08-05 00:01:15 +0000828/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000830typedef struct {
831 PyObject_HEAD
832 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000833 Py_ssize_t si_used;
834 Py_ssize_t si_pos;
835 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000836} setiterobject;
837
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000838static void
839setiter_dealloc(setiterobject *si)
840{
841 Py_XDECREF(si->si_set);
842 PyObject_Del(si);
843}
844
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000845static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000846setiter_len(setiterobject *si)
847{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000848 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000849 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000850 len = si->len;
851 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000852}
853
Armin Rigof5b3e362006-02-11 21:32:43 +0000854PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000855
856static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000857 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000858 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000859};
860
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000861static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000862{
863 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000864 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000865 register setentry *entry;
866 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000867
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000868 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000869 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000870 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000871
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000872 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000873 PyErr_SetString(PyExc_RuntimeError,
874 "Set changed size during iteration");
875 si->si_used = -1; /* Make this state sticky */
876 return NULL;
877 }
878
879 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000880 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000881 entry = so->table;
882 mask = so->mask;
883 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000884 i++;
885 si->si_pos = i+1;
886 if (i > mask)
887 goto fail;
888 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000889 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000890 Py_INCREF(key);
891 return key;
892
893fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000894 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000895 si->si_set = NULL;
896 return NULL;
897}
898
Hye-Shik Change2956762005-08-01 05:26:41 +0000899static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000900 PyObject_HEAD_INIT(&PyType_Type)
901 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000902 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000903 sizeof(setiterobject), /* tp_basicsize */
904 0, /* tp_itemsize */
905 /* methods */
906 (destructor)setiter_dealloc, /* tp_dealloc */
907 0, /* tp_print */
908 0, /* tp_getattr */
909 0, /* tp_setattr */
910 0, /* tp_compare */
911 0, /* tp_repr */
912 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000913 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000914 0, /* tp_as_mapping */
915 0, /* tp_hash */
916 0, /* tp_call */
917 0, /* tp_str */
918 PyObject_GenericGetAttr, /* tp_getattro */
919 0, /* tp_setattro */
920 0, /* tp_as_buffer */
921 Py_TPFLAGS_DEFAULT, /* tp_flags */
922 0, /* tp_doc */
923 0, /* tp_traverse */
924 0, /* tp_clear */
925 0, /* tp_richcompare */
926 0, /* tp_weaklistoffset */
927 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000928 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000929 setiter_methods, /* tp_methods */
930 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000931};
932
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933static PyObject *
934set_iter(PySetObject *so)
935{
936 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
937 if (si == NULL)
938 return NULL;
939 Py_INCREF(so);
940 si->si_set = so;
941 si->si_used = so->used;
942 si->si_pos = 0;
943 si->len = so->used;
944 return (PyObject *)si;
945}
946
Raymond Hettingerd7946662005-08-01 21:39:29 +0000947static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000948set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000949{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000950 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000951
Thomas Wouterscf297e42007-02-23 15:07:44 +0000952 if (PyAnySet_CheckExact(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000953 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000954
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000955 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000956 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000957 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000958 long hash;
959 Py_ssize_t dictsize = PyDict_Size(other);
960
961 /* Do one big resize at the start, rather than
962 * incrementally resizing as we insert new keys. Expect
963 * that there will be no (or few) overlapping keys.
964 */
965 if (dictsize == -1)
966 return -1;
967 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
968 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
969 return -1;
970 }
971 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
972 setentry an_entry;
973
974 an_entry.hash = hash;
975 an_entry.key = key;
976 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000977 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000978 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000979 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000980 }
981
Raymond Hettingera38123e2003-11-24 22:18:49 +0000982 it = PyObject_GetIter(other);
983 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000984 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000985
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000986 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000987 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000988 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000989 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000990 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000991 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000992 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000993 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000994 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000995 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000996 return -1;
997 return 0;
998}
999
1000static PyObject *
1001set_update(PySetObject *so, PyObject *other)
1002{
1003 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +00001004 return NULL;
1005 Py_RETURN_NONE;
1006}
1007
1008PyDoc_STRVAR(update_doc,
1009"Update a set with the union of itself and another.");
1010
1011static PyObject *
1012make_new_set(PyTypeObject *type, PyObject *iterable)
1013{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001014 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001015
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001016 if (dummy == NULL) { /* Auto-initialize dummy */
1017 dummy = PyString_FromString("<dummy key>");
1018 if (dummy == NULL)
1019 return NULL;
1020 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001021
1022 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001023 if (num_free_sets &&
1024 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1025 so = free_sets[--num_free_sets];
1026 assert (so != NULL && PyAnySet_CheckExact(so));
1027 so->ob_type = type;
1028 _Py_NewReference((PyObject *)so);
1029 EMPTY_TO_MINSIZE(so);
1030 PyObject_GC_Track(so);
1031 } else {
1032 so = (PySetObject *)type->tp_alloc(type, 0);
1033 if (so == NULL)
1034 return NULL;
1035 /* tp_alloc has already zeroed the structure */
1036 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1037 INIT_NONZERO_SET_SLOTS(so);
1038 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001039
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001040 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001041 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001042
Raymond Hettingera38123e2003-11-24 22:18:49 +00001043 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001044 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001045 Py_DECREF(so);
1046 return NULL;
1047 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001048 }
1049
Raymond Hettingera690a992003-11-16 16:17:49 +00001050 return (PyObject *)so;
1051}
1052
Raymond Hettingerd7946662005-08-01 21:39:29 +00001053/* The empty frozenset is a singleton */
1054static PyObject *emptyfrozenset = NULL;
1055
Raymond Hettingera690a992003-11-16 16:17:49 +00001056static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001057frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001058{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001059 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001060
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001061 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001062 return NULL;
1063
Raymond Hettingera690a992003-11-16 16:17:49 +00001064 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1065 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001066
1067 if (type != &PyFrozenSet_Type)
1068 return make_new_set(type, iterable);
1069
1070 if (iterable != NULL) {
1071 /* frozenset(f) is idempotent */
1072 if (PyFrozenSet_CheckExact(iterable)) {
1073 Py_INCREF(iterable);
1074 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001075 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001076 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001077 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001078 return result;
1079 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001080 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001081 /* The empty frozenset is a singleton */
1082 if (emptyfrozenset == NULL)
1083 emptyfrozenset = make_new_set(type, NULL);
1084 Py_XINCREF(emptyfrozenset);
1085 return emptyfrozenset;
1086}
1087
1088void
1089PySet_Fini(void)
1090{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001091 PySetObject *so;
1092
1093 while (num_free_sets) {
1094 num_free_sets--;
1095 so = free_sets[num_free_sets];
1096 PyObject_GC_Del(so);
1097 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098 Py_CLEAR(dummy);
1099 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001100}
1101
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001102static PyObject *
1103set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1104{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001105 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001106 return NULL;
1107
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001108 return make_new_set(type, NULL);
1109}
1110
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001111/* set_swap_bodies() switches the contents of any two sets by moving their
1112 internal data pointers and, if needed, copying the internal smalltables.
1113 Semantically equivalent to:
1114
1115 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1116
1117 The function always succeeds and it leaves both objects in a stable state.
1118 Useful for creating temporary frozensets from sets for membership testing
1119 in __contains__(), discard(), and remove(). Also useful for operations
1120 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001121 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001122*/
1123
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001124static void
1125set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001126{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001127 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001128 setentry *u;
1129 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1130 setentry tab[PySet_MINSIZE];
1131 long h;
1132
1133 t = a->fill; a->fill = b->fill; b->fill = t;
1134 t = a->used; a->used = b->used; b->used = t;
1135 t = a->mask; a->mask = b->mask; b->mask = t;
1136
1137 u = a->table;
1138 if (a->table == a->smalltable)
1139 u = b->smalltable;
1140 a->table = b->table;
1141 if (b->table == b->smalltable)
1142 a->table = a->smalltable;
1143 b->table = u;
1144
1145 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1146
1147 if (a->table == a->smalltable || b->table == b->smalltable) {
1148 memcpy(tab, a->smalltable, sizeof(tab));
1149 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1150 memcpy(b->smalltable, tab, sizeof(tab));
1151 }
1152
Raymond Hettingera580c472005-08-05 17:19:54 +00001153 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1154 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1155 h = a->hash; a->hash = b->hash; b->hash = h;
1156 } else {
1157 a->hash = -1;
1158 b->hash = -1;
1159 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001160}
1161
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001162static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001163set_copy(PySetObject *so)
1164{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001165 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001166}
1167
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001168static PyObject *
1169frozenset_copy(PySetObject *so)
1170{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001171 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001172 Py_INCREF(so);
1173 return (PyObject *)so;
1174 }
1175 return set_copy(so);
1176}
1177
Raymond Hettingera690a992003-11-16 16:17:49 +00001178PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1179
1180static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001181set_clear(PySetObject *so)
1182{
1183 set_clear_internal(so);
1184 Py_RETURN_NONE;
1185}
1186
1187PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1188
1189static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001190set_union(PySetObject *so, PyObject *other)
1191{
1192 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001193
1194 result = (PySetObject *)set_copy(so);
1195 if (result == NULL)
1196 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001197 if ((PyObject *)so == other)
1198 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001199 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001200 Py_DECREF(result);
1201 return NULL;
1202 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001203 return (PyObject *)result;
1204}
1205
1206PyDoc_STRVAR(union_doc,
1207 "Return the union of two sets as a new set.\n\
1208\n\
1209(i.e. all elements that are in either set.)");
1210
1211static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001212set_or(PySetObject *so, PyObject *other)
1213{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001214 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001215 Py_INCREF(Py_NotImplemented);
1216 return Py_NotImplemented;
1217 }
1218 return set_union(so, other);
1219}
1220
1221static PyObject *
1222set_ior(PySetObject *so, PyObject *other)
1223{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001224 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001225 Py_INCREF(Py_NotImplemented);
1226 return Py_NotImplemented;
1227 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001228 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001229 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001230 Py_INCREF(so);
1231 return (PyObject *)so;
1232}
1233
1234static PyObject *
1235set_intersection(PySetObject *so, PyObject *other)
1236{
1237 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001238 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001239
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001240 if ((PyObject *)so == other)
1241 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001242
Raymond Hettingera690a992003-11-16 16:17:49 +00001243 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1244 if (result == NULL)
1245 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001246
Thomas Wouterscf297e42007-02-23 15:07:44 +00001247 if (PyAnySet_CheckExact(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001248 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001249 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001250
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001251 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001252 tmp = (PyObject *)so;
1253 so = (PySetObject *)other;
1254 other = tmp;
1255 }
1256
Raymond Hettingerc991db22005-08-11 07:58:45 +00001257 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001258 int rv = set_contains_entry(so, entry);
1259 if (rv == -1) {
1260 Py_DECREF(result);
1261 return NULL;
1262 }
1263 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001264 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001265 Py_DECREF(result);
1266 return NULL;
1267 }
1268 }
1269 }
1270 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001271 }
1272
Raymond Hettingera690a992003-11-16 16:17:49 +00001273 it = PyObject_GetIter(other);
1274 if (it == NULL) {
1275 Py_DECREF(result);
1276 return NULL;
1277 }
1278
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001279 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001280 int rv;
1281 setentry entry;
1282 long hash = PyObject_Hash(key);
1283
1284 if (hash == -1) {
1285 Py_DECREF(it);
1286 Py_DECREF(result);
1287 Py_DECREF(key);
1288 return NULL;
1289 }
1290 entry.hash = hash;
1291 entry.key = key;
1292 rv = set_contains_entry(so, &entry);
1293 if (rv == -1) {
1294 Py_DECREF(it);
1295 Py_DECREF(result);
1296 Py_DECREF(key);
1297 return NULL;
1298 }
1299 if (rv) {
1300 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001301 Py_DECREF(it);
1302 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001303 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001304 return NULL;
1305 }
1306 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001307 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001308 }
1309 Py_DECREF(it);
1310 if (PyErr_Occurred()) {
1311 Py_DECREF(result);
1312 return NULL;
1313 }
1314 return (PyObject *)result;
1315}
1316
1317PyDoc_STRVAR(intersection_doc,
1318"Return the intersection of two sets as a new set.\n\
1319\n\
1320(i.e. all elements that are in both sets.)");
1321
1322static PyObject *
1323set_intersection_update(PySetObject *so, PyObject *other)
1324{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001325 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001326
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001327 tmp = set_intersection(so, other);
1328 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001329 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001330 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001331 Py_DECREF(tmp);
1332 Py_RETURN_NONE;
1333}
1334
1335PyDoc_STRVAR(intersection_update_doc,
1336"Update a set with the intersection of itself and another.");
1337
1338static PyObject *
1339set_and(PySetObject *so, PyObject *other)
1340{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001341 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001342 Py_INCREF(Py_NotImplemented);
1343 return Py_NotImplemented;
1344 }
1345 return set_intersection(so, other);
1346}
1347
1348static PyObject *
1349set_iand(PySetObject *so, PyObject *other)
1350{
1351 PyObject *result;
1352
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001353 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001354 Py_INCREF(Py_NotImplemented);
1355 return Py_NotImplemented;
1356 }
1357 result = set_intersection_update(so, other);
1358 if (result == NULL)
1359 return NULL;
1360 Py_DECREF(result);
1361 Py_INCREF(so);
1362 return (PyObject *)so;
1363}
1364
Neal Norwitz6576bd82005-11-13 18:41:28 +00001365static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001366set_difference_update_internal(PySetObject *so, PyObject *other)
1367{
1368 if ((PyObject *)so == other)
1369 return set_clear_internal(so);
1370
Thomas Wouterscf297e42007-02-23 15:07:44 +00001371 if (PyAnySet_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001372 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001373 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001374
1375 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001376 if (set_discard_entry(so, entry) == -1)
1377 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001378 } else {
1379 PyObject *key, *it;
1380 it = PyObject_GetIter(other);
1381 if (it == NULL)
1382 return -1;
1383
1384 while ((key = PyIter_Next(it)) != NULL) {
1385 if (set_discard_key(so, key) == -1) {
1386 Py_DECREF(it);
1387 Py_DECREF(key);
1388 return -1;
1389 }
1390 Py_DECREF(key);
1391 }
1392 Py_DECREF(it);
1393 if (PyErr_Occurred())
1394 return -1;
1395 }
1396 /* If more than 1/5 are dummies, then resize them away. */
1397 if ((so->fill - so->used) * 5 < so->mask)
1398 return 0;
1399 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1400}
1401
Raymond Hettingera690a992003-11-16 16:17:49 +00001402static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001403set_difference_update(PySetObject *so, PyObject *other)
1404{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001405 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001406 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001407 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001408}
1409
1410PyDoc_STRVAR(difference_update_doc,
1411"Remove all elements of another set from this set.");
1412
1413static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001414set_difference(PySetObject *so, PyObject *other)
1415{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001416 PyObject *result;
1417 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001418 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001419
Thomas Wouterscf297e42007-02-23 15:07:44 +00001420 if (!PyAnySet_CheckExact(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001421 result = set_copy(so);
1422 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001423 return NULL;
1424 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001425 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001426 Py_DECREF(result);
1427 return NULL;
1428 }
1429
1430 result = make_new_set(so->ob_type, NULL);
1431 if (result == NULL)
1432 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001433
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001434 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001435 while (set_next(so, &pos, &entry)) {
1436 setentry entrycopy;
1437 entrycopy.hash = entry->hash;
1438 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001439 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001440 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1441 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001442 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001443 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001444 }
1445 }
1446 return result;
1447 }
1448
Raymond Hettingerc991db22005-08-11 07:58:45 +00001449 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001450 int rv = set_contains_entry((PySetObject *)other, entry);
1451 if (rv == -1) {
1452 Py_DECREF(result);
1453 return NULL;
1454 }
1455 if (!rv) {
1456 if (set_add_entry((PySetObject *)result, entry) == -1) {
1457 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001458 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001459 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001460 }
1461 }
1462 return result;
1463}
1464
1465PyDoc_STRVAR(difference_doc,
1466"Return the difference of two sets as a new set.\n\
1467\n\
1468(i.e. all elements that are in this set but not the other.)");
1469static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001470set_sub(PySetObject *so, PyObject *other)
1471{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001472 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001473 Py_INCREF(Py_NotImplemented);
1474 return Py_NotImplemented;
1475 }
1476 return set_difference(so, other);
1477}
1478
1479static PyObject *
1480set_isub(PySetObject *so, PyObject *other)
1481{
1482 PyObject *result;
1483
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001484 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001485 Py_INCREF(Py_NotImplemented);
1486 return Py_NotImplemented;
1487 }
1488 result = set_difference_update(so, other);
1489 if (result == NULL)
1490 return NULL;
1491 Py_DECREF(result);
1492 Py_INCREF(so);
1493 return (PyObject *)so;
1494}
1495
1496static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001497set_symmetric_difference_update(PySetObject *so, PyObject *other)
1498{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001499 PySetObject *otherset;
1500 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001501 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001502 setentry *entry;
1503
1504 if ((PyObject *)so == other)
1505 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001506
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001507 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001508 PyObject *value;
1509 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001510 long hash;
1511 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001512 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001513
Thomas Wouters89f507f2006-12-13 04:49:30 +00001514 an_entry.hash = hash;
1515 an_entry.key = key;
1516 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001517 if (rv == -1)
1518 return NULL;
1519 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001520 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001521 return NULL;
1522 }
1523 }
1524 Py_RETURN_NONE;
1525 }
1526
Thomas Wouterscf297e42007-02-23 15:07:44 +00001527 if (PyAnySet_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001528 Py_INCREF(other);
1529 otherset = (PySetObject *)other;
1530 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001531 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1532 if (otherset == NULL)
1533 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001534 }
1535
Raymond Hettingerc991db22005-08-11 07:58:45 +00001536 while (set_next(otherset, &pos, &entry)) {
1537 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001538 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001539 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001540 return NULL;
1541 }
1542 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001543 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001544 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001545 return NULL;
1546 }
1547 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001548 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001549 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001550 Py_RETURN_NONE;
1551}
1552
1553PyDoc_STRVAR(symmetric_difference_update_doc,
1554"Update a set with the symmetric difference of itself and another.");
1555
1556static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001557set_symmetric_difference(PySetObject *so, PyObject *other)
1558{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001559 PyObject *rv;
1560 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001561
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001562 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1563 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001564 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001565 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1566 if (rv == NULL)
1567 return NULL;
1568 Py_DECREF(rv);
1569 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001570}
1571
1572PyDoc_STRVAR(symmetric_difference_doc,
1573"Return the symmetric difference of two sets as a new set.\n\
1574\n\
1575(i.e. all elements that are in exactly one of the sets.)");
1576
1577static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001578set_xor(PySetObject *so, PyObject *other)
1579{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001580 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001581 Py_INCREF(Py_NotImplemented);
1582 return Py_NotImplemented;
1583 }
1584 return set_symmetric_difference(so, other);
1585}
1586
1587static PyObject *
1588set_ixor(PySetObject *so, PyObject *other)
1589{
1590 PyObject *result;
1591
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001592 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001593 Py_INCREF(Py_NotImplemented);
1594 return Py_NotImplemented;
1595 }
1596 result = set_symmetric_difference_update(so, other);
1597 if (result == NULL)
1598 return NULL;
1599 Py_DECREF(result);
1600 Py_INCREF(so);
1601 return (PyObject *)so;
1602}
1603
1604static PyObject *
1605set_issubset(PySetObject *so, PyObject *other)
1606{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001607 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001608 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001609
Thomas Wouterscf297e42007-02-23 15:07:44 +00001610 if (!PyAnySet_CheckExact(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001611 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001612 tmp = make_new_set(&PySet_Type, other);
1613 if (tmp == NULL)
1614 return NULL;
1615 result = set_issubset(so, tmp);
1616 Py_DECREF(tmp);
1617 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001618 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001619 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001620 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001621
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001622 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001623 int rv = set_contains_entry((PySetObject *)other, entry);
1624 if (rv == -1)
1625 return NULL;
1626 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001627 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001628 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001629 Py_RETURN_TRUE;
1630}
1631
1632PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1633
1634static PyObject *
1635set_issuperset(PySetObject *so, PyObject *other)
1636{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001637 PyObject *tmp, *result;
1638
Thomas Wouterscf297e42007-02-23 15:07:44 +00001639 if (!PyAnySet_CheckExact(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001640 tmp = make_new_set(&PySet_Type, other);
1641 if (tmp == NULL)
1642 return NULL;
1643 result = set_issuperset(so, tmp);
1644 Py_DECREF(tmp);
1645 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001646 }
1647 return set_issubset((PySetObject *)other, (PyObject *)so);
1648}
1649
1650PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1651
Raymond Hettingera690a992003-11-16 16:17:49 +00001652static PyObject *
1653set_richcompare(PySetObject *v, PyObject *w, int op)
1654{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001655 PyObject *r1, *r2;
1656
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001657 if(!PyAnySet_Check(w)) {
1658 if (op == Py_EQ)
1659 Py_RETURN_FALSE;
1660 if (op == Py_NE)
1661 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001662 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1663 return NULL;
1664 }
1665 switch (op) {
1666 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001667 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001668 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001669 if (v->hash != -1 &&
1670 ((PySetObject *)w)->hash != -1 &&
1671 v->hash != ((PySetObject *)w)->hash)
1672 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001673 return set_issubset(v, w);
1674 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001675 r1 = set_richcompare(v, w, Py_EQ);
1676 if (r1 == NULL)
1677 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001678 r2 = PyBool_FromLong(PyObject_Not(r1));
1679 Py_DECREF(r1);
1680 return r2;
1681 case Py_LE:
1682 return set_issubset(v, w);
1683 case Py_GE:
1684 return set_issuperset(v, w);
1685 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001686 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001687 Py_RETURN_FALSE;
1688 return set_issubset(v, w);
1689 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001690 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001691 Py_RETURN_FALSE;
1692 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001693 }
1694 Py_INCREF(Py_NotImplemented);
1695 return Py_NotImplemented;
1696}
1697
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001698static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001699set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001700{
1701 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1702 return -1;
1703}
1704
Raymond Hettingera690a992003-11-16 16:17:49 +00001705static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001706set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001707{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001708 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001709 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001710 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001711}
1712
1713PyDoc_STRVAR(add_doc,
1714"Add an element to a set.\n\
1715\n\
1716This has no effect if the element is already present.");
1717
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001718static int
1719set_contains(PySetObject *so, PyObject *key)
1720{
1721 PyObject *tmpkey;
1722 int rv;
1723
1724 rv = set_contains_key(so, key);
1725 if (rv == -1) {
1726 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1727 return -1;
1728 PyErr_Clear();
1729 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1730 if (tmpkey == NULL)
1731 return -1;
1732 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1733 rv = set_contains(so, tmpkey);
1734 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1735 Py_DECREF(tmpkey);
1736 }
1737 return rv;
1738}
1739
1740static PyObject *
1741set_direct_contains(PySetObject *so, PyObject *key)
1742{
1743 long result;
1744
1745 result = set_contains(so, key);
1746 if (result == -1)
1747 return NULL;
1748 return PyBool_FromLong(result);
1749}
1750
1751PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1752
Raymond Hettingera690a992003-11-16 16:17:49 +00001753static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001754set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001755{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001756 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001757 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001758
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001759 rv = set_discard_key(so, key);
1760 if (rv == -1) {
1761 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1762 return NULL;
1763 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001764 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1765 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001766 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001767 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001768 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001769 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001770 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001771 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001772 } else if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001773 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001774 return NULL;
1775 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001776 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001777}
1778
1779PyDoc_STRVAR(remove_doc,
1780"Remove an element from a set; it must be a member.\n\
1781\n\
1782If the element is not a member, raise a KeyError.");
1783
1784static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001785set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001786{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001787 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001788 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001789
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001790 rv = set_discard_key(so, key);
1791 if (rv == -1) {
1792 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1793 return NULL;
1794 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001795 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1796 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001797 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001798 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001799 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001800 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001801 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001802 return result;
1803 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001804 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001805}
1806
1807PyDoc_STRVAR(discard_doc,
1808"Remove an element from a set if it is a member.\n\
1809\n\
1810If the element is not a member, do nothing.");
1811
1812static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001813set_reduce(PySetObject *so)
1814{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001815 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001816
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001817 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001818 if (keys == NULL)
1819 goto done;
1820 args = PyTuple_Pack(1, keys);
1821 if (args == NULL)
1822 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001823 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1824 if (dict == NULL) {
1825 PyErr_Clear();
1826 dict = Py_None;
1827 Py_INCREF(dict);
1828 }
1829 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001830done:
1831 Py_XDECREF(args);
1832 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001833 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001834 return result;
1835}
1836
1837PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1838
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001839static int
1840set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1841{
1842 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001843
1844 if (!PyAnySet_Check(self))
1845 return -1;
1846 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1847 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001848 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001849 self->hash = -1;
1850 if (iterable == NULL)
1851 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001852 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001853}
1854
Raymond Hettingera690a992003-11-16 16:17:49 +00001855static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001856 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001857 0, /* sq_concat */
1858 0, /* sq_repeat */
1859 0, /* sq_item */
1860 0, /* sq_slice */
1861 0, /* sq_ass_item */
1862 0, /* sq_ass_slice */
1863 (objobjproc)set_contains, /* sq_contains */
1864};
1865
1866/* set object ********************************************************/
1867
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001868#ifdef Py_DEBUG
1869static PyObject *test_c_api(PySetObject *so);
1870
1871PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1872All is well if assertions don't fail.");
1873#endif
1874
Raymond Hettingera690a992003-11-16 16:17:49 +00001875static PyMethodDef set_methods[] = {
1876 {"add", (PyCFunction)set_add, METH_O,
1877 add_doc},
1878 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1879 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001880 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001881 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001882 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1883 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001884 {"discard", (PyCFunction)set_discard, METH_O,
1885 discard_doc},
1886 {"difference", (PyCFunction)set_difference, METH_O,
1887 difference_doc},
1888 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1889 difference_update_doc},
1890 {"intersection",(PyCFunction)set_intersection, METH_O,
1891 intersection_doc},
1892 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1893 intersection_update_doc},
1894 {"issubset", (PyCFunction)set_issubset, METH_O,
1895 issubset_doc},
1896 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1897 issuperset_doc},
1898 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1899 pop_doc},
1900 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1901 reduce_doc},
1902 {"remove", (PyCFunction)set_remove, METH_O,
1903 remove_doc},
1904 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1905 symmetric_difference_doc},
1906 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1907 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001908#ifdef Py_DEBUG
1909 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1910 test_c_api_doc},
1911#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001912 {"union", (PyCFunction)set_union, METH_O,
1913 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001914 {"update", (PyCFunction)set_update, METH_O,
1915 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001916 {NULL, NULL} /* sentinel */
1917};
1918
1919static PyNumberMethods set_as_number = {
1920 0, /*nb_add*/
1921 (binaryfunc)set_sub, /*nb_subtract*/
1922 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001923 0, /*nb_remainder*/
1924 0, /*nb_divmod*/
1925 0, /*nb_power*/
1926 0, /*nb_negative*/
1927 0, /*nb_positive*/
1928 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001929 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001930 0, /*nb_invert*/
1931 0, /*nb_lshift*/
1932 0, /*nb_rshift*/
1933 (binaryfunc)set_and, /*nb_and*/
1934 (binaryfunc)set_xor, /*nb_xor*/
1935 (binaryfunc)set_or, /*nb_or*/
1936 0, /*nb_coerce*/
1937 0, /*nb_int*/
1938 0, /*nb_long*/
1939 0, /*nb_float*/
1940 0, /*nb_oct*/
1941 0, /*nb_hex*/
1942 0, /*nb_inplace_add*/
1943 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1944 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001945 0, /*nb_inplace_remainder*/
1946 0, /*nb_inplace_power*/
1947 0, /*nb_inplace_lshift*/
1948 0, /*nb_inplace_rshift*/
1949 (binaryfunc)set_iand, /*nb_inplace_and*/
1950 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1951 (binaryfunc)set_ior, /*nb_inplace_or*/
1952};
1953
1954PyDoc_STRVAR(set_doc,
1955"set(iterable) --> set object\n\
1956\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001957Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001958
1959PyTypeObject PySet_Type = {
1960 PyObject_HEAD_INIT(&PyType_Type)
1961 0, /* ob_size */
1962 "set", /* tp_name */
1963 sizeof(PySetObject), /* tp_basicsize */
1964 0, /* tp_itemsize */
1965 /* methods */
1966 (destructor)set_dealloc, /* tp_dealloc */
1967 (printfunc)set_tp_print, /* tp_print */
1968 0, /* tp_getattr */
1969 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001970 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001971 (reprfunc)set_repr, /* tp_repr */
1972 &set_as_number, /* tp_as_number */
1973 &set_as_sequence, /* tp_as_sequence */
1974 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001975 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001976 0, /* tp_call */
1977 0, /* tp_str */
1978 PyObject_GenericGetAttr, /* tp_getattro */
1979 0, /* tp_setattro */
1980 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001981 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001982 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001983 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001984 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001985 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001986 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001987 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001988 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001989 0, /* tp_iternext */
1990 set_methods, /* tp_methods */
1991 0, /* tp_members */
1992 0, /* tp_getset */
1993 0, /* tp_base */
1994 0, /* tp_dict */
1995 0, /* tp_descr_get */
1996 0, /* tp_descr_set */
1997 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001998 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001999 PyType_GenericAlloc, /* tp_alloc */
2000 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002001 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002002};
2003
2004/* frozenset object ********************************************************/
2005
2006
2007static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002008 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002009 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002010 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002011 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002012 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002013 difference_doc},
2014 {"intersection",(PyCFunction)set_intersection, METH_O,
2015 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002016 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002017 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002018 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002019 issuperset_doc},
2020 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2021 reduce_doc},
2022 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2023 symmetric_difference_doc},
2024 {"union", (PyCFunction)set_union, METH_O,
2025 union_doc},
2026 {NULL, NULL} /* sentinel */
2027};
2028
2029static PyNumberMethods frozenset_as_number = {
2030 0, /*nb_add*/
2031 (binaryfunc)set_sub, /*nb_subtract*/
2032 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002033 0, /*nb_remainder*/
2034 0, /*nb_divmod*/
2035 0, /*nb_power*/
2036 0, /*nb_negative*/
2037 0, /*nb_positive*/
2038 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002039 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002040 0, /*nb_invert*/
2041 0, /*nb_lshift*/
2042 0, /*nb_rshift*/
2043 (binaryfunc)set_and, /*nb_and*/
2044 (binaryfunc)set_xor, /*nb_xor*/
2045 (binaryfunc)set_or, /*nb_or*/
2046};
2047
2048PyDoc_STRVAR(frozenset_doc,
2049"frozenset(iterable) --> frozenset object\n\
2050\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002051Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002052
2053PyTypeObject PyFrozenSet_Type = {
2054 PyObject_HEAD_INIT(&PyType_Type)
2055 0, /* ob_size */
2056 "frozenset", /* tp_name */
2057 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002058 0, /* tp_itemsize */
2059 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002060 (destructor)set_dealloc, /* tp_dealloc */
2061 (printfunc)set_tp_print, /* tp_print */
2062 0, /* tp_getattr */
2063 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002064 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002065 (reprfunc)set_repr, /* tp_repr */
2066 &frozenset_as_number, /* tp_as_number */
2067 &set_as_sequence, /* tp_as_sequence */
2068 0, /* tp_as_mapping */
2069 frozenset_hash, /* tp_hash */
2070 0, /* tp_call */
2071 0, /* tp_str */
2072 PyObject_GenericGetAttr, /* tp_getattro */
2073 0, /* tp_setattro */
2074 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002075 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002076 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002077 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002078 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002079 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002080 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002081 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002082 (getiterfunc)set_iter, /* tp_iter */
2083 0, /* tp_iternext */
2084 frozenset_methods, /* tp_methods */
2085 0, /* tp_members */
2086 0, /* tp_getset */
2087 0, /* tp_base */
2088 0, /* tp_dict */
2089 0, /* tp_descr_get */
2090 0, /* tp_descr_set */
2091 0, /* tp_dictoffset */
2092 0, /* tp_init */
2093 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002094 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002095 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002096};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002097
2098
2099/***** C API functions *************************************************/
2100
2101PyObject *
2102PySet_New(PyObject *iterable)
2103{
2104 return make_new_set(&PySet_Type, iterable);
2105}
2106
2107PyObject *
2108PyFrozenSet_New(PyObject *iterable)
2109{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002110 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002111
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002112 if (iterable == NULL)
2113 args = PyTuple_New(0);
2114 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002115 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002116 if (args == NULL)
2117 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002118 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002119 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002120 return result;
2121}
2122
Neal Norwitz8c49c822006-03-04 18:41:19 +00002123Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002124PySet_Size(PyObject *anyset)
2125{
2126 if (!PyAnySet_Check(anyset)) {
2127 PyErr_BadInternalCall();
2128 return -1;
2129 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002130 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002131}
2132
2133int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002134PySet_Clear(PyObject *set)
2135{
2136 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2137 PyErr_BadInternalCall();
2138 return -1;
2139 }
2140 return set_clear_internal((PySetObject *)set);
2141}
2142
2143int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002144PySet_Contains(PyObject *anyset, PyObject *key)
2145{
2146 if (!PyAnySet_Check(anyset)) {
2147 PyErr_BadInternalCall();
2148 return -1;
2149 }
2150 return set_contains_key((PySetObject *)anyset, key);
2151}
2152
2153int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002154PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002155{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002156 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002157 PyErr_BadInternalCall();
2158 return -1;
2159 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002160 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002161}
2162
2163int
2164PySet_Add(PyObject *set, PyObject *key)
2165{
2166 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2167 PyErr_BadInternalCall();
2168 return -1;
2169 }
2170 return set_add_key((PySetObject *)set, key);
2171}
2172
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002173int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002174_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002175{
2176 setentry *entry_ptr;
2177
2178 if (!PyAnySet_Check(set)) {
2179 PyErr_BadInternalCall();
2180 return -1;
2181 }
2182 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2183 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002184 *key = entry_ptr->key;
2185 return 1;
2186}
2187
2188int
2189_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2190{
2191 setentry *entry;
2192
2193 if (!PyAnySet_Check(set)) {
2194 PyErr_BadInternalCall();
2195 return -1;
2196 }
2197 if (set_next((PySetObject *)set, pos, &entry) == 0)
2198 return 0;
2199 *key = entry->key;
2200 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002201 return 1;
2202}
2203
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002204PyObject *
2205PySet_Pop(PyObject *set)
2206{
2207 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2208 PyErr_BadInternalCall();
2209 return NULL;
2210 }
2211 return set_pop((PySetObject *)set);
2212}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002213
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002214int
2215_PySet_Update(PyObject *set, PyObject *iterable)
2216{
2217 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2218 PyErr_BadInternalCall();
2219 return -1;
2220 }
2221 return set_update_internal((PySetObject *)set, iterable);
2222}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002223
2224#ifdef Py_DEBUG
2225
2226/* Test code to be called with any three element set.
2227 Returns True and original set is restored. */
2228
2229#define assertRaises(call_return_value, exception) \
2230 do { \
2231 assert(call_return_value); \
2232 assert(PyErr_ExceptionMatches(exception)); \
2233 PyErr_Clear(); \
2234 } while(0)
2235
2236static PyObject *
2237test_c_api(PySetObject *so)
2238{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002239 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002240 char *s;
2241 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002242 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002243 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002244
2245 /* Verify preconditions and exercise type/size checks */
2246 assert(PyAnySet_Check(ob));
2247 assert(PyAnySet_CheckExact(ob));
2248 assert(!PyFrozenSet_CheckExact(ob));
2249 assert(PySet_Size(ob) == 3);
2250 assert(PySet_GET_SIZE(ob) == 3);
2251
2252 /* Raise TypeError for non-iterable constructor arguments */
2253 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2254 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2255
2256 /* Raise TypeError for unhashable key */
2257 dup = PySet_New(ob);
2258 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2259 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2260 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2261
2262 /* Exercise successful pop, contains, add, and discard */
2263 elem = PySet_Pop(ob);
2264 assert(PySet_Contains(ob, elem) == 0);
2265 assert(PySet_GET_SIZE(ob) == 2);
2266 assert(PySet_Add(ob, elem) == 0);
2267 assert(PySet_Contains(ob, elem) == 1);
2268 assert(PySet_GET_SIZE(ob) == 3);
2269 assert(PySet_Discard(ob, elem) == 1);
2270 assert(PySet_GET_SIZE(ob) == 2);
2271 assert(PySet_Discard(ob, elem) == 0);
2272 assert(PySet_GET_SIZE(ob) == 2);
2273
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002274 /* Exercise clear */
2275 dup2 = PySet_New(dup);
2276 assert(PySet_Clear(dup2) == 0);
2277 assert(PySet_Size(dup2) == 0);
2278 Py_DECREF(dup2);
2279
2280 /* Raise SystemError on clear or update of frozen set */
2281 f = PyFrozenSet_New(dup);
2282 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2283 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2284 Py_DECREF(f);
2285
2286 /* Exercise direct iteration */
2287 i = 0, count = 0;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002288 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2289 s = PyString_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002290 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2291 count++;
2292 }
2293 assert(count == 3);
2294
2295 /* Exercise updates */
2296 dup2 = PySet_New(NULL);
2297 assert(_PySet_Update(dup2, dup) == 0);
2298 assert(PySet_Size(dup2) == 3);
2299 assert(_PySet_Update(dup2, dup) == 0);
2300 assert(PySet_Size(dup2) == 3);
2301 Py_DECREF(dup2);
2302
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002303 /* Raise SystemError when self argument is not a set or frozenset. */
2304 t = PyTuple_New(0);
2305 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2306 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2307 Py_DECREF(t);
2308
2309 /* Raise SystemError when self argument is not a set. */
2310 f = PyFrozenSet_New(dup);
2311 assert(PySet_Size(f) == 3);
2312 assert(PyFrozenSet_CheckExact(f));
2313 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2314 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2315 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2316 Py_DECREF(f);
2317
2318 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002319 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2320 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002321 assert(PySet_GET_SIZE(ob) == 0);
2322 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2323
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002324 /* Restore the set from the copy using the PyNumber API */
2325 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2326 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002327
2328 /* Verify constructors accept NULL arguments */
2329 f = PySet_New(NULL);
2330 assert(f != NULL);
2331 assert(PySet_GET_SIZE(f) == 0);
2332 Py_DECREF(f);
2333 f = PyFrozenSet_New(NULL);
2334 assert(f != NULL);
2335 assert(PyFrozenSet_CheckExact(f));
2336 assert(PySet_GET_SIZE(f) == 0);
2337 Py_DECREF(f);
2338
2339 Py_DECREF(elem);
2340 Py_DECREF(dup);
2341 Py_RETURN_TRUE;
2342}
2343
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002344#undef assertRaises
2345
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002346#endif