blob: 14d9155481a31fa76df3144f9772867917d8f21f [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00006 Copyright (c) 2003-2007 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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000564 Py_Type(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000565 Py_TRASHCAN_SAFE_END(so)
566}
567
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568static PyObject *
569set_repr(PySetObject *so)
570{
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000571 PyObject *keys, *result=NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000572 Py_UNICODE *u;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000573 int status = Py_ReprEnter((PyObject*)so);
574
575 if (status != 0) {
576 if (status < 0)
577 return NULL;
Martin v. Löwis5d7428b2007-07-21 18:47:48 +0000578 return PyUnicode_FromFormat("%s(...)", Py_Type(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000579 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000580
Georg Brandlc4996ba2006-08-28 19:37:11 +0000581 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000582 if (!so->used) {
583 Py_ReprLeave((PyObject*)so);
Martin v. Löwis5d7428b2007-07-21 18:47:48 +0000584 return PyUnicode_FromFormat("%s()", Py_Type(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000585 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000586
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000587 keys = PySequence_List((PyObject *)so);
588 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000589 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000590
Martin v. Löwis5d7428b2007-07-21 18:47:48 +0000591 if (Py_Type(so) != &PySet_Type) {
592 result = PyUnicode_FromFormat("%s(%R)", Py_Type(so)->tp_name, keys);
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000593 Py_DECREF(keys);
594 }
595 else {
596 PyObject *listrepr = PyObject_Repr(keys);
597 Py_ssize_t newsize;
598 Py_DECREF(keys);
599 if (listrepr == NULL) {
600 Py_DECREF(keys);
601 goto done;
602 }
603 newsize = PyUnicode_GET_SIZE(listrepr);
604 result = PyUnicode_FromUnicode(NULL, newsize);
605 if (result) {
606 u = PyUnicode_AS_UNICODE(result);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000607 *u++ = '{';
608 /* Omit the brackets from the listrepr */
609 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000610 PyUnicode_GET_SIZE(listrepr)-2);
611 u += newsize-2;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000612 *u++ = '}';
613 }
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000614 Py_DECREF(listrepr);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000615 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000616done:
617 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000618 return result;
619}
620
Martin v. Löwis18e16552006-02-15 17:27:45 +0000621static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000622set_len(PyObject *so)
623{
624 return ((PySetObject *)so)->used;
625}
626
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000627static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000628set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000629{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000630 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000632 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000633
634 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000635 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000636
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000637 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000638 if (other == so || other->used == 0)
639 /* a.update(a) or a.update({}); nothing to do */
640 return 0;
641 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000642 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000643 * that there will be no (or few) overlapping keys.
644 */
645 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
646 if (set_table_resize(so, (so->used + other->used)*2) != 0)
647 return -1;
648 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000649 for (i = 0; i <= other->mask; i++) {
650 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000651 if (entry->key != NULL &&
652 entry->key != dummy) {
653 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000654 if (set_insert_key(so, entry->key, entry->hash) == -1) {
655 Py_DECREF(entry->key);
656 return -1;
657 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000658 }
659 }
660 return 0;
661}
662
663static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000664set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000665{
666 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000667 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668
669 if (!PyString_CheckExact(key) ||
670 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
671 hash = PyObject_Hash(key);
672 if (hash == -1)
673 return -1;
674 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000675 entry = (so->lookup)(so, key, hash);
676 if (entry == NULL)
677 return -1;
678 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000679 return key != NULL && key != dummy;
680}
681
Raymond Hettingerc991db22005-08-11 07:58:45 +0000682static int
683set_contains_entry(PySetObject *so, setentry *entry)
684{
685 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000686 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000687
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000688 lu_entry = (so->lookup)(so, entry->key, entry->hash);
689 if (lu_entry == NULL)
690 return -1;
691 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000692 return key != NULL && key != dummy;
693}
694
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000695static PyObject *
696set_pop(PySetObject *so)
697{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000698 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000699 register setentry *entry;
700 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000701
702 assert (PyAnySet_Check(so));
703 if (so->used == 0) {
704 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
705 return NULL;
706 }
707
708 /* Set entry to "the first" unused or dummy set entry. We abuse
709 * the hash field of slot 0 to hold a search finger:
710 * If slot 0 has a value, use slot 0.
711 * Else slot 0 is being used to hold a search finger,
712 * and we use its hash value as the first index to look.
713 */
714 entry = &so->table[0];
715 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000716 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000717 /* The hash field may be a real hash value, or it may be a
718 * legit search finger, or it may be a once-legit search
719 * finger that's out of bounds now because it wrapped around
720 * or the table shrunk -- simply make sure it's in bounds now.
721 */
722 if (i > so->mask || i < 1)
723 i = 1; /* skip slot 0 */
724 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
725 i++;
726 if (i > so->mask)
727 i = 1;
728 }
729 }
730 key = entry->key;
731 Py_INCREF(dummy);
732 entry->key = dummy;
733 so->used--;
734 so->table[0].hash = i + 1; /* next place to start */
735 return key;
736}
737
738PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
739
740static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000741set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000742{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000743 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000744 setentry *entry;
745
746 while (set_next(so, &pos, &entry))
747 Py_VISIT(entry->key);
748 return 0;
749}
750
751static long
752frozenset_hash(PyObject *self)
753{
754 PySetObject *so = (PySetObject *)self;
755 long h, hash = 1927868237L;
756 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000757 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000758
759 if (so->hash != -1)
760 return so->hash;
761
762 hash *= PySet_GET_SIZE(self) + 1;
763 while (set_next(so, &pos, &entry)) {
764 /* Work to increase the bit dispersion for closely spaced hash
765 values. The is important because some use cases have many
766 combinations of a small number of elements with nearby
767 hashes so that many distinct combinations collapse to only
768 a handful of distinct hash values. */
769 h = entry->hash;
770 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
771 }
772 hash = hash * 69069L + 907133923L;
773 if (hash == -1)
774 hash = 590923713L;
775 so->hash = hash;
776 return hash;
777}
778
Raymond Hettingera9d99362005-08-05 00:01:15 +0000779/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000780
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000781typedef struct {
782 PyObject_HEAD
783 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000784 Py_ssize_t si_used;
785 Py_ssize_t si_pos;
786 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787} setiterobject;
788
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789static void
790setiter_dealloc(setiterobject *si)
791{
792 Py_XDECREF(si->si_set);
793 PyObject_Del(si);
794}
795
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000796static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797setiter_len(setiterobject *si)
798{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000799 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000801 len = si->len;
802 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000803}
804
Armin Rigof5b3e362006-02-11 21:32:43 +0000805PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000806
807static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000808 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000809 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810};
811
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000812static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813{
814 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000815 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000816 register setentry *entry;
817 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000819 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000820 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000821 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000823 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824 PyErr_SetString(PyExc_RuntimeError,
825 "Set changed size during iteration");
826 si->si_used = -1; /* Make this state sticky */
827 return NULL;
828 }
829
830 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000831 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000832 entry = so->table;
833 mask = so->mask;
834 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835 i++;
836 si->si_pos = i+1;
837 if (i > mask)
838 goto fail;
839 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000840 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000841 Py_INCREF(key);
842 return key;
843
844fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000845 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000846 si->si_set = NULL;
847 return NULL;
848}
849
Hye-Shik Change2956762005-08-01 05:26:41 +0000850static PyTypeObject PySetIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000851 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000852 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853 sizeof(setiterobject), /* tp_basicsize */
854 0, /* tp_itemsize */
855 /* methods */
856 (destructor)setiter_dealloc, /* tp_dealloc */
857 0, /* tp_print */
858 0, /* tp_getattr */
859 0, /* tp_setattr */
860 0, /* tp_compare */
861 0, /* tp_repr */
862 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000863 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000864 0, /* tp_as_mapping */
865 0, /* tp_hash */
866 0, /* tp_call */
867 0, /* tp_str */
868 PyObject_GenericGetAttr, /* tp_getattro */
869 0, /* tp_setattro */
870 0, /* tp_as_buffer */
871 Py_TPFLAGS_DEFAULT, /* tp_flags */
872 0, /* tp_doc */
873 0, /* tp_traverse */
874 0, /* tp_clear */
875 0, /* tp_richcompare */
876 0, /* tp_weaklistoffset */
877 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000878 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000879 setiter_methods, /* tp_methods */
880 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000881};
882
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883static PyObject *
884set_iter(PySetObject *so)
885{
886 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
887 if (si == NULL)
888 return NULL;
889 Py_INCREF(so);
890 si->si_set = so;
891 si->si_used = so->used;
892 si->si_pos = 0;
893 si->len = so->used;
894 return (PyObject *)si;
895}
896
Raymond Hettingerd7946662005-08-01 21:39:29 +0000897static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000898set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000899{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000900 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000901
Thomas Wouterscf297e42007-02-23 15:07:44 +0000902 if (PyAnySet_CheckExact(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000903 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000904
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000905 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000906 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000907 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000908 long hash;
909 Py_ssize_t dictsize = PyDict_Size(other);
910
911 /* Do one big resize at the start, rather than
912 * incrementally resizing as we insert new keys. Expect
913 * that there will be no (or few) overlapping keys.
914 */
915 if (dictsize == -1)
916 return -1;
917 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
918 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
919 return -1;
920 }
921 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
922 setentry an_entry;
923
924 an_entry.hash = hash;
925 an_entry.key = key;
926 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000927 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000928 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000929 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000930 }
931
Raymond Hettingera38123e2003-11-24 22:18:49 +0000932 it = PyObject_GetIter(other);
933 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000934 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000935
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000936 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000937 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000938 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000939 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000940 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000941 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000942 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000943 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000944 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000945 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000946 return -1;
947 return 0;
948}
949
950static PyObject *
951set_update(PySetObject *so, PyObject *other)
952{
953 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000954 return NULL;
955 Py_RETURN_NONE;
956}
957
958PyDoc_STRVAR(update_doc,
959"Update a set with the union of itself and another.");
960
961static PyObject *
962make_new_set(PyTypeObject *type, PyObject *iterable)
963{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000964 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000965
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000966 if (dummy == NULL) { /* Auto-initialize dummy */
967 dummy = PyString_FromString("<dummy key>");
968 if (dummy == NULL)
969 return NULL;
970 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000971
972 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000973 if (num_free_sets &&
974 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
975 so = free_sets[--num_free_sets];
976 assert (so != NULL && PyAnySet_CheckExact(so));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000977 Py_Type(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000978 _Py_NewReference((PyObject *)so);
979 EMPTY_TO_MINSIZE(so);
980 PyObject_GC_Track(so);
981 } else {
982 so = (PySetObject *)type->tp_alloc(type, 0);
983 if (so == NULL)
984 return NULL;
985 /* tp_alloc has already zeroed the structure */
986 assert(so->table == NULL && so->fill == 0 && so->used == 0);
987 INIT_NONZERO_SET_SLOTS(so);
988 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000989
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000990 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000991 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000992
Raymond Hettingera38123e2003-11-24 22:18:49 +0000993 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000994 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000995 Py_DECREF(so);
996 return NULL;
997 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000998 }
999
Raymond Hettingera690a992003-11-16 16:17:49 +00001000 return (PyObject *)so;
1001}
1002
Raymond Hettingerd7946662005-08-01 21:39:29 +00001003/* The empty frozenset is a singleton */
1004static PyObject *emptyfrozenset = NULL;
1005
Raymond Hettingera690a992003-11-16 16:17:49 +00001006static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001007frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001008{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001009 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001010
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001011 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001012 return NULL;
1013
Raymond Hettingera690a992003-11-16 16:17:49 +00001014 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1015 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001016
1017 if (type != &PyFrozenSet_Type)
1018 return make_new_set(type, iterable);
1019
1020 if (iterable != NULL) {
1021 /* frozenset(f) is idempotent */
1022 if (PyFrozenSet_CheckExact(iterable)) {
1023 Py_INCREF(iterable);
1024 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001025 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001026 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001027 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001028 return result;
1029 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001030 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001031 /* The empty frozenset is a singleton */
1032 if (emptyfrozenset == NULL)
1033 emptyfrozenset = make_new_set(type, NULL);
1034 Py_XINCREF(emptyfrozenset);
1035 return emptyfrozenset;
1036}
1037
1038void
1039PySet_Fini(void)
1040{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001041 PySetObject *so;
1042
1043 while (num_free_sets) {
1044 num_free_sets--;
1045 so = free_sets[num_free_sets];
1046 PyObject_GC_Del(so);
1047 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001048 Py_CLEAR(dummy);
1049 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001050}
1051
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001052static PyObject *
1053set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1054{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001055 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001056 return NULL;
1057
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001058 return make_new_set(type, NULL);
1059}
1060
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001061/* set_swap_bodies() switches the contents of any two sets by moving their
1062 internal data pointers and, if needed, copying the internal smalltables.
1063 Semantically equivalent to:
1064
1065 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1066
1067 The function always succeeds and it leaves both objects in a stable state.
1068 Useful for creating temporary frozensets from sets for membership testing
1069 in __contains__(), discard(), and remove(). Also useful for operations
1070 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001071 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001072*/
1073
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001074static void
1075set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001076{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001077 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001078 setentry *u;
1079 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1080 setentry tab[PySet_MINSIZE];
1081 long h;
1082
1083 t = a->fill; a->fill = b->fill; b->fill = t;
1084 t = a->used; a->used = b->used; b->used = t;
1085 t = a->mask; a->mask = b->mask; b->mask = t;
1086
1087 u = a->table;
1088 if (a->table == a->smalltable)
1089 u = b->smalltable;
1090 a->table = b->table;
1091 if (b->table == b->smalltable)
1092 a->table = a->smalltable;
1093 b->table = u;
1094
1095 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1096
1097 if (a->table == a->smalltable || b->table == b->smalltable) {
1098 memcpy(tab, a->smalltable, sizeof(tab));
1099 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1100 memcpy(b->smalltable, tab, sizeof(tab));
1101 }
1102
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001103 if (PyType_IsSubtype(Py_Type(a), &PyFrozenSet_Type) &&
1104 PyType_IsSubtype(Py_Type(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001105 h = a->hash; a->hash = b->hash; b->hash = h;
1106 } else {
1107 a->hash = -1;
1108 b->hash = -1;
1109 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001110}
1111
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001112static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001113set_copy(PySetObject *so)
1114{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001115 return make_new_set(Py_Type(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001116}
1117
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001118static PyObject *
1119frozenset_copy(PySetObject *so)
1120{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001121 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001122 Py_INCREF(so);
1123 return (PyObject *)so;
1124 }
1125 return set_copy(so);
1126}
1127
Raymond Hettingera690a992003-11-16 16:17:49 +00001128PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1129
1130static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001131set_clear(PySetObject *so)
1132{
1133 set_clear_internal(so);
1134 Py_RETURN_NONE;
1135}
1136
1137PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1138
1139static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001140set_union(PySetObject *so, PyObject *other)
1141{
1142 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001143
1144 result = (PySetObject *)set_copy(so);
1145 if (result == NULL)
1146 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001147 if ((PyObject *)so == other)
1148 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001149 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001150 Py_DECREF(result);
1151 return NULL;
1152 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001153 return (PyObject *)result;
1154}
1155
1156PyDoc_STRVAR(union_doc,
1157 "Return the union of two sets as a new set.\n\
1158\n\
1159(i.e. all elements that are in either set.)");
1160
1161static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001162set_or(PySetObject *so, PyObject *other)
1163{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001164 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001165 Py_INCREF(Py_NotImplemented);
1166 return Py_NotImplemented;
1167 }
1168 return set_union(so, other);
1169}
1170
1171static PyObject *
1172set_ior(PySetObject *so, PyObject *other)
1173{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001174 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001175 Py_INCREF(Py_NotImplemented);
1176 return Py_NotImplemented;
1177 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001178 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001179 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001180 Py_INCREF(so);
1181 return (PyObject *)so;
1182}
1183
1184static PyObject *
1185set_intersection(PySetObject *so, PyObject *other)
1186{
1187 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001188 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001189
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001190 if ((PyObject *)so == other)
1191 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001192
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001193 result = (PySetObject *)make_new_set(Py_Type(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001194 if (result == NULL)
1195 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001196
Thomas Wouterscf297e42007-02-23 15:07:44 +00001197 if (PyAnySet_CheckExact(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001198 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001199 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001200
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001201 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001202 tmp = (PyObject *)so;
1203 so = (PySetObject *)other;
1204 other = tmp;
1205 }
1206
Raymond Hettingerc991db22005-08-11 07:58:45 +00001207 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001208 int rv = set_contains_entry(so, entry);
1209 if (rv == -1) {
1210 Py_DECREF(result);
1211 return NULL;
1212 }
1213 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001214 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001215 Py_DECREF(result);
1216 return NULL;
1217 }
1218 }
1219 }
1220 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001221 }
1222
Raymond Hettingera690a992003-11-16 16:17:49 +00001223 it = PyObject_GetIter(other);
1224 if (it == NULL) {
1225 Py_DECREF(result);
1226 return NULL;
1227 }
1228
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001229 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001230 int rv;
1231 setentry entry;
1232 long hash = PyObject_Hash(key);
1233
1234 if (hash == -1) {
1235 Py_DECREF(it);
1236 Py_DECREF(result);
1237 Py_DECREF(key);
1238 return NULL;
1239 }
1240 entry.hash = hash;
1241 entry.key = key;
1242 rv = set_contains_entry(so, &entry);
1243 if (rv == -1) {
1244 Py_DECREF(it);
1245 Py_DECREF(result);
1246 Py_DECREF(key);
1247 return NULL;
1248 }
1249 if (rv) {
1250 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001251 Py_DECREF(it);
1252 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001253 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001254 return NULL;
1255 }
1256 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001257 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001258 }
1259 Py_DECREF(it);
1260 if (PyErr_Occurred()) {
1261 Py_DECREF(result);
1262 return NULL;
1263 }
1264 return (PyObject *)result;
1265}
1266
1267PyDoc_STRVAR(intersection_doc,
1268"Return the intersection of two sets as a new set.\n\
1269\n\
1270(i.e. all elements that are in both sets.)");
1271
1272static PyObject *
1273set_intersection_update(PySetObject *so, PyObject *other)
1274{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001275 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001276
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001277 tmp = set_intersection(so, other);
1278 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001279 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001280 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001281 Py_DECREF(tmp);
1282 Py_RETURN_NONE;
1283}
1284
1285PyDoc_STRVAR(intersection_update_doc,
1286"Update a set with the intersection of itself and another.");
1287
1288static PyObject *
1289set_and(PySetObject *so, PyObject *other)
1290{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001291 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001292 Py_INCREF(Py_NotImplemented);
1293 return Py_NotImplemented;
1294 }
1295 return set_intersection(so, other);
1296}
1297
1298static PyObject *
1299set_iand(PySetObject *so, PyObject *other)
1300{
1301 PyObject *result;
1302
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001303 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001304 Py_INCREF(Py_NotImplemented);
1305 return Py_NotImplemented;
1306 }
1307 result = set_intersection_update(so, other);
1308 if (result == NULL)
1309 return NULL;
1310 Py_DECREF(result);
1311 Py_INCREF(so);
1312 return (PyObject *)so;
1313}
1314
Neal Norwitz6576bd82005-11-13 18:41:28 +00001315static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001316set_difference_update_internal(PySetObject *so, PyObject *other)
1317{
1318 if ((PyObject *)so == other)
1319 return set_clear_internal(so);
1320
Thomas Wouterscf297e42007-02-23 15:07:44 +00001321 if (PyAnySet_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001322 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001323 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001324
1325 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001326 if (set_discard_entry(so, entry) == -1)
1327 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001328 } else {
1329 PyObject *key, *it;
1330 it = PyObject_GetIter(other);
1331 if (it == NULL)
1332 return -1;
1333
1334 while ((key = PyIter_Next(it)) != NULL) {
1335 if (set_discard_key(so, key) == -1) {
1336 Py_DECREF(it);
1337 Py_DECREF(key);
1338 return -1;
1339 }
1340 Py_DECREF(key);
1341 }
1342 Py_DECREF(it);
1343 if (PyErr_Occurred())
1344 return -1;
1345 }
1346 /* If more than 1/5 are dummies, then resize them away. */
1347 if ((so->fill - so->used) * 5 < so->mask)
1348 return 0;
1349 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1350}
1351
Raymond Hettingera690a992003-11-16 16:17:49 +00001352static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001353set_difference_update(PySetObject *so, PyObject *other)
1354{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001355 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001356 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001357 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001358}
1359
1360PyDoc_STRVAR(difference_update_doc,
1361"Remove all elements of another set from this set.");
1362
1363static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001364set_difference(PySetObject *so, PyObject *other)
1365{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001366 PyObject *result;
1367 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001368 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001369
Thomas Wouterscf297e42007-02-23 15:07:44 +00001370 if (!PyAnySet_CheckExact(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001371 result = set_copy(so);
1372 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001373 return NULL;
1374 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001375 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001376 Py_DECREF(result);
1377 return NULL;
1378 }
1379
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001380 result = make_new_set(Py_Type(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001381 if (result == NULL)
1382 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001383
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001384 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001385 while (set_next(so, &pos, &entry)) {
1386 setentry entrycopy;
1387 entrycopy.hash = entry->hash;
1388 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001389 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001390 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1391 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001392 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001393 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001394 }
1395 }
1396 return result;
1397 }
1398
Raymond Hettingerc991db22005-08-11 07:58:45 +00001399 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001400 int rv = set_contains_entry((PySetObject *)other, entry);
1401 if (rv == -1) {
1402 Py_DECREF(result);
1403 return NULL;
1404 }
1405 if (!rv) {
1406 if (set_add_entry((PySetObject *)result, entry) == -1) {
1407 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001408 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001409 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001410 }
1411 }
1412 return result;
1413}
1414
1415PyDoc_STRVAR(difference_doc,
1416"Return the difference of two sets as a new set.\n\
1417\n\
1418(i.e. all elements that are in this set but not the other.)");
1419static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001420set_sub(PySetObject *so, PyObject *other)
1421{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001422 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001423 Py_INCREF(Py_NotImplemented);
1424 return Py_NotImplemented;
1425 }
1426 return set_difference(so, other);
1427}
1428
1429static PyObject *
1430set_isub(PySetObject *so, PyObject *other)
1431{
1432 PyObject *result;
1433
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001434 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001435 Py_INCREF(Py_NotImplemented);
1436 return Py_NotImplemented;
1437 }
1438 result = set_difference_update(so, other);
1439 if (result == NULL)
1440 return NULL;
1441 Py_DECREF(result);
1442 Py_INCREF(so);
1443 return (PyObject *)so;
1444}
1445
1446static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001447set_symmetric_difference_update(PySetObject *so, PyObject *other)
1448{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001449 PySetObject *otherset;
1450 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001451 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001452 setentry *entry;
1453
1454 if ((PyObject *)so == other)
1455 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001456
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001457 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001458 PyObject *value;
1459 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001460 long hash;
1461 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001462 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001463
Thomas Wouters89f507f2006-12-13 04:49:30 +00001464 an_entry.hash = hash;
1465 an_entry.key = key;
1466 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001467 if (rv == -1)
1468 return NULL;
1469 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001470 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001471 return NULL;
1472 }
1473 }
1474 Py_RETURN_NONE;
1475 }
1476
Thomas Wouterscf297e42007-02-23 15:07:44 +00001477 if (PyAnySet_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001478 Py_INCREF(other);
1479 otherset = (PySetObject *)other;
1480 } else {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001481 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001482 if (otherset == NULL)
1483 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001484 }
1485
Raymond Hettingerc991db22005-08-11 07:58:45 +00001486 while (set_next(otherset, &pos, &entry)) {
1487 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001488 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001489 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001490 return NULL;
1491 }
1492 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001493 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001494 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001495 return NULL;
1496 }
1497 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001498 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001499 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001500 Py_RETURN_NONE;
1501}
1502
1503PyDoc_STRVAR(symmetric_difference_update_doc,
1504"Update a set with the symmetric difference of itself and another.");
1505
1506static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001507set_symmetric_difference(PySetObject *so, PyObject *other)
1508{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001509 PyObject *rv;
1510 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001511
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001512 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001513 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001514 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001515 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1516 if (rv == NULL)
1517 return NULL;
1518 Py_DECREF(rv);
1519 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001520}
1521
1522PyDoc_STRVAR(symmetric_difference_doc,
1523"Return the symmetric difference of two sets as a new set.\n\
1524\n\
1525(i.e. all elements that are in exactly one of the sets.)");
1526
1527static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001528set_xor(PySetObject *so, PyObject *other)
1529{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001530 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001531 Py_INCREF(Py_NotImplemented);
1532 return Py_NotImplemented;
1533 }
1534 return set_symmetric_difference(so, other);
1535}
1536
1537static PyObject *
1538set_ixor(PySetObject *so, PyObject *other)
1539{
1540 PyObject *result;
1541
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001542 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001543 Py_INCREF(Py_NotImplemented);
1544 return Py_NotImplemented;
1545 }
1546 result = set_symmetric_difference_update(so, other);
1547 if (result == NULL)
1548 return NULL;
1549 Py_DECREF(result);
1550 Py_INCREF(so);
1551 return (PyObject *)so;
1552}
1553
1554static PyObject *
1555set_issubset(PySetObject *so, PyObject *other)
1556{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001557 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001558 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001559
Thomas Wouterscf297e42007-02-23 15:07:44 +00001560 if (!PyAnySet_CheckExact(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001561 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001562 tmp = make_new_set(&PySet_Type, other);
1563 if (tmp == NULL)
1564 return NULL;
1565 result = set_issubset(so, tmp);
1566 Py_DECREF(tmp);
1567 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001568 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001569 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001570 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001571
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001572 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001573 int rv = set_contains_entry((PySetObject *)other, entry);
1574 if (rv == -1)
1575 return NULL;
1576 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001577 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001578 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001579 Py_RETURN_TRUE;
1580}
1581
1582PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1583
1584static PyObject *
1585set_issuperset(PySetObject *so, PyObject *other)
1586{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001587 PyObject *tmp, *result;
1588
Thomas Wouterscf297e42007-02-23 15:07:44 +00001589 if (!PyAnySet_CheckExact(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001590 tmp = make_new_set(&PySet_Type, other);
1591 if (tmp == NULL)
1592 return NULL;
1593 result = set_issuperset(so, tmp);
1594 Py_DECREF(tmp);
1595 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001596 }
1597 return set_issubset((PySetObject *)other, (PyObject *)so);
1598}
1599
1600PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1601
Raymond Hettingera690a992003-11-16 16:17:49 +00001602static PyObject *
1603set_richcompare(PySetObject *v, PyObject *w, int op)
1604{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001605 PyObject *r1, *r2;
1606
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001607 if(!PyAnySet_Check(w)) {
1608 if (op == Py_EQ)
1609 Py_RETURN_FALSE;
1610 if (op == Py_NE)
1611 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001612 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1613 return NULL;
1614 }
1615 switch (op) {
1616 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001617 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001618 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001619 if (v->hash != -1 &&
1620 ((PySetObject *)w)->hash != -1 &&
1621 v->hash != ((PySetObject *)w)->hash)
1622 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001623 return set_issubset(v, w);
1624 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001625 r1 = set_richcompare(v, w, Py_EQ);
1626 if (r1 == NULL)
1627 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001628 r2 = PyBool_FromLong(PyObject_Not(r1));
1629 Py_DECREF(r1);
1630 return r2;
1631 case Py_LE:
1632 return set_issubset(v, w);
1633 case Py_GE:
1634 return set_issuperset(v, w);
1635 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001636 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001637 Py_RETURN_FALSE;
1638 return set_issubset(v, w);
1639 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001640 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001641 Py_RETURN_FALSE;
1642 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001643 }
1644 Py_INCREF(Py_NotImplemented);
1645 return Py_NotImplemented;
1646}
1647
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001648static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001649set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001650{
1651 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1652 return -1;
1653}
1654
Raymond Hettingera690a992003-11-16 16:17:49 +00001655static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001656set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001657{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001658 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001659 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001660 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001661}
1662
1663PyDoc_STRVAR(add_doc,
1664"Add an element to a set.\n\
1665\n\
1666This has no effect if the element is already present.");
1667
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001668static int
1669set_contains(PySetObject *so, PyObject *key)
1670{
1671 PyObject *tmpkey;
1672 int rv;
1673
1674 rv = set_contains_key(so, key);
1675 if (rv == -1) {
1676 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1677 return -1;
1678 PyErr_Clear();
1679 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1680 if (tmpkey == NULL)
1681 return -1;
1682 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1683 rv = set_contains(so, tmpkey);
1684 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1685 Py_DECREF(tmpkey);
1686 }
1687 return rv;
1688}
1689
1690static PyObject *
1691set_direct_contains(PySetObject *so, PyObject *key)
1692{
1693 long result;
1694
1695 result = set_contains(so, key);
1696 if (result == -1)
1697 return NULL;
1698 return PyBool_FromLong(result);
1699}
1700
1701PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1702
Raymond Hettingera690a992003-11-16 16:17:49 +00001703static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001704set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001705{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001706 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001707 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001708
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001709 rv = set_discard_key(so, key);
1710 if (rv == -1) {
1711 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1712 return NULL;
1713 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001714 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1715 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001716 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001717 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001718 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001719 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001720 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001721 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001722 } else if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001723 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001724 return NULL;
1725 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001726 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001727}
1728
1729PyDoc_STRVAR(remove_doc,
1730"Remove an element from a set; it must be a member.\n\
1731\n\
1732If the element is not a member, raise a KeyError.");
1733
1734static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001735set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001736{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001737 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001738 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001739
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001740 rv = set_discard_key(so, key);
1741 if (rv == -1) {
1742 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1743 return NULL;
1744 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001745 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1746 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001747 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001748 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001749 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001750 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001751 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001752 return result;
1753 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001754 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001755}
1756
1757PyDoc_STRVAR(discard_doc,
1758"Remove an element from a set if it is a member.\n\
1759\n\
1760If the element is not a member, do nothing.");
1761
1762static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001763set_reduce(PySetObject *so)
1764{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001765 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001766
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001767 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001768 if (keys == NULL)
1769 goto done;
1770 args = PyTuple_Pack(1, keys);
1771 if (args == NULL)
1772 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001773 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1774 if (dict == NULL) {
1775 PyErr_Clear();
1776 dict = Py_None;
1777 Py_INCREF(dict);
1778 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001779 result = PyTuple_Pack(3, Py_Type(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001780done:
1781 Py_XDECREF(args);
1782 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001783 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001784 return result;
1785}
1786
1787PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1788
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001789static int
1790set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1791{
1792 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001793
1794 if (!PyAnySet_Check(self))
1795 return -1;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001796 if (!PyArg_UnpackTuple(args, Py_Type(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001797 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001798 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001799 self->hash = -1;
1800 if (iterable == NULL)
1801 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001802 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001803}
1804
Raymond Hettingera690a992003-11-16 16:17:49 +00001805static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001806 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001807 0, /* sq_concat */
1808 0, /* sq_repeat */
1809 0, /* sq_item */
1810 0, /* sq_slice */
1811 0, /* sq_ass_item */
1812 0, /* sq_ass_slice */
1813 (objobjproc)set_contains, /* sq_contains */
1814};
1815
1816/* set object ********************************************************/
1817
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001818#ifdef Py_DEBUG
1819static PyObject *test_c_api(PySetObject *so);
1820
1821PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1822All is well if assertions don't fail.");
1823#endif
1824
Raymond Hettingera690a992003-11-16 16:17:49 +00001825static PyMethodDef set_methods[] = {
1826 {"add", (PyCFunction)set_add, METH_O,
1827 add_doc},
1828 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1829 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001830 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001831 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001832 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1833 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001834 {"discard", (PyCFunction)set_discard, METH_O,
1835 discard_doc},
1836 {"difference", (PyCFunction)set_difference, METH_O,
1837 difference_doc},
1838 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1839 difference_update_doc},
1840 {"intersection",(PyCFunction)set_intersection, METH_O,
1841 intersection_doc},
1842 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1843 intersection_update_doc},
1844 {"issubset", (PyCFunction)set_issubset, METH_O,
1845 issubset_doc},
1846 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1847 issuperset_doc},
1848 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1849 pop_doc},
1850 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1851 reduce_doc},
1852 {"remove", (PyCFunction)set_remove, METH_O,
1853 remove_doc},
1854 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1855 symmetric_difference_doc},
1856 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1857 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001858#ifdef Py_DEBUG
1859 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1860 test_c_api_doc},
1861#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001862 {"union", (PyCFunction)set_union, METH_O,
1863 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001864 {"update", (PyCFunction)set_update, METH_O,
1865 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001866 {NULL, NULL} /* sentinel */
1867};
1868
1869static PyNumberMethods set_as_number = {
1870 0, /*nb_add*/
1871 (binaryfunc)set_sub, /*nb_subtract*/
1872 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001873 0, /*nb_remainder*/
1874 0, /*nb_divmod*/
1875 0, /*nb_power*/
1876 0, /*nb_negative*/
1877 0, /*nb_positive*/
1878 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001879 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001880 0, /*nb_invert*/
1881 0, /*nb_lshift*/
1882 0, /*nb_rshift*/
1883 (binaryfunc)set_and, /*nb_and*/
1884 (binaryfunc)set_xor, /*nb_xor*/
1885 (binaryfunc)set_or, /*nb_or*/
1886 0, /*nb_coerce*/
1887 0, /*nb_int*/
1888 0, /*nb_long*/
1889 0, /*nb_float*/
1890 0, /*nb_oct*/
1891 0, /*nb_hex*/
1892 0, /*nb_inplace_add*/
1893 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1894 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001895 0, /*nb_inplace_remainder*/
1896 0, /*nb_inplace_power*/
1897 0, /*nb_inplace_lshift*/
1898 0, /*nb_inplace_rshift*/
1899 (binaryfunc)set_iand, /*nb_inplace_and*/
1900 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1901 (binaryfunc)set_ior, /*nb_inplace_or*/
1902};
1903
1904PyDoc_STRVAR(set_doc,
1905"set(iterable) --> set object\n\
1906\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001907Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001908
1909PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001910 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001911 "set", /* tp_name */
1912 sizeof(PySetObject), /* tp_basicsize */
1913 0, /* tp_itemsize */
1914 /* methods */
1915 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001916 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00001917 0, /* tp_getattr */
1918 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001919 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001920 (reprfunc)set_repr, /* tp_repr */
1921 &set_as_number, /* tp_as_number */
1922 &set_as_sequence, /* tp_as_sequence */
1923 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001924 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001925 0, /* tp_call */
1926 0, /* tp_str */
1927 PyObject_GenericGetAttr, /* tp_getattro */
1928 0, /* tp_setattro */
1929 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001930 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001931 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001932 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001933 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001934 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001935 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001936 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001937 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001938 0, /* tp_iternext */
1939 set_methods, /* tp_methods */
1940 0, /* tp_members */
1941 0, /* tp_getset */
1942 0, /* tp_base */
1943 0, /* tp_dict */
1944 0, /* tp_descr_get */
1945 0, /* tp_descr_set */
1946 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001947 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001948 PyType_GenericAlloc, /* tp_alloc */
1949 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001950 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001951};
1952
1953/* frozenset object ********************************************************/
1954
1955
1956static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001957 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001958 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001959 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001960 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001961 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001962 difference_doc},
1963 {"intersection",(PyCFunction)set_intersection, METH_O,
1964 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001965 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001966 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001967 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001968 issuperset_doc},
1969 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1970 reduce_doc},
1971 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1972 symmetric_difference_doc},
1973 {"union", (PyCFunction)set_union, METH_O,
1974 union_doc},
1975 {NULL, NULL} /* sentinel */
1976};
1977
1978static PyNumberMethods frozenset_as_number = {
1979 0, /*nb_add*/
1980 (binaryfunc)set_sub, /*nb_subtract*/
1981 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001982 0, /*nb_remainder*/
1983 0, /*nb_divmod*/
1984 0, /*nb_power*/
1985 0, /*nb_negative*/
1986 0, /*nb_positive*/
1987 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001988 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001989 0, /*nb_invert*/
1990 0, /*nb_lshift*/
1991 0, /*nb_rshift*/
1992 (binaryfunc)set_and, /*nb_and*/
1993 (binaryfunc)set_xor, /*nb_xor*/
1994 (binaryfunc)set_or, /*nb_or*/
1995};
1996
1997PyDoc_STRVAR(frozenset_doc,
1998"frozenset(iterable) --> frozenset object\n\
1999\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002000Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002001
2002PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002003 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002004 "frozenset", /* tp_name */
2005 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002006 0, /* tp_itemsize */
2007 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002008 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002009 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002010 0, /* tp_getattr */
2011 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002012 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002013 (reprfunc)set_repr, /* tp_repr */
2014 &frozenset_as_number, /* tp_as_number */
2015 &set_as_sequence, /* tp_as_sequence */
2016 0, /* tp_as_mapping */
2017 frozenset_hash, /* tp_hash */
2018 0, /* tp_call */
2019 0, /* tp_str */
2020 PyObject_GenericGetAttr, /* tp_getattro */
2021 0, /* tp_setattro */
2022 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002023 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002024 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002025 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002026 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002027 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002028 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002029 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002030 (getiterfunc)set_iter, /* tp_iter */
2031 0, /* tp_iternext */
2032 frozenset_methods, /* tp_methods */
2033 0, /* tp_members */
2034 0, /* tp_getset */
2035 0, /* tp_base */
2036 0, /* tp_dict */
2037 0, /* tp_descr_get */
2038 0, /* tp_descr_set */
2039 0, /* tp_dictoffset */
2040 0, /* tp_init */
2041 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002042 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002043 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002044};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002045
2046
2047/***** C API functions *************************************************/
2048
2049PyObject *
2050PySet_New(PyObject *iterable)
2051{
2052 return make_new_set(&PySet_Type, iterable);
2053}
2054
2055PyObject *
2056PyFrozenSet_New(PyObject *iterable)
2057{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002058 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002059
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002060 if (iterable == NULL)
2061 args = PyTuple_New(0);
2062 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002063 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002064 if (args == NULL)
2065 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002066 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002067 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002068 return result;
2069}
2070
Neal Norwitz8c49c822006-03-04 18:41:19 +00002071Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002072PySet_Size(PyObject *anyset)
2073{
2074 if (!PyAnySet_Check(anyset)) {
2075 PyErr_BadInternalCall();
2076 return -1;
2077 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002078 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002079}
2080
2081int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002082PySet_Clear(PyObject *set)
2083{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002084 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002085 PyErr_BadInternalCall();
2086 return -1;
2087 }
2088 return set_clear_internal((PySetObject *)set);
2089}
2090
2091int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002092PySet_Contains(PyObject *anyset, PyObject *key)
2093{
2094 if (!PyAnySet_Check(anyset)) {
2095 PyErr_BadInternalCall();
2096 return -1;
2097 }
2098 return set_contains_key((PySetObject *)anyset, key);
2099}
2100
2101int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002102PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002103{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002104 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002105 PyErr_BadInternalCall();
2106 return -1;
2107 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002108 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002109}
2110
2111int
2112PySet_Add(PyObject *set, PyObject *key)
2113{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002114 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002115 PyErr_BadInternalCall();
2116 return -1;
2117 }
2118 return set_add_key((PySetObject *)set, key);
2119}
2120
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002121int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002122_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002123{
2124 setentry *entry_ptr;
2125
2126 if (!PyAnySet_Check(set)) {
2127 PyErr_BadInternalCall();
2128 return -1;
2129 }
2130 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2131 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002132 *key = entry_ptr->key;
2133 return 1;
2134}
2135
2136int
2137_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2138{
2139 setentry *entry;
2140
2141 if (!PyAnySet_Check(set)) {
2142 PyErr_BadInternalCall();
2143 return -1;
2144 }
2145 if (set_next((PySetObject *)set, pos, &entry) == 0)
2146 return 0;
2147 *key = entry->key;
2148 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002149 return 1;
2150}
2151
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002152PyObject *
2153PySet_Pop(PyObject *set)
2154{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002155 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002156 PyErr_BadInternalCall();
2157 return NULL;
2158 }
2159 return set_pop((PySetObject *)set);
2160}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002161
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002162int
2163_PySet_Update(PyObject *set, PyObject *iterable)
2164{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002165 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002166 PyErr_BadInternalCall();
2167 return -1;
2168 }
2169 return set_update_internal((PySetObject *)set, iterable);
2170}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002171
2172#ifdef Py_DEBUG
2173
2174/* Test code to be called with any three element set.
2175 Returns True and original set is restored. */
2176
2177#define assertRaises(call_return_value, exception) \
2178 do { \
2179 assert(call_return_value); \
2180 assert(PyErr_ExceptionMatches(exception)); \
2181 PyErr_Clear(); \
2182 } while(0)
2183
2184static PyObject *
2185test_c_api(PySetObject *so)
2186{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002187 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002188 char *s;
2189 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002190 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002191 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002192
2193 /* Verify preconditions and exercise type/size checks */
2194 assert(PyAnySet_Check(ob));
2195 assert(PyAnySet_CheckExact(ob));
2196 assert(!PyFrozenSet_CheckExact(ob));
2197 assert(PySet_Size(ob) == 3);
2198 assert(PySet_GET_SIZE(ob) == 3);
2199
2200 /* Raise TypeError for non-iterable constructor arguments */
2201 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2202 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2203
2204 /* Raise TypeError for unhashable key */
2205 dup = PySet_New(ob);
2206 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2207 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2208 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2209
2210 /* Exercise successful pop, contains, add, and discard */
2211 elem = PySet_Pop(ob);
2212 assert(PySet_Contains(ob, elem) == 0);
2213 assert(PySet_GET_SIZE(ob) == 2);
2214 assert(PySet_Add(ob, elem) == 0);
2215 assert(PySet_Contains(ob, elem) == 1);
2216 assert(PySet_GET_SIZE(ob) == 3);
2217 assert(PySet_Discard(ob, elem) == 1);
2218 assert(PySet_GET_SIZE(ob) == 2);
2219 assert(PySet_Discard(ob, elem) == 0);
2220 assert(PySet_GET_SIZE(ob) == 2);
2221
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002222 /* Exercise clear */
2223 dup2 = PySet_New(dup);
2224 assert(PySet_Clear(dup2) == 0);
2225 assert(PySet_Size(dup2) == 0);
2226 Py_DECREF(dup2);
2227
2228 /* Raise SystemError on clear or update of frozen set */
2229 f = PyFrozenSet_New(dup);
2230 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2231 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2232 Py_DECREF(f);
2233
2234 /* Exercise direct iteration */
2235 i = 0, count = 0;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002236 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2237 s = PyString_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002238 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2239 count++;
2240 }
2241 assert(count == 3);
2242
2243 /* Exercise updates */
2244 dup2 = PySet_New(NULL);
2245 assert(_PySet_Update(dup2, dup) == 0);
2246 assert(PySet_Size(dup2) == 3);
2247 assert(_PySet_Update(dup2, dup) == 0);
2248 assert(PySet_Size(dup2) == 3);
2249 Py_DECREF(dup2);
2250
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002251 /* Raise SystemError when self argument is not a set or frozenset. */
2252 t = PyTuple_New(0);
2253 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2254 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2255 Py_DECREF(t);
2256
2257 /* Raise SystemError when self argument is not a set. */
2258 f = PyFrozenSet_New(dup);
2259 assert(PySet_Size(f) == 3);
2260 assert(PyFrozenSet_CheckExact(f));
2261 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2262 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2263 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2264 Py_DECREF(f);
2265
2266 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002267 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2268 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002269 assert(PySet_GET_SIZE(ob) == 0);
2270 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2271
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002272 /* Restore the set from the copy using the PyNumber API */
2273 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2274 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002275
2276 /* Verify constructors accept NULL arguments */
2277 f = PySet_New(NULL);
2278 assert(f != NULL);
2279 assert(PySet_GET_SIZE(f) == 0);
2280 Py_DECREF(f);
2281 f = PyFrozenSet_New(NULL);
2282 assert(f != NULL);
2283 assert(PyFrozenSet_CheckExact(f));
2284 assert(PySet_GET_SIZE(f) == 0);
2285 Py_DECREF(f);
2286
2287 Py_DECREF(elem);
2288 Py_DECREF(dup);
2289 Py_RETURN_TRUE;
2290}
2291
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002292#undef assertRaises
2293
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002294#endif