blob: be829a8611e81a5c4cdd4996bc0d903fdcb76701 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006 Copyright (c) 2003-6 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
Thomas Wouters89f507f2006-12-13 04:49:30 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
19 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
25}
26
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000027/* This must be >= 1. */
28#define PERTURB_SHIFT 5
29
30/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000031static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033#ifdef Py_REF_DEBUG
34PyObject *
35_PySet_Dummy(void)
36{
37 return dummy;
38}
39#endif
40
Raymond Hettingerbc841a12005-08-07 13:02:53 +000041#define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
44 (so)->hash = -1; \
45 } while(0)
46
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047#define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000050 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051 } while(0)
52
Raymond Hettingerbc841a12005-08-07 13:02:53 +000053/* Reuse scheme to save calls to malloc, free, and memset */
54#define MAXFREESETS 80
55static PySetObject *free_sets[MAXFREESETS];
56static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057
58/*
59The basic lookup function used by all operations.
60This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
61Open addressing is preferred over chaining since the link overhead for
62chaining would be substantial (100% with typical malloc overhead).
63
64The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000065probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000066
67All arithmetic on hash should ignore overflow.
68
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000069Unlike the dictionary implementation, the lookkey functions can return
70NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000071*/
72
73static setentry *
74set_lookkey(PySetObject *so, PyObject *key, register long hash)
75{
Martin v. Löwis18e16552006-02-15 17:27:45 +000076 register Py_ssize_t i;
77 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000078 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000080 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000081 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000083 PyObject *startkey;
84
85 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000086 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000087 if (entry->key == NULL || entry->key == key)
88 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000089
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000090 if (entry->key == dummy)
91 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000092 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000093 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000095 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
96 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000097 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +000098 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000099 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000100 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 }
102 else {
103 /* The compare did major nasty stuff to the
104 * set: start over.
105 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000106 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000107 }
108 }
109 freeslot = NULL;
110 }
111
112 /* In the loop, key == dummy is by far (factor of 100s) the
113 least likely outcome, so test for that last. */
114 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
115 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000116 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000117 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000118 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000119 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 break;
121 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000122 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000123 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000124 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
127 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000128 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000129 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130 if (cmp > 0)
131 break;
132 }
133 else {
134 /* The compare did major nasty stuff to the
135 * set: start over.
136 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000137 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138 }
139 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000140 else if (entry->key == dummy && freeslot == NULL)
141 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000142 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000143 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000144}
145
146/*
147 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000148 * This means we can always use _PyString_Eq directly and not have to check to
149 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150 */
151static setentry *
152set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
153{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000154 register Py_ssize_t i;
155 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000156 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000158 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000159 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000160
161 /* Make sure this function doesn't have to handle non-string keys,
162 including subclasses of str; e.g., one reason to subclass
163 strings is to override __eq__, and for speed we don't cater to
164 that here. */
165 if (!PyString_CheckExact(key)) {
166 so->lookup = set_lookkey;
167 return set_lookkey(so, key, hash);
168 }
169 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000170 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000171 if (entry->key == NULL || entry->key == key)
172 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000173 if (entry->key == dummy)
174 freeslot = entry;
175 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000176 if (entry->hash == hash && _PyString_Eq(entry->key, key))
177 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000178 freeslot = NULL;
179 }
180
181 /* In the loop, key == dummy is by far (factor of 100s) the
182 least likely outcome, so test for that last. */
183 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
184 i = (i << 2) + i + perturb + 1;
185 entry = &table[i & mask];
186 if (entry->key == NULL)
187 return freeslot == NULL ? entry : freeslot;
188 if (entry->key == key
189 || (entry->hash == hash
190 && entry->key != dummy
191 && _PyString_Eq(entry->key, key)))
192 return entry;
193 if (entry->key == dummy && freeslot == NULL)
194 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000195 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000196 assert(0); /* NOT REACHED */
197 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000198}
199
200/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000201Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000202Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000203Eats a reference to key.
204*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000205static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206set_insert_key(register PySetObject *so, PyObject *key, long hash)
207{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000208 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
210
211 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000212 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213 if (entry == NULL)
214 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000215 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000216 /* UNUSED */
217 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000218 entry->key = key;
219 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000221 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 entry->key = key;
224 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000225 so->used++;
226 Py_DECREF(dummy);
227 } else {
228 /* ACTIVE */
229 Py_DECREF(key);
230 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000231 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000232}
233
234/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000235Internal routine used by set_table_resize() to insert an item which is
236known to be absent from the set. This routine also assumes that
237the set contains no deleted entries. Besides the performance benefit,
238using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
239Note that no refcounts are changed by this routine; if needed, the caller
240is responsible for incref'ing `key`.
241*/
242static void
243set_insert_clean(register PySetObject *so, PyObject *key, long hash)
244{
245 register size_t i;
246 register size_t perturb;
247 register size_t mask = (size_t)so->mask;
248 setentry *table = so->table;
249 register setentry *entry;
250
251 i = hash & mask;
252 entry = &table[i];
253 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
254 i = (i << 2) + i + perturb + 1;
255 entry = &table[i & mask];
256 }
257 so->fill++;
258 entry->key = key;
259 entry->hash = hash;
260 so->used++;
261}
262
263/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000264Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000265keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266actually be smaller than the old one.
267*/
268static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000269set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000272 setentry *oldtable, *newtable, *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274 int is_oldtable_malloced;
275 setentry small_copy[PySet_MINSIZE];
276
277 assert(minused >= 0);
278
279 /* Find the smallest table size > minused. */
280 for (newsize = PySet_MINSIZE;
281 newsize <= minused && newsize > 0;
282 newsize <<= 1)
283 ;
284 if (newsize <= 0) {
285 PyErr_NoMemory();
286 return -1;
287 }
288
289 /* Get space for a new table. */
290 oldtable = so->table;
291 assert(oldtable != NULL);
292 is_oldtable_malloced = oldtable != so->smalltable;
293
294 if (newsize == PySet_MINSIZE) {
295 /* A large table is shrinking, or we can't get any smaller. */
296 newtable = so->smalltable;
297 if (newtable == oldtable) {
298 if (so->fill == so->used) {
299 /* No dummies, so no point doing anything. */
300 return 0;
301 }
302 /* We're not going to resize it, but rebuild the
303 table anyway to purge old dummy entries.
304 Subtle: This is *necessary* if fill==size,
305 as set_lookkey needs at least one virgin slot to
306 terminate failing searches. If fill < size, it's
307 merely desirable, as dummies slow searches. */
308 assert(so->fill > so->used);
309 memcpy(small_copy, oldtable, sizeof(small_copy));
310 oldtable = small_copy;
311 }
312 }
313 else {
314 newtable = PyMem_NEW(setentry, newsize);
315 if (newtable == NULL) {
316 PyErr_NoMemory();
317 return -1;
318 }
319 }
320
321 /* Make the set empty, using the new table. */
322 assert(newtable != oldtable);
323 so->table = newtable;
324 so->mask = newsize - 1;
325 memset(newtable, 0, sizeof(setentry) * newsize);
326 so->used = 0;
327 i = so->fill;
328 so->fill = 0;
329
330 /* Copy the data over; this is refcount-neutral for active entries;
331 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000332 for (entry = oldtable; i > 0; entry++) {
333 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334 /* UNUSED */
335 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000336 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337 /* DUMMY */
338 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000339 assert(entry->key == dummy);
340 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341 } else {
342 /* ACTIVE */
343 --i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000344 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345 }
346 }
347
348 if (is_oldtable_malloced)
349 PyMem_DEL(oldtable);
350 return 0;
351}
352
Raymond Hettingerc991db22005-08-11 07:58:45 +0000353/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
354
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356set_add_entry(register PySetObject *so, setentry *entry)
357{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000358 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000359
360 assert(so->fill <= so->mask); /* at least one empty slot */
361 n_used = so->used;
362 Py_INCREF(entry->key);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000363 if (set_insert_key(so, entry->key, entry->hash) == -1) {
364 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000365 return -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000367 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
368 return 0;
369 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
370}
371
372static int
373set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000374{
375 register long hash;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000376 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377
Raymond Hettingerc991db22005-08-11 07:58:45 +0000378 if (!PyString_CheckExact(key) ||
379 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380 hash = PyObject_Hash(key);
381 if (hash == -1)
382 return -1;
383 }
384 assert(so->fill <= so->mask); /* at least one empty slot */
385 n_used = so->used;
386 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000387 if (set_insert_key(so, key, hash) == -1) {
388 Py_DECREF(key);
389 return -1;
390 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000391 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
392 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000393 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394}
395
396#define DISCARD_NOTFOUND 0
397#define DISCARD_FOUND 1
398
399static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000400set_discard_entry(PySetObject *so, setentry *oldentry)
401{ register setentry *entry;
402 PyObject *old_key;
403
404 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000405 if (entry == NULL)
406 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000407 if (entry->key == NULL || entry->key == dummy)
408 return DISCARD_NOTFOUND;
409 old_key = entry->key;
410 Py_INCREF(dummy);
411 entry->key = dummy;
412 so->used--;
413 Py_DECREF(old_key);
414 return DISCARD_FOUND;
415}
416
417static int
418set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000419{
420 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000421 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000422 PyObject *old_key;
423
424 assert (PyAnySet_Check(so));
425 if (!PyString_CheckExact(key) ||
426 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
427 hash = PyObject_Hash(key);
428 if (hash == -1)
429 return -1;
430 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000431 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000432 if (entry == NULL)
433 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000434 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000435 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000438 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439 so->used--;
440 Py_DECREF(old_key);
441 return DISCARD_FOUND;
442}
443
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000444static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445set_clear_internal(PySetObject *so)
446{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000447 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448 int table_is_malloced;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450 setentry small_copy[PySet_MINSIZE];
451#ifdef Py_DEBUG
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000452 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000454
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455 n = so->mask + 1;
456 i = 0;
457#endif
458
459 table = so->table;
460 assert(table != NULL);
461 table_is_malloced = table != so->smalltable;
462
463 /* This is delicate. During the process of clearing the set,
464 * decrefs can cause the set to mutate. To avoid fatal confusion
465 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000466 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467 * clearing.
468 */
469 fill = so->fill;
470 if (table_is_malloced)
471 EMPTY_TO_MINSIZE(so);
472
473 else if (fill > 0) {
474 /* It's a small table with something that needs to be cleared.
475 * Afraid the only safe way is to copy the set entries into
476 * another small table first.
477 */
478 memcpy(small_copy, table, sizeof(small_copy));
479 table = small_copy;
480 EMPTY_TO_MINSIZE(so);
481 }
482 /* else it's a small table that's already empty */
483
484 /* Now we can finally clear things. If C had refcounts, we could
485 * assert that the refcount on table is 1 now, i.e. that this function
486 * has unique access to it, so decref side-effects can't alter it.
487 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000488 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489#ifdef Py_DEBUG
490 assert(i < n);
491 ++i;
492#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000493 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000495 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496 }
497#ifdef Py_DEBUG
498 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500#endif
501 }
502
503 if (table_is_malloced)
504 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000505 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506}
507
508/*
509 * Iterate over a set table. Use like so:
510 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000512 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000513 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000514 * while (set_next(yourset, &pos, &entry)) {
515 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516 * }
517 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000518 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519 * mutates the table.
520 */
521static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000522set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000526 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000527
528 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000529 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000530 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000531 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000533 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536 if (i > mask)
537 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000538 assert(table[i].key != NULL);
539 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540 return 1;
541}
542
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000543static void
544set_dealloc(PySetObject *so)
545{
546 register setentry *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000547 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000548 PyObject_GC_UnTrack(so);
549 Py_TRASHCAN_SAFE_BEGIN(so)
550 if (so->weakreflist != NULL)
551 PyObject_ClearWeakRefs((PyObject *) so);
552
553 for (entry = so->table; fill > 0; entry++) {
554 if (entry->key) {
555 --fill;
556 Py_DECREF(entry->key);
557 }
558 }
559 if (so->table != so->smalltable)
560 PyMem_DEL(so->table);
561 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
562 free_sets[num_free_sets++] = so;
563 else
564 so->ob_type->tp_free(so);
565 Py_TRASHCAN_SAFE_END(so)
566}
567
568static int
569set_tp_print(PySetObject *so, FILE *fp, int flags)
570{
571 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000572 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000573 char *emit = ""; /* No separator emitted on first pass */
574 char *separator = ", ";
Georg Brandlc4996ba2006-08-28 19:37:11 +0000575 int literalform = 0;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000576 int status = Py_ReprEnter((PyObject*)so);
577
578 if (status != 0) {
579 if (status < 0)
580 return status;
581 fprintf(fp, "%s(...)", so->ob_type->tp_name);
582 return 0;
583 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000584
Georg Brandlc4996ba2006-08-28 19:37:11 +0000585 if (!so->used) {
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000586 Py_ReprLeave((PyObject*)so);
Georg Brandlc4996ba2006-08-28 19:37:11 +0000587 fprintf(fp, "%s()", so->ob_type->tp_name);
588 return 0;
589 }
590
591 if (so->ob_type == &PySet_Type) {
592 literalform = 1;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000593 fprintf(fp, "{");
Georg Brandlc4996ba2006-08-28 19:37:11 +0000594 } else
Guido van Rossum86e58e22006-08-28 15:27:34 +0000595 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000596 while (set_next(so, &pos, &entry)) {
597 fputs(emit, fp);
598 emit = separator;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000599 if (PyObject_Print(entry->key, fp, 0) != 0) {
600 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601 return -1;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000602 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000603 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000604 if (literalform)
Guido van Rossum86e58e22006-08-28 15:27:34 +0000605 fputs("}", fp);
606 else
607 fputs("])", fp);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000608 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000609 return 0;
610}
611
612static PyObject *
613set_repr(PySetObject *so)
614{
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000615 PyObject *keys, *result=NULL, *listrepr;
616 int status = Py_ReprEnter((PyObject*)so);
617
618 if (status != 0) {
619 if (status < 0)
620 return NULL;
621 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
622 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000623
Georg Brandlc4996ba2006-08-28 19:37:11 +0000624 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000625 if (!so->used) {
626 Py_ReprLeave((PyObject*)so);
Georg Brandlc4996ba2006-08-28 19:37:11 +0000627 return PyString_FromFormat("%s()", so->ob_type->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000628 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000629
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000630 keys = PySequence_List((PyObject *)so);
631 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000632 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000633 listrepr = PyObject_Repr(keys);
634 Py_DECREF(keys);
635 if (listrepr == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000636 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000637
Guido van Rossum86e58e22006-08-28 15:27:34 +0000638 if (so->ob_type == &PySet_Type) {
639 char *s = PyString_AS_STRING(listrepr);
640 s += 1;
641 s[strlen(s)-1] = 0;
642 result = PyString_FromFormat("{%s}", s);
643 } else {
644 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
Georg Brandlc4996ba2006-08-28 19:37:11 +0000645 PyString_AS_STRING(listrepr));
Guido van Rossum86e58e22006-08-28 15:27:34 +0000646 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000647 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000648done:
649 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000650 return result;
651}
652
Martin v. Löwis18e16552006-02-15 17:27:45 +0000653static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000654set_len(PyObject *so)
655{
656 return ((PySetObject *)so)->used;
657}
658
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000659static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000660set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000661{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000662 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000663 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000664 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000665
666 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000667 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000669 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670 if (other == so || other->used == 0)
671 /* a.update(a) or a.update({}); nothing to do */
672 return 0;
673 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000674 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000675 * that there will be no (or few) overlapping keys.
676 */
677 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
678 if (set_table_resize(so, (so->used + other->used)*2) != 0)
679 return -1;
680 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000681 for (i = 0; i <= other->mask; i++) {
682 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000683 if (entry->key != NULL &&
684 entry->key != dummy) {
685 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000686 if (set_insert_key(so, entry->key, entry->hash) == -1) {
687 Py_DECREF(entry->key);
688 return -1;
689 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000690 }
691 }
692 return 0;
693}
694
695static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000696set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000697{
698 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000699 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000700
701 if (!PyString_CheckExact(key) ||
702 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
703 hash = PyObject_Hash(key);
704 if (hash == -1)
705 return -1;
706 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000707 entry = (so->lookup)(so, key, hash);
708 if (entry == NULL)
709 return -1;
710 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000711 return key != NULL && key != dummy;
712}
713
Raymond Hettingerc991db22005-08-11 07:58:45 +0000714static int
715set_contains_entry(PySetObject *so, setentry *entry)
716{
717 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000718 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000719
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000720 lu_entry = (so->lookup)(so, entry->key, entry->hash);
721 if (lu_entry == NULL)
722 return -1;
723 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000724 return key != NULL && key != dummy;
725}
726
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000727static PyObject *
728set_pop(PySetObject *so)
729{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000730 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000731 register setentry *entry;
732 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000733
734 assert (PyAnySet_Check(so));
735 if (so->used == 0) {
736 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
737 return NULL;
738 }
739
740 /* Set entry to "the first" unused or dummy set entry. We abuse
741 * the hash field of slot 0 to hold a search finger:
742 * If slot 0 has a value, use slot 0.
743 * Else slot 0 is being used to hold a search finger,
744 * and we use its hash value as the first index to look.
745 */
746 entry = &so->table[0];
747 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000748 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000749 /* The hash field may be a real hash value, or it may be a
750 * legit search finger, or it may be a once-legit search
751 * finger that's out of bounds now because it wrapped around
752 * or the table shrunk -- simply make sure it's in bounds now.
753 */
754 if (i > so->mask || i < 1)
755 i = 1; /* skip slot 0 */
756 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
757 i++;
758 if (i > so->mask)
759 i = 1;
760 }
761 }
762 key = entry->key;
763 Py_INCREF(dummy);
764 entry->key = dummy;
765 so->used--;
766 so->table[0].hash = i + 1; /* next place to start */
767 return key;
768}
769
770PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
771
772static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000773set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000774{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000775 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000776 setentry *entry;
777
778 while (set_next(so, &pos, &entry))
779 Py_VISIT(entry->key);
780 return 0;
781}
782
783static long
784frozenset_hash(PyObject *self)
785{
786 PySetObject *so = (PySetObject *)self;
787 long h, hash = 1927868237L;
788 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000789 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000790
791 if (so->hash != -1)
792 return so->hash;
793
794 hash *= PySet_GET_SIZE(self) + 1;
795 while (set_next(so, &pos, &entry)) {
796 /* Work to increase the bit dispersion for closely spaced hash
797 values. The is important because some use cases have many
798 combinations of a small number of elements with nearby
799 hashes so that many distinct combinations collapse to only
800 a handful of distinct hash values. */
801 h = entry->hash;
802 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
803 }
804 hash = hash * 69069L + 907133923L;
805 if (hash == -1)
806 hash = 590923713L;
807 so->hash = hash;
808 return hash;
809}
810
Raymond Hettingera9d99362005-08-05 00:01:15 +0000811/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813typedef struct {
814 PyObject_HEAD
815 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000816 Py_ssize_t si_used;
817 Py_ssize_t si_pos;
818 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000819} setiterobject;
820
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000821static void
822setiter_dealloc(setiterobject *si)
823{
824 Py_XDECREF(si->si_set);
825 PyObject_Del(si);
826}
827
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000828static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829setiter_len(setiterobject *si)
830{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000831 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000833 len = si->len;
834 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835}
836
Armin Rigof5b3e362006-02-11 21:32:43 +0000837PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000838
839static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000840 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000841 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000842};
843
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000844static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000845{
846 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000847 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000848 register setentry *entry;
849 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000851 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000852 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000853 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000854
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000855 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856 PyErr_SetString(PyExc_RuntimeError,
857 "Set changed size during iteration");
858 si->si_used = -1; /* Make this state sticky */
859 return NULL;
860 }
861
862 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000863 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000864 entry = so->table;
865 mask = so->mask;
866 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000867 i++;
868 si->si_pos = i+1;
869 if (i > mask)
870 goto fail;
871 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000872 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000873 Py_INCREF(key);
874 return key;
875
876fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000877 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000878 si->si_set = NULL;
879 return NULL;
880}
881
Hye-Shik Change2956762005-08-01 05:26:41 +0000882static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000883 PyObject_HEAD_INIT(&PyType_Type)
884 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000885 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000886 sizeof(setiterobject), /* tp_basicsize */
887 0, /* tp_itemsize */
888 /* methods */
889 (destructor)setiter_dealloc, /* tp_dealloc */
890 0, /* tp_print */
891 0, /* tp_getattr */
892 0, /* tp_setattr */
893 0, /* tp_compare */
894 0, /* tp_repr */
895 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000896 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000897 0, /* tp_as_mapping */
898 0, /* tp_hash */
899 0, /* tp_call */
900 0, /* tp_str */
901 PyObject_GenericGetAttr, /* tp_getattro */
902 0, /* tp_setattro */
903 0, /* tp_as_buffer */
904 Py_TPFLAGS_DEFAULT, /* tp_flags */
905 0, /* tp_doc */
906 0, /* tp_traverse */
907 0, /* tp_clear */
908 0, /* tp_richcompare */
909 0, /* tp_weaklistoffset */
910 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000911 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000912 setiter_methods, /* tp_methods */
913 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000914};
915
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000916static PyObject *
917set_iter(PySetObject *so)
918{
919 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
920 if (si == NULL)
921 return NULL;
922 Py_INCREF(so);
923 si->si_set = so;
924 si->si_used = so->used;
925 si->si_pos = 0;
926 si->len = so->used;
927 return (PyObject *)si;
928}
929
Raymond Hettingerd7946662005-08-01 21:39:29 +0000930static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000931set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000932{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000933 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000934
Raymond Hettingerd7946662005-08-01 21:39:29 +0000935 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000936 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000937
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000938 if (PyDict_Check(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000939 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000940 Py_ssize_t pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000941 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000942 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000943 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000944 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000945 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000946 }
947
Raymond Hettingera38123e2003-11-24 22:18:49 +0000948 it = PyObject_GetIter(other);
949 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000950 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000951
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000952 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000953 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000954 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000955 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000956 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000957 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000958 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000959 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000960 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000961 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000962 return -1;
963 return 0;
964}
965
966static PyObject *
967set_update(PySetObject *so, PyObject *other)
968{
969 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000970 return NULL;
971 Py_RETURN_NONE;
972}
973
974PyDoc_STRVAR(update_doc,
975"Update a set with the union of itself and another.");
976
977static PyObject *
978make_new_set(PyTypeObject *type, PyObject *iterable)
979{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000980 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000981
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000982 if (dummy == NULL) { /* Auto-initialize dummy */
983 dummy = PyString_FromString("<dummy key>");
984 if (dummy == NULL)
985 return NULL;
986 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000987
988 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000989 if (num_free_sets &&
990 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
991 so = free_sets[--num_free_sets];
992 assert (so != NULL && PyAnySet_CheckExact(so));
993 so->ob_type = type;
994 _Py_NewReference((PyObject *)so);
995 EMPTY_TO_MINSIZE(so);
996 PyObject_GC_Track(so);
997 } else {
998 so = (PySetObject *)type->tp_alloc(type, 0);
999 if (so == NULL)
1000 return NULL;
1001 /* tp_alloc has already zeroed the structure */
1002 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1003 INIT_NONZERO_SET_SLOTS(so);
1004 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001005
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001006 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001007 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001008
Raymond Hettingera38123e2003-11-24 22:18:49 +00001009 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001010 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001011 Py_DECREF(so);
1012 return NULL;
1013 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001014 }
1015
Raymond Hettingera690a992003-11-16 16:17:49 +00001016 return (PyObject *)so;
1017}
1018
Raymond Hettingerd7946662005-08-01 21:39:29 +00001019/* The empty frozenset is a singleton */
1020static PyObject *emptyfrozenset = NULL;
1021
Raymond Hettingera690a992003-11-16 16:17:49 +00001022static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001023frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001024{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001025 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001026
Georg Brandl02c42872005-08-26 06:42:30 +00001027 if (!_PyArg_NoKeywords("frozenset()", kwds))
1028 return NULL;
1029
Raymond Hettingera690a992003-11-16 16:17:49 +00001030 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1031 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001032
1033 if (type != &PyFrozenSet_Type)
1034 return make_new_set(type, iterable);
1035
1036 if (iterable != NULL) {
1037 /* frozenset(f) is idempotent */
1038 if (PyFrozenSet_CheckExact(iterable)) {
1039 Py_INCREF(iterable);
1040 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001041 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001042 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001043 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001044 return result;
1045 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001046 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001047 /* The empty frozenset is a singleton */
1048 if (emptyfrozenset == NULL)
1049 emptyfrozenset = make_new_set(type, NULL);
1050 Py_XINCREF(emptyfrozenset);
1051 return emptyfrozenset;
1052}
1053
1054void
1055PySet_Fini(void)
1056{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001057 PySetObject *so;
1058
1059 while (num_free_sets) {
1060 num_free_sets--;
1061 so = free_sets[num_free_sets];
1062 PyObject_GC_Del(so);
1063 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001064 Py_CLEAR(dummy);
1065 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001066}
1067
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001068static PyObject *
1069set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1070{
Georg Brandl02c42872005-08-26 06:42:30 +00001071 if (!_PyArg_NoKeywords("set()", kwds))
1072 return NULL;
1073
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001074 return make_new_set(type, NULL);
1075}
1076
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001077/* set_swap_bodies() switches the contents of any two sets by moving their
1078 internal data pointers and, if needed, copying the internal smalltables.
1079 Semantically equivalent to:
1080
1081 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1082
1083 The function always succeeds and it leaves both objects in a stable state.
1084 Useful for creating temporary frozensets from sets for membership testing
1085 in __contains__(), discard(), and remove(). Also useful for operations
1086 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001087 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001088*/
1089
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001090static void
1091set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001092{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001093 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001094 setentry *u;
1095 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1096 setentry tab[PySet_MINSIZE];
1097 long h;
1098
1099 t = a->fill; a->fill = b->fill; b->fill = t;
1100 t = a->used; a->used = b->used; b->used = t;
1101 t = a->mask; a->mask = b->mask; b->mask = t;
1102
1103 u = a->table;
1104 if (a->table == a->smalltable)
1105 u = b->smalltable;
1106 a->table = b->table;
1107 if (b->table == b->smalltable)
1108 a->table = a->smalltable;
1109 b->table = u;
1110
1111 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1112
1113 if (a->table == a->smalltable || b->table == b->smalltable) {
1114 memcpy(tab, a->smalltable, sizeof(tab));
1115 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1116 memcpy(b->smalltable, tab, sizeof(tab));
1117 }
1118
Raymond Hettingera580c472005-08-05 17:19:54 +00001119 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1120 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1121 h = a->hash; a->hash = b->hash; b->hash = h;
1122 } else {
1123 a->hash = -1;
1124 b->hash = -1;
1125 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001126}
1127
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001128static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001129set_copy(PySetObject *so)
1130{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001131 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001132}
1133
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001134static PyObject *
1135frozenset_copy(PySetObject *so)
1136{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001137 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001138 Py_INCREF(so);
1139 return (PyObject *)so;
1140 }
1141 return set_copy(so);
1142}
1143
Raymond Hettingera690a992003-11-16 16:17:49 +00001144PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1145
1146static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001147set_clear(PySetObject *so)
1148{
1149 set_clear_internal(so);
1150 Py_RETURN_NONE;
1151}
1152
1153PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1154
1155static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001156set_union(PySetObject *so, PyObject *other)
1157{
1158 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001159
1160 result = (PySetObject *)set_copy(so);
1161 if (result == NULL)
1162 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001163 if ((PyObject *)so == other)
1164 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001165 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001166 Py_DECREF(result);
1167 return NULL;
1168 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001169 return (PyObject *)result;
1170}
1171
1172PyDoc_STRVAR(union_doc,
1173 "Return the union of two sets as a new set.\n\
1174\n\
1175(i.e. all elements that are in either set.)");
1176
1177static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001178set_or(PySetObject *so, PyObject *other)
1179{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001180 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001181 Py_INCREF(Py_NotImplemented);
1182 return Py_NotImplemented;
1183 }
1184 return set_union(so, other);
1185}
1186
1187static PyObject *
1188set_ior(PySetObject *so, PyObject *other)
1189{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001190 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001191 Py_INCREF(Py_NotImplemented);
1192 return Py_NotImplemented;
1193 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001194 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001195 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001196 Py_INCREF(so);
1197 return (PyObject *)so;
1198}
1199
1200static PyObject *
1201set_intersection(PySetObject *so, PyObject *other)
1202{
1203 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001204 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001205
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001206 if ((PyObject *)so == other)
1207 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001208
Raymond Hettingera690a992003-11-16 16:17:49 +00001209 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1210 if (result == NULL)
1211 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001212
Raymond Hettingerc991db22005-08-11 07:58:45 +00001213 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001214 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001215 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001216
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001217 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001218 tmp = (PyObject *)so;
1219 so = (PySetObject *)other;
1220 other = tmp;
1221 }
1222
Raymond Hettingerc991db22005-08-11 07:58:45 +00001223 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001224 int rv = set_contains_entry(so, entry);
1225 if (rv == -1) {
1226 Py_DECREF(result);
1227 return NULL;
1228 }
1229 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001230 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001231 Py_DECREF(result);
1232 return NULL;
1233 }
1234 }
1235 }
1236 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001237 }
1238
Raymond Hettingera690a992003-11-16 16:17:49 +00001239 it = PyObject_GetIter(other);
1240 if (it == NULL) {
1241 Py_DECREF(result);
1242 return NULL;
1243 }
1244
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001245 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001246 int rv;
1247 setentry entry;
1248 long hash = PyObject_Hash(key);
1249
1250 if (hash == -1) {
1251 Py_DECREF(it);
1252 Py_DECREF(result);
1253 Py_DECREF(key);
1254 return NULL;
1255 }
1256 entry.hash = hash;
1257 entry.key = key;
1258 rv = set_contains_entry(so, &entry);
1259 if (rv == -1) {
1260 Py_DECREF(it);
1261 Py_DECREF(result);
1262 Py_DECREF(key);
1263 return NULL;
1264 }
1265 if (rv) {
1266 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001267 Py_DECREF(it);
1268 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001269 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001270 return NULL;
1271 }
1272 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001273 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001274 }
1275 Py_DECREF(it);
1276 if (PyErr_Occurred()) {
1277 Py_DECREF(result);
1278 return NULL;
1279 }
1280 return (PyObject *)result;
1281}
1282
1283PyDoc_STRVAR(intersection_doc,
1284"Return the intersection of two sets as a new set.\n\
1285\n\
1286(i.e. all elements that are in both sets.)");
1287
1288static PyObject *
1289set_intersection_update(PySetObject *so, PyObject *other)
1290{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001291 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001292
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001293 tmp = set_intersection(so, other);
1294 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001295 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001296 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001297 Py_DECREF(tmp);
1298 Py_RETURN_NONE;
1299}
1300
1301PyDoc_STRVAR(intersection_update_doc,
1302"Update a set with the intersection of itself and another.");
1303
1304static PyObject *
1305set_and(PySetObject *so, PyObject *other)
1306{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001307 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001308 Py_INCREF(Py_NotImplemented);
1309 return Py_NotImplemented;
1310 }
1311 return set_intersection(so, other);
1312}
1313
1314static PyObject *
1315set_iand(PySetObject *so, PyObject *other)
1316{
1317 PyObject *result;
1318
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001319 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001320 Py_INCREF(Py_NotImplemented);
1321 return Py_NotImplemented;
1322 }
1323 result = set_intersection_update(so, other);
1324 if (result == NULL)
1325 return NULL;
1326 Py_DECREF(result);
1327 Py_INCREF(so);
1328 return (PyObject *)so;
1329}
1330
Neal Norwitz6576bd82005-11-13 18:41:28 +00001331static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001332set_difference_update_internal(PySetObject *so, PyObject *other)
1333{
1334 if ((PyObject *)so == other)
1335 return set_clear_internal(so);
1336
1337 if (PyAnySet_Check(other)) {
1338 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001339 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001340
1341 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001342 if (set_discard_entry(so, entry) == -1)
1343 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001344 } else {
1345 PyObject *key, *it;
1346 it = PyObject_GetIter(other);
1347 if (it == NULL)
1348 return -1;
1349
1350 while ((key = PyIter_Next(it)) != NULL) {
1351 if (set_discard_key(so, key) == -1) {
1352 Py_DECREF(it);
1353 Py_DECREF(key);
1354 return -1;
1355 }
1356 Py_DECREF(key);
1357 }
1358 Py_DECREF(it);
1359 if (PyErr_Occurred())
1360 return -1;
1361 }
1362 /* If more than 1/5 are dummies, then resize them away. */
1363 if ((so->fill - so->used) * 5 < so->mask)
1364 return 0;
1365 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1366}
1367
Raymond Hettingera690a992003-11-16 16:17:49 +00001368static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001369set_difference_update(PySetObject *so, PyObject *other)
1370{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001371 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001372 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001373 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001374}
1375
1376PyDoc_STRVAR(difference_update_doc,
1377"Remove all elements of another set from this set.");
1378
1379static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001380set_difference(PySetObject *so, PyObject *other)
1381{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001382 PyObject *result;
1383 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001384 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001385
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001386 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001387 result = set_copy(so);
1388 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001389 return NULL;
1390 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001391 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001392 Py_DECREF(result);
1393 return NULL;
1394 }
1395
1396 result = make_new_set(so->ob_type, NULL);
1397 if (result == NULL)
1398 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001399
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001400 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001401 while (set_next(so, &pos, &entry)) {
1402 setentry entrycopy;
1403 entrycopy.hash = entry->hash;
1404 entrycopy.key = entry->key;
1405 if (!PyDict_Contains(other, entry->key)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001406 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1407 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001408 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001409 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001410 }
1411 }
1412 return result;
1413 }
1414
Raymond Hettingerc991db22005-08-11 07:58:45 +00001415 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001416 int rv = set_contains_entry((PySetObject *)other, entry);
1417 if (rv == -1) {
1418 Py_DECREF(result);
1419 return NULL;
1420 }
1421 if (!rv) {
1422 if (set_add_entry((PySetObject *)result, entry) == -1) {
1423 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001424 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001425 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001426 }
1427 }
1428 return result;
1429}
1430
1431PyDoc_STRVAR(difference_doc,
1432"Return the difference of two sets as a new set.\n\
1433\n\
1434(i.e. all elements that are in this set but not the other.)");
1435static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001436set_sub(PySetObject *so, PyObject *other)
1437{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001438 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001439 Py_INCREF(Py_NotImplemented);
1440 return Py_NotImplemented;
1441 }
1442 return set_difference(so, other);
1443}
1444
1445static PyObject *
1446set_isub(PySetObject *so, PyObject *other)
1447{
1448 PyObject *result;
1449
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001450 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001451 Py_INCREF(Py_NotImplemented);
1452 return Py_NotImplemented;
1453 }
1454 result = set_difference_update(so, other);
1455 if (result == NULL)
1456 return NULL;
1457 Py_DECREF(result);
1458 Py_INCREF(so);
1459 return (PyObject *)so;
1460}
1461
1462static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001463set_symmetric_difference_update(PySetObject *so, PyObject *other)
1464{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001465 PySetObject *otherset;
1466 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001467 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001468 setentry *entry;
1469
1470 if ((PyObject *)so == other)
1471 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001472
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001473 if (PyDict_Check(other)) {
1474 PyObject *value;
1475 int rv;
1476 while (PyDict_Next(other, &pos, &key, &value)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001477 setentry an_entry;
1478 long hash = PyObject_Hash(key);
1479
1480 if (hash == -1)
1481 return NULL;
1482 an_entry.hash = hash;
1483 an_entry.key = key;
1484 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001485 if (rv == -1)
1486 return NULL;
1487 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001488 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001489 return NULL;
1490 }
1491 }
1492 Py_RETURN_NONE;
1493 }
1494
1495 if (PyAnySet_Check(other)) {
1496 Py_INCREF(other);
1497 otherset = (PySetObject *)other;
1498 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001499 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1500 if (otherset == NULL)
1501 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001502 }
1503
Raymond Hettingerc991db22005-08-11 07:58:45 +00001504 while (set_next(otherset, &pos, &entry)) {
1505 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001506 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001507 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001508 return NULL;
1509 }
1510 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001511 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001512 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001513 return NULL;
1514 }
1515 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001516 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001517 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001518 Py_RETURN_NONE;
1519}
1520
1521PyDoc_STRVAR(symmetric_difference_update_doc,
1522"Update a set with the symmetric difference of itself and another.");
1523
1524static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001525set_symmetric_difference(PySetObject *so, PyObject *other)
1526{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001527 PyObject *rv;
1528 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001529
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001530 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1531 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001532 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001533 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1534 if (rv == NULL)
1535 return NULL;
1536 Py_DECREF(rv);
1537 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001538}
1539
1540PyDoc_STRVAR(symmetric_difference_doc,
1541"Return the symmetric difference of two sets as a new set.\n\
1542\n\
1543(i.e. all elements that are in exactly one of the sets.)");
1544
1545static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001546set_xor(PySetObject *so, PyObject *other)
1547{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001548 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001549 Py_INCREF(Py_NotImplemented);
1550 return Py_NotImplemented;
1551 }
1552 return set_symmetric_difference(so, other);
1553}
1554
1555static PyObject *
1556set_ixor(PySetObject *so, PyObject *other)
1557{
1558 PyObject *result;
1559
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001560 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001561 Py_INCREF(Py_NotImplemented);
1562 return Py_NotImplemented;
1563 }
1564 result = set_symmetric_difference_update(so, other);
1565 if (result == NULL)
1566 return NULL;
1567 Py_DECREF(result);
1568 Py_INCREF(so);
1569 return (PyObject *)so;
1570}
1571
1572static PyObject *
1573set_issubset(PySetObject *so, PyObject *other)
1574{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001575 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001576 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001577
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001578 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001579 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001580 tmp = make_new_set(&PySet_Type, other);
1581 if (tmp == NULL)
1582 return NULL;
1583 result = set_issubset(so, tmp);
1584 Py_DECREF(tmp);
1585 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001586 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001587 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001588 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001589
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001590 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001591 int rv = set_contains_entry((PySetObject *)other, entry);
1592 if (rv == -1)
1593 return NULL;
1594 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001595 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001596 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001597 Py_RETURN_TRUE;
1598}
1599
1600PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1601
1602static PyObject *
1603set_issuperset(PySetObject *so, PyObject *other)
1604{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001605 PyObject *tmp, *result;
1606
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001607 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001608 tmp = make_new_set(&PySet_Type, other);
1609 if (tmp == NULL)
1610 return NULL;
1611 result = set_issuperset(so, tmp);
1612 Py_DECREF(tmp);
1613 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001614 }
1615 return set_issubset((PySetObject *)other, (PyObject *)so);
1616}
1617
1618PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1619
Raymond Hettingera690a992003-11-16 16:17:49 +00001620static PyObject *
1621set_richcompare(PySetObject *v, PyObject *w, int op)
1622{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001623 PyObject *r1, *r2;
1624
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001625 if(!PyAnySet_Check(w)) {
1626 if (op == Py_EQ)
1627 Py_RETURN_FALSE;
1628 if (op == Py_NE)
1629 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001630 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1631 return NULL;
1632 }
1633 switch (op) {
1634 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001635 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001636 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001637 if (v->hash != -1 &&
1638 ((PySetObject *)w)->hash != -1 &&
1639 v->hash != ((PySetObject *)w)->hash)
1640 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001641 return set_issubset(v, w);
1642 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001643 r1 = set_richcompare(v, w, Py_EQ);
1644 if (r1 == NULL)
1645 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001646 r2 = PyBool_FromLong(PyObject_Not(r1));
1647 Py_DECREF(r1);
1648 return r2;
1649 case Py_LE:
1650 return set_issubset(v, w);
1651 case Py_GE:
1652 return set_issuperset(v, w);
1653 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001654 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001655 Py_RETURN_FALSE;
1656 return set_issubset(v, w);
1657 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001658 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001659 Py_RETURN_FALSE;
1660 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001661 }
1662 Py_INCREF(Py_NotImplemented);
1663 return Py_NotImplemented;
1664}
1665
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001666static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001667set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001668{
1669 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1670 return -1;
1671}
1672
Raymond Hettingera690a992003-11-16 16:17:49 +00001673static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001674set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001675{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001676 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001677 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001678 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001679}
1680
1681PyDoc_STRVAR(add_doc,
1682"Add an element to a set.\n\
1683\n\
1684This has no effect if the element is already present.");
1685
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001686static int
1687set_contains(PySetObject *so, PyObject *key)
1688{
1689 PyObject *tmpkey;
1690 int rv;
1691
1692 rv = set_contains_key(so, key);
1693 if (rv == -1) {
1694 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1695 return -1;
1696 PyErr_Clear();
1697 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1698 if (tmpkey == NULL)
1699 return -1;
1700 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1701 rv = set_contains(so, tmpkey);
1702 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1703 Py_DECREF(tmpkey);
1704 }
1705 return rv;
1706}
1707
1708static PyObject *
1709set_direct_contains(PySetObject *so, PyObject *key)
1710{
1711 long result;
1712
1713 result = set_contains(so, key);
1714 if (result == -1)
1715 return NULL;
1716 return PyBool_FromLong(result);
1717}
1718
1719PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1720
Raymond Hettingera690a992003-11-16 16:17:49 +00001721static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001722set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001723{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001724 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001725 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001726
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001727 rv = set_discard_key(so, key);
1728 if (rv == -1) {
1729 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1730 return NULL;
1731 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001732 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1733 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001734 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001735 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001736 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001737 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001738 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001739 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001740 } else if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001741 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001742 return NULL;
1743 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001744 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001745}
1746
1747PyDoc_STRVAR(remove_doc,
1748"Remove an element from a set; it must be a member.\n\
1749\n\
1750If the element is not a member, raise a KeyError.");
1751
1752static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001753set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001754{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001755 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001756 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001757
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001758 rv = set_discard_key(so, key);
1759 if (rv == -1) {
1760 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1761 return NULL;
1762 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001763 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1764 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001765 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001766 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001767 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001768 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001769 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001770 return result;
1771 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001772 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001773}
1774
1775PyDoc_STRVAR(discard_doc,
1776"Remove an element from a set if it is a member.\n\
1777\n\
1778If the element is not a member, do nothing.");
1779
1780static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001781set_reduce(PySetObject *so)
1782{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001783 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001784
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001785 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001786 if (keys == NULL)
1787 goto done;
1788 args = PyTuple_Pack(1, keys);
1789 if (args == NULL)
1790 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001791 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1792 if (dict == NULL) {
1793 PyErr_Clear();
1794 dict = Py_None;
1795 Py_INCREF(dict);
1796 }
1797 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001798done:
1799 Py_XDECREF(args);
1800 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001801 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001802 return result;
1803}
1804
1805PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1806
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001807static int
1808set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1809{
1810 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001811
1812 if (!PyAnySet_Check(self))
1813 return -1;
1814 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1815 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001816 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001817 self->hash = -1;
1818 if (iterable == NULL)
1819 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001820 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001821}
1822
Raymond Hettingera690a992003-11-16 16:17:49 +00001823static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001824 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001825 0, /* sq_concat */
1826 0, /* sq_repeat */
1827 0, /* sq_item */
1828 0, /* sq_slice */
1829 0, /* sq_ass_item */
1830 0, /* sq_ass_slice */
1831 (objobjproc)set_contains, /* sq_contains */
1832};
1833
1834/* set object ********************************************************/
1835
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001836#ifdef Py_DEBUG
1837static PyObject *test_c_api(PySetObject *so);
1838
1839PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1840All is well if assertions don't fail.");
1841#endif
1842
Raymond Hettingera690a992003-11-16 16:17:49 +00001843static PyMethodDef set_methods[] = {
1844 {"add", (PyCFunction)set_add, METH_O,
1845 add_doc},
1846 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1847 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001848 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001849 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001850 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1851 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001852 {"discard", (PyCFunction)set_discard, METH_O,
1853 discard_doc},
1854 {"difference", (PyCFunction)set_difference, METH_O,
1855 difference_doc},
1856 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1857 difference_update_doc},
1858 {"intersection",(PyCFunction)set_intersection, METH_O,
1859 intersection_doc},
1860 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1861 intersection_update_doc},
1862 {"issubset", (PyCFunction)set_issubset, METH_O,
1863 issubset_doc},
1864 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1865 issuperset_doc},
1866 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1867 pop_doc},
1868 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1869 reduce_doc},
1870 {"remove", (PyCFunction)set_remove, METH_O,
1871 remove_doc},
1872 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1873 symmetric_difference_doc},
1874 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1875 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001876#ifdef Py_DEBUG
1877 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1878 test_c_api_doc},
1879#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001880 {"union", (PyCFunction)set_union, METH_O,
1881 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001882 {"update", (PyCFunction)set_update, METH_O,
1883 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001884 {NULL, NULL} /* sentinel */
1885};
1886
1887static PyNumberMethods set_as_number = {
1888 0, /*nb_add*/
1889 (binaryfunc)set_sub, /*nb_subtract*/
1890 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001891 0, /*nb_remainder*/
1892 0, /*nb_divmod*/
1893 0, /*nb_power*/
1894 0, /*nb_negative*/
1895 0, /*nb_positive*/
1896 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001897 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001898 0, /*nb_invert*/
1899 0, /*nb_lshift*/
1900 0, /*nb_rshift*/
1901 (binaryfunc)set_and, /*nb_and*/
1902 (binaryfunc)set_xor, /*nb_xor*/
1903 (binaryfunc)set_or, /*nb_or*/
1904 0, /*nb_coerce*/
1905 0, /*nb_int*/
1906 0, /*nb_long*/
1907 0, /*nb_float*/
1908 0, /*nb_oct*/
1909 0, /*nb_hex*/
1910 0, /*nb_inplace_add*/
1911 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1912 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001913 0, /*nb_inplace_remainder*/
1914 0, /*nb_inplace_power*/
1915 0, /*nb_inplace_lshift*/
1916 0, /*nb_inplace_rshift*/
1917 (binaryfunc)set_iand, /*nb_inplace_and*/
1918 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1919 (binaryfunc)set_ior, /*nb_inplace_or*/
1920};
1921
1922PyDoc_STRVAR(set_doc,
1923"set(iterable) --> set object\n\
1924\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001925Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001926
1927PyTypeObject PySet_Type = {
1928 PyObject_HEAD_INIT(&PyType_Type)
1929 0, /* ob_size */
1930 "set", /* tp_name */
1931 sizeof(PySetObject), /* tp_basicsize */
1932 0, /* tp_itemsize */
1933 /* methods */
1934 (destructor)set_dealloc, /* tp_dealloc */
1935 (printfunc)set_tp_print, /* tp_print */
1936 0, /* tp_getattr */
1937 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001938 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001939 (reprfunc)set_repr, /* tp_repr */
1940 &set_as_number, /* tp_as_number */
1941 &set_as_sequence, /* tp_as_sequence */
1942 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001943 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001944 0, /* tp_call */
1945 0, /* tp_str */
1946 PyObject_GenericGetAttr, /* tp_getattro */
1947 0, /* tp_setattro */
1948 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001949 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001950 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001951 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001952 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001953 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001954 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001955 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001956 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001957 0, /* tp_iternext */
1958 set_methods, /* tp_methods */
1959 0, /* tp_members */
1960 0, /* tp_getset */
1961 0, /* tp_base */
1962 0, /* tp_dict */
1963 0, /* tp_descr_get */
1964 0, /* tp_descr_set */
1965 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001966 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001967 PyType_GenericAlloc, /* tp_alloc */
1968 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001969 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001970};
1971
1972/* frozenset object ********************************************************/
1973
1974
1975static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001976 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001977 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001978 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001979 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001980 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001981 difference_doc},
1982 {"intersection",(PyCFunction)set_intersection, METH_O,
1983 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001984 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001985 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001986 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001987 issuperset_doc},
1988 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1989 reduce_doc},
1990 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1991 symmetric_difference_doc},
1992 {"union", (PyCFunction)set_union, METH_O,
1993 union_doc},
1994 {NULL, NULL} /* sentinel */
1995};
1996
1997static PyNumberMethods frozenset_as_number = {
1998 0, /*nb_add*/
1999 (binaryfunc)set_sub, /*nb_subtract*/
2000 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002001 0, /*nb_remainder*/
2002 0, /*nb_divmod*/
2003 0, /*nb_power*/
2004 0, /*nb_negative*/
2005 0, /*nb_positive*/
2006 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002007 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002008 0, /*nb_invert*/
2009 0, /*nb_lshift*/
2010 0, /*nb_rshift*/
2011 (binaryfunc)set_and, /*nb_and*/
2012 (binaryfunc)set_xor, /*nb_xor*/
2013 (binaryfunc)set_or, /*nb_or*/
2014};
2015
2016PyDoc_STRVAR(frozenset_doc,
2017"frozenset(iterable) --> frozenset object\n\
2018\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002019Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002020
2021PyTypeObject PyFrozenSet_Type = {
2022 PyObject_HEAD_INIT(&PyType_Type)
2023 0, /* ob_size */
2024 "frozenset", /* tp_name */
2025 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002026 0, /* tp_itemsize */
2027 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002028 (destructor)set_dealloc, /* tp_dealloc */
2029 (printfunc)set_tp_print, /* tp_print */
2030 0, /* tp_getattr */
2031 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002032 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002033 (reprfunc)set_repr, /* tp_repr */
2034 &frozenset_as_number, /* tp_as_number */
2035 &set_as_sequence, /* tp_as_sequence */
2036 0, /* tp_as_mapping */
2037 frozenset_hash, /* tp_hash */
2038 0, /* tp_call */
2039 0, /* tp_str */
2040 PyObject_GenericGetAttr, /* tp_getattro */
2041 0, /* tp_setattro */
2042 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002043 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002044 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002045 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002046 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002047 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002048 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002049 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002050 (getiterfunc)set_iter, /* tp_iter */
2051 0, /* tp_iternext */
2052 frozenset_methods, /* tp_methods */
2053 0, /* tp_members */
2054 0, /* tp_getset */
2055 0, /* tp_base */
2056 0, /* tp_dict */
2057 0, /* tp_descr_get */
2058 0, /* tp_descr_set */
2059 0, /* tp_dictoffset */
2060 0, /* tp_init */
2061 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002062 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002063 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002064};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002065
2066
2067/***** C API functions *************************************************/
2068
2069PyObject *
2070PySet_New(PyObject *iterable)
2071{
2072 return make_new_set(&PySet_Type, iterable);
2073}
2074
2075PyObject *
2076PyFrozenSet_New(PyObject *iterable)
2077{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002078 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002079
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002080 if (iterable == NULL)
2081 args = PyTuple_New(0);
2082 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002083 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002084 if (args == NULL)
2085 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002086 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002087 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002088 return result;
2089}
2090
Neal Norwitz8c49c822006-03-04 18:41:19 +00002091Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002092PySet_Size(PyObject *anyset)
2093{
2094 if (!PyAnySet_Check(anyset)) {
2095 PyErr_BadInternalCall();
2096 return -1;
2097 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002098 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002099}
2100
2101int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002102PySet_Clear(PyObject *set)
2103{
2104 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2105 PyErr_BadInternalCall();
2106 return -1;
2107 }
2108 return set_clear_internal((PySetObject *)set);
2109}
2110
2111int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002112PySet_Contains(PyObject *anyset, PyObject *key)
2113{
2114 if (!PyAnySet_Check(anyset)) {
2115 PyErr_BadInternalCall();
2116 return -1;
2117 }
2118 return set_contains_key((PySetObject *)anyset, key);
2119}
2120
2121int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002122PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002123{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002124 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002125 PyErr_BadInternalCall();
2126 return -1;
2127 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002128 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002129}
2130
2131int
2132PySet_Add(PyObject *set, PyObject *key)
2133{
2134 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2135 PyErr_BadInternalCall();
2136 return -1;
2137 }
2138 return set_add_key((PySetObject *)set, key);
2139}
2140
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002141int
2142_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2143{
2144 setentry *entry_ptr;
2145
2146 if (!PyAnySet_Check(set)) {
2147 PyErr_BadInternalCall();
2148 return -1;
2149 }
2150 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2151 return 0;
2152 *entry = entry_ptr->key;
2153 return 1;
2154}
2155
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002156PyObject *
2157PySet_Pop(PyObject *set)
2158{
2159 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2160 PyErr_BadInternalCall();
2161 return NULL;
2162 }
2163 return set_pop((PySetObject *)set);
2164}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002165
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002166int
2167_PySet_Update(PyObject *set, PyObject *iterable)
2168{
2169 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2170 PyErr_BadInternalCall();
2171 return -1;
2172 }
2173 return set_update_internal((PySetObject *)set, iterable);
2174}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002175
2176#ifdef Py_DEBUG
2177
2178/* Test code to be called with any three element set.
2179 Returns True and original set is restored. */
2180
2181#define assertRaises(call_return_value, exception) \
2182 do { \
2183 assert(call_return_value); \
2184 assert(PyErr_ExceptionMatches(exception)); \
2185 PyErr_Clear(); \
2186 } while(0)
2187
2188static PyObject *
2189test_c_api(PySetObject *so)
2190{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002191 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002192 char *s;
2193 Py_ssize_t i;
2194 PyObject *elem, *dup, *t, *f, *dup2;
2195 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002196
2197 /* Verify preconditions and exercise type/size checks */
2198 assert(PyAnySet_Check(ob));
2199 assert(PyAnySet_CheckExact(ob));
2200 assert(!PyFrozenSet_CheckExact(ob));
2201 assert(PySet_Size(ob) == 3);
2202 assert(PySet_GET_SIZE(ob) == 3);
2203
2204 /* Raise TypeError for non-iterable constructor arguments */
2205 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2206 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2207
2208 /* Raise TypeError for unhashable key */
2209 dup = PySet_New(ob);
2210 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2211 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2212 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2213
2214 /* Exercise successful pop, contains, add, and discard */
2215 elem = PySet_Pop(ob);
2216 assert(PySet_Contains(ob, elem) == 0);
2217 assert(PySet_GET_SIZE(ob) == 2);
2218 assert(PySet_Add(ob, elem) == 0);
2219 assert(PySet_Contains(ob, elem) == 1);
2220 assert(PySet_GET_SIZE(ob) == 3);
2221 assert(PySet_Discard(ob, elem) == 1);
2222 assert(PySet_GET_SIZE(ob) == 2);
2223 assert(PySet_Discard(ob, elem) == 0);
2224 assert(PySet_GET_SIZE(ob) == 2);
2225
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002226 /* Exercise clear */
2227 dup2 = PySet_New(dup);
2228 assert(PySet_Clear(dup2) == 0);
2229 assert(PySet_Size(dup2) == 0);
2230 Py_DECREF(dup2);
2231
2232 /* Raise SystemError on clear or update of frozen set */
2233 f = PyFrozenSet_New(dup);
2234 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2235 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2236 Py_DECREF(f);
2237
2238 /* Exercise direct iteration */
2239 i = 0, count = 0;
2240 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2241 s = PyString_AsString(elem);
2242 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2243 count++;
2244 }
2245 assert(count == 3);
2246
2247 /* Exercise updates */
2248 dup2 = PySet_New(NULL);
2249 assert(_PySet_Update(dup2, dup) == 0);
2250 assert(PySet_Size(dup2) == 3);
2251 assert(_PySet_Update(dup2, dup) == 0);
2252 assert(PySet_Size(dup2) == 3);
2253 Py_DECREF(dup2);
2254
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002255 /* Raise SystemError when self argument is not a set or frozenset. */
2256 t = PyTuple_New(0);
2257 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2258 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2259 Py_DECREF(t);
2260
2261 /* Raise SystemError when self argument is not a set. */
2262 f = PyFrozenSet_New(dup);
2263 assert(PySet_Size(f) == 3);
2264 assert(PyFrozenSet_CheckExact(f));
2265 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2266 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2267 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2268 Py_DECREF(f);
2269
2270 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002271 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2272 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002273 assert(PySet_GET_SIZE(ob) == 0);
2274 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2275
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002276 /* Restore the set from the copy using the PyNumber API */
2277 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2278 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002279
2280 /* Verify constructors accept NULL arguments */
2281 f = PySet_New(NULL);
2282 assert(f != NULL);
2283 assert(PySet_GET_SIZE(f) == 0);
2284 Py_DECREF(f);
2285 f = PyFrozenSet_New(NULL);
2286 assert(f != NULL);
2287 assert(PyFrozenSet_CheckExact(f));
2288 assert(PySet_GET_SIZE(f) == 0);
2289 Py_DECREF(f);
2290
2291 Py_DECREF(elem);
2292 Py_DECREF(dup);
2293 Py_RETURN_TRUE;
2294}
2295
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002296#undef assertRaises
2297
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002298#endif