blob: f760b6a94b37a5bbef1c590674c48e1bdb4013b5 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00006 Copyright (c) 2003-2007 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000012#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
26}
27
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028/* This must be >= 1. */
29#define PERTURB_SHIFT 5
30
31/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000032static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#ifdef Py_REF_DEBUG
35PyObject *
36_PySet_Dummy(void)
37{
38 return dummy;
39}
40#endif
41
Raymond Hettingerbc841a12005-08-07 13:02:53 +000042#define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
45 (so)->hash = -1; \
46 } while(0)
47
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000048#define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000051 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052 } while(0)
53
Raymond Hettingerbc841a12005-08-07 13:02:53 +000054/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000055#ifndef PySet_MAXFREELIST
56#define PySet_MAXFREELIST 80
57#endif
58static PySetObject *free_list[PySet_MAXFREELIST];
59static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060
Christian Heimes0ded5b52007-12-10 15:50:56 +000061
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062/*
63The basic lookup function used by all operations.
64This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65Open addressing is preferred over chaining since the link overhead for
66chaining would be substantial (100% with typical malloc overhead).
67
68The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000069probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070
71All arithmetic on hash should ignore overflow.
72
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000073Unlike the dictionary implementation, the lookkey functions can return
74NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075*/
76
77static setentry *
78set_lookkey(PySetObject *so, PyObject *key, register long hash)
79{
Martin v. Löwis18e16552006-02-15 17:27:45 +000080 register Py_ssize_t i;
81 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000084 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000085 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000087 PyObject *startkey;
88
89 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000090 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000091 if (entry->key == NULL || entry->key == key)
92 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 if (entry->key == dummy)
95 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000096 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000097 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000098 startkey = entry->key;
Georg Brandlf08a9dd2008-06-10 16:57:31 +000099 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000101 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000102 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000103 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000104 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000105 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000106 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000107 }
108 else {
109 /* The compare did major nasty stuff to the
110 * set: start over.
111 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000112 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000113 }
114 }
115 freeslot = NULL;
116 }
117
118 /* In the loop, key == dummy is by far (factor of 100s) the
119 least likely outcome, so test for that last. */
120 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
121 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000122 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000123 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 break;
127 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000128 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000129 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000130 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000131 startkey = entry->key;
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000132 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000134 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000135 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000136 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000137 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138 if (cmp > 0)
139 break;
140 }
141 else {
142 /* The compare did major nasty stuff to the
143 * set: start over.
144 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000145 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146 }
147 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000148 else if (entry->key == dummy && freeslot == NULL)
149 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000151 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152}
153
154/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000155 * Hacked up version of set_lookkey which can assume keys are always unicode;
156 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000157 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158 */
159static setentry *
Christian Heimes0ded5b52007-12-10 15:50:56 +0000160set_lookkey_unicode(PySetObject *so, PyObject *key, register long hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000161{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000162 register Py_ssize_t i;
163 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000164 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000165 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000166 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000167 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000168
Christian Heimes0ded5b52007-12-10 15:50:56 +0000169 /* Make sure this function doesn't have to handle non-unicode keys,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000170 including subclasses of str; e.g., one reason to subclass
171 strings is to override __eq__, and for speed we don't cater to
172 that here. */
Christian Heimes0ded5b52007-12-10 15:50:56 +0000173 if (!PyUnicode_CheckExact(key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000174 so->lookup = set_lookkey;
175 return set_lookkey(so, key, hash);
176 }
177 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000178 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000179 if (entry->key == NULL || entry->key == key)
180 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000181 if (entry->key == dummy)
182 freeslot = entry;
183 else {
Christian Heimes0ded5b52007-12-10 15:50:56 +0000184 if (entry->hash == hash && unicode_eq(entry->key, key))
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000185 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000186 freeslot = NULL;
187 }
188
189 /* In the loop, key == dummy is by far (factor of 100s) the
190 least likely outcome, so test for that last. */
191 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
192 i = (i << 2) + i + perturb + 1;
193 entry = &table[i & mask];
194 if (entry->key == NULL)
195 return freeslot == NULL ? entry : freeslot;
196 if (entry->key == key
197 || (entry->hash == hash
198 && entry->key != dummy
Christian Heimes0ded5b52007-12-10 15:50:56 +0000199 && unicode_eq(entry->key, key)))
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000200 return entry;
201 if (entry->key == dummy && freeslot == NULL)
202 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000203 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204 assert(0); /* NOT REACHED */
205 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206}
207
208/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211Eats a reference to key.
212*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000214set_insert_key(register PySetObject *so, PyObject *key, long hash)
215{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000216 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
218
219 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000221 if (entry == NULL)
222 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000224 /* UNUSED */
225 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000226 entry->key = key;
227 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000228 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000229 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000230 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000231 entry->key = key;
232 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000233 so->used++;
234 Py_DECREF(dummy);
235 } else {
236 /* ACTIVE */
237 Py_DECREF(key);
238 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000239 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000240}
241
242/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000243Internal routine used by set_table_resize() to insert an item which is
244known to be absent from the set. This routine also assumes that
245the set contains no deleted entries. Besides the performance benefit,
246using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
247Note that no refcounts are changed by this routine; if needed, the caller
248is responsible for incref'ing `key`.
249*/
250static void
251set_insert_clean(register PySetObject *so, PyObject *key, long hash)
252{
253 register size_t i;
254 register size_t perturb;
255 register size_t mask = (size_t)so->mask;
256 setentry *table = so->table;
257 register setentry *entry;
258
259 i = hash & mask;
260 entry = &table[i];
261 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
262 i = (i << 2) + i + perturb + 1;
263 entry = &table[i & mask];
264 }
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
269}
270
271/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000273keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274actually be smaller than the old one.
275*/
276static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000280 setentry *oldtable, *newtable, *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000281 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE];
284
285 assert(minused >= 0);
286
287 /* Find the smallest table size > minused. */
288 for (newsize = PySet_MINSIZE;
289 newsize <= minused && newsize > 0;
290 newsize <<= 1)
291 ;
292 if (newsize <= 0) {
293 PyErr_NoMemory();
294 return -1;
295 }
296
297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL);
300 is_oldtable_malloced = oldtable != so->smalltable;
301
302 if (newsize == PySet_MINSIZE) {
303 /* A large table is shrinking, or we can't get any smaller. */
304 newtable = so->smalltable;
305 if (newtable == oldtable) {
306 if (so->fill == so->used) {
307 /* No dummies, so no point doing anything. */
308 return 0;
309 }
310 /* We're not going to resize it, but rebuild the
311 table anyway to purge old dummy entries.
312 Subtle: This is *necessary* if fill==size,
313 as set_lookkey needs at least one virgin slot to
314 terminate failing searches. If fill < size, it's
315 merely desirable, as dummies slow searches. */
316 assert(so->fill > so->used);
317 memcpy(small_copy, oldtable, sizeof(small_copy));
318 oldtable = small_copy;
319 }
320 }
321 else {
322 newtable = PyMem_NEW(setentry, newsize);
323 if (newtable == NULL) {
324 PyErr_NoMemory();
325 return -1;
326 }
327 }
328
329 /* Make the set empty, using the new table. */
330 assert(newtable != oldtable);
331 so->table = newtable;
332 so->mask = newsize - 1;
333 memset(newtable, 0, sizeof(setentry) * newsize);
334 so->used = 0;
335 i = so->fill;
336 so->fill = 0;
337
338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000340 for (entry = oldtable; i > 0; entry++) {
341 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000342 /* UNUSED */
343 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000344 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345 /* DUMMY */
346 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000347 assert(entry->key == dummy);
348 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000349 } else {
350 /* ACTIVE */
351 --i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000352 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000353 }
354 }
355
356 if (is_oldtable_malloced)
357 PyMem_DEL(oldtable);
358 return 0;
359}
360
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
362
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000364set_add_entry(register PySetObject *so, setentry *entry)
365{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000366 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000367
368 assert(so->fill <= so->mask); /* at least one empty slot */
369 n_used = so->used;
370 Py_INCREF(entry->key);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000371 if (set_insert_key(so, entry->key, entry->hash) == -1) {
372 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000373 return -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000374 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000375 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
376 return 0;
377 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
378}
379
380static int
381set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382{
383 register long hash;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000384 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000385
Christian Heimes0ded5b52007-12-10 15:50:56 +0000386 if (!PyUnicode_CheckExact(key) ||
387 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000388 hash = PyObject_Hash(key);
389 if (hash == -1)
390 return -1;
391 }
392 assert(so->fill <= so->mask); /* at least one empty slot */
393 n_used = so->used;
394 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000395 if (set_insert_key(so, key, hash) == -1) {
396 Py_DECREF(key);
397 return -1;
398 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000399 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
400 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000401 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402}
403
404#define DISCARD_NOTFOUND 0
405#define DISCARD_FOUND 1
406
407static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000408set_discard_entry(PySetObject *so, setentry *oldentry)
409{ register setentry *entry;
410 PyObject *old_key;
411
412 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000413 if (entry == NULL)
414 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000415 if (entry->key == NULL || entry->key == dummy)
416 return DISCARD_NOTFOUND;
417 old_key = entry->key;
418 Py_INCREF(dummy);
419 entry->key = dummy;
420 so->used--;
421 Py_DECREF(old_key);
422 return DISCARD_FOUND;
423}
424
425static int
426set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000427{
428 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000429 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000430 PyObject *old_key;
431
432 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000433
434 if (!PyUnicode_CheckExact(key) ||
435 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000436 hash = PyObject_Hash(key);
437 if (hash == -1)
438 return -1;
439 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000440 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000441 if (entry == NULL)
442 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000443 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000444 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000445 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000446 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000447 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448 so->used--;
449 Py_DECREF(old_key);
450 return DISCARD_FOUND;
451}
452
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000453static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454set_clear_internal(PySetObject *so)
455{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000456 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457 int table_is_malloced;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000458 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000459 setentry small_copy[PySet_MINSIZE];
460#ifdef Py_DEBUG
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000461 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000463
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464 n = so->mask + 1;
465 i = 0;
466#endif
467
468 table = so->table;
469 assert(table != NULL);
470 table_is_malloced = table != so->smalltable;
471
472 /* This is delicate. During the process of clearing the set,
473 * decrefs can cause the set to mutate. To avoid fatal confusion
474 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000475 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476 * clearing.
477 */
478 fill = so->fill;
479 if (table_is_malloced)
480 EMPTY_TO_MINSIZE(so);
481
482 else if (fill > 0) {
483 /* It's a small table with something that needs to be cleared.
484 * Afraid the only safe way is to copy the set entries into
485 * another small table first.
486 */
487 memcpy(small_copy, table, sizeof(small_copy));
488 table = small_copy;
489 EMPTY_TO_MINSIZE(so);
490 }
491 /* else it's a small table that's already empty */
492
493 /* Now we can finally clear things. If C had refcounts, we could
494 * assert that the refcount on table is 1 now, i.e. that this function
495 * has unique access to it, so decref side-effects can't alter it.
496 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000497 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498#ifdef Py_DEBUG
499 assert(i < n);
500 ++i;
501#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000502 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000504 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505 }
506#ifdef Py_DEBUG
507 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509#endif
510 }
511
512 if (table_is_malloced)
513 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000514 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515}
516
517/*
518 * Iterate over a set table. Use like so:
519 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000520 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000521 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000522 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000523 * while (set_next(yourset, &pos, &entry)) {
524 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525 * }
526 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000527 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000528 * mutates the table.
529 */
530static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000531set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000533 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000534 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536
537 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000538 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000539 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000540 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000541 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000542 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000544 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545 if (i > mask)
546 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000547 assert(table[i].key != NULL);
548 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000549 return 1;
550}
551
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000552static void
553set_dealloc(PySetObject *so)
554{
555 register setentry *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000556 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000557 PyObject_GC_UnTrack(so);
558 Py_TRASHCAN_SAFE_BEGIN(so)
559 if (so->weakreflist != NULL)
560 PyObject_ClearWeakRefs((PyObject *) so);
561
562 for (entry = so->table; fill > 0; entry++) {
563 if (entry->key) {
564 --fill;
565 Py_DECREF(entry->key);
566 }
567 }
568 if (so->table != so->smalltable)
569 PyMem_DEL(so->table);
Christian Heimes2202f872008-02-06 14:31:34 +0000570 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
571 free_list[numfree++] = so;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000572 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000573 Py_TYPE(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000574 Py_TRASHCAN_SAFE_END(so)
575}
576
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000577static PyObject *
578set_repr(PySetObject *so)
579{
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000580 PyObject *keys, *result=NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000581 Py_UNICODE *u;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000582 int status = Py_ReprEnter((PyObject*)so);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000583 PyObject *listrepr;
584 Py_ssize_t newsize;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000585
586 if (status != 0) {
587 if (status < 0)
588 return NULL;
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 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591
Georg Brandlc4996ba2006-08-28 19:37:11 +0000592 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000593 if (!so->used) {
594 Py_ReprLeave((PyObject*)so);
Christian Heimes90aa7642007-12-19 02:45:37 +0000595 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000596 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000597
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000598 keys = PySequence_List((PyObject *)so);
599 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000600 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000602 listrepr = PyObject_Repr(keys);
603 Py_DECREF(keys);
604 if (listrepr == NULL) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000605 Py_DECREF(keys);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000606 goto done;
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000607 }
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000608 newsize = PyUnicode_GET_SIZE(listrepr);
609 result = PyUnicode_FromUnicode(NULL, newsize);
610 if (result) {
611 u = PyUnicode_AS_UNICODE(result);
612 *u++ = '{';
613 /* Omit the brackets from the listrepr */
614 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
615 PyUnicode_GET_SIZE(listrepr)-2);
616 u += newsize-2;
617 *u++ = '}';
618 }
619 Py_DECREF(listrepr);
Christian Heimes90aa7642007-12-19 02:45:37 +0000620 if (Py_TYPE(so) != &PySet_Type) {
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000621 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000622 Py_TYPE(so)->tp_name,
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000623 result);
624 Py_DECREF(result);
625 result = tmp;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000626 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000627done:
628 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000629 return result;
630}
631
Martin v. Löwis18e16552006-02-15 17:27:45 +0000632static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000633set_len(PyObject *so)
634{
635 return ((PySetObject *)so)->used;
636}
637
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000638static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000639set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000640{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000641 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000643 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000644
645 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000646 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000647
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000648 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000649 if (other == so || other->used == 0)
650 /* a.update(a) or a.update({}); nothing to do */
651 return 0;
652 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000653 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000654 * that there will be no (or few) overlapping keys.
655 */
656 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
657 if (set_table_resize(so, (so->used + other->used)*2) != 0)
658 return -1;
659 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000660 for (i = 0; i <= other->mask; i++) {
661 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000662 if (entry->key != NULL &&
663 entry->key != dummy) {
664 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000665 if (set_insert_key(so, entry->key, entry->hash) == -1) {
666 Py_DECREF(entry->key);
667 return -1;
668 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000669 }
670 }
671 return 0;
672}
673
674static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000675set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000676{
677 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000678 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000679
Christian Heimes0ded5b52007-12-10 15:50:56 +0000680 if (!PyUnicode_CheckExact(key) ||
681 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000682 hash = PyObject_Hash(key);
683 if (hash == -1)
684 return -1;
685 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000686 entry = (so->lookup)(so, key, hash);
687 if (entry == NULL)
688 return -1;
689 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000690 return key != NULL && key != dummy;
691}
692
Raymond Hettingerc991db22005-08-11 07:58:45 +0000693static int
694set_contains_entry(PySetObject *so, setentry *entry)
695{
696 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000697 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000698
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000699 lu_entry = (so->lookup)(so, entry->key, entry->hash);
700 if (lu_entry == NULL)
701 return -1;
702 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000703 return key != NULL && key != dummy;
704}
705
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000706static PyObject *
707set_pop(PySetObject *so)
708{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000709 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000710 register setentry *entry;
711 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000712
713 assert (PyAnySet_Check(so));
714 if (so->used == 0) {
715 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
716 return NULL;
717 }
718
719 /* Set entry to "the first" unused or dummy set entry. We abuse
720 * the hash field of slot 0 to hold a search finger:
721 * If slot 0 has a value, use slot 0.
722 * Else slot 0 is being used to hold a search finger,
723 * and we use its hash value as the first index to look.
724 */
725 entry = &so->table[0];
726 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000727 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000728 /* The hash field may be a real hash value, or it may be a
729 * legit search finger, or it may be a once-legit search
730 * finger that's out of bounds now because it wrapped around
731 * or the table shrunk -- simply make sure it's in bounds now.
732 */
733 if (i > so->mask || i < 1)
734 i = 1; /* skip slot 0 */
735 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
736 i++;
737 if (i > so->mask)
738 i = 1;
739 }
740 }
741 key = entry->key;
742 Py_INCREF(dummy);
743 entry->key = dummy;
744 so->used--;
745 so->table[0].hash = i + 1; /* next place to start */
746 return key;
747}
748
749PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
750
751static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000752set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000753{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000754 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000755 setentry *entry;
756
757 while (set_next(so, &pos, &entry))
758 Py_VISIT(entry->key);
759 return 0;
760}
761
762static long
763frozenset_hash(PyObject *self)
764{
765 PySetObject *so = (PySetObject *)self;
766 long h, hash = 1927868237L;
767 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000768 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000769
770 if (so->hash != -1)
771 return so->hash;
772
773 hash *= PySet_GET_SIZE(self) + 1;
774 while (set_next(so, &pos, &entry)) {
775 /* Work to increase the bit dispersion for closely spaced hash
776 values. The is important because some use cases have many
777 combinations of a small number of elements with nearby
778 hashes so that many distinct combinations collapse to only
779 a handful of distinct hash values. */
780 h = entry->hash;
781 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
782 }
783 hash = hash * 69069L + 907133923L;
784 if (hash == -1)
785 hash = 590923713L;
786 so->hash = hash;
787 return hash;
788}
789
Raymond Hettingera9d99362005-08-05 00:01:15 +0000790/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792typedef struct {
793 PyObject_HEAD
794 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000795 Py_ssize_t si_used;
796 Py_ssize_t si_pos;
797 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000798} setiterobject;
799
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800static void
801setiter_dealloc(setiterobject *si)
802{
803 Py_XDECREF(si->si_set);
804 PyObject_Del(si);
805}
806
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000807static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000808setiter_len(setiterobject *si)
809{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000810 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000812 len = si->len;
Christian Heimes217cfd12007-12-02 14:31:20 +0000813 return PyLong_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000814}
815
Armin Rigof5b3e362006-02-11 21:32:43 +0000816PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000817
818static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000819 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000820 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000821};
822
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000823static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824{
825 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000826 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000827 register setentry *entry;
828 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000830 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000832 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000834 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835 PyErr_SetString(PyExc_RuntimeError,
836 "Set changed size during iteration");
837 si->si_used = -1; /* Make this state sticky */
838 return NULL;
839 }
840
841 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000842 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000843 entry = so->table;
844 mask = so->mask;
845 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000846 i++;
847 si->si_pos = i+1;
848 if (i > mask)
849 goto fail;
850 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000851 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000852 Py_INCREF(key);
853 return key;
854
855fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000856 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000857 si->si_set = NULL;
858 return NULL;
859}
860
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000861PyTypeObject PySetIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000862 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +0000863 "set_iterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000864 sizeof(setiterobject), /* tp_basicsize */
865 0, /* tp_itemsize */
866 /* methods */
867 (destructor)setiter_dealloc, /* tp_dealloc */
868 0, /* tp_print */
869 0, /* tp_getattr */
870 0, /* tp_setattr */
871 0, /* tp_compare */
872 0, /* tp_repr */
873 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000874 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000875 0, /* tp_as_mapping */
876 0, /* tp_hash */
877 0, /* tp_call */
878 0, /* tp_str */
879 PyObject_GenericGetAttr, /* tp_getattro */
880 0, /* tp_setattro */
881 0, /* tp_as_buffer */
882 Py_TPFLAGS_DEFAULT, /* tp_flags */
883 0, /* tp_doc */
884 0, /* tp_traverse */
885 0, /* tp_clear */
886 0, /* tp_richcompare */
887 0, /* tp_weaklistoffset */
888 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000889 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000890 setiter_methods, /* tp_methods */
891 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000892};
893
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000894static PyObject *
895set_iter(PySetObject *so)
896{
897 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
898 if (si == NULL)
899 return NULL;
900 Py_INCREF(so);
901 si->si_set = so;
902 si->si_used = so->used;
903 si->si_pos = 0;
904 si->len = so->used;
905 return (PyObject *)si;
906}
907
Raymond Hettingerd7946662005-08-01 21:39:29 +0000908static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000909set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000910{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000911 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000912
Christian Heimesaf98da12008-01-27 15:18:18 +0000913 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000914 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000915
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000916 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000917 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000918 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000919 long hash;
920 Py_ssize_t dictsize = PyDict_Size(other);
921
922 /* Do one big resize at the start, rather than
923 * incrementally resizing as we insert new keys. Expect
924 * that there will be no (or few) overlapping keys.
925 */
926 if (dictsize == -1)
927 return -1;
928 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
929 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
930 return -1;
931 }
932 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
933 setentry an_entry;
934
935 an_entry.hash = hash;
936 an_entry.key = key;
937 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000938 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000939 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000940 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000941 }
942
Raymond Hettingera38123e2003-11-24 22:18:49 +0000943 it = PyObject_GetIter(other);
944 if (it == NULL)
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 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000948 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000949 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000950 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000951 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000952 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000953 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000954 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000955 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000956 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000957 return -1;
958 return 0;
959}
960
961static PyObject *
962set_update(PySetObject *so, PyObject *other)
963{
964 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000965 return NULL;
966 Py_RETURN_NONE;
967}
968
969PyDoc_STRVAR(update_doc,
970"Update a set with the union of itself and another.");
971
972static PyObject *
973make_new_set(PyTypeObject *type, PyObject *iterable)
974{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000975 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000976
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000977 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000978 dummy = PyUnicode_FromString("<dummy key>");
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000979 if (dummy == NULL)
980 return NULL;
981 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000982
983 /* create PySetObject structure */
Christian Heimes2202f872008-02-06 14:31:34 +0000984 if (numfree &&
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000985 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
Christian Heimes2202f872008-02-06 14:31:34 +0000986 so = free_list[--numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000987 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimes90aa7642007-12-19 02:45:37 +0000988 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000989 _Py_NewReference((PyObject *)so);
990 EMPTY_TO_MINSIZE(so);
991 PyObject_GC_Track(so);
992 } else {
993 so = (PySetObject *)type->tp_alloc(type, 0);
994 if (so == NULL)
995 return NULL;
996 /* tp_alloc has already zeroed the structure */
997 assert(so->table == NULL && so->fill == 0 && so->used == 0);
998 INIT_NONZERO_SET_SLOTS(so);
999 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001000
Christian Heimes0ded5b52007-12-10 15:50:56 +00001001 so->lookup = set_lookkey_unicode;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001002 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001003
Raymond Hettingera38123e2003-11-24 22:18:49 +00001004 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001005 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001006 Py_DECREF(so);
1007 return NULL;
1008 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001009 }
1010
Raymond Hettingera690a992003-11-16 16:17:49 +00001011 return (PyObject *)so;
1012}
1013
Raymond Hettingerd7946662005-08-01 21:39:29 +00001014/* The empty frozenset is a singleton */
1015static PyObject *emptyfrozenset = NULL;
1016
Raymond Hettingera690a992003-11-16 16:17:49 +00001017static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001018frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001019{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001020 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001021
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001022 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001023 return NULL;
1024
Raymond Hettingera690a992003-11-16 16:17:49 +00001025 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1026 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001027
1028 if (type != &PyFrozenSet_Type)
1029 return make_new_set(type, iterable);
1030
1031 if (iterable != NULL) {
1032 /* frozenset(f) is idempotent */
1033 if (PyFrozenSet_CheckExact(iterable)) {
1034 Py_INCREF(iterable);
1035 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001036 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001037 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001038 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001039 return result;
1040 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001041 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001042 /* The empty frozenset is a singleton */
1043 if (emptyfrozenset == NULL)
1044 emptyfrozenset = make_new_set(type, NULL);
1045 Py_XINCREF(emptyfrozenset);
1046 return emptyfrozenset;
1047}
1048
1049void
1050PySet_Fini(void)
1051{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001052 PySetObject *so;
1053
Christian Heimes2202f872008-02-06 14:31:34 +00001054 while (numfree) {
1055 numfree--;
1056 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001057 PyObject_GC_Del(so);
1058 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001059 Py_CLEAR(dummy);
1060 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001061}
1062
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001063static PyObject *
1064set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1065{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001066 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001067 return NULL;
1068
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001069 return make_new_set(type, NULL);
1070}
1071
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001072/* set_swap_bodies() switches the contents of any two sets by moving their
1073 internal data pointers and, if needed, copying the internal smalltables.
1074 Semantically equivalent to:
1075
1076 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1077
1078 The function always succeeds and it leaves both objects in a stable state.
1079 Useful for creating temporary frozensets from sets for membership testing
1080 in __contains__(), discard(), and remove(). Also useful for operations
1081 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001082 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001083*/
1084
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001085static void
1086set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001087{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001088 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001089 setentry *u;
1090 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1091 setentry tab[PySet_MINSIZE];
1092 long h;
1093
1094 t = a->fill; a->fill = b->fill; b->fill = t;
1095 t = a->used; a->used = b->used; b->used = t;
1096 t = a->mask; a->mask = b->mask; b->mask = t;
1097
1098 u = a->table;
1099 if (a->table == a->smalltable)
1100 u = b->smalltable;
1101 a->table = b->table;
1102 if (b->table == b->smalltable)
1103 a->table = a->smalltable;
1104 b->table = u;
1105
1106 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1107
1108 if (a->table == a->smalltable || b->table == b->smalltable) {
1109 memcpy(tab, a->smalltable, sizeof(tab));
1110 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1111 memcpy(b->smalltable, tab, sizeof(tab));
1112 }
1113
Christian Heimes90aa7642007-12-19 02:45:37 +00001114 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1115 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001116 h = a->hash; a->hash = b->hash; b->hash = h;
1117 } else {
1118 a->hash = -1;
1119 b->hash = -1;
1120 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001121}
1122
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001123static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001124set_copy(PySetObject *so)
1125{
Christian Heimes90aa7642007-12-19 02:45:37 +00001126 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001127}
1128
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001129static PyObject *
1130frozenset_copy(PySetObject *so)
1131{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001132 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001133 Py_INCREF(so);
1134 return (PyObject *)so;
1135 }
1136 return set_copy(so);
1137}
1138
Raymond Hettingera690a992003-11-16 16:17:49 +00001139PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1140
1141static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001142set_clear(PySetObject *so)
1143{
1144 set_clear_internal(so);
1145 Py_RETURN_NONE;
1146}
1147
1148PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1149
1150static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001151set_union(PySetObject *so, PyObject *other)
1152{
1153 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001154
1155 result = (PySetObject *)set_copy(so);
1156 if (result == NULL)
1157 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001158 if ((PyObject *)so == other)
1159 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001160 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001161 Py_DECREF(result);
1162 return NULL;
1163 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001164 return (PyObject *)result;
1165}
1166
1167PyDoc_STRVAR(union_doc,
1168 "Return the union of two sets as a new set.\n\
1169\n\
1170(i.e. all elements that are in either set.)");
1171
1172static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001173set_or(PySetObject *so, PyObject *other)
1174{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001175 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001176 Py_INCREF(Py_NotImplemented);
1177 return Py_NotImplemented;
1178 }
1179 return set_union(so, other);
1180}
1181
1182static PyObject *
1183set_ior(PySetObject *so, PyObject *other)
1184{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001185 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001186 Py_INCREF(Py_NotImplemented);
1187 return Py_NotImplemented;
1188 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001189 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001190 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001191 Py_INCREF(so);
1192 return (PyObject *)so;
1193}
1194
1195static PyObject *
1196set_intersection(PySetObject *so, PyObject *other)
1197{
1198 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001199 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001200
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001201 if ((PyObject *)so == other)
1202 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001203
Christian Heimes90aa7642007-12-19 02:45:37 +00001204 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001205 if (result == NULL)
1206 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001207
Christian Heimesaf98da12008-01-27 15:18:18 +00001208 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001209 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001210 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001211
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001212 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001213 tmp = (PyObject *)so;
1214 so = (PySetObject *)other;
1215 other = tmp;
1216 }
1217
Raymond Hettingerc991db22005-08-11 07:58:45 +00001218 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001219 int rv = set_contains_entry(so, entry);
1220 if (rv == -1) {
1221 Py_DECREF(result);
1222 return NULL;
1223 }
1224 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001225 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001226 Py_DECREF(result);
1227 return NULL;
1228 }
1229 }
1230 }
1231 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001232 }
1233
Raymond Hettingera690a992003-11-16 16:17:49 +00001234 it = PyObject_GetIter(other);
1235 if (it == NULL) {
1236 Py_DECREF(result);
1237 return NULL;
1238 }
1239
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001240 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001241 int rv;
1242 setentry entry;
1243 long hash = PyObject_Hash(key);
1244
1245 if (hash == -1) {
1246 Py_DECREF(it);
1247 Py_DECREF(result);
1248 Py_DECREF(key);
1249 return NULL;
1250 }
1251 entry.hash = hash;
1252 entry.key = key;
1253 rv = set_contains_entry(so, &entry);
1254 if (rv == -1) {
1255 Py_DECREF(it);
1256 Py_DECREF(result);
1257 Py_DECREF(key);
1258 return NULL;
1259 }
1260 if (rv) {
1261 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001262 Py_DECREF(it);
1263 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001264 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001265 return NULL;
1266 }
1267 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001268 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001269 }
1270 Py_DECREF(it);
1271 if (PyErr_Occurred()) {
1272 Py_DECREF(result);
1273 return NULL;
1274 }
1275 return (PyObject *)result;
1276}
1277
1278PyDoc_STRVAR(intersection_doc,
1279"Return the intersection of two sets as a new set.\n\
1280\n\
1281(i.e. all elements that are in both sets.)");
1282
1283static PyObject *
1284set_intersection_update(PySetObject *so, PyObject *other)
1285{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001286 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001287
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001288 tmp = set_intersection(so, other);
1289 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001290 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001291 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001292 Py_DECREF(tmp);
1293 Py_RETURN_NONE;
1294}
1295
1296PyDoc_STRVAR(intersection_update_doc,
1297"Update a set with the intersection of itself and another.");
1298
1299static PyObject *
1300set_and(PySetObject *so, PyObject *other)
1301{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001302 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001303 Py_INCREF(Py_NotImplemented);
1304 return Py_NotImplemented;
1305 }
1306 return set_intersection(so, other);
1307}
1308
1309static PyObject *
1310set_iand(PySetObject *so, PyObject *other)
1311{
1312 PyObject *result;
1313
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001314 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001315 Py_INCREF(Py_NotImplemented);
1316 return Py_NotImplemented;
1317 }
1318 result = set_intersection_update(so, other);
1319 if (result == NULL)
1320 return NULL;
1321 Py_DECREF(result);
1322 Py_INCREF(so);
1323 return (PyObject *)so;
1324}
1325
Guido van Rossum58da9312007-11-10 23:39:45 +00001326static PyObject *
1327set_isdisjoint(PySetObject *so, PyObject *other)
1328{
1329 PyObject *key, *it, *tmp;
1330
1331 if ((PyObject *)so == other) {
1332 if (PySet_GET_SIZE(so) == 0)
1333 Py_RETURN_TRUE;
1334 else
1335 Py_RETURN_FALSE;
1336 }
1337
1338 if (PyAnySet_CheckExact(other)) {
1339 Py_ssize_t pos = 0;
1340 setentry *entry;
1341
1342 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1343 tmp = (PyObject *)so;
1344 so = (PySetObject *)other;
1345 other = tmp;
1346 }
1347 while (set_next((PySetObject *)other, &pos, &entry)) {
1348 int rv = set_contains_entry(so, entry);
1349 if (rv == -1)
1350 return NULL;
1351 if (rv)
1352 Py_RETURN_FALSE;
1353 }
1354 Py_RETURN_TRUE;
1355 }
1356
1357 it = PyObject_GetIter(other);
1358 if (it == NULL)
1359 return NULL;
1360
1361 while ((key = PyIter_Next(it)) != NULL) {
1362 int rv;
1363 setentry entry;
Christian Heimes0ded5b52007-12-10 15:50:56 +00001364 long hash = PyObject_Hash(key);;
Guido van Rossum58da9312007-11-10 23:39:45 +00001365
1366 if (hash == -1) {
1367 Py_DECREF(key);
1368 Py_DECREF(it);
1369 return NULL;
1370 }
1371 entry.hash = hash;
1372 entry.key = key;
1373 rv = set_contains_entry(so, &entry);
1374 Py_DECREF(key);
1375 if (rv == -1) {
1376 Py_DECREF(it);
1377 return NULL;
1378 }
1379 if (rv) {
1380 Py_DECREF(it);
1381 Py_RETURN_FALSE;
1382 }
1383 }
1384 Py_DECREF(it);
1385 if (PyErr_Occurred())
1386 return NULL;
1387 Py_RETURN_TRUE;
1388}
1389
1390PyDoc_STRVAR(isdisjoint_doc,
1391"Return True if two sets have a null intersection.");
1392
Neal Norwitz6576bd82005-11-13 18:41:28 +00001393static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001394set_difference_update_internal(PySetObject *so, PyObject *other)
1395{
1396 if ((PyObject *)so == other)
1397 return set_clear_internal(so);
1398
Christian Heimesaf98da12008-01-27 15:18:18 +00001399 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001400 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001401 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001402
1403 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001404 if (set_discard_entry(so, entry) == -1)
1405 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001406 } else {
1407 PyObject *key, *it;
1408 it = PyObject_GetIter(other);
1409 if (it == NULL)
1410 return -1;
1411
1412 while ((key = PyIter_Next(it)) != NULL) {
1413 if (set_discard_key(so, key) == -1) {
1414 Py_DECREF(it);
1415 Py_DECREF(key);
1416 return -1;
1417 }
1418 Py_DECREF(key);
1419 }
1420 Py_DECREF(it);
1421 if (PyErr_Occurred())
1422 return -1;
1423 }
1424 /* If more than 1/5 are dummies, then resize them away. */
1425 if ((so->fill - so->used) * 5 < so->mask)
1426 return 0;
1427 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1428}
1429
Raymond Hettingera690a992003-11-16 16:17:49 +00001430static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001431set_difference_update(PySetObject *so, PyObject *other)
1432{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001433 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001434 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001435 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001436}
1437
1438PyDoc_STRVAR(difference_update_doc,
1439"Remove all elements of another set from this set.");
1440
1441static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001442set_difference(PySetObject *so, PyObject *other)
1443{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001444 PyObject *result;
1445 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001446 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001447
Christian Heimesaf98da12008-01-27 15:18:18 +00001448 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001449 result = set_copy(so);
1450 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001451 return NULL;
1452 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001453 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001454 Py_DECREF(result);
1455 return NULL;
1456 }
1457
Christian Heimes90aa7642007-12-19 02:45:37 +00001458 result = make_new_set(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001459 if (result == NULL)
1460 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001461
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001462 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001463 while (set_next(so, &pos, &entry)) {
1464 setentry entrycopy;
1465 entrycopy.hash = entry->hash;
1466 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001467 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001468 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1469 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001470 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001471 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001472 }
1473 }
1474 return result;
1475 }
1476
Raymond Hettingerc991db22005-08-11 07:58:45 +00001477 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001478 int rv = set_contains_entry((PySetObject *)other, entry);
1479 if (rv == -1) {
1480 Py_DECREF(result);
1481 return NULL;
1482 }
1483 if (!rv) {
1484 if (set_add_entry((PySetObject *)result, entry) == -1) {
1485 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001486 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001487 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001488 }
1489 }
1490 return result;
1491}
1492
1493PyDoc_STRVAR(difference_doc,
1494"Return the difference of two sets as a new set.\n\
1495\n\
1496(i.e. all elements that are in this set but not the other.)");
1497static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001498set_sub(PySetObject *so, PyObject *other)
1499{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001500 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001501 Py_INCREF(Py_NotImplemented);
1502 return Py_NotImplemented;
1503 }
1504 return set_difference(so, other);
1505}
1506
1507static PyObject *
1508set_isub(PySetObject *so, PyObject *other)
1509{
1510 PyObject *result;
1511
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001512 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001513 Py_INCREF(Py_NotImplemented);
1514 return Py_NotImplemented;
1515 }
1516 result = set_difference_update(so, other);
1517 if (result == NULL)
1518 return NULL;
1519 Py_DECREF(result);
1520 Py_INCREF(so);
1521 return (PyObject *)so;
1522}
1523
1524static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001525set_symmetric_difference_update(PySetObject *so, PyObject *other)
1526{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001527 PySetObject *otherset;
1528 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001529 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001530 setentry *entry;
1531
1532 if ((PyObject *)so == other)
1533 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001534
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001535 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001536 PyObject *value;
1537 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001538 long hash;
1539 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001540 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001541
Thomas Wouters89f507f2006-12-13 04:49:30 +00001542 an_entry.hash = hash;
1543 an_entry.key = key;
1544 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001545 if (rv == -1)
1546 return NULL;
1547 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001548 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001549 return NULL;
1550 }
1551 }
1552 Py_RETURN_NONE;
1553 }
1554
Christian Heimesaf98da12008-01-27 15:18:18 +00001555 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001556 Py_INCREF(other);
1557 otherset = (PySetObject *)other;
1558 } else {
Christian Heimes90aa7642007-12-19 02:45:37 +00001559 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001560 if (otherset == NULL)
1561 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001562 }
1563
Raymond Hettingerc991db22005-08-11 07:58:45 +00001564 while (set_next(otherset, &pos, &entry)) {
1565 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001566 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001567 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001568 return NULL;
1569 }
1570 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001571 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001572 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001573 return NULL;
1574 }
1575 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001576 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001577 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001578 Py_RETURN_NONE;
1579}
1580
1581PyDoc_STRVAR(symmetric_difference_update_doc,
1582"Update a set with the symmetric difference of itself and another.");
1583
1584static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001585set_symmetric_difference(PySetObject *so, PyObject *other)
1586{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001587 PyObject *rv;
1588 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001589
Christian Heimes90aa7642007-12-19 02:45:37 +00001590 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001591 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001592 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001593 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1594 if (rv == NULL)
1595 return NULL;
1596 Py_DECREF(rv);
1597 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001598}
1599
1600PyDoc_STRVAR(symmetric_difference_doc,
1601"Return the symmetric difference of two sets as a new set.\n\
1602\n\
1603(i.e. all elements that are in exactly one of the sets.)");
1604
1605static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001606set_xor(PySetObject *so, PyObject *other)
1607{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001608 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001609 Py_INCREF(Py_NotImplemented);
1610 return Py_NotImplemented;
1611 }
1612 return set_symmetric_difference(so, other);
1613}
1614
1615static PyObject *
1616set_ixor(PySetObject *so, PyObject *other)
1617{
1618 PyObject *result;
1619
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001620 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001621 Py_INCREF(Py_NotImplemented);
1622 return Py_NotImplemented;
1623 }
1624 result = set_symmetric_difference_update(so, other);
1625 if (result == NULL)
1626 return NULL;
1627 Py_DECREF(result);
1628 Py_INCREF(so);
1629 return (PyObject *)so;
1630}
1631
1632static PyObject *
1633set_issubset(PySetObject *so, PyObject *other)
1634{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001635 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001636 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001637
Christian Heimesaf98da12008-01-27 15:18:18 +00001638 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001639 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001640 tmp = make_new_set(&PySet_Type, other);
1641 if (tmp == NULL)
1642 return NULL;
1643 result = set_issubset(so, tmp);
1644 Py_DECREF(tmp);
1645 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001646 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001647 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001648 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001649
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001650 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001651 int rv = set_contains_entry((PySetObject *)other, entry);
1652 if (rv == -1)
1653 return NULL;
1654 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001655 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001656 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001657 Py_RETURN_TRUE;
1658}
1659
1660PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1661
1662static PyObject *
1663set_issuperset(PySetObject *so, PyObject *other)
1664{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001665 PyObject *tmp, *result;
1666
Christian Heimesaf98da12008-01-27 15:18:18 +00001667 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001668 tmp = make_new_set(&PySet_Type, other);
1669 if (tmp == NULL)
1670 return NULL;
1671 result = set_issuperset(so, tmp);
1672 Py_DECREF(tmp);
1673 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001674 }
1675 return set_issubset((PySetObject *)other, (PyObject *)so);
1676}
1677
1678PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1679
Raymond Hettingera690a992003-11-16 16:17:49 +00001680static PyObject *
1681set_richcompare(PySetObject *v, PyObject *w, int op)
1682{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001683 PyObject *r1, *r2;
1684
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001685 if(!PyAnySet_Check(w)) {
Guido van Rossum10ab4ae2007-08-23 23:57:24 +00001686 Py_INCREF(Py_NotImplemented);
1687 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001688 }
1689 switch (op) {
1690 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001691 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001692 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001693 if (v->hash != -1 &&
1694 ((PySetObject *)w)->hash != -1 &&
1695 v->hash != ((PySetObject *)w)->hash)
1696 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001697 return set_issubset(v, w);
1698 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001699 r1 = set_richcompare(v, w, Py_EQ);
1700 if (r1 == NULL)
1701 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001702 r2 = PyBool_FromLong(PyObject_Not(r1));
1703 Py_DECREF(r1);
1704 return r2;
1705 case Py_LE:
1706 return set_issubset(v, w);
1707 case Py_GE:
1708 return set_issuperset(v, w);
1709 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001710 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001711 Py_RETURN_FALSE;
1712 return set_issubset(v, w);
1713 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001714 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001715 Py_RETURN_FALSE;
1716 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001717 }
1718 Py_INCREF(Py_NotImplemented);
1719 return Py_NotImplemented;
1720}
1721
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001722static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001723set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001724{
1725 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1726 return -1;
1727}
1728
Raymond Hettingera690a992003-11-16 16:17:49 +00001729static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001730set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001731{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001732 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001733 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001734 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001735}
1736
1737PyDoc_STRVAR(add_doc,
1738"Add an element to a set.\n\
1739\n\
1740This has no effect if the element is already present.");
1741
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001742static int
1743set_contains(PySetObject *so, PyObject *key)
1744{
1745 PyObject *tmpkey;
1746 int rv;
1747
1748 rv = set_contains_key(so, key);
1749 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001750 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001751 return -1;
1752 PyErr_Clear();
1753 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1754 if (tmpkey == NULL)
1755 return -1;
1756 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1757 rv = set_contains(so, tmpkey);
1758 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1759 Py_DECREF(tmpkey);
1760 }
1761 return rv;
1762}
1763
1764static PyObject *
1765set_direct_contains(PySetObject *so, PyObject *key)
1766{
1767 long result;
1768
1769 result = set_contains(so, key);
1770 if (result == -1)
1771 return NULL;
1772 return PyBool_FromLong(result);
1773}
1774
1775PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1776
Raymond Hettingera690a992003-11-16 16:17:49 +00001777static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001778set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001779{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001780 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001781 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001782
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001783 rv = set_discard_key(so, key);
1784 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001785 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001786 return NULL;
1787 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001788 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1789 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001790 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001791 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001792 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001793 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001794 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001795 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001796 } else if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001797 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001798 return NULL;
1799 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001800 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001801}
1802
1803PyDoc_STRVAR(remove_doc,
1804"Remove an element from a set; it must be a member.\n\
1805\n\
1806If the element is not a member, raise a KeyError.");
1807
1808static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001809set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001810{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001811 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001812 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001813
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001814 rv = set_discard_key(so, key);
1815 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001816 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001817 return NULL;
1818 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001819 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1820 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001821 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001822 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001823 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001824 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001825 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001826 return result;
1827 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001828 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001829}
1830
1831PyDoc_STRVAR(discard_doc,
1832"Remove an element from a set if it is a member.\n\
1833\n\
1834If the element is not a member, do nothing.");
1835
1836static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001837set_reduce(PySetObject *so)
1838{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001839 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001840
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001841 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001842 if (keys == NULL)
1843 goto done;
1844 args = PyTuple_Pack(1, keys);
1845 if (args == NULL)
1846 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001847 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1848 if (dict == NULL) {
1849 PyErr_Clear();
1850 dict = Py_None;
1851 Py_INCREF(dict);
1852 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001853 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001854done:
1855 Py_XDECREF(args);
1856 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001857 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001858 return result;
1859}
1860
1861PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1862
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001863static int
1864set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1865{
1866 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001867
1868 if (!PyAnySet_Check(self))
1869 return -1;
Christian Heimes90aa7642007-12-19 02:45:37 +00001870 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001871 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001872 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001873 self->hash = -1;
1874 if (iterable == NULL)
1875 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001876 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001877}
1878
Raymond Hettingera690a992003-11-16 16:17:49 +00001879static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001880 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001881 0, /* sq_concat */
1882 0, /* sq_repeat */
1883 0, /* sq_item */
1884 0, /* sq_slice */
1885 0, /* sq_ass_item */
1886 0, /* sq_ass_slice */
1887 (objobjproc)set_contains, /* sq_contains */
1888};
1889
1890/* set object ********************************************************/
1891
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001892#ifdef Py_DEBUG
1893static PyObject *test_c_api(PySetObject *so);
1894
1895PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1896All is well if assertions don't fail.");
1897#endif
1898
Raymond Hettingera690a992003-11-16 16:17:49 +00001899static PyMethodDef set_methods[] = {
1900 {"add", (PyCFunction)set_add, METH_O,
1901 add_doc},
1902 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1903 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001904 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001905 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001906 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1907 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001908 {"discard", (PyCFunction)set_discard, METH_O,
1909 discard_doc},
1910 {"difference", (PyCFunction)set_difference, METH_O,
1911 difference_doc},
1912 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1913 difference_update_doc},
1914 {"intersection",(PyCFunction)set_intersection, METH_O,
1915 intersection_doc},
1916 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1917 intersection_update_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00001918 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1919 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001920 {"issubset", (PyCFunction)set_issubset, METH_O,
1921 issubset_doc},
1922 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1923 issuperset_doc},
1924 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1925 pop_doc},
1926 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1927 reduce_doc},
1928 {"remove", (PyCFunction)set_remove, METH_O,
1929 remove_doc},
1930 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1931 symmetric_difference_doc},
1932 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1933 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001934#ifdef Py_DEBUG
1935 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1936 test_c_api_doc},
1937#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001938 {"union", (PyCFunction)set_union, METH_O,
1939 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001940 {"update", (PyCFunction)set_update, METH_O,
1941 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001942 {NULL, NULL} /* sentinel */
1943};
1944
1945static PyNumberMethods set_as_number = {
1946 0, /*nb_add*/
1947 (binaryfunc)set_sub, /*nb_subtract*/
1948 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001949 0, /*nb_remainder*/
1950 0, /*nb_divmod*/
1951 0, /*nb_power*/
1952 0, /*nb_negative*/
1953 0, /*nb_positive*/
1954 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001955 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001956 0, /*nb_invert*/
1957 0, /*nb_lshift*/
1958 0, /*nb_rshift*/
1959 (binaryfunc)set_and, /*nb_and*/
1960 (binaryfunc)set_xor, /*nb_xor*/
1961 (binaryfunc)set_or, /*nb_or*/
Neil Schemenauer16c70752007-09-21 20:19:23 +00001962 0, /*nb_reserved*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001963 0, /*nb_int*/
1964 0, /*nb_long*/
1965 0, /*nb_float*/
1966 0, /*nb_oct*/
1967 0, /*nb_hex*/
1968 0, /*nb_inplace_add*/
1969 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1970 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001971 0, /*nb_inplace_remainder*/
1972 0, /*nb_inplace_power*/
1973 0, /*nb_inplace_lshift*/
1974 0, /*nb_inplace_rshift*/
1975 (binaryfunc)set_iand, /*nb_inplace_and*/
1976 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1977 (binaryfunc)set_ior, /*nb_inplace_or*/
1978};
1979
1980PyDoc_STRVAR(set_doc,
1981"set(iterable) --> set object\n\
1982\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001983Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001984
1985PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001986 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001987 "set", /* tp_name */
1988 sizeof(PySetObject), /* tp_basicsize */
1989 0, /* tp_itemsize */
1990 /* methods */
1991 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001992 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00001993 0, /* tp_getattr */
1994 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001995 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001996 (reprfunc)set_repr, /* tp_repr */
1997 &set_as_number, /* tp_as_number */
1998 &set_as_sequence, /* tp_as_sequence */
1999 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002000 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00002001 0, /* tp_call */
2002 0, /* tp_str */
2003 PyObject_GenericGetAttr, /* tp_getattro */
2004 0, /* tp_setattro */
2005 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002006 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002007 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002008 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002009 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002010 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002011 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002012 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002013 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002014 0, /* tp_iternext */
2015 set_methods, /* tp_methods */
2016 0, /* tp_members */
2017 0, /* tp_getset */
2018 0, /* tp_base */
2019 0, /* tp_dict */
2020 0, /* tp_descr_get */
2021 0, /* tp_descr_set */
2022 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002023 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002024 PyType_GenericAlloc, /* tp_alloc */
2025 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002026 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002027};
2028
2029/* frozenset object ********************************************************/
2030
2031
2032static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002033 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002034 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002035 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002036 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002037 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002038 difference_doc},
2039 {"intersection",(PyCFunction)set_intersection, METH_O,
2040 intersection_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002041 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2042 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002043 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002044 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002045 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002046 issuperset_doc},
2047 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2048 reduce_doc},
2049 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2050 symmetric_difference_doc},
2051 {"union", (PyCFunction)set_union, METH_O,
2052 union_doc},
2053 {NULL, NULL} /* sentinel */
2054};
2055
2056static PyNumberMethods frozenset_as_number = {
2057 0, /*nb_add*/
2058 (binaryfunc)set_sub, /*nb_subtract*/
2059 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002060 0, /*nb_remainder*/
2061 0, /*nb_divmod*/
2062 0, /*nb_power*/
2063 0, /*nb_negative*/
2064 0, /*nb_positive*/
2065 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002066 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002067 0, /*nb_invert*/
2068 0, /*nb_lshift*/
2069 0, /*nb_rshift*/
2070 (binaryfunc)set_and, /*nb_and*/
2071 (binaryfunc)set_xor, /*nb_xor*/
2072 (binaryfunc)set_or, /*nb_or*/
2073};
2074
2075PyDoc_STRVAR(frozenset_doc,
2076"frozenset(iterable) --> frozenset object\n\
2077\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002078Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002079
2080PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002081 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002082 "frozenset", /* tp_name */
2083 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002084 0, /* tp_itemsize */
2085 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002086 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002087 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002088 0, /* tp_getattr */
2089 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002090 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002091 (reprfunc)set_repr, /* tp_repr */
2092 &frozenset_as_number, /* tp_as_number */
2093 &set_as_sequence, /* tp_as_sequence */
2094 0, /* tp_as_mapping */
2095 frozenset_hash, /* tp_hash */
2096 0, /* tp_call */
2097 0, /* tp_str */
2098 PyObject_GenericGetAttr, /* tp_getattro */
2099 0, /* tp_setattro */
2100 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002101 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002102 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002103 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002104 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002105 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002106 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002107 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002108 (getiterfunc)set_iter, /* tp_iter */
2109 0, /* tp_iternext */
2110 frozenset_methods, /* tp_methods */
2111 0, /* tp_members */
2112 0, /* tp_getset */
2113 0, /* tp_base */
2114 0, /* tp_dict */
2115 0, /* tp_descr_get */
2116 0, /* tp_descr_set */
2117 0, /* tp_dictoffset */
2118 0, /* tp_init */
2119 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002120 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002121 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002122};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002123
2124
2125/***** C API functions *************************************************/
2126
2127PyObject *
2128PySet_New(PyObject *iterable)
2129{
2130 return make_new_set(&PySet_Type, iterable);
2131}
2132
2133PyObject *
2134PyFrozenSet_New(PyObject *iterable)
2135{
Christian Heimesfd66e512008-01-29 12:18:50 +00002136 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002137}
2138
Neal Norwitz8c49c822006-03-04 18:41:19 +00002139Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002140PySet_Size(PyObject *anyset)
2141{
2142 if (!PyAnySet_Check(anyset)) {
2143 PyErr_BadInternalCall();
2144 return -1;
2145 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002146 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002147}
2148
2149int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002150PySet_Clear(PyObject *set)
2151{
Christian Heimesfd66e512008-01-29 12:18:50 +00002152 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002153 PyErr_BadInternalCall();
2154 return -1;
2155 }
2156 return set_clear_internal((PySetObject *)set);
2157}
2158
2159int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002160PySet_Contains(PyObject *anyset, PyObject *key)
2161{
2162 if (!PyAnySet_Check(anyset)) {
2163 PyErr_BadInternalCall();
2164 return -1;
2165 }
2166 return set_contains_key((PySetObject *)anyset, key);
2167}
2168
2169int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002170PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002171{
Christian Heimesfd66e512008-01-29 12:18:50 +00002172 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002173 PyErr_BadInternalCall();
2174 return -1;
2175 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002176 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002177}
2178
2179int
Christian Heimesfd66e512008-01-29 12:18:50 +00002180PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002181{
Christian Heimes15ebc882008-02-04 18:48:49 +00002182 if (!PySet_Check(anyset) &&
2183 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002184 PyErr_BadInternalCall();
2185 return -1;
2186 }
Christian Heimesfd66e512008-01-29 12:18:50 +00002187 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002188}
2189
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002190int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002191_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2192{
2193 setentry *entry;
2194
2195 if (!PyAnySet_Check(set)) {
2196 PyErr_BadInternalCall();
2197 return -1;
2198 }
2199 if (set_next((PySetObject *)set, pos, &entry) == 0)
2200 return 0;
2201 *key = entry->key;
2202 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002203 return 1;
2204}
2205
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002206PyObject *
2207PySet_Pop(PyObject *set)
2208{
Christian Heimesfd66e512008-01-29 12:18:50 +00002209 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002210 PyErr_BadInternalCall();
2211 return NULL;
2212 }
2213 return set_pop((PySetObject *)set);
2214}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002215
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002216int
2217_PySet_Update(PyObject *set, PyObject *iterable)
2218{
Christian Heimesfd66e512008-01-29 12:18:50 +00002219 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002220 PyErr_BadInternalCall();
2221 return -1;
2222 }
2223 return set_update_internal((PySetObject *)set, iterable);
2224}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002225
2226#ifdef Py_DEBUG
2227
2228/* Test code to be called with any three element set.
2229 Returns True and original set is restored. */
2230
2231#define assertRaises(call_return_value, exception) \
2232 do { \
2233 assert(call_return_value); \
2234 assert(PyErr_ExceptionMatches(exception)); \
2235 PyErr_Clear(); \
2236 } while(0)
2237
2238static PyObject *
2239test_c_api(PySetObject *so)
2240{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002241 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002242 char *s;
2243 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002244 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002245 PyObject *ob = (PyObject *)so;
Christian Heimesdb967892008-01-31 01:08:32 +00002246 long hash;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002247
2248 /* Verify preconditions and exercise type/size checks */
2249 assert(PyAnySet_Check(ob));
2250 assert(PyAnySet_CheckExact(ob));
2251 assert(!PyFrozenSet_CheckExact(ob));
2252 assert(PySet_Size(ob) == 3);
2253 assert(PySet_GET_SIZE(ob) == 3);
2254
2255 /* Raise TypeError for non-iterable constructor arguments */
2256 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2257 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2258
2259 /* Raise TypeError for unhashable key */
2260 dup = PySet_New(ob);
2261 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2262 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2263 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2264
2265 /* Exercise successful pop, contains, add, and discard */
2266 elem = PySet_Pop(ob);
2267 assert(PySet_Contains(ob, elem) == 0);
2268 assert(PySet_GET_SIZE(ob) == 2);
2269 assert(PySet_Add(ob, elem) == 0);
2270 assert(PySet_Contains(ob, elem) == 1);
2271 assert(PySet_GET_SIZE(ob) == 3);
2272 assert(PySet_Discard(ob, elem) == 1);
2273 assert(PySet_GET_SIZE(ob) == 2);
2274 assert(PySet_Discard(ob, elem) == 0);
2275 assert(PySet_GET_SIZE(ob) == 2);
2276
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002277 /* Exercise clear */
2278 dup2 = PySet_New(dup);
2279 assert(PySet_Clear(dup2) == 0);
2280 assert(PySet_Size(dup2) == 0);
2281 Py_DECREF(dup2);
2282
2283 /* Raise SystemError on clear or update of frozen set */
2284 f = PyFrozenSet_New(dup);
2285 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2286 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Christian Heimes15ebc882008-02-04 18:48:49 +00002287 assert(PySet_Add(f, elem) == 0);
2288 Py_INCREF(f);
2289 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2290 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002291 Py_DECREF(f);
2292
2293 /* Exercise direct iteration */
2294 i = 0, count = 0;
Christian Heimesdb967892008-01-31 01:08:32 +00002295 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
Amaury Forgeot d'Arc39599dc2007-11-22 02:48:12 +00002296 s = PyUnicode_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002297 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2298 count++;
2299 }
2300 assert(count == 3);
2301
2302 /* Exercise updates */
2303 dup2 = PySet_New(NULL);
2304 assert(_PySet_Update(dup2, dup) == 0);
2305 assert(PySet_Size(dup2) == 3);
2306 assert(_PySet_Update(dup2, dup) == 0);
2307 assert(PySet_Size(dup2) == 3);
2308 Py_DECREF(dup2);
2309
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002310 /* Raise SystemError when self argument is not a set or frozenset. */
2311 t = PyTuple_New(0);
2312 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2313 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2314 Py_DECREF(t);
2315
2316 /* Raise SystemError when self argument is not a set. */
2317 f = PyFrozenSet_New(dup);
2318 assert(PySet_Size(f) == 3);
2319 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002320 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2321 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2322 Py_DECREF(f);
2323
2324 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002325 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2326 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002327 assert(PySet_GET_SIZE(ob) == 0);
2328 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2329
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002330 /* Restore the set from the copy using the PyNumber API */
2331 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2332 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002333
2334 /* Verify constructors accept NULL arguments */
2335 f = PySet_New(NULL);
2336 assert(f != NULL);
2337 assert(PySet_GET_SIZE(f) == 0);
2338 Py_DECREF(f);
2339 f = PyFrozenSet_New(NULL);
2340 assert(f != NULL);
2341 assert(PyFrozenSet_CheckExact(f));
2342 assert(PySet_GET_SIZE(f) == 0);
2343 Py_DECREF(f);
2344
2345 Py_DECREF(elem);
2346 Py_DECREF(dup);
2347 Py_RETURN_TRUE;
2348}
2349
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002350#undef assertRaises
2351
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002352#endif