blob: 3cbcd9ec8ce1ebcb0572fec12633d432eff70e97 [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öwis68192102007-07-21 06:55:02 +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
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +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
Armin Rigoe1709372006-04-12 17:06:05 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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 }
Neal Norwitza5ccda92006-10-28 21:16:54 +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.
Raymond Hettinger0c850862006-12-08 04:24:33 +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/*
Raymond Hettinger0c850862006-12-08 04:24:33 +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
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000269set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000271 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000272 setentry *oldtable, *newtable, *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Raymond Hettinger0c850862006-12-08 04:24:33 +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{
Neal Norwitz0f2783c2006-06-19 05:40:44 +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);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +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;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000449 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450 setentry small_copy[PySet_MINSIZE];
451#ifdef Py_DEBUG
Neal Norwitz0f2783c2006-06-19 05:40:44 +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
Raymond Hettinger334b5b22006-03-26 03:11:29 +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 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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öwis68192102007-07-21 06:55:02 +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 = ", ";
Raymond Hettinger53999102006-12-30 04:01:17 +0000575 int status = Py_ReprEnter((PyObject*)so);
576
577 if (status != 0) {
578 if (status < 0)
579 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000580 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000581 fprintf(fp, "%s(...)", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000582 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000583 return 0;
584 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000585
Brett Cannon01531592007-09-17 03:28:34 +0000586 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000587 fprintf(fp, "%s([", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000588 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000589 while (set_next(so, &pos, &entry)) {
Brett Cannon01531592007-09-17 03:28:34 +0000590 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591 fputs(emit, fp);
Brett Cannon01531592007-09-17 03:28:34 +0000592 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000593 emit = separator;
Raymond Hettinger53999102006-12-30 04:01:17 +0000594 if (PyObject_Print(entry->key, fp, 0) != 0) {
595 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000596 return -1;
Raymond Hettinger53999102006-12-30 04:01:17 +0000597 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000598 }
Brett Cannon01531592007-09-17 03:28:34 +0000599 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000600 fputs("])", fp);
Brett Cannon01531592007-09-17 03:28:34 +0000601 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000602 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000603 return 0;
604}
605
606static PyObject *
607set_repr(PySetObject *so)
608{
Raymond Hettinger53999102006-12-30 04:01:17 +0000609 PyObject *keys, *result=NULL, *listrepr;
610 int status = Py_ReprEnter((PyObject*)so);
611
612 if (status != 0) {
613 if (status < 0)
614 return NULL;
615 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
616 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000617
618 keys = PySequence_List((PyObject *)so);
619 if (keys == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000620 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000621 listrepr = PyObject_Repr(keys);
622 Py_DECREF(keys);
623 if (listrepr == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000624 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000625
626 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
627 PyString_AS_STRING(listrepr));
628 Py_DECREF(listrepr);
Raymond Hettinger53999102006-12-30 04:01:17 +0000629done:
630 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000631 return result;
632}
633
Martin v. Löwis18e16552006-02-15 17:27:45 +0000634static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000635set_len(PyObject *so)
636{
637 return ((PySetObject *)so)->used;
638}
639
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000640static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000641set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000642{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000643 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000644 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000645 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000646
647 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000648 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000649
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000650 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000651 if (other == so || other->used == 0)
652 /* a.update(a) or a.update({}); nothing to do */
653 return 0;
654 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000655 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000656 * that there will be no (or few) overlapping keys.
657 */
658 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
659 if (set_table_resize(so, (so->used + other->used)*2) != 0)
660 return -1;
661 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000662 for (i = 0; i <= other->mask; i++) {
663 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000664 if (entry->key != NULL &&
665 entry->key != dummy) {
666 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000667 if (set_insert_key(so, entry->key, entry->hash) == -1) {
668 Py_DECREF(entry->key);
669 return -1;
670 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000671 }
672 }
673 return 0;
674}
675
676static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000677set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000678{
679 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000680 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000681
682 if (!PyString_CheckExact(key) ||
683 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
684 hash = PyObject_Hash(key);
685 if (hash == -1)
686 return -1;
687 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000688 entry = (so->lookup)(so, key, hash);
689 if (entry == NULL)
690 return -1;
691 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000692 return key != NULL && key != dummy;
693}
694
Raymond Hettingerc991db22005-08-11 07:58:45 +0000695static int
696set_contains_entry(PySetObject *so, setentry *entry)
697{
698 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000699 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000700
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000701 lu_entry = (so->lookup)(so, entry->key, entry->hash);
702 if (lu_entry == NULL)
703 return -1;
704 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000705 return key != NULL && key != dummy;
706}
707
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000708static PyObject *
709set_pop(PySetObject *so)
710{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000711 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000712 register setentry *entry;
713 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000714
715 assert (PyAnySet_Check(so));
716 if (so->used == 0) {
717 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
718 return NULL;
719 }
720
721 /* Set entry to "the first" unused or dummy set entry. We abuse
722 * the hash field of slot 0 to hold a search finger:
723 * If slot 0 has a value, use slot 0.
724 * Else slot 0 is being used to hold a search finger,
725 * and we use its hash value as the first index to look.
726 */
727 entry = &so->table[0];
728 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000729 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000730 /* The hash field may be a real hash value, or it may be a
731 * legit search finger, or it may be a once-legit search
732 * finger that's out of bounds now because it wrapped around
733 * or the table shrunk -- simply make sure it's in bounds now.
734 */
735 if (i > so->mask || i < 1)
736 i = 1; /* skip slot 0 */
737 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
738 i++;
739 if (i > so->mask)
740 i = 1;
741 }
742 }
743 key = entry->key;
744 Py_INCREF(dummy);
745 entry->key = dummy;
746 so->used--;
747 so->table[0].hash = i + 1; /* next place to start */
748 return key;
749}
750
751PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
752
753static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000754set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000755{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000756 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000757 setentry *entry;
758
759 while (set_next(so, &pos, &entry))
760 Py_VISIT(entry->key);
761 return 0;
762}
763
764static long
765frozenset_hash(PyObject *self)
766{
767 PySetObject *so = (PySetObject *)self;
768 long h, hash = 1927868237L;
769 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000770 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000771
772 if (so->hash != -1)
773 return so->hash;
774
775 hash *= PySet_GET_SIZE(self) + 1;
776 while (set_next(so, &pos, &entry)) {
777 /* Work to increase the bit dispersion for closely spaced hash
778 values. The is important because some use cases have many
779 combinations of a small number of elements with nearby
780 hashes so that many distinct combinations collapse to only
781 a handful of distinct hash values. */
782 h = entry->hash;
783 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
784 }
785 hash = hash * 69069L + 907133923L;
786 if (hash == -1)
787 hash = 590923713L;
788 so->hash = hash;
789 return hash;
790}
791
792static long
793set_nohash(PyObject *self)
794{
795 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
796 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000797}
798
Raymond Hettingera9d99362005-08-05 00:01:15 +0000799/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801typedef struct {
802 PyObject_HEAD
803 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000804 Py_ssize_t si_used;
805 Py_ssize_t si_pos;
806 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807} setiterobject;
808
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809static void
810setiter_dealloc(setiterobject *si)
811{
812 Py_XDECREF(si->si_set);
813 PyObject_Del(si);
814}
815
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000816static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000817setiter_len(setiterobject *si)
818{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000819 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000820 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000821 len = si->len;
822 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000823}
824
Armin Rigof5b3e362006-02-11 21:32:43 +0000825PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000826
827static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000828 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000829 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000830};
831
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000832static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833{
834 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000835 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000836 register setentry *entry;
837 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000838
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000839 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000840 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000841 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000842
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000843 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000844 PyErr_SetString(PyExc_RuntimeError,
845 "Set changed size during iteration");
846 si->si_used = -1; /* Make this state sticky */
847 return NULL;
848 }
849
850 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000851 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000852 entry = so->table;
853 mask = so->mask;
854 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000855 i++;
856 si->si_pos = i+1;
857 if (i > mask)
858 goto fail;
859 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000860 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861 Py_INCREF(key);
862 return key;
863
864fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000865 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000866 si->si_set = NULL;
867 return NULL;
868}
869
Hye-Shik Change2956762005-08-01 05:26:41 +0000870static PyTypeObject PySetIter_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000871 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000872 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000873 sizeof(setiterobject), /* tp_basicsize */
874 0, /* tp_itemsize */
875 /* methods */
876 (destructor)setiter_dealloc, /* tp_dealloc */
877 0, /* tp_print */
878 0, /* tp_getattr */
879 0, /* tp_setattr */
880 0, /* tp_compare */
881 0, /* tp_repr */
882 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000883 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000884 0, /* tp_as_mapping */
885 0, /* tp_hash */
886 0, /* tp_call */
887 0, /* tp_str */
888 PyObject_GenericGetAttr, /* tp_getattro */
889 0, /* tp_setattro */
890 0, /* tp_as_buffer */
891 Py_TPFLAGS_DEFAULT, /* tp_flags */
892 0, /* tp_doc */
893 0, /* tp_traverse */
894 0, /* tp_clear */
895 0, /* tp_richcompare */
896 0, /* tp_weaklistoffset */
897 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000898 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000899 setiter_methods, /* tp_methods */
900 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000901};
902
Martin v. Löwis72d20672006-04-11 09:04:12 +0000903static PyObject *
904set_iter(PySetObject *so)
905{
906 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
907 if (si == NULL)
908 return NULL;
909 Py_INCREF(so);
910 si->si_set = so;
911 si->si_used = so->used;
912 si->si_pos = 0;
913 si->len = so->used;
914 return (PyObject *)si;
915}
916
Raymond Hettingerd7946662005-08-01 21:39:29 +0000917static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000918set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000919{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000920 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000921
Raymond Hettinger0e7a6322007-02-08 00:50:39 +0000922 if (PyAnySet_CheckExact(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000923 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000924
Raymond Hettingerdb67aef2007-02-01 21:02:59 +0000925 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000926 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000927 Py_ssize_t pos = 0;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000928 long hash;
Raymond Hettinger15cade02007-02-19 20:44:04 +0000929 Py_ssize_t dictsize = PyDict_Size(other);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000930
Raymond Hettinger15cade02007-02-19 20:44:04 +0000931 /* Do one big resize at the start, rather than
932 * incrementally resizing as we insert new keys. Expect
933 * that there will be no (or few) overlapping keys.
934 */
935 if (dictsize == -1)
936 return -1;
937 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
938 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
939 return -1;
940 }
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000941 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
942 setentry an_entry;
943
944 an_entry.hash = hash;
945 an_entry.key = key;
946 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000947 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000948 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000950 }
951
Raymond Hettingera38123e2003-11-24 22:18:49 +0000952 it = PyObject_GetIter(other);
953 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000954 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000955
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000956 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000957 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000958 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000959 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000960 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000961 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000962 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000963 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000964 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000965 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000966 return -1;
967 return 0;
968}
969
970static PyObject *
971set_update(PySetObject *so, PyObject *other)
972{
973 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000974 return NULL;
975 Py_RETURN_NONE;
976}
977
978PyDoc_STRVAR(update_doc,
979"Update a set with the union of itself and another.");
980
981static PyObject *
982make_new_set(PyTypeObject *type, PyObject *iterable)
983{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000984 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000985
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000986 if (dummy == NULL) { /* Auto-initialize dummy */
987 dummy = PyString_FromString("<dummy key>");
988 if (dummy == NULL)
989 return NULL;
990 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000991
992 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000993 if (num_free_sets &&
994 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
995 so = free_sets[--num_free_sets];
996 assert (so != NULL && PyAnySet_CheckExact(so));
Martin v. Löwis68192102007-07-21 06:55:02 +0000997 Py_Type(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000998 _Py_NewReference((PyObject *)so);
999 EMPTY_TO_MINSIZE(so);
1000 PyObject_GC_Track(so);
1001 } else {
1002 so = (PySetObject *)type->tp_alloc(type, 0);
1003 if (so == NULL)
1004 return NULL;
1005 /* tp_alloc has already zeroed the structure */
1006 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1007 INIT_NONZERO_SET_SLOTS(so);
1008 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001009
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001010 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001011 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001012
Raymond Hettingera38123e2003-11-24 22:18:49 +00001013 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001014 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001015 Py_DECREF(so);
1016 return NULL;
1017 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001018 }
1019
Raymond Hettingera690a992003-11-16 16:17:49 +00001020 return (PyObject *)so;
1021}
1022
Raymond Hettingerd7946662005-08-01 21:39:29 +00001023/* The empty frozenset is a singleton */
1024static PyObject *emptyfrozenset = NULL;
1025
Raymond Hettingera690a992003-11-16 16:17:49 +00001026static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001027frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001028{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001029 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001030
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001031 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001032 return NULL;
1033
Raymond Hettingera690a992003-11-16 16:17:49 +00001034 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1035 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001036
1037 if (type != &PyFrozenSet_Type)
1038 return make_new_set(type, iterable);
1039
1040 if (iterable != NULL) {
1041 /* frozenset(f) is idempotent */
1042 if (PyFrozenSet_CheckExact(iterable)) {
1043 Py_INCREF(iterable);
1044 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001045 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001046 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001047 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001048 return result;
1049 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001050 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001051 /* The empty frozenset is a singleton */
1052 if (emptyfrozenset == NULL)
1053 emptyfrozenset = make_new_set(type, NULL);
1054 Py_XINCREF(emptyfrozenset);
1055 return emptyfrozenset;
1056}
1057
1058void
1059PySet_Fini(void)
1060{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001061 PySetObject *so;
1062
1063 while (num_free_sets) {
1064 num_free_sets--;
1065 so = free_sets[num_free_sets];
1066 PyObject_GC_Del(so);
1067 }
Martin v. Löwised8f7832006-04-15 12:47:23 +00001068 Py_CLEAR(dummy);
1069 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001070}
1071
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001072static PyObject *
1073set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1074{
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001075 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001076 return NULL;
1077
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001078 return make_new_set(type, NULL);
1079}
1080
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001081/* set_swap_bodies() switches the contents of any two sets by moving their
1082 internal data pointers and, if needed, copying the internal smalltables.
1083 Semantically equivalent to:
1084
1085 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1086
1087 The function always succeeds and it leaves both objects in a stable state.
1088 Useful for creating temporary frozensets from sets for membership testing
1089 in __contains__(), discard(), and remove(). Also useful for operations
1090 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001091 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001092*/
1093
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001094static void
1095set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001096{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001097 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001098 setentry *u;
1099 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1100 setentry tab[PySet_MINSIZE];
1101 long h;
1102
1103 t = a->fill; a->fill = b->fill; b->fill = t;
1104 t = a->used; a->used = b->used; b->used = t;
1105 t = a->mask; a->mask = b->mask; b->mask = t;
1106
1107 u = a->table;
1108 if (a->table == a->smalltable)
1109 u = b->smalltable;
1110 a->table = b->table;
1111 if (b->table == b->smalltable)
1112 a->table = a->smalltable;
1113 b->table = u;
1114
1115 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1116
1117 if (a->table == a->smalltable || b->table == b->smalltable) {
1118 memcpy(tab, a->smalltable, sizeof(tab));
1119 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1120 memcpy(b->smalltable, tab, sizeof(tab));
1121 }
1122
Martin v. Löwis68192102007-07-21 06:55:02 +00001123 if (PyType_IsSubtype(Py_Type(a), &PyFrozenSet_Type) &&
1124 PyType_IsSubtype(Py_Type(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001125 h = a->hash; a->hash = b->hash; b->hash = h;
1126 } else {
1127 a->hash = -1;
1128 b->hash = -1;
1129 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001130}
1131
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001132static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001133set_copy(PySetObject *so)
1134{
Martin v. Löwis68192102007-07-21 06:55:02 +00001135 return make_new_set(Py_Type(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001136}
1137
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001138static PyObject *
1139frozenset_copy(PySetObject *so)
1140{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001141 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001142 Py_INCREF(so);
1143 return (PyObject *)so;
1144 }
1145 return set_copy(so);
1146}
1147
Raymond Hettingera690a992003-11-16 16:17:49 +00001148PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1149
1150static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001151set_clear(PySetObject *so)
1152{
1153 set_clear_internal(so);
1154 Py_RETURN_NONE;
1155}
1156
1157PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1158
1159static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001160set_union(PySetObject *so, PyObject *other)
1161{
1162 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001163
1164 result = (PySetObject *)set_copy(so);
1165 if (result == NULL)
1166 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001167 if ((PyObject *)so == other)
1168 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001169 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001170 Py_DECREF(result);
1171 return NULL;
1172 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001173 return (PyObject *)result;
1174}
1175
1176PyDoc_STRVAR(union_doc,
1177 "Return the union of two sets as a new set.\n\
1178\n\
1179(i.e. all elements that are in either set.)");
1180
1181static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001182set_or(PySetObject *so, PyObject *other)
1183{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001184 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001185 Py_INCREF(Py_NotImplemented);
1186 return Py_NotImplemented;
1187 }
1188 return set_union(so, other);
1189}
1190
1191static PyObject *
1192set_ior(PySetObject *so, PyObject *other)
1193{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001194 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001195 Py_INCREF(Py_NotImplemented);
1196 return Py_NotImplemented;
1197 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001198 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001199 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001200 Py_INCREF(so);
1201 return (PyObject *)so;
1202}
1203
1204static PyObject *
1205set_intersection(PySetObject *so, PyObject *other)
1206{
1207 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001208 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001209
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001210 if ((PyObject *)so == other)
1211 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001212
Martin v. Löwis68192102007-07-21 06:55:02 +00001213 result = (PySetObject *)make_new_set(Py_Type(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001214 if (result == NULL)
1215 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001216
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001217 if (PyAnySet_CheckExact(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001218 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001219 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001220
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001221 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001222 tmp = (PyObject *)so;
1223 so = (PySetObject *)other;
1224 other = tmp;
1225 }
1226
Raymond Hettingerc991db22005-08-11 07:58:45 +00001227 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001228 int rv = set_contains_entry(so, entry);
1229 if (rv == -1) {
1230 Py_DECREF(result);
1231 return NULL;
1232 }
1233 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001234 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001235 Py_DECREF(result);
1236 return NULL;
1237 }
1238 }
1239 }
1240 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001241 }
1242
Raymond Hettingera690a992003-11-16 16:17:49 +00001243 it = PyObject_GetIter(other);
1244 if (it == NULL) {
1245 Py_DECREF(result);
1246 return NULL;
1247 }
1248
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001249 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001250 int rv;
1251 setentry entry;
1252 long hash = PyObject_Hash(key);
1253
1254 if (hash == -1) {
1255 Py_DECREF(it);
1256 Py_DECREF(result);
1257 Py_DECREF(key);
1258 return NULL;
1259 }
1260 entry.hash = hash;
1261 entry.key = key;
1262 rv = set_contains_entry(so, &entry);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001263 if (rv == -1) {
1264 Py_DECREF(it);
1265 Py_DECREF(result);
1266 Py_DECREF(key);
1267 return NULL;
1268 }
1269 if (rv) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001270 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001271 Py_DECREF(it);
1272 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001273 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001274 return NULL;
1275 }
1276 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001277 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001278 }
1279 Py_DECREF(it);
1280 if (PyErr_Occurred()) {
1281 Py_DECREF(result);
1282 return NULL;
1283 }
1284 return (PyObject *)result;
1285}
1286
1287PyDoc_STRVAR(intersection_doc,
1288"Return the intersection of two sets as a new set.\n\
1289\n\
1290(i.e. all elements that are in both sets.)");
1291
1292static PyObject *
1293set_intersection_update(PySetObject *so, PyObject *other)
1294{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001295 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001296
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001297 tmp = set_intersection(so, other);
1298 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001299 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001300 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001301 Py_DECREF(tmp);
1302 Py_RETURN_NONE;
1303}
1304
1305PyDoc_STRVAR(intersection_update_doc,
1306"Update a set with the intersection of itself and another.");
1307
1308static PyObject *
1309set_and(PySetObject *so, PyObject *other)
1310{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001311 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001312 Py_INCREF(Py_NotImplemented);
1313 return Py_NotImplemented;
1314 }
1315 return set_intersection(so, other);
1316}
1317
1318static PyObject *
1319set_iand(PySetObject *so, PyObject *other)
1320{
1321 PyObject *result;
1322
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001323 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001324 Py_INCREF(Py_NotImplemented);
1325 return Py_NotImplemented;
1326 }
1327 result = set_intersection_update(so, other);
1328 if (result == NULL)
1329 return NULL;
1330 Py_DECREF(result);
1331 Py_INCREF(so);
1332 return (PyObject *)so;
1333}
1334
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001335static PyObject *
1336set_isdisjoint(PySetObject *so, PyObject *other)
1337{
1338 PyObject *key, *it, *tmp;
1339
1340 if ((PyObject *)so == other) {
1341 if (PySet_GET_SIZE(so) == 0)
1342 Py_RETURN_TRUE;
1343 else
1344 Py_RETURN_FALSE;
1345 }
1346
1347 if (PyAnySet_CheckExact(other)) {
1348 Py_ssize_t pos = 0;
1349 setentry *entry;
1350
1351 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1352 tmp = (PyObject *)so;
1353 so = (PySetObject *)other;
1354 other = tmp;
1355 }
1356 while (set_next((PySetObject *)other, &pos, &entry)) {
1357 int rv = set_contains_entry(so, entry);
1358 if (rv == -1)
1359 return NULL;
1360 if (rv)
1361 Py_RETURN_FALSE;
1362 }
1363 Py_RETURN_TRUE;
1364 }
1365
1366 it = PyObject_GetIter(other);
1367 if (it == NULL)
1368 return NULL;
1369
1370 while ((key = PyIter_Next(it)) != NULL) {
1371 int rv;
1372 setentry entry;
1373 long hash = PyObject_Hash(key);
1374
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001375 if (hash == -1) {
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001376 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001377 Py_DECREF(it);
1378 return NULL;
1379 }
1380 entry.hash = hash;
1381 entry.key = key;
1382 rv = set_contains_entry(so, &entry);
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001383 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001384 if (rv == -1) {
1385 Py_DECREF(it);
1386 return NULL;
1387 }
1388 if (rv) {
1389 Py_DECREF(it);
1390 Py_RETURN_FALSE;
1391 }
1392 }
1393 Py_DECREF(it);
1394 if (PyErr_Occurred())
1395 return NULL;
1396 Py_RETURN_TRUE;
1397}
1398
1399PyDoc_STRVAR(isdisjoint_doc,
1400"Return True if two sets have a null intersection.");
1401
Neal Norwitz6576bd82005-11-13 18:41:28 +00001402static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001403set_difference_update_internal(PySetObject *so, PyObject *other)
1404{
1405 if ((PyObject *)so == other)
1406 return set_clear_internal(so);
1407
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001408 if (PyAnySet_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001409 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001410 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001411
1412 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001413 if (set_discard_entry(so, entry) == -1)
1414 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001415 } else {
1416 PyObject *key, *it;
1417 it = PyObject_GetIter(other);
1418 if (it == NULL)
1419 return -1;
1420
1421 while ((key = PyIter_Next(it)) != NULL) {
1422 if (set_discard_key(so, key) == -1) {
1423 Py_DECREF(it);
1424 Py_DECREF(key);
1425 return -1;
1426 }
1427 Py_DECREF(key);
1428 }
1429 Py_DECREF(it);
1430 if (PyErr_Occurred())
1431 return -1;
1432 }
1433 /* If more than 1/5 are dummies, then resize them away. */
1434 if ((so->fill - so->used) * 5 < so->mask)
1435 return 0;
1436 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1437}
1438
Raymond Hettingera690a992003-11-16 16:17:49 +00001439static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001440set_difference_update(PySetObject *so, PyObject *other)
1441{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001442 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001443 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001444 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001445}
1446
1447PyDoc_STRVAR(difference_update_doc,
1448"Remove all elements of another set from this set.");
1449
1450static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001451set_difference(PySetObject *so, PyObject *other)
1452{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001453 PyObject *result;
1454 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001455 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001456
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001457 if (!PyAnySet_CheckExact(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001458 result = set_copy(so);
1459 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001460 return NULL;
1461 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001462 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001463 Py_DECREF(result);
1464 return NULL;
1465 }
1466
Martin v. Löwis68192102007-07-21 06:55:02 +00001467 result = make_new_set(Py_Type(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001468 if (result == NULL)
1469 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001470
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001471 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001472 while (set_next(so, &pos, &entry)) {
1473 setentry entrycopy;
1474 entrycopy.hash = entry->hash;
1475 entrycopy.key = entry->key;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001476 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001477 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1478 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001479 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001480 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001481 }
1482 }
1483 return result;
1484 }
1485
Raymond Hettingerc991db22005-08-11 07:58:45 +00001486 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001487 int rv = set_contains_entry((PySetObject *)other, entry);
1488 if (rv == -1) {
1489 Py_DECREF(result);
1490 return NULL;
1491 }
1492 if (!rv) {
1493 if (set_add_entry((PySetObject *)result, entry) == -1) {
1494 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001495 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001496 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001497 }
1498 }
1499 return result;
1500}
1501
1502PyDoc_STRVAR(difference_doc,
1503"Return the difference of two sets as a new set.\n\
1504\n\
1505(i.e. all elements that are in this set but not the other.)");
1506static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001507set_sub(PySetObject *so, PyObject *other)
1508{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001509 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001510 Py_INCREF(Py_NotImplemented);
1511 return Py_NotImplemented;
1512 }
1513 return set_difference(so, other);
1514}
1515
1516static PyObject *
1517set_isub(PySetObject *so, PyObject *other)
1518{
1519 PyObject *result;
1520
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001521 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001522 Py_INCREF(Py_NotImplemented);
1523 return Py_NotImplemented;
1524 }
1525 result = set_difference_update(so, other);
1526 if (result == NULL)
1527 return NULL;
1528 Py_DECREF(result);
1529 Py_INCREF(so);
1530 return (PyObject *)so;
1531}
1532
1533static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001534set_symmetric_difference_update(PySetObject *so, PyObject *other)
1535{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001536 PySetObject *otherset;
1537 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001538 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001539 setentry *entry;
1540
1541 if ((PyObject *)so == other)
1542 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001543
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001544 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001545 PyObject *value;
1546 int rv;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001547 long hash;
1548 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001549 setentry an_entry;
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001550
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001551 an_entry.hash = hash;
1552 an_entry.key = key;
1553 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001554 if (rv == -1)
1555 return NULL;
1556 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001557 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001558 return NULL;
1559 }
1560 }
1561 Py_RETURN_NONE;
1562 }
1563
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001564 if (PyAnySet_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001565 Py_INCREF(other);
1566 otherset = (PySetObject *)other;
1567 } else {
Martin v. Löwis68192102007-07-21 06:55:02 +00001568 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001569 if (otherset == NULL)
1570 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001571 }
1572
Raymond Hettingerc991db22005-08-11 07:58:45 +00001573 while (set_next(otherset, &pos, &entry)) {
1574 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001575 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001576 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001577 return NULL;
1578 }
1579 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001580 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001581 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001582 return NULL;
1583 }
1584 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001585 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001586 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001587 Py_RETURN_NONE;
1588}
1589
1590PyDoc_STRVAR(symmetric_difference_update_doc,
1591"Update a set with the symmetric difference of itself and another.");
1592
1593static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001594set_symmetric_difference(PySetObject *so, PyObject *other)
1595{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001596 PyObject *rv;
1597 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001598
Martin v. Löwis68192102007-07-21 06:55:02 +00001599 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001600 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001601 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001602 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1603 if (rv == NULL)
1604 return NULL;
1605 Py_DECREF(rv);
1606 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001607}
1608
1609PyDoc_STRVAR(symmetric_difference_doc,
1610"Return the symmetric difference of two sets as a new set.\n\
1611\n\
1612(i.e. all elements that are in exactly one of the sets.)");
1613
1614static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001615set_xor(PySetObject *so, PyObject *other)
1616{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001617 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001618 Py_INCREF(Py_NotImplemented);
1619 return Py_NotImplemented;
1620 }
1621 return set_symmetric_difference(so, other);
1622}
1623
1624static PyObject *
1625set_ixor(PySetObject *so, PyObject *other)
1626{
1627 PyObject *result;
1628
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001629 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001630 Py_INCREF(Py_NotImplemented);
1631 return Py_NotImplemented;
1632 }
1633 result = set_symmetric_difference_update(so, other);
1634 if (result == NULL)
1635 return NULL;
1636 Py_DECREF(result);
1637 Py_INCREF(so);
1638 return (PyObject *)so;
1639}
1640
1641static PyObject *
1642set_issubset(PySetObject *so, PyObject *other)
1643{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001644 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001645 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001646
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001647 if (!PyAnySet_CheckExact(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001648 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001649 tmp = make_new_set(&PySet_Type, other);
1650 if (tmp == NULL)
1651 return NULL;
1652 result = set_issubset(so, tmp);
1653 Py_DECREF(tmp);
1654 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001655 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001656 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001657 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001658
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001659 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001660 int rv = set_contains_entry((PySetObject *)other, entry);
1661 if (rv == -1)
1662 return NULL;
1663 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001664 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001665 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001666 Py_RETURN_TRUE;
1667}
1668
1669PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1670
1671static PyObject *
1672set_issuperset(PySetObject *so, PyObject *other)
1673{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001674 PyObject *tmp, *result;
1675
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001676 if (!PyAnySet_CheckExact(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001677 tmp = make_new_set(&PySet_Type, other);
1678 if (tmp == NULL)
1679 return NULL;
1680 result = set_issuperset(so, tmp);
1681 Py_DECREF(tmp);
1682 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001683 }
1684 return set_issubset((PySetObject *)other, (PyObject *)so);
1685}
1686
1687PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1688
Raymond Hettingera690a992003-11-16 16:17:49 +00001689static PyObject *
1690set_richcompare(PySetObject *v, PyObject *w, int op)
1691{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001692 PyObject *r1, *r2;
1693
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001694 if(!PyAnySet_Check(w)) {
1695 if (op == Py_EQ)
1696 Py_RETURN_FALSE;
1697 if (op == Py_NE)
1698 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001699 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1700 return NULL;
1701 }
1702 switch (op) {
1703 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001704 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001705 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001706 if (v->hash != -1 &&
1707 ((PySetObject *)w)->hash != -1 &&
1708 v->hash != ((PySetObject *)w)->hash)
1709 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001710 return set_issubset(v, w);
1711 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001712 r1 = set_richcompare(v, w, Py_EQ);
1713 if (r1 == NULL)
1714 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001715 r2 = PyBool_FromLong(PyObject_Not(r1));
1716 Py_DECREF(r1);
1717 return r2;
1718 case Py_LE:
1719 return set_issubset(v, w);
1720 case Py_GE:
1721 return set_issuperset(v, w);
1722 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001723 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001724 Py_RETURN_FALSE;
1725 return set_issubset(v, w);
1726 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001727 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001728 Py_RETURN_FALSE;
1729 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001730 }
1731 Py_INCREF(Py_NotImplemented);
1732 return Py_NotImplemented;
1733}
1734
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001735static int
Georg Brandl347b3002006-03-30 11:57:00 +00001736set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001737{
1738 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1739 return -1;
1740}
1741
Raymond Hettingera690a992003-11-16 16:17:49 +00001742static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001743set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001744{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001745 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001746 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001747 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001748}
1749
1750PyDoc_STRVAR(add_doc,
1751"Add an element to a set.\n\
1752\n\
1753This has no effect if the element is already present.");
1754
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001755static int
1756set_contains(PySetObject *so, PyObject *key)
1757{
1758 PyObject *tmpkey;
1759 int rv;
1760
1761 rv = set_contains_key(so, key);
1762 if (rv == -1) {
1763 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1764 return -1;
1765 PyErr_Clear();
1766 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1767 if (tmpkey == NULL)
1768 return -1;
1769 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1770 rv = set_contains(so, tmpkey);
1771 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1772 Py_DECREF(tmpkey);
1773 }
1774 return rv;
1775}
1776
1777static PyObject *
1778set_direct_contains(PySetObject *so, PyObject *key)
1779{
1780 long result;
1781
1782 result = set_contains(so, key);
1783 if (result == -1)
1784 return NULL;
1785 return PyBool_FromLong(result);
1786}
1787
1788PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1789
Raymond Hettingera690a992003-11-16 16:17:49 +00001790static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001791set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001792{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001793 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001794 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001795
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001796 rv = set_discard_key(so, key);
1797 if (rv == -1) {
1798 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1799 return NULL;
1800 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001801 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1802 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001803 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001804 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001805 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001806 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001807 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001808 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001809 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +00001810 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001811 return NULL;
1812 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001813 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001814}
1815
1816PyDoc_STRVAR(remove_doc,
1817"Remove an element from a set; it must be a member.\n\
1818\n\
1819If the element is not a member, raise a KeyError.");
1820
1821static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001822set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001823{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001824 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001825 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001826
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001827 rv = set_discard_key(so, key);
1828 if (rv == -1) {
1829 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1830 return NULL;
1831 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001832 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1833 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001834 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001835 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001836 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001837 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001838 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001839 return result;
1840 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001841 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001842}
1843
1844PyDoc_STRVAR(discard_doc,
1845"Remove an element from a set if it is a member.\n\
1846\n\
1847If the element is not a member, do nothing.");
1848
1849static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001850set_reduce(PySetObject *so)
1851{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001852 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001853
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001854 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001855 if (keys == NULL)
1856 goto done;
1857 args = PyTuple_Pack(1, keys);
1858 if (args == NULL)
1859 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001860 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1861 if (dict == NULL) {
1862 PyErr_Clear();
1863 dict = Py_None;
1864 Py_INCREF(dict);
1865 }
Martin v. Löwis68192102007-07-21 06:55:02 +00001866 result = PyTuple_Pack(3, Py_Type(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001867done:
1868 Py_XDECREF(args);
1869 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001870 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001871 return result;
1872}
1873
1874PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1875
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001876static int
1877set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1878{
1879 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001880
1881 if (!PyAnySet_Check(self))
1882 return -1;
Martin v. Löwis68192102007-07-21 06:55:02 +00001883 if (!PyArg_UnpackTuple(args, Py_Type(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001884 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001885 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001886 self->hash = -1;
1887 if (iterable == NULL)
1888 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001889 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001890}
1891
Raymond Hettingera690a992003-11-16 16:17:49 +00001892static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001893 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001894 0, /* sq_concat */
1895 0, /* sq_repeat */
1896 0, /* sq_item */
1897 0, /* sq_slice */
1898 0, /* sq_ass_item */
1899 0, /* sq_ass_slice */
1900 (objobjproc)set_contains, /* sq_contains */
1901};
1902
1903/* set object ********************************************************/
1904
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001905#ifdef Py_DEBUG
1906static PyObject *test_c_api(PySetObject *so);
1907
1908PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1909All is well if assertions don't fail.");
1910#endif
1911
Raymond Hettingera690a992003-11-16 16:17:49 +00001912static PyMethodDef set_methods[] = {
1913 {"add", (PyCFunction)set_add, METH_O,
1914 add_doc},
1915 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1916 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001917 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001918 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001919 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1920 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001921 {"discard", (PyCFunction)set_discard, METH_O,
1922 discard_doc},
1923 {"difference", (PyCFunction)set_difference, METH_O,
1924 difference_doc},
1925 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1926 difference_update_doc},
1927 {"intersection",(PyCFunction)set_intersection, METH_O,
1928 intersection_doc},
1929 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1930 intersection_update_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001931 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1932 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001933 {"issubset", (PyCFunction)set_issubset, METH_O,
1934 issubset_doc},
1935 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1936 issuperset_doc},
1937 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1938 pop_doc},
1939 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1940 reduce_doc},
1941 {"remove", (PyCFunction)set_remove, METH_O,
1942 remove_doc},
1943 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1944 symmetric_difference_doc},
1945 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1946 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001947#ifdef Py_DEBUG
1948 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1949 test_c_api_doc},
1950#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001951 {"union", (PyCFunction)set_union, METH_O,
1952 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001953 {"update", (PyCFunction)set_update, METH_O,
1954 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001955 {NULL, NULL} /* sentinel */
1956};
1957
1958static PyNumberMethods set_as_number = {
1959 0, /*nb_add*/
1960 (binaryfunc)set_sub, /*nb_subtract*/
1961 0, /*nb_multiply*/
1962 0, /*nb_divide*/
1963 0, /*nb_remainder*/
1964 0, /*nb_divmod*/
1965 0, /*nb_power*/
1966 0, /*nb_negative*/
1967 0, /*nb_positive*/
1968 0, /*nb_absolute*/
1969 0, /*nb_nonzero*/
1970 0, /*nb_invert*/
1971 0, /*nb_lshift*/
1972 0, /*nb_rshift*/
1973 (binaryfunc)set_and, /*nb_and*/
1974 (binaryfunc)set_xor, /*nb_xor*/
1975 (binaryfunc)set_or, /*nb_or*/
1976 0, /*nb_coerce*/
1977 0, /*nb_int*/
1978 0, /*nb_long*/
1979 0, /*nb_float*/
1980 0, /*nb_oct*/
1981 0, /*nb_hex*/
1982 0, /*nb_inplace_add*/
1983 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1984 0, /*nb_inplace_multiply*/
1985 0, /*nb_inplace_divide*/
1986 0, /*nb_inplace_remainder*/
1987 0, /*nb_inplace_power*/
1988 0, /*nb_inplace_lshift*/
1989 0, /*nb_inplace_rshift*/
1990 (binaryfunc)set_iand, /*nb_inplace_and*/
1991 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1992 (binaryfunc)set_ior, /*nb_inplace_or*/
1993};
1994
1995PyDoc_STRVAR(set_doc,
1996"set(iterable) --> set object\n\
1997\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001998Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001999
2000PyTypeObject PySet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002001 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002002 "set", /* tp_name */
2003 sizeof(PySetObject), /* tp_basicsize */
2004 0, /* tp_itemsize */
2005 /* methods */
2006 (destructor)set_dealloc, /* tp_dealloc */
2007 (printfunc)set_tp_print, /* tp_print */
2008 0, /* tp_getattr */
2009 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002010 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002011 (reprfunc)set_repr, /* tp_repr */
2012 &set_as_number, /* tp_as_number */
2013 &set_as_sequence, /* tp_as_sequence */
2014 0, /* tp_as_mapping */
2015 set_nohash, /* tp_hash */
2016 0, /* tp_call */
2017 0, /* tp_str */
2018 PyObject_GenericGetAttr, /* tp_getattro */
2019 0, /* tp_setattro */
2020 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002021 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002022 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002023 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002024 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002025 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002026 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002027 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002028 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002029 0, /* tp_iternext */
2030 set_methods, /* tp_methods */
2031 0, /* tp_members */
2032 0, /* tp_getset */
2033 0, /* tp_base */
2034 0, /* tp_dict */
2035 0, /* tp_descr_get */
2036 0, /* tp_descr_set */
2037 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002038 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002039 PyType_GenericAlloc, /* tp_alloc */
2040 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002041 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002042};
2043
2044/* frozenset object ********************************************************/
2045
2046
2047static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002048 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002049 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002050 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002051 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002052 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002053 difference_doc},
2054 {"intersection",(PyCFunction)set_intersection, METH_O,
2055 intersection_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00002056 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2057 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002058 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002059 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002060 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002061 issuperset_doc},
2062 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2063 reduce_doc},
2064 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2065 symmetric_difference_doc},
2066 {"union", (PyCFunction)set_union, METH_O,
2067 union_doc},
2068 {NULL, NULL} /* sentinel */
2069};
2070
2071static PyNumberMethods frozenset_as_number = {
2072 0, /*nb_add*/
2073 (binaryfunc)set_sub, /*nb_subtract*/
2074 0, /*nb_multiply*/
2075 0, /*nb_divide*/
2076 0, /*nb_remainder*/
2077 0, /*nb_divmod*/
2078 0, /*nb_power*/
2079 0, /*nb_negative*/
2080 0, /*nb_positive*/
2081 0, /*nb_absolute*/
2082 0, /*nb_nonzero*/
2083 0, /*nb_invert*/
2084 0, /*nb_lshift*/
2085 0, /*nb_rshift*/
2086 (binaryfunc)set_and, /*nb_and*/
2087 (binaryfunc)set_xor, /*nb_xor*/
2088 (binaryfunc)set_or, /*nb_or*/
2089};
2090
2091PyDoc_STRVAR(frozenset_doc,
2092"frozenset(iterable) --> frozenset object\n\
2093\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002094Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002095
2096PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002097 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002098 "frozenset", /* tp_name */
2099 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002100 0, /* tp_itemsize */
2101 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002102 (destructor)set_dealloc, /* tp_dealloc */
2103 (printfunc)set_tp_print, /* tp_print */
2104 0, /* tp_getattr */
2105 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002106 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002107 (reprfunc)set_repr, /* tp_repr */
2108 &frozenset_as_number, /* tp_as_number */
2109 &set_as_sequence, /* tp_as_sequence */
2110 0, /* tp_as_mapping */
2111 frozenset_hash, /* tp_hash */
2112 0, /* tp_call */
2113 0, /* tp_str */
2114 PyObject_GenericGetAttr, /* tp_getattro */
2115 0, /* tp_setattro */
2116 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002117 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002118 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002119 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002120 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002121 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002122 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002123 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002124 (getiterfunc)set_iter, /* tp_iter */
2125 0, /* tp_iternext */
2126 frozenset_methods, /* tp_methods */
2127 0, /* tp_members */
2128 0, /* tp_getset */
2129 0, /* tp_base */
2130 0, /* tp_dict */
2131 0, /* tp_descr_get */
2132 0, /* tp_descr_set */
2133 0, /* tp_dictoffset */
2134 0, /* tp_init */
2135 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002136 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002137 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002138};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002139
2140
2141/***** C API functions *************************************************/
2142
2143PyObject *
2144PySet_New(PyObject *iterable)
2145{
2146 return make_new_set(&PySet_Type, iterable);
2147}
2148
2149PyObject *
2150PyFrozenSet_New(PyObject *iterable)
2151{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002152 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002153
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002154 if (iterable == NULL)
2155 args = PyTuple_New(0);
2156 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002157 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002158 if (args == NULL)
2159 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002160 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002161 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002162 return result;
2163}
2164
Neal Norwitz8c49c822006-03-04 18:41:19 +00002165Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002166PySet_Size(PyObject *anyset)
2167{
2168 if (!PyAnySet_Check(anyset)) {
2169 PyErr_BadInternalCall();
2170 return -1;
2171 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002172 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002173}
2174
2175int
Barry Warsaw176014f2006-03-30 22:45:35 +00002176PySet_Clear(PyObject *set)
2177{
Martin v. Löwis68192102007-07-21 06:55:02 +00002178 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002179 PyErr_BadInternalCall();
2180 return -1;
2181 }
2182 return set_clear_internal((PySetObject *)set);
2183}
2184
2185int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002186PySet_Contains(PyObject *anyset, PyObject *key)
2187{
2188 if (!PyAnySet_Check(anyset)) {
2189 PyErr_BadInternalCall();
2190 return -1;
2191 }
2192 return set_contains_key((PySetObject *)anyset, key);
2193}
2194
2195int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002196PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002197{
Martin v. Löwis68192102007-07-21 06:55:02 +00002198 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002199 PyErr_BadInternalCall();
2200 return -1;
2201 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002202 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002203}
2204
2205int
2206PySet_Add(PyObject *set, PyObject *key)
2207{
Martin v. Löwis68192102007-07-21 06:55:02 +00002208 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002209 PyErr_BadInternalCall();
2210 return -1;
2211 }
2212 return set_add_key((PySetObject *)set, key);
2213}
2214
Barry Warsaw176014f2006-03-30 22:45:35 +00002215int
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002216_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Barry Warsaw176014f2006-03-30 22:45:35 +00002217{
2218 setentry *entry_ptr;
2219
2220 if (!PyAnySet_Check(set)) {
2221 PyErr_BadInternalCall();
2222 return -1;
2223 }
2224 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2225 return 0;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002226 *key = entry_ptr->key;
2227 return 1;
2228}
2229
2230int
2231_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2232{
2233 setentry *entry;
2234
2235 if (!PyAnySet_Check(set)) {
2236 PyErr_BadInternalCall();
2237 return -1;
2238 }
2239 if (set_next((PySetObject *)set, pos, &entry) == 0)
2240 return 0;
2241 *key = entry->key;
2242 *hash = entry->hash;
Barry Warsaw176014f2006-03-30 22:45:35 +00002243 return 1;
2244}
2245
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002246PyObject *
2247PySet_Pop(PyObject *set)
2248{
Martin v. Löwis68192102007-07-21 06:55:02 +00002249 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002250 PyErr_BadInternalCall();
2251 return NULL;
2252 }
2253 return set_pop((PySetObject *)set);
2254}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002255
Barry Warsaw176014f2006-03-30 22:45:35 +00002256int
2257_PySet_Update(PyObject *set, PyObject *iterable)
2258{
Martin v. Löwis68192102007-07-21 06:55:02 +00002259 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002260 PyErr_BadInternalCall();
2261 return -1;
2262 }
2263 return set_update_internal((PySetObject *)set, iterable);
2264}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002265
2266#ifdef Py_DEBUG
2267
2268/* Test code to be called with any three element set.
2269 Returns True and original set is restored. */
2270
2271#define assertRaises(call_return_value, exception) \
2272 do { \
2273 assert(call_return_value); \
2274 assert(PyErr_ExceptionMatches(exception)); \
2275 PyErr_Clear(); \
2276 } while(0)
2277
2278static PyObject *
2279test_c_api(PySetObject *so)
2280{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002281 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002282 char *s;
2283 Py_ssize_t i;
Guido van Rossum360496d2007-05-10 17:20:15 +00002284 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Barry Warsaw176014f2006-03-30 22:45:35 +00002285 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002286
2287 /* Verify preconditions and exercise type/size checks */
2288 assert(PyAnySet_Check(ob));
2289 assert(PyAnySet_CheckExact(ob));
2290 assert(!PyFrozenSet_CheckExact(ob));
2291 assert(PySet_Size(ob) == 3);
2292 assert(PySet_GET_SIZE(ob) == 3);
2293
2294 /* Raise TypeError for non-iterable constructor arguments */
2295 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2296 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2297
2298 /* Raise TypeError for unhashable key */
2299 dup = PySet_New(ob);
2300 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2301 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2302 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2303
2304 /* Exercise successful pop, contains, add, and discard */
2305 elem = PySet_Pop(ob);
2306 assert(PySet_Contains(ob, elem) == 0);
2307 assert(PySet_GET_SIZE(ob) == 2);
2308 assert(PySet_Add(ob, elem) == 0);
2309 assert(PySet_Contains(ob, elem) == 1);
2310 assert(PySet_GET_SIZE(ob) == 3);
2311 assert(PySet_Discard(ob, elem) == 1);
2312 assert(PySet_GET_SIZE(ob) == 2);
2313 assert(PySet_Discard(ob, elem) == 0);
2314 assert(PySet_GET_SIZE(ob) == 2);
2315
Barry Warsaw176014f2006-03-30 22:45:35 +00002316 /* Exercise clear */
2317 dup2 = PySet_New(dup);
2318 assert(PySet_Clear(dup2) == 0);
2319 assert(PySet_Size(dup2) == 0);
2320 Py_DECREF(dup2);
2321
2322 /* Raise SystemError on clear or update of frozen set */
2323 f = PyFrozenSet_New(dup);
2324 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2325 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2326 Py_DECREF(f);
2327
2328 /* Exercise direct iteration */
2329 i = 0, count = 0;
Guido van Rossum360496d2007-05-10 17:20:15 +00002330 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2331 s = PyString_AsString(x);
Barry Warsaw176014f2006-03-30 22:45:35 +00002332 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2333 count++;
2334 }
2335 assert(count == 3);
2336
2337 /* Exercise updates */
2338 dup2 = PySet_New(NULL);
2339 assert(_PySet_Update(dup2, dup) == 0);
2340 assert(PySet_Size(dup2) == 3);
2341 assert(_PySet_Update(dup2, dup) == 0);
2342 assert(PySet_Size(dup2) == 3);
2343 Py_DECREF(dup2);
2344
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002345 /* Raise SystemError when self argument is not a set or frozenset. */
2346 t = PyTuple_New(0);
2347 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2348 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2349 Py_DECREF(t);
2350
2351 /* Raise SystemError when self argument is not a set. */
2352 f = PyFrozenSet_New(dup);
2353 assert(PySet_Size(f) == 3);
2354 assert(PyFrozenSet_CheckExact(f));
2355 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2356 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2357 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2358 Py_DECREF(f);
2359
2360 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002361 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2362 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002363 assert(PySet_GET_SIZE(ob) == 0);
2364 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2365
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002366 /* Restore the set from the copy using the PyNumber API */
2367 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2368 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002369
2370 /* Verify constructors accept NULL arguments */
2371 f = PySet_New(NULL);
2372 assert(f != NULL);
2373 assert(PySet_GET_SIZE(f) == 0);
2374 Py_DECREF(f);
2375 f = PyFrozenSet_New(NULL);
2376 assert(f != NULL);
2377 assert(PyFrozenSet_CheckExact(f));
2378 assert(PySet_GET_SIZE(f) == 0);
2379 Py_DECREF(f);
2380
2381 Py_DECREF(elem);
2382 Py_DECREF(dup);
2383 Py_RETURN_TRUE;
2384}
2385
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002386#undef assertRaises
2387
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002388#endif