blob: a5366c89bf543be3789a7cf1d6abe57935ae1cd5 [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
568static int
569set_tp_print(PySetObject *so, FILE *fp, int flags)
570{
571 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000572 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000573 char *emit = ""; /* No separator emitted on first pass */
574 char *separator = ", ";
Georg Brandlc4996ba2006-08-28 19:37:11 +0000575 int literalform = 0;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000576 int status = Py_ReprEnter((PyObject*)so);
577
578 if (status != 0) {
579 if (status < 0)
580 return status;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000581 fprintf(fp, "%s(...)", Py_Type(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000582 return 0;
583 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000584
Georg Brandlc4996ba2006-08-28 19:37:11 +0000585 if (!so->used) {
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000586 Py_ReprLeave((PyObject*)so);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000587 fprintf(fp, "%s()", Py_Type(so)->tp_name);
Georg Brandlc4996ba2006-08-28 19:37:11 +0000588 return 0;
589 }
590
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000591 if (Py_Type(so) == &PySet_Type) {
Georg Brandlc4996ba2006-08-28 19:37:11 +0000592 literalform = 1;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000593 fprintf(fp, "{");
Georg Brandlc4996ba2006-08-28 19:37:11 +0000594 } else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000595 fprintf(fp, "%s([", Py_Type(so)->tp_name);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000596 while (set_next(so, &pos, &entry)) {
597 fputs(emit, fp);
598 emit = separator;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000599 if (PyObject_Print(entry->key, fp, 0) != 0) {
600 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601 return -1;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000602 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000603 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000604 if (literalform)
Guido van Rossum86e58e22006-08-28 15:27:34 +0000605 fputs("}", fp);
606 else
607 fputs("])", fp);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000608 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000609 return 0;
610}
611
612static PyObject *
613set_repr(PySetObject *so)
614{
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000615 PyObject *keys, *result=NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000616 Py_UNICODE *u;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000617 int status = Py_ReprEnter((PyObject*)so);
618
619 if (status != 0) {
620 if (status < 0)
621 return NULL;
Martin v. Löwis5d7428b2007-07-21 18:47:48 +0000622 return PyUnicode_FromFormat("%s(...)", Py_Type(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000623 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000624
Georg Brandlc4996ba2006-08-28 19:37:11 +0000625 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000626 if (!so->used) {
627 Py_ReprLeave((PyObject*)so);
Martin v. Löwis5d7428b2007-07-21 18:47:48 +0000628 return PyUnicode_FromFormat("%s()", Py_Type(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000629 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000630
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000631 keys = PySequence_List((PyObject *)so);
632 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000633 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000634
Martin v. Löwis5d7428b2007-07-21 18:47:48 +0000635 if (Py_Type(so) != &PySet_Type) {
636 result = PyUnicode_FromFormat("%s(%R)", Py_Type(so)->tp_name, keys);
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000637 Py_DECREF(keys);
638 }
639 else {
640 PyObject *listrepr = PyObject_Repr(keys);
641 Py_ssize_t newsize;
642 Py_DECREF(keys);
643 if (listrepr == NULL) {
644 Py_DECREF(keys);
645 goto done;
646 }
647 newsize = PyUnicode_GET_SIZE(listrepr);
648 result = PyUnicode_FromUnicode(NULL, newsize);
649 if (result) {
650 u = PyUnicode_AS_UNICODE(result);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000651 *u++ = '{';
652 /* Omit the brackets from the listrepr */
653 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000654 PyUnicode_GET_SIZE(listrepr)-2);
655 u += newsize-2;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000656 *u++ = '}';
657 }
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000658 Py_DECREF(listrepr);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000659 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000660done:
661 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000662 return result;
663}
664
Martin v. Löwis18e16552006-02-15 17:27:45 +0000665static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000666set_len(PyObject *so)
667{
668 return ((PySetObject *)so)->used;
669}
670
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000671static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000672set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000674 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000675 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000676 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000677
678 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000679 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000680
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000681 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000682 if (other == so || other->used == 0)
683 /* a.update(a) or a.update({}); nothing to do */
684 return 0;
685 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000686 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000687 * that there will be no (or few) overlapping keys.
688 */
689 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
690 if (set_table_resize(so, (so->used + other->used)*2) != 0)
691 return -1;
692 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000693 for (i = 0; i <= other->mask; i++) {
694 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000695 if (entry->key != NULL &&
696 entry->key != dummy) {
697 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000698 if (set_insert_key(so, entry->key, entry->hash) == -1) {
699 Py_DECREF(entry->key);
700 return -1;
701 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000702 }
703 }
704 return 0;
705}
706
707static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000708set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000709{
710 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000711 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000712
713 if (!PyString_CheckExact(key) ||
714 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
715 hash = PyObject_Hash(key);
716 if (hash == -1)
717 return -1;
718 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000719 entry = (so->lookup)(so, key, hash);
720 if (entry == NULL)
721 return -1;
722 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000723 return key != NULL && key != dummy;
724}
725
Raymond Hettingerc991db22005-08-11 07:58:45 +0000726static int
727set_contains_entry(PySetObject *so, setentry *entry)
728{
729 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000730 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000731
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000732 lu_entry = (so->lookup)(so, entry->key, entry->hash);
733 if (lu_entry == NULL)
734 return -1;
735 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000736 return key != NULL && key != dummy;
737}
738
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000739static PyObject *
740set_pop(PySetObject *so)
741{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000742 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000743 register setentry *entry;
744 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000745
746 assert (PyAnySet_Check(so));
747 if (so->used == 0) {
748 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
749 return NULL;
750 }
751
752 /* Set entry to "the first" unused or dummy set entry. We abuse
753 * the hash field of slot 0 to hold a search finger:
754 * If slot 0 has a value, use slot 0.
755 * Else slot 0 is being used to hold a search finger,
756 * and we use its hash value as the first index to look.
757 */
758 entry = &so->table[0];
759 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000760 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000761 /* The hash field may be a real hash value, or it may be a
762 * legit search finger, or it may be a once-legit search
763 * finger that's out of bounds now because it wrapped around
764 * or the table shrunk -- simply make sure it's in bounds now.
765 */
766 if (i > so->mask || i < 1)
767 i = 1; /* skip slot 0 */
768 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
769 i++;
770 if (i > so->mask)
771 i = 1;
772 }
773 }
774 key = entry->key;
775 Py_INCREF(dummy);
776 entry->key = dummy;
777 so->used--;
778 so->table[0].hash = i + 1; /* next place to start */
779 return key;
780}
781
782PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
783
784static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000785set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000786{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000787 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000788 setentry *entry;
789
790 while (set_next(so, &pos, &entry))
791 Py_VISIT(entry->key);
792 return 0;
793}
794
795static long
796frozenset_hash(PyObject *self)
797{
798 PySetObject *so = (PySetObject *)self;
799 long h, hash = 1927868237L;
800 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000801 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000802
803 if (so->hash != -1)
804 return so->hash;
805
806 hash *= PySet_GET_SIZE(self) + 1;
807 while (set_next(so, &pos, &entry)) {
808 /* Work to increase the bit dispersion for closely spaced hash
809 values. The is important because some use cases have many
810 combinations of a small number of elements with nearby
811 hashes so that many distinct combinations collapse to only
812 a handful of distinct hash values. */
813 h = entry->hash;
814 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
815 }
816 hash = hash * 69069L + 907133923L;
817 if (hash == -1)
818 hash = 590923713L;
819 so->hash = hash;
820 return hash;
821}
822
Raymond Hettingera9d99362005-08-05 00:01:15 +0000823/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825typedef struct {
826 PyObject_HEAD
827 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000828 Py_ssize_t si_used;
829 Py_ssize_t si_pos;
830 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831} setiterobject;
832
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833static void
834setiter_dealloc(setiterobject *si)
835{
836 Py_XDECREF(si->si_set);
837 PyObject_Del(si);
838}
839
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000840static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000841setiter_len(setiterobject *si)
842{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000843 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000844 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000845 len = si->len;
846 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847}
848
Armin Rigof5b3e362006-02-11 21:32:43 +0000849PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000850
851static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000852 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000853 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000854};
855
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000856static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000857{
858 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000859 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000860 register setentry *entry;
861 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000862
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000863 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000864 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000865 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000866
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000867 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868 PyErr_SetString(PyExc_RuntimeError,
869 "Set changed size during iteration");
870 si->si_used = -1; /* Make this state sticky */
871 return NULL;
872 }
873
874 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000875 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000876 entry = so->table;
877 mask = so->mask;
878 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000879 i++;
880 si->si_pos = i+1;
881 if (i > mask)
882 goto fail;
883 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000884 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000885 Py_INCREF(key);
886 return key;
887
888fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000889 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000890 si->si_set = NULL;
891 return NULL;
892}
893
Hye-Shik Change2956762005-08-01 05:26:41 +0000894static PyTypeObject PySetIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000895 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000896 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000897 sizeof(setiterobject), /* tp_basicsize */
898 0, /* tp_itemsize */
899 /* methods */
900 (destructor)setiter_dealloc, /* tp_dealloc */
901 0, /* tp_print */
902 0, /* tp_getattr */
903 0, /* tp_setattr */
904 0, /* tp_compare */
905 0, /* tp_repr */
906 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000907 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000908 0, /* tp_as_mapping */
909 0, /* tp_hash */
910 0, /* tp_call */
911 0, /* tp_str */
912 PyObject_GenericGetAttr, /* tp_getattro */
913 0, /* tp_setattro */
914 0, /* tp_as_buffer */
915 Py_TPFLAGS_DEFAULT, /* tp_flags */
916 0, /* tp_doc */
917 0, /* tp_traverse */
918 0, /* tp_clear */
919 0, /* tp_richcompare */
920 0, /* tp_weaklistoffset */
921 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000922 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000923 setiter_methods, /* tp_methods */
924 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000925};
926
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000927static PyObject *
928set_iter(PySetObject *so)
929{
930 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
931 if (si == NULL)
932 return NULL;
933 Py_INCREF(so);
934 si->si_set = so;
935 si->si_used = so->used;
936 si->si_pos = 0;
937 si->len = so->used;
938 return (PyObject *)si;
939}
940
Raymond Hettingerd7946662005-08-01 21:39:29 +0000941static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000943{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000944 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000945
Thomas Wouterscf297e42007-02-23 15:07:44 +0000946 if (PyAnySet_CheckExact(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000947 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000948
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000949 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000950 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000951 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000952 long hash;
953 Py_ssize_t dictsize = PyDict_Size(other);
954
955 /* Do one big resize at the start, rather than
956 * incrementally resizing as we insert new keys. Expect
957 * that there will be no (or few) overlapping keys.
958 */
959 if (dictsize == -1)
960 return -1;
961 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
962 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
963 return -1;
964 }
965 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
966 setentry an_entry;
967
968 an_entry.hash = hash;
969 an_entry.key = key;
970 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000971 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000972 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000973 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000974 }
975
Raymond Hettingera38123e2003-11-24 22:18:49 +0000976 it = PyObject_GetIter(other);
977 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000978 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000979
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000980 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000981 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000982 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000983 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000984 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000985 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000986 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000987 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000988 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000989 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000990 return -1;
991 return 0;
992}
993
994static PyObject *
995set_update(PySetObject *so, PyObject *other)
996{
997 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000998 return NULL;
999 Py_RETURN_NONE;
1000}
1001
1002PyDoc_STRVAR(update_doc,
1003"Update a set with the union of itself and another.");
1004
1005static PyObject *
1006make_new_set(PyTypeObject *type, PyObject *iterable)
1007{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001008 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001009
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001010 if (dummy == NULL) { /* Auto-initialize dummy */
1011 dummy = PyString_FromString("<dummy key>");
1012 if (dummy == NULL)
1013 return NULL;
1014 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001015
1016 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001017 if (num_free_sets &&
1018 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1019 so = free_sets[--num_free_sets];
1020 assert (so != NULL && PyAnySet_CheckExact(so));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001021 Py_Type(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001022 _Py_NewReference((PyObject *)so);
1023 EMPTY_TO_MINSIZE(so);
1024 PyObject_GC_Track(so);
1025 } else {
1026 so = (PySetObject *)type->tp_alloc(type, 0);
1027 if (so == NULL)
1028 return NULL;
1029 /* tp_alloc has already zeroed the structure */
1030 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1031 INIT_NONZERO_SET_SLOTS(so);
1032 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001034 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001035 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001036
Raymond Hettingera38123e2003-11-24 22:18:49 +00001037 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001038 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001039 Py_DECREF(so);
1040 return NULL;
1041 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001042 }
1043
Raymond Hettingera690a992003-11-16 16:17:49 +00001044 return (PyObject *)so;
1045}
1046
Raymond Hettingerd7946662005-08-01 21:39:29 +00001047/* The empty frozenset is a singleton */
1048static PyObject *emptyfrozenset = NULL;
1049
Raymond Hettingera690a992003-11-16 16:17:49 +00001050static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001051frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001052{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001053 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001054
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001055 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001056 return NULL;
1057
Raymond Hettingera690a992003-11-16 16:17:49 +00001058 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1059 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001060
1061 if (type != &PyFrozenSet_Type)
1062 return make_new_set(type, iterable);
1063
1064 if (iterable != NULL) {
1065 /* frozenset(f) is idempotent */
1066 if (PyFrozenSet_CheckExact(iterable)) {
1067 Py_INCREF(iterable);
1068 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001069 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001070 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001071 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001072 return result;
1073 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001074 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001075 /* The empty frozenset is a singleton */
1076 if (emptyfrozenset == NULL)
1077 emptyfrozenset = make_new_set(type, NULL);
1078 Py_XINCREF(emptyfrozenset);
1079 return emptyfrozenset;
1080}
1081
1082void
1083PySet_Fini(void)
1084{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001085 PySetObject *so;
1086
1087 while (num_free_sets) {
1088 num_free_sets--;
1089 so = free_sets[num_free_sets];
1090 PyObject_GC_Del(so);
1091 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092 Py_CLEAR(dummy);
1093 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001094}
1095
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001096static PyObject *
1097set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1098{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001099 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001100 return NULL;
1101
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001102 return make_new_set(type, NULL);
1103}
1104
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001105/* set_swap_bodies() switches the contents of any two sets by moving their
1106 internal data pointers and, if needed, copying the internal smalltables.
1107 Semantically equivalent to:
1108
1109 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1110
1111 The function always succeeds and it leaves both objects in a stable state.
1112 Useful for creating temporary frozensets from sets for membership testing
1113 in __contains__(), discard(), and remove(). Also useful for operations
1114 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001115 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001116*/
1117
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001118static void
1119set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001120{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001121 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001122 setentry *u;
1123 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1124 setentry tab[PySet_MINSIZE];
1125 long h;
1126
1127 t = a->fill; a->fill = b->fill; b->fill = t;
1128 t = a->used; a->used = b->used; b->used = t;
1129 t = a->mask; a->mask = b->mask; b->mask = t;
1130
1131 u = a->table;
1132 if (a->table == a->smalltable)
1133 u = b->smalltable;
1134 a->table = b->table;
1135 if (b->table == b->smalltable)
1136 a->table = a->smalltable;
1137 b->table = u;
1138
1139 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1140
1141 if (a->table == a->smalltable || b->table == b->smalltable) {
1142 memcpy(tab, a->smalltable, sizeof(tab));
1143 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1144 memcpy(b->smalltable, tab, sizeof(tab));
1145 }
1146
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001147 if (PyType_IsSubtype(Py_Type(a), &PyFrozenSet_Type) &&
1148 PyType_IsSubtype(Py_Type(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001149 h = a->hash; a->hash = b->hash; b->hash = h;
1150 } else {
1151 a->hash = -1;
1152 b->hash = -1;
1153 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001154}
1155
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001156static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001157set_copy(PySetObject *so)
1158{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001159 return make_new_set(Py_Type(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001160}
1161
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001162static PyObject *
1163frozenset_copy(PySetObject *so)
1164{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001165 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001166 Py_INCREF(so);
1167 return (PyObject *)so;
1168 }
1169 return set_copy(so);
1170}
1171
Raymond Hettingera690a992003-11-16 16:17:49 +00001172PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1173
1174static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001175set_clear(PySetObject *so)
1176{
1177 set_clear_internal(so);
1178 Py_RETURN_NONE;
1179}
1180
1181PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1182
1183static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001184set_union(PySetObject *so, PyObject *other)
1185{
1186 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001187
1188 result = (PySetObject *)set_copy(so);
1189 if (result == NULL)
1190 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001191 if ((PyObject *)so == other)
1192 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001193 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001194 Py_DECREF(result);
1195 return NULL;
1196 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001197 return (PyObject *)result;
1198}
1199
1200PyDoc_STRVAR(union_doc,
1201 "Return the union of two sets as a new set.\n\
1202\n\
1203(i.e. all elements that are in either set.)");
1204
1205static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001206set_or(PySetObject *so, PyObject *other)
1207{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001208 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001209 Py_INCREF(Py_NotImplemented);
1210 return Py_NotImplemented;
1211 }
1212 return set_union(so, other);
1213}
1214
1215static PyObject *
1216set_ior(PySetObject *so, PyObject *other)
1217{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001218 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001219 Py_INCREF(Py_NotImplemented);
1220 return Py_NotImplemented;
1221 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001222 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001223 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001224 Py_INCREF(so);
1225 return (PyObject *)so;
1226}
1227
1228static PyObject *
1229set_intersection(PySetObject *so, PyObject *other)
1230{
1231 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001232 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001233
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001234 if ((PyObject *)so == other)
1235 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001236
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001237 result = (PySetObject *)make_new_set(Py_Type(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001238 if (result == NULL)
1239 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001240
Thomas Wouterscf297e42007-02-23 15:07:44 +00001241 if (PyAnySet_CheckExact(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001242 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001243 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001244
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001245 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001246 tmp = (PyObject *)so;
1247 so = (PySetObject *)other;
1248 other = tmp;
1249 }
1250
Raymond Hettingerc991db22005-08-11 07:58:45 +00001251 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001252 int rv = set_contains_entry(so, entry);
1253 if (rv == -1) {
1254 Py_DECREF(result);
1255 return NULL;
1256 }
1257 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001258 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001259 Py_DECREF(result);
1260 return NULL;
1261 }
1262 }
1263 }
1264 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001265 }
1266
Raymond Hettingera690a992003-11-16 16:17:49 +00001267 it = PyObject_GetIter(other);
1268 if (it == NULL) {
1269 Py_DECREF(result);
1270 return NULL;
1271 }
1272
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001273 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001274 int rv;
1275 setentry entry;
1276 long hash = PyObject_Hash(key);
1277
1278 if (hash == -1) {
1279 Py_DECREF(it);
1280 Py_DECREF(result);
1281 Py_DECREF(key);
1282 return NULL;
1283 }
1284 entry.hash = hash;
1285 entry.key = key;
1286 rv = set_contains_entry(so, &entry);
1287 if (rv == -1) {
1288 Py_DECREF(it);
1289 Py_DECREF(result);
1290 Py_DECREF(key);
1291 return NULL;
1292 }
1293 if (rv) {
1294 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001295 Py_DECREF(it);
1296 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001297 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001298 return NULL;
1299 }
1300 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001301 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001302 }
1303 Py_DECREF(it);
1304 if (PyErr_Occurred()) {
1305 Py_DECREF(result);
1306 return NULL;
1307 }
1308 return (PyObject *)result;
1309}
1310
1311PyDoc_STRVAR(intersection_doc,
1312"Return the intersection of two sets as a new set.\n\
1313\n\
1314(i.e. all elements that are in both sets.)");
1315
1316static PyObject *
1317set_intersection_update(PySetObject *so, PyObject *other)
1318{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001319 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001320
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001321 tmp = set_intersection(so, other);
1322 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001323 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001324 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001325 Py_DECREF(tmp);
1326 Py_RETURN_NONE;
1327}
1328
1329PyDoc_STRVAR(intersection_update_doc,
1330"Update a set with the intersection of itself and another.");
1331
1332static PyObject *
1333set_and(PySetObject *so, PyObject *other)
1334{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001335 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001336 Py_INCREF(Py_NotImplemented);
1337 return Py_NotImplemented;
1338 }
1339 return set_intersection(so, other);
1340}
1341
1342static PyObject *
1343set_iand(PySetObject *so, PyObject *other)
1344{
1345 PyObject *result;
1346
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001347 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001348 Py_INCREF(Py_NotImplemented);
1349 return Py_NotImplemented;
1350 }
1351 result = set_intersection_update(so, other);
1352 if (result == NULL)
1353 return NULL;
1354 Py_DECREF(result);
1355 Py_INCREF(so);
1356 return (PyObject *)so;
1357}
1358
Neal Norwitz6576bd82005-11-13 18:41:28 +00001359static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001360set_difference_update_internal(PySetObject *so, PyObject *other)
1361{
1362 if ((PyObject *)so == other)
1363 return set_clear_internal(so);
1364
Thomas Wouterscf297e42007-02-23 15:07:44 +00001365 if (PyAnySet_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001366 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001367 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001368
1369 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001370 if (set_discard_entry(so, entry) == -1)
1371 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001372 } else {
1373 PyObject *key, *it;
1374 it = PyObject_GetIter(other);
1375 if (it == NULL)
1376 return -1;
1377
1378 while ((key = PyIter_Next(it)) != NULL) {
1379 if (set_discard_key(so, key) == -1) {
1380 Py_DECREF(it);
1381 Py_DECREF(key);
1382 return -1;
1383 }
1384 Py_DECREF(key);
1385 }
1386 Py_DECREF(it);
1387 if (PyErr_Occurred())
1388 return -1;
1389 }
1390 /* If more than 1/5 are dummies, then resize them away. */
1391 if ((so->fill - so->used) * 5 < so->mask)
1392 return 0;
1393 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1394}
1395
Raymond Hettingera690a992003-11-16 16:17:49 +00001396static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001397set_difference_update(PySetObject *so, PyObject *other)
1398{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001399 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001400 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001401 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001402}
1403
1404PyDoc_STRVAR(difference_update_doc,
1405"Remove all elements of another set from this set.");
1406
1407static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001408set_difference(PySetObject *so, PyObject *other)
1409{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001410 PyObject *result;
1411 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001412 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001413
Thomas Wouterscf297e42007-02-23 15:07:44 +00001414 if (!PyAnySet_CheckExact(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001415 result = set_copy(so);
1416 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001417 return NULL;
1418 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001419 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001420 Py_DECREF(result);
1421 return NULL;
1422 }
1423
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001424 result = make_new_set(Py_Type(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001425 if (result == NULL)
1426 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001427
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001428 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001429 while (set_next(so, &pos, &entry)) {
1430 setentry entrycopy;
1431 entrycopy.hash = entry->hash;
1432 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001433 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001434 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1435 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001436 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001437 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001438 }
1439 }
1440 return result;
1441 }
1442
Raymond Hettingerc991db22005-08-11 07:58:45 +00001443 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001444 int rv = set_contains_entry((PySetObject *)other, entry);
1445 if (rv == -1) {
1446 Py_DECREF(result);
1447 return NULL;
1448 }
1449 if (!rv) {
1450 if (set_add_entry((PySetObject *)result, entry) == -1) {
1451 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001452 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001453 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001454 }
1455 }
1456 return result;
1457}
1458
1459PyDoc_STRVAR(difference_doc,
1460"Return the difference of two sets as a new set.\n\
1461\n\
1462(i.e. all elements that are in this set but not the other.)");
1463static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001464set_sub(PySetObject *so, PyObject *other)
1465{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001466 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001467 Py_INCREF(Py_NotImplemented);
1468 return Py_NotImplemented;
1469 }
1470 return set_difference(so, other);
1471}
1472
1473static PyObject *
1474set_isub(PySetObject *so, PyObject *other)
1475{
1476 PyObject *result;
1477
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001478 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001479 Py_INCREF(Py_NotImplemented);
1480 return Py_NotImplemented;
1481 }
1482 result = set_difference_update(so, other);
1483 if (result == NULL)
1484 return NULL;
1485 Py_DECREF(result);
1486 Py_INCREF(so);
1487 return (PyObject *)so;
1488}
1489
1490static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001491set_symmetric_difference_update(PySetObject *so, PyObject *other)
1492{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001493 PySetObject *otherset;
1494 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001495 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001496 setentry *entry;
1497
1498 if ((PyObject *)so == other)
1499 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001500
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001501 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001502 PyObject *value;
1503 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001504 long hash;
1505 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001506 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001507
Thomas Wouters89f507f2006-12-13 04:49:30 +00001508 an_entry.hash = hash;
1509 an_entry.key = key;
1510 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001511 if (rv == -1)
1512 return NULL;
1513 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001514 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001515 return NULL;
1516 }
1517 }
1518 Py_RETURN_NONE;
1519 }
1520
Thomas Wouterscf297e42007-02-23 15:07:44 +00001521 if (PyAnySet_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001522 Py_INCREF(other);
1523 otherset = (PySetObject *)other;
1524 } else {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001525 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001526 if (otherset == NULL)
1527 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001528 }
1529
Raymond Hettingerc991db22005-08-11 07:58:45 +00001530 while (set_next(otherset, &pos, &entry)) {
1531 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001532 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001533 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001534 return NULL;
1535 }
1536 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001537 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001538 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001539 return NULL;
1540 }
1541 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001542 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001543 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001544 Py_RETURN_NONE;
1545}
1546
1547PyDoc_STRVAR(symmetric_difference_update_doc,
1548"Update a set with the symmetric difference of itself and another.");
1549
1550static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001551set_symmetric_difference(PySetObject *so, PyObject *other)
1552{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001553 PyObject *rv;
1554 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001555
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001556 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001557 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001558 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001559 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1560 if (rv == NULL)
1561 return NULL;
1562 Py_DECREF(rv);
1563 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001564}
1565
1566PyDoc_STRVAR(symmetric_difference_doc,
1567"Return the symmetric difference of two sets as a new set.\n\
1568\n\
1569(i.e. all elements that are in exactly one of the sets.)");
1570
1571static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001572set_xor(PySetObject *so, PyObject *other)
1573{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001574 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001575 Py_INCREF(Py_NotImplemented);
1576 return Py_NotImplemented;
1577 }
1578 return set_symmetric_difference(so, other);
1579}
1580
1581static PyObject *
1582set_ixor(PySetObject *so, PyObject *other)
1583{
1584 PyObject *result;
1585
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001586 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001587 Py_INCREF(Py_NotImplemented);
1588 return Py_NotImplemented;
1589 }
1590 result = set_symmetric_difference_update(so, other);
1591 if (result == NULL)
1592 return NULL;
1593 Py_DECREF(result);
1594 Py_INCREF(so);
1595 return (PyObject *)so;
1596}
1597
1598static PyObject *
1599set_issubset(PySetObject *so, PyObject *other)
1600{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001601 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001602 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001603
Thomas Wouterscf297e42007-02-23 15:07:44 +00001604 if (!PyAnySet_CheckExact(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001605 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001606 tmp = make_new_set(&PySet_Type, other);
1607 if (tmp == NULL)
1608 return NULL;
1609 result = set_issubset(so, tmp);
1610 Py_DECREF(tmp);
1611 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001612 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001613 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001614 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001615
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001616 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001617 int rv = set_contains_entry((PySetObject *)other, entry);
1618 if (rv == -1)
1619 return NULL;
1620 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001621 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001622 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001623 Py_RETURN_TRUE;
1624}
1625
1626PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1627
1628static PyObject *
1629set_issuperset(PySetObject *so, PyObject *other)
1630{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001631 PyObject *tmp, *result;
1632
Thomas Wouterscf297e42007-02-23 15:07:44 +00001633 if (!PyAnySet_CheckExact(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001634 tmp = make_new_set(&PySet_Type, other);
1635 if (tmp == NULL)
1636 return NULL;
1637 result = set_issuperset(so, tmp);
1638 Py_DECREF(tmp);
1639 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001640 }
1641 return set_issubset((PySetObject *)other, (PyObject *)so);
1642}
1643
1644PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1645
Raymond Hettingera690a992003-11-16 16:17:49 +00001646static PyObject *
1647set_richcompare(PySetObject *v, PyObject *w, int op)
1648{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001649 PyObject *r1, *r2;
1650
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001651 if(!PyAnySet_Check(w)) {
1652 if (op == Py_EQ)
1653 Py_RETURN_FALSE;
1654 if (op == Py_NE)
1655 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001656 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1657 return NULL;
1658 }
1659 switch (op) {
1660 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001661 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001662 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001663 if (v->hash != -1 &&
1664 ((PySetObject *)w)->hash != -1 &&
1665 v->hash != ((PySetObject *)w)->hash)
1666 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001667 return set_issubset(v, w);
1668 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001669 r1 = set_richcompare(v, w, Py_EQ);
1670 if (r1 == NULL)
1671 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001672 r2 = PyBool_FromLong(PyObject_Not(r1));
1673 Py_DECREF(r1);
1674 return r2;
1675 case Py_LE:
1676 return set_issubset(v, w);
1677 case Py_GE:
1678 return set_issuperset(v, w);
1679 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001680 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001681 Py_RETURN_FALSE;
1682 return set_issubset(v, w);
1683 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001684 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001685 Py_RETURN_FALSE;
1686 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001687 }
1688 Py_INCREF(Py_NotImplemented);
1689 return Py_NotImplemented;
1690}
1691
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001692static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001693set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001694{
1695 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1696 return -1;
1697}
1698
Raymond Hettingera690a992003-11-16 16:17:49 +00001699static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001700set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001701{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001702 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001703 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001704 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001705}
1706
1707PyDoc_STRVAR(add_doc,
1708"Add an element to a set.\n\
1709\n\
1710This has no effect if the element is already present.");
1711
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001712static int
1713set_contains(PySetObject *so, PyObject *key)
1714{
1715 PyObject *tmpkey;
1716 int rv;
1717
1718 rv = set_contains_key(so, key);
1719 if (rv == -1) {
1720 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1721 return -1;
1722 PyErr_Clear();
1723 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1724 if (tmpkey == NULL)
1725 return -1;
1726 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1727 rv = set_contains(so, tmpkey);
1728 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1729 Py_DECREF(tmpkey);
1730 }
1731 return rv;
1732}
1733
1734static PyObject *
1735set_direct_contains(PySetObject *so, PyObject *key)
1736{
1737 long result;
1738
1739 result = set_contains(so, key);
1740 if (result == -1)
1741 return NULL;
1742 return PyBool_FromLong(result);
1743}
1744
1745PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1746
Raymond Hettingera690a992003-11-16 16:17:49 +00001747static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001748set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001749{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001750 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001751 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001752
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001753 rv = set_discard_key(so, key);
1754 if (rv == -1) {
1755 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1756 return NULL;
1757 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001758 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1759 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001760 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001761 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001762 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001763 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001764 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001765 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001766 } else if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001767 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001768 return NULL;
1769 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001770 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001771}
1772
1773PyDoc_STRVAR(remove_doc,
1774"Remove an element from a set; it must be a member.\n\
1775\n\
1776If the element is not a member, raise a KeyError.");
1777
1778static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001779set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001780{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001781 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001782 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001783
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001784 rv = set_discard_key(so, key);
1785 if (rv == -1) {
1786 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1787 return NULL;
1788 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001789 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1790 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001791 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001792 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001793 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001794 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001795 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001796 return result;
1797 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001798 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001799}
1800
1801PyDoc_STRVAR(discard_doc,
1802"Remove an element from a set if it is a member.\n\
1803\n\
1804If the element is not a member, do nothing.");
1805
1806static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001807set_reduce(PySetObject *so)
1808{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001809 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001810
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001811 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001812 if (keys == NULL)
1813 goto done;
1814 args = PyTuple_Pack(1, keys);
1815 if (args == NULL)
1816 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001817 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1818 if (dict == NULL) {
1819 PyErr_Clear();
1820 dict = Py_None;
1821 Py_INCREF(dict);
1822 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001823 result = PyTuple_Pack(3, Py_Type(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001824done:
1825 Py_XDECREF(args);
1826 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001827 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001828 return result;
1829}
1830
1831PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1832
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001833static int
1834set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1835{
1836 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001837
1838 if (!PyAnySet_Check(self))
1839 return -1;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001840 if (!PyArg_UnpackTuple(args, Py_Type(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001841 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001842 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001843 self->hash = -1;
1844 if (iterable == NULL)
1845 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001846 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001847}
1848
Raymond Hettingera690a992003-11-16 16:17:49 +00001849static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001850 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001851 0, /* sq_concat */
1852 0, /* sq_repeat */
1853 0, /* sq_item */
1854 0, /* sq_slice */
1855 0, /* sq_ass_item */
1856 0, /* sq_ass_slice */
1857 (objobjproc)set_contains, /* sq_contains */
1858};
1859
1860/* set object ********************************************************/
1861
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001862#ifdef Py_DEBUG
1863static PyObject *test_c_api(PySetObject *so);
1864
1865PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1866All is well if assertions don't fail.");
1867#endif
1868
Raymond Hettingera690a992003-11-16 16:17:49 +00001869static PyMethodDef set_methods[] = {
1870 {"add", (PyCFunction)set_add, METH_O,
1871 add_doc},
1872 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1873 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001874 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001875 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001876 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1877 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001878 {"discard", (PyCFunction)set_discard, METH_O,
1879 discard_doc},
1880 {"difference", (PyCFunction)set_difference, METH_O,
1881 difference_doc},
1882 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1883 difference_update_doc},
1884 {"intersection",(PyCFunction)set_intersection, METH_O,
1885 intersection_doc},
1886 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1887 intersection_update_doc},
1888 {"issubset", (PyCFunction)set_issubset, METH_O,
1889 issubset_doc},
1890 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1891 issuperset_doc},
1892 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1893 pop_doc},
1894 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1895 reduce_doc},
1896 {"remove", (PyCFunction)set_remove, METH_O,
1897 remove_doc},
1898 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1899 symmetric_difference_doc},
1900 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1901 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001902#ifdef Py_DEBUG
1903 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1904 test_c_api_doc},
1905#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001906 {"union", (PyCFunction)set_union, METH_O,
1907 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001908 {"update", (PyCFunction)set_update, METH_O,
1909 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001910 {NULL, NULL} /* sentinel */
1911};
1912
1913static PyNumberMethods set_as_number = {
1914 0, /*nb_add*/
1915 (binaryfunc)set_sub, /*nb_subtract*/
1916 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001917 0, /*nb_remainder*/
1918 0, /*nb_divmod*/
1919 0, /*nb_power*/
1920 0, /*nb_negative*/
1921 0, /*nb_positive*/
1922 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001923 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001924 0, /*nb_invert*/
1925 0, /*nb_lshift*/
1926 0, /*nb_rshift*/
1927 (binaryfunc)set_and, /*nb_and*/
1928 (binaryfunc)set_xor, /*nb_xor*/
1929 (binaryfunc)set_or, /*nb_or*/
1930 0, /*nb_coerce*/
1931 0, /*nb_int*/
1932 0, /*nb_long*/
1933 0, /*nb_float*/
1934 0, /*nb_oct*/
1935 0, /*nb_hex*/
1936 0, /*nb_inplace_add*/
1937 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1938 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001939 0, /*nb_inplace_remainder*/
1940 0, /*nb_inplace_power*/
1941 0, /*nb_inplace_lshift*/
1942 0, /*nb_inplace_rshift*/
1943 (binaryfunc)set_iand, /*nb_inplace_and*/
1944 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1945 (binaryfunc)set_ior, /*nb_inplace_or*/
1946};
1947
1948PyDoc_STRVAR(set_doc,
1949"set(iterable) --> set object\n\
1950\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001951Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001952
1953PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001954 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001955 "set", /* tp_name */
1956 sizeof(PySetObject), /* tp_basicsize */
1957 0, /* tp_itemsize */
1958 /* methods */
1959 (destructor)set_dealloc, /* tp_dealloc */
1960 (printfunc)set_tp_print, /* tp_print */
1961 0, /* tp_getattr */
1962 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001963 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001964 (reprfunc)set_repr, /* tp_repr */
1965 &set_as_number, /* tp_as_number */
1966 &set_as_sequence, /* tp_as_sequence */
1967 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001968 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001969 0, /* tp_call */
1970 0, /* tp_str */
1971 PyObject_GenericGetAttr, /* tp_getattro */
1972 0, /* tp_setattro */
1973 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001974 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001975 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001976 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001977 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001978 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001979 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001980 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001981 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001982 0, /* tp_iternext */
1983 set_methods, /* tp_methods */
1984 0, /* tp_members */
1985 0, /* tp_getset */
1986 0, /* tp_base */
1987 0, /* tp_dict */
1988 0, /* tp_descr_get */
1989 0, /* tp_descr_set */
1990 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001991 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001992 PyType_GenericAlloc, /* tp_alloc */
1993 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001994 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001995};
1996
1997/* frozenset object ********************************************************/
1998
1999
2000static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002001 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002002 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002003 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002004 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002005 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002006 difference_doc},
2007 {"intersection",(PyCFunction)set_intersection, METH_O,
2008 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002009 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002010 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002011 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002012 issuperset_doc},
2013 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2014 reduce_doc},
2015 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2016 symmetric_difference_doc},
2017 {"union", (PyCFunction)set_union, METH_O,
2018 union_doc},
2019 {NULL, NULL} /* sentinel */
2020};
2021
2022static PyNumberMethods frozenset_as_number = {
2023 0, /*nb_add*/
2024 (binaryfunc)set_sub, /*nb_subtract*/
2025 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002026 0, /*nb_remainder*/
2027 0, /*nb_divmod*/
2028 0, /*nb_power*/
2029 0, /*nb_negative*/
2030 0, /*nb_positive*/
2031 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002032 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002033 0, /*nb_invert*/
2034 0, /*nb_lshift*/
2035 0, /*nb_rshift*/
2036 (binaryfunc)set_and, /*nb_and*/
2037 (binaryfunc)set_xor, /*nb_xor*/
2038 (binaryfunc)set_or, /*nb_or*/
2039};
2040
2041PyDoc_STRVAR(frozenset_doc,
2042"frozenset(iterable) --> frozenset object\n\
2043\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002044Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002045
2046PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002047 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002048 "frozenset", /* tp_name */
2049 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002050 0, /* tp_itemsize */
2051 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002052 (destructor)set_dealloc, /* tp_dealloc */
2053 (printfunc)set_tp_print, /* tp_print */
2054 0, /* tp_getattr */
2055 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002056 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002057 (reprfunc)set_repr, /* tp_repr */
2058 &frozenset_as_number, /* tp_as_number */
2059 &set_as_sequence, /* tp_as_sequence */
2060 0, /* tp_as_mapping */
2061 frozenset_hash, /* tp_hash */
2062 0, /* tp_call */
2063 0, /* tp_str */
2064 PyObject_GenericGetAttr, /* tp_getattro */
2065 0, /* tp_setattro */
2066 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002067 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002068 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002069 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002070 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002071 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002072 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002073 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002074 (getiterfunc)set_iter, /* tp_iter */
2075 0, /* tp_iternext */
2076 frozenset_methods, /* tp_methods */
2077 0, /* tp_members */
2078 0, /* tp_getset */
2079 0, /* tp_base */
2080 0, /* tp_dict */
2081 0, /* tp_descr_get */
2082 0, /* tp_descr_set */
2083 0, /* tp_dictoffset */
2084 0, /* tp_init */
2085 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002086 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002087 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002088};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002089
2090
2091/***** C API functions *************************************************/
2092
2093PyObject *
2094PySet_New(PyObject *iterable)
2095{
2096 return make_new_set(&PySet_Type, iterable);
2097}
2098
2099PyObject *
2100PyFrozenSet_New(PyObject *iterable)
2101{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002102 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002103
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002104 if (iterable == NULL)
2105 args = PyTuple_New(0);
2106 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002107 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002108 if (args == NULL)
2109 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002110 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002111 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002112 return result;
2113}
2114
Neal Norwitz8c49c822006-03-04 18:41:19 +00002115Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002116PySet_Size(PyObject *anyset)
2117{
2118 if (!PyAnySet_Check(anyset)) {
2119 PyErr_BadInternalCall();
2120 return -1;
2121 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002122 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002123}
2124
2125int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002126PySet_Clear(PyObject *set)
2127{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002128 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002129 PyErr_BadInternalCall();
2130 return -1;
2131 }
2132 return set_clear_internal((PySetObject *)set);
2133}
2134
2135int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002136PySet_Contains(PyObject *anyset, PyObject *key)
2137{
2138 if (!PyAnySet_Check(anyset)) {
2139 PyErr_BadInternalCall();
2140 return -1;
2141 }
2142 return set_contains_key((PySetObject *)anyset, key);
2143}
2144
2145int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002146PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002147{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002148 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002149 PyErr_BadInternalCall();
2150 return -1;
2151 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002152 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002153}
2154
2155int
2156PySet_Add(PyObject *set, PyObject *key)
2157{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002158 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002159 PyErr_BadInternalCall();
2160 return -1;
2161 }
2162 return set_add_key((PySetObject *)set, key);
2163}
2164
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002165int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002166_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002167{
2168 setentry *entry_ptr;
2169
2170 if (!PyAnySet_Check(set)) {
2171 PyErr_BadInternalCall();
2172 return -1;
2173 }
2174 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2175 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002176 *key = entry_ptr->key;
2177 return 1;
2178}
2179
2180int
2181_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2182{
2183 setentry *entry;
2184
2185 if (!PyAnySet_Check(set)) {
2186 PyErr_BadInternalCall();
2187 return -1;
2188 }
2189 if (set_next((PySetObject *)set, pos, &entry) == 0)
2190 return 0;
2191 *key = entry->key;
2192 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002193 return 1;
2194}
2195
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002196PyObject *
2197PySet_Pop(PyObject *set)
2198{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002199 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002200 PyErr_BadInternalCall();
2201 return NULL;
2202 }
2203 return set_pop((PySetObject *)set);
2204}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002205
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002206int
2207_PySet_Update(PyObject *set, PyObject *iterable)
2208{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002209 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002210 PyErr_BadInternalCall();
2211 return -1;
2212 }
2213 return set_update_internal((PySetObject *)set, iterable);
2214}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002215
2216#ifdef Py_DEBUG
2217
2218/* Test code to be called with any three element set.
2219 Returns True and original set is restored. */
2220
2221#define assertRaises(call_return_value, exception) \
2222 do { \
2223 assert(call_return_value); \
2224 assert(PyErr_ExceptionMatches(exception)); \
2225 PyErr_Clear(); \
2226 } while(0)
2227
2228static PyObject *
2229test_c_api(PySetObject *so)
2230{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002231 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002232 char *s;
2233 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002234 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002235 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002236
2237 /* Verify preconditions and exercise type/size checks */
2238 assert(PyAnySet_Check(ob));
2239 assert(PyAnySet_CheckExact(ob));
2240 assert(!PyFrozenSet_CheckExact(ob));
2241 assert(PySet_Size(ob) == 3);
2242 assert(PySet_GET_SIZE(ob) == 3);
2243
2244 /* Raise TypeError for non-iterable constructor arguments */
2245 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2246 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2247
2248 /* Raise TypeError for unhashable key */
2249 dup = PySet_New(ob);
2250 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2251 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2252 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2253
2254 /* Exercise successful pop, contains, add, and discard */
2255 elem = PySet_Pop(ob);
2256 assert(PySet_Contains(ob, elem) == 0);
2257 assert(PySet_GET_SIZE(ob) == 2);
2258 assert(PySet_Add(ob, elem) == 0);
2259 assert(PySet_Contains(ob, elem) == 1);
2260 assert(PySet_GET_SIZE(ob) == 3);
2261 assert(PySet_Discard(ob, elem) == 1);
2262 assert(PySet_GET_SIZE(ob) == 2);
2263 assert(PySet_Discard(ob, elem) == 0);
2264 assert(PySet_GET_SIZE(ob) == 2);
2265
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002266 /* Exercise clear */
2267 dup2 = PySet_New(dup);
2268 assert(PySet_Clear(dup2) == 0);
2269 assert(PySet_Size(dup2) == 0);
2270 Py_DECREF(dup2);
2271
2272 /* Raise SystemError on clear or update of frozen set */
2273 f = PyFrozenSet_New(dup);
2274 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2275 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2276 Py_DECREF(f);
2277
2278 /* Exercise direct iteration */
2279 i = 0, count = 0;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002280 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2281 s = PyString_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002282 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2283 count++;
2284 }
2285 assert(count == 3);
2286
2287 /* Exercise updates */
2288 dup2 = PySet_New(NULL);
2289 assert(_PySet_Update(dup2, dup) == 0);
2290 assert(PySet_Size(dup2) == 3);
2291 assert(_PySet_Update(dup2, dup) == 0);
2292 assert(PySet_Size(dup2) == 3);
2293 Py_DECREF(dup2);
2294
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002295 /* Raise SystemError when self argument is not a set or frozenset. */
2296 t = PyTuple_New(0);
2297 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2298 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2299 Py_DECREF(t);
2300
2301 /* Raise SystemError when self argument is not a set. */
2302 f = PyFrozenSet_New(dup);
2303 assert(PySet_Size(f) == 3);
2304 assert(PyFrozenSet_CheckExact(f));
2305 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2306 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2307 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2308 Py_DECREF(f);
2309
2310 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002311 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2312 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002313 assert(PySet_GET_SIZE(ob) == 0);
2314 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2315
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002316 /* Restore the set from the copy using the PyNumber API */
2317 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2318 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002319
2320 /* Verify constructors accept NULL arguments */
2321 f = PySet_New(NULL);
2322 assert(f != NULL);
2323 assert(PySet_GET_SIZE(f) == 0);
2324 Py_DECREF(f);
2325 f = PyFrozenSet_New(NULL);
2326 assert(f != NULL);
2327 assert(PyFrozenSet_CheckExact(f));
2328 assert(PySet_GET_SIZE(f) == 0);
2329 Py_DECREF(f);
2330
2331 Py_DECREF(elem);
2332 Py_DECREF(dup);
2333 Py_RETURN_TRUE;
2334}
2335
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002336#undef assertRaises
2337
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002338#endif