blob: fcc152a7a4769d61efe047756eb2f7eac516f768 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00006 Copyright (c) 2003-2007 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000012#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
26}
27
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028/* This must be >= 1. */
29#define PERTURB_SHIFT 5
30
31/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000032static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#ifdef Py_REF_DEBUG
35PyObject *
36_PySet_Dummy(void)
37{
38 return dummy;
39}
40#endif
41
Raymond Hettingerbc841a12005-08-07 13:02:53 +000042#define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
45 (so)->hash = -1; \
46 } while(0)
47
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000048#define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000051 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052 } while(0)
53
Raymond Hettingerbc841a12005-08-07 13:02:53 +000054/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000055#ifndef PySet_MAXFREELIST
56#define PySet_MAXFREELIST 80
57#endif
58static PySetObject *free_list[PySet_MAXFREELIST];
59static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060
Christian Heimes0ded5b52007-12-10 15:50:56 +000061
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062/*
63The basic lookup function used by all operations.
64This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65Open addressing is preferred over chaining since the link overhead for
66chaining would be substantial (100% with typical malloc overhead).
67
68The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000069probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070
71All arithmetic on hash should ignore overflow.
72
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000073Unlike the dictionary implementation, the lookkey functions can return
74NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075*/
76
77static setentry *
78set_lookkey(PySetObject *so, PyObject *key, register long hash)
79{
Martin v. Löwis18e16552006-02-15 17:27:45 +000080 register Py_ssize_t i;
81 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000084 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000085 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000087 PyObject *startkey;
88
89 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000090 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000091 if (entry->key == NULL || entry->key == key)
92 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 if (entry->key == dummy)
95 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000096 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000097 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000098 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000099 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
100 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000101 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000102 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000103 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000104 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000105 }
106 else {
107 /* The compare did major nasty stuff to the
108 * set: start over.
109 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000110 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000111 }
112 }
113 freeslot = NULL;
114 }
115
116 /* In the loop, key == dummy is by far (factor of 100s) the
117 least likely outcome, so test for that last. */
118 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
119 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000120 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000121 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000123 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 break;
125 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000126 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000127 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000128 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000129 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
131 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000132 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000133 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000134 if (cmp > 0)
135 break;
136 }
137 else {
138 /* The compare did major nasty stuff to the
139 * set: start over.
140 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000141 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000142 }
143 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000144 else if (entry->key == dummy && freeslot == NULL)
145 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000147 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000148}
149
150/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000151 * Hacked up version of set_lookkey which can assume keys are always unicode;
152 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000153 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000154 */
155static setentry *
Christian Heimes0ded5b52007-12-10 15:50:56 +0000156set_lookkey_unicode(PySetObject *so, PyObject *key, register long hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000157{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000158 register Py_ssize_t i;
159 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000160 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000161 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000162 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000163 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000164
Christian Heimes0ded5b52007-12-10 15:50:56 +0000165 /* Make sure this function doesn't have to handle non-unicode keys,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000166 including subclasses of str; e.g., one reason to subclass
167 strings is to override __eq__, and for speed we don't cater to
168 that here. */
Christian Heimes0ded5b52007-12-10 15:50:56 +0000169 if (!PyUnicode_CheckExact(key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000170 so->lookup = set_lookkey;
171 return set_lookkey(so, key, hash);
172 }
173 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000174 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000175 if (entry->key == NULL || entry->key == key)
176 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000177 if (entry->key == dummy)
178 freeslot = entry;
179 else {
Christian Heimes0ded5b52007-12-10 15:50:56 +0000180 if (entry->hash == hash && unicode_eq(entry->key, key))
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000181 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000182 freeslot = NULL;
183 }
184
185 /* In the loop, key == dummy is by far (factor of 100s) the
186 least likely outcome, so test for that last. */
187 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
188 i = (i << 2) + i + perturb + 1;
189 entry = &table[i & mask];
190 if (entry->key == NULL)
191 return freeslot == NULL ? entry : freeslot;
192 if (entry->key == key
193 || (entry->hash == hash
194 && entry->key != dummy
Christian Heimes0ded5b52007-12-10 15:50:56 +0000195 && unicode_eq(entry->key, key)))
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000196 return entry;
197 if (entry->key == dummy && freeslot == NULL)
198 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000199 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000200 assert(0); /* NOT REACHED */
201 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000202}
203
204/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000205Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000206Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000207Eats a reference to key.
208*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000209static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000210set_insert_key(register PySetObject *so, PyObject *key, long hash)
211{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000212 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000213 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
214
215 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000216 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000217 if (entry == NULL)
218 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000219 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220 /* UNUSED */
221 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000222 entry->key = key;
223 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000224 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000225 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000226 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000227 entry->key = key;
228 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000229 so->used++;
230 Py_DECREF(dummy);
231 } else {
232 /* ACTIVE */
233 Py_DECREF(key);
234 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000235 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000236}
237
238/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000239Internal routine used by set_table_resize() to insert an item which is
240known to be absent from the set. This routine also assumes that
241the set contains no deleted entries. Besides the performance benefit,
242using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
243Note that no refcounts are changed by this routine; if needed, the caller
244is responsible for incref'ing `key`.
245*/
246static void
247set_insert_clean(register PySetObject *so, PyObject *key, long hash)
248{
249 register size_t i;
250 register size_t perturb;
251 register size_t mask = (size_t)so->mask;
252 setentry *table = so->table;
253 register setentry *entry;
254
255 i = hash & mask;
256 entry = &table[i];
257 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
258 i = (i << 2) + i + perturb + 1;
259 entry = &table[i & mask];
260 }
261 so->fill++;
262 entry->key = key;
263 entry->hash = hash;
264 so->used++;
265}
266
267/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000268Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000269keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270actually be smaller than the old one.
271*/
272static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000276 setentry *oldtable, *newtable, *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278 int is_oldtable_malloced;
279 setentry small_copy[PySet_MINSIZE];
280
281 assert(minused >= 0);
282
283 /* Find the smallest table size > minused. */
284 for (newsize = PySet_MINSIZE;
285 newsize <= minused && newsize > 0;
286 newsize <<= 1)
287 ;
288 if (newsize <= 0) {
289 PyErr_NoMemory();
290 return -1;
291 }
292
293 /* Get space for a new table. */
294 oldtable = so->table;
295 assert(oldtable != NULL);
296 is_oldtable_malloced = oldtable != so->smalltable;
297
298 if (newsize == PySet_MINSIZE) {
299 /* A large table is shrinking, or we can't get any smaller. */
300 newtable = so->smalltable;
301 if (newtable == oldtable) {
302 if (so->fill == so->used) {
303 /* No dummies, so no point doing anything. */
304 return 0;
305 }
306 /* We're not going to resize it, but rebuild the
307 table anyway to purge old dummy entries.
308 Subtle: This is *necessary* if fill==size,
309 as set_lookkey needs at least one virgin slot to
310 terminate failing searches. If fill < size, it's
311 merely desirable, as dummies slow searches. */
312 assert(so->fill > so->used);
313 memcpy(small_copy, oldtable, sizeof(small_copy));
314 oldtable = small_copy;
315 }
316 }
317 else {
318 newtable = PyMem_NEW(setentry, newsize);
319 if (newtable == NULL) {
320 PyErr_NoMemory();
321 return -1;
322 }
323 }
324
325 /* Make the set empty, using the new table. */
326 assert(newtable != oldtable);
327 so->table = newtable;
328 so->mask = newsize - 1;
329 memset(newtable, 0, sizeof(setentry) * newsize);
330 so->used = 0;
331 i = so->fill;
332 so->fill = 0;
333
334 /* Copy the data over; this is refcount-neutral for active entries;
335 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000336 for (entry = oldtable; i > 0; entry++) {
337 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000338 /* UNUSED */
339 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000340 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341 /* DUMMY */
342 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000343 assert(entry->key == dummy);
344 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345 } else {
346 /* ACTIVE */
347 --i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000348 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000349 }
350 }
351
352 if (is_oldtable_malloced)
353 PyMem_DEL(oldtable);
354 return 0;
355}
356
Raymond Hettingerc991db22005-08-11 07:58:45 +0000357/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
358
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000359static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000360set_add_entry(register PySetObject *so, setentry *entry)
361{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000362 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000363
364 assert(so->fill <= so->mask); /* at least one empty slot */
365 n_used = so->used;
366 Py_INCREF(entry->key);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000367 if (set_insert_key(so, entry->key, entry->hash) == -1) {
368 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000369 return -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000370 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000371 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
372 return 0;
373 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
374}
375
376static int
377set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000378{
379 register long hash;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000380 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000381
Christian Heimes0ded5b52007-12-10 15:50:56 +0000382 if (!PyUnicode_CheckExact(key) ||
383 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000384 hash = PyObject_Hash(key);
385 if (hash == -1)
386 return -1;
387 }
388 assert(so->fill <= so->mask); /* at least one empty slot */
389 n_used = so->used;
390 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000391 if (set_insert_key(so, key, hash) == -1) {
392 Py_DECREF(key);
393 return -1;
394 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000395 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
396 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000397 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000398}
399
400#define DISCARD_NOTFOUND 0
401#define DISCARD_FOUND 1
402
403static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000404set_discard_entry(PySetObject *so, setentry *oldentry)
405{ register setentry *entry;
406 PyObject *old_key;
407
408 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000409 if (entry == NULL)
410 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000411 if (entry->key == NULL || entry->key == dummy)
412 return DISCARD_NOTFOUND;
413 old_key = entry->key;
414 Py_INCREF(dummy);
415 entry->key = dummy;
416 so->used--;
417 Py_DECREF(old_key);
418 return DISCARD_FOUND;
419}
420
421static int
422set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000423{
424 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000425 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000426 PyObject *old_key;
427
428 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000429
430 if (!PyUnicode_CheckExact(key) ||
431 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000432 hash = PyObject_Hash(key);
433 if (hash == -1)
434 return -1;
435 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000437 if (entry == NULL)
438 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000439 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000440 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000441 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000442 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000443 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000444 so->used--;
445 Py_DECREF(old_key);
446 return DISCARD_FOUND;
447}
448
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000449static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450set_clear_internal(PySetObject *so)
451{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000452 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 int table_is_malloced;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000454 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455 setentry small_copy[PySet_MINSIZE];
456#ifdef Py_DEBUG
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000457 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000458 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000459
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000460 n = so->mask + 1;
461 i = 0;
462#endif
463
464 table = so->table;
465 assert(table != NULL);
466 table_is_malloced = table != so->smalltable;
467
468 /* This is delicate. During the process of clearing the set,
469 * decrefs can cause the set to mutate. To avoid fatal confusion
470 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000471 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472 * clearing.
473 */
474 fill = so->fill;
475 if (table_is_malloced)
476 EMPTY_TO_MINSIZE(so);
477
478 else if (fill > 0) {
479 /* It's a small table with something that needs to be cleared.
480 * Afraid the only safe way is to copy the set entries into
481 * another small table first.
482 */
483 memcpy(small_copy, table, sizeof(small_copy));
484 table = small_copy;
485 EMPTY_TO_MINSIZE(so);
486 }
487 /* else it's a small table that's already empty */
488
489 /* Now we can finally clear things. If C had refcounts, we could
490 * assert that the refcount on table is 1 now, i.e. that this function
491 * has unique access to it, so decref side-effects can't alter it.
492 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000493 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494#ifdef Py_DEBUG
495 assert(i < n);
496 ++i;
497#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000498 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000500 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501 }
502#ifdef Py_DEBUG
503 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000504 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505#endif
506 }
507
508 if (table_is_malloced)
509 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000510 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000511}
512
513/*
514 * Iterate over a set table. Use like so:
515 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000516 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000517 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000518 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000519 * while (set_next(yourset, &pos, &entry)) {
520 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000521 * }
522 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000523 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000524 * mutates the table.
525 */
526static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000527set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000528{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000529 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000530 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000531 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532
533 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000534 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000535 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000536 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000537 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000538 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000539 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000540 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000541 if (i > mask)
542 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000543 assert(table[i].key != NULL);
544 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545 return 1;
546}
547
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000548static void
549set_dealloc(PySetObject *so)
550{
551 register setentry *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000552 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000553 PyObject_GC_UnTrack(so);
554 Py_TRASHCAN_SAFE_BEGIN(so)
555 if (so->weakreflist != NULL)
556 PyObject_ClearWeakRefs((PyObject *) so);
557
558 for (entry = so->table; fill > 0; entry++) {
559 if (entry->key) {
560 --fill;
561 Py_DECREF(entry->key);
562 }
563 }
564 if (so->table != so->smalltable)
565 PyMem_DEL(so->table);
Christian Heimes2202f872008-02-06 14:31:34 +0000566 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
567 free_list[numfree++] = so;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000569 Py_TYPE(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000570 Py_TRASHCAN_SAFE_END(so)
571}
572
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000573static PyObject *
574set_repr(PySetObject *so)
575{
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000576 PyObject *keys, *result=NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000577 Py_UNICODE *u;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000578 int status = Py_ReprEnter((PyObject*)so);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000579 PyObject *listrepr;
580 Py_ssize_t newsize;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000581
582 if (status != 0) {
583 if (status < 0)
584 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000585 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000586 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000587
Georg Brandlc4996ba2006-08-28 19:37:11 +0000588 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000589 if (!so->used) {
590 Py_ReprLeave((PyObject*)so);
Christian Heimes90aa7642007-12-19 02:45:37 +0000591 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000592 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000593
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000594 keys = PySequence_List((PyObject *)so);
595 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000596 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000597
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000598 listrepr = PyObject_Repr(keys);
599 Py_DECREF(keys);
600 if (listrepr == NULL) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000601 Py_DECREF(keys);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000602 goto done;
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000603 }
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000604 newsize = PyUnicode_GET_SIZE(listrepr);
605 result = PyUnicode_FromUnicode(NULL, newsize);
606 if (result) {
607 u = PyUnicode_AS_UNICODE(result);
608 *u++ = '{';
609 /* Omit the brackets from the listrepr */
610 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
611 PyUnicode_GET_SIZE(listrepr)-2);
612 u += newsize-2;
613 *u++ = '}';
614 }
615 Py_DECREF(listrepr);
Christian Heimes90aa7642007-12-19 02:45:37 +0000616 if (Py_TYPE(so) != &PySet_Type) {
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000617 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000618 Py_TYPE(so)->tp_name,
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000619 result);
620 Py_DECREF(result);
621 result = tmp;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000622 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000623done:
624 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000625 return result;
626}
627
Martin v. Löwis18e16552006-02-15 17:27:45 +0000628static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000629set_len(PyObject *so)
630{
631 return ((PySetObject *)so)->used;
632}
633
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000634static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000635set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000636{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000637 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000638 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000639 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000640
641 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000642 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000643
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000644 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000645 if (other == so || other->used == 0)
646 /* a.update(a) or a.update({}); nothing to do */
647 return 0;
648 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000649 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000650 * that there will be no (or few) overlapping keys.
651 */
652 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
653 if (set_table_resize(so, (so->used + other->used)*2) != 0)
654 return -1;
655 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000656 for (i = 0; i <= other->mask; i++) {
657 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000658 if (entry->key != NULL &&
659 entry->key != dummy) {
660 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000661 if (set_insert_key(so, entry->key, entry->hash) == -1) {
662 Py_DECREF(entry->key);
663 return -1;
664 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000665 }
666 }
667 return 0;
668}
669
670static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000671set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000672{
673 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000674 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000675
Christian Heimes0ded5b52007-12-10 15:50:56 +0000676 if (!PyUnicode_CheckExact(key) ||
677 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000678 hash = PyObject_Hash(key);
679 if (hash == -1)
680 return -1;
681 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000682 entry = (so->lookup)(so, key, hash);
683 if (entry == NULL)
684 return -1;
685 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000686 return key != NULL && key != dummy;
687}
688
Raymond Hettingerc991db22005-08-11 07:58:45 +0000689static int
690set_contains_entry(PySetObject *so, setentry *entry)
691{
692 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000693 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000694
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000695 lu_entry = (so->lookup)(so, entry->key, entry->hash);
696 if (lu_entry == NULL)
697 return -1;
698 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000699 return key != NULL && key != dummy;
700}
701
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000702static PyObject *
703set_pop(PySetObject *so)
704{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000705 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000706 register setentry *entry;
707 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000708
709 assert (PyAnySet_Check(so));
710 if (so->used == 0) {
711 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
712 return NULL;
713 }
714
715 /* Set entry to "the first" unused or dummy set entry. We abuse
716 * the hash field of slot 0 to hold a search finger:
717 * If slot 0 has a value, use slot 0.
718 * Else slot 0 is being used to hold a search finger,
719 * and we use its hash value as the first index to look.
720 */
721 entry = &so->table[0];
722 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000723 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000724 /* The hash field may be a real hash value, or it may be a
725 * legit search finger, or it may be a once-legit search
726 * finger that's out of bounds now because it wrapped around
727 * or the table shrunk -- simply make sure it's in bounds now.
728 */
729 if (i > so->mask || i < 1)
730 i = 1; /* skip slot 0 */
731 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
732 i++;
733 if (i > so->mask)
734 i = 1;
735 }
736 }
737 key = entry->key;
738 Py_INCREF(dummy);
739 entry->key = dummy;
740 so->used--;
741 so->table[0].hash = i + 1; /* next place to start */
742 return key;
743}
744
745PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
746
747static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000748set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000749{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000750 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000751 setentry *entry;
752
753 while (set_next(so, &pos, &entry))
754 Py_VISIT(entry->key);
755 return 0;
756}
757
758static long
759frozenset_hash(PyObject *self)
760{
761 PySetObject *so = (PySetObject *)self;
762 long h, hash = 1927868237L;
763 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000764 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000765
766 if (so->hash != -1)
767 return so->hash;
768
769 hash *= PySet_GET_SIZE(self) + 1;
770 while (set_next(so, &pos, &entry)) {
771 /* Work to increase the bit dispersion for closely spaced hash
772 values. The is important because some use cases have many
773 combinations of a small number of elements with nearby
774 hashes so that many distinct combinations collapse to only
775 a handful of distinct hash values. */
776 h = entry->hash;
777 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
778 }
779 hash = hash * 69069L + 907133923L;
780 if (hash == -1)
781 hash = 590923713L;
782 so->hash = hash;
783 return hash;
784}
785
Raymond Hettingera9d99362005-08-05 00:01:15 +0000786/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788typedef struct {
789 PyObject_HEAD
790 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000791 Py_ssize_t si_used;
792 Py_ssize_t si_pos;
793 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794} setiterobject;
795
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796static void
797setiter_dealloc(setiterobject *si)
798{
799 Py_XDECREF(si->si_set);
800 PyObject_Del(si);
801}
802
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000803static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804setiter_len(setiterobject *si)
805{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000806 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000808 len = si->len;
Christian Heimes217cfd12007-12-02 14:31:20 +0000809 return PyLong_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810}
811
Armin Rigof5b3e362006-02-11 21:32:43 +0000812PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000813
814static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000815 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000816 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000817};
818
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000819static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000820{
821 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000822 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000823 register setentry *entry;
824 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000826 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000827 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000828 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000830 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831 PyErr_SetString(PyExc_RuntimeError,
832 "Set changed size during iteration");
833 si->si_used = -1; /* Make this state sticky */
834 return NULL;
835 }
836
837 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000838 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000839 entry = so->table;
840 mask = so->mask;
841 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000842 i++;
843 si->si_pos = i+1;
844 if (i > mask)
845 goto fail;
846 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000847 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848 Py_INCREF(key);
849 return key;
850
851fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000852 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853 si->si_set = NULL;
854 return NULL;
855}
856
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000857PyTypeObject PySetIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000858 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +0000859 "set_iterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000860 sizeof(setiterobject), /* tp_basicsize */
861 0, /* tp_itemsize */
862 /* methods */
863 (destructor)setiter_dealloc, /* tp_dealloc */
864 0, /* tp_print */
865 0, /* tp_getattr */
866 0, /* tp_setattr */
867 0, /* tp_compare */
868 0, /* tp_repr */
869 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000870 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000871 0, /* tp_as_mapping */
872 0, /* tp_hash */
873 0, /* tp_call */
874 0, /* tp_str */
875 PyObject_GenericGetAttr, /* tp_getattro */
876 0, /* tp_setattro */
877 0, /* tp_as_buffer */
878 Py_TPFLAGS_DEFAULT, /* tp_flags */
879 0, /* tp_doc */
880 0, /* tp_traverse */
881 0, /* tp_clear */
882 0, /* tp_richcompare */
883 0, /* tp_weaklistoffset */
884 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000885 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000886 setiter_methods, /* tp_methods */
887 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000888};
889
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000890static PyObject *
891set_iter(PySetObject *so)
892{
893 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
894 if (si == NULL)
895 return NULL;
896 Py_INCREF(so);
897 si->si_set = so;
898 si->si_used = so->used;
899 si->si_pos = 0;
900 si->len = so->used;
901 return (PyObject *)si;
902}
903
Raymond Hettingerd7946662005-08-01 21:39:29 +0000904static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000905set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000906{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000907 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000908
Christian Heimesaf98da12008-01-27 15:18:18 +0000909 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000910 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000911
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000912 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000913 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000914 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000915 long hash;
916 Py_ssize_t dictsize = PyDict_Size(other);
917
918 /* Do one big resize at the start, rather than
919 * incrementally resizing as we insert new keys. Expect
920 * that there will be no (or few) overlapping keys.
921 */
922 if (dictsize == -1)
923 return -1;
924 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
925 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
926 return -1;
927 }
928 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
929 setentry an_entry;
930
931 an_entry.hash = hash;
932 an_entry.key = key;
933 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000934 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000935 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000936 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000937 }
938
Raymond Hettingera38123e2003-11-24 22:18:49 +0000939 it = PyObject_GetIter(other);
940 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000941 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000942
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000943 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000944 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000945 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000946 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000947 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000948 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000949 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000950 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000951 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000952 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000953 return -1;
954 return 0;
955}
956
957static PyObject *
958set_update(PySetObject *so, PyObject *other)
959{
960 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000961 return NULL;
962 Py_RETURN_NONE;
963}
964
965PyDoc_STRVAR(update_doc,
966"Update a set with the union of itself and another.");
967
968static PyObject *
969make_new_set(PyTypeObject *type, PyObject *iterable)
970{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000971 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000972
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000973 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000974 dummy = PyUnicode_FromString("<dummy key>");
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000975 if (dummy == NULL)
976 return NULL;
977 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000978
979 /* create PySetObject structure */
Christian Heimes2202f872008-02-06 14:31:34 +0000980 if (numfree &&
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000981 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
Christian Heimes2202f872008-02-06 14:31:34 +0000982 so = free_list[--numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000983 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimes90aa7642007-12-19 02:45:37 +0000984 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000985 _Py_NewReference((PyObject *)so);
986 EMPTY_TO_MINSIZE(so);
987 PyObject_GC_Track(so);
988 } else {
989 so = (PySetObject *)type->tp_alloc(type, 0);
990 if (so == NULL)
991 return NULL;
992 /* tp_alloc has already zeroed the structure */
993 assert(so->table == NULL && so->fill == 0 && so->used == 0);
994 INIT_NONZERO_SET_SLOTS(so);
995 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000996
Christian Heimes0ded5b52007-12-10 15:50:56 +0000997 so->lookup = set_lookkey_unicode;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000998 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000999
Raymond Hettingera38123e2003-11-24 22:18:49 +00001000 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001001 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001002 Py_DECREF(so);
1003 return NULL;
1004 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001005 }
1006
Raymond Hettingera690a992003-11-16 16:17:49 +00001007 return (PyObject *)so;
1008}
1009
Raymond Hettingerd7946662005-08-01 21:39:29 +00001010/* The empty frozenset is a singleton */
1011static PyObject *emptyfrozenset = NULL;
1012
Raymond Hettingera690a992003-11-16 16:17:49 +00001013static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001014frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001015{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001016 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001017
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001018 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001019 return NULL;
1020
Raymond Hettingera690a992003-11-16 16:17:49 +00001021 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1022 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001023
1024 if (type != &PyFrozenSet_Type)
1025 return make_new_set(type, iterable);
1026
1027 if (iterable != NULL) {
1028 /* frozenset(f) is idempotent */
1029 if (PyFrozenSet_CheckExact(iterable)) {
1030 Py_INCREF(iterable);
1031 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001032 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001033 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001034 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001035 return result;
1036 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001037 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001038 /* The empty frozenset is a singleton */
1039 if (emptyfrozenset == NULL)
1040 emptyfrozenset = make_new_set(type, NULL);
1041 Py_XINCREF(emptyfrozenset);
1042 return emptyfrozenset;
1043}
1044
1045void
1046PySet_Fini(void)
1047{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001048 PySetObject *so;
1049
Christian Heimes2202f872008-02-06 14:31:34 +00001050 while (numfree) {
1051 numfree--;
1052 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001053 PyObject_GC_Del(so);
1054 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055 Py_CLEAR(dummy);
1056 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001057}
1058
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001059static PyObject *
1060set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1061{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001062 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001063 return NULL;
1064
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001065 return make_new_set(type, NULL);
1066}
1067
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001068/* set_swap_bodies() switches the contents of any two sets by moving their
1069 internal data pointers and, if needed, copying the internal smalltables.
1070 Semantically equivalent to:
1071
1072 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1073
1074 The function always succeeds and it leaves both objects in a stable state.
1075 Useful for creating temporary frozensets from sets for membership testing
1076 in __contains__(), discard(), and remove(). Also useful for operations
1077 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001078 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001079*/
1080
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001081static void
1082set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001083{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001084 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001085 setentry *u;
1086 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1087 setentry tab[PySet_MINSIZE];
1088 long h;
1089
1090 t = a->fill; a->fill = b->fill; b->fill = t;
1091 t = a->used; a->used = b->used; b->used = t;
1092 t = a->mask; a->mask = b->mask; b->mask = t;
1093
1094 u = a->table;
1095 if (a->table == a->smalltable)
1096 u = b->smalltable;
1097 a->table = b->table;
1098 if (b->table == b->smalltable)
1099 a->table = a->smalltable;
1100 b->table = u;
1101
1102 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1103
1104 if (a->table == a->smalltable || b->table == b->smalltable) {
1105 memcpy(tab, a->smalltable, sizeof(tab));
1106 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1107 memcpy(b->smalltable, tab, sizeof(tab));
1108 }
1109
Christian Heimes90aa7642007-12-19 02:45:37 +00001110 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1111 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001112 h = a->hash; a->hash = b->hash; b->hash = h;
1113 } else {
1114 a->hash = -1;
1115 b->hash = -1;
1116 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001117}
1118
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001119static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001120set_copy(PySetObject *so)
1121{
Christian Heimes90aa7642007-12-19 02:45:37 +00001122 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001123}
1124
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001125static PyObject *
1126frozenset_copy(PySetObject *so)
1127{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001128 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001129 Py_INCREF(so);
1130 return (PyObject *)so;
1131 }
1132 return set_copy(so);
1133}
1134
Raymond Hettingera690a992003-11-16 16:17:49 +00001135PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1136
1137static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001138set_clear(PySetObject *so)
1139{
1140 set_clear_internal(so);
1141 Py_RETURN_NONE;
1142}
1143
1144PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1145
1146static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001147set_union(PySetObject *so, PyObject *other)
1148{
1149 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001150
1151 result = (PySetObject *)set_copy(so);
1152 if (result == NULL)
1153 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001154 if ((PyObject *)so == other)
1155 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001156 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001157 Py_DECREF(result);
1158 return NULL;
1159 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001160 return (PyObject *)result;
1161}
1162
1163PyDoc_STRVAR(union_doc,
1164 "Return the union of two sets as a new set.\n\
1165\n\
1166(i.e. all elements that are in either set.)");
1167
1168static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001169set_or(PySetObject *so, PyObject *other)
1170{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001171 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001172 Py_INCREF(Py_NotImplemented);
1173 return Py_NotImplemented;
1174 }
1175 return set_union(so, other);
1176}
1177
1178static PyObject *
1179set_ior(PySetObject *so, PyObject *other)
1180{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001181 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001182 Py_INCREF(Py_NotImplemented);
1183 return Py_NotImplemented;
1184 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001185 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001186 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001187 Py_INCREF(so);
1188 return (PyObject *)so;
1189}
1190
1191static PyObject *
1192set_intersection(PySetObject *so, PyObject *other)
1193{
1194 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001195 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001196
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001197 if ((PyObject *)so == other)
1198 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001199
Christian Heimes90aa7642007-12-19 02:45:37 +00001200 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001201 if (result == NULL)
1202 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001203
Christian Heimesaf98da12008-01-27 15:18:18 +00001204 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001205 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001206 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001207
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001208 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001209 tmp = (PyObject *)so;
1210 so = (PySetObject *)other;
1211 other = tmp;
1212 }
1213
Raymond Hettingerc991db22005-08-11 07:58:45 +00001214 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001215 int rv = set_contains_entry(so, entry);
1216 if (rv == -1) {
1217 Py_DECREF(result);
1218 return NULL;
1219 }
1220 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001221 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001222 Py_DECREF(result);
1223 return NULL;
1224 }
1225 }
1226 }
1227 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001228 }
1229
Raymond Hettingera690a992003-11-16 16:17:49 +00001230 it = PyObject_GetIter(other);
1231 if (it == NULL) {
1232 Py_DECREF(result);
1233 return NULL;
1234 }
1235
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001236 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001237 int rv;
1238 setentry entry;
1239 long hash = PyObject_Hash(key);
1240
1241 if (hash == -1) {
1242 Py_DECREF(it);
1243 Py_DECREF(result);
1244 Py_DECREF(key);
1245 return NULL;
1246 }
1247 entry.hash = hash;
1248 entry.key = key;
1249 rv = set_contains_entry(so, &entry);
1250 if (rv == -1) {
1251 Py_DECREF(it);
1252 Py_DECREF(result);
1253 Py_DECREF(key);
1254 return NULL;
1255 }
1256 if (rv) {
1257 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001258 Py_DECREF(it);
1259 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001260 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001261 return NULL;
1262 }
1263 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001264 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001265 }
1266 Py_DECREF(it);
1267 if (PyErr_Occurred()) {
1268 Py_DECREF(result);
1269 return NULL;
1270 }
1271 return (PyObject *)result;
1272}
1273
1274PyDoc_STRVAR(intersection_doc,
1275"Return the intersection of two sets as a new set.\n\
1276\n\
1277(i.e. all elements that are in both sets.)");
1278
1279static PyObject *
1280set_intersection_update(PySetObject *so, PyObject *other)
1281{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001282 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001283
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001284 tmp = set_intersection(so, other);
1285 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001286 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001287 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001288 Py_DECREF(tmp);
1289 Py_RETURN_NONE;
1290}
1291
1292PyDoc_STRVAR(intersection_update_doc,
1293"Update a set with the intersection of itself and another.");
1294
1295static PyObject *
1296set_and(PySetObject *so, PyObject *other)
1297{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001298 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001299 Py_INCREF(Py_NotImplemented);
1300 return Py_NotImplemented;
1301 }
1302 return set_intersection(so, other);
1303}
1304
1305static PyObject *
1306set_iand(PySetObject *so, PyObject *other)
1307{
1308 PyObject *result;
1309
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001310 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001311 Py_INCREF(Py_NotImplemented);
1312 return Py_NotImplemented;
1313 }
1314 result = set_intersection_update(so, other);
1315 if (result == NULL)
1316 return NULL;
1317 Py_DECREF(result);
1318 Py_INCREF(so);
1319 return (PyObject *)so;
1320}
1321
Guido van Rossum58da9312007-11-10 23:39:45 +00001322static PyObject *
1323set_isdisjoint(PySetObject *so, PyObject *other)
1324{
1325 PyObject *key, *it, *tmp;
1326
1327 if ((PyObject *)so == other) {
1328 if (PySet_GET_SIZE(so) == 0)
1329 Py_RETURN_TRUE;
1330 else
1331 Py_RETURN_FALSE;
1332 }
1333
1334 if (PyAnySet_CheckExact(other)) {
1335 Py_ssize_t pos = 0;
1336 setentry *entry;
1337
1338 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1339 tmp = (PyObject *)so;
1340 so = (PySetObject *)other;
1341 other = tmp;
1342 }
1343 while (set_next((PySetObject *)other, &pos, &entry)) {
1344 int rv = set_contains_entry(so, entry);
1345 if (rv == -1)
1346 return NULL;
1347 if (rv)
1348 Py_RETURN_FALSE;
1349 }
1350 Py_RETURN_TRUE;
1351 }
1352
1353 it = PyObject_GetIter(other);
1354 if (it == NULL)
1355 return NULL;
1356
1357 while ((key = PyIter_Next(it)) != NULL) {
1358 int rv;
1359 setentry entry;
Christian Heimes0ded5b52007-12-10 15:50:56 +00001360 long hash = PyObject_Hash(key);;
Guido van Rossum58da9312007-11-10 23:39:45 +00001361
1362 if (hash == -1) {
1363 Py_DECREF(key);
1364 Py_DECREF(it);
1365 return NULL;
1366 }
1367 entry.hash = hash;
1368 entry.key = key;
1369 rv = set_contains_entry(so, &entry);
1370 Py_DECREF(key);
1371 if (rv == -1) {
1372 Py_DECREF(it);
1373 return NULL;
1374 }
1375 if (rv) {
1376 Py_DECREF(it);
1377 Py_RETURN_FALSE;
1378 }
1379 }
1380 Py_DECREF(it);
1381 if (PyErr_Occurred())
1382 return NULL;
1383 Py_RETURN_TRUE;
1384}
1385
1386PyDoc_STRVAR(isdisjoint_doc,
1387"Return True if two sets have a null intersection.");
1388
Neal Norwitz6576bd82005-11-13 18:41:28 +00001389static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001390set_difference_update_internal(PySetObject *so, PyObject *other)
1391{
1392 if ((PyObject *)so == other)
1393 return set_clear_internal(so);
1394
Christian Heimesaf98da12008-01-27 15:18:18 +00001395 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001396 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001397 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001398
1399 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001400 if (set_discard_entry(so, entry) == -1)
1401 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001402 } else {
1403 PyObject *key, *it;
1404 it = PyObject_GetIter(other);
1405 if (it == NULL)
1406 return -1;
1407
1408 while ((key = PyIter_Next(it)) != NULL) {
1409 if (set_discard_key(so, key) == -1) {
1410 Py_DECREF(it);
1411 Py_DECREF(key);
1412 return -1;
1413 }
1414 Py_DECREF(key);
1415 }
1416 Py_DECREF(it);
1417 if (PyErr_Occurred())
1418 return -1;
1419 }
1420 /* If more than 1/5 are dummies, then resize them away. */
1421 if ((so->fill - so->used) * 5 < so->mask)
1422 return 0;
1423 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1424}
1425
Raymond Hettingera690a992003-11-16 16:17:49 +00001426static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001427set_difference_update(PySetObject *so, PyObject *other)
1428{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001429 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001430 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001431 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001432}
1433
1434PyDoc_STRVAR(difference_update_doc,
1435"Remove all elements of another set from this set.");
1436
1437static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001438set_difference(PySetObject *so, PyObject *other)
1439{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001440 PyObject *result;
1441 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001442 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001443
Christian Heimesaf98da12008-01-27 15:18:18 +00001444 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001445 result = set_copy(so);
1446 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001447 return NULL;
1448 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001449 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001450 Py_DECREF(result);
1451 return NULL;
1452 }
1453
Christian Heimes90aa7642007-12-19 02:45:37 +00001454 result = make_new_set(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001455 if (result == NULL)
1456 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001457
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001458 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001459 while (set_next(so, &pos, &entry)) {
1460 setentry entrycopy;
1461 entrycopy.hash = entry->hash;
1462 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001463 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001464 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1465 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001466 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001467 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001468 }
1469 }
1470 return result;
1471 }
1472
Raymond Hettingerc991db22005-08-11 07:58:45 +00001473 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001474 int rv = set_contains_entry((PySetObject *)other, entry);
1475 if (rv == -1) {
1476 Py_DECREF(result);
1477 return NULL;
1478 }
1479 if (!rv) {
1480 if (set_add_entry((PySetObject *)result, entry) == -1) {
1481 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001482 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001483 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001484 }
1485 }
1486 return result;
1487}
1488
1489PyDoc_STRVAR(difference_doc,
1490"Return the difference of two sets as a new set.\n\
1491\n\
1492(i.e. all elements that are in this set but not the other.)");
1493static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001494set_sub(PySetObject *so, PyObject *other)
1495{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001496 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001497 Py_INCREF(Py_NotImplemented);
1498 return Py_NotImplemented;
1499 }
1500 return set_difference(so, other);
1501}
1502
1503static PyObject *
1504set_isub(PySetObject *so, PyObject *other)
1505{
1506 PyObject *result;
1507
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001508 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001509 Py_INCREF(Py_NotImplemented);
1510 return Py_NotImplemented;
1511 }
1512 result = set_difference_update(so, other);
1513 if (result == NULL)
1514 return NULL;
1515 Py_DECREF(result);
1516 Py_INCREF(so);
1517 return (PyObject *)so;
1518}
1519
1520static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001521set_symmetric_difference_update(PySetObject *so, PyObject *other)
1522{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001523 PySetObject *otherset;
1524 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001525 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001526 setentry *entry;
1527
1528 if ((PyObject *)so == other)
1529 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001530
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001531 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001532 PyObject *value;
1533 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001534 long hash;
1535 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001536 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001537
Thomas Wouters89f507f2006-12-13 04:49:30 +00001538 an_entry.hash = hash;
1539 an_entry.key = key;
1540 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001541 if (rv == -1)
1542 return NULL;
1543 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001544 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001545 return NULL;
1546 }
1547 }
1548 Py_RETURN_NONE;
1549 }
1550
Christian Heimesaf98da12008-01-27 15:18:18 +00001551 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001552 Py_INCREF(other);
1553 otherset = (PySetObject *)other;
1554 } else {
Christian Heimes90aa7642007-12-19 02:45:37 +00001555 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001556 if (otherset == NULL)
1557 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001558 }
1559
Raymond Hettingerc991db22005-08-11 07:58:45 +00001560 while (set_next(otherset, &pos, &entry)) {
1561 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001562 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001563 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001564 return NULL;
1565 }
1566 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001567 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001568 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001569 return NULL;
1570 }
1571 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001572 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001573 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001574 Py_RETURN_NONE;
1575}
1576
1577PyDoc_STRVAR(symmetric_difference_update_doc,
1578"Update a set with the symmetric difference of itself and another.");
1579
1580static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001581set_symmetric_difference(PySetObject *so, PyObject *other)
1582{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001583 PyObject *rv;
1584 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001585
Christian Heimes90aa7642007-12-19 02:45:37 +00001586 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001587 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001588 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001589 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1590 if (rv == NULL)
1591 return NULL;
1592 Py_DECREF(rv);
1593 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001594}
1595
1596PyDoc_STRVAR(symmetric_difference_doc,
1597"Return the symmetric difference of two sets as a new set.\n\
1598\n\
1599(i.e. all elements that are in exactly one of the sets.)");
1600
1601static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001602set_xor(PySetObject *so, PyObject *other)
1603{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001604 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001605 Py_INCREF(Py_NotImplemented);
1606 return Py_NotImplemented;
1607 }
1608 return set_symmetric_difference(so, other);
1609}
1610
1611static PyObject *
1612set_ixor(PySetObject *so, PyObject *other)
1613{
1614 PyObject *result;
1615
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001616 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001617 Py_INCREF(Py_NotImplemented);
1618 return Py_NotImplemented;
1619 }
1620 result = set_symmetric_difference_update(so, other);
1621 if (result == NULL)
1622 return NULL;
1623 Py_DECREF(result);
1624 Py_INCREF(so);
1625 return (PyObject *)so;
1626}
1627
1628static PyObject *
1629set_issubset(PySetObject *so, PyObject *other)
1630{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001631 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001632 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001633
Christian Heimesaf98da12008-01-27 15:18:18 +00001634 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001635 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001636 tmp = make_new_set(&PySet_Type, other);
1637 if (tmp == NULL)
1638 return NULL;
1639 result = set_issubset(so, tmp);
1640 Py_DECREF(tmp);
1641 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001642 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001643 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001644 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001645
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001646 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001647 int rv = set_contains_entry((PySetObject *)other, entry);
1648 if (rv == -1)
1649 return NULL;
1650 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001651 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001652 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001653 Py_RETURN_TRUE;
1654}
1655
1656PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1657
1658static PyObject *
1659set_issuperset(PySetObject *so, PyObject *other)
1660{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001661 PyObject *tmp, *result;
1662
Christian Heimesaf98da12008-01-27 15:18:18 +00001663 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001664 tmp = make_new_set(&PySet_Type, other);
1665 if (tmp == NULL)
1666 return NULL;
1667 result = set_issuperset(so, tmp);
1668 Py_DECREF(tmp);
1669 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001670 }
1671 return set_issubset((PySetObject *)other, (PyObject *)so);
1672}
1673
1674PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1675
Raymond Hettingera690a992003-11-16 16:17:49 +00001676static PyObject *
1677set_richcompare(PySetObject *v, PyObject *w, int op)
1678{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001679 PyObject *r1, *r2;
1680
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001681 if(!PyAnySet_Check(w)) {
Guido van Rossum10ab4ae2007-08-23 23:57:24 +00001682 Py_INCREF(Py_NotImplemented);
1683 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001684 }
1685 switch (op) {
1686 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001687 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001688 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001689 if (v->hash != -1 &&
1690 ((PySetObject *)w)->hash != -1 &&
1691 v->hash != ((PySetObject *)w)->hash)
1692 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001693 return set_issubset(v, w);
1694 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001695 r1 = set_richcompare(v, w, Py_EQ);
1696 if (r1 == NULL)
1697 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001698 r2 = PyBool_FromLong(PyObject_Not(r1));
1699 Py_DECREF(r1);
1700 return r2;
1701 case Py_LE:
1702 return set_issubset(v, w);
1703 case Py_GE:
1704 return set_issuperset(v, w);
1705 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001706 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001707 Py_RETURN_FALSE;
1708 return set_issubset(v, w);
1709 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001710 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001711 Py_RETURN_FALSE;
1712 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001713 }
1714 Py_INCREF(Py_NotImplemented);
1715 return Py_NotImplemented;
1716}
1717
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001718static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001719set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001720{
1721 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1722 return -1;
1723}
1724
Raymond Hettingera690a992003-11-16 16:17:49 +00001725static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001726set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001727{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001728 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001729 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001730 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001731}
1732
1733PyDoc_STRVAR(add_doc,
1734"Add an element to a set.\n\
1735\n\
1736This has no effect if the element is already present.");
1737
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001738static int
1739set_contains(PySetObject *so, PyObject *key)
1740{
1741 PyObject *tmpkey;
1742 int rv;
1743
1744 rv = set_contains_key(so, key);
1745 if (rv == -1) {
1746 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1747 return -1;
1748 PyErr_Clear();
1749 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1750 if (tmpkey == NULL)
1751 return -1;
1752 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1753 rv = set_contains(so, tmpkey);
1754 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1755 Py_DECREF(tmpkey);
1756 }
1757 return rv;
1758}
1759
1760static PyObject *
1761set_direct_contains(PySetObject *so, PyObject *key)
1762{
1763 long result;
1764
1765 result = set_contains(so, key);
1766 if (result == -1)
1767 return NULL;
1768 return PyBool_FromLong(result);
1769}
1770
1771PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1772
Raymond Hettingera690a992003-11-16 16:17:49 +00001773static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001774set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001775{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001776 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001777 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001778
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001779 rv = set_discard_key(so, key);
1780 if (rv == -1) {
1781 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1782 return NULL;
1783 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001784 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1785 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001786 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001787 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001788 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001789 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001790 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001791 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001792 } else if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001793 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001794 return NULL;
1795 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001796 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001797}
1798
1799PyDoc_STRVAR(remove_doc,
1800"Remove an element from a set; it must be a member.\n\
1801\n\
1802If the element is not a member, raise a KeyError.");
1803
1804static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001805set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001806{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001807 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001808 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001809
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001810 rv = set_discard_key(so, key);
1811 if (rv == -1) {
1812 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1813 return NULL;
1814 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001815 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1816 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001817 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001818 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001819 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001820 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001821 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001822 return result;
1823 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001824 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001825}
1826
1827PyDoc_STRVAR(discard_doc,
1828"Remove an element from a set if it is a member.\n\
1829\n\
1830If the element is not a member, do nothing.");
1831
1832static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001833set_reduce(PySetObject *so)
1834{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001835 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001836
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001837 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001838 if (keys == NULL)
1839 goto done;
1840 args = PyTuple_Pack(1, keys);
1841 if (args == NULL)
1842 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001843 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1844 if (dict == NULL) {
1845 PyErr_Clear();
1846 dict = Py_None;
1847 Py_INCREF(dict);
1848 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001849 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001850done:
1851 Py_XDECREF(args);
1852 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001853 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001854 return result;
1855}
1856
1857PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1858
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001859static int
1860set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1861{
1862 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001863
1864 if (!PyAnySet_Check(self))
1865 return -1;
Christian Heimes90aa7642007-12-19 02:45:37 +00001866 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001867 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001868 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001869 self->hash = -1;
1870 if (iterable == NULL)
1871 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001872 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001873}
1874
Raymond Hettingera690a992003-11-16 16:17:49 +00001875static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001876 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001877 0, /* sq_concat */
1878 0, /* sq_repeat */
1879 0, /* sq_item */
1880 0, /* sq_slice */
1881 0, /* sq_ass_item */
1882 0, /* sq_ass_slice */
1883 (objobjproc)set_contains, /* sq_contains */
1884};
1885
1886/* set object ********************************************************/
1887
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001888#ifdef Py_DEBUG
1889static PyObject *test_c_api(PySetObject *so);
1890
1891PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1892All is well if assertions don't fail.");
1893#endif
1894
Raymond Hettingera690a992003-11-16 16:17:49 +00001895static PyMethodDef set_methods[] = {
1896 {"add", (PyCFunction)set_add, METH_O,
1897 add_doc},
1898 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1899 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001900 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001901 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001902 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1903 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001904 {"discard", (PyCFunction)set_discard, METH_O,
1905 discard_doc},
1906 {"difference", (PyCFunction)set_difference, METH_O,
1907 difference_doc},
1908 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1909 difference_update_doc},
1910 {"intersection",(PyCFunction)set_intersection, METH_O,
1911 intersection_doc},
1912 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1913 intersection_update_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00001914 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1915 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001916 {"issubset", (PyCFunction)set_issubset, METH_O,
1917 issubset_doc},
1918 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1919 issuperset_doc},
1920 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1921 pop_doc},
1922 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1923 reduce_doc},
1924 {"remove", (PyCFunction)set_remove, METH_O,
1925 remove_doc},
1926 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1927 symmetric_difference_doc},
1928 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1929 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001930#ifdef Py_DEBUG
1931 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1932 test_c_api_doc},
1933#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001934 {"union", (PyCFunction)set_union, METH_O,
1935 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001936 {"update", (PyCFunction)set_update, METH_O,
1937 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001938 {NULL, NULL} /* sentinel */
1939};
1940
1941static PyNumberMethods set_as_number = {
1942 0, /*nb_add*/
1943 (binaryfunc)set_sub, /*nb_subtract*/
1944 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001945 0, /*nb_remainder*/
1946 0, /*nb_divmod*/
1947 0, /*nb_power*/
1948 0, /*nb_negative*/
1949 0, /*nb_positive*/
1950 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001951 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001952 0, /*nb_invert*/
1953 0, /*nb_lshift*/
1954 0, /*nb_rshift*/
1955 (binaryfunc)set_and, /*nb_and*/
1956 (binaryfunc)set_xor, /*nb_xor*/
1957 (binaryfunc)set_or, /*nb_or*/
Neil Schemenauer16c70752007-09-21 20:19:23 +00001958 0, /*nb_reserved*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001959 0, /*nb_int*/
1960 0, /*nb_long*/
1961 0, /*nb_float*/
1962 0, /*nb_oct*/
1963 0, /*nb_hex*/
1964 0, /*nb_inplace_add*/
1965 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1966 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001967 0, /*nb_inplace_remainder*/
1968 0, /*nb_inplace_power*/
1969 0, /*nb_inplace_lshift*/
1970 0, /*nb_inplace_rshift*/
1971 (binaryfunc)set_iand, /*nb_inplace_and*/
1972 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1973 (binaryfunc)set_ior, /*nb_inplace_or*/
1974};
1975
1976PyDoc_STRVAR(set_doc,
1977"set(iterable) --> set object\n\
1978\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001979Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001980
1981PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001982 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001983 "set", /* tp_name */
1984 sizeof(PySetObject), /* tp_basicsize */
1985 0, /* tp_itemsize */
1986 /* methods */
1987 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001988 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00001989 0, /* tp_getattr */
1990 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001991 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001992 (reprfunc)set_repr, /* tp_repr */
1993 &set_as_number, /* tp_as_number */
1994 &set_as_sequence, /* tp_as_sequence */
1995 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001996 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001997 0, /* tp_call */
1998 0, /* tp_str */
1999 PyObject_GenericGetAttr, /* tp_getattro */
2000 0, /* tp_setattro */
2001 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002002 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002003 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002004 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002005 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002006 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002007 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002008 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002009 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002010 0, /* tp_iternext */
2011 set_methods, /* tp_methods */
2012 0, /* tp_members */
2013 0, /* tp_getset */
2014 0, /* tp_base */
2015 0, /* tp_dict */
2016 0, /* tp_descr_get */
2017 0, /* tp_descr_set */
2018 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002019 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002020 PyType_GenericAlloc, /* tp_alloc */
2021 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002022 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002023};
2024
2025/* frozenset object ********************************************************/
2026
2027
2028static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002029 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002030 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002031 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002032 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002033 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002034 difference_doc},
2035 {"intersection",(PyCFunction)set_intersection, METH_O,
2036 intersection_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002037 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2038 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002039 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002040 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002041 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002042 issuperset_doc},
2043 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2044 reduce_doc},
2045 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2046 symmetric_difference_doc},
2047 {"union", (PyCFunction)set_union, METH_O,
2048 union_doc},
2049 {NULL, NULL} /* sentinel */
2050};
2051
2052static PyNumberMethods frozenset_as_number = {
2053 0, /*nb_add*/
2054 (binaryfunc)set_sub, /*nb_subtract*/
2055 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002056 0, /*nb_remainder*/
2057 0, /*nb_divmod*/
2058 0, /*nb_power*/
2059 0, /*nb_negative*/
2060 0, /*nb_positive*/
2061 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002062 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002063 0, /*nb_invert*/
2064 0, /*nb_lshift*/
2065 0, /*nb_rshift*/
2066 (binaryfunc)set_and, /*nb_and*/
2067 (binaryfunc)set_xor, /*nb_xor*/
2068 (binaryfunc)set_or, /*nb_or*/
2069};
2070
2071PyDoc_STRVAR(frozenset_doc,
2072"frozenset(iterable) --> frozenset object\n\
2073\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002074Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002075
2076PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002077 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002078 "frozenset", /* tp_name */
2079 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002080 0, /* tp_itemsize */
2081 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002082 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002083 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002084 0, /* tp_getattr */
2085 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002086 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002087 (reprfunc)set_repr, /* tp_repr */
2088 &frozenset_as_number, /* tp_as_number */
2089 &set_as_sequence, /* tp_as_sequence */
2090 0, /* tp_as_mapping */
2091 frozenset_hash, /* tp_hash */
2092 0, /* tp_call */
2093 0, /* tp_str */
2094 PyObject_GenericGetAttr, /* tp_getattro */
2095 0, /* tp_setattro */
2096 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002097 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002098 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002099 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002100 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002101 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002102 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002103 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002104 (getiterfunc)set_iter, /* tp_iter */
2105 0, /* tp_iternext */
2106 frozenset_methods, /* tp_methods */
2107 0, /* tp_members */
2108 0, /* tp_getset */
2109 0, /* tp_base */
2110 0, /* tp_dict */
2111 0, /* tp_descr_get */
2112 0, /* tp_descr_set */
2113 0, /* tp_dictoffset */
2114 0, /* tp_init */
2115 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002116 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002117 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002118};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002119
2120
2121/***** C API functions *************************************************/
2122
2123PyObject *
2124PySet_New(PyObject *iterable)
2125{
2126 return make_new_set(&PySet_Type, iterable);
2127}
2128
2129PyObject *
2130PyFrozenSet_New(PyObject *iterable)
2131{
Christian Heimesfd66e512008-01-29 12:18:50 +00002132 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002133}
2134
Neal Norwitz8c49c822006-03-04 18:41:19 +00002135Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002136PySet_Size(PyObject *anyset)
2137{
2138 if (!PyAnySet_Check(anyset)) {
2139 PyErr_BadInternalCall();
2140 return -1;
2141 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002142 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002143}
2144
2145int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002146PySet_Clear(PyObject *set)
2147{
Christian Heimesfd66e512008-01-29 12:18:50 +00002148 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002149 PyErr_BadInternalCall();
2150 return -1;
2151 }
2152 return set_clear_internal((PySetObject *)set);
2153}
2154
2155int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002156PySet_Contains(PyObject *anyset, PyObject *key)
2157{
2158 if (!PyAnySet_Check(anyset)) {
2159 PyErr_BadInternalCall();
2160 return -1;
2161 }
2162 return set_contains_key((PySetObject *)anyset, key);
2163}
2164
2165int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002166PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002167{
Christian Heimesfd66e512008-01-29 12:18:50 +00002168 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002169 PyErr_BadInternalCall();
2170 return -1;
2171 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002172 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002173}
2174
2175int
Christian Heimesfd66e512008-01-29 12:18:50 +00002176PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002177{
Christian Heimes15ebc882008-02-04 18:48:49 +00002178 if (!PySet_Check(anyset) &&
2179 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002180 PyErr_BadInternalCall();
2181 return -1;
2182 }
Christian Heimesfd66e512008-01-29 12:18:50 +00002183 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002184}
2185
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002186int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002187_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2188{
2189 setentry *entry;
2190
2191 if (!PyAnySet_Check(set)) {
2192 PyErr_BadInternalCall();
2193 return -1;
2194 }
2195 if (set_next((PySetObject *)set, pos, &entry) == 0)
2196 return 0;
2197 *key = entry->key;
2198 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002199 return 1;
2200}
2201
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002202PyObject *
2203PySet_Pop(PyObject *set)
2204{
Christian Heimesfd66e512008-01-29 12:18:50 +00002205 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002206 PyErr_BadInternalCall();
2207 return NULL;
2208 }
2209 return set_pop((PySetObject *)set);
2210}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002211
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002212int
2213_PySet_Update(PyObject *set, PyObject *iterable)
2214{
Christian Heimesfd66e512008-01-29 12:18:50 +00002215 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002216 PyErr_BadInternalCall();
2217 return -1;
2218 }
2219 return set_update_internal((PySetObject *)set, iterable);
2220}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002221
2222#ifdef Py_DEBUG
2223
2224/* Test code to be called with any three element set.
2225 Returns True and original set is restored. */
2226
2227#define assertRaises(call_return_value, exception) \
2228 do { \
2229 assert(call_return_value); \
2230 assert(PyErr_ExceptionMatches(exception)); \
2231 PyErr_Clear(); \
2232 } while(0)
2233
2234static PyObject *
2235test_c_api(PySetObject *so)
2236{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002237 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002238 char *s;
2239 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002240 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002241 PyObject *ob = (PyObject *)so;
Christian Heimesdb967892008-01-31 01:08:32 +00002242 long hash;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002243
2244 /* Verify preconditions and exercise type/size checks */
2245 assert(PyAnySet_Check(ob));
2246 assert(PyAnySet_CheckExact(ob));
2247 assert(!PyFrozenSet_CheckExact(ob));
2248 assert(PySet_Size(ob) == 3);
2249 assert(PySet_GET_SIZE(ob) == 3);
2250
2251 /* Raise TypeError for non-iterable constructor arguments */
2252 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2253 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2254
2255 /* Raise TypeError for unhashable key */
2256 dup = PySet_New(ob);
2257 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2258 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2259 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2260
2261 /* Exercise successful pop, contains, add, and discard */
2262 elem = PySet_Pop(ob);
2263 assert(PySet_Contains(ob, elem) == 0);
2264 assert(PySet_GET_SIZE(ob) == 2);
2265 assert(PySet_Add(ob, elem) == 0);
2266 assert(PySet_Contains(ob, elem) == 1);
2267 assert(PySet_GET_SIZE(ob) == 3);
2268 assert(PySet_Discard(ob, elem) == 1);
2269 assert(PySet_GET_SIZE(ob) == 2);
2270 assert(PySet_Discard(ob, elem) == 0);
2271 assert(PySet_GET_SIZE(ob) == 2);
2272
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002273 /* Exercise clear */
2274 dup2 = PySet_New(dup);
2275 assert(PySet_Clear(dup2) == 0);
2276 assert(PySet_Size(dup2) == 0);
2277 Py_DECREF(dup2);
2278
2279 /* Raise SystemError on clear or update of frozen set */
2280 f = PyFrozenSet_New(dup);
2281 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2282 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Christian Heimes15ebc882008-02-04 18:48:49 +00002283 assert(PySet_Add(f, elem) == 0);
2284 Py_INCREF(f);
2285 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2286 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002287 Py_DECREF(f);
2288
2289 /* Exercise direct iteration */
2290 i = 0, count = 0;
Christian Heimesdb967892008-01-31 01:08:32 +00002291 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
Amaury Forgeot d'Arc39599dc2007-11-22 02:48:12 +00002292 s = PyUnicode_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002293 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2294 count++;
2295 }
2296 assert(count == 3);
2297
2298 /* Exercise updates */
2299 dup2 = PySet_New(NULL);
2300 assert(_PySet_Update(dup2, dup) == 0);
2301 assert(PySet_Size(dup2) == 3);
2302 assert(_PySet_Update(dup2, dup) == 0);
2303 assert(PySet_Size(dup2) == 3);
2304 Py_DECREF(dup2);
2305
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002306 /* Raise SystemError when self argument is not a set or frozenset. */
2307 t = PyTuple_New(0);
2308 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2309 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2310 Py_DECREF(t);
2311
2312 /* Raise SystemError when self argument is not a set. */
2313 f = PyFrozenSet_New(dup);
2314 assert(PySet_Size(f) == 3);
2315 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002316 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2317 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2318 Py_DECREF(f);
2319
2320 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002321 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2322 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002323 assert(PySet_GET_SIZE(ob) == 0);
2324 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2325
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002326 /* Restore the set from the copy using the PyNumber API */
2327 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2328 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002329
2330 /* Verify constructors accept NULL arguments */
2331 f = PySet_New(NULL);
2332 assert(f != NULL);
2333 assert(PySet_GET_SIZE(f) == 0);
2334 Py_DECREF(f);
2335 f = PyFrozenSet_New(NULL);
2336 assert(f != NULL);
2337 assert(PyFrozenSet_CheckExact(f));
2338 assert(PySet_GET_SIZE(f) == 0);
2339 Py_DECREF(f);
2340
2341 Py_DECREF(elem);
2342 Py_DECREF(dup);
2343 Py_RETURN_TRUE;
2344}
2345
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002346#undef assertRaises
2347
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002348#endif