blob: d24e1af0d2bd757672b3fc6042b94851e295ff65 [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 Hettingerd7946662005-08-01 21:39:29 +00001020/* The empty frozenset is a singleton */
1021static PyObject *emptyfrozenset = NULL;
1022
Raymond Hettingera690a992003-11-16 16:17:49 +00001023static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001024frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001025{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001026 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001027
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001028 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001029 return NULL;
1030
Raymond Hettingera690a992003-11-16 16:17:49 +00001031 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1032 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001033
1034 if (type != &PyFrozenSet_Type)
1035 return make_new_set(type, iterable);
1036
1037 if (iterable != NULL) {
1038 /* frozenset(f) is idempotent */
1039 if (PyFrozenSet_CheckExact(iterable)) {
1040 Py_INCREF(iterable);
1041 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001042 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001043 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001044 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001045 return result;
1046 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001047 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001048 /* The empty frozenset is a singleton */
1049 if (emptyfrozenset == NULL)
1050 emptyfrozenset = make_new_set(type, NULL);
1051 Py_XINCREF(emptyfrozenset);
1052 return emptyfrozenset;
1053}
1054
1055void
1056PySet_Fini(void)
1057{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001058 PySetObject *so;
1059
Christian Heimes2202f872008-02-06 14:31:34 +00001060 while (numfree) {
1061 numfree--;
1062 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001063 PyObject_GC_Del(so);
1064 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001065 Py_CLEAR(dummy);
1066 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001067}
1068
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001069static PyObject *
1070set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1071{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001072 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001073 return NULL;
1074
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001075 return make_new_set(type, NULL);
1076}
1077
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001078/* set_swap_bodies() switches the contents of any two sets by moving their
1079 internal data pointers and, if needed, copying the internal smalltables.
1080 Semantically equivalent to:
1081
1082 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1083
1084 The function always succeeds and it leaves both objects in a stable state.
1085 Useful for creating temporary frozensets from sets for membership testing
1086 in __contains__(), discard(), and remove(). Also useful for operations
1087 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001088 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001089*/
1090
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001091static void
1092set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001093{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001094 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001095 setentry *u;
1096 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1097 setentry tab[PySet_MINSIZE];
1098 long h;
1099
1100 t = a->fill; a->fill = b->fill; b->fill = t;
1101 t = a->used; a->used = b->used; b->used = t;
1102 t = a->mask; a->mask = b->mask; b->mask = t;
1103
1104 u = a->table;
1105 if (a->table == a->smalltable)
1106 u = b->smalltable;
1107 a->table = b->table;
1108 if (b->table == b->smalltable)
1109 a->table = a->smalltable;
1110 b->table = u;
1111
1112 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1113
1114 if (a->table == a->smalltable || b->table == b->smalltable) {
1115 memcpy(tab, a->smalltable, sizeof(tab));
1116 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1117 memcpy(b->smalltable, tab, sizeof(tab));
1118 }
1119
Christian Heimes90aa7642007-12-19 02:45:37 +00001120 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1121 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001122 h = a->hash; a->hash = b->hash; b->hash = h;
1123 } else {
1124 a->hash = -1;
1125 b->hash = -1;
1126 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001127}
1128
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001129static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001130set_copy(PySetObject *so)
1131{
Christian Heimes90aa7642007-12-19 02:45:37 +00001132 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001133}
1134
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001135static PyObject *
1136frozenset_copy(PySetObject *so)
1137{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001138 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001139 Py_INCREF(so);
1140 return (PyObject *)so;
1141 }
1142 return set_copy(so);
1143}
1144
Raymond Hettingera690a992003-11-16 16:17:49 +00001145PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1146
1147static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001148set_clear(PySetObject *so)
1149{
1150 set_clear_internal(so);
1151 Py_RETURN_NONE;
1152}
1153
1154PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1155
1156static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001157set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001158{
1159 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001160 PyObject *other;
1161 Py_ssize_t i;
1162
1163 result = (PySetObject *)set_copy(so);
1164 if (result == NULL)
1165 return NULL;
1166
1167 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1168 other = PyTuple_GET_ITEM(args, i);
1169 if ((PyObject *)so == other)
1170 return (PyObject *)result;
1171 if (set_update_internal(result, other) == -1) {
1172 Py_DECREF(result);
1173 return NULL;
1174 }
1175 }
1176 return (PyObject *)result;
1177}
1178
1179PyDoc_STRVAR(union_doc,
1180 "Return the union of sets as a new set.\n\
1181\n\
1182(i.e. all elements that are in either set.)");
1183
1184static PyObject *
1185set_or(PySetObject *so, PyObject *other)
1186{
1187 PySetObject *result;
1188
1189 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1190 Py_INCREF(Py_NotImplemented);
1191 return Py_NotImplemented;
1192 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001193
1194 result = (PySetObject *)set_copy(so);
1195 if (result == NULL)
1196 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001197 if ((PyObject *)so == other)
1198 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001199 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001200 Py_DECREF(result);
1201 return NULL;
1202 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001203 return (PyObject *)result;
1204}
1205
Raymond Hettingera690a992003-11-16 16:17:49 +00001206static PyObject *
1207set_ior(PySetObject *so, PyObject *other)
1208{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001209 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001210 Py_INCREF(Py_NotImplemented);
1211 return Py_NotImplemented;
1212 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001213 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001214 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001215 Py_INCREF(so);
1216 return (PyObject *)so;
1217}
1218
1219static PyObject *
1220set_intersection(PySetObject *so, PyObject *other)
1221{
1222 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001223 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001224
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001225 if ((PyObject *)so == other)
1226 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001227
Christian Heimes90aa7642007-12-19 02:45:37 +00001228 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001229 if (result == NULL)
1230 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001231
Christian Heimesaf98da12008-01-27 15:18:18 +00001232 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001233 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001234 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001235
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001236 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001237 tmp = (PyObject *)so;
1238 so = (PySetObject *)other;
1239 other = tmp;
1240 }
1241
Raymond Hettingerc991db22005-08-11 07:58:45 +00001242 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001243 int rv = set_contains_entry(so, entry);
1244 if (rv == -1) {
1245 Py_DECREF(result);
1246 return NULL;
1247 }
1248 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001249 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001250 Py_DECREF(result);
1251 return NULL;
1252 }
1253 }
1254 }
1255 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001256 }
1257
Raymond Hettingera690a992003-11-16 16:17:49 +00001258 it = PyObject_GetIter(other);
1259 if (it == NULL) {
1260 Py_DECREF(result);
1261 return NULL;
1262 }
1263
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001264 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001265 int rv;
1266 setentry entry;
1267 long hash = PyObject_Hash(key);
1268
1269 if (hash == -1) {
1270 Py_DECREF(it);
1271 Py_DECREF(result);
1272 Py_DECREF(key);
1273 return NULL;
1274 }
1275 entry.hash = hash;
1276 entry.key = key;
1277 rv = set_contains_entry(so, &entry);
1278 if (rv == -1) {
1279 Py_DECREF(it);
1280 Py_DECREF(result);
1281 Py_DECREF(key);
1282 return NULL;
1283 }
1284 if (rv) {
1285 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001286 Py_DECREF(it);
1287 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001288 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001289 return NULL;
1290 }
1291 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001292 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001293 }
1294 Py_DECREF(it);
1295 if (PyErr_Occurred()) {
1296 Py_DECREF(result);
1297 return NULL;
1298 }
1299 return (PyObject *)result;
1300}
1301
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001302static PyObject *
1303set_intersection_multi(PySetObject *so, PyObject *args)
1304{
1305 Py_ssize_t i;
1306 PyObject *result = (PyObject *)so;
1307
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001308 if (PyTuple_GET_SIZE(args) == 0)
1309 return set_copy(so);
1310
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001311 Py_INCREF(so);
1312 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1313 PyObject *other = PyTuple_GET_ITEM(args, i);
1314 PyObject *newresult = set_intersection((PySetObject *)result, other);
1315 if (newresult == NULL) {
1316 Py_DECREF(result);
1317 return NULL;
1318 }
1319 Py_DECREF(result);
1320 result = newresult;
1321 }
1322 return result;
1323}
1324
Raymond Hettingera690a992003-11-16 16:17:49 +00001325PyDoc_STRVAR(intersection_doc,
1326"Return the intersection of two sets as a new set.\n\
1327\n\
1328(i.e. all elements that are in both sets.)");
1329
1330static PyObject *
1331set_intersection_update(PySetObject *so, PyObject *other)
1332{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001333 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001334
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001335 tmp = set_intersection(so, other);
1336 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001337 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001338 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001339 Py_DECREF(tmp);
1340 Py_RETURN_NONE;
1341}
1342
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001343static PyObject *
1344set_intersection_update_multi(PySetObject *so, PyObject *args)
1345{
1346 PyObject *tmp;
1347
1348 tmp = set_intersection_multi(so, args);
1349 if (tmp == NULL)
1350 return NULL;
1351 set_swap_bodies(so, (PySetObject *)tmp);
1352 Py_DECREF(tmp);
1353 Py_RETURN_NONE;
1354}
1355
Raymond Hettingera690a992003-11-16 16:17:49 +00001356PyDoc_STRVAR(intersection_update_doc,
1357"Update a set with the intersection of itself and another.");
1358
1359static PyObject *
1360set_and(PySetObject *so, PyObject *other)
1361{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001362 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001363 Py_INCREF(Py_NotImplemented);
1364 return Py_NotImplemented;
1365 }
1366 return set_intersection(so, other);
1367}
1368
1369static PyObject *
1370set_iand(PySetObject *so, PyObject *other)
1371{
1372 PyObject *result;
1373
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001374 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001375 Py_INCREF(Py_NotImplemented);
1376 return Py_NotImplemented;
1377 }
1378 result = set_intersection_update(so, other);
1379 if (result == NULL)
1380 return NULL;
1381 Py_DECREF(result);
1382 Py_INCREF(so);
1383 return (PyObject *)so;
1384}
1385
Guido van Rossum58da9312007-11-10 23:39:45 +00001386static PyObject *
1387set_isdisjoint(PySetObject *so, PyObject *other)
1388{
1389 PyObject *key, *it, *tmp;
1390
1391 if ((PyObject *)so == other) {
1392 if (PySet_GET_SIZE(so) == 0)
1393 Py_RETURN_TRUE;
1394 else
1395 Py_RETURN_FALSE;
1396 }
1397
1398 if (PyAnySet_CheckExact(other)) {
1399 Py_ssize_t pos = 0;
1400 setentry *entry;
1401
1402 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1403 tmp = (PyObject *)so;
1404 so = (PySetObject *)other;
1405 other = tmp;
1406 }
1407 while (set_next((PySetObject *)other, &pos, &entry)) {
1408 int rv = set_contains_entry(so, entry);
1409 if (rv == -1)
1410 return NULL;
1411 if (rv)
1412 Py_RETURN_FALSE;
1413 }
1414 Py_RETURN_TRUE;
1415 }
1416
1417 it = PyObject_GetIter(other);
1418 if (it == NULL)
1419 return NULL;
1420
1421 while ((key = PyIter_Next(it)) != NULL) {
1422 int rv;
1423 setentry entry;
Christian Heimes0ded5b52007-12-10 15:50:56 +00001424 long hash = PyObject_Hash(key);;
Guido van Rossum58da9312007-11-10 23:39:45 +00001425
1426 if (hash == -1) {
1427 Py_DECREF(key);
1428 Py_DECREF(it);
1429 return NULL;
1430 }
1431 entry.hash = hash;
1432 entry.key = key;
1433 rv = set_contains_entry(so, &entry);
1434 Py_DECREF(key);
1435 if (rv == -1) {
1436 Py_DECREF(it);
1437 return NULL;
1438 }
1439 if (rv) {
1440 Py_DECREF(it);
1441 Py_RETURN_FALSE;
1442 }
1443 }
1444 Py_DECREF(it);
1445 if (PyErr_Occurred())
1446 return NULL;
1447 Py_RETURN_TRUE;
1448}
1449
1450PyDoc_STRVAR(isdisjoint_doc,
1451"Return True if two sets have a null intersection.");
1452
Neal Norwitz6576bd82005-11-13 18:41:28 +00001453static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001454set_difference_update_internal(PySetObject *so, PyObject *other)
1455{
1456 if ((PyObject *)so == other)
1457 return set_clear_internal(so);
1458
Christian Heimesaf98da12008-01-27 15:18:18 +00001459 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001460 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001461 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001462
1463 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001464 if (set_discard_entry(so, entry) == -1)
1465 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001466 } else {
1467 PyObject *key, *it;
1468 it = PyObject_GetIter(other);
1469 if (it == NULL)
1470 return -1;
1471
1472 while ((key = PyIter_Next(it)) != NULL) {
1473 if (set_discard_key(so, key) == -1) {
1474 Py_DECREF(it);
1475 Py_DECREF(key);
1476 return -1;
1477 }
1478 Py_DECREF(key);
1479 }
1480 Py_DECREF(it);
1481 if (PyErr_Occurred())
1482 return -1;
1483 }
1484 /* If more than 1/5 are dummies, then resize them away. */
1485 if ((so->fill - so->used) * 5 < so->mask)
1486 return 0;
1487 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1488}
1489
Raymond Hettingera690a992003-11-16 16:17:49 +00001490static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001491set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001492{
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001493 Py_ssize_t i;
1494
1495 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1496 PyObject *other = PyTuple_GET_ITEM(args, i);
1497 if (set_difference_update_internal(so, other) == -1)
1498 return NULL;
1499 }
1500 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001501}
1502
1503PyDoc_STRVAR(difference_update_doc,
1504"Remove all elements of another set from this set.");
1505
1506static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001507set_difference(PySetObject *so, PyObject *other)
1508{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001509 PyObject *result;
1510 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001511 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001512
Christian Heimesaf98da12008-01-27 15:18:18 +00001513 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001514 result = set_copy(so);
1515 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001516 return NULL;
1517 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001518 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001519 Py_DECREF(result);
1520 return NULL;
1521 }
1522
Christian Heimes90aa7642007-12-19 02:45:37 +00001523 result = make_new_set(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001524 if (result == NULL)
1525 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001526
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001527 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001528 while (set_next(so, &pos, &entry)) {
1529 setentry entrycopy;
1530 entrycopy.hash = entry->hash;
1531 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001532 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001533 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1534 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001535 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001536 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001537 }
1538 }
1539 return result;
1540 }
1541
Raymond Hettingerc991db22005-08-11 07:58:45 +00001542 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001543 int rv = set_contains_entry((PySetObject *)other, entry);
1544 if (rv == -1) {
1545 Py_DECREF(result);
1546 return NULL;
1547 }
1548 if (!rv) {
1549 if (set_add_entry((PySetObject *)result, entry) == -1) {
1550 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001551 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001552 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001553 }
1554 }
1555 return result;
1556}
1557
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001558static PyObject *
1559set_difference_multi(PySetObject *so, PyObject *args)
1560{
1561 Py_ssize_t i;
1562 PyObject *result, *other;
1563
1564 if (PyTuple_GET_SIZE(args) == 0)
1565 return set_copy(so);
1566
1567 other = PyTuple_GET_ITEM(args, 0);
1568 result = set_difference(so, other);
1569 if (result == NULL)
1570 return NULL;
1571
1572 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1573 other = PyTuple_GET_ITEM(args, i);
1574 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1575 Py_DECREF(result);
1576 return NULL;
1577 }
1578 }
1579 return result;
1580}
1581
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001582PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001583"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001584\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001585(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001586static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001587set_sub(PySetObject *so, PyObject *other)
1588{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001589 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001590 Py_INCREF(Py_NotImplemented);
1591 return Py_NotImplemented;
1592 }
1593 return set_difference(so, other);
1594}
1595
1596static PyObject *
1597set_isub(PySetObject *so, PyObject *other)
1598{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001599 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001600 Py_INCREF(Py_NotImplemented);
1601 return Py_NotImplemented;
1602 }
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001603 if (set_difference_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001604 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001605 Py_INCREF(so);
1606 return (PyObject *)so;
1607}
1608
1609static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001610set_symmetric_difference_update(PySetObject *so, PyObject *other)
1611{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001612 PySetObject *otherset;
1613 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001614 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001615 setentry *entry;
1616
1617 if ((PyObject *)so == other)
1618 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001619
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001620 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001621 PyObject *value;
1622 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001623 long hash;
1624 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001625 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001626
Thomas Wouters89f507f2006-12-13 04:49:30 +00001627 an_entry.hash = hash;
1628 an_entry.key = key;
1629 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001630 if (rv == -1)
1631 return NULL;
1632 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001633 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001634 return NULL;
1635 }
1636 }
1637 Py_RETURN_NONE;
1638 }
1639
Christian Heimesaf98da12008-01-27 15:18:18 +00001640 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001641 Py_INCREF(other);
1642 otherset = (PySetObject *)other;
1643 } else {
Christian Heimes90aa7642007-12-19 02:45:37 +00001644 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001645 if (otherset == NULL)
1646 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001647 }
1648
Raymond Hettingerc991db22005-08-11 07:58:45 +00001649 while (set_next(otherset, &pos, &entry)) {
1650 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001651 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001652 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001653 return NULL;
1654 }
1655 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001656 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001657 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001658 return NULL;
1659 }
1660 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001661 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001662 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001663 Py_RETURN_NONE;
1664}
1665
1666PyDoc_STRVAR(symmetric_difference_update_doc,
1667"Update a set with the symmetric difference of itself and another.");
1668
1669static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001670set_symmetric_difference(PySetObject *so, PyObject *other)
1671{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001672 PyObject *rv;
1673 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001674
Christian Heimes90aa7642007-12-19 02:45:37 +00001675 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001676 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001677 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001678 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1679 if (rv == NULL)
1680 return NULL;
1681 Py_DECREF(rv);
1682 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001683}
1684
1685PyDoc_STRVAR(symmetric_difference_doc,
1686"Return the symmetric difference of two sets as a new set.\n\
1687\n\
1688(i.e. all elements that are in exactly one of the sets.)");
1689
1690static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001691set_xor(PySetObject *so, PyObject *other)
1692{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001693 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001694 Py_INCREF(Py_NotImplemented);
1695 return Py_NotImplemented;
1696 }
1697 return set_symmetric_difference(so, other);
1698}
1699
1700static PyObject *
1701set_ixor(PySetObject *so, PyObject *other)
1702{
1703 PyObject *result;
1704
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001705 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001706 Py_INCREF(Py_NotImplemented);
1707 return Py_NotImplemented;
1708 }
1709 result = set_symmetric_difference_update(so, other);
1710 if (result == NULL)
1711 return NULL;
1712 Py_DECREF(result);
1713 Py_INCREF(so);
1714 return (PyObject *)so;
1715}
1716
1717static PyObject *
1718set_issubset(PySetObject *so, PyObject *other)
1719{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001720 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001721 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001722
Christian Heimesaf98da12008-01-27 15:18:18 +00001723 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001724 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001725 tmp = make_new_set(&PySet_Type, other);
1726 if (tmp == NULL)
1727 return NULL;
1728 result = set_issubset(so, tmp);
1729 Py_DECREF(tmp);
1730 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001731 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001732 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001733 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001734
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001735 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001736 int rv = set_contains_entry((PySetObject *)other, entry);
1737 if (rv == -1)
1738 return NULL;
1739 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001740 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001741 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001742 Py_RETURN_TRUE;
1743}
1744
1745PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1746
1747static PyObject *
1748set_issuperset(PySetObject *so, PyObject *other)
1749{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001750 PyObject *tmp, *result;
1751
Christian Heimesaf98da12008-01-27 15:18:18 +00001752 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001753 tmp = make_new_set(&PySet_Type, other);
1754 if (tmp == NULL)
1755 return NULL;
1756 result = set_issuperset(so, tmp);
1757 Py_DECREF(tmp);
1758 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001759 }
1760 return set_issubset((PySetObject *)other, (PyObject *)so);
1761}
1762
1763PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1764
Raymond Hettingera690a992003-11-16 16:17:49 +00001765static PyObject *
1766set_richcompare(PySetObject *v, PyObject *w, int op)
1767{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001768 PyObject *r1, *r2;
1769
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001770 if(!PyAnySet_Check(w)) {
Guido van Rossum10ab4ae2007-08-23 23:57:24 +00001771 Py_INCREF(Py_NotImplemented);
1772 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001773 }
1774 switch (op) {
1775 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001776 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001777 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001778 if (v->hash != -1 &&
1779 ((PySetObject *)w)->hash != -1 &&
1780 v->hash != ((PySetObject *)w)->hash)
1781 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001782 return set_issubset(v, w);
1783 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001784 r1 = set_richcompare(v, w, Py_EQ);
1785 if (r1 == NULL)
1786 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001787 r2 = PyBool_FromLong(PyObject_Not(r1));
1788 Py_DECREF(r1);
1789 return r2;
1790 case Py_LE:
1791 return set_issubset(v, w);
1792 case Py_GE:
1793 return set_issuperset(v, w);
1794 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001795 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001796 Py_RETURN_FALSE;
1797 return set_issubset(v, w);
1798 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001799 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001800 Py_RETURN_FALSE;
1801 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001802 }
1803 Py_INCREF(Py_NotImplemented);
1804 return Py_NotImplemented;
1805}
1806
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001807static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001808set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001809{
1810 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1811 return -1;
1812}
1813
Raymond Hettingera690a992003-11-16 16:17:49 +00001814static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001815set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001816{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001817 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001818 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001819 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001820}
1821
1822PyDoc_STRVAR(add_doc,
1823"Add an element to a set.\n\
1824\n\
1825This has no effect if the element is already present.");
1826
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001827static int
1828set_contains(PySetObject *so, PyObject *key)
1829{
1830 PyObject *tmpkey;
1831 int rv;
1832
1833 rv = set_contains_key(so, key);
1834 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001835 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001836 return -1;
1837 PyErr_Clear();
1838 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1839 if (tmpkey == NULL)
1840 return -1;
1841 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1842 rv = set_contains(so, tmpkey);
1843 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1844 Py_DECREF(tmpkey);
1845 }
1846 return rv;
1847}
1848
1849static PyObject *
1850set_direct_contains(PySetObject *so, PyObject *key)
1851{
1852 long result;
1853
1854 result = set_contains(so, key);
1855 if (result == -1)
1856 return NULL;
1857 return PyBool_FromLong(result);
1858}
1859
1860PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1861
Raymond Hettingera690a992003-11-16 16:17:49 +00001862static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001863set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001864{
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001865 PyObject *tmpkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001866 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001867
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001868 rv = set_discard_key(so, key);
1869 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001870 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001871 return NULL;
1872 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001873 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1874 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001875 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001876 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001877 rv = set_discard_key(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001878 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001879 Py_DECREF(tmpkey);
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001880 if (rv == -1)
1881 return NULL;
1882 }
1883
1884 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001885 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001886 return NULL;
1887 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001888 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001889}
1890
1891PyDoc_STRVAR(remove_doc,
1892"Remove an element from a set; it must be a member.\n\
1893\n\
1894If the element is not a member, raise a KeyError.");
1895
1896static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001897set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001898{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001899 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001900 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001901
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001902 rv = set_discard_key(so, key);
1903 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001904 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001905 return NULL;
1906 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001907 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1908 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001909 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001910 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001911 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001912 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001913 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001914 return result;
1915 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001916 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001917}
1918
1919PyDoc_STRVAR(discard_doc,
1920"Remove an element from a set if it is a member.\n\
1921\n\
1922If the element is not a member, do nothing.");
1923
1924static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001925set_reduce(PySetObject *so)
1926{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001927 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001928
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001929 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001930 if (keys == NULL)
1931 goto done;
1932 args = PyTuple_Pack(1, keys);
1933 if (args == NULL)
1934 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001935 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1936 if (dict == NULL) {
1937 PyErr_Clear();
1938 dict = Py_None;
1939 Py_INCREF(dict);
1940 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001941 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001942done:
1943 Py_XDECREF(args);
1944 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001945 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001946 return result;
1947}
1948
1949PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1950
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001951static PyObject *
1952set_sizeof(PySetObject *so)
1953{
1954 Py_ssize_t res;
1955
1956 res = sizeof(PySetObject);
1957 if (so->table != so->smalltable)
1958 res = res + (so->mask + 1) * sizeof(setentry);
1959 return PyLong_FromSsize_t(res);
1960}
1961
1962PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001963static int
1964set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1965{
1966 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001967
1968 if (!PyAnySet_Check(self))
1969 return -1;
Christian Heimes90aa7642007-12-19 02:45:37 +00001970 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001971 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001972 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001973 self->hash = -1;
1974 if (iterable == NULL)
1975 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001976 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001977}
1978
Raymond Hettingera690a992003-11-16 16:17:49 +00001979static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001980 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001981 0, /* sq_concat */
1982 0, /* sq_repeat */
1983 0, /* sq_item */
1984 0, /* sq_slice */
1985 0, /* sq_ass_item */
1986 0, /* sq_ass_slice */
1987 (objobjproc)set_contains, /* sq_contains */
1988};
1989
1990/* set object ********************************************************/
1991
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001992#ifdef Py_DEBUG
1993static PyObject *test_c_api(PySetObject *so);
1994
1995PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1996All is well if assertions don't fail.");
1997#endif
1998
Raymond Hettingera690a992003-11-16 16:17:49 +00001999static PyMethodDef set_methods[] = {
2000 {"add", (PyCFunction)set_add, METH_O,
2001 add_doc},
2002 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2003 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00002004 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002005 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002006 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2007 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002008 {"discard", (PyCFunction)set_discard, METH_O,
2009 discard_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002010 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002011 difference_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002012 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002013 difference_update_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002014 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002015 intersection_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002016 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002017 intersection_update_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002018 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2019 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002020 {"issubset", (PyCFunction)set_issubset, METH_O,
2021 issubset_doc},
2022 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2023 issuperset_doc},
2024 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2025 pop_doc},
2026 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2027 reduce_doc},
2028 {"remove", (PyCFunction)set_remove, METH_O,
2029 remove_doc},
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002030 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2031 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002032 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2033 symmetric_difference_doc},
2034 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2035 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002036#ifdef Py_DEBUG
2037 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2038 test_c_api_doc},
2039#endif
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002040 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002041 union_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002042 {"update", (PyCFunction)set_update, METH_VARARGS,
Raymond Hettingera38123e2003-11-24 22:18:49 +00002043 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002044 {NULL, NULL} /* sentinel */
2045};
2046
2047static PyNumberMethods set_as_number = {
2048 0, /*nb_add*/
2049 (binaryfunc)set_sub, /*nb_subtract*/
2050 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002051 0, /*nb_remainder*/
2052 0, /*nb_divmod*/
2053 0, /*nb_power*/
2054 0, /*nb_negative*/
2055 0, /*nb_positive*/
2056 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002057 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002058 0, /*nb_invert*/
2059 0, /*nb_lshift*/
2060 0, /*nb_rshift*/
2061 (binaryfunc)set_and, /*nb_and*/
2062 (binaryfunc)set_xor, /*nb_xor*/
2063 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002064 0, /*nb_int*/
2065 0, /*nb_long*/
2066 0, /*nb_float*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002067 0, /*nb_inplace_add*/
2068 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2069 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002070 0, /*nb_inplace_remainder*/
2071 0, /*nb_inplace_power*/
2072 0, /*nb_inplace_lshift*/
2073 0, /*nb_inplace_rshift*/
2074 (binaryfunc)set_iand, /*nb_inplace_and*/
2075 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2076 (binaryfunc)set_ior, /*nb_inplace_or*/
2077};
2078
2079PyDoc_STRVAR(set_doc,
2080"set(iterable) --> set object\n\
2081\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002082Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002083
2084PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002085 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002086 "set", /* tp_name */
2087 sizeof(PySetObject), /* tp_basicsize */
2088 0, /* tp_itemsize */
2089 /* methods */
2090 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002091 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002092 0, /* tp_getattr */
2093 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002094 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002095 (reprfunc)set_repr, /* tp_repr */
2096 &set_as_number, /* tp_as_number */
2097 &set_as_sequence, /* tp_as_sequence */
2098 0, /* tp_as_mapping */
Nick Coghland1abd252008-07-15 15:46:38 +00002099 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00002100 0, /* tp_call */
2101 0, /* tp_str */
2102 PyObject_GenericGetAttr, /* tp_getattro */
2103 0, /* tp_setattro */
2104 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002105 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002106 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002107 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002108 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002109 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002110 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002111 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002112 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002113 0, /* tp_iternext */
2114 set_methods, /* tp_methods */
2115 0, /* tp_members */
2116 0, /* tp_getset */
2117 0, /* tp_base */
2118 0, /* tp_dict */
2119 0, /* tp_descr_get */
2120 0, /* tp_descr_set */
2121 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002122 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002123 PyType_GenericAlloc, /* tp_alloc */
2124 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002125 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002126};
2127
2128/* frozenset object ********************************************************/
2129
2130
2131static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002132 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002133 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002134 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002135 copy_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002136 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002137 difference_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002138 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002139 intersection_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002140 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2141 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002142 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002143 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002144 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002145 issuperset_doc},
2146 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2147 reduce_doc},
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002148 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2149 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002150 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2151 symmetric_difference_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002152 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002153 union_doc},
2154 {NULL, NULL} /* sentinel */
2155};
2156
2157static PyNumberMethods frozenset_as_number = {
2158 0, /*nb_add*/
2159 (binaryfunc)set_sub, /*nb_subtract*/
2160 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002161 0, /*nb_remainder*/
2162 0, /*nb_divmod*/
2163 0, /*nb_power*/
2164 0, /*nb_negative*/
2165 0, /*nb_positive*/
2166 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002167 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002168 0, /*nb_invert*/
2169 0, /*nb_lshift*/
2170 0, /*nb_rshift*/
2171 (binaryfunc)set_and, /*nb_and*/
2172 (binaryfunc)set_xor, /*nb_xor*/
2173 (binaryfunc)set_or, /*nb_or*/
2174};
2175
2176PyDoc_STRVAR(frozenset_doc,
2177"frozenset(iterable) --> frozenset object\n\
2178\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002179Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002180
2181PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002182 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002183 "frozenset", /* tp_name */
2184 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002185 0, /* tp_itemsize */
2186 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002187 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002188 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002189 0, /* tp_getattr */
2190 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002191 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002192 (reprfunc)set_repr, /* tp_repr */
2193 &frozenset_as_number, /* tp_as_number */
2194 &set_as_sequence, /* tp_as_sequence */
2195 0, /* tp_as_mapping */
2196 frozenset_hash, /* tp_hash */
2197 0, /* tp_call */
2198 0, /* tp_str */
2199 PyObject_GenericGetAttr, /* tp_getattro */
2200 0, /* tp_setattro */
2201 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002202 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002203 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002204 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002205 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002206 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002207 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002208 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002209 (getiterfunc)set_iter, /* tp_iter */
2210 0, /* tp_iternext */
2211 frozenset_methods, /* tp_methods */
2212 0, /* tp_members */
2213 0, /* tp_getset */
2214 0, /* tp_base */
2215 0, /* tp_dict */
2216 0, /* tp_descr_get */
2217 0, /* tp_descr_set */
2218 0, /* tp_dictoffset */
2219 0, /* tp_init */
2220 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002221 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002222 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002223};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002224
2225
2226/***** C API functions *************************************************/
2227
2228PyObject *
2229PySet_New(PyObject *iterable)
2230{
2231 return make_new_set(&PySet_Type, iterable);
2232}
2233
2234PyObject *
2235PyFrozenSet_New(PyObject *iterable)
2236{
Christian Heimesfd66e512008-01-29 12:18:50 +00002237 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002238}
2239
Neal Norwitz8c49c822006-03-04 18:41:19 +00002240Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002241PySet_Size(PyObject *anyset)
2242{
2243 if (!PyAnySet_Check(anyset)) {
2244 PyErr_BadInternalCall();
2245 return -1;
2246 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002247 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002248}
2249
2250int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002251PySet_Clear(PyObject *set)
2252{
Christian Heimesfd66e512008-01-29 12:18:50 +00002253 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002254 PyErr_BadInternalCall();
2255 return -1;
2256 }
2257 return set_clear_internal((PySetObject *)set);
2258}
2259
2260int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002261PySet_Contains(PyObject *anyset, PyObject *key)
2262{
2263 if (!PyAnySet_Check(anyset)) {
2264 PyErr_BadInternalCall();
2265 return -1;
2266 }
2267 return set_contains_key((PySetObject *)anyset, key);
2268}
2269
2270int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002271PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002272{
Christian Heimesfd66e512008-01-29 12:18:50 +00002273 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002274 PyErr_BadInternalCall();
2275 return -1;
2276 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002277 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002278}
2279
2280int
Christian Heimesfd66e512008-01-29 12:18:50 +00002281PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002282{
Christian Heimes15ebc882008-02-04 18:48:49 +00002283 if (!PySet_Check(anyset) &&
2284 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002285 PyErr_BadInternalCall();
2286 return -1;
2287 }
Christian Heimesfd66e512008-01-29 12:18:50 +00002288 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002289}
2290
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002291int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002292_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2293{
2294 setentry *entry;
2295
2296 if (!PyAnySet_Check(set)) {
2297 PyErr_BadInternalCall();
2298 return -1;
2299 }
2300 if (set_next((PySetObject *)set, pos, &entry) == 0)
2301 return 0;
2302 *key = entry->key;
2303 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002304 return 1;
2305}
2306
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002307PyObject *
2308PySet_Pop(PyObject *set)
2309{
Christian Heimesfd66e512008-01-29 12:18:50 +00002310 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002311 PyErr_BadInternalCall();
2312 return NULL;
2313 }
2314 return set_pop((PySetObject *)set);
2315}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002316
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002317int
2318_PySet_Update(PyObject *set, PyObject *iterable)
2319{
Christian Heimesfd66e512008-01-29 12:18:50 +00002320 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002321 PyErr_BadInternalCall();
2322 return -1;
2323 }
2324 return set_update_internal((PySetObject *)set, iterable);
2325}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002326
2327#ifdef Py_DEBUG
2328
2329/* Test code to be called with any three element set.
2330 Returns True and original set is restored. */
2331
2332#define assertRaises(call_return_value, exception) \
2333 do { \
2334 assert(call_return_value); \
2335 assert(PyErr_ExceptionMatches(exception)); \
2336 PyErr_Clear(); \
2337 } while(0)
2338
2339static PyObject *
2340test_c_api(PySetObject *so)
2341{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002342 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002343 char *s;
2344 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002345 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002346 PyObject *ob = (PyObject *)so;
Christian Heimesdb967892008-01-31 01:08:32 +00002347 long hash;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002348
2349 /* Verify preconditions and exercise type/size checks */
2350 assert(PyAnySet_Check(ob));
2351 assert(PyAnySet_CheckExact(ob));
2352 assert(!PyFrozenSet_CheckExact(ob));
2353 assert(PySet_Size(ob) == 3);
2354 assert(PySet_GET_SIZE(ob) == 3);
2355
2356 /* Raise TypeError for non-iterable constructor arguments */
2357 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2358 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2359
2360 /* Raise TypeError for unhashable key */
2361 dup = PySet_New(ob);
2362 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2363 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2364 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2365
2366 /* Exercise successful pop, contains, add, and discard */
2367 elem = PySet_Pop(ob);
2368 assert(PySet_Contains(ob, elem) == 0);
2369 assert(PySet_GET_SIZE(ob) == 2);
2370 assert(PySet_Add(ob, elem) == 0);
2371 assert(PySet_Contains(ob, elem) == 1);
2372 assert(PySet_GET_SIZE(ob) == 3);
2373 assert(PySet_Discard(ob, elem) == 1);
2374 assert(PySet_GET_SIZE(ob) == 2);
2375 assert(PySet_Discard(ob, elem) == 0);
2376 assert(PySet_GET_SIZE(ob) == 2);
2377
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002378 /* Exercise clear */
2379 dup2 = PySet_New(dup);
2380 assert(PySet_Clear(dup2) == 0);
2381 assert(PySet_Size(dup2) == 0);
2382 Py_DECREF(dup2);
2383
2384 /* Raise SystemError on clear or update of frozen set */
2385 f = PyFrozenSet_New(dup);
2386 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2387 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Christian Heimes15ebc882008-02-04 18:48:49 +00002388 assert(PySet_Add(f, elem) == 0);
2389 Py_INCREF(f);
2390 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2391 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002392 Py_DECREF(f);
2393
2394 /* Exercise direct iteration */
2395 i = 0, count = 0;
Christian Heimesdb967892008-01-31 01:08:32 +00002396 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002397 s = _PyUnicode_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002398 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2399 count++;
2400 }
2401 assert(count == 3);
2402
2403 /* Exercise updates */
2404 dup2 = PySet_New(NULL);
2405 assert(_PySet_Update(dup2, dup) == 0);
2406 assert(PySet_Size(dup2) == 3);
2407 assert(_PySet_Update(dup2, dup) == 0);
2408 assert(PySet_Size(dup2) == 3);
2409 Py_DECREF(dup2);
2410
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002411 /* Raise SystemError when self argument is not a set or frozenset. */
2412 t = PyTuple_New(0);
2413 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2414 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2415 Py_DECREF(t);
2416
2417 /* Raise SystemError when self argument is not a set. */
2418 f = PyFrozenSet_New(dup);
2419 assert(PySet_Size(f) == 3);
2420 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002421 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2422 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2423 Py_DECREF(f);
2424
2425 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002426 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2427 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002428 assert(PySet_GET_SIZE(ob) == 0);
2429 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2430
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002431 /* Restore the set from the copy using the PyNumber API */
2432 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2433 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002434
2435 /* Verify constructors accept NULL arguments */
2436 f = PySet_New(NULL);
2437 assert(f != NULL);
2438 assert(PySet_GET_SIZE(f) == 0);
2439 Py_DECREF(f);
2440 f = PyFrozenSet_New(NULL);
2441 assert(f != NULL);
2442 assert(PyFrozenSet_CheckExact(f));
2443 assert(PySet_GET_SIZE(f) == 0);
2444 Py_DECREF(f);
2445
2446 Py_DECREF(elem);
2447 Py_DECREF(dup);
2448 Py_RETURN_TRUE;
2449}
2450
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002451#undef assertRaises
2452
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002453#endif