blob: f6ea441785b4147e3108ff7d43756c8b48cb7e53 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00006 Copyright (c) 2003-2007 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
Thomas Wouters89f507f2006-12-13 04:49:30 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
19 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
25}
26
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000027/* This must be >= 1. */
28#define PERTURB_SHIFT 5
29
30/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000031static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033#ifdef Py_REF_DEBUG
34PyObject *
35_PySet_Dummy(void)
36{
37 return dummy;
38}
39#endif
40
Raymond Hettingerbc841a12005-08-07 13:02:53 +000041#define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
44 (so)->hash = -1; \
45 } while(0)
46
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047#define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000050 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051 } while(0)
52
Raymond Hettingerbc841a12005-08-07 13:02:53 +000053/* Reuse scheme to save calls to malloc, free, and memset */
54#define MAXFREESETS 80
55static PySetObject *free_sets[MAXFREESETS];
56static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057
58/*
59The basic lookup function used by all operations.
60This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
61Open addressing is preferred over chaining since the link overhead for
62chaining would be substantial (100% with typical malloc overhead).
63
64The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000065probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000066
67All arithmetic on hash should ignore overflow.
68
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000069Unlike the dictionary implementation, the lookkey functions can return
70NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000071*/
72
73static setentry *
74set_lookkey(PySetObject *so, PyObject *key, register long hash)
75{
Martin v. Löwis18e16552006-02-15 17:27:45 +000076 register Py_ssize_t i;
77 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000078 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000080 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000081 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000083 PyObject *startkey;
84
85 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000086 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000087 if (entry->key == NULL || entry->key == key)
88 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000089
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000090 if (entry->key == dummy)
91 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000092 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000093 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000095 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
96 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000097 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +000098 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000099 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000100 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 }
102 else {
103 /* The compare did major nasty stuff to the
104 * set: start over.
105 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000106 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000107 }
108 }
109 freeslot = NULL;
110 }
111
112 /* In the loop, key == dummy is by far (factor of 100s) the
113 least likely outcome, so test for that last. */
114 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
115 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000116 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000117 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000118 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000119 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 break;
121 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000122 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000123 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000124 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
127 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000128 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000129 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130 if (cmp > 0)
131 break;
132 }
133 else {
134 /* The compare did major nasty stuff to the
135 * set: start over.
136 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000137 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138 }
139 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000140 else if (entry->key == dummy && freeslot == NULL)
141 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000142 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000143 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000144}
145
146/*
147 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000148 * This means we can always use _PyString_Eq directly and not have to check to
149 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150 */
151static setentry *
152set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
153{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000154 register Py_ssize_t i;
155 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000156 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000158 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000159 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000160
161 /* Make sure this function doesn't have to handle non-string keys,
162 including subclasses of str; e.g., one reason to subclass
163 strings is to override __eq__, and for speed we don't cater to
164 that here. */
165 if (!PyString_CheckExact(key)) {
166 so->lookup = set_lookkey;
167 return set_lookkey(so, key, hash);
168 }
169 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000170 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000171 if (entry->key == NULL || entry->key == key)
172 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000173 if (entry->key == dummy)
174 freeslot = entry;
175 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000176 if (entry->hash == hash && _PyString_Eq(entry->key, key))
177 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000178 freeslot = NULL;
179 }
180
181 /* In the loop, key == dummy is by far (factor of 100s) the
182 least likely outcome, so test for that last. */
183 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
184 i = (i << 2) + i + perturb + 1;
185 entry = &table[i & mask];
186 if (entry->key == NULL)
187 return freeslot == NULL ? entry : freeslot;
188 if (entry->key == key
189 || (entry->hash == hash
190 && entry->key != dummy
191 && _PyString_Eq(entry->key, key)))
192 return entry;
193 if (entry->key == dummy && freeslot == NULL)
194 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000195 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000196 assert(0); /* NOT REACHED */
197 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000198}
199
200/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000201Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000202Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000203Eats a reference to key.
204*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000205static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206set_insert_key(register PySetObject *so, PyObject *key, long hash)
207{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000208 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
210
211 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000212 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213 if (entry == NULL)
214 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000215 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000216 /* UNUSED */
217 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000218 entry->key = key;
219 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000221 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 entry->key = key;
224 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000225 so->used++;
226 Py_DECREF(dummy);
227 } else {
228 /* ACTIVE */
229 Py_DECREF(key);
230 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000231 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000232}
233
234/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000235Internal routine used by set_table_resize() to insert an item which is
236known to be absent from the set. This routine also assumes that
237the set contains no deleted entries. Besides the performance benefit,
238using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
239Note that no refcounts are changed by this routine; if needed, the caller
240is responsible for incref'ing `key`.
241*/
242static void
243set_insert_clean(register PySetObject *so, PyObject *key, long hash)
244{
245 register size_t i;
246 register size_t perturb;
247 register size_t mask = (size_t)so->mask;
248 setentry *table = so->table;
249 register setentry *entry;
250
251 i = hash & mask;
252 entry = &table[i];
253 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
254 i = (i << 2) + i + perturb + 1;
255 entry = &table[i & mask];
256 }
257 so->fill++;
258 entry->key = key;
259 entry->hash = hash;
260 so->used++;
261}
262
263/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000264Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000265keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266actually be smaller than the old one.
267*/
268static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000269set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000272 setentry *oldtable, *newtable, *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274 int is_oldtable_malloced;
275 setentry small_copy[PySet_MINSIZE];
276
277 assert(minused >= 0);
278
279 /* Find the smallest table size > minused. */
280 for (newsize = PySet_MINSIZE;
281 newsize <= minused && newsize > 0;
282 newsize <<= 1)
283 ;
284 if (newsize <= 0) {
285 PyErr_NoMemory();
286 return -1;
287 }
288
289 /* Get space for a new table. */
290 oldtable = so->table;
291 assert(oldtable != NULL);
292 is_oldtable_malloced = oldtable != so->smalltable;
293
294 if (newsize == PySet_MINSIZE) {
295 /* A large table is shrinking, or we can't get any smaller. */
296 newtable = so->smalltable;
297 if (newtable == oldtable) {
298 if (so->fill == so->used) {
299 /* No dummies, so no point doing anything. */
300 return 0;
301 }
302 /* We're not going to resize it, but rebuild the
303 table anyway to purge old dummy entries.
304 Subtle: This is *necessary* if fill==size,
305 as set_lookkey needs at least one virgin slot to
306 terminate failing searches. If fill < size, it's
307 merely desirable, as dummies slow searches. */
308 assert(so->fill > so->used);
309 memcpy(small_copy, oldtable, sizeof(small_copy));
310 oldtable = small_copy;
311 }
312 }
313 else {
314 newtable = PyMem_NEW(setentry, newsize);
315 if (newtable == NULL) {
316 PyErr_NoMemory();
317 return -1;
318 }
319 }
320
321 /* Make the set empty, using the new table. */
322 assert(newtable != oldtable);
323 so->table = newtable;
324 so->mask = newsize - 1;
325 memset(newtable, 0, sizeof(setentry) * newsize);
326 so->used = 0;
327 i = so->fill;
328 so->fill = 0;
329
330 /* Copy the data over; this is refcount-neutral for active entries;
331 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000332 for (entry = oldtable; i > 0; entry++) {
333 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334 /* UNUSED */
335 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000336 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337 /* DUMMY */
338 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000339 assert(entry->key == dummy);
340 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341 } else {
342 /* ACTIVE */
343 --i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000344 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345 }
346 }
347
348 if (is_oldtable_malloced)
349 PyMem_DEL(oldtable);
350 return 0;
351}
352
Raymond Hettingerc991db22005-08-11 07:58:45 +0000353/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
354
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356set_add_entry(register PySetObject *so, setentry *entry)
357{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000358 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000359
360 assert(so->fill <= so->mask); /* at least one empty slot */
361 n_used = so->used;
362 Py_INCREF(entry->key);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000363 if (set_insert_key(so, entry->key, entry->hash) == -1) {
364 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000365 return -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000367 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
368 return 0;
369 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
370}
371
372static int
373set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000374{
375 register long hash;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000376 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377
Raymond Hettingerc991db22005-08-11 07:58:45 +0000378 if (!PyString_CheckExact(key) ||
379 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380 hash = PyObject_Hash(key);
381 if (hash == -1)
382 return -1;
383 }
384 assert(so->fill <= so->mask); /* at least one empty slot */
385 n_used = so->used;
386 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000387 if (set_insert_key(so, key, hash) == -1) {
388 Py_DECREF(key);
389 return -1;
390 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000391 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
392 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000393 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394}
395
396#define DISCARD_NOTFOUND 0
397#define DISCARD_FOUND 1
398
399static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000400set_discard_entry(PySetObject *so, setentry *oldentry)
401{ register setentry *entry;
402 PyObject *old_key;
403
404 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000405 if (entry == NULL)
406 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000407 if (entry->key == NULL || entry->key == dummy)
408 return DISCARD_NOTFOUND;
409 old_key = entry->key;
410 Py_INCREF(dummy);
411 entry->key = dummy;
412 so->used--;
413 Py_DECREF(old_key);
414 return DISCARD_FOUND;
415}
416
417static int
418set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000419{
420 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000421 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000422 PyObject *old_key;
423
424 assert (PyAnySet_Check(so));
425 if (!PyString_CheckExact(key) ||
426 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
427 hash = PyObject_Hash(key);
428 if (hash == -1)
429 return -1;
430 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000431 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000432 if (entry == NULL)
433 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000434 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000435 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000438 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439 so->used--;
440 Py_DECREF(old_key);
441 return DISCARD_FOUND;
442}
443
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000444static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445set_clear_internal(PySetObject *so)
446{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000447 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448 int table_is_malloced;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450 setentry small_copy[PySet_MINSIZE];
451#ifdef Py_DEBUG
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000452 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000454
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455 n = so->mask + 1;
456 i = 0;
457#endif
458
459 table = so->table;
460 assert(table != NULL);
461 table_is_malloced = table != so->smalltable;
462
463 /* This is delicate. During the process of clearing the set,
464 * decrefs can cause the set to mutate. To avoid fatal confusion
465 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000466 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467 * clearing.
468 */
469 fill = so->fill;
470 if (table_is_malloced)
471 EMPTY_TO_MINSIZE(so);
472
473 else if (fill > 0) {
474 /* It's a small table with something that needs to be cleared.
475 * Afraid the only safe way is to copy the set entries into
476 * another small table first.
477 */
478 memcpy(small_copy, table, sizeof(small_copy));
479 table = small_copy;
480 EMPTY_TO_MINSIZE(so);
481 }
482 /* else it's a small table that's already empty */
483
484 /* Now we can finally clear things. If C had refcounts, we could
485 * assert that the refcount on table is 1 now, i.e. that this function
486 * has unique access to it, so decref side-effects can't alter it.
487 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000488 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489#ifdef Py_DEBUG
490 assert(i < n);
491 ++i;
492#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000493 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000495 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496 }
497#ifdef Py_DEBUG
498 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000499 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500#endif
501 }
502
503 if (table_is_malloced)
504 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000505 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506}
507
508/*
509 * Iterate over a set table. Use like so:
510 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000512 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000513 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000514 * while (set_next(yourset, &pos, &entry)) {
515 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516 * }
517 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000518 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519 * mutates the table.
520 */
521static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000522set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000526 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000527
528 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000529 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000530 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000531 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000533 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536 if (i > mask)
537 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000538 assert(table[i].key != NULL);
539 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540 return 1;
541}
542
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000543static void
544set_dealloc(PySetObject *so)
545{
546 register setentry *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000547 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000548 PyObject_GC_UnTrack(so);
549 Py_TRASHCAN_SAFE_BEGIN(so)
550 if (so->weakreflist != NULL)
551 PyObject_ClearWeakRefs((PyObject *) so);
552
553 for (entry = so->table; fill > 0; entry++) {
554 if (entry->key) {
555 --fill;
556 Py_DECREF(entry->key);
557 }
558 }
559 if (so->table != so->smalltable)
560 PyMem_DEL(so->table);
561 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
562 free_sets[num_free_sets++] = so;
563 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000564 Py_Type(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000565 Py_TRASHCAN_SAFE_END(so)
566}
567
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568static PyObject *
569set_repr(PySetObject *so)
570{
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000571 PyObject *keys, *result=NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000572 Py_UNICODE *u;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000573 int status = Py_ReprEnter((PyObject*)so);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000574 PyObject *listrepr;
575 Py_ssize_t newsize;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000576
577 if (status != 0) {
578 if (status < 0)
579 return NULL;
Martin v. Löwis5d7428b2007-07-21 18:47:48 +0000580 return PyUnicode_FromFormat("%s(...)", Py_Type(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000581 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000582
Georg Brandlc4996ba2006-08-28 19:37:11 +0000583 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000584 if (!so->used) {
585 Py_ReprLeave((PyObject*)so);
Martin v. Löwis5d7428b2007-07-21 18:47:48 +0000586 return PyUnicode_FromFormat("%s()", Py_Type(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000587 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000588
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000589 keys = PySequence_List((PyObject *)so);
590 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000591 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000592
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000593 listrepr = PyObject_Repr(keys);
594 Py_DECREF(keys);
595 if (listrepr == NULL) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000596 Py_DECREF(keys);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000597 goto done;
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000598 }
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000599 newsize = PyUnicode_GET_SIZE(listrepr);
600 result = PyUnicode_FromUnicode(NULL, newsize);
601 if (result) {
602 u = PyUnicode_AS_UNICODE(result);
603 *u++ = '{';
604 /* Omit the brackets from the listrepr */
605 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
606 PyUnicode_GET_SIZE(listrepr)-2);
607 u += newsize-2;
608 *u++ = '}';
609 }
610 Py_DECREF(listrepr);
611 if (Py_Type(so) != &PySet_Type) {
612 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
613 Py_Type(so)->tp_name,
614 result);
615 Py_DECREF(result);
616 result = tmp;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000617 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000618done:
619 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000620 return result;
621}
622
Martin v. Löwis18e16552006-02-15 17:27:45 +0000623static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000624set_len(PyObject *so)
625{
626 return ((PySetObject *)so)->used;
627}
628
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000629static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000630set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000631{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000632 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000633 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000634 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000635
636 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000637 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000638
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000639 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000640 if (other == so || other->used == 0)
641 /* a.update(a) or a.update({}); nothing to do */
642 return 0;
643 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000644 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000645 * that there will be no (or few) overlapping keys.
646 */
647 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
648 if (set_table_resize(so, (so->used + other->used)*2) != 0)
649 return -1;
650 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000651 for (i = 0; i <= other->mask; i++) {
652 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000653 if (entry->key != NULL &&
654 entry->key != dummy) {
655 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000656 if (set_insert_key(so, entry->key, entry->hash) == -1) {
657 Py_DECREF(entry->key);
658 return -1;
659 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000660 }
661 }
662 return 0;
663}
664
665static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000666set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000667{
668 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000669 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670
671 if (!PyString_CheckExact(key) ||
672 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
673 hash = PyObject_Hash(key);
674 if (hash == -1)
675 return -1;
676 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000677 entry = (so->lookup)(so, key, hash);
678 if (entry == NULL)
679 return -1;
680 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000681 return key != NULL && key != dummy;
682}
683
Raymond Hettingerc991db22005-08-11 07:58:45 +0000684static int
685set_contains_entry(PySetObject *so, setentry *entry)
686{
687 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000688 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000689
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000690 lu_entry = (so->lookup)(so, entry->key, entry->hash);
691 if (lu_entry == NULL)
692 return -1;
693 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000694 return key != NULL && key != dummy;
695}
696
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000697static PyObject *
698set_pop(PySetObject *so)
699{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000700 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000701 register setentry *entry;
702 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000703
704 assert (PyAnySet_Check(so));
705 if (so->used == 0) {
706 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
707 return NULL;
708 }
709
710 /* Set entry to "the first" unused or dummy set entry. We abuse
711 * the hash field of slot 0 to hold a search finger:
712 * If slot 0 has a value, use slot 0.
713 * Else slot 0 is being used to hold a search finger,
714 * and we use its hash value as the first index to look.
715 */
716 entry = &so->table[0];
717 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000718 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000719 /* The hash field may be a real hash value, or it may be a
720 * legit search finger, or it may be a once-legit search
721 * finger that's out of bounds now because it wrapped around
722 * or the table shrunk -- simply make sure it's in bounds now.
723 */
724 if (i > so->mask || i < 1)
725 i = 1; /* skip slot 0 */
726 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
727 i++;
728 if (i > so->mask)
729 i = 1;
730 }
731 }
732 key = entry->key;
733 Py_INCREF(dummy);
734 entry->key = dummy;
735 so->used--;
736 so->table[0].hash = i + 1; /* next place to start */
737 return key;
738}
739
740PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
741
742static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000743set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000744{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000745 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000746 setentry *entry;
747
748 while (set_next(so, &pos, &entry))
749 Py_VISIT(entry->key);
750 return 0;
751}
752
753static long
754frozenset_hash(PyObject *self)
755{
756 PySetObject *so = (PySetObject *)self;
757 long h, hash = 1927868237L;
758 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000759 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000760
761 if (so->hash != -1)
762 return so->hash;
763
764 hash *= PySet_GET_SIZE(self) + 1;
765 while (set_next(so, &pos, &entry)) {
766 /* Work to increase the bit dispersion for closely spaced hash
767 values. The is important because some use cases have many
768 combinations of a small number of elements with nearby
769 hashes so that many distinct combinations collapse to only
770 a handful of distinct hash values. */
771 h = entry->hash;
772 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
773 }
774 hash = hash * 69069L + 907133923L;
775 if (hash == -1)
776 hash = 590923713L;
777 so->hash = hash;
778 return hash;
779}
780
Raymond Hettingera9d99362005-08-05 00:01:15 +0000781/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000782
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000783typedef struct {
784 PyObject_HEAD
785 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000786 Py_ssize_t si_used;
787 Py_ssize_t si_pos;
788 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789} setiterobject;
790
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791static void
792setiter_dealloc(setiterobject *si)
793{
794 Py_XDECREF(si->si_set);
795 PyObject_Del(si);
796}
797
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000798static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799setiter_len(setiterobject *si)
800{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000801 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000803 len = si->len;
804 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805}
806
Armin Rigof5b3e362006-02-11 21:32:43 +0000807PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000808
809static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000810 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000811 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812};
813
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000814static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815{
816 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000817 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000818 register setentry *entry;
819 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000820
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000821 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000823 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000825 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000826 PyErr_SetString(PyExc_RuntimeError,
827 "Set changed size during iteration");
828 si->si_used = -1; /* Make this state sticky */
829 return NULL;
830 }
831
832 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000833 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000834 entry = so->table;
835 mask = so->mask;
836 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837 i++;
838 si->si_pos = i+1;
839 if (i > mask)
840 goto fail;
841 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000842 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000843 Py_INCREF(key);
844 return key;
845
846fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000847 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848 si->si_set = NULL;
849 return NULL;
850}
851
Hye-Shik Change2956762005-08-01 05:26:41 +0000852static PyTypeObject PySetIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000853 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000854 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000855 sizeof(setiterobject), /* tp_basicsize */
856 0, /* tp_itemsize */
857 /* methods */
858 (destructor)setiter_dealloc, /* tp_dealloc */
859 0, /* tp_print */
860 0, /* tp_getattr */
861 0, /* tp_setattr */
862 0, /* tp_compare */
863 0, /* tp_repr */
864 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000865 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000866 0, /* tp_as_mapping */
867 0, /* tp_hash */
868 0, /* tp_call */
869 0, /* tp_str */
870 PyObject_GenericGetAttr, /* tp_getattro */
871 0, /* tp_setattro */
872 0, /* tp_as_buffer */
873 Py_TPFLAGS_DEFAULT, /* tp_flags */
874 0, /* tp_doc */
875 0, /* tp_traverse */
876 0, /* tp_clear */
877 0, /* tp_richcompare */
878 0, /* tp_weaklistoffset */
879 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000880 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000881 setiter_methods, /* tp_methods */
882 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000883};
884
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000885static PyObject *
886set_iter(PySetObject *so)
887{
888 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
889 if (si == NULL)
890 return NULL;
891 Py_INCREF(so);
892 si->si_set = so;
893 si->si_used = so->used;
894 si->si_pos = 0;
895 si->len = so->used;
896 return (PyObject *)si;
897}
898
Raymond Hettingerd7946662005-08-01 21:39:29 +0000899static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000900set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000901{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000902 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000903
Thomas Wouterscf297e42007-02-23 15:07:44 +0000904 if (PyAnySet_CheckExact(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000905 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000906
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000907 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000908 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000909 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000910 long hash;
911 Py_ssize_t dictsize = PyDict_Size(other);
912
913 /* Do one big resize at the start, rather than
914 * incrementally resizing as we insert new keys. Expect
915 * that there will be no (or few) overlapping keys.
916 */
917 if (dictsize == -1)
918 return -1;
919 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
920 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
921 return -1;
922 }
923 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
924 setentry an_entry;
925
926 an_entry.hash = hash;
927 an_entry.key = key;
928 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000929 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000930 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000931 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000932 }
933
Raymond Hettingera38123e2003-11-24 22:18:49 +0000934 it = PyObject_GetIter(other);
935 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000936 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000937
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000938 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000939 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000940 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000941 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000943 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000944 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000945 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000946 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000947 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000948 return -1;
949 return 0;
950}
951
952static PyObject *
953set_update(PySetObject *so, PyObject *other)
954{
955 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000956 return NULL;
957 Py_RETURN_NONE;
958}
959
960PyDoc_STRVAR(update_doc,
961"Update a set with the union of itself and another.");
962
963static PyObject *
964make_new_set(PyTypeObject *type, PyObject *iterable)
965{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000966 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000967
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000968 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000969 dummy = PyUnicode_FromString("<dummy key>");
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000970 if (dummy == NULL)
971 return NULL;
972 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000973
974 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000975 if (num_free_sets &&
976 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
977 so = free_sets[--num_free_sets];
978 assert (so != NULL && PyAnySet_CheckExact(so));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000979 Py_Type(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000980 _Py_NewReference((PyObject *)so);
981 EMPTY_TO_MINSIZE(so);
982 PyObject_GC_Track(so);
983 } else {
984 so = (PySetObject *)type->tp_alloc(type, 0);
985 if (so == NULL)
986 return NULL;
987 /* tp_alloc has already zeroed the structure */
988 assert(so->table == NULL && so->fill == 0 && so->used == 0);
989 INIT_NONZERO_SET_SLOTS(so);
990 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000991
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000992 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000993 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000994
Raymond Hettingera38123e2003-11-24 22:18:49 +0000995 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000996 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000997 Py_DECREF(so);
998 return NULL;
999 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001000 }
1001
Raymond Hettingera690a992003-11-16 16:17:49 +00001002 return (PyObject *)so;
1003}
1004
Raymond Hettingerd7946662005-08-01 21:39:29 +00001005/* The empty frozenset is a singleton */
1006static PyObject *emptyfrozenset = NULL;
1007
Raymond Hettingera690a992003-11-16 16:17:49 +00001008static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001009frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001010{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001011 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001012
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001013 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001014 return NULL;
1015
Raymond Hettingera690a992003-11-16 16:17:49 +00001016 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1017 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001018
1019 if (type != &PyFrozenSet_Type)
1020 return make_new_set(type, iterable);
1021
1022 if (iterable != NULL) {
1023 /* frozenset(f) is idempotent */
1024 if (PyFrozenSet_CheckExact(iterable)) {
1025 Py_INCREF(iterable);
1026 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001027 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001028 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001029 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001030 return result;
1031 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001032 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001033 /* The empty frozenset is a singleton */
1034 if (emptyfrozenset == NULL)
1035 emptyfrozenset = make_new_set(type, NULL);
1036 Py_XINCREF(emptyfrozenset);
1037 return emptyfrozenset;
1038}
1039
1040void
1041PySet_Fini(void)
1042{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001043 PySetObject *so;
1044
1045 while (num_free_sets) {
1046 num_free_sets--;
1047 so = free_sets[num_free_sets];
1048 PyObject_GC_Del(so);
1049 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001050 Py_CLEAR(dummy);
1051 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001052}
1053
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001054static PyObject *
1055set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1056{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001057 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001058 return NULL;
1059
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001060 return make_new_set(type, NULL);
1061}
1062
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001063/* set_swap_bodies() switches the contents of any two sets by moving their
1064 internal data pointers and, if needed, copying the internal smalltables.
1065 Semantically equivalent to:
1066
1067 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1068
1069 The function always succeeds and it leaves both objects in a stable state.
1070 Useful for creating temporary frozensets from sets for membership testing
1071 in __contains__(), discard(), and remove(). Also useful for operations
1072 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001073 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001074*/
1075
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001076static void
1077set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001078{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001079 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001080 setentry *u;
1081 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1082 setentry tab[PySet_MINSIZE];
1083 long h;
1084
1085 t = a->fill; a->fill = b->fill; b->fill = t;
1086 t = a->used; a->used = b->used; b->used = t;
1087 t = a->mask; a->mask = b->mask; b->mask = t;
1088
1089 u = a->table;
1090 if (a->table == a->smalltable)
1091 u = b->smalltable;
1092 a->table = b->table;
1093 if (b->table == b->smalltable)
1094 a->table = a->smalltable;
1095 b->table = u;
1096
1097 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1098
1099 if (a->table == a->smalltable || b->table == b->smalltable) {
1100 memcpy(tab, a->smalltable, sizeof(tab));
1101 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1102 memcpy(b->smalltable, tab, sizeof(tab));
1103 }
1104
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001105 if (PyType_IsSubtype(Py_Type(a), &PyFrozenSet_Type) &&
1106 PyType_IsSubtype(Py_Type(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001107 h = a->hash; a->hash = b->hash; b->hash = h;
1108 } else {
1109 a->hash = -1;
1110 b->hash = -1;
1111 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001112}
1113
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001114static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001115set_copy(PySetObject *so)
1116{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001117 return make_new_set(Py_Type(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001118}
1119
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001120static PyObject *
1121frozenset_copy(PySetObject *so)
1122{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001123 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001124 Py_INCREF(so);
1125 return (PyObject *)so;
1126 }
1127 return set_copy(so);
1128}
1129
Raymond Hettingera690a992003-11-16 16:17:49 +00001130PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1131
1132static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001133set_clear(PySetObject *so)
1134{
1135 set_clear_internal(so);
1136 Py_RETURN_NONE;
1137}
1138
1139PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1140
1141static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001142set_union(PySetObject *so, PyObject *other)
1143{
1144 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001145
1146 result = (PySetObject *)set_copy(so);
1147 if (result == NULL)
1148 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001149 if ((PyObject *)so == other)
1150 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001151 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001152 Py_DECREF(result);
1153 return NULL;
1154 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001155 return (PyObject *)result;
1156}
1157
1158PyDoc_STRVAR(union_doc,
1159 "Return the union of two sets as a new set.\n\
1160\n\
1161(i.e. all elements that are in either set.)");
1162
1163static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001164set_or(PySetObject *so, PyObject *other)
1165{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001166 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001167 Py_INCREF(Py_NotImplemented);
1168 return Py_NotImplemented;
1169 }
1170 return set_union(so, other);
1171}
1172
1173static PyObject *
1174set_ior(PySetObject *so, PyObject *other)
1175{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001176 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001177 Py_INCREF(Py_NotImplemented);
1178 return Py_NotImplemented;
1179 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001180 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001181 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001182 Py_INCREF(so);
1183 return (PyObject *)so;
1184}
1185
1186static PyObject *
1187set_intersection(PySetObject *so, PyObject *other)
1188{
1189 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001190 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001191
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001192 if ((PyObject *)so == other)
1193 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001194
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001195 result = (PySetObject *)make_new_set(Py_Type(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001196 if (result == NULL)
1197 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001198
Thomas Wouterscf297e42007-02-23 15:07:44 +00001199 if (PyAnySet_CheckExact(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001200 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001201 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001202
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001203 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001204 tmp = (PyObject *)so;
1205 so = (PySetObject *)other;
1206 other = tmp;
1207 }
1208
Raymond Hettingerc991db22005-08-11 07:58:45 +00001209 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001210 int rv = set_contains_entry(so, entry);
1211 if (rv == -1) {
1212 Py_DECREF(result);
1213 return NULL;
1214 }
1215 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001216 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001217 Py_DECREF(result);
1218 return NULL;
1219 }
1220 }
1221 }
1222 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001223 }
1224
Raymond Hettingera690a992003-11-16 16:17:49 +00001225 it = PyObject_GetIter(other);
1226 if (it == NULL) {
1227 Py_DECREF(result);
1228 return NULL;
1229 }
1230
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001231 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001232 int rv;
1233 setentry entry;
1234 long hash = PyObject_Hash(key);
1235
1236 if (hash == -1) {
1237 Py_DECREF(it);
1238 Py_DECREF(result);
1239 Py_DECREF(key);
1240 return NULL;
1241 }
1242 entry.hash = hash;
1243 entry.key = key;
1244 rv = set_contains_entry(so, &entry);
1245 if (rv == -1) {
1246 Py_DECREF(it);
1247 Py_DECREF(result);
1248 Py_DECREF(key);
1249 return NULL;
1250 }
1251 if (rv) {
1252 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001253 Py_DECREF(it);
1254 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001255 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001256 return NULL;
1257 }
1258 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001259 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001260 }
1261 Py_DECREF(it);
1262 if (PyErr_Occurred()) {
1263 Py_DECREF(result);
1264 return NULL;
1265 }
1266 return (PyObject *)result;
1267}
1268
1269PyDoc_STRVAR(intersection_doc,
1270"Return the intersection of two sets as a new set.\n\
1271\n\
1272(i.e. all elements that are in both sets.)");
1273
1274static PyObject *
1275set_intersection_update(PySetObject *so, PyObject *other)
1276{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001277 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001278
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001279 tmp = set_intersection(so, other);
1280 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001281 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001282 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001283 Py_DECREF(tmp);
1284 Py_RETURN_NONE;
1285}
1286
1287PyDoc_STRVAR(intersection_update_doc,
1288"Update a set with the intersection of itself and another.");
1289
1290static PyObject *
1291set_and(PySetObject *so, PyObject *other)
1292{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001293 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001294 Py_INCREF(Py_NotImplemented);
1295 return Py_NotImplemented;
1296 }
1297 return set_intersection(so, other);
1298}
1299
1300static PyObject *
1301set_iand(PySetObject *so, PyObject *other)
1302{
1303 PyObject *result;
1304
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001305 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001306 Py_INCREF(Py_NotImplemented);
1307 return Py_NotImplemented;
1308 }
1309 result = set_intersection_update(so, other);
1310 if (result == NULL)
1311 return NULL;
1312 Py_DECREF(result);
1313 Py_INCREF(so);
1314 return (PyObject *)so;
1315}
1316
Guido van Rossum58da9312007-11-10 23:39:45 +00001317static PyObject *
1318set_isdisjoint(PySetObject *so, PyObject *other)
1319{
1320 PyObject *key, *it, *tmp;
1321
1322 if ((PyObject *)so == other) {
1323 if (PySet_GET_SIZE(so) == 0)
1324 Py_RETURN_TRUE;
1325 else
1326 Py_RETURN_FALSE;
1327 }
1328
1329 if (PyAnySet_CheckExact(other)) {
1330 Py_ssize_t pos = 0;
1331 setentry *entry;
1332
1333 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1334 tmp = (PyObject *)so;
1335 so = (PySetObject *)other;
1336 other = tmp;
1337 }
1338 while (set_next((PySetObject *)other, &pos, &entry)) {
1339 int rv = set_contains_entry(so, entry);
1340 if (rv == -1)
1341 return NULL;
1342 if (rv)
1343 Py_RETURN_FALSE;
1344 }
1345 Py_RETURN_TRUE;
1346 }
1347
1348 it = PyObject_GetIter(other);
1349 if (it == NULL)
1350 return NULL;
1351
1352 while ((key = PyIter_Next(it)) != NULL) {
1353 int rv;
1354 setentry entry;
1355 long hash = PyObject_Hash(key);
1356
1357 if (hash == -1) {
1358 Py_DECREF(key);
1359 Py_DECREF(it);
1360 return NULL;
1361 }
1362 entry.hash = hash;
1363 entry.key = key;
1364 rv = set_contains_entry(so, &entry);
1365 Py_DECREF(key);
1366 if (rv == -1) {
1367 Py_DECREF(it);
1368 return NULL;
1369 }
1370 if (rv) {
1371 Py_DECREF(it);
1372 Py_RETURN_FALSE;
1373 }
1374 }
1375 Py_DECREF(it);
1376 if (PyErr_Occurred())
1377 return NULL;
1378 Py_RETURN_TRUE;
1379}
1380
1381PyDoc_STRVAR(isdisjoint_doc,
1382"Return True if two sets have a null intersection.");
1383
Neal Norwitz6576bd82005-11-13 18:41:28 +00001384static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001385set_difference_update_internal(PySetObject *so, PyObject *other)
1386{
1387 if ((PyObject *)so == other)
1388 return set_clear_internal(so);
1389
Thomas Wouterscf297e42007-02-23 15:07:44 +00001390 if (PyAnySet_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001391 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001392 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001393
1394 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001395 if (set_discard_entry(so, entry) == -1)
1396 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001397 } else {
1398 PyObject *key, *it;
1399 it = PyObject_GetIter(other);
1400 if (it == NULL)
1401 return -1;
1402
1403 while ((key = PyIter_Next(it)) != NULL) {
1404 if (set_discard_key(so, key) == -1) {
1405 Py_DECREF(it);
1406 Py_DECREF(key);
1407 return -1;
1408 }
1409 Py_DECREF(key);
1410 }
1411 Py_DECREF(it);
1412 if (PyErr_Occurred())
1413 return -1;
1414 }
1415 /* If more than 1/5 are dummies, then resize them away. */
1416 if ((so->fill - so->used) * 5 < so->mask)
1417 return 0;
1418 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1419}
1420
Raymond Hettingera690a992003-11-16 16:17:49 +00001421static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001422set_difference_update(PySetObject *so, PyObject *other)
1423{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001424 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001425 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001426 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001427}
1428
1429PyDoc_STRVAR(difference_update_doc,
1430"Remove all elements of another set from this set.");
1431
1432static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001433set_difference(PySetObject *so, PyObject *other)
1434{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001435 PyObject *result;
1436 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001437 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001438
Thomas Wouterscf297e42007-02-23 15:07:44 +00001439 if (!PyAnySet_CheckExact(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001440 result = set_copy(so);
1441 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001442 return NULL;
1443 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001444 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001445 Py_DECREF(result);
1446 return NULL;
1447 }
1448
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001449 result = make_new_set(Py_Type(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001450 if (result == NULL)
1451 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001452
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001453 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001454 while (set_next(so, &pos, &entry)) {
1455 setentry entrycopy;
1456 entrycopy.hash = entry->hash;
1457 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001458 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001459 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1460 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001461 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001462 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001463 }
1464 }
1465 return result;
1466 }
1467
Raymond Hettingerc991db22005-08-11 07:58:45 +00001468 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001469 int rv = set_contains_entry((PySetObject *)other, entry);
1470 if (rv == -1) {
1471 Py_DECREF(result);
1472 return NULL;
1473 }
1474 if (!rv) {
1475 if (set_add_entry((PySetObject *)result, entry) == -1) {
1476 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001477 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001478 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001479 }
1480 }
1481 return result;
1482}
1483
1484PyDoc_STRVAR(difference_doc,
1485"Return the difference of two sets as a new set.\n\
1486\n\
1487(i.e. all elements that are in this set but not the other.)");
1488static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001489set_sub(PySetObject *so, PyObject *other)
1490{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001491 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001492 Py_INCREF(Py_NotImplemented);
1493 return Py_NotImplemented;
1494 }
1495 return set_difference(so, other);
1496}
1497
1498static PyObject *
1499set_isub(PySetObject *so, PyObject *other)
1500{
1501 PyObject *result;
1502
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001503 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001504 Py_INCREF(Py_NotImplemented);
1505 return Py_NotImplemented;
1506 }
1507 result = set_difference_update(so, other);
1508 if (result == NULL)
1509 return NULL;
1510 Py_DECREF(result);
1511 Py_INCREF(so);
1512 return (PyObject *)so;
1513}
1514
1515static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001516set_symmetric_difference_update(PySetObject *so, PyObject *other)
1517{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001518 PySetObject *otherset;
1519 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001520 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001521 setentry *entry;
1522
1523 if ((PyObject *)so == other)
1524 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001525
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001526 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001527 PyObject *value;
1528 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001529 long hash;
1530 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001531 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001532
Thomas Wouters89f507f2006-12-13 04:49:30 +00001533 an_entry.hash = hash;
1534 an_entry.key = key;
1535 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001536 if (rv == -1)
1537 return NULL;
1538 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001539 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001540 return NULL;
1541 }
1542 }
1543 Py_RETURN_NONE;
1544 }
1545
Thomas Wouterscf297e42007-02-23 15:07:44 +00001546 if (PyAnySet_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001547 Py_INCREF(other);
1548 otherset = (PySetObject *)other;
1549 } else {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001550 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001551 if (otherset == NULL)
1552 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001553 }
1554
Raymond Hettingerc991db22005-08-11 07:58:45 +00001555 while (set_next(otherset, &pos, &entry)) {
1556 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001557 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001558 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001559 return NULL;
1560 }
1561 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001562 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001563 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001564 return NULL;
1565 }
1566 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001567 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001568 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001569 Py_RETURN_NONE;
1570}
1571
1572PyDoc_STRVAR(symmetric_difference_update_doc,
1573"Update a set with the symmetric difference of itself and another.");
1574
1575static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001576set_symmetric_difference(PySetObject *so, PyObject *other)
1577{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001578 PyObject *rv;
1579 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001580
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001581 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001582 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001583 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001584 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1585 if (rv == NULL)
1586 return NULL;
1587 Py_DECREF(rv);
1588 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001589}
1590
1591PyDoc_STRVAR(symmetric_difference_doc,
1592"Return the symmetric difference of two sets as a new set.\n\
1593\n\
1594(i.e. all elements that are in exactly one of the sets.)");
1595
1596static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001597set_xor(PySetObject *so, PyObject *other)
1598{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001599 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001600 Py_INCREF(Py_NotImplemented);
1601 return Py_NotImplemented;
1602 }
1603 return set_symmetric_difference(so, other);
1604}
1605
1606static PyObject *
1607set_ixor(PySetObject *so, PyObject *other)
1608{
1609 PyObject *result;
1610
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001611 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001612 Py_INCREF(Py_NotImplemented);
1613 return Py_NotImplemented;
1614 }
1615 result = set_symmetric_difference_update(so, other);
1616 if (result == NULL)
1617 return NULL;
1618 Py_DECREF(result);
1619 Py_INCREF(so);
1620 return (PyObject *)so;
1621}
1622
1623static PyObject *
1624set_issubset(PySetObject *so, PyObject *other)
1625{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001626 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001627 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001628
Thomas Wouterscf297e42007-02-23 15:07:44 +00001629 if (!PyAnySet_CheckExact(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001630 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001631 tmp = make_new_set(&PySet_Type, other);
1632 if (tmp == NULL)
1633 return NULL;
1634 result = set_issubset(so, tmp);
1635 Py_DECREF(tmp);
1636 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001637 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001638 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001639 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001640
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001641 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001642 int rv = set_contains_entry((PySetObject *)other, entry);
1643 if (rv == -1)
1644 return NULL;
1645 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001646 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001647 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001648 Py_RETURN_TRUE;
1649}
1650
1651PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1652
1653static PyObject *
1654set_issuperset(PySetObject *so, PyObject *other)
1655{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001656 PyObject *tmp, *result;
1657
Thomas Wouterscf297e42007-02-23 15:07:44 +00001658 if (!PyAnySet_CheckExact(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001659 tmp = make_new_set(&PySet_Type, other);
1660 if (tmp == NULL)
1661 return NULL;
1662 result = set_issuperset(so, tmp);
1663 Py_DECREF(tmp);
1664 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001665 }
1666 return set_issubset((PySetObject *)other, (PyObject *)so);
1667}
1668
1669PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1670
Raymond Hettingera690a992003-11-16 16:17:49 +00001671static PyObject *
1672set_richcompare(PySetObject *v, PyObject *w, int op)
1673{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001674 PyObject *r1, *r2;
1675
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001676 if(!PyAnySet_Check(w)) {
Guido van Rossum10ab4ae2007-08-23 23:57:24 +00001677 Py_INCREF(Py_NotImplemented);
1678 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001679 }
1680 switch (op) {
1681 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001682 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001683 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001684 if (v->hash != -1 &&
1685 ((PySetObject *)w)->hash != -1 &&
1686 v->hash != ((PySetObject *)w)->hash)
1687 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001688 return set_issubset(v, w);
1689 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001690 r1 = set_richcompare(v, w, Py_EQ);
1691 if (r1 == NULL)
1692 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001693 r2 = PyBool_FromLong(PyObject_Not(r1));
1694 Py_DECREF(r1);
1695 return r2;
1696 case Py_LE:
1697 return set_issubset(v, w);
1698 case Py_GE:
1699 return set_issuperset(v, w);
1700 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001701 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001702 Py_RETURN_FALSE;
1703 return set_issubset(v, w);
1704 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001705 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001706 Py_RETURN_FALSE;
1707 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001708 }
1709 Py_INCREF(Py_NotImplemented);
1710 return Py_NotImplemented;
1711}
1712
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001713static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001714set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001715{
1716 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1717 return -1;
1718}
1719
Raymond Hettingera690a992003-11-16 16:17:49 +00001720static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001721set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001722{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001723 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001724 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001725 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001726}
1727
1728PyDoc_STRVAR(add_doc,
1729"Add an element to a set.\n\
1730\n\
1731This has no effect if the element is already present.");
1732
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001733static int
1734set_contains(PySetObject *so, PyObject *key)
1735{
1736 PyObject *tmpkey;
1737 int rv;
1738
1739 rv = set_contains_key(so, key);
1740 if (rv == -1) {
1741 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1742 return -1;
1743 PyErr_Clear();
1744 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1745 if (tmpkey == NULL)
1746 return -1;
1747 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1748 rv = set_contains(so, tmpkey);
1749 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1750 Py_DECREF(tmpkey);
1751 }
1752 return rv;
1753}
1754
1755static PyObject *
1756set_direct_contains(PySetObject *so, PyObject *key)
1757{
1758 long result;
1759
1760 result = set_contains(so, key);
1761 if (result == -1)
1762 return NULL;
1763 return PyBool_FromLong(result);
1764}
1765
1766PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1767
Raymond Hettingera690a992003-11-16 16:17:49 +00001768static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001769set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001770{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001771 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001772 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001773
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001774 rv = set_discard_key(so, key);
1775 if (rv == -1) {
1776 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1777 return NULL;
1778 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001779 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1780 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001781 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001782 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001783 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001784 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001785 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001786 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001787 } else if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001788 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001789 return NULL;
1790 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001791 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001792}
1793
1794PyDoc_STRVAR(remove_doc,
1795"Remove an element from a set; it must be a member.\n\
1796\n\
1797If the element is not a member, raise a KeyError.");
1798
1799static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001800set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001801{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001802 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001803 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001804
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001805 rv = set_discard_key(so, key);
1806 if (rv == -1) {
1807 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1808 return NULL;
1809 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001810 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1811 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001812 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001813 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001814 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001815 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001816 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001817 return result;
1818 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001819 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001820}
1821
1822PyDoc_STRVAR(discard_doc,
1823"Remove an element from a set if it is a member.\n\
1824\n\
1825If the element is not a member, do nothing.");
1826
1827static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001828set_reduce(PySetObject *so)
1829{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001830 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001831
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001832 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001833 if (keys == NULL)
1834 goto done;
1835 args = PyTuple_Pack(1, keys);
1836 if (args == NULL)
1837 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001838 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1839 if (dict == NULL) {
1840 PyErr_Clear();
1841 dict = Py_None;
1842 Py_INCREF(dict);
1843 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001844 result = PyTuple_Pack(3, Py_Type(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001845done:
1846 Py_XDECREF(args);
1847 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001848 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001849 return result;
1850}
1851
1852PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1853
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001854static int
1855set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1856{
1857 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001858
1859 if (!PyAnySet_Check(self))
1860 return -1;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001861 if (!PyArg_UnpackTuple(args, Py_Type(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001862 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001863 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001864 self->hash = -1;
1865 if (iterable == NULL)
1866 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001867 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001868}
1869
Raymond Hettingera690a992003-11-16 16:17:49 +00001870static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001871 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001872 0, /* sq_concat */
1873 0, /* sq_repeat */
1874 0, /* sq_item */
1875 0, /* sq_slice */
1876 0, /* sq_ass_item */
1877 0, /* sq_ass_slice */
1878 (objobjproc)set_contains, /* sq_contains */
1879};
1880
1881/* set object ********************************************************/
1882
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001883#ifdef Py_DEBUG
1884static PyObject *test_c_api(PySetObject *so);
1885
1886PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1887All is well if assertions don't fail.");
1888#endif
1889
Raymond Hettingera690a992003-11-16 16:17:49 +00001890static PyMethodDef set_methods[] = {
1891 {"add", (PyCFunction)set_add, METH_O,
1892 add_doc},
1893 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1894 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001895 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001896 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001897 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1898 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001899 {"discard", (PyCFunction)set_discard, METH_O,
1900 discard_doc},
1901 {"difference", (PyCFunction)set_difference, METH_O,
1902 difference_doc},
1903 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1904 difference_update_doc},
1905 {"intersection",(PyCFunction)set_intersection, METH_O,
1906 intersection_doc},
1907 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1908 intersection_update_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00001909 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1910 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001911 {"issubset", (PyCFunction)set_issubset, METH_O,
1912 issubset_doc},
1913 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1914 issuperset_doc},
1915 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1916 pop_doc},
1917 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1918 reduce_doc},
1919 {"remove", (PyCFunction)set_remove, METH_O,
1920 remove_doc},
1921 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1922 symmetric_difference_doc},
1923 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1924 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001925#ifdef Py_DEBUG
1926 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1927 test_c_api_doc},
1928#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001929 {"union", (PyCFunction)set_union, METH_O,
1930 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001931 {"update", (PyCFunction)set_update, METH_O,
1932 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001933 {NULL, NULL} /* sentinel */
1934};
1935
1936static PyNumberMethods set_as_number = {
1937 0, /*nb_add*/
1938 (binaryfunc)set_sub, /*nb_subtract*/
1939 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001940 0, /*nb_remainder*/
1941 0, /*nb_divmod*/
1942 0, /*nb_power*/
1943 0, /*nb_negative*/
1944 0, /*nb_positive*/
1945 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001946 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001947 0, /*nb_invert*/
1948 0, /*nb_lshift*/
1949 0, /*nb_rshift*/
1950 (binaryfunc)set_and, /*nb_and*/
1951 (binaryfunc)set_xor, /*nb_xor*/
1952 (binaryfunc)set_or, /*nb_or*/
Neil Schemenauer16c70752007-09-21 20:19:23 +00001953 0, /*nb_reserved*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001954 0, /*nb_int*/
1955 0, /*nb_long*/
1956 0, /*nb_float*/
1957 0, /*nb_oct*/
1958 0, /*nb_hex*/
1959 0, /*nb_inplace_add*/
1960 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1961 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001962 0, /*nb_inplace_remainder*/
1963 0, /*nb_inplace_power*/
1964 0, /*nb_inplace_lshift*/
1965 0, /*nb_inplace_rshift*/
1966 (binaryfunc)set_iand, /*nb_inplace_and*/
1967 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1968 (binaryfunc)set_ior, /*nb_inplace_or*/
1969};
1970
1971PyDoc_STRVAR(set_doc,
1972"set(iterable) --> set object\n\
1973\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001974Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001975
1976PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001977 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001978 "set", /* tp_name */
1979 sizeof(PySetObject), /* tp_basicsize */
1980 0, /* tp_itemsize */
1981 /* methods */
1982 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001983 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00001984 0, /* tp_getattr */
1985 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001986 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001987 (reprfunc)set_repr, /* tp_repr */
1988 &set_as_number, /* tp_as_number */
1989 &set_as_sequence, /* tp_as_sequence */
1990 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001991 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001992 0, /* tp_call */
1993 0, /* tp_str */
1994 PyObject_GenericGetAttr, /* tp_getattro */
1995 0, /* tp_setattro */
1996 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001997 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001998 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001999 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002000 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002001 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002002 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002003 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002004 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002005 0, /* tp_iternext */
2006 set_methods, /* tp_methods */
2007 0, /* tp_members */
2008 0, /* tp_getset */
2009 0, /* tp_base */
2010 0, /* tp_dict */
2011 0, /* tp_descr_get */
2012 0, /* tp_descr_set */
2013 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002014 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002015 PyType_GenericAlloc, /* tp_alloc */
2016 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002017 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002018};
2019
2020/* frozenset object ********************************************************/
2021
2022
2023static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002024 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002025 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002026 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002027 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002028 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002029 difference_doc},
2030 {"intersection",(PyCFunction)set_intersection, METH_O,
2031 intersection_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002032 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2033 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002034 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002035 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002036 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002037 issuperset_doc},
2038 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2039 reduce_doc},
2040 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2041 symmetric_difference_doc},
2042 {"union", (PyCFunction)set_union, METH_O,
2043 union_doc},
2044 {NULL, NULL} /* sentinel */
2045};
2046
2047static PyNumberMethods frozenset_as_number = {
2048 0, /*nb_add*/
2049 (binaryfunc)set_sub, /*nb_subtract*/
2050 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002051 0, /*nb_remainder*/
2052 0, /*nb_divmod*/
2053 0, /*nb_power*/
2054 0, /*nb_negative*/
2055 0, /*nb_positive*/
2056 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002057 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002058 0, /*nb_invert*/
2059 0, /*nb_lshift*/
2060 0, /*nb_rshift*/
2061 (binaryfunc)set_and, /*nb_and*/
2062 (binaryfunc)set_xor, /*nb_xor*/
2063 (binaryfunc)set_or, /*nb_or*/
2064};
2065
2066PyDoc_STRVAR(frozenset_doc,
2067"frozenset(iterable) --> frozenset object\n\
2068\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002069Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002070
2071PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002072 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002073 "frozenset", /* tp_name */
2074 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002075 0, /* tp_itemsize */
2076 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002077 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002078 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002079 0, /* tp_getattr */
2080 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002081 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002082 (reprfunc)set_repr, /* tp_repr */
2083 &frozenset_as_number, /* tp_as_number */
2084 &set_as_sequence, /* tp_as_sequence */
2085 0, /* tp_as_mapping */
2086 frozenset_hash, /* tp_hash */
2087 0, /* tp_call */
2088 0, /* tp_str */
2089 PyObject_GenericGetAttr, /* tp_getattro */
2090 0, /* tp_setattro */
2091 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002092 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002093 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002094 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002095 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002096 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002097 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002098 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002099 (getiterfunc)set_iter, /* tp_iter */
2100 0, /* tp_iternext */
2101 frozenset_methods, /* tp_methods */
2102 0, /* tp_members */
2103 0, /* tp_getset */
2104 0, /* tp_base */
2105 0, /* tp_dict */
2106 0, /* tp_descr_get */
2107 0, /* tp_descr_set */
2108 0, /* tp_dictoffset */
2109 0, /* tp_init */
2110 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002111 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002112 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002113};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002114
2115
2116/***** C API functions *************************************************/
2117
2118PyObject *
2119PySet_New(PyObject *iterable)
2120{
2121 return make_new_set(&PySet_Type, iterable);
2122}
2123
2124PyObject *
2125PyFrozenSet_New(PyObject *iterable)
2126{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002127 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002128
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002129 if (iterable == NULL)
2130 args = PyTuple_New(0);
2131 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002132 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002133 if (args == NULL)
2134 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002135 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002136 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002137 return result;
2138}
2139
Neal Norwitz8c49c822006-03-04 18:41:19 +00002140Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002141PySet_Size(PyObject *anyset)
2142{
2143 if (!PyAnySet_Check(anyset)) {
2144 PyErr_BadInternalCall();
2145 return -1;
2146 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002147 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002148}
2149
2150int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002151PySet_Clear(PyObject *set)
2152{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002153 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002154 PyErr_BadInternalCall();
2155 return -1;
2156 }
2157 return set_clear_internal((PySetObject *)set);
2158}
2159
2160int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002161PySet_Contains(PyObject *anyset, PyObject *key)
2162{
2163 if (!PyAnySet_Check(anyset)) {
2164 PyErr_BadInternalCall();
2165 return -1;
2166 }
2167 return set_contains_key((PySetObject *)anyset, key);
2168}
2169
2170int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002171PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002172{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002173 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002174 PyErr_BadInternalCall();
2175 return -1;
2176 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002177 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002178}
2179
2180int
2181PySet_Add(PyObject *set, PyObject *key)
2182{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002183 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002184 PyErr_BadInternalCall();
2185 return -1;
2186 }
2187 return set_add_key((PySetObject *)set, key);
2188}
2189
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002190int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002191_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002192{
2193 setentry *entry_ptr;
2194
2195 if (!PyAnySet_Check(set)) {
2196 PyErr_BadInternalCall();
2197 return -1;
2198 }
2199 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2200 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002201 *key = entry_ptr->key;
2202 return 1;
2203}
2204
2205int
2206_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2207{
2208 setentry *entry;
2209
2210 if (!PyAnySet_Check(set)) {
2211 PyErr_BadInternalCall();
2212 return -1;
2213 }
2214 if (set_next((PySetObject *)set, pos, &entry) == 0)
2215 return 0;
2216 *key = entry->key;
2217 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002218 return 1;
2219}
2220
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002221PyObject *
2222PySet_Pop(PyObject *set)
2223{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002224 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002225 PyErr_BadInternalCall();
2226 return NULL;
2227 }
2228 return set_pop((PySetObject *)set);
2229}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002230
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002231int
2232_PySet_Update(PyObject *set, PyObject *iterable)
2233{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002234 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002235 PyErr_BadInternalCall();
2236 return -1;
2237 }
2238 return set_update_internal((PySetObject *)set, iterable);
2239}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002240
2241#ifdef Py_DEBUG
2242
2243/* Test code to be called with any three element set.
2244 Returns True and original set is restored. */
2245
2246#define assertRaises(call_return_value, exception) \
2247 do { \
2248 assert(call_return_value); \
2249 assert(PyErr_ExceptionMatches(exception)); \
2250 PyErr_Clear(); \
2251 } while(0)
2252
2253static PyObject *
2254test_c_api(PySetObject *so)
2255{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002256 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002257 char *s;
2258 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002259 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002260 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002261
2262 /* Verify preconditions and exercise type/size checks */
2263 assert(PyAnySet_Check(ob));
2264 assert(PyAnySet_CheckExact(ob));
2265 assert(!PyFrozenSet_CheckExact(ob));
2266 assert(PySet_Size(ob) == 3);
2267 assert(PySet_GET_SIZE(ob) == 3);
2268
2269 /* Raise TypeError for non-iterable constructor arguments */
2270 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2271 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2272
2273 /* Raise TypeError for unhashable key */
2274 dup = PySet_New(ob);
2275 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2276 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2277 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2278
2279 /* Exercise successful pop, contains, add, and discard */
2280 elem = PySet_Pop(ob);
2281 assert(PySet_Contains(ob, elem) == 0);
2282 assert(PySet_GET_SIZE(ob) == 2);
2283 assert(PySet_Add(ob, elem) == 0);
2284 assert(PySet_Contains(ob, elem) == 1);
2285 assert(PySet_GET_SIZE(ob) == 3);
2286 assert(PySet_Discard(ob, elem) == 1);
2287 assert(PySet_GET_SIZE(ob) == 2);
2288 assert(PySet_Discard(ob, elem) == 0);
2289 assert(PySet_GET_SIZE(ob) == 2);
2290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002291 /* Exercise clear */
2292 dup2 = PySet_New(dup);
2293 assert(PySet_Clear(dup2) == 0);
2294 assert(PySet_Size(dup2) == 0);
2295 Py_DECREF(dup2);
2296
2297 /* Raise SystemError on clear or update of frozen set */
2298 f = PyFrozenSet_New(dup);
2299 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2300 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2301 Py_DECREF(f);
2302
2303 /* Exercise direct iteration */
2304 i = 0, count = 0;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002305 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2306 s = PyString_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002307 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2308 count++;
2309 }
2310 assert(count == 3);
2311
2312 /* Exercise updates */
2313 dup2 = PySet_New(NULL);
2314 assert(_PySet_Update(dup2, dup) == 0);
2315 assert(PySet_Size(dup2) == 3);
2316 assert(_PySet_Update(dup2, dup) == 0);
2317 assert(PySet_Size(dup2) == 3);
2318 Py_DECREF(dup2);
2319
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002320 /* Raise SystemError when self argument is not a set or frozenset. */
2321 t = PyTuple_New(0);
2322 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2323 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2324 Py_DECREF(t);
2325
2326 /* Raise SystemError when self argument is not a set. */
2327 f = PyFrozenSet_New(dup);
2328 assert(PySet_Size(f) == 3);
2329 assert(PyFrozenSet_CheckExact(f));
2330 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2331 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2332 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2333 Py_DECREF(f);
2334
2335 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002336 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2337 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002338 assert(PySet_GET_SIZE(ob) == 0);
2339 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2340
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002341 /* Restore the set from the copy using the PyNumber API */
2342 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2343 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002344
2345 /* Verify constructors accept NULL arguments */
2346 f = PySet_New(NULL);
2347 assert(f != NULL);
2348 assert(PySet_GET_SIZE(f) == 0);
2349 Py_DECREF(f);
2350 f = PyFrozenSet_New(NULL);
2351 assert(f != NULL);
2352 assert(PyFrozenSet_CheckExact(f));
2353 assert(PySet_GET_SIZE(f) == 0);
2354 Py_DECREF(f);
2355
2356 Py_DECREF(elem);
2357 Py_DECREF(dup);
2358 Py_RETURN_TRUE;
2359}
2360
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002361#undef assertRaises
2362
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002363#endif