blob: d08ff5f33775370729b0753f91de73c72323541a [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
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000749PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
750Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000751
752static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000753set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000754{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000755 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000756 setentry *entry;
757
758 while (set_next(so, &pos, &entry))
759 Py_VISIT(entry->key);
760 return 0;
761}
762
763static long
764frozenset_hash(PyObject *self)
765{
766 PySetObject *so = (PySetObject *)self;
767 long h, hash = 1927868237L;
768 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000769 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000770
771 if (so->hash != -1)
772 return so->hash;
773
774 hash *= PySet_GET_SIZE(self) + 1;
775 while (set_next(so, &pos, &entry)) {
776 /* Work to increase the bit dispersion for closely spaced hash
777 values. The is important because some use cases have many
778 combinations of a small number of elements with nearby
779 hashes so that many distinct combinations collapse to only
780 a handful of distinct hash values. */
781 h = entry->hash;
782 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
783 }
784 hash = hash * 69069L + 907133923L;
785 if (hash == -1)
786 hash = 590923713L;
787 so->hash = hash;
788 return hash;
789}
790
Raymond Hettingera9d99362005-08-05 00:01:15 +0000791/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793typedef struct {
794 PyObject_HEAD
795 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000796 Py_ssize_t si_used;
797 Py_ssize_t si_pos;
798 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799} setiterobject;
800
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801static void
802setiter_dealloc(setiterobject *si)
803{
804 Py_XDECREF(si->si_set);
805 PyObject_Del(si);
806}
807
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000808static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809setiter_len(setiterobject *si)
810{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000811 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000813 len = si->len;
Christian Heimes217cfd12007-12-02 14:31:20 +0000814 return PyLong_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815}
816
Armin Rigof5b3e362006-02-11 21:32:43 +0000817PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000818
819static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000820 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000821 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822};
823
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000824static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825{
826 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000827 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000828 register setentry *entry;
829 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000830
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000831 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000833 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000834
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000835 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000836 PyErr_SetString(PyExc_RuntimeError,
837 "Set changed size during iteration");
838 si->si_used = -1; /* Make this state sticky */
839 return NULL;
840 }
841
842 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000843 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000844 entry = so->table;
845 mask = so->mask;
846 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847 i++;
848 si->si_pos = i+1;
849 if (i > mask)
850 goto fail;
851 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000852 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853 Py_INCREF(key);
854 return key;
855
856fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000857 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000858 si->si_set = NULL;
859 return NULL;
860}
861
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000862PyTypeObject PySetIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000863 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +0000864 "set_iterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000865 sizeof(setiterobject), /* tp_basicsize */
866 0, /* tp_itemsize */
867 /* methods */
868 (destructor)setiter_dealloc, /* tp_dealloc */
869 0, /* tp_print */
870 0, /* tp_getattr */
871 0, /* tp_setattr */
872 0, /* tp_compare */
873 0, /* tp_repr */
874 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000875 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000876 0, /* tp_as_mapping */
877 0, /* tp_hash */
878 0, /* tp_call */
879 0, /* tp_str */
880 PyObject_GenericGetAttr, /* tp_getattro */
881 0, /* tp_setattro */
882 0, /* tp_as_buffer */
883 Py_TPFLAGS_DEFAULT, /* tp_flags */
884 0, /* tp_doc */
885 0, /* tp_traverse */
886 0, /* tp_clear */
887 0, /* tp_richcompare */
888 0, /* tp_weaklistoffset */
889 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000890 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000891 setiter_methods, /* tp_methods */
892 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000893};
894
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000895static PyObject *
896set_iter(PySetObject *so)
897{
898 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
899 if (si == NULL)
900 return NULL;
901 Py_INCREF(so);
902 si->si_set = so;
903 si->si_used = so->used;
904 si->si_pos = 0;
905 si->len = so->used;
906 return (PyObject *)si;
907}
908
Raymond Hettingerd7946662005-08-01 21:39:29 +0000909static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000910set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000911{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000912 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000913
Christian Heimesaf98da12008-01-27 15:18:18 +0000914 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000915 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000916
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000917 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000918 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000920 long hash;
921 Py_ssize_t dictsize = PyDict_Size(other);
922
923 /* Do one big resize at the start, rather than
924 * incrementally resizing as we insert new keys. Expect
925 * that there will be no (or few) overlapping keys.
926 */
927 if (dictsize == -1)
928 return -1;
929 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
930 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
931 return -1;
932 }
933 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
934 setentry an_entry;
935
936 an_entry.hash = hash;
937 an_entry.key = key;
938 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000939 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000940 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000941 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000942 }
943
Raymond Hettingera38123e2003-11-24 22:18:49 +0000944 it = PyObject_GetIter(other);
945 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000946 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000947
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000948 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000949 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000950 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000951 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000953 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000954 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000955 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000956 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000957 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000958 return -1;
959 return 0;
960}
961
962static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000963set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000964{
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000965 Py_ssize_t i;
966
967 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
968 PyObject *other = PyTuple_GET_ITEM(args, i);
969 if (set_update_internal(so, other) == -1)
970 return NULL;
971 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000972 Py_RETURN_NONE;
973}
974
975PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000976"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000977
978static PyObject *
979make_new_set(PyTypeObject *type, PyObject *iterable)
980{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000981 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000982
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000983 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000984 dummy = PyUnicode_FromString("<dummy key>");
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000985 if (dummy == NULL)
986 return NULL;
987 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000988
989 /* create PySetObject structure */
Christian Heimes2202f872008-02-06 14:31:34 +0000990 if (numfree &&
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000991 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
Christian Heimes2202f872008-02-06 14:31:34 +0000992 so = free_list[--numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000993 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimes90aa7642007-12-19 02:45:37 +0000994 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000995 _Py_NewReference((PyObject *)so);
996 EMPTY_TO_MINSIZE(so);
997 PyObject_GC_Track(so);
998 } else {
999 so = (PySetObject *)type->tp_alloc(type, 0);
1000 if (so == NULL)
1001 return NULL;
1002 /* tp_alloc has already zeroed the structure */
1003 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1004 INIT_NONZERO_SET_SLOTS(so);
1005 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001006
Christian Heimes0ded5b52007-12-10 15:50:56 +00001007 so->lookup = set_lookkey_unicode;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001008 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001009
Raymond Hettingera38123e2003-11-24 22:18:49 +00001010 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001011 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001012 Py_DECREF(so);
1013 return NULL;
1014 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001015 }
1016
Raymond Hettingera690a992003-11-16 16:17:49 +00001017 return (PyObject *)so;
1018}
1019
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001020static PyObject *
1021make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1022{
1023 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1024 if (PyType_IsSubtype(type, &PySet_Type))
1025 type = &PySet_Type;
1026 else
1027 type = &PyFrozenSet_Type;
1028 }
1029 return make_new_set(type, iterable);
1030}
1031
Raymond Hettingerd7946662005-08-01 21:39:29 +00001032/* The empty frozenset is a singleton */
1033static PyObject *emptyfrozenset = NULL;
1034
Raymond Hettingera690a992003-11-16 16:17:49 +00001035static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001036frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001037{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001038 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001039
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001040 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001041 return NULL;
1042
Raymond Hettingera690a992003-11-16 16:17:49 +00001043 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1044 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001045
1046 if (type != &PyFrozenSet_Type)
1047 return make_new_set(type, iterable);
1048
1049 if (iterable != NULL) {
1050 /* frozenset(f) is idempotent */
1051 if (PyFrozenSet_CheckExact(iterable)) {
1052 Py_INCREF(iterable);
1053 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001054 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001055 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001056 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001057 return result;
1058 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001059 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001060 /* The empty frozenset is a singleton */
1061 if (emptyfrozenset == NULL)
1062 emptyfrozenset = make_new_set(type, NULL);
1063 Py_XINCREF(emptyfrozenset);
1064 return emptyfrozenset;
1065}
1066
1067void
1068PySet_Fini(void)
1069{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001070 PySetObject *so;
1071
Christian Heimes2202f872008-02-06 14:31:34 +00001072 while (numfree) {
1073 numfree--;
1074 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001075 PyObject_GC_Del(so);
1076 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001077 Py_CLEAR(dummy);
1078 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001079}
1080
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001081static PyObject *
1082set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1083{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001084 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001085 return NULL;
1086
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001087 return make_new_set(type, NULL);
1088}
1089
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001090/* set_swap_bodies() switches the contents of any two sets by moving their
1091 internal data pointers and, if needed, copying the internal smalltables.
1092 Semantically equivalent to:
1093
1094 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1095
1096 The function always succeeds and it leaves both objects in a stable state.
1097 Useful for creating temporary frozensets from sets for membership testing
1098 in __contains__(), discard(), and remove(). Also useful for operations
1099 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001100 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001101*/
1102
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001103static void
1104set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001105{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001106 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001107 setentry *u;
1108 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1109 setentry tab[PySet_MINSIZE];
1110 long h;
1111
1112 t = a->fill; a->fill = b->fill; b->fill = t;
1113 t = a->used; a->used = b->used; b->used = t;
1114 t = a->mask; a->mask = b->mask; b->mask = t;
1115
1116 u = a->table;
1117 if (a->table == a->smalltable)
1118 u = b->smalltable;
1119 a->table = b->table;
1120 if (b->table == b->smalltable)
1121 a->table = a->smalltable;
1122 b->table = u;
1123
1124 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1125
1126 if (a->table == a->smalltable || b->table == b->smalltable) {
1127 memcpy(tab, a->smalltable, sizeof(tab));
1128 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1129 memcpy(b->smalltable, tab, sizeof(tab));
1130 }
1131
Christian Heimes90aa7642007-12-19 02:45:37 +00001132 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1133 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001134 h = a->hash; a->hash = b->hash; b->hash = h;
1135 } else {
1136 a->hash = -1;
1137 b->hash = -1;
1138 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001139}
1140
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001141static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001142set_copy(PySetObject *so)
1143{
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001144 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001145}
1146
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001147static PyObject *
1148frozenset_copy(PySetObject *so)
1149{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001150 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001151 Py_INCREF(so);
1152 return (PyObject *)so;
1153 }
1154 return set_copy(so);
1155}
1156
Raymond Hettingera690a992003-11-16 16:17:49 +00001157PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1158
1159static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001160set_clear(PySetObject *so)
1161{
1162 set_clear_internal(so);
1163 Py_RETURN_NONE;
1164}
1165
1166PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1167
1168static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001169set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001170{
1171 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001172 PyObject *other;
1173 Py_ssize_t i;
1174
1175 result = (PySetObject *)set_copy(so);
1176 if (result == NULL)
1177 return NULL;
1178
1179 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1180 other = PyTuple_GET_ITEM(args, i);
1181 if ((PyObject *)so == other)
1182 return (PyObject *)result;
1183 if (set_update_internal(result, other) == -1) {
1184 Py_DECREF(result);
1185 return NULL;
1186 }
1187 }
1188 return (PyObject *)result;
1189}
1190
1191PyDoc_STRVAR(union_doc,
1192 "Return the union of sets as a new set.\n\
1193\n\
1194(i.e. all elements that are in either set.)");
1195
1196static PyObject *
1197set_or(PySetObject *so, PyObject *other)
1198{
1199 PySetObject *result;
1200
1201 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1202 Py_INCREF(Py_NotImplemented);
1203 return Py_NotImplemented;
1204 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001205
1206 result = (PySetObject *)set_copy(so);
1207 if (result == NULL)
1208 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001209 if ((PyObject *)so == other)
1210 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001211 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001212 Py_DECREF(result);
1213 return NULL;
1214 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001215 return (PyObject *)result;
1216}
1217
Raymond Hettingera690a992003-11-16 16:17:49 +00001218static PyObject *
1219set_ior(PySetObject *so, PyObject *other)
1220{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001221 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001222 Py_INCREF(Py_NotImplemented);
1223 return Py_NotImplemented;
1224 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001225 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001226 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001227 Py_INCREF(so);
1228 return (PyObject *)so;
1229}
1230
1231static PyObject *
1232set_intersection(PySetObject *so, PyObject *other)
1233{
1234 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001235 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001236
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001237 if ((PyObject *)so == other)
1238 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001239
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001240 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001241 if (result == NULL)
1242 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001243
Christian Heimesaf98da12008-01-27 15:18:18 +00001244 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001245 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001246 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001247
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001248 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001249 tmp = (PyObject *)so;
1250 so = (PySetObject *)other;
1251 other = tmp;
1252 }
1253
Raymond Hettingerc991db22005-08-11 07:58:45 +00001254 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001255 int rv = set_contains_entry(so, entry);
1256 if (rv == -1) {
1257 Py_DECREF(result);
1258 return NULL;
1259 }
1260 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001261 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001262 Py_DECREF(result);
1263 return NULL;
1264 }
1265 }
1266 }
1267 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001268 }
1269
Raymond Hettingera690a992003-11-16 16:17:49 +00001270 it = PyObject_GetIter(other);
1271 if (it == NULL) {
1272 Py_DECREF(result);
1273 return NULL;
1274 }
1275
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001276 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001277 int rv;
1278 setentry entry;
1279 long hash = PyObject_Hash(key);
1280
1281 if (hash == -1) {
1282 Py_DECREF(it);
1283 Py_DECREF(result);
1284 Py_DECREF(key);
1285 return NULL;
1286 }
1287 entry.hash = hash;
1288 entry.key = key;
1289 rv = set_contains_entry(so, &entry);
1290 if (rv == -1) {
1291 Py_DECREF(it);
1292 Py_DECREF(result);
1293 Py_DECREF(key);
1294 return NULL;
1295 }
1296 if (rv) {
1297 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001298 Py_DECREF(it);
1299 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001300 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001301 return NULL;
1302 }
1303 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001304 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001305 }
1306 Py_DECREF(it);
1307 if (PyErr_Occurred()) {
1308 Py_DECREF(result);
1309 return NULL;
1310 }
1311 return (PyObject *)result;
1312}
1313
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001314static PyObject *
1315set_intersection_multi(PySetObject *so, PyObject *args)
1316{
1317 Py_ssize_t i;
1318 PyObject *result = (PyObject *)so;
1319
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001320 if (PyTuple_GET_SIZE(args) == 0)
1321 return set_copy(so);
1322
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001323 Py_INCREF(so);
1324 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1325 PyObject *other = PyTuple_GET_ITEM(args, i);
1326 PyObject *newresult = set_intersection((PySetObject *)result, other);
1327 if (newresult == NULL) {
1328 Py_DECREF(result);
1329 return NULL;
1330 }
1331 Py_DECREF(result);
1332 result = newresult;
1333 }
1334 return result;
1335}
1336
Raymond Hettingera690a992003-11-16 16:17:49 +00001337PyDoc_STRVAR(intersection_doc,
1338"Return the intersection of two sets as a new set.\n\
1339\n\
1340(i.e. all elements that are in both sets.)");
1341
1342static PyObject *
1343set_intersection_update(PySetObject *so, PyObject *other)
1344{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001345 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001346
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001347 tmp = set_intersection(so, other);
1348 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001349 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001350 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001351 Py_DECREF(tmp);
1352 Py_RETURN_NONE;
1353}
1354
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001355static PyObject *
1356set_intersection_update_multi(PySetObject *so, PyObject *args)
1357{
1358 PyObject *tmp;
1359
1360 tmp = set_intersection_multi(so, args);
1361 if (tmp == NULL)
1362 return NULL;
1363 set_swap_bodies(so, (PySetObject *)tmp);
1364 Py_DECREF(tmp);
1365 Py_RETURN_NONE;
1366}
1367
Raymond Hettingera690a992003-11-16 16:17:49 +00001368PyDoc_STRVAR(intersection_update_doc,
1369"Update a set with the intersection of itself and another.");
1370
1371static PyObject *
1372set_and(PySetObject *so, PyObject *other)
1373{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001374 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001375 Py_INCREF(Py_NotImplemented);
1376 return Py_NotImplemented;
1377 }
1378 return set_intersection(so, other);
1379}
1380
1381static PyObject *
1382set_iand(PySetObject *so, PyObject *other)
1383{
1384 PyObject *result;
1385
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001386 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001387 Py_INCREF(Py_NotImplemented);
1388 return Py_NotImplemented;
1389 }
1390 result = set_intersection_update(so, other);
1391 if (result == NULL)
1392 return NULL;
1393 Py_DECREF(result);
1394 Py_INCREF(so);
1395 return (PyObject *)so;
1396}
1397
Guido van Rossum58da9312007-11-10 23:39:45 +00001398static PyObject *
1399set_isdisjoint(PySetObject *so, PyObject *other)
1400{
1401 PyObject *key, *it, *tmp;
1402
1403 if ((PyObject *)so == other) {
1404 if (PySet_GET_SIZE(so) == 0)
1405 Py_RETURN_TRUE;
1406 else
1407 Py_RETURN_FALSE;
1408 }
1409
1410 if (PyAnySet_CheckExact(other)) {
1411 Py_ssize_t pos = 0;
1412 setentry *entry;
1413
1414 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1415 tmp = (PyObject *)so;
1416 so = (PySetObject *)other;
1417 other = tmp;
1418 }
1419 while (set_next((PySetObject *)other, &pos, &entry)) {
1420 int rv = set_contains_entry(so, entry);
1421 if (rv == -1)
1422 return NULL;
1423 if (rv)
1424 Py_RETURN_FALSE;
1425 }
1426 Py_RETURN_TRUE;
1427 }
1428
1429 it = PyObject_GetIter(other);
1430 if (it == NULL)
1431 return NULL;
1432
1433 while ((key = PyIter_Next(it)) != NULL) {
1434 int rv;
1435 setentry entry;
Christian Heimes0ded5b52007-12-10 15:50:56 +00001436 long hash = PyObject_Hash(key);;
Guido van Rossum58da9312007-11-10 23:39:45 +00001437
1438 if (hash == -1) {
1439 Py_DECREF(key);
1440 Py_DECREF(it);
1441 return NULL;
1442 }
1443 entry.hash = hash;
1444 entry.key = key;
1445 rv = set_contains_entry(so, &entry);
1446 Py_DECREF(key);
1447 if (rv == -1) {
1448 Py_DECREF(it);
1449 return NULL;
1450 }
1451 if (rv) {
1452 Py_DECREF(it);
1453 Py_RETURN_FALSE;
1454 }
1455 }
1456 Py_DECREF(it);
1457 if (PyErr_Occurred())
1458 return NULL;
1459 Py_RETURN_TRUE;
1460}
1461
1462PyDoc_STRVAR(isdisjoint_doc,
1463"Return True if two sets have a null intersection.");
1464
Neal Norwitz6576bd82005-11-13 18:41:28 +00001465static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001466set_difference_update_internal(PySetObject *so, PyObject *other)
1467{
1468 if ((PyObject *)so == other)
1469 return set_clear_internal(so);
1470
Christian Heimesaf98da12008-01-27 15:18:18 +00001471 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001472 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001473 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001474
1475 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001476 if (set_discard_entry(so, entry) == -1)
1477 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001478 } else {
1479 PyObject *key, *it;
1480 it = PyObject_GetIter(other);
1481 if (it == NULL)
1482 return -1;
1483
1484 while ((key = PyIter_Next(it)) != NULL) {
1485 if (set_discard_key(so, key) == -1) {
1486 Py_DECREF(it);
1487 Py_DECREF(key);
1488 return -1;
1489 }
1490 Py_DECREF(key);
1491 }
1492 Py_DECREF(it);
1493 if (PyErr_Occurred())
1494 return -1;
1495 }
1496 /* If more than 1/5 are dummies, then resize them away. */
1497 if ((so->fill - so->used) * 5 < so->mask)
1498 return 0;
1499 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1500}
1501
Raymond Hettingera690a992003-11-16 16:17:49 +00001502static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001503set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001504{
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001505 Py_ssize_t i;
1506
1507 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1508 PyObject *other = PyTuple_GET_ITEM(args, i);
1509 if (set_difference_update_internal(so, other) == -1)
1510 return NULL;
1511 }
1512 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001513}
1514
1515PyDoc_STRVAR(difference_update_doc,
1516"Remove all elements of another set from this set.");
1517
1518static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001519set_difference(PySetObject *so, PyObject *other)
1520{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001521 PyObject *result;
1522 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001523 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001524
Christian Heimesaf98da12008-01-27 15:18:18 +00001525 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001526 result = set_copy(so);
1527 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001528 return NULL;
1529 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001530 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001531 Py_DECREF(result);
1532 return NULL;
1533 }
1534
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001535 result = make_new_set_basetype(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001536 if (result == NULL)
1537 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001538
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001539 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001540 while (set_next(so, &pos, &entry)) {
1541 setentry entrycopy;
1542 entrycopy.hash = entry->hash;
1543 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001544 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001545 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1546 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001547 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001548 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001549 }
1550 }
1551 return result;
1552 }
1553
Raymond Hettingerc991db22005-08-11 07:58:45 +00001554 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001555 int rv = set_contains_entry((PySetObject *)other, entry);
1556 if (rv == -1) {
1557 Py_DECREF(result);
1558 return NULL;
1559 }
1560 if (!rv) {
1561 if (set_add_entry((PySetObject *)result, entry) == -1) {
1562 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001563 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001564 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001565 }
1566 }
1567 return result;
1568}
1569
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001570static PyObject *
1571set_difference_multi(PySetObject *so, PyObject *args)
1572{
1573 Py_ssize_t i;
1574 PyObject *result, *other;
1575
1576 if (PyTuple_GET_SIZE(args) == 0)
1577 return set_copy(so);
1578
1579 other = PyTuple_GET_ITEM(args, 0);
1580 result = set_difference(so, other);
1581 if (result == NULL)
1582 return NULL;
1583
1584 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1585 other = PyTuple_GET_ITEM(args, i);
1586 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1587 Py_DECREF(result);
1588 return NULL;
1589 }
1590 }
1591 return result;
1592}
1593
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001594PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001595"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001596\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001597(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001598static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001599set_sub(PySetObject *so, PyObject *other)
1600{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001601 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001602 Py_INCREF(Py_NotImplemented);
1603 return Py_NotImplemented;
1604 }
1605 return set_difference(so, other);
1606}
1607
1608static PyObject *
1609set_isub(PySetObject *so, PyObject *other)
1610{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001611 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001612 Py_INCREF(Py_NotImplemented);
1613 return Py_NotImplemented;
1614 }
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001615 if (set_difference_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001616 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001617 Py_INCREF(so);
1618 return (PyObject *)so;
1619}
1620
1621static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001622set_symmetric_difference_update(PySetObject *so, PyObject *other)
1623{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001624 PySetObject *otherset;
1625 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001626 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001627 setentry *entry;
1628
1629 if ((PyObject *)so == other)
1630 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001631
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001632 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001633 PyObject *value;
1634 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001635 long hash;
1636 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001637 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001638
Thomas Wouters89f507f2006-12-13 04:49:30 +00001639 an_entry.hash = hash;
1640 an_entry.key = key;
1641 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001642 if (rv == -1)
1643 return NULL;
1644 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001645 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001646 return NULL;
1647 }
1648 }
1649 Py_RETURN_NONE;
1650 }
1651
Christian Heimesaf98da12008-01-27 15:18:18 +00001652 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001653 Py_INCREF(other);
1654 otherset = (PySetObject *)other;
1655 } else {
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001656 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001657 if (otherset == NULL)
1658 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001659 }
1660
Raymond Hettingerc991db22005-08-11 07:58:45 +00001661 while (set_next(otherset, &pos, &entry)) {
1662 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001663 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001664 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001665 return NULL;
1666 }
1667 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001668 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001669 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001670 return NULL;
1671 }
1672 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001673 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001674 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001675 Py_RETURN_NONE;
1676}
1677
1678PyDoc_STRVAR(symmetric_difference_update_doc,
1679"Update a set with the symmetric difference of itself and another.");
1680
1681static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001682set_symmetric_difference(PySetObject *so, PyObject *other)
1683{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001684 PyObject *rv;
1685 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001686
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001687 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001688 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001689 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001690 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1691 if (rv == NULL)
1692 return NULL;
1693 Py_DECREF(rv);
1694 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001695}
1696
1697PyDoc_STRVAR(symmetric_difference_doc,
1698"Return the symmetric difference of two sets as a new set.\n\
1699\n\
1700(i.e. all elements that are in exactly one of the sets.)");
1701
1702static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001703set_xor(PySetObject *so, PyObject *other)
1704{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001705 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001706 Py_INCREF(Py_NotImplemented);
1707 return Py_NotImplemented;
1708 }
1709 return set_symmetric_difference(so, other);
1710}
1711
1712static PyObject *
1713set_ixor(PySetObject *so, PyObject *other)
1714{
1715 PyObject *result;
1716
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001717 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001718 Py_INCREF(Py_NotImplemented);
1719 return Py_NotImplemented;
1720 }
1721 result = set_symmetric_difference_update(so, other);
1722 if (result == NULL)
1723 return NULL;
1724 Py_DECREF(result);
1725 Py_INCREF(so);
1726 return (PyObject *)so;
1727}
1728
1729static PyObject *
1730set_issubset(PySetObject *so, PyObject *other)
1731{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001732 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001733 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001734
Christian Heimesaf98da12008-01-27 15:18:18 +00001735 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001736 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001737 tmp = make_new_set(&PySet_Type, other);
1738 if (tmp == NULL)
1739 return NULL;
1740 result = set_issubset(so, tmp);
1741 Py_DECREF(tmp);
1742 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001743 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001744 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001745 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001746
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001747 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001748 int rv = set_contains_entry((PySetObject *)other, entry);
1749 if (rv == -1)
1750 return NULL;
1751 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001752 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001753 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001754 Py_RETURN_TRUE;
1755}
1756
1757PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1758
1759static PyObject *
1760set_issuperset(PySetObject *so, PyObject *other)
1761{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001762 PyObject *tmp, *result;
1763
Christian Heimesaf98da12008-01-27 15:18:18 +00001764 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001765 tmp = make_new_set(&PySet_Type, other);
1766 if (tmp == NULL)
1767 return NULL;
1768 result = set_issuperset(so, tmp);
1769 Py_DECREF(tmp);
1770 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001771 }
1772 return set_issubset((PySetObject *)other, (PyObject *)so);
1773}
1774
1775PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1776
Raymond Hettingera690a992003-11-16 16:17:49 +00001777static PyObject *
1778set_richcompare(PySetObject *v, PyObject *w, int op)
1779{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001780 PyObject *r1, *r2;
1781
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001782 if(!PyAnySet_Check(w)) {
Guido van Rossum10ab4ae2007-08-23 23:57:24 +00001783 Py_INCREF(Py_NotImplemented);
1784 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001785 }
1786 switch (op) {
1787 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001788 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001789 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001790 if (v->hash != -1 &&
1791 ((PySetObject *)w)->hash != -1 &&
1792 v->hash != ((PySetObject *)w)->hash)
1793 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001794 return set_issubset(v, w);
1795 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001796 r1 = set_richcompare(v, w, Py_EQ);
1797 if (r1 == NULL)
1798 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001799 r2 = PyBool_FromLong(PyObject_Not(r1));
1800 Py_DECREF(r1);
1801 return r2;
1802 case Py_LE:
1803 return set_issubset(v, w);
1804 case Py_GE:
1805 return set_issuperset(v, w);
1806 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001807 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001808 Py_RETURN_FALSE;
1809 return set_issubset(v, w);
1810 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001811 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001812 Py_RETURN_FALSE;
1813 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001814 }
1815 Py_INCREF(Py_NotImplemented);
1816 return Py_NotImplemented;
1817}
1818
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001819static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001820set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001821{
1822 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1823 return -1;
1824}
1825
Raymond Hettingera690a992003-11-16 16:17:49 +00001826static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001827set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001828{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001829 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001830 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001831 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001832}
1833
1834PyDoc_STRVAR(add_doc,
1835"Add an element to a set.\n\
1836\n\
1837This has no effect if the element is already present.");
1838
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001839static int
1840set_contains(PySetObject *so, PyObject *key)
1841{
1842 PyObject *tmpkey;
1843 int rv;
1844
1845 rv = set_contains_key(so, key);
1846 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001847 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001848 return -1;
1849 PyErr_Clear();
1850 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1851 if (tmpkey == NULL)
1852 return -1;
1853 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1854 rv = set_contains(so, tmpkey);
1855 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1856 Py_DECREF(tmpkey);
1857 }
1858 return rv;
1859}
1860
1861static PyObject *
1862set_direct_contains(PySetObject *so, PyObject *key)
1863{
1864 long result;
1865
1866 result = set_contains(so, key);
1867 if (result == -1)
1868 return NULL;
1869 return PyBool_FromLong(result);
1870}
1871
1872PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1873
Raymond Hettingera690a992003-11-16 16:17:49 +00001874static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001875set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001876{
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001877 PyObject *tmpkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001878 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001879
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001880 rv = set_discard_key(so, key);
1881 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001882 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001883 return NULL;
1884 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001885 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1886 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001887 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001888 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001889 rv = set_discard_key(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001890 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001891 Py_DECREF(tmpkey);
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001892 if (rv == -1)
1893 return NULL;
1894 }
1895
1896 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001897 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001898 return NULL;
1899 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001900 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001901}
1902
1903PyDoc_STRVAR(remove_doc,
1904"Remove an element from a set; it must be a member.\n\
1905\n\
1906If the element is not a member, raise a KeyError.");
1907
1908static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001909set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001910{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001911 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001912 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001913
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001914 rv = set_discard_key(so, key);
1915 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001916 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001917 return NULL;
1918 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001919 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1920 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001921 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001922 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001923 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001924 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001925 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001926 return result;
1927 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001928 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001929}
1930
1931PyDoc_STRVAR(discard_doc,
1932"Remove an element from a set if it is a member.\n\
1933\n\
1934If the element is not a member, do nothing.");
1935
1936static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001937set_reduce(PySetObject *so)
1938{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001939 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001940
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001941 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001942 if (keys == NULL)
1943 goto done;
1944 args = PyTuple_Pack(1, keys);
1945 if (args == NULL)
1946 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001947 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1948 if (dict == NULL) {
1949 PyErr_Clear();
1950 dict = Py_None;
1951 Py_INCREF(dict);
1952 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001953 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001954done:
1955 Py_XDECREF(args);
1956 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001957 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001958 return result;
1959}
1960
1961PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1962
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001963static PyObject *
1964set_sizeof(PySetObject *so)
1965{
1966 Py_ssize_t res;
1967
1968 res = sizeof(PySetObject);
1969 if (so->table != so->smalltable)
1970 res = res + (so->mask + 1) * sizeof(setentry);
1971 return PyLong_FromSsize_t(res);
1972}
1973
1974PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001975static int
1976set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1977{
1978 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001979
1980 if (!PyAnySet_Check(self))
1981 return -1;
Christian Heimes90aa7642007-12-19 02:45:37 +00001982 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001983 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001984 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001985 self->hash = -1;
1986 if (iterable == NULL)
1987 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001988 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001989}
1990
Raymond Hettingera690a992003-11-16 16:17:49 +00001991static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001992 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001993 0, /* sq_concat */
1994 0, /* sq_repeat */
1995 0, /* sq_item */
1996 0, /* sq_slice */
1997 0, /* sq_ass_item */
1998 0, /* sq_ass_slice */
1999 (objobjproc)set_contains, /* sq_contains */
2000};
2001
2002/* set object ********************************************************/
2003
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002004#ifdef Py_DEBUG
2005static PyObject *test_c_api(PySetObject *so);
2006
2007PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2008All is well if assertions don't fail.");
2009#endif
2010
Raymond Hettingera690a992003-11-16 16:17:49 +00002011static PyMethodDef set_methods[] = {
2012 {"add", (PyCFunction)set_add, METH_O,
2013 add_doc},
2014 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2015 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00002016 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002017 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002018 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2019 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002020 {"discard", (PyCFunction)set_discard, METH_O,
2021 discard_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002022 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002023 difference_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002024 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002025 difference_update_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002026 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002027 intersection_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002028 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002029 intersection_update_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002030 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2031 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002032 {"issubset", (PyCFunction)set_issubset, METH_O,
2033 issubset_doc},
2034 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2035 issuperset_doc},
2036 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2037 pop_doc},
2038 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2039 reduce_doc},
2040 {"remove", (PyCFunction)set_remove, METH_O,
2041 remove_doc},
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002042 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2043 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002044 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2045 symmetric_difference_doc},
2046 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2047 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002048#ifdef Py_DEBUG
2049 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2050 test_c_api_doc},
2051#endif
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002052 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002053 union_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002054 {"update", (PyCFunction)set_update, METH_VARARGS,
Raymond Hettingera38123e2003-11-24 22:18:49 +00002055 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002056 {NULL, NULL} /* sentinel */
2057};
2058
2059static PyNumberMethods set_as_number = {
2060 0, /*nb_add*/
2061 (binaryfunc)set_sub, /*nb_subtract*/
2062 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002063 0, /*nb_remainder*/
2064 0, /*nb_divmod*/
2065 0, /*nb_power*/
2066 0, /*nb_negative*/
2067 0, /*nb_positive*/
2068 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002069 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002070 0, /*nb_invert*/
2071 0, /*nb_lshift*/
2072 0, /*nb_rshift*/
2073 (binaryfunc)set_and, /*nb_and*/
2074 (binaryfunc)set_xor, /*nb_xor*/
2075 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002076 0, /*nb_int*/
2077 0, /*nb_long*/
2078 0, /*nb_float*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002079 0, /*nb_inplace_add*/
2080 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2081 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002082 0, /*nb_inplace_remainder*/
2083 0, /*nb_inplace_power*/
2084 0, /*nb_inplace_lshift*/
2085 0, /*nb_inplace_rshift*/
2086 (binaryfunc)set_iand, /*nb_inplace_and*/
2087 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2088 (binaryfunc)set_ior, /*nb_inplace_or*/
2089};
2090
2091PyDoc_STRVAR(set_doc,
2092"set(iterable) --> set object\n\
2093\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002094Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002095
2096PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002097 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002098 "set", /* tp_name */
2099 sizeof(PySetObject), /* tp_basicsize */
2100 0, /* tp_itemsize */
2101 /* methods */
2102 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002103 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002104 0, /* tp_getattr */
2105 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002106 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002107 (reprfunc)set_repr, /* tp_repr */
2108 &set_as_number, /* tp_as_number */
2109 &set_as_sequence, /* tp_as_sequence */
2110 0, /* tp_as_mapping */
Nick Coghland1abd252008-07-15 15:46:38 +00002111 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00002112 0, /* tp_call */
2113 0, /* tp_str */
2114 PyObject_GenericGetAttr, /* tp_getattro */
2115 0, /* tp_setattro */
2116 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002117 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002118 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002119 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002120 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002121 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002122 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002123 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002124 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002125 0, /* tp_iternext */
2126 set_methods, /* tp_methods */
2127 0, /* tp_members */
2128 0, /* tp_getset */
2129 0, /* tp_base */
2130 0, /* tp_dict */
2131 0, /* tp_descr_get */
2132 0, /* tp_descr_set */
2133 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002134 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002135 PyType_GenericAlloc, /* tp_alloc */
2136 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002137 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002138};
2139
2140/* frozenset object ********************************************************/
2141
2142
2143static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002144 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002145 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002146 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002147 copy_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002148 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002149 difference_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002150 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002151 intersection_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002152 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2153 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002154 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002155 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002156 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002157 issuperset_doc},
2158 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2159 reduce_doc},
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002160 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2161 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002162 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2163 symmetric_difference_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002164 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002165 union_doc},
2166 {NULL, NULL} /* sentinel */
2167};
2168
2169static PyNumberMethods frozenset_as_number = {
2170 0, /*nb_add*/
2171 (binaryfunc)set_sub, /*nb_subtract*/
2172 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002173 0, /*nb_remainder*/
2174 0, /*nb_divmod*/
2175 0, /*nb_power*/
2176 0, /*nb_negative*/
2177 0, /*nb_positive*/
2178 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002179 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002180 0, /*nb_invert*/
2181 0, /*nb_lshift*/
2182 0, /*nb_rshift*/
2183 (binaryfunc)set_and, /*nb_and*/
2184 (binaryfunc)set_xor, /*nb_xor*/
2185 (binaryfunc)set_or, /*nb_or*/
2186};
2187
2188PyDoc_STRVAR(frozenset_doc,
2189"frozenset(iterable) --> frozenset object\n\
2190\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002191Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002192
2193PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002194 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002195 "frozenset", /* tp_name */
2196 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002197 0, /* tp_itemsize */
2198 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002199 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002200 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002201 0, /* tp_getattr */
2202 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002203 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002204 (reprfunc)set_repr, /* tp_repr */
2205 &frozenset_as_number, /* tp_as_number */
2206 &set_as_sequence, /* tp_as_sequence */
2207 0, /* tp_as_mapping */
2208 frozenset_hash, /* tp_hash */
2209 0, /* tp_call */
2210 0, /* tp_str */
2211 PyObject_GenericGetAttr, /* tp_getattro */
2212 0, /* tp_setattro */
2213 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002214 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002215 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002216 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002217 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002218 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002219 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002220 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002221 (getiterfunc)set_iter, /* tp_iter */
2222 0, /* tp_iternext */
2223 frozenset_methods, /* tp_methods */
2224 0, /* tp_members */
2225 0, /* tp_getset */
2226 0, /* tp_base */
2227 0, /* tp_dict */
2228 0, /* tp_descr_get */
2229 0, /* tp_descr_set */
2230 0, /* tp_dictoffset */
2231 0, /* tp_init */
2232 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002233 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002234 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002235};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002236
2237
2238/***** C API functions *************************************************/
2239
2240PyObject *
2241PySet_New(PyObject *iterable)
2242{
2243 return make_new_set(&PySet_Type, iterable);
2244}
2245
2246PyObject *
2247PyFrozenSet_New(PyObject *iterable)
2248{
Christian Heimesfd66e512008-01-29 12:18:50 +00002249 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002250}
2251
Neal Norwitz8c49c822006-03-04 18:41:19 +00002252Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002253PySet_Size(PyObject *anyset)
2254{
2255 if (!PyAnySet_Check(anyset)) {
2256 PyErr_BadInternalCall();
2257 return -1;
2258 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002259 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002260}
2261
2262int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002263PySet_Clear(PyObject *set)
2264{
Christian Heimesfd66e512008-01-29 12:18:50 +00002265 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002266 PyErr_BadInternalCall();
2267 return -1;
2268 }
2269 return set_clear_internal((PySetObject *)set);
2270}
2271
2272int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002273PySet_Contains(PyObject *anyset, PyObject *key)
2274{
2275 if (!PyAnySet_Check(anyset)) {
2276 PyErr_BadInternalCall();
2277 return -1;
2278 }
2279 return set_contains_key((PySetObject *)anyset, key);
2280}
2281
2282int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002283PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002284{
Christian Heimesfd66e512008-01-29 12:18:50 +00002285 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002286 PyErr_BadInternalCall();
2287 return -1;
2288 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002289 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002290}
2291
2292int
Christian Heimesfd66e512008-01-29 12:18:50 +00002293PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002294{
Christian Heimes15ebc882008-02-04 18:48:49 +00002295 if (!PySet_Check(anyset) &&
2296 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002297 PyErr_BadInternalCall();
2298 return -1;
2299 }
Christian Heimesfd66e512008-01-29 12:18:50 +00002300 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002301}
2302
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002303int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002304_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2305{
2306 setentry *entry;
2307
2308 if (!PyAnySet_Check(set)) {
2309 PyErr_BadInternalCall();
2310 return -1;
2311 }
2312 if (set_next((PySetObject *)set, pos, &entry) == 0)
2313 return 0;
2314 *key = entry->key;
2315 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002316 return 1;
2317}
2318
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002319PyObject *
2320PySet_Pop(PyObject *set)
2321{
Christian Heimesfd66e512008-01-29 12:18:50 +00002322 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002323 PyErr_BadInternalCall();
2324 return NULL;
2325 }
2326 return set_pop((PySetObject *)set);
2327}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002328
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002329int
2330_PySet_Update(PyObject *set, PyObject *iterable)
2331{
Christian Heimesfd66e512008-01-29 12:18:50 +00002332 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002333 PyErr_BadInternalCall();
2334 return -1;
2335 }
2336 return set_update_internal((PySetObject *)set, iterable);
2337}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002338
2339#ifdef Py_DEBUG
2340
2341/* Test code to be called with any three element set.
2342 Returns True and original set is restored. */
2343
2344#define assertRaises(call_return_value, exception) \
2345 do { \
2346 assert(call_return_value); \
2347 assert(PyErr_ExceptionMatches(exception)); \
2348 PyErr_Clear(); \
2349 } while(0)
2350
2351static PyObject *
2352test_c_api(PySetObject *so)
2353{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002354 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002355 char *s;
2356 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002357 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002358 PyObject *ob = (PyObject *)so;
Christian Heimesdb967892008-01-31 01:08:32 +00002359 long hash;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002360
2361 /* Verify preconditions and exercise type/size checks */
2362 assert(PyAnySet_Check(ob));
2363 assert(PyAnySet_CheckExact(ob));
2364 assert(!PyFrozenSet_CheckExact(ob));
2365 assert(PySet_Size(ob) == 3);
2366 assert(PySet_GET_SIZE(ob) == 3);
2367
2368 /* Raise TypeError for non-iterable constructor arguments */
2369 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2370 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2371
2372 /* Raise TypeError for unhashable key */
2373 dup = PySet_New(ob);
2374 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2375 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2376 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2377
2378 /* Exercise successful pop, contains, add, and discard */
2379 elem = PySet_Pop(ob);
2380 assert(PySet_Contains(ob, elem) == 0);
2381 assert(PySet_GET_SIZE(ob) == 2);
2382 assert(PySet_Add(ob, elem) == 0);
2383 assert(PySet_Contains(ob, elem) == 1);
2384 assert(PySet_GET_SIZE(ob) == 3);
2385 assert(PySet_Discard(ob, elem) == 1);
2386 assert(PySet_GET_SIZE(ob) == 2);
2387 assert(PySet_Discard(ob, elem) == 0);
2388 assert(PySet_GET_SIZE(ob) == 2);
2389
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002390 /* Exercise clear */
2391 dup2 = PySet_New(dup);
2392 assert(PySet_Clear(dup2) == 0);
2393 assert(PySet_Size(dup2) == 0);
2394 Py_DECREF(dup2);
2395
2396 /* Raise SystemError on clear or update of frozen set */
2397 f = PyFrozenSet_New(dup);
2398 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2399 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Christian Heimes15ebc882008-02-04 18:48:49 +00002400 assert(PySet_Add(f, elem) == 0);
2401 Py_INCREF(f);
2402 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2403 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002404 Py_DECREF(f);
2405
2406 /* Exercise direct iteration */
2407 i = 0, count = 0;
Christian Heimesdb967892008-01-31 01:08:32 +00002408 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002409 s = _PyUnicode_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002410 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2411 count++;
2412 }
2413 assert(count == 3);
2414
2415 /* Exercise updates */
2416 dup2 = PySet_New(NULL);
2417 assert(_PySet_Update(dup2, dup) == 0);
2418 assert(PySet_Size(dup2) == 3);
2419 assert(_PySet_Update(dup2, dup) == 0);
2420 assert(PySet_Size(dup2) == 3);
2421 Py_DECREF(dup2);
2422
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002423 /* Raise SystemError when self argument is not a set or frozenset. */
2424 t = PyTuple_New(0);
2425 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2426 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2427 Py_DECREF(t);
2428
2429 /* Raise SystemError when self argument is not a set. */
2430 f = PyFrozenSet_New(dup);
2431 assert(PySet_Size(f) == 3);
2432 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002433 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2434 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2435 Py_DECREF(f);
2436
2437 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002438 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2439 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002440 assert(PySet_GET_SIZE(ob) == 0);
2441 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2442
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002443 /* Restore the set from the copy using the PyNumber API */
2444 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2445 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002446
2447 /* Verify constructors accept NULL arguments */
2448 f = PySet_New(NULL);
2449 assert(f != NULL);
2450 assert(PySet_GET_SIZE(f) == 0);
2451 Py_DECREF(f);
2452 f = PyFrozenSet_New(NULL);
2453 assert(f != NULL);
2454 assert(PyFrozenSet_CheckExact(f));
2455 assert(PySet_GET_SIZE(f) == 0);
2456 Py_DECREF(f);
2457
2458 Py_DECREF(elem);
2459 Py_DECREF(dup);
2460 Py_RETURN_TRUE;
2461}
2462
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002463#undef assertRaises
2464
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002465#endif