blob: f0a11caecb01ecddb2d8833a38f28d50f934404d [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;
580 fprintf(fp, "%s(...)", so->ob_type->tp_name);
581 return 0;
582 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000583
584 fprintf(fp, "%s([", so->ob_type->tp_name);
585 while (set_next(so, &pos, &entry)) {
586 fputs(emit, fp);
587 emit = separator;
Raymond Hettinger53999102006-12-30 04:01:17 +0000588 if (PyObject_Print(entry->key, fp, 0) != 0) {
589 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000590 return -1;
Raymond Hettinger53999102006-12-30 04:01:17 +0000591 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000592 }
593 fputs("])", fp);
Raymond Hettinger53999102006-12-30 04:01:17 +0000594 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000595 return 0;
596}
597
598static PyObject *
599set_repr(PySetObject *so)
600{
Raymond Hettinger53999102006-12-30 04:01:17 +0000601 PyObject *keys, *result=NULL, *listrepr;
602 int status = Py_ReprEnter((PyObject*)so);
603
604 if (status != 0) {
605 if (status < 0)
606 return NULL;
607 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
608 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000609
610 keys = PySequence_List((PyObject *)so);
611 if (keys == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000612 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000613 listrepr = PyObject_Repr(keys);
614 Py_DECREF(keys);
615 if (listrepr == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000616 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000617
618 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
619 PyString_AS_STRING(listrepr));
620 Py_DECREF(listrepr);
Raymond Hettinger53999102006-12-30 04:01:17 +0000621done:
622 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000623 return result;
624}
625
Martin v. Löwis18e16552006-02-15 17:27:45 +0000626static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000627set_len(PyObject *so)
628{
629 return ((PySetObject *)so)->used;
630}
631
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000632static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000633set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000634{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000635 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000636 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000637 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000638
639 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000640 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000641
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000642 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000643 if (other == so || other->used == 0)
644 /* a.update(a) or a.update({}); nothing to do */
645 return 0;
646 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000647 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648 * that there will be no (or few) overlapping keys.
649 */
650 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
651 if (set_table_resize(so, (so->used + other->used)*2) != 0)
652 return -1;
653 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000654 for (i = 0; i <= other->mask; i++) {
655 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000656 if (entry->key != NULL &&
657 entry->key != dummy) {
658 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000659 if (set_insert_key(so, entry->key, entry->hash) == -1) {
660 Py_DECREF(entry->key);
661 return -1;
662 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000663 }
664 }
665 return 0;
666}
667
668static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000669set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670{
671 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000672 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673
674 if (!PyString_CheckExact(key) ||
675 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
676 hash = PyObject_Hash(key);
677 if (hash == -1)
678 return -1;
679 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000680 entry = (so->lookup)(so, key, hash);
681 if (entry == NULL)
682 return -1;
683 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000684 return key != NULL && key != dummy;
685}
686
Raymond Hettingerc991db22005-08-11 07:58:45 +0000687static int
688set_contains_entry(PySetObject *so, setentry *entry)
689{
690 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000691 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000692
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000693 lu_entry = (so->lookup)(so, entry->key, entry->hash);
694 if (lu_entry == NULL)
695 return -1;
696 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000697 return key != NULL && key != dummy;
698}
699
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000700static PyObject *
701set_pop(PySetObject *so)
702{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000703 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000704 register setentry *entry;
705 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000706
707 assert (PyAnySet_Check(so));
708 if (so->used == 0) {
709 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
710 return NULL;
711 }
712
713 /* Set entry to "the first" unused or dummy set entry. We abuse
714 * the hash field of slot 0 to hold a search finger:
715 * If slot 0 has a value, use slot 0.
716 * Else slot 0 is being used to hold a search finger,
717 * and we use its hash value as the first index to look.
718 */
719 entry = &so->table[0];
720 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000721 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000722 /* The hash field may be a real hash value, or it may be a
723 * legit search finger, or it may be a once-legit search
724 * finger that's out of bounds now because it wrapped around
725 * or the table shrunk -- simply make sure it's in bounds now.
726 */
727 if (i > so->mask || i < 1)
728 i = 1; /* skip slot 0 */
729 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
730 i++;
731 if (i > so->mask)
732 i = 1;
733 }
734 }
735 key = entry->key;
736 Py_INCREF(dummy);
737 entry->key = dummy;
738 so->used--;
739 so->table[0].hash = i + 1; /* next place to start */
740 return key;
741}
742
743PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
744
745static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000746set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000747{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000748 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000749 setentry *entry;
750
751 while (set_next(so, &pos, &entry))
752 Py_VISIT(entry->key);
753 return 0;
754}
755
756static long
757frozenset_hash(PyObject *self)
758{
759 PySetObject *so = (PySetObject *)self;
760 long h, hash = 1927868237L;
761 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000762 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000763
764 if (so->hash != -1)
765 return so->hash;
766
767 hash *= PySet_GET_SIZE(self) + 1;
768 while (set_next(so, &pos, &entry)) {
769 /* Work to increase the bit dispersion for closely spaced hash
770 values. The is important because some use cases have many
771 combinations of a small number of elements with nearby
772 hashes so that many distinct combinations collapse to only
773 a handful of distinct hash values. */
774 h = entry->hash;
775 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
776 }
777 hash = hash * 69069L + 907133923L;
778 if (hash == -1)
779 hash = 590923713L;
780 so->hash = hash;
781 return hash;
782}
783
784static long
785set_nohash(PyObject *self)
786{
787 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
788 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000789}
790
Raymond Hettingera9d99362005-08-05 00:01:15 +0000791/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793typedef struct {
794 PyObject_HEAD
795 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000796 Py_ssize_t si_used;
797 Py_ssize_t si_pos;
798 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799} setiterobject;
800
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801static void
802setiter_dealloc(setiterobject *si)
803{
804 Py_XDECREF(si->si_set);
805 PyObject_Del(si);
806}
807
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000808static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809setiter_len(setiterobject *si)
810{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000811 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000813 len = si->len;
814 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815}
816
Armin Rigof5b3e362006-02-11 21:32:43 +0000817PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000818
819static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000820 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000821 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822};
823
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000824static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825{
826 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000827 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000828 register setentry *entry;
829 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000830
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000831 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000833 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000834
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000835 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000836 PyErr_SetString(PyExc_RuntimeError,
837 "Set changed size during iteration");
838 si->si_used = -1; /* Make this state sticky */
839 return NULL;
840 }
841
842 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000843 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000844 entry = so->table;
845 mask = so->mask;
846 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847 i++;
848 si->si_pos = i+1;
849 if (i > mask)
850 goto fail;
851 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000852 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853 Py_INCREF(key);
854 return key;
855
856fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000857 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000858 si->si_set = NULL;
859 return NULL;
860}
861
Hye-Shik Change2956762005-08-01 05:26:41 +0000862static PyTypeObject PySetIter_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000863 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000864 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000865 sizeof(setiterobject), /* tp_basicsize */
866 0, /* tp_itemsize */
867 /* methods */
868 (destructor)setiter_dealloc, /* tp_dealloc */
869 0, /* tp_print */
870 0, /* tp_getattr */
871 0, /* tp_setattr */
872 0, /* tp_compare */
873 0, /* tp_repr */
874 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000875 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000876 0, /* tp_as_mapping */
877 0, /* tp_hash */
878 0, /* tp_call */
879 0, /* tp_str */
880 PyObject_GenericGetAttr, /* tp_getattro */
881 0, /* tp_setattro */
882 0, /* tp_as_buffer */
883 Py_TPFLAGS_DEFAULT, /* tp_flags */
884 0, /* tp_doc */
885 0, /* tp_traverse */
886 0, /* tp_clear */
887 0, /* tp_richcompare */
888 0, /* tp_weaklistoffset */
889 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000890 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000891 setiter_methods, /* tp_methods */
892 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000893};
894
Martin v. Löwis72d20672006-04-11 09:04:12 +0000895static PyObject *
896set_iter(PySetObject *so)
897{
898 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
899 if (si == NULL)
900 return NULL;
901 Py_INCREF(so);
902 si->si_set = so;
903 si->si_used = so->used;
904 si->si_pos = 0;
905 si->len = so->used;
906 return (PyObject *)si;
907}
908
Raymond Hettingerd7946662005-08-01 21:39:29 +0000909static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000910set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000911{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000912 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000913
Raymond Hettinger0e7a6322007-02-08 00:50:39 +0000914 if (PyAnySet_CheckExact(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000915 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000916
Raymond Hettingerdb67aef2007-02-01 21:02:59 +0000917 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000918 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919 Py_ssize_t pos = 0;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000920 long hash;
Raymond Hettinger15cade02007-02-19 20:44:04 +0000921 Py_ssize_t dictsize = PyDict_Size(other);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000922
Raymond Hettinger15cade02007-02-19 20:44:04 +0000923 /* Do one big resize at the start, rather than
924 * incrementally resizing as we insert new keys. Expect
925 * that there will be no (or few) overlapping keys.
926 */
927 if (dictsize == -1)
928 return -1;
929 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
930 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
931 return -1;
932 }
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000933 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
934 setentry an_entry;
935
936 an_entry.hash = hash;
937 an_entry.key = key;
938 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000939 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000940 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000941 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000942 }
943
Raymond Hettingera38123e2003-11-24 22:18:49 +0000944 it = PyObject_GetIter(other);
945 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000946 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000947
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000948 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000949 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000950 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000951 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000953 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000954 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000955 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000956 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000957 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000958 return -1;
959 return 0;
960}
961
962static PyObject *
963set_update(PySetObject *so, PyObject *other)
964{
965 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000966 return NULL;
967 Py_RETURN_NONE;
968}
969
970PyDoc_STRVAR(update_doc,
971"Update a set with the union of itself and another.");
972
973static PyObject *
974make_new_set(PyTypeObject *type, PyObject *iterable)
975{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000976 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000977
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000978 if (dummy == NULL) { /* Auto-initialize dummy */
979 dummy = PyString_FromString("<dummy key>");
980 if (dummy == NULL)
981 return NULL;
982 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000983
984 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000985 if (num_free_sets &&
986 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
987 so = free_sets[--num_free_sets];
988 assert (so != NULL && PyAnySet_CheckExact(so));
Martin v. Löwis68192102007-07-21 06:55:02 +0000989 Py_Type(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000990 _Py_NewReference((PyObject *)so);
991 EMPTY_TO_MINSIZE(so);
992 PyObject_GC_Track(so);
993 } else {
994 so = (PySetObject *)type->tp_alloc(type, 0);
995 if (so == NULL)
996 return NULL;
997 /* tp_alloc has already zeroed the structure */
998 assert(so->table == NULL && so->fill == 0 && so->used == 0);
999 INIT_NONZERO_SET_SLOTS(so);
1000 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001001
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001002 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001003 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001004
Raymond Hettingera38123e2003-11-24 22:18:49 +00001005 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001006 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001007 Py_DECREF(so);
1008 return NULL;
1009 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001010 }
1011
Raymond Hettingera690a992003-11-16 16:17:49 +00001012 return (PyObject *)so;
1013}
1014
Raymond Hettingerd7946662005-08-01 21:39:29 +00001015/* The empty frozenset is a singleton */
1016static PyObject *emptyfrozenset = NULL;
1017
Raymond Hettingera690a992003-11-16 16:17:49 +00001018static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001019frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001020{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001021 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001022
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001023 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001024 return NULL;
1025
Raymond Hettingera690a992003-11-16 16:17:49 +00001026 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1027 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001028
1029 if (type != &PyFrozenSet_Type)
1030 return make_new_set(type, iterable);
1031
1032 if (iterable != NULL) {
1033 /* frozenset(f) is idempotent */
1034 if (PyFrozenSet_CheckExact(iterable)) {
1035 Py_INCREF(iterable);
1036 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001037 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001038 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001039 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001040 return result;
1041 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001042 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001043 /* The empty frozenset is a singleton */
1044 if (emptyfrozenset == NULL)
1045 emptyfrozenset = make_new_set(type, NULL);
1046 Py_XINCREF(emptyfrozenset);
1047 return emptyfrozenset;
1048}
1049
1050void
1051PySet_Fini(void)
1052{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001053 PySetObject *so;
1054
1055 while (num_free_sets) {
1056 num_free_sets--;
1057 so = free_sets[num_free_sets];
1058 PyObject_GC_Del(so);
1059 }
Martin v. Löwised8f7832006-04-15 12:47:23 +00001060 Py_CLEAR(dummy);
1061 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001062}
1063
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001064static PyObject *
1065set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1066{
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001067 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001068 return NULL;
1069
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001070 return make_new_set(type, NULL);
1071}
1072
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001073/* set_swap_bodies() switches the contents of any two sets by moving their
1074 internal data pointers and, if needed, copying the internal smalltables.
1075 Semantically equivalent to:
1076
1077 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1078
1079 The function always succeeds and it leaves both objects in a stable state.
1080 Useful for creating temporary frozensets from sets for membership testing
1081 in __contains__(), discard(), and remove(). Also useful for operations
1082 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001083 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001084*/
1085
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001086static void
1087set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001088{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001089 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001090 setentry *u;
1091 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1092 setentry tab[PySet_MINSIZE];
1093 long h;
1094
1095 t = a->fill; a->fill = b->fill; b->fill = t;
1096 t = a->used; a->used = b->used; b->used = t;
1097 t = a->mask; a->mask = b->mask; b->mask = t;
1098
1099 u = a->table;
1100 if (a->table == a->smalltable)
1101 u = b->smalltable;
1102 a->table = b->table;
1103 if (b->table == b->smalltable)
1104 a->table = a->smalltable;
1105 b->table = u;
1106
1107 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1108
1109 if (a->table == a->smalltable || b->table == b->smalltable) {
1110 memcpy(tab, a->smalltable, sizeof(tab));
1111 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1112 memcpy(b->smalltable, tab, sizeof(tab));
1113 }
1114
Martin v. Löwis68192102007-07-21 06:55:02 +00001115 if (PyType_IsSubtype(Py_Type(a), &PyFrozenSet_Type) &&
1116 PyType_IsSubtype(Py_Type(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001117 h = a->hash; a->hash = b->hash; b->hash = h;
1118 } else {
1119 a->hash = -1;
1120 b->hash = -1;
1121 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001122}
1123
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001124static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001125set_copy(PySetObject *so)
1126{
Martin v. Löwis68192102007-07-21 06:55:02 +00001127 return make_new_set(Py_Type(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001128}
1129
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001130static PyObject *
1131frozenset_copy(PySetObject *so)
1132{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001133 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001134 Py_INCREF(so);
1135 return (PyObject *)so;
1136 }
1137 return set_copy(so);
1138}
1139
Raymond Hettingera690a992003-11-16 16:17:49 +00001140PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1141
1142static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001143set_clear(PySetObject *so)
1144{
1145 set_clear_internal(so);
1146 Py_RETURN_NONE;
1147}
1148
1149PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1150
1151static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001152set_union(PySetObject *so, PyObject *other)
1153{
1154 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001155
1156 result = (PySetObject *)set_copy(so);
1157 if (result == NULL)
1158 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001159 if ((PyObject *)so == other)
1160 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001161 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001162 Py_DECREF(result);
1163 return NULL;
1164 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001165 return (PyObject *)result;
1166}
1167
1168PyDoc_STRVAR(union_doc,
1169 "Return the union of two sets as a new set.\n\
1170\n\
1171(i.e. all elements that are in either set.)");
1172
1173static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001174set_or(PySetObject *so, PyObject *other)
1175{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001176 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001177 Py_INCREF(Py_NotImplemented);
1178 return Py_NotImplemented;
1179 }
1180 return set_union(so, other);
1181}
1182
1183static PyObject *
1184set_ior(PySetObject *so, PyObject *other)
1185{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001186 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001187 Py_INCREF(Py_NotImplemented);
1188 return Py_NotImplemented;
1189 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001190 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001191 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001192 Py_INCREF(so);
1193 return (PyObject *)so;
1194}
1195
1196static PyObject *
1197set_intersection(PySetObject *so, PyObject *other)
1198{
1199 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001200 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001201
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001202 if ((PyObject *)so == other)
1203 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001204
Martin v. Löwis68192102007-07-21 06:55:02 +00001205 result = (PySetObject *)make_new_set(Py_Type(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001206 if (result == NULL)
1207 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001208
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001209 if (PyAnySet_CheckExact(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001210 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001211 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001212
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001213 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001214 tmp = (PyObject *)so;
1215 so = (PySetObject *)other;
1216 other = tmp;
1217 }
1218
Raymond Hettingerc991db22005-08-11 07:58:45 +00001219 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001220 int rv = set_contains_entry(so, entry);
1221 if (rv == -1) {
1222 Py_DECREF(result);
1223 return NULL;
1224 }
1225 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001226 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001227 Py_DECREF(result);
1228 return NULL;
1229 }
1230 }
1231 }
1232 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001233 }
1234
Raymond Hettingera690a992003-11-16 16:17:49 +00001235 it = PyObject_GetIter(other);
1236 if (it == NULL) {
1237 Py_DECREF(result);
1238 return NULL;
1239 }
1240
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001241 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001242 int rv;
1243 setentry entry;
1244 long hash = PyObject_Hash(key);
1245
1246 if (hash == -1) {
1247 Py_DECREF(it);
1248 Py_DECREF(result);
1249 Py_DECREF(key);
1250 return NULL;
1251 }
1252 entry.hash = hash;
1253 entry.key = key;
1254 rv = set_contains_entry(so, &entry);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001255 if (rv == -1) {
1256 Py_DECREF(it);
1257 Py_DECREF(result);
1258 Py_DECREF(key);
1259 return NULL;
1260 }
1261 if (rv) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001262 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001263 Py_DECREF(it);
1264 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001265 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001266 return NULL;
1267 }
1268 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001269 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001270 }
1271 Py_DECREF(it);
1272 if (PyErr_Occurred()) {
1273 Py_DECREF(result);
1274 return NULL;
1275 }
1276 return (PyObject *)result;
1277}
1278
1279PyDoc_STRVAR(intersection_doc,
1280"Return the intersection of two sets as a new set.\n\
1281\n\
1282(i.e. all elements that are in both sets.)");
1283
1284static PyObject *
1285set_intersection_update(PySetObject *so, PyObject *other)
1286{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001287 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001288
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001289 tmp = set_intersection(so, other);
1290 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001291 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001292 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001293 Py_DECREF(tmp);
1294 Py_RETURN_NONE;
1295}
1296
1297PyDoc_STRVAR(intersection_update_doc,
1298"Update a set with the intersection of itself and another.");
1299
1300static PyObject *
1301set_and(PySetObject *so, PyObject *other)
1302{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001303 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001304 Py_INCREF(Py_NotImplemented);
1305 return Py_NotImplemented;
1306 }
1307 return set_intersection(so, other);
1308}
1309
1310static PyObject *
1311set_iand(PySetObject *so, PyObject *other)
1312{
1313 PyObject *result;
1314
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001315 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001316 Py_INCREF(Py_NotImplemented);
1317 return Py_NotImplemented;
1318 }
1319 result = set_intersection_update(so, other);
1320 if (result == NULL)
1321 return NULL;
1322 Py_DECREF(result);
1323 Py_INCREF(so);
1324 return (PyObject *)so;
1325}
1326
Neal Norwitz6576bd82005-11-13 18:41:28 +00001327static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001328set_difference_update_internal(PySetObject *so, PyObject *other)
1329{
1330 if ((PyObject *)so == other)
1331 return set_clear_internal(so);
1332
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001333 if (PyAnySet_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001334 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001335 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001336
1337 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001338 if (set_discard_entry(so, entry) == -1)
1339 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001340 } else {
1341 PyObject *key, *it;
1342 it = PyObject_GetIter(other);
1343 if (it == NULL)
1344 return -1;
1345
1346 while ((key = PyIter_Next(it)) != NULL) {
1347 if (set_discard_key(so, key) == -1) {
1348 Py_DECREF(it);
1349 Py_DECREF(key);
1350 return -1;
1351 }
1352 Py_DECREF(key);
1353 }
1354 Py_DECREF(it);
1355 if (PyErr_Occurred())
1356 return -1;
1357 }
1358 /* If more than 1/5 are dummies, then resize them away. */
1359 if ((so->fill - so->used) * 5 < so->mask)
1360 return 0;
1361 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1362}
1363
Raymond Hettingera690a992003-11-16 16:17:49 +00001364static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001365set_difference_update(PySetObject *so, PyObject *other)
1366{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001367 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001368 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001369 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001370}
1371
1372PyDoc_STRVAR(difference_update_doc,
1373"Remove all elements of another set from this set.");
1374
1375static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001376set_difference(PySetObject *so, PyObject *other)
1377{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001378 PyObject *result;
1379 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001380 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001381
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001382 if (!PyAnySet_CheckExact(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001383 result = set_copy(so);
1384 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001385 return NULL;
1386 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001387 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001388 Py_DECREF(result);
1389 return NULL;
1390 }
1391
Martin v. Löwis68192102007-07-21 06:55:02 +00001392 result = make_new_set(Py_Type(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001393 if (result == NULL)
1394 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001395
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001396 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001397 while (set_next(so, &pos, &entry)) {
1398 setentry entrycopy;
1399 entrycopy.hash = entry->hash;
1400 entrycopy.key = entry->key;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001401 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001402 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1403 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001404 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001405 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001406 }
1407 }
1408 return result;
1409 }
1410
Raymond Hettingerc991db22005-08-11 07:58:45 +00001411 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001412 int rv = set_contains_entry((PySetObject *)other, entry);
1413 if (rv == -1) {
1414 Py_DECREF(result);
1415 return NULL;
1416 }
1417 if (!rv) {
1418 if (set_add_entry((PySetObject *)result, entry) == -1) {
1419 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001420 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001421 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001422 }
1423 }
1424 return result;
1425}
1426
1427PyDoc_STRVAR(difference_doc,
1428"Return the difference of two sets as a new set.\n\
1429\n\
1430(i.e. all elements that are in this set but not the other.)");
1431static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001432set_sub(PySetObject *so, PyObject *other)
1433{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001434 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001435 Py_INCREF(Py_NotImplemented);
1436 return Py_NotImplemented;
1437 }
1438 return set_difference(so, other);
1439}
1440
1441static PyObject *
1442set_isub(PySetObject *so, PyObject *other)
1443{
1444 PyObject *result;
1445
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001446 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001447 Py_INCREF(Py_NotImplemented);
1448 return Py_NotImplemented;
1449 }
1450 result = set_difference_update(so, other);
1451 if (result == NULL)
1452 return NULL;
1453 Py_DECREF(result);
1454 Py_INCREF(so);
1455 return (PyObject *)so;
1456}
1457
1458static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001459set_symmetric_difference_update(PySetObject *so, PyObject *other)
1460{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001461 PySetObject *otherset;
1462 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001463 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001464 setentry *entry;
1465
1466 if ((PyObject *)so == other)
1467 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001468
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001469 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001470 PyObject *value;
1471 int rv;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001472 long hash;
1473 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001474 setentry an_entry;
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001475
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001476 an_entry.hash = hash;
1477 an_entry.key = key;
1478 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001479 if (rv == -1)
1480 return NULL;
1481 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001482 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001483 return NULL;
1484 }
1485 }
1486 Py_RETURN_NONE;
1487 }
1488
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001489 if (PyAnySet_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001490 Py_INCREF(other);
1491 otherset = (PySetObject *)other;
1492 } else {
Martin v. Löwis68192102007-07-21 06:55:02 +00001493 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001494 if (otherset == NULL)
1495 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001496 }
1497
Raymond Hettingerc991db22005-08-11 07:58:45 +00001498 while (set_next(otherset, &pos, &entry)) {
1499 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001500 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001501 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001502 return NULL;
1503 }
1504 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001505 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001506 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001507 return NULL;
1508 }
1509 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001510 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001511 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001512 Py_RETURN_NONE;
1513}
1514
1515PyDoc_STRVAR(symmetric_difference_update_doc,
1516"Update a set with the symmetric difference of itself and another.");
1517
1518static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001519set_symmetric_difference(PySetObject *so, PyObject *other)
1520{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001521 PyObject *rv;
1522 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001523
Martin v. Löwis68192102007-07-21 06:55:02 +00001524 otherset = (PySetObject *)make_new_set(Py_Type(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001525 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001526 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001527 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1528 if (rv == NULL)
1529 return NULL;
1530 Py_DECREF(rv);
1531 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001532}
1533
1534PyDoc_STRVAR(symmetric_difference_doc,
1535"Return the symmetric difference of two sets as a new set.\n\
1536\n\
1537(i.e. all elements that are in exactly one of the sets.)");
1538
1539static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001540set_xor(PySetObject *so, PyObject *other)
1541{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001542 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001543 Py_INCREF(Py_NotImplemented);
1544 return Py_NotImplemented;
1545 }
1546 return set_symmetric_difference(so, other);
1547}
1548
1549static PyObject *
1550set_ixor(PySetObject *so, PyObject *other)
1551{
1552 PyObject *result;
1553
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001554 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001555 Py_INCREF(Py_NotImplemented);
1556 return Py_NotImplemented;
1557 }
1558 result = set_symmetric_difference_update(so, other);
1559 if (result == NULL)
1560 return NULL;
1561 Py_DECREF(result);
1562 Py_INCREF(so);
1563 return (PyObject *)so;
1564}
1565
1566static PyObject *
1567set_issubset(PySetObject *so, PyObject *other)
1568{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001569 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001570 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001571
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001572 if (!PyAnySet_CheckExact(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001573 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001574 tmp = make_new_set(&PySet_Type, other);
1575 if (tmp == NULL)
1576 return NULL;
1577 result = set_issubset(so, tmp);
1578 Py_DECREF(tmp);
1579 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001580 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001581 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001582 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001583
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001584 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001585 int rv = set_contains_entry((PySetObject *)other, entry);
1586 if (rv == -1)
1587 return NULL;
1588 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001589 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001590 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001591 Py_RETURN_TRUE;
1592}
1593
1594PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1595
1596static PyObject *
1597set_issuperset(PySetObject *so, PyObject *other)
1598{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001599 PyObject *tmp, *result;
1600
Raymond Hettinger0e7a6322007-02-08 00:50:39 +00001601 if (!PyAnySet_CheckExact(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001602 tmp = make_new_set(&PySet_Type, other);
1603 if (tmp == NULL)
1604 return NULL;
1605 result = set_issuperset(so, tmp);
1606 Py_DECREF(tmp);
1607 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001608 }
1609 return set_issubset((PySetObject *)other, (PyObject *)so);
1610}
1611
1612PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1613
Raymond Hettingera690a992003-11-16 16:17:49 +00001614static PyObject *
1615set_richcompare(PySetObject *v, PyObject *w, int op)
1616{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001617 PyObject *r1, *r2;
1618
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001619 if(!PyAnySet_Check(w)) {
1620 if (op == Py_EQ)
1621 Py_RETURN_FALSE;
1622 if (op == Py_NE)
1623 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001624 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1625 return NULL;
1626 }
1627 switch (op) {
1628 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001629 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001630 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001631 if (v->hash != -1 &&
1632 ((PySetObject *)w)->hash != -1 &&
1633 v->hash != ((PySetObject *)w)->hash)
1634 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001635 return set_issubset(v, w);
1636 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001637 r1 = set_richcompare(v, w, Py_EQ);
1638 if (r1 == NULL)
1639 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001640 r2 = PyBool_FromLong(PyObject_Not(r1));
1641 Py_DECREF(r1);
1642 return r2;
1643 case Py_LE:
1644 return set_issubset(v, w);
1645 case Py_GE:
1646 return set_issuperset(v, w);
1647 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001648 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001649 Py_RETURN_FALSE;
1650 return set_issubset(v, w);
1651 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001652 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001653 Py_RETURN_FALSE;
1654 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001655 }
1656 Py_INCREF(Py_NotImplemented);
1657 return Py_NotImplemented;
1658}
1659
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001660static int
Georg Brandl347b3002006-03-30 11:57:00 +00001661set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001662{
1663 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1664 return -1;
1665}
1666
Raymond Hettingera690a992003-11-16 16:17:49 +00001667static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001668set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001669{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001670 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001671 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001672 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001673}
1674
1675PyDoc_STRVAR(add_doc,
1676"Add an element to a set.\n\
1677\n\
1678This has no effect if the element is already present.");
1679
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001680static int
1681set_contains(PySetObject *so, PyObject *key)
1682{
1683 PyObject *tmpkey;
1684 int rv;
1685
1686 rv = set_contains_key(so, key);
1687 if (rv == -1) {
1688 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1689 return -1;
1690 PyErr_Clear();
1691 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1692 if (tmpkey == NULL)
1693 return -1;
1694 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1695 rv = set_contains(so, tmpkey);
1696 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1697 Py_DECREF(tmpkey);
1698 }
1699 return rv;
1700}
1701
1702static PyObject *
1703set_direct_contains(PySetObject *so, PyObject *key)
1704{
1705 long result;
1706
1707 result = set_contains(so, key);
1708 if (result == -1)
1709 return NULL;
1710 return PyBool_FromLong(result);
1711}
1712
1713PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1714
Raymond Hettingera690a992003-11-16 16:17:49 +00001715static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001716set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001717{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001718 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001719 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001720
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001721 rv = set_discard_key(so, key);
1722 if (rv == -1) {
1723 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1724 return NULL;
1725 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001726 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1727 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001728 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001729 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001730 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001731 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001732 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001733 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001734 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +00001735 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001736 return NULL;
1737 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001738 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001739}
1740
1741PyDoc_STRVAR(remove_doc,
1742"Remove an element from a set; it must be a member.\n\
1743\n\
1744If the element is not a member, raise a KeyError.");
1745
1746static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001747set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001748{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001749 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001750 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001751
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001752 rv = set_discard_key(so, key);
1753 if (rv == -1) {
1754 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1755 return NULL;
1756 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001757 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1758 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001759 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001760 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001761 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001762 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001763 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001764 return result;
1765 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001766 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001767}
1768
1769PyDoc_STRVAR(discard_doc,
1770"Remove an element from a set if it is a member.\n\
1771\n\
1772If the element is not a member, do nothing.");
1773
1774static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001775set_reduce(PySetObject *so)
1776{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001777 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001778
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001779 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001780 if (keys == NULL)
1781 goto done;
1782 args = PyTuple_Pack(1, keys);
1783 if (args == NULL)
1784 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001785 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1786 if (dict == NULL) {
1787 PyErr_Clear();
1788 dict = Py_None;
1789 Py_INCREF(dict);
1790 }
Martin v. Löwis68192102007-07-21 06:55:02 +00001791 result = PyTuple_Pack(3, Py_Type(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001792done:
1793 Py_XDECREF(args);
1794 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001795 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001796 return result;
1797}
1798
1799PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1800
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001801static int
1802set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1803{
1804 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001805
1806 if (!PyAnySet_Check(self))
1807 return -1;
Martin v. Löwis68192102007-07-21 06:55:02 +00001808 if (!PyArg_UnpackTuple(args, Py_Type(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001809 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001810 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001811 self->hash = -1;
1812 if (iterable == NULL)
1813 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001814 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001815}
1816
Raymond Hettingera690a992003-11-16 16:17:49 +00001817static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001818 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001819 0, /* sq_concat */
1820 0, /* sq_repeat */
1821 0, /* sq_item */
1822 0, /* sq_slice */
1823 0, /* sq_ass_item */
1824 0, /* sq_ass_slice */
1825 (objobjproc)set_contains, /* sq_contains */
1826};
1827
1828/* set object ********************************************************/
1829
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001830#ifdef Py_DEBUG
1831static PyObject *test_c_api(PySetObject *so);
1832
1833PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1834All is well if assertions don't fail.");
1835#endif
1836
Raymond Hettingera690a992003-11-16 16:17:49 +00001837static PyMethodDef set_methods[] = {
1838 {"add", (PyCFunction)set_add, METH_O,
1839 add_doc},
1840 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1841 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001842 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001843 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001844 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1845 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001846 {"discard", (PyCFunction)set_discard, METH_O,
1847 discard_doc},
1848 {"difference", (PyCFunction)set_difference, METH_O,
1849 difference_doc},
1850 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1851 difference_update_doc},
1852 {"intersection",(PyCFunction)set_intersection, METH_O,
1853 intersection_doc},
1854 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1855 intersection_update_doc},
1856 {"issubset", (PyCFunction)set_issubset, METH_O,
1857 issubset_doc},
1858 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1859 issuperset_doc},
1860 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1861 pop_doc},
1862 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1863 reduce_doc},
1864 {"remove", (PyCFunction)set_remove, METH_O,
1865 remove_doc},
1866 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1867 symmetric_difference_doc},
1868 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1869 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001870#ifdef Py_DEBUG
1871 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1872 test_c_api_doc},
1873#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001874 {"union", (PyCFunction)set_union, METH_O,
1875 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001876 {"update", (PyCFunction)set_update, METH_O,
1877 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001878 {NULL, NULL} /* sentinel */
1879};
1880
1881static PyNumberMethods set_as_number = {
1882 0, /*nb_add*/
1883 (binaryfunc)set_sub, /*nb_subtract*/
1884 0, /*nb_multiply*/
1885 0, /*nb_divide*/
1886 0, /*nb_remainder*/
1887 0, /*nb_divmod*/
1888 0, /*nb_power*/
1889 0, /*nb_negative*/
1890 0, /*nb_positive*/
1891 0, /*nb_absolute*/
1892 0, /*nb_nonzero*/
1893 0, /*nb_invert*/
1894 0, /*nb_lshift*/
1895 0, /*nb_rshift*/
1896 (binaryfunc)set_and, /*nb_and*/
1897 (binaryfunc)set_xor, /*nb_xor*/
1898 (binaryfunc)set_or, /*nb_or*/
1899 0, /*nb_coerce*/
1900 0, /*nb_int*/
1901 0, /*nb_long*/
1902 0, /*nb_float*/
1903 0, /*nb_oct*/
1904 0, /*nb_hex*/
1905 0, /*nb_inplace_add*/
1906 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1907 0, /*nb_inplace_multiply*/
1908 0, /*nb_inplace_divide*/
1909 0, /*nb_inplace_remainder*/
1910 0, /*nb_inplace_power*/
1911 0, /*nb_inplace_lshift*/
1912 0, /*nb_inplace_rshift*/
1913 (binaryfunc)set_iand, /*nb_inplace_and*/
1914 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1915 (binaryfunc)set_ior, /*nb_inplace_or*/
1916};
1917
1918PyDoc_STRVAR(set_doc,
1919"set(iterable) --> set object\n\
1920\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001921Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001922
1923PyTypeObject PySet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001924 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001925 "set", /* tp_name */
1926 sizeof(PySetObject), /* tp_basicsize */
1927 0, /* tp_itemsize */
1928 /* methods */
1929 (destructor)set_dealloc, /* tp_dealloc */
1930 (printfunc)set_tp_print, /* tp_print */
1931 0, /* tp_getattr */
1932 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001933 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001934 (reprfunc)set_repr, /* tp_repr */
1935 &set_as_number, /* tp_as_number */
1936 &set_as_sequence, /* tp_as_sequence */
1937 0, /* tp_as_mapping */
1938 set_nohash, /* tp_hash */
1939 0, /* tp_call */
1940 0, /* tp_str */
1941 PyObject_GenericGetAttr, /* tp_getattro */
1942 0, /* tp_setattro */
1943 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001944 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001945 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001946 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001947 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001948 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001949 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001950 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001951 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001952 0, /* tp_iternext */
1953 set_methods, /* tp_methods */
1954 0, /* tp_members */
1955 0, /* tp_getset */
1956 0, /* tp_base */
1957 0, /* tp_dict */
1958 0, /* tp_descr_get */
1959 0, /* tp_descr_set */
1960 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001961 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001962 PyType_GenericAlloc, /* tp_alloc */
1963 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001964 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001965};
1966
1967/* frozenset object ********************************************************/
1968
1969
1970static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001971 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001972 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001973 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001974 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001975 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001976 difference_doc},
1977 {"intersection",(PyCFunction)set_intersection, METH_O,
1978 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001979 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001980 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001981 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001982 issuperset_doc},
1983 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1984 reduce_doc},
1985 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1986 symmetric_difference_doc},
1987 {"union", (PyCFunction)set_union, METH_O,
1988 union_doc},
1989 {NULL, NULL} /* sentinel */
1990};
1991
1992static PyNumberMethods frozenset_as_number = {
1993 0, /*nb_add*/
1994 (binaryfunc)set_sub, /*nb_subtract*/
1995 0, /*nb_multiply*/
1996 0, /*nb_divide*/
1997 0, /*nb_remainder*/
1998 0, /*nb_divmod*/
1999 0, /*nb_power*/
2000 0, /*nb_negative*/
2001 0, /*nb_positive*/
2002 0, /*nb_absolute*/
2003 0, /*nb_nonzero*/
2004 0, /*nb_invert*/
2005 0, /*nb_lshift*/
2006 0, /*nb_rshift*/
2007 (binaryfunc)set_and, /*nb_and*/
2008 (binaryfunc)set_xor, /*nb_xor*/
2009 (binaryfunc)set_or, /*nb_or*/
2010};
2011
2012PyDoc_STRVAR(frozenset_doc,
2013"frozenset(iterable) --> frozenset object\n\
2014\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002015Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002016
2017PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002018 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002019 "frozenset", /* tp_name */
2020 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002021 0, /* tp_itemsize */
2022 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002023 (destructor)set_dealloc, /* tp_dealloc */
2024 (printfunc)set_tp_print, /* tp_print */
2025 0, /* tp_getattr */
2026 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002027 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002028 (reprfunc)set_repr, /* tp_repr */
2029 &frozenset_as_number, /* tp_as_number */
2030 &set_as_sequence, /* tp_as_sequence */
2031 0, /* tp_as_mapping */
2032 frozenset_hash, /* tp_hash */
2033 0, /* tp_call */
2034 0, /* tp_str */
2035 PyObject_GenericGetAttr, /* tp_getattro */
2036 0, /* tp_setattro */
2037 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002038 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002039 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002040 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002041 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002042 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002043 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002044 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002045 (getiterfunc)set_iter, /* tp_iter */
2046 0, /* tp_iternext */
2047 frozenset_methods, /* tp_methods */
2048 0, /* tp_members */
2049 0, /* tp_getset */
2050 0, /* tp_base */
2051 0, /* tp_dict */
2052 0, /* tp_descr_get */
2053 0, /* tp_descr_set */
2054 0, /* tp_dictoffset */
2055 0, /* tp_init */
2056 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002057 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002058 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002059};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002060
2061
2062/***** C API functions *************************************************/
2063
2064PyObject *
2065PySet_New(PyObject *iterable)
2066{
2067 return make_new_set(&PySet_Type, iterable);
2068}
2069
2070PyObject *
2071PyFrozenSet_New(PyObject *iterable)
2072{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002073 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002074
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002075 if (iterable == NULL)
2076 args = PyTuple_New(0);
2077 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002078 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002079 if (args == NULL)
2080 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002081 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002082 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002083 return result;
2084}
2085
Neal Norwitz8c49c822006-03-04 18:41:19 +00002086Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002087PySet_Size(PyObject *anyset)
2088{
2089 if (!PyAnySet_Check(anyset)) {
2090 PyErr_BadInternalCall();
2091 return -1;
2092 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002093 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002094}
2095
2096int
Barry Warsaw176014f2006-03-30 22:45:35 +00002097PySet_Clear(PyObject *set)
2098{
Martin v. Löwis68192102007-07-21 06:55:02 +00002099 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002100 PyErr_BadInternalCall();
2101 return -1;
2102 }
2103 return set_clear_internal((PySetObject *)set);
2104}
2105
2106int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002107PySet_Contains(PyObject *anyset, PyObject *key)
2108{
2109 if (!PyAnySet_Check(anyset)) {
2110 PyErr_BadInternalCall();
2111 return -1;
2112 }
2113 return set_contains_key((PySetObject *)anyset, key);
2114}
2115
2116int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002117PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002118{
Martin v. Löwis68192102007-07-21 06:55:02 +00002119 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002120 PyErr_BadInternalCall();
2121 return -1;
2122 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002123 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002124}
2125
2126int
2127PySet_Add(PyObject *set, PyObject *key)
2128{
Martin v. Löwis68192102007-07-21 06:55:02 +00002129 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002130 PyErr_BadInternalCall();
2131 return -1;
2132 }
2133 return set_add_key((PySetObject *)set, key);
2134}
2135
Barry Warsaw176014f2006-03-30 22:45:35 +00002136int
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002137_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Barry Warsaw176014f2006-03-30 22:45:35 +00002138{
2139 setentry *entry_ptr;
2140
2141 if (!PyAnySet_Check(set)) {
2142 PyErr_BadInternalCall();
2143 return -1;
2144 }
2145 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2146 return 0;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002147 *key = entry_ptr->key;
2148 return 1;
2149}
2150
2151int
2152_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2153{
2154 setentry *entry;
2155
2156 if (!PyAnySet_Check(set)) {
2157 PyErr_BadInternalCall();
2158 return -1;
2159 }
2160 if (set_next((PySetObject *)set, pos, &entry) == 0)
2161 return 0;
2162 *key = entry->key;
2163 *hash = entry->hash;
Barry Warsaw176014f2006-03-30 22:45:35 +00002164 return 1;
2165}
2166
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002167PyObject *
2168PySet_Pop(PyObject *set)
2169{
Martin v. Löwis68192102007-07-21 06:55:02 +00002170 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002171 PyErr_BadInternalCall();
2172 return NULL;
2173 }
2174 return set_pop((PySetObject *)set);
2175}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002176
Barry Warsaw176014f2006-03-30 22:45:35 +00002177int
2178_PySet_Update(PyObject *set, PyObject *iterable)
2179{
Martin v. Löwis68192102007-07-21 06:55:02 +00002180 if (!PyType_IsSubtype(Py_Type(set), &PySet_Type)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002181 PyErr_BadInternalCall();
2182 return -1;
2183 }
2184 return set_update_internal((PySetObject *)set, iterable);
2185}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002186
2187#ifdef Py_DEBUG
2188
2189/* Test code to be called with any three element set.
2190 Returns True and original set is restored. */
2191
2192#define assertRaises(call_return_value, exception) \
2193 do { \
2194 assert(call_return_value); \
2195 assert(PyErr_ExceptionMatches(exception)); \
2196 PyErr_Clear(); \
2197 } while(0)
2198
2199static PyObject *
2200test_c_api(PySetObject *so)
2201{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002202 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002203 char *s;
2204 Py_ssize_t i;
Guido van Rossum360496d2007-05-10 17:20:15 +00002205 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Barry Warsaw176014f2006-03-30 22:45:35 +00002206 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002207
2208 /* Verify preconditions and exercise type/size checks */
2209 assert(PyAnySet_Check(ob));
2210 assert(PyAnySet_CheckExact(ob));
2211 assert(!PyFrozenSet_CheckExact(ob));
2212 assert(PySet_Size(ob) == 3);
2213 assert(PySet_GET_SIZE(ob) == 3);
2214
2215 /* Raise TypeError for non-iterable constructor arguments */
2216 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2217 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2218
2219 /* Raise TypeError for unhashable key */
2220 dup = PySet_New(ob);
2221 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2222 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2223 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2224
2225 /* Exercise successful pop, contains, add, and discard */
2226 elem = PySet_Pop(ob);
2227 assert(PySet_Contains(ob, elem) == 0);
2228 assert(PySet_GET_SIZE(ob) == 2);
2229 assert(PySet_Add(ob, elem) == 0);
2230 assert(PySet_Contains(ob, elem) == 1);
2231 assert(PySet_GET_SIZE(ob) == 3);
2232 assert(PySet_Discard(ob, elem) == 1);
2233 assert(PySet_GET_SIZE(ob) == 2);
2234 assert(PySet_Discard(ob, elem) == 0);
2235 assert(PySet_GET_SIZE(ob) == 2);
2236
Barry Warsaw176014f2006-03-30 22:45:35 +00002237 /* Exercise clear */
2238 dup2 = PySet_New(dup);
2239 assert(PySet_Clear(dup2) == 0);
2240 assert(PySet_Size(dup2) == 0);
2241 Py_DECREF(dup2);
2242
2243 /* Raise SystemError on clear or update of frozen set */
2244 f = PyFrozenSet_New(dup);
2245 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2246 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2247 Py_DECREF(f);
2248
2249 /* Exercise direct iteration */
2250 i = 0, count = 0;
Guido van Rossum360496d2007-05-10 17:20:15 +00002251 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2252 s = PyString_AsString(x);
Barry Warsaw176014f2006-03-30 22:45:35 +00002253 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2254 count++;
2255 }
2256 assert(count == 3);
2257
2258 /* Exercise updates */
2259 dup2 = PySet_New(NULL);
2260 assert(_PySet_Update(dup2, dup) == 0);
2261 assert(PySet_Size(dup2) == 3);
2262 assert(_PySet_Update(dup2, dup) == 0);
2263 assert(PySet_Size(dup2) == 3);
2264 Py_DECREF(dup2);
2265
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002266 /* Raise SystemError when self argument is not a set or frozenset. */
2267 t = PyTuple_New(0);
2268 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2269 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2270 Py_DECREF(t);
2271
2272 /* Raise SystemError when self argument is not a set. */
2273 f = PyFrozenSet_New(dup);
2274 assert(PySet_Size(f) == 3);
2275 assert(PyFrozenSet_CheckExact(f));
2276 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2277 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2278 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2279 Py_DECREF(f);
2280
2281 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002282 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2283 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002284 assert(PySet_GET_SIZE(ob) == 0);
2285 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2286
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002287 /* Restore the set from the copy using the PyNumber API */
2288 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2289 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002290
2291 /* Verify constructors accept NULL arguments */
2292 f = PySet_New(NULL);
2293 assert(f != NULL);
2294 assert(PySet_GET_SIZE(f) == 0);
2295 Py_DECREF(f);
2296 f = PyFrozenSet_New(NULL);
2297 assert(f != NULL);
2298 assert(PyFrozenSet_CheckExact(f));
2299 assert(PySet_GET_SIZE(f) == 0);
2300 Py_DECREF(f);
2301
2302 Py_DECREF(elem);
2303 Py_DECREF(dup);
2304 Py_RETURN_TRUE;
2305}
2306
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002307#undef assertRaises
2308
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002309#endif