blob: d97dc2874b72db299daa6c4132810e2402dc9fc3 [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 */
55#define MAXFREESETS 80
56static PySetObject *free_sets[MAXFREESETS];
57static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000058
Christian Heimes0ded5b52007-12-10 15:50:56 +000059
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060/*
61The basic lookup function used by all operations.
62This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
63Open addressing is preferred over chaining since the link overhead for
64chaining would be substantial (100% with typical malloc overhead).
65
66The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000067probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000068
69All arithmetic on hash should ignore overflow.
70
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000071Unlike the dictionary implementation, the lookkey functions can return
72NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073*/
74
75static setentry *
76set_lookkey(PySetObject *so, PyObject *key, register long hash)
77{
Martin v. Löwis18e16552006-02-15 17:27:45 +000078 register Py_ssize_t i;
79 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000080 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000081 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000082 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000083 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000084 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000085 PyObject *startkey;
86
87 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000088 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000089 if (entry->key == NULL || entry->key == key)
90 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000091
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000092 if (entry->key == dummy)
93 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000094 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000095 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000096 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000097 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
98 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000099 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000100 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000102 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000103 }
104 else {
105 /* The compare did major nasty stuff to the
106 * set: start over.
107 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000108 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000109 }
110 }
111 freeslot = NULL;
112 }
113
114 /* In the loop, key == dummy is by far (factor of 100s) the
115 least likely outcome, so test for that last. */
116 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
117 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000118 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000119 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000121 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 break;
123 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000124 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000125 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000126 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000127 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000128 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
129 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000130 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000131 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000132 if (cmp > 0)
133 break;
134 }
135 else {
136 /* The compare did major nasty stuff to the
137 * set: start over.
138 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000139 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000140 }
141 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000142 else if (entry->key == dummy && freeslot == NULL)
143 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000144 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000145 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146}
147
148/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000149 * Hacked up version of set_lookkey which can assume keys are always unicode;
150 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000151 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152 */
153static setentry *
Christian Heimes0ded5b52007-12-10 15:50:56 +0000154set_lookkey_unicode(PySetObject *so, PyObject *key, register long hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000155{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156 register Py_ssize_t i;
157 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000159 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000160 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000161 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000162
Christian Heimes0ded5b52007-12-10 15:50:56 +0000163 /* Make sure this function doesn't have to handle non-unicode keys,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000164 including subclasses of str; e.g., one reason to subclass
165 strings is to override __eq__, and for speed we don't cater to
166 that here. */
Christian Heimes0ded5b52007-12-10 15:50:56 +0000167 if (!PyUnicode_CheckExact(key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000168 so->lookup = set_lookkey;
169 return set_lookkey(so, key, hash);
170 }
171 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000172 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000173 if (entry->key == NULL || entry->key == key)
174 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000175 if (entry->key == dummy)
176 freeslot = entry;
177 else {
Christian Heimes0ded5b52007-12-10 15:50:56 +0000178 if (entry->hash == hash && unicode_eq(entry->key, key))
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000179 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000180 freeslot = NULL;
181 }
182
183 /* In the loop, key == dummy is by far (factor of 100s) the
184 least likely outcome, so test for that last. */
185 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
186 i = (i << 2) + i + perturb + 1;
187 entry = &table[i & mask];
188 if (entry->key == NULL)
189 return freeslot == NULL ? entry : freeslot;
190 if (entry->key == key
191 || (entry->hash == hash
192 && entry->key != dummy
Christian Heimes0ded5b52007-12-10 15:50:56 +0000193 && unicode_eq(entry->key, key)))
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000194 return entry;
195 if (entry->key == dummy && freeslot == NULL)
196 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000197 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000198 assert(0); /* NOT REACHED */
199 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000200}
201
202/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000203Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000205Eats a reference to key.
206*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000207static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208set_insert_key(register PySetObject *so, PyObject *key, long hash)
209{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000210 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
212
213 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000214 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000215 if (entry == NULL)
216 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000217 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000218 /* UNUSED */
219 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220 entry->key = key;
221 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000224 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000225 entry->key = key;
226 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000227 so->used++;
228 Py_DECREF(dummy);
229 } else {
230 /* ACTIVE */
231 Py_DECREF(key);
232 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000233 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234}
235
236/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000237Internal routine used by set_table_resize() to insert an item which is
238known to be absent from the set. This routine also assumes that
239the set contains no deleted entries. Besides the performance benefit,
240using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
241Note that no refcounts are changed by this routine; if needed, the caller
242is responsible for incref'ing `key`.
243*/
244static void
245set_insert_clean(register PySetObject *so, PyObject *key, long hash)
246{
247 register size_t i;
248 register size_t perturb;
249 register size_t mask = (size_t)so->mask;
250 setentry *table = so->table;
251 register setentry *entry;
252
253 i = hash & mask;
254 entry = &table[i];
255 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
256 i = (i << 2) + i + perturb + 1;
257 entry = &table[i & mask];
258 }
259 so->fill++;
260 entry->key = key;
261 entry->hash = hash;
262 so->used++;
263}
264
265/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000267keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000268actually be smaller than the old one.
269*/
270static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000274 setentry *oldtable, *newtable, *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000276 int is_oldtable_malloced;
277 setentry small_copy[PySet_MINSIZE];
278
279 assert(minused >= 0);
280
281 /* Find the smallest table size > minused. */
282 for (newsize = PySet_MINSIZE;
283 newsize <= minused && newsize > 0;
284 newsize <<= 1)
285 ;
286 if (newsize <= 0) {
287 PyErr_NoMemory();
288 return -1;
289 }
290
291 /* Get space for a new table. */
292 oldtable = so->table;
293 assert(oldtable != NULL);
294 is_oldtable_malloced = oldtable != so->smalltable;
295
296 if (newsize == PySet_MINSIZE) {
297 /* A large table is shrinking, or we can't get any smaller. */
298 newtable = so->smalltable;
299 if (newtable == oldtable) {
300 if (so->fill == so->used) {
301 /* No dummies, so no point doing anything. */
302 return 0;
303 }
304 /* We're not going to resize it, but rebuild the
305 table anyway to purge old dummy entries.
306 Subtle: This is *necessary* if fill==size,
307 as set_lookkey needs at least one virgin slot to
308 terminate failing searches. If fill < size, it's
309 merely desirable, as dummies slow searches. */
310 assert(so->fill > so->used);
311 memcpy(small_copy, oldtable, sizeof(small_copy));
312 oldtable = small_copy;
313 }
314 }
315 else {
316 newtable = PyMem_NEW(setentry, newsize);
317 if (newtable == NULL) {
318 PyErr_NoMemory();
319 return -1;
320 }
321 }
322
323 /* Make the set empty, using the new table. */
324 assert(newtable != oldtable);
325 so->table = newtable;
326 so->mask = newsize - 1;
327 memset(newtable, 0, sizeof(setentry) * newsize);
328 so->used = 0;
329 i = so->fill;
330 so->fill = 0;
331
332 /* Copy the data over; this is refcount-neutral for active entries;
333 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000334 for (entry = oldtable; i > 0; entry++) {
335 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000336 /* UNUSED */
337 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000338 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000339 /* DUMMY */
340 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000341 assert(entry->key == dummy);
342 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000343 } else {
344 /* ACTIVE */
345 --i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000346 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347 }
348 }
349
350 if (is_oldtable_malloced)
351 PyMem_DEL(oldtable);
352 return 0;
353}
354
Raymond Hettingerc991db22005-08-11 07:58:45 +0000355/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
356
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000357static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000358set_add_entry(register PySetObject *so, setentry *entry)
359{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000360 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361
362 assert(so->fill <= so->mask); /* at least one empty slot */
363 n_used = so->used;
364 Py_INCREF(entry->key);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000365 if (set_insert_key(so, entry->key, entry->hash) == -1) {
366 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000367 return -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000368 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000369 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
370 return 0;
371 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
372}
373
374static int
375set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376{
377 register long hash;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000378 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000379
Christian Heimes0ded5b52007-12-10 15:50:56 +0000380 if (!PyUnicode_CheckExact(key) ||
381 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382 hash = PyObject_Hash(key);
383 if (hash == -1)
384 return -1;
385 }
386 assert(so->fill <= so->mask); /* at least one empty slot */
387 n_used = so->used;
388 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000389 if (set_insert_key(so, key, hash) == -1) {
390 Py_DECREF(key);
391 return -1;
392 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000393 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
394 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000395 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000396}
397
398#define DISCARD_NOTFOUND 0
399#define DISCARD_FOUND 1
400
401static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000402set_discard_entry(PySetObject *so, setentry *oldentry)
403{ register setentry *entry;
404 PyObject *old_key;
405
406 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000407 if (entry == NULL)
408 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000409 if (entry->key == NULL || entry->key == dummy)
410 return DISCARD_NOTFOUND;
411 old_key = entry->key;
412 Py_INCREF(dummy);
413 entry->key = dummy;
414 so->used--;
415 Py_DECREF(old_key);
416 return DISCARD_FOUND;
417}
418
419static int
420set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421{
422 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000423 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424 PyObject *old_key;
425
426 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000427
428 if (!PyUnicode_CheckExact(key) ||
429 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000430 hash = PyObject_Hash(key);
431 if (hash == -1)
432 return -1;
433 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000434 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000435 if (entry == NULL)
436 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000437 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000438 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000439 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000440 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000441 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000442 so->used--;
443 Py_DECREF(old_key);
444 return DISCARD_FOUND;
445}
446
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000447static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448set_clear_internal(PySetObject *so)
449{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000450 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451 int table_is_malloced;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000452 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 setentry small_copy[PySet_MINSIZE];
454#ifdef Py_DEBUG
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000455 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000457
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000458 n = so->mask + 1;
459 i = 0;
460#endif
461
462 table = so->table;
463 assert(table != NULL);
464 table_is_malloced = table != so->smalltable;
465
466 /* This is delicate. During the process of clearing the set,
467 * decrefs can cause the set to mutate. To avoid fatal confusion
468 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000469 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470 * clearing.
471 */
472 fill = so->fill;
473 if (table_is_malloced)
474 EMPTY_TO_MINSIZE(so);
475
476 else if (fill > 0) {
477 /* It's a small table with something that needs to be cleared.
478 * Afraid the only safe way is to copy the set entries into
479 * another small table first.
480 */
481 memcpy(small_copy, table, sizeof(small_copy));
482 table = small_copy;
483 EMPTY_TO_MINSIZE(so);
484 }
485 /* else it's a small table that's already empty */
486
487 /* Now we can finally clear things. If C had refcounts, we could
488 * assert that the refcount on table is 1 now, i.e. that this function
489 * has unique access to it, so decref side-effects can't alter it.
490 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000491 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000492#ifdef Py_DEBUG
493 assert(i < n);
494 ++i;
495#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000496 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000497 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000498 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499 }
500#ifdef Py_DEBUG
501 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503#endif
504 }
505
506 if (table_is_malloced)
507 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000508 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509}
510
511/*
512 * Iterate over a set table. Use like so:
513 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000514 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000515 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000516 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000517 * while (set_next(yourset, &pos, &entry)) {
518 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519 * }
520 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000521 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522 * mutates the table.
523 */
524static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000525set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000526{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000527 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000529 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000530
531 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000532 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000533 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000534 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000535 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000536 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000537 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000538 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000539 if (i > mask)
540 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000541 assert(table[i].key != NULL);
542 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543 return 1;
544}
545
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000546static void
547set_dealloc(PySetObject *so)
548{
549 register setentry *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000550 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000551 PyObject_GC_UnTrack(so);
552 Py_TRASHCAN_SAFE_BEGIN(so)
553 if (so->weakreflist != NULL)
554 PyObject_ClearWeakRefs((PyObject *) so);
555
556 for (entry = so->table; fill > 0; entry++) {
557 if (entry->key) {
558 --fill;
559 Py_DECREF(entry->key);
560 }
561 }
562 if (so->table != so->smalltable)
563 PyMem_DEL(so->table);
564 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
565 free_sets[num_free_sets++] = so;
566 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000567 Py_TYPE(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568 Py_TRASHCAN_SAFE_END(so)
569}
570
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000571static PyObject *
572set_repr(PySetObject *so)
573{
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000574 PyObject *keys, *result=NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000575 Py_UNICODE *u;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000576 int status = Py_ReprEnter((PyObject*)so);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000577 PyObject *listrepr;
578 Py_ssize_t newsize;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000579
580 if (status != 0) {
581 if (status < 0)
582 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000583 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000584 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000585
Georg Brandlc4996ba2006-08-28 19:37:11 +0000586 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000587 if (!so->used) {
588 Py_ReprLeave((PyObject*)so);
Christian Heimes90aa7642007-12-19 02:45:37 +0000589 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000590 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000591
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000592 keys = PySequence_List((PyObject *)so);
593 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000594 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000595
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000596 listrepr = PyObject_Repr(keys);
597 Py_DECREF(keys);
598 if (listrepr == NULL) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000599 Py_DECREF(keys);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000600 goto done;
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000601 }
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000602 newsize = PyUnicode_GET_SIZE(listrepr);
603 result = PyUnicode_FromUnicode(NULL, newsize);
604 if (result) {
605 u = PyUnicode_AS_UNICODE(result);
606 *u++ = '{';
607 /* Omit the brackets from the listrepr */
608 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
609 PyUnicode_GET_SIZE(listrepr)-2);
610 u += newsize-2;
611 *u++ = '}';
612 }
613 Py_DECREF(listrepr);
Christian Heimes90aa7642007-12-19 02:45:37 +0000614 if (Py_TYPE(so) != &PySet_Type) {
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000615 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000616 Py_TYPE(so)->tp_name,
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000617 result);
618 Py_DECREF(result);
619 result = tmp;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000620 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000621done:
622 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000623 return result;
624}
625
Martin v. Löwis18e16552006-02-15 17:27:45 +0000626static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000627set_len(PyObject *so)
628{
629 return ((PySetObject *)so)->used;
630}
631
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000632static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000633set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000634{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000635 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000636 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000637 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000638
639 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000640 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000641
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000642 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000643 if (other == so || other->used == 0)
644 /* a.update(a) or a.update({}); nothing to do */
645 return 0;
646 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000647 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648 * that there will be no (or few) overlapping keys.
649 */
650 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
651 if (set_table_resize(so, (so->used + other->used)*2) != 0)
652 return -1;
653 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000654 for (i = 0; i <= other->mask; i++) {
655 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000656 if (entry->key != NULL &&
657 entry->key != dummy) {
658 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000659 if (set_insert_key(so, entry->key, entry->hash) == -1) {
660 Py_DECREF(entry->key);
661 return -1;
662 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000663 }
664 }
665 return 0;
666}
667
668static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000669set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670{
671 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000672 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673
Christian Heimes0ded5b52007-12-10 15:50:56 +0000674 if (!PyUnicode_CheckExact(key) ||
675 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000676 hash = PyObject_Hash(key);
677 if (hash == -1)
678 return -1;
679 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000680 entry = (so->lookup)(so, key, hash);
681 if (entry == NULL)
682 return -1;
683 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000684 return key != NULL && key != dummy;
685}
686
Raymond Hettingerc991db22005-08-11 07:58:45 +0000687static int
688set_contains_entry(PySetObject *so, setentry *entry)
689{
690 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000691 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000692
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000693 lu_entry = (so->lookup)(so, entry->key, entry->hash);
694 if (lu_entry == NULL)
695 return -1;
696 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000697 return key != NULL && key != dummy;
698}
699
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000700static PyObject *
701set_pop(PySetObject *so)
702{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000703 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000704 register setentry *entry;
705 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000706
707 assert (PyAnySet_Check(so));
708 if (so->used == 0) {
709 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
710 return NULL;
711 }
712
713 /* Set entry to "the first" unused or dummy set entry. We abuse
714 * the hash field of slot 0 to hold a search finger:
715 * If slot 0 has a value, use slot 0.
716 * Else slot 0 is being used to hold a search finger,
717 * and we use its hash value as the first index to look.
718 */
719 entry = &so->table[0];
720 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000721 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000722 /* The hash field may be a real hash value, or it may be a
723 * legit search finger, or it may be a once-legit search
724 * finger that's out of bounds now because it wrapped around
725 * or the table shrunk -- simply make sure it's in bounds now.
726 */
727 if (i > so->mask || i < 1)
728 i = 1; /* skip slot 0 */
729 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
730 i++;
731 if (i > so->mask)
732 i = 1;
733 }
734 }
735 key = entry->key;
736 Py_INCREF(dummy);
737 entry->key = dummy;
738 so->used--;
739 so->table[0].hash = i + 1; /* next place to start */
740 return key;
741}
742
743PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
744
745static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000746set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000747{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000748 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000749 setentry *entry;
750
751 while (set_next(so, &pos, &entry))
752 Py_VISIT(entry->key);
753 return 0;
754}
755
756static long
757frozenset_hash(PyObject *self)
758{
759 PySetObject *so = (PySetObject *)self;
760 long h, hash = 1927868237L;
761 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000762 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000763
764 if (so->hash != -1)
765 return so->hash;
766
767 hash *= PySet_GET_SIZE(self) + 1;
768 while (set_next(so, &pos, &entry)) {
769 /* Work to increase the bit dispersion for closely spaced hash
770 values. The is important because some use cases have many
771 combinations of a small number of elements with nearby
772 hashes so that many distinct combinations collapse to only
773 a handful of distinct hash values. */
774 h = entry->hash;
775 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
776 }
777 hash = hash * 69069L + 907133923L;
778 if (hash == -1)
779 hash = 590923713L;
780 so->hash = hash;
781 return hash;
782}
783
Raymond Hettingera9d99362005-08-05 00:01:15 +0000784/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000785
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000786typedef struct {
787 PyObject_HEAD
788 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000789 Py_ssize_t si_used;
790 Py_ssize_t si_pos;
791 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792} setiterobject;
793
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794static void
795setiter_dealloc(setiterobject *si)
796{
797 Py_XDECREF(si->si_set);
798 PyObject_Del(si);
799}
800
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000801static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802setiter_len(setiterobject *si)
803{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000804 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000806 len = si->len;
Christian Heimes217cfd12007-12-02 14:31:20 +0000807 return PyLong_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000808}
809
Armin Rigof5b3e362006-02-11 21:32:43 +0000810PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000811
812static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000813 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000814 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815};
816
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000817static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818{
819 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000820 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000821 register setentry *entry;
822 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000823
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000824 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000826 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000827
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000828 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829 PyErr_SetString(PyExc_RuntimeError,
830 "Set changed size during iteration");
831 si->si_used = -1; /* Make this state sticky */
832 return NULL;
833 }
834
835 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000836 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000837 entry = so->table;
838 mask = so->mask;
839 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000840 i++;
841 si->si_pos = i+1;
842 if (i > mask)
843 goto fail;
844 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000845 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000846 Py_INCREF(key);
847 return key;
848
849fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000850 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000851 si->si_set = NULL;
852 return NULL;
853}
854
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000855PyTypeObject PySetIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000856 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +0000857 "set_iterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000858 sizeof(setiterobject), /* tp_basicsize */
859 0, /* tp_itemsize */
860 /* methods */
861 (destructor)setiter_dealloc, /* tp_dealloc */
862 0, /* tp_print */
863 0, /* tp_getattr */
864 0, /* tp_setattr */
865 0, /* tp_compare */
866 0, /* tp_repr */
867 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000868 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000869 0, /* tp_as_mapping */
870 0, /* tp_hash */
871 0, /* tp_call */
872 0, /* tp_str */
873 PyObject_GenericGetAttr, /* tp_getattro */
874 0, /* tp_setattro */
875 0, /* tp_as_buffer */
876 Py_TPFLAGS_DEFAULT, /* tp_flags */
877 0, /* tp_doc */
878 0, /* tp_traverse */
879 0, /* tp_clear */
880 0, /* tp_richcompare */
881 0, /* tp_weaklistoffset */
882 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000883 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000884 setiter_methods, /* tp_methods */
885 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000886};
887
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000888static PyObject *
889set_iter(PySetObject *so)
890{
891 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
892 if (si == NULL)
893 return NULL;
894 Py_INCREF(so);
895 si->si_set = so;
896 si->si_used = so->used;
897 si->si_pos = 0;
898 si->len = so->used;
899 return (PyObject *)si;
900}
901
Raymond Hettingerd7946662005-08-01 21:39:29 +0000902static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000903set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000904{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000905 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000906
Christian Heimesaf98da12008-01-27 15:18:18 +0000907 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000908 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000909
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000910 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000911 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000912 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000913 long hash;
914 Py_ssize_t dictsize = PyDict_Size(other);
915
916 /* Do one big resize at the start, rather than
917 * incrementally resizing as we insert new keys. Expect
918 * that there will be no (or few) overlapping keys.
919 */
920 if (dictsize == -1)
921 return -1;
922 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
923 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
924 return -1;
925 }
926 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
927 setentry an_entry;
928
929 an_entry.hash = hash;
930 an_entry.key = key;
931 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000932 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000933 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000934 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000935 }
936
Raymond Hettingera38123e2003-11-24 22:18:49 +0000937 it = PyObject_GetIter(other);
938 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000939 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000940
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000941 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000942 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000943 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000944 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000945 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000946 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000947 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000948 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000949 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000950 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000951 return -1;
952 return 0;
953}
954
955static PyObject *
956set_update(PySetObject *so, PyObject *other)
957{
958 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000959 return NULL;
960 Py_RETURN_NONE;
961}
962
963PyDoc_STRVAR(update_doc,
964"Update a set with the union of itself and another.");
965
966static PyObject *
967make_new_set(PyTypeObject *type, PyObject *iterable)
968{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000969 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000970
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000971 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000972 dummy = PyUnicode_FromString("<dummy key>");
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000973 if (dummy == NULL)
974 return NULL;
975 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000976
977 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000978 if (num_free_sets &&
979 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
980 so = free_sets[--num_free_sets];
981 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimes90aa7642007-12-19 02:45:37 +0000982 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000983 _Py_NewReference((PyObject *)so);
984 EMPTY_TO_MINSIZE(so);
985 PyObject_GC_Track(so);
986 } else {
987 so = (PySetObject *)type->tp_alloc(type, 0);
988 if (so == NULL)
989 return NULL;
990 /* tp_alloc has already zeroed the structure */
991 assert(so->table == NULL && so->fill == 0 && so->used == 0);
992 INIT_NONZERO_SET_SLOTS(so);
993 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000994
Christian Heimes0ded5b52007-12-10 15:50:56 +0000995 so->lookup = set_lookkey_unicode;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000996 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000997
Raymond Hettingera38123e2003-11-24 22:18:49 +0000998 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000999 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001000 Py_DECREF(so);
1001 return NULL;
1002 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001003 }
1004
Raymond Hettingera690a992003-11-16 16:17:49 +00001005 return (PyObject *)so;
1006}
1007
Raymond Hettingerd7946662005-08-01 21:39:29 +00001008/* The empty frozenset is a singleton */
1009static PyObject *emptyfrozenset = NULL;
1010
Raymond Hettingera690a992003-11-16 16:17:49 +00001011static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001012frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001013{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001014 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001015
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001016 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001017 return NULL;
1018
Raymond Hettingera690a992003-11-16 16:17:49 +00001019 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1020 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001021
1022 if (type != &PyFrozenSet_Type)
1023 return make_new_set(type, iterable);
1024
1025 if (iterable != NULL) {
1026 /* frozenset(f) is idempotent */
1027 if (PyFrozenSet_CheckExact(iterable)) {
1028 Py_INCREF(iterable);
1029 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001030 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001031 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001032 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001033 return result;
1034 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001035 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001036 /* The empty frozenset is a singleton */
1037 if (emptyfrozenset == NULL)
1038 emptyfrozenset = make_new_set(type, NULL);
1039 Py_XINCREF(emptyfrozenset);
1040 return emptyfrozenset;
1041}
1042
1043void
1044PySet_Fini(void)
1045{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001046 PySetObject *so;
1047
1048 while (num_free_sets) {
1049 num_free_sets--;
1050 so = free_sets[num_free_sets];
1051 PyObject_GC_Del(so);
1052 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001053 Py_CLEAR(dummy);
1054 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001055}
1056
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001057static PyObject *
1058set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1059{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001060 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001061 return NULL;
1062
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001063 return make_new_set(type, NULL);
1064}
1065
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001066/* set_swap_bodies() switches the contents of any two sets by moving their
1067 internal data pointers and, if needed, copying the internal smalltables.
1068 Semantically equivalent to:
1069
1070 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1071
1072 The function always succeeds and it leaves both objects in a stable state.
1073 Useful for creating temporary frozensets from sets for membership testing
1074 in __contains__(), discard(), and remove(). Also useful for operations
1075 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001076 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001077*/
1078
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001079static void
1080set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001081{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001082 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001083 setentry *u;
1084 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1085 setentry tab[PySet_MINSIZE];
1086 long h;
1087
1088 t = a->fill; a->fill = b->fill; b->fill = t;
1089 t = a->used; a->used = b->used; b->used = t;
1090 t = a->mask; a->mask = b->mask; b->mask = t;
1091
1092 u = a->table;
1093 if (a->table == a->smalltable)
1094 u = b->smalltable;
1095 a->table = b->table;
1096 if (b->table == b->smalltable)
1097 a->table = a->smalltable;
1098 b->table = u;
1099
1100 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1101
1102 if (a->table == a->smalltable || b->table == b->smalltable) {
1103 memcpy(tab, a->smalltable, sizeof(tab));
1104 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1105 memcpy(b->smalltable, tab, sizeof(tab));
1106 }
1107
Christian Heimes90aa7642007-12-19 02:45:37 +00001108 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1109 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001110 h = a->hash; a->hash = b->hash; b->hash = h;
1111 } else {
1112 a->hash = -1;
1113 b->hash = -1;
1114 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001115}
1116
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001117static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001118set_copy(PySetObject *so)
1119{
Christian Heimes90aa7642007-12-19 02:45:37 +00001120 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001121}
1122
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001123static PyObject *
1124frozenset_copy(PySetObject *so)
1125{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001126 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001127 Py_INCREF(so);
1128 return (PyObject *)so;
1129 }
1130 return set_copy(so);
1131}
1132
Raymond Hettingera690a992003-11-16 16:17:49 +00001133PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1134
1135static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001136set_clear(PySetObject *so)
1137{
1138 set_clear_internal(so);
1139 Py_RETURN_NONE;
1140}
1141
1142PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1143
1144static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001145set_union(PySetObject *so, PyObject *other)
1146{
1147 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001148
1149 result = (PySetObject *)set_copy(so);
1150 if (result == NULL)
1151 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001152 if ((PyObject *)so == other)
1153 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001154 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001155 Py_DECREF(result);
1156 return NULL;
1157 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001158 return (PyObject *)result;
1159}
1160
1161PyDoc_STRVAR(union_doc,
1162 "Return the union of two sets as a new set.\n\
1163\n\
1164(i.e. all elements that are in either set.)");
1165
1166static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001167set_or(PySetObject *so, PyObject *other)
1168{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001169 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001170 Py_INCREF(Py_NotImplemented);
1171 return Py_NotImplemented;
1172 }
1173 return set_union(so, other);
1174}
1175
1176static PyObject *
1177set_ior(PySetObject *so, PyObject *other)
1178{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001179 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001180 Py_INCREF(Py_NotImplemented);
1181 return Py_NotImplemented;
1182 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001183 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001184 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001185 Py_INCREF(so);
1186 return (PyObject *)so;
1187}
1188
1189static PyObject *
1190set_intersection(PySetObject *so, PyObject *other)
1191{
1192 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001193 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001194
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001195 if ((PyObject *)so == other)
1196 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001197
Christian Heimes90aa7642007-12-19 02:45:37 +00001198 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001199 if (result == NULL)
1200 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001201
Christian Heimesaf98da12008-01-27 15:18:18 +00001202 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001203 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001204 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001205
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001206 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001207 tmp = (PyObject *)so;
1208 so = (PySetObject *)other;
1209 other = tmp;
1210 }
1211
Raymond Hettingerc991db22005-08-11 07:58:45 +00001212 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001213 int rv = set_contains_entry(so, entry);
1214 if (rv == -1) {
1215 Py_DECREF(result);
1216 return NULL;
1217 }
1218 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001219 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001220 Py_DECREF(result);
1221 return NULL;
1222 }
1223 }
1224 }
1225 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001226 }
1227
Raymond Hettingera690a992003-11-16 16:17:49 +00001228 it = PyObject_GetIter(other);
1229 if (it == NULL) {
1230 Py_DECREF(result);
1231 return NULL;
1232 }
1233
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001234 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001235 int rv;
1236 setentry entry;
1237 long hash = PyObject_Hash(key);
1238
1239 if (hash == -1) {
1240 Py_DECREF(it);
1241 Py_DECREF(result);
1242 Py_DECREF(key);
1243 return NULL;
1244 }
1245 entry.hash = hash;
1246 entry.key = key;
1247 rv = set_contains_entry(so, &entry);
1248 if (rv == -1) {
1249 Py_DECREF(it);
1250 Py_DECREF(result);
1251 Py_DECREF(key);
1252 return NULL;
1253 }
1254 if (rv) {
1255 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001256 Py_DECREF(it);
1257 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001258 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001259 return NULL;
1260 }
1261 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001262 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001263 }
1264 Py_DECREF(it);
1265 if (PyErr_Occurred()) {
1266 Py_DECREF(result);
1267 return NULL;
1268 }
1269 return (PyObject *)result;
1270}
1271
1272PyDoc_STRVAR(intersection_doc,
1273"Return the intersection of two sets as a new set.\n\
1274\n\
1275(i.e. all elements that are in both sets.)");
1276
1277static PyObject *
1278set_intersection_update(PySetObject *so, PyObject *other)
1279{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001280 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001281
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001282 tmp = set_intersection(so, other);
1283 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001284 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001285 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001286 Py_DECREF(tmp);
1287 Py_RETURN_NONE;
1288}
1289
1290PyDoc_STRVAR(intersection_update_doc,
1291"Update a set with the intersection of itself and another.");
1292
1293static PyObject *
1294set_and(PySetObject *so, PyObject *other)
1295{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001296 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001297 Py_INCREF(Py_NotImplemented);
1298 return Py_NotImplemented;
1299 }
1300 return set_intersection(so, other);
1301}
1302
1303static PyObject *
1304set_iand(PySetObject *so, PyObject *other)
1305{
1306 PyObject *result;
1307
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001308 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001309 Py_INCREF(Py_NotImplemented);
1310 return Py_NotImplemented;
1311 }
1312 result = set_intersection_update(so, other);
1313 if (result == NULL)
1314 return NULL;
1315 Py_DECREF(result);
1316 Py_INCREF(so);
1317 return (PyObject *)so;
1318}
1319
Guido van Rossum58da9312007-11-10 23:39:45 +00001320static PyObject *
1321set_isdisjoint(PySetObject *so, PyObject *other)
1322{
1323 PyObject *key, *it, *tmp;
1324
1325 if ((PyObject *)so == other) {
1326 if (PySet_GET_SIZE(so) == 0)
1327 Py_RETURN_TRUE;
1328 else
1329 Py_RETURN_FALSE;
1330 }
1331
1332 if (PyAnySet_CheckExact(other)) {
1333 Py_ssize_t pos = 0;
1334 setentry *entry;
1335
1336 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1337 tmp = (PyObject *)so;
1338 so = (PySetObject *)other;
1339 other = tmp;
1340 }
1341 while (set_next((PySetObject *)other, &pos, &entry)) {
1342 int rv = set_contains_entry(so, entry);
1343 if (rv == -1)
1344 return NULL;
1345 if (rv)
1346 Py_RETURN_FALSE;
1347 }
1348 Py_RETURN_TRUE;
1349 }
1350
1351 it = PyObject_GetIter(other);
1352 if (it == NULL)
1353 return NULL;
1354
1355 while ((key = PyIter_Next(it)) != NULL) {
1356 int rv;
1357 setentry entry;
Christian Heimes0ded5b52007-12-10 15:50:56 +00001358 long hash = PyObject_Hash(key);;
Guido van Rossum58da9312007-11-10 23:39:45 +00001359
1360 if (hash == -1) {
1361 Py_DECREF(key);
1362 Py_DECREF(it);
1363 return NULL;
1364 }
1365 entry.hash = hash;
1366 entry.key = key;
1367 rv = set_contains_entry(so, &entry);
1368 Py_DECREF(key);
1369 if (rv == -1) {
1370 Py_DECREF(it);
1371 return NULL;
1372 }
1373 if (rv) {
1374 Py_DECREF(it);
1375 Py_RETURN_FALSE;
1376 }
1377 }
1378 Py_DECREF(it);
1379 if (PyErr_Occurred())
1380 return NULL;
1381 Py_RETURN_TRUE;
1382}
1383
1384PyDoc_STRVAR(isdisjoint_doc,
1385"Return True if two sets have a null intersection.");
1386
Neal Norwitz6576bd82005-11-13 18:41:28 +00001387static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001388set_difference_update_internal(PySetObject *so, PyObject *other)
1389{
1390 if ((PyObject *)so == other)
1391 return set_clear_internal(so);
1392
Christian Heimesaf98da12008-01-27 15:18:18 +00001393 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001394 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001395 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001396
1397 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001398 if (set_discard_entry(so, entry) == -1)
1399 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001400 } else {
1401 PyObject *key, *it;
1402 it = PyObject_GetIter(other);
1403 if (it == NULL)
1404 return -1;
1405
1406 while ((key = PyIter_Next(it)) != NULL) {
1407 if (set_discard_key(so, key) == -1) {
1408 Py_DECREF(it);
1409 Py_DECREF(key);
1410 return -1;
1411 }
1412 Py_DECREF(key);
1413 }
1414 Py_DECREF(it);
1415 if (PyErr_Occurred())
1416 return -1;
1417 }
1418 /* If more than 1/5 are dummies, then resize them away. */
1419 if ((so->fill - so->used) * 5 < so->mask)
1420 return 0;
1421 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1422}
1423
Raymond Hettingera690a992003-11-16 16:17:49 +00001424static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001425set_difference_update(PySetObject *so, PyObject *other)
1426{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001427 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001428 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001429 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001430}
1431
1432PyDoc_STRVAR(difference_update_doc,
1433"Remove all elements of another set from this set.");
1434
1435static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001436set_difference(PySetObject *so, PyObject *other)
1437{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001438 PyObject *result;
1439 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001440 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001441
Christian Heimesaf98da12008-01-27 15:18:18 +00001442 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001443 result = set_copy(so);
1444 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001445 return NULL;
1446 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001447 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001448 Py_DECREF(result);
1449 return NULL;
1450 }
1451
Christian Heimes90aa7642007-12-19 02:45:37 +00001452 result = make_new_set(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001453 if (result == NULL)
1454 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001455
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001456 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001457 while (set_next(so, &pos, &entry)) {
1458 setentry entrycopy;
1459 entrycopy.hash = entry->hash;
1460 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001461 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001462 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1463 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001464 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001465 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001466 }
1467 }
1468 return result;
1469 }
1470
Raymond Hettingerc991db22005-08-11 07:58:45 +00001471 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001472 int rv = set_contains_entry((PySetObject *)other, entry);
1473 if (rv == -1) {
1474 Py_DECREF(result);
1475 return NULL;
1476 }
1477 if (!rv) {
1478 if (set_add_entry((PySetObject *)result, entry) == -1) {
1479 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001480 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001481 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001482 }
1483 }
1484 return result;
1485}
1486
1487PyDoc_STRVAR(difference_doc,
1488"Return the difference of two sets as a new set.\n\
1489\n\
1490(i.e. all elements that are in this set but not the other.)");
1491static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001492set_sub(PySetObject *so, PyObject *other)
1493{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001494 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001495 Py_INCREF(Py_NotImplemented);
1496 return Py_NotImplemented;
1497 }
1498 return set_difference(so, other);
1499}
1500
1501static PyObject *
1502set_isub(PySetObject *so, PyObject *other)
1503{
1504 PyObject *result;
1505
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001506 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001507 Py_INCREF(Py_NotImplemented);
1508 return Py_NotImplemented;
1509 }
1510 result = set_difference_update(so, other);
1511 if (result == NULL)
1512 return NULL;
1513 Py_DECREF(result);
1514 Py_INCREF(so);
1515 return (PyObject *)so;
1516}
1517
1518static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001519set_symmetric_difference_update(PySetObject *so, PyObject *other)
1520{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001521 PySetObject *otherset;
1522 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001523 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001524 setentry *entry;
1525
1526 if ((PyObject *)so == other)
1527 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001528
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001529 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001530 PyObject *value;
1531 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001532 long hash;
1533 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001534 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001535
Thomas Wouters89f507f2006-12-13 04:49:30 +00001536 an_entry.hash = hash;
1537 an_entry.key = key;
1538 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001539 if (rv == -1)
1540 return NULL;
1541 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001542 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001543 return NULL;
1544 }
1545 }
1546 Py_RETURN_NONE;
1547 }
1548
Christian Heimesaf98da12008-01-27 15:18:18 +00001549 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001550 Py_INCREF(other);
1551 otherset = (PySetObject *)other;
1552 } else {
Christian Heimes90aa7642007-12-19 02:45:37 +00001553 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001554 if (otherset == NULL)
1555 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001556 }
1557
Raymond Hettingerc991db22005-08-11 07:58:45 +00001558 while (set_next(otherset, &pos, &entry)) {
1559 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001560 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001561 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001562 return NULL;
1563 }
1564 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001565 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001566 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001567 return NULL;
1568 }
1569 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001570 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001571 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001572 Py_RETURN_NONE;
1573}
1574
1575PyDoc_STRVAR(symmetric_difference_update_doc,
1576"Update a set with the symmetric difference of itself and another.");
1577
1578static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001579set_symmetric_difference(PySetObject *so, PyObject *other)
1580{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001581 PyObject *rv;
1582 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001583
Christian Heimes90aa7642007-12-19 02:45:37 +00001584 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001585 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001586 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001587 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1588 if (rv == NULL)
1589 return NULL;
1590 Py_DECREF(rv);
1591 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001592}
1593
1594PyDoc_STRVAR(symmetric_difference_doc,
1595"Return the symmetric difference of two sets as a new set.\n\
1596\n\
1597(i.e. all elements that are in exactly one of the sets.)");
1598
1599static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001600set_xor(PySetObject *so, PyObject *other)
1601{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001602 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001603 Py_INCREF(Py_NotImplemented);
1604 return Py_NotImplemented;
1605 }
1606 return set_symmetric_difference(so, other);
1607}
1608
1609static PyObject *
1610set_ixor(PySetObject *so, PyObject *other)
1611{
1612 PyObject *result;
1613
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001614 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001615 Py_INCREF(Py_NotImplemented);
1616 return Py_NotImplemented;
1617 }
1618 result = set_symmetric_difference_update(so, other);
1619 if (result == NULL)
1620 return NULL;
1621 Py_DECREF(result);
1622 Py_INCREF(so);
1623 return (PyObject *)so;
1624}
1625
1626static PyObject *
1627set_issubset(PySetObject *so, PyObject *other)
1628{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001629 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001630 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001631
Christian Heimesaf98da12008-01-27 15:18:18 +00001632 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001633 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001634 tmp = make_new_set(&PySet_Type, other);
1635 if (tmp == NULL)
1636 return NULL;
1637 result = set_issubset(so, tmp);
1638 Py_DECREF(tmp);
1639 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001640 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001641 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001642 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001643
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001644 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001645 int rv = set_contains_entry((PySetObject *)other, entry);
1646 if (rv == -1)
1647 return NULL;
1648 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001649 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001650 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001651 Py_RETURN_TRUE;
1652}
1653
1654PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1655
1656static PyObject *
1657set_issuperset(PySetObject *so, PyObject *other)
1658{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001659 PyObject *tmp, *result;
1660
Christian Heimesaf98da12008-01-27 15:18:18 +00001661 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001662 tmp = make_new_set(&PySet_Type, other);
1663 if (tmp == NULL)
1664 return NULL;
1665 result = set_issuperset(so, tmp);
1666 Py_DECREF(tmp);
1667 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001668 }
1669 return set_issubset((PySetObject *)other, (PyObject *)so);
1670}
1671
1672PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1673
Raymond Hettingera690a992003-11-16 16:17:49 +00001674static PyObject *
1675set_richcompare(PySetObject *v, PyObject *w, int op)
1676{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001677 PyObject *r1, *r2;
1678
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001679 if(!PyAnySet_Check(w)) {
Guido van Rossum10ab4ae2007-08-23 23:57:24 +00001680 Py_INCREF(Py_NotImplemented);
1681 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001682 }
1683 switch (op) {
1684 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001685 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001686 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001687 if (v->hash != -1 &&
1688 ((PySetObject *)w)->hash != -1 &&
1689 v->hash != ((PySetObject *)w)->hash)
1690 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001691 return set_issubset(v, w);
1692 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001693 r1 = set_richcompare(v, w, Py_EQ);
1694 if (r1 == NULL)
1695 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001696 r2 = PyBool_FromLong(PyObject_Not(r1));
1697 Py_DECREF(r1);
1698 return r2;
1699 case Py_LE:
1700 return set_issubset(v, w);
1701 case Py_GE:
1702 return set_issuperset(v, w);
1703 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001704 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001705 Py_RETURN_FALSE;
1706 return set_issubset(v, w);
1707 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001708 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001709 Py_RETURN_FALSE;
1710 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001711 }
1712 Py_INCREF(Py_NotImplemented);
1713 return Py_NotImplemented;
1714}
1715
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001716static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001717set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001718{
1719 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1720 return -1;
1721}
1722
Raymond Hettingera690a992003-11-16 16:17:49 +00001723static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001724set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001725{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001726 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001727 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001728 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001729}
1730
1731PyDoc_STRVAR(add_doc,
1732"Add an element to a set.\n\
1733\n\
1734This has no effect if the element is already present.");
1735
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001736static int
1737set_contains(PySetObject *so, PyObject *key)
1738{
1739 PyObject *tmpkey;
1740 int rv;
1741
1742 rv = set_contains_key(so, key);
1743 if (rv == -1) {
1744 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1745 return -1;
1746 PyErr_Clear();
1747 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1748 if (tmpkey == NULL)
1749 return -1;
1750 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1751 rv = set_contains(so, tmpkey);
1752 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1753 Py_DECREF(tmpkey);
1754 }
1755 return rv;
1756}
1757
1758static PyObject *
1759set_direct_contains(PySetObject *so, PyObject *key)
1760{
1761 long result;
1762
1763 result = set_contains(so, key);
1764 if (result == -1)
1765 return NULL;
1766 return PyBool_FromLong(result);
1767}
1768
1769PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1770
Raymond Hettingera690a992003-11-16 16:17:49 +00001771static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001772set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001773{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001774 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001775 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001776
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001777 rv = set_discard_key(so, key);
1778 if (rv == -1) {
1779 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1780 return NULL;
1781 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001782 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1783 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001784 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001785 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001786 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001787 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001788 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001789 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001790 } else if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001791 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001792 return NULL;
1793 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001794 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001795}
1796
1797PyDoc_STRVAR(remove_doc,
1798"Remove an element from a set; it must be a member.\n\
1799\n\
1800If the element is not a member, raise a KeyError.");
1801
1802static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001803set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001804{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001805 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001806 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001807
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001808 rv = set_discard_key(so, key);
1809 if (rv == -1) {
1810 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1811 return NULL;
1812 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001813 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1814 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001815 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001816 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001817 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001818 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001819 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001820 return result;
1821 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001822 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001823}
1824
1825PyDoc_STRVAR(discard_doc,
1826"Remove an element from a set if it is a member.\n\
1827\n\
1828If the element is not a member, do nothing.");
1829
1830static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001831set_reduce(PySetObject *so)
1832{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001833 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001834
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001835 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001836 if (keys == NULL)
1837 goto done;
1838 args = PyTuple_Pack(1, keys);
1839 if (args == NULL)
1840 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001841 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1842 if (dict == NULL) {
1843 PyErr_Clear();
1844 dict = Py_None;
1845 Py_INCREF(dict);
1846 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001847 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001848done:
1849 Py_XDECREF(args);
1850 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001851 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001852 return result;
1853}
1854
1855PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1856
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001857static int
1858set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1859{
1860 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001861
1862 if (!PyAnySet_Check(self))
1863 return -1;
Christian Heimes90aa7642007-12-19 02:45:37 +00001864 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001865 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001866 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001867 self->hash = -1;
1868 if (iterable == NULL)
1869 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001870 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001871}
1872
Raymond Hettingera690a992003-11-16 16:17:49 +00001873static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001874 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001875 0, /* sq_concat */
1876 0, /* sq_repeat */
1877 0, /* sq_item */
1878 0, /* sq_slice */
1879 0, /* sq_ass_item */
1880 0, /* sq_ass_slice */
1881 (objobjproc)set_contains, /* sq_contains */
1882};
1883
1884/* set object ********************************************************/
1885
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001886#ifdef Py_DEBUG
1887static PyObject *test_c_api(PySetObject *so);
1888
1889PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1890All is well if assertions don't fail.");
1891#endif
1892
Raymond Hettingera690a992003-11-16 16:17:49 +00001893static PyMethodDef set_methods[] = {
1894 {"add", (PyCFunction)set_add, METH_O,
1895 add_doc},
1896 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1897 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001898 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001899 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001900 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1901 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001902 {"discard", (PyCFunction)set_discard, METH_O,
1903 discard_doc},
1904 {"difference", (PyCFunction)set_difference, METH_O,
1905 difference_doc},
1906 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1907 difference_update_doc},
1908 {"intersection",(PyCFunction)set_intersection, METH_O,
1909 intersection_doc},
1910 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1911 intersection_update_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00001912 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1913 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001914 {"issubset", (PyCFunction)set_issubset, METH_O,
1915 issubset_doc},
1916 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1917 issuperset_doc},
1918 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1919 pop_doc},
1920 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1921 reduce_doc},
1922 {"remove", (PyCFunction)set_remove, METH_O,
1923 remove_doc},
1924 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1925 symmetric_difference_doc},
1926 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1927 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001928#ifdef Py_DEBUG
1929 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1930 test_c_api_doc},
1931#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001932 {"union", (PyCFunction)set_union, METH_O,
1933 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001934 {"update", (PyCFunction)set_update, METH_O,
1935 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001936 {NULL, NULL} /* sentinel */
1937};
1938
1939static PyNumberMethods set_as_number = {
1940 0, /*nb_add*/
1941 (binaryfunc)set_sub, /*nb_subtract*/
1942 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001943 0, /*nb_remainder*/
1944 0, /*nb_divmod*/
1945 0, /*nb_power*/
1946 0, /*nb_negative*/
1947 0, /*nb_positive*/
1948 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001949 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001950 0, /*nb_invert*/
1951 0, /*nb_lshift*/
1952 0, /*nb_rshift*/
1953 (binaryfunc)set_and, /*nb_and*/
1954 (binaryfunc)set_xor, /*nb_xor*/
1955 (binaryfunc)set_or, /*nb_or*/
Neil Schemenauer16c70752007-09-21 20:19:23 +00001956 0, /*nb_reserved*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001957 0, /*nb_int*/
1958 0, /*nb_long*/
1959 0, /*nb_float*/
1960 0, /*nb_oct*/
1961 0, /*nb_hex*/
1962 0, /*nb_inplace_add*/
1963 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1964 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001965 0, /*nb_inplace_remainder*/
1966 0, /*nb_inplace_power*/
1967 0, /*nb_inplace_lshift*/
1968 0, /*nb_inplace_rshift*/
1969 (binaryfunc)set_iand, /*nb_inplace_and*/
1970 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1971 (binaryfunc)set_ior, /*nb_inplace_or*/
1972};
1973
1974PyDoc_STRVAR(set_doc,
1975"set(iterable) --> set object\n\
1976\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001977Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001978
1979PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001980 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001981 "set", /* tp_name */
1982 sizeof(PySetObject), /* tp_basicsize */
1983 0, /* tp_itemsize */
1984 /* methods */
1985 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001986 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00001987 0, /* tp_getattr */
1988 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001989 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001990 (reprfunc)set_repr, /* tp_repr */
1991 &set_as_number, /* tp_as_number */
1992 &set_as_sequence, /* tp_as_sequence */
1993 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001994 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001995 0, /* tp_call */
1996 0, /* tp_str */
1997 PyObject_GenericGetAttr, /* tp_getattro */
1998 0, /* tp_setattro */
1999 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002000 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002001 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002002 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002003 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002004 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002005 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002006 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002007 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002008 0, /* tp_iternext */
2009 set_methods, /* tp_methods */
2010 0, /* tp_members */
2011 0, /* tp_getset */
2012 0, /* tp_base */
2013 0, /* tp_dict */
2014 0, /* tp_descr_get */
2015 0, /* tp_descr_set */
2016 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002017 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002018 PyType_GenericAlloc, /* tp_alloc */
2019 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002020 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002021};
2022
2023/* frozenset object ********************************************************/
2024
2025
2026static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002027 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002028 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002029 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002030 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002031 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002032 difference_doc},
2033 {"intersection",(PyCFunction)set_intersection, METH_O,
2034 intersection_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002035 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2036 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002037 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002038 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002039 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002040 issuperset_doc},
2041 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2042 reduce_doc},
2043 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2044 symmetric_difference_doc},
2045 {"union", (PyCFunction)set_union, METH_O,
2046 union_doc},
2047 {NULL, NULL} /* sentinel */
2048};
2049
2050static PyNumberMethods frozenset_as_number = {
2051 0, /*nb_add*/
2052 (binaryfunc)set_sub, /*nb_subtract*/
2053 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002054 0, /*nb_remainder*/
2055 0, /*nb_divmod*/
2056 0, /*nb_power*/
2057 0, /*nb_negative*/
2058 0, /*nb_positive*/
2059 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002060 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002061 0, /*nb_invert*/
2062 0, /*nb_lshift*/
2063 0, /*nb_rshift*/
2064 (binaryfunc)set_and, /*nb_and*/
2065 (binaryfunc)set_xor, /*nb_xor*/
2066 (binaryfunc)set_or, /*nb_or*/
2067};
2068
2069PyDoc_STRVAR(frozenset_doc,
2070"frozenset(iterable) --> frozenset object\n\
2071\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002072Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002073
2074PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002075 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002076 "frozenset", /* tp_name */
2077 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002078 0, /* tp_itemsize */
2079 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002080 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002081 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002082 0, /* tp_getattr */
2083 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002084 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002085 (reprfunc)set_repr, /* tp_repr */
2086 &frozenset_as_number, /* tp_as_number */
2087 &set_as_sequence, /* tp_as_sequence */
2088 0, /* tp_as_mapping */
2089 frozenset_hash, /* tp_hash */
2090 0, /* tp_call */
2091 0, /* tp_str */
2092 PyObject_GenericGetAttr, /* tp_getattro */
2093 0, /* tp_setattro */
2094 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002095 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002096 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002097 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002098 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002099 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002100 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002101 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002102 (getiterfunc)set_iter, /* tp_iter */
2103 0, /* tp_iternext */
2104 frozenset_methods, /* tp_methods */
2105 0, /* tp_members */
2106 0, /* tp_getset */
2107 0, /* tp_base */
2108 0, /* tp_dict */
2109 0, /* tp_descr_get */
2110 0, /* tp_descr_set */
2111 0, /* tp_dictoffset */
2112 0, /* tp_init */
2113 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002114 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002115 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002116};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002117
2118
2119/***** C API functions *************************************************/
2120
2121PyObject *
2122PySet_New(PyObject *iterable)
2123{
2124 return make_new_set(&PySet_Type, iterable);
2125}
2126
2127PyObject *
2128PyFrozenSet_New(PyObject *iterable)
2129{
Christian Heimesfd66e512008-01-29 12:18:50 +00002130 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002131}
2132
Neal Norwitz8c49c822006-03-04 18:41:19 +00002133Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002134PySet_Size(PyObject *anyset)
2135{
2136 if (!PyAnySet_Check(anyset)) {
2137 PyErr_BadInternalCall();
2138 return -1;
2139 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002140 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002141}
2142
2143int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002144PySet_Clear(PyObject *set)
2145{
Christian Heimesfd66e512008-01-29 12:18:50 +00002146 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002147 PyErr_BadInternalCall();
2148 return -1;
2149 }
2150 return set_clear_internal((PySetObject *)set);
2151}
2152
2153int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002154PySet_Contains(PyObject *anyset, PyObject *key)
2155{
2156 if (!PyAnySet_Check(anyset)) {
2157 PyErr_BadInternalCall();
2158 return -1;
2159 }
2160 return set_contains_key((PySetObject *)anyset, key);
2161}
2162
2163int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002164PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002165{
Christian Heimesfd66e512008-01-29 12:18:50 +00002166 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002167 PyErr_BadInternalCall();
2168 return -1;
2169 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002170 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002171}
2172
2173int
Christian Heimesfd66e512008-01-29 12:18:50 +00002174PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002175{
Christian Heimesfd66e512008-01-29 12:18:50 +00002176 if (!PyAnySet_Check(anyset)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002177 PyErr_BadInternalCall();
2178 return -1;
2179 }
Christian Heimesfd66e512008-01-29 12:18:50 +00002180 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002181}
2182
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002183int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002184_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002185{
2186 setentry *entry_ptr;
2187
2188 if (!PyAnySet_Check(set)) {
2189 PyErr_BadInternalCall();
2190 return -1;
2191 }
2192 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2193 return 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002194 *key = entry_ptr->key;
2195 return 1;
2196}
2197
2198int
2199_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2200{
2201 setentry *entry;
2202
2203 if (!PyAnySet_Check(set)) {
2204 PyErr_BadInternalCall();
2205 return -1;
2206 }
2207 if (set_next((PySetObject *)set, pos, &entry) == 0)
2208 return 0;
2209 *key = entry->key;
2210 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002211 return 1;
2212}
2213
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002214PyObject *
2215PySet_Pop(PyObject *set)
2216{
Christian Heimesfd66e512008-01-29 12:18:50 +00002217 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002218 PyErr_BadInternalCall();
2219 return NULL;
2220 }
2221 return set_pop((PySetObject *)set);
2222}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002223
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002224int
2225_PySet_Update(PyObject *set, PyObject *iterable)
2226{
Christian Heimesfd66e512008-01-29 12:18:50 +00002227 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002228 PyErr_BadInternalCall();
2229 return -1;
2230 }
2231 return set_update_internal((PySetObject *)set, iterable);
2232}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002233
2234#ifdef Py_DEBUG
2235
2236/* Test code to be called with any three element set.
2237 Returns True and original set is restored. */
2238
2239#define assertRaises(call_return_value, exception) \
2240 do { \
2241 assert(call_return_value); \
2242 assert(PyErr_ExceptionMatches(exception)); \
2243 PyErr_Clear(); \
2244 } while(0)
2245
2246static PyObject *
2247test_c_api(PySetObject *so)
2248{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002249 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002250 char *s;
2251 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002252 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002253 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002254
2255 /* Verify preconditions and exercise type/size checks */
2256 assert(PyAnySet_Check(ob));
2257 assert(PyAnySet_CheckExact(ob));
2258 assert(!PyFrozenSet_CheckExact(ob));
2259 assert(PySet_Size(ob) == 3);
2260 assert(PySet_GET_SIZE(ob) == 3);
2261
2262 /* Raise TypeError for non-iterable constructor arguments */
2263 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2264 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2265
2266 /* Raise TypeError for unhashable key */
2267 dup = PySet_New(ob);
2268 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2269 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2270 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2271
2272 /* Exercise successful pop, contains, add, and discard */
2273 elem = PySet_Pop(ob);
2274 assert(PySet_Contains(ob, elem) == 0);
2275 assert(PySet_GET_SIZE(ob) == 2);
2276 assert(PySet_Add(ob, elem) == 0);
2277 assert(PySet_Contains(ob, elem) == 1);
2278 assert(PySet_GET_SIZE(ob) == 3);
2279 assert(PySet_Discard(ob, elem) == 1);
2280 assert(PySet_GET_SIZE(ob) == 2);
2281 assert(PySet_Discard(ob, elem) == 0);
2282 assert(PySet_GET_SIZE(ob) == 2);
2283
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002284 /* Exercise clear */
2285 dup2 = PySet_New(dup);
2286 assert(PySet_Clear(dup2) == 0);
2287 assert(PySet_Size(dup2) == 0);
2288 Py_DECREF(dup2);
2289
2290 /* Raise SystemError on clear or update of frozen set */
2291 f = PyFrozenSet_New(dup);
2292 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2293 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2294 Py_DECREF(f);
2295
2296 /* Exercise direct iteration */
2297 i = 0, count = 0;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002298 while (_PySet_Next((PyObject *)dup, &i, &x)) {
Amaury Forgeot d'Arc39599dc2007-11-22 02:48:12 +00002299 s = PyUnicode_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002300 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2301 count++;
2302 }
2303 assert(count == 3);
2304
2305 /* Exercise updates */
2306 dup2 = PySet_New(NULL);
2307 assert(_PySet_Update(dup2, dup) == 0);
2308 assert(PySet_Size(dup2) == 3);
2309 assert(_PySet_Update(dup2, dup) == 0);
2310 assert(PySet_Size(dup2) == 3);
2311 Py_DECREF(dup2);
2312
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002313 /* Raise SystemError when self argument is not a set or frozenset. */
2314 t = PyTuple_New(0);
2315 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2316 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2317 Py_DECREF(t);
2318
2319 /* Raise SystemError when self argument is not a set. */
2320 f = PyFrozenSet_New(dup);
2321 assert(PySet_Size(f) == 3);
2322 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002323 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2324 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2325 Py_DECREF(f);
2326
2327 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002328 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2329 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002330 assert(PySet_GET_SIZE(ob) == 0);
2331 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2332
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002333 /* Restore the set from the copy using the PyNumber API */
2334 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2335 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002336
2337 /* Verify constructors accept NULL arguments */
2338 f = PySet_New(NULL);
2339 assert(f != NULL);
2340 assert(PySet_GET_SIZE(f) == 0);
2341 Py_DECREF(f);
2342 f = PyFrozenSet_New(NULL);
2343 assert(f != NULL);
2344 assert(PyFrozenSet_CheckExact(f));
2345 assert(PySet_GET_SIZE(f) == 0);
2346 Py_DECREF(f);
2347
2348 Py_DECREF(elem);
2349 Py_DECREF(dup);
2350 Py_RETURN_TRUE;
2351}
2352
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002353#undef assertRaises
2354
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002355#endif