blob: 79598f272d009e93c43efd14003310f9298097e8 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00006 Copyright (c) 2003-2007 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000012#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
26}
27
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028/* This must be >= 1. */
29#define PERTURB_SHIFT 5
30
31/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000032static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#ifdef Py_REF_DEBUG
35PyObject *
36_PySet_Dummy(void)
37{
38 return dummy;
39}
40#endif
41
Raymond Hettingerbc841a12005-08-07 13:02:53 +000042#define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
45 (so)->hash = -1; \
46 } while(0)
47
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000048#define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000051 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052 } while(0)
53
Raymond Hettingerbc841a12005-08-07 13:02:53 +000054/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000055#ifndef PySet_MAXFREELIST
56#define PySet_MAXFREELIST 80
57#endif
58static PySetObject *free_list[PySet_MAXFREELIST];
59static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060
Christian Heimes0ded5b52007-12-10 15:50:56 +000061
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062/*
63The basic lookup function used by all operations.
64This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65Open addressing is preferred over chaining since the link overhead for
66chaining would be substantial (100% with typical malloc overhead).
67
68The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000069probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070
71All arithmetic on hash should ignore overflow.
72
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000073Unlike the dictionary implementation, the lookkey functions can return
74NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075*/
76
77static setentry *
78set_lookkey(PySetObject *so, PyObject *key, register long hash)
79{
Martin v. Löwis18e16552006-02-15 17:27:45 +000080 register Py_ssize_t i;
81 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000084 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000085 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000087 PyObject *startkey;
88
89 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000090 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000091 if (entry->key == NULL || entry->key == key)
92 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 if (entry->key == dummy)
95 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000096 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000097 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000098 startkey = entry->key;
Georg Brandlf08a9dd2008-06-10 16:57:31 +000099 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000101 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000102 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000103 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000104 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000105 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000106 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000107 }
108 else {
109 /* The compare did major nasty stuff to the
110 * set: start over.
111 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000112 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000113 }
114 }
115 freeslot = NULL;
116 }
117
118 /* In the loop, key == dummy is by far (factor of 100s) the
119 least likely outcome, so test for that last. */
120 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
121 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000122 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000123 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 break;
127 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000128 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000129 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000130 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000131 startkey = entry->key;
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000132 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000134 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000135 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000136 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000137 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138 if (cmp > 0)
139 break;
140 }
141 else {
142 /* The compare did major nasty stuff to the
143 * set: start over.
144 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000145 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146 }
147 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000148 else if (entry->key == dummy && freeslot == NULL)
149 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000151 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152}
153
154/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000155 * Hacked up version of set_lookkey which can assume keys are always unicode;
156 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000157 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158 */
159static setentry *
Christian Heimes0ded5b52007-12-10 15:50:56 +0000160set_lookkey_unicode(PySetObject *so, PyObject *key, register long hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000161{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000162 register Py_ssize_t i;
163 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000164 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000165 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000166 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000167 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000168
Christian Heimes0ded5b52007-12-10 15:50:56 +0000169 /* Make sure this function doesn't have to handle non-unicode keys,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000170 including subclasses of str; e.g., one reason to subclass
171 strings is to override __eq__, and for speed we don't cater to
172 that here. */
Christian Heimes0ded5b52007-12-10 15:50:56 +0000173 if (!PyUnicode_CheckExact(key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000174 so->lookup = set_lookkey;
175 return set_lookkey(so, key, hash);
176 }
177 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000178 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000179 if (entry->key == NULL || entry->key == key)
180 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000181 if (entry->key == dummy)
182 freeslot = entry;
183 else {
Christian Heimes0ded5b52007-12-10 15:50:56 +0000184 if (entry->hash == hash && unicode_eq(entry->key, key))
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000185 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000186 freeslot = NULL;
187 }
188
189 /* In the loop, key == dummy is by far (factor of 100s) the
190 least likely outcome, so test for that last. */
191 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
192 i = (i << 2) + i + perturb + 1;
193 entry = &table[i & mask];
194 if (entry->key == NULL)
195 return freeslot == NULL ? entry : freeslot;
196 if (entry->key == key
197 || (entry->hash == hash
198 && entry->key != dummy
Christian Heimes0ded5b52007-12-10 15:50:56 +0000199 && unicode_eq(entry->key, key)))
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000200 return entry;
201 if (entry->key == dummy && freeslot == NULL)
202 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000203 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204 assert(0); /* NOT REACHED */
205 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206}
207
208/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211Eats a reference to key.
212*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000214set_insert_key(register PySetObject *so, PyObject *key, long hash)
215{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000216 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
218
219 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000221 if (entry == NULL)
222 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000224 /* UNUSED */
225 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000226 entry->key = key;
227 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000228 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000229 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000230 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000231 entry->key = key;
232 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000233 so->used++;
234 Py_DECREF(dummy);
235 } else {
236 /* ACTIVE */
237 Py_DECREF(key);
238 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000239 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000240}
241
242/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000243Internal routine used by set_table_resize() to insert an item which is
244known to be absent from the set. This routine also assumes that
245the set contains no deleted entries. Besides the performance benefit,
246using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
247Note that no refcounts are changed by this routine; if needed, the caller
248is responsible for incref'ing `key`.
249*/
250static void
251set_insert_clean(register PySetObject *so, PyObject *key, long hash)
252{
253 register size_t i;
254 register size_t perturb;
255 register size_t mask = (size_t)so->mask;
256 setentry *table = so->table;
257 register setentry *entry;
258
259 i = hash & mask;
260 entry = &table[i];
261 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
262 i = (i << 2) + i + perturb + 1;
263 entry = &table[i & mask];
264 }
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
269}
270
271/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000273keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274actually be smaller than the old one.
275*/
276static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000280 setentry *oldtable, *newtable, *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000281 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE];
284
285 assert(minused >= 0);
286
287 /* Find the smallest table size > minused. */
288 for (newsize = PySet_MINSIZE;
289 newsize <= minused && newsize > 0;
290 newsize <<= 1)
291 ;
292 if (newsize <= 0) {
293 PyErr_NoMemory();
294 return -1;
295 }
296
297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL);
300 is_oldtable_malloced = oldtable != so->smalltable;
301
302 if (newsize == PySet_MINSIZE) {
303 /* A large table is shrinking, or we can't get any smaller. */
304 newtable = so->smalltable;
305 if (newtable == oldtable) {
306 if (so->fill == so->used) {
307 /* No dummies, so no point doing anything. */
308 return 0;
309 }
310 /* We're not going to resize it, but rebuild the
311 table anyway to purge old dummy entries.
312 Subtle: This is *necessary* if fill==size,
313 as set_lookkey needs at least one virgin slot to
314 terminate failing searches. If fill < size, it's
315 merely desirable, as dummies slow searches. */
316 assert(so->fill > so->used);
317 memcpy(small_copy, oldtable, sizeof(small_copy));
318 oldtable = small_copy;
319 }
320 }
321 else {
322 newtable = PyMem_NEW(setentry, newsize);
323 if (newtable == NULL) {
324 PyErr_NoMemory();
325 return -1;
326 }
327 }
328
329 /* Make the set empty, using the new table. */
330 assert(newtable != oldtable);
331 so->table = newtable;
332 so->mask = newsize - 1;
333 memset(newtable, 0, sizeof(setentry) * newsize);
334 so->used = 0;
335 i = so->fill;
336 so->fill = 0;
337
338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000340 for (entry = oldtable; i > 0; entry++) {
341 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000342 /* UNUSED */
343 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000344 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345 /* DUMMY */
346 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000347 assert(entry->key == dummy);
348 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000349 } else {
350 /* ACTIVE */
351 --i;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000352 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000353 }
354 }
355
356 if (is_oldtable_malloced)
357 PyMem_DEL(oldtable);
358 return 0;
359}
360
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
362
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000364set_add_entry(register PySetObject *so, setentry *entry)
365{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000366 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000367
368 assert(so->fill <= so->mask); /* at least one empty slot */
369 n_used = so->used;
370 Py_INCREF(entry->key);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000371 if (set_insert_key(so, entry->key, entry->hash) == -1) {
372 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000373 return -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000374 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000375 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
376 return 0;
377 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
378}
379
380static int
381set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382{
383 register long hash;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000384 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000385
Christian Heimes0ded5b52007-12-10 15:50:56 +0000386 if (!PyUnicode_CheckExact(key) ||
387 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000388 hash = PyObject_Hash(key);
389 if (hash == -1)
390 return -1;
391 }
392 assert(so->fill <= so->mask); /* at least one empty slot */
393 n_used = so->used;
394 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000395 if (set_insert_key(so, key, hash) == -1) {
396 Py_DECREF(key);
397 return -1;
398 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000399 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
400 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000401 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402}
403
404#define DISCARD_NOTFOUND 0
405#define DISCARD_FOUND 1
406
407static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000408set_discard_entry(PySetObject *so, setentry *oldentry)
409{ register setentry *entry;
410 PyObject *old_key;
411
412 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000413 if (entry == NULL)
414 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000415 if (entry->key == NULL || entry->key == dummy)
416 return DISCARD_NOTFOUND;
417 old_key = entry->key;
418 Py_INCREF(dummy);
419 entry->key = dummy;
420 so->used--;
421 Py_DECREF(old_key);
422 return DISCARD_FOUND;
423}
424
425static int
426set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000427{
428 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000429 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000430 PyObject *old_key;
431
432 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000433
434 if (!PyUnicode_CheckExact(key) ||
435 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000436 hash = PyObject_Hash(key);
437 if (hash == -1)
438 return -1;
439 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000440 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000441 if (entry == NULL)
442 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000443 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000444 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000445 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000446 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000447 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448 so->used--;
449 Py_DECREF(old_key);
450 return DISCARD_FOUND;
451}
452
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000453static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454set_clear_internal(PySetObject *so)
455{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000456 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457 int table_is_malloced;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000458 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000459 setentry small_copy[PySet_MINSIZE];
460#ifdef Py_DEBUG
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000461 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000463
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464 n = so->mask + 1;
465 i = 0;
466#endif
467
468 table = so->table;
469 assert(table != NULL);
470 table_is_malloced = table != so->smalltable;
471
472 /* This is delicate. During the process of clearing the set,
473 * decrefs can cause the set to mutate. To avoid fatal confusion
474 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000475 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476 * clearing.
477 */
478 fill = so->fill;
479 if (table_is_malloced)
480 EMPTY_TO_MINSIZE(so);
481
482 else if (fill > 0) {
483 /* It's a small table with something that needs to be cleared.
484 * Afraid the only safe way is to copy the set entries into
485 * another small table first.
486 */
487 memcpy(small_copy, table, sizeof(small_copy));
488 table = small_copy;
489 EMPTY_TO_MINSIZE(so);
490 }
491 /* else it's a small table that's already empty */
492
493 /* Now we can finally clear things. If C had refcounts, we could
494 * assert that the refcount on table is 1 now, i.e. that this function
495 * has unique access to it, so decref side-effects can't alter it.
496 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000497 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498#ifdef Py_DEBUG
499 assert(i < n);
500 ++i;
501#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000502 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000504 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505 }
506#ifdef Py_DEBUG
507 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509#endif
510 }
511
512 if (table_is_malloced)
513 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000514 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515}
516
517/*
518 * Iterate over a set table. Use like so:
519 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000520 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000521 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000522 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000523 * while (set_next(yourset, &pos, &entry)) {
524 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525 * }
526 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000527 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000528 * mutates the table.
529 */
530static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000531set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000533 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000534 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536
537 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000538 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000539 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000540 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000541 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000542 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000544 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545 if (i > mask)
546 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000547 assert(table[i].key != NULL);
548 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000549 return 1;
550}
551
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000552static void
553set_dealloc(PySetObject *so)
554{
555 register setentry *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000556 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000557 PyObject_GC_UnTrack(so);
558 Py_TRASHCAN_SAFE_BEGIN(so)
559 if (so->weakreflist != NULL)
560 PyObject_ClearWeakRefs((PyObject *) so);
561
562 for (entry = so->table; fill > 0; entry++) {
563 if (entry->key) {
564 --fill;
565 Py_DECREF(entry->key);
566 }
567 }
568 if (so->table != so->smalltable)
569 PyMem_DEL(so->table);
Christian Heimes2202f872008-02-06 14:31:34 +0000570 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
571 free_list[numfree++] = so;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000572 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000573 Py_TYPE(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000574 Py_TRASHCAN_SAFE_END(so)
575}
576
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000577static PyObject *
578set_repr(PySetObject *so)
579{
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000580 PyObject *keys, *result=NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000581 Py_UNICODE *u;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000582 int status = Py_ReprEnter((PyObject*)so);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000583 PyObject *listrepr;
584 Py_ssize_t newsize;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000585
586 if (status != 0) {
587 if (status < 0)
588 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000589 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000590 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591
Georg Brandlc4996ba2006-08-28 19:37:11 +0000592 /* shortcut for the empty set */
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000593 if (!so->used) {
594 Py_ReprLeave((PyObject*)so);
Christian Heimes90aa7642007-12-19 02:45:37 +0000595 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000596 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000597
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000598 keys = PySequence_List((PyObject *)so);
599 if (keys == NULL)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000600 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000602 listrepr = PyObject_Repr(keys);
603 Py_DECREF(keys);
604 if (listrepr == NULL) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000605 Py_DECREF(keys);
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000606 goto done;
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000607 }
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000608 newsize = PyUnicode_GET_SIZE(listrepr);
609 result = PyUnicode_FromUnicode(NULL, newsize);
610 if (result) {
611 u = PyUnicode_AS_UNICODE(result);
612 *u++ = '{';
613 /* Omit the brackets from the listrepr */
614 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
615 PyUnicode_GET_SIZE(listrepr)-2);
616 u += newsize-2;
617 *u++ = '}';
618 }
619 Py_DECREF(listrepr);
Christian Heimes90aa7642007-12-19 02:45:37 +0000620 if (Py_TYPE(so) != &PySet_Type) {
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000621 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
Christian Heimes90aa7642007-12-19 02:45:37 +0000622 Py_TYPE(so)->tp_name,
Guido van Rossumbdba5cf2007-08-07 22:44:20 +0000623 result);
624 Py_DECREF(result);
625 result = tmp;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000626 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000627done:
628 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000629 return result;
630}
631
Martin v. Löwis18e16552006-02-15 17:27:45 +0000632static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000633set_len(PyObject *so)
634{
635 return ((PySetObject *)so)->used;
636}
637
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000638static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000639set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000640{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000641 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000643 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000644
645 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000646 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000647
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000648 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000649 if (other == so || other->used == 0)
650 /* a.update(a) or a.update({}); nothing to do */
651 return 0;
652 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000653 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000654 * that there will be no (or few) overlapping keys.
655 */
656 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
657 if (set_table_resize(so, (so->used + other->used)*2) != 0)
658 return -1;
659 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000660 for (i = 0; i <= other->mask; i++) {
661 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000662 if (entry->key != NULL &&
663 entry->key != dummy) {
664 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000665 if (set_insert_key(so, entry->key, entry->hash) == -1) {
666 Py_DECREF(entry->key);
667 return -1;
668 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000669 }
670 }
671 return 0;
672}
673
674static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000675set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000676{
677 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000678 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000679
Christian Heimes0ded5b52007-12-10 15:50:56 +0000680 if (!PyUnicode_CheckExact(key) ||
681 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000682 hash = PyObject_Hash(key);
683 if (hash == -1)
684 return -1;
685 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000686 entry = (so->lookup)(so, key, hash);
687 if (entry == NULL)
688 return -1;
689 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000690 return key != NULL && key != dummy;
691}
692
Raymond Hettingerc991db22005-08-11 07:58:45 +0000693static int
694set_contains_entry(PySetObject *so, setentry *entry)
695{
696 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000697 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000698
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000699 lu_entry = (so->lookup)(so, entry->key, entry->hash);
700 if (lu_entry == NULL)
701 return -1;
702 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000703 return key != NULL && key != dummy;
704}
705
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000706static PyObject *
707set_pop(PySetObject *so)
708{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000709 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000710 register setentry *entry;
711 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000712
713 assert (PyAnySet_Check(so));
714 if (so->used == 0) {
715 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
716 return NULL;
717 }
718
719 /* Set entry to "the first" unused or dummy set entry. We abuse
720 * the hash field of slot 0 to hold a search finger:
721 * If slot 0 has a value, use slot 0.
722 * Else slot 0 is being used to hold a search finger,
723 * and we use its hash value as the first index to look.
724 */
725 entry = &so->table[0];
726 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000727 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000728 /* The hash field may be a real hash value, or it may be a
729 * legit search finger, or it may be a once-legit search
730 * finger that's out of bounds now because it wrapped around
731 * or the table shrunk -- simply make sure it's in bounds now.
732 */
733 if (i > so->mask || i < 1)
734 i = 1; /* skip slot 0 */
735 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
736 i++;
737 if (i > so->mask)
738 i = 1;
739 }
740 }
741 key = entry->key;
742 Py_INCREF(dummy);
743 entry->key = dummy;
744 so->used--;
745 so->table[0].hash = i + 1; /* next place to start */
746 return key;
747}
748
749PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
750
751static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000752set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000753{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000754 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000755 setentry *entry;
756
757 while (set_next(so, &pos, &entry))
758 Py_VISIT(entry->key);
759 return 0;
760}
761
762static long
763frozenset_hash(PyObject *self)
764{
765 PySetObject *so = (PySetObject *)self;
766 long h, hash = 1927868237L;
767 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000768 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000769
770 if (so->hash != -1)
771 return so->hash;
772
773 hash *= PySet_GET_SIZE(self) + 1;
774 while (set_next(so, &pos, &entry)) {
775 /* Work to increase the bit dispersion for closely spaced hash
776 values. The is important because some use cases have many
777 combinations of a small number of elements with nearby
778 hashes so that many distinct combinations collapse to only
779 a handful of distinct hash values. */
780 h = entry->hash;
781 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
782 }
783 hash = hash * 69069L + 907133923L;
784 if (hash == -1)
785 hash = 590923713L;
786 so->hash = hash;
787 return hash;
788}
789
Raymond Hettingera9d99362005-08-05 00:01:15 +0000790/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792typedef struct {
793 PyObject_HEAD
794 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000795 Py_ssize_t si_used;
796 Py_ssize_t si_pos;
797 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000798} setiterobject;
799
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800static void
801setiter_dealloc(setiterobject *si)
802{
803 Py_XDECREF(si->si_set);
804 PyObject_Del(si);
805}
806
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000807static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000808setiter_len(setiterobject *si)
809{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000810 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000812 len = si->len;
Christian Heimes217cfd12007-12-02 14:31:20 +0000813 return PyLong_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000814}
815
Armin Rigof5b3e362006-02-11 21:32:43 +0000816PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000817
818static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000819 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000820 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000821};
822
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000823static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824{
825 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000826 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000827 register setentry *entry;
828 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000830 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000832 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000834 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835 PyErr_SetString(PyExc_RuntimeError,
836 "Set changed size during iteration");
837 si->si_used = -1; /* Make this state sticky */
838 return NULL;
839 }
840
841 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000842 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000843 entry = so->table;
844 mask = so->mask;
845 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000846 i++;
847 si->si_pos = i+1;
848 if (i > mask)
849 goto fail;
850 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000851 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000852 Py_INCREF(key);
853 return key;
854
855fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000856 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000857 si->si_set = NULL;
858 return NULL;
859}
860
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000861PyTypeObject PySetIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000862 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +0000863 "set_iterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000864 sizeof(setiterobject), /* tp_basicsize */
865 0, /* tp_itemsize */
866 /* methods */
867 (destructor)setiter_dealloc, /* tp_dealloc */
868 0, /* tp_print */
869 0, /* tp_getattr */
870 0, /* tp_setattr */
871 0, /* tp_compare */
872 0, /* tp_repr */
873 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000874 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000875 0, /* tp_as_mapping */
876 0, /* tp_hash */
877 0, /* tp_call */
878 0, /* tp_str */
879 PyObject_GenericGetAttr, /* tp_getattro */
880 0, /* tp_setattro */
881 0, /* tp_as_buffer */
882 Py_TPFLAGS_DEFAULT, /* tp_flags */
883 0, /* tp_doc */
884 0, /* tp_traverse */
885 0, /* tp_clear */
886 0, /* tp_richcompare */
887 0, /* tp_weaklistoffset */
888 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000889 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000890 setiter_methods, /* tp_methods */
891 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000892};
893
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000894static PyObject *
895set_iter(PySetObject *so)
896{
897 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
898 if (si == NULL)
899 return NULL;
900 Py_INCREF(so);
901 si->si_set = so;
902 si->si_used = so->used;
903 si->si_pos = 0;
904 si->len = so->used;
905 return (PyObject *)si;
906}
907
Raymond Hettingerd7946662005-08-01 21:39:29 +0000908static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000909set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000910{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000911 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000912
Christian Heimesaf98da12008-01-27 15:18:18 +0000913 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000914 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000915
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000916 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000917 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000918 Py_ssize_t pos = 0;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000919 long hash;
920 Py_ssize_t dictsize = PyDict_Size(other);
921
922 /* Do one big resize at the start, rather than
923 * incrementally resizing as we insert new keys. Expect
924 * that there will be no (or few) overlapping keys.
925 */
926 if (dictsize == -1)
927 return -1;
928 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
929 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
930 return -1;
931 }
932 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
933 setentry an_entry;
934
935 an_entry.hash = hash;
936 an_entry.key = key;
937 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000938 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000939 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000940 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000941 }
942
Raymond Hettingera38123e2003-11-24 22:18:49 +0000943 it = PyObject_GetIter(other);
944 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000945 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000946
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000947 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000948 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000949 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000950 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000951 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000952 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000953 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000954 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000955 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000956 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000957 return -1;
958 return 0;
959}
960
961static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000962set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000963{
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000964 Py_ssize_t i;
965
966 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
967 PyObject *other = PyTuple_GET_ITEM(args, i);
968 if (set_update_internal(so, other) == -1)
969 return NULL;
970 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000971 Py_RETURN_NONE;
972}
973
974PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000975"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000976
977static PyObject *
978make_new_set(PyTypeObject *type, PyObject *iterable)
979{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000980 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000981
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000982 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000983 dummy = PyUnicode_FromString("<dummy key>");
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000984 if (dummy == NULL)
985 return NULL;
986 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000987
988 /* create PySetObject structure */
Christian Heimes2202f872008-02-06 14:31:34 +0000989 if (numfree &&
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000990 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
Christian Heimes2202f872008-02-06 14:31:34 +0000991 so = free_list[--numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000992 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimes90aa7642007-12-19 02:45:37 +0000993 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000994 _Py_NewReference((PyObject *)so);
995 EMPTY_TO_MINSIZE(so);
996 PyObject_GC_Track(so);
997 } else {
998 so = (PySetObject *)type->tp_alloc(type, 0);
999 if (so == NULL)
1000 return NULL;
1001 /* tp_alloc has already zeroed the structure */
1002 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1003 INIT_NONZERO_SET_SLOTS(so);
1004 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001005
Christian Heimes0ded5b52007-12-10 15:50:56 +00001006 so->lookup = set_lookkey_unicode;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001007 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001008
Raymond Hettingera38123e2003-11-24 22:18:49 +00001009 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001010 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001011 Py_DECREF(so);
1012 return NULL;
1013 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001014 }
1015
Raymond Hettingera690a992003-11-16 16:17:49 +00001016 return (PyObject *)so;
1017}
1018
Raymond Hettingerd7946662005-08-01 21:39:29 +00001019/* The empty frozenset is a singleton */
1020static PyObject *emptyfrozenset = NULL;
1021
Raymond Hettingera690a992003-11-16 16:17:49 +00001022static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001023frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001024{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001025 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001026
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001027 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001028 return NULL;
1029
Raymond Hettingera690a992003-11-16 16:17:49 +00001030 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1031 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001032
1033 if (type != &PyFrozenSet_Type)
1034 return make_new_set(type, iterable);
1035
1036 if (iterable != NULL) {
1037 /* frozenset(f) is idempotent */
1038 if (PyFrozenSet_CheckExact(iterable)) {
1039 Py_INCREF(iterable);
1040 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001041 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001042 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001043 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001044 return result;
1045 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001046 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001047 /* The empty frozenset is a singleton */
1048 if (emptyfrozenset == NULL)
1049 emptyfrozenset = make_new_set(type, NULL);
1050 Py_XINCREF(emptyfrozenset);
1051 return emptyfrozenset;
1052}
1053
1054void
1055PySet_Fini(void)
1056{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001057 PySetObject *so;
1058
Christian Heimes2202f872008-02-06 14:31:34 +00001059 while (numfree) {
1060 numfree--;
1061 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001062 PyObject_GC_Del(so);
1063 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001064 Py_CLEAR(dummy);
1065 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001066}
1067
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001068static PyObject *
1069set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1070{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001071 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001072 return NULL;
1073
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001074 return make_new_set(type, NULL);
1075}
1076
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001077/* set_swap_bodies() switches the contents of any two sets by moving their
1078 internal data pointers and, if needed, copying the internal smalltables.
1079 Semantically equivalent to:
1080
1081 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1082
1083 The function always succeeds and it leaves both objects in a stable state.
1084 Useful for creating temporary frozensets from sets for membership testing
1085 in __contains__(), discard(), and remove(). Also useful for operations
1086 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001087 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001088*/
1089
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001090static void
1091set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001092{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001093 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001094 setentry *u;
1095 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1096 setentry tab[PySet_MINSIZE];
1097 long h;
1098
1099 t = a->fill; a->fill = b->fill; b->fill = t;
1100 t = a->used; a->used = b->used; b->used = t;
1101 t = a->mask; a->mask = b->mask; b->mask = t;
1102
1103 u = a->table;
1104 if (a->table == a->smalltable)
1105 u = b->smalltable;
1106 a->table = b->table;
1107 if (b->table == b->smalltable)
1108 a->table = a->smalltable;
1109 b->table = u;
1110
1111 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1112
1113 if (a->table == a->smalltable || b->table == b->smalltable) {
1114 memcpy(tab, a->smalltable, sizeof(tab));
1115 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1116 memcpy(b->smalltable, tab, sizeof(tab));
1117 }
1118
Christian Heimes90aa7642007-12-19 02:45:37 +00001119 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1120 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001121 h = a->hash; a->hash = b->hash; b->hash = h;
1122 } else {
1123 a->hash = -1;
1124 b->hash = -1;
1125 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001126}
1127
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001128static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001129set_copy(PySetObject *so)
1130{
Christian Heimes90aa7642007-12-19 02:45:37 +00001131 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001132}
1133
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001134static PyObject *
1135frozenset_copy(PySetObject *so)
1136{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001137 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001138 Py_INCREF(so);
1139 return (PyObject *)so;
1140 }
1141 return set_copy(so);
1142}
1143
Raymond Hettingera690a992003-11-16 16:17:49 +00001144PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1145
1146static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001147set_clear(PySetObject *so)
1148{
1149 set_clear_internal(so);
1150 Py_RETURN_NONE;
1151}
1152
1153PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1154
1155static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001156set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001157{
1158 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001159 PyObject *other;
1160 Py_ssize_t i;
1161
1162 result = (PySetObject *)set_copy(so);
1163 if (result == NULL)
1164 return NULL;
1165
1166 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1167 other = PyTuple_GET_ITEM(args, i);
1168 if ((PyObject *)so == other)
1169 return (PyObject *)result;
1170 if (set_update_internal(result, other) == -1) {
1171 Py_DECREF(result);
1172 return NULL;
1173 }
1174 }
1175 return (PyObject *)result;
1176}
1177
1178PyDoc_STRVAR(union_doc,
1179 "Return the union of sets as a new set.\n\
1180\n\
1181(i.e. all elements that are in either set.)");
1182
1183static PyObject *
1184set_or(PySetObject *so, PyObject *other)
1185{
1186 PySetObject *result;
1187
1188 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1189 Py_INCREF(Py_NotImplemented);
1190 return Py_NotImplemented;
1191 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001192
1193 result = (PySetObject *)set_copy(so);
1194 if (result == NULL)
1195 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001196 if ((PyObject *)so == other)
1197 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001198 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001199 Py_DECREF(result);
1200 return NULL;
1201 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001202 return (PyObject *)result;
1203}
1204
Raymond Hettingera690a992003-11-16 16:17:49 +00001205static PyObject *
1206set_ior(PySetObject *so, PyObject *other)
1207{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001208 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001209 Py_INCREF(Py_NotImplemented);
1210 return Py_NotImplemented;
1211 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001212 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001213 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001214 Py_INCREF(so);
1215 return (PyObject *)so;
1216}
1217
1218static PyObject *
1219set_intersection(PySetObject *so, PyObject *other)
1220{
1221 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001222 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001223
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001224 if ((PyObject *)so == other)
1225 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001226
Christian Heimes90aa7642007-12-19 02:45:37 +00001227 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001228 if (result == NULL)
1229 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001230
Christian Heimesaf98da12008-01-27 15:18:18 +00001231 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001232 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001233 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001234
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001235 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001236 tmp = (PyObject *)so;
1237 so = (PySetObject *)other;
1238 other = tmp;
1239 }
1240
Raymond Hettingerc991db22005-08-11 07:58:45 +00001241 while (set_next((PySetObject *)other, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001242 int rv = set_contains_entry(so, entry);
1243 if (rv == -1) {
1244 Py_DECREF(result);
1245 return NULL;
1246 }
1247 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001248 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001249 Py_DECREF(result);
1250 return NULL;
1251 }
1252 }
1253 }
1254 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001255 }
1256
Raymond Hettingera690a992003-11-16 16:17:49 +00001257 it = PyObject_GetIter(other);
1258 if (it == NULL) {
1259 Py_DECREF(result);
1260 return NULL;
1261 }
1262
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001263 while ((key = PyIter_Next(it)) != NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001264 int rv;
1265 setentry entry;
1266 long hash = PyObject_Hash(key);
1267
1268 if (hash == -1) {
1269 Py_DECREF(it);
1270 Py_DECREF(result);
1271 Py_DECREF(key);
1272 return NULL;
1273 }
1274 entry.hash = hash;
1275 entry.key = key;
1276 rv = set_contains_entry(so, &entry);
1277 if (rv == -1) {
1278 Py_DECREF(it);
1279 Py_DECREF(result);
1280 Py_DECREF(key);
1281 return NULL;
1282 }
1283 if (rv) {
1284 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001285 Py_DECREF(it);
1286 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001287 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001288 return NULL;
1289 }
1290 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001291 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001292 }
1293 Py_DECREF(it);
1294 if (PyErr_Occurred()) {
1295 Py_DECREF(result);
1296 return NULL;
1297 }
1298 return (PyObject *)result;
1299}
1300
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001301static PyObject *
1302set_intersection_multi(PySetObject *so, PyObject *args)
1303{
1304 Py_ssize_t i;
1305 PyObject *result = (PyObject *)so;
1306
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001307 if (PyTuple_GET_SIZE(args) == 0)
1308 return set_copy(so);
1309
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001310 Py_INCREF(so);
1311 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1312 PyObject *other = PyTuple_GET_ITEM(args, i);
1313 PyObject *newresult = set_intersection((PySetObject *)result, other);
1314 if (newresult == NULL) {
1315 Py_DECREF(result);
1316 return NULL;
1317 }
1318 Py_DECREF(result);
1319 result = newresult;
1320 }
1321 return result;
1322}
1323
Raymond Hettingera690a992003-11-16 16:17:49 +00001324PyDoc_STRVAR(intersection_doc,
1325"Return the intersection of two sets as a new set.\n\
1326\n\
1327(i.e. all elements that are in both sets.)");
1328
1329static PyObject *
1330set_intersection_update(PySetObject *so, PyObject *other)
1331{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001332 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001333
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001334 tmp = set_intersection(so, other);
1335 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001336 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001337 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001338 Py_DECREF(tmp);
1339 Py_RETURN_NONE;
1340}
1341
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001342static PyObject *
1343set_intersection_update_multi(PySetObject *so, PyObject *args)
1344{
1345 PyObject *tmp;
1346
1347 tmp = set_intersection_multi(so, args);
1348 if (tmp == NULL)
1349 return NULL;
1350 set_swap_bodies(so, (PySetObject *)tmp);
1351 Py_DECREF(tmp);
1352 Py_RETURN_NONE;
1353}
1354
Raymond Hettingera690a992003-11-16 16:17:49 +00001355PyDoc_STRVAR(intersection_update_doc,
1356"Update a set with the intersection of itself and another.");
1357
1358static PyObject *
1359set_and(PySetObject *so, PyObject *other)
1360{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001361 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001362 Py_INCREF(Py_NotImplemented);
1363 return Py_NotImplemented;
1364 }
1365 return set_intersection(so, other);
1366}
1367
1368static PyObject *
1369set_iand(PySetObject *so, PyObject *other)
1370{
1371 PyObject *result;
1372
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001373 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001374 Py_INCREF(Py_NotImplemented);
1375 return Py_NotImplemented;
1376 }
1377 result = set_intersection_update(so, other);
1378 if (result == NULL)
1379 return NULL;
1380 Py_DECREF(result);
1381 Py_INCREF(so);
1382 return (PyObject *)so;
1383}
1384
Guido van Rossum58da9312007-11-10 23:39:45 +00001385static PyObject *
1386set_isdisjoint(PySetObject *so, PyObject *other)
1387{
1388 PyObject *key, *it, *tmp;
1389
1390 if ((PyObject *)so == other) {
1391 if (PySet_GET_SIZE(so) == 0)
1392 Py_RETURN_TRUE;
1393 else
1394 Py_RETURN_FALSE;
1395 }
1396
1397 if (PyAnySet_CheckExact(other)) {
1398 Py_ssize_t pos = 0;
1399 setentry *entry;
1400
1401 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1402 tmp = (PyObject *)so;
1403 so = (PySetObject *)other;
1404 other = tmp;
1405 }
1406 while (set_next((PySetObject *)other, &pos, &entry)) {
1407 int rv = set_contains_entry(so, entry);
1408 if (rv == -1)
1409 return NULL;
1410 if (rv)
1411 Py_RETURN_FALSE;
1412 }
1413 Py_RETURN_TRUE;
1414 }
1415
1416 it = PyObject_GetIter(other);
1417 if (it == NULL)
1418 return NULL;
1419
1420 while ((key = PyIter_Next(it)) != NULL) {
1421 int rv;
1422 setentry entry;
Christian Heimes0ded5b52007-12-10 15:50:56 +00001423 long hash = PyObject_Hash(key);;
Guido van Rossum58da9312007-11-10 23:39:45 +00001424
1425 if (hash == -1) {
1426 Py_DECREF(key);
1427 Py_DECREF(it);
1428 return NULL;
1429 }
1430 entry.hash = hash;
1431 entry.key = key;
1432 rv = set_contains_entry(so, &entry);
1433 Py_DECREF(key);
1434 if (rv == -1) {
1435 Py_DECREF(it);
1436 return NULL;
1437 }
1438 if (rv) {
1439 Py_DECREF(it);
1440 Py_RETURN_FALSE;
1441 }
1442 }
1443 Py_DECREF(it);
1444 if (PyErr_Occurred())
1445 return NULL;
1446 Py_RETURN_TRUE;
1447}
1448
1449PyDoc_STRVAR(isdisjoint_doc,
1450"Return True if two sets have a null intersection.");
1451
Neal Norwitz6576bd82005-11-13 18:41:28 +00001452static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001453set_difference_update_internal(PySetObject *so, PyObject *other)
1454{
1455 if ((PyObject *)so == other)
1456 return set_clear_internal(so);
1457
Christian Heimesaf98da12008-01-27 15:18:18 +00001458 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001459 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001460 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001461
1462 while (set_next((PySetObject *)other, &pos, &entry))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001463 if (set_discard_entry(so, entry) == -1)
1464 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001465 } else {
1466 PyObject *key, *it;
1467 it = PyObject_GetIter(other);
1468 if (it == NULL)
1469 return -1;
1470
1471 while ((key = PyIter_Next(it)) != NULL) {
1472 if (set_discard_key(so, key) == -1) {
1473 Py_DECREF(it);
1474 Py_DECREF(key);
1475 return -1;
1476 }
1477 Py_DECREF(key);
1478 }
1479 Py_DECREF(it);
1480 if (PyErr_Occurred())
1481 return -1;
1482 }
1483 /* If more than 1/5 are dummies, then resize them away. */
1484 if ((so->fill - so->used) * 5 < so->mask)
1485 return 0;
1486 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1487}
1488
Raymond Hettingera690a992003-11-16 16:17:49 +00001489static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001490set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001491{
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001492 Py_ssize_t i;
1493
1494 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1495 PyObject *other = PyTuple_GET_ITEM(args, i);
1496 if (set_difference_update_internal(so, other) == -1)
1497 return NULL;
1498 }
1499 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001500}
1501
1502PyDoc_STRVAR(difference_update_doc,
1503"Remove all elements of another set from this set.");
1504
1505static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001506set_difference(PySetObject *so, PyObject *other)
1507{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001508 PyObject *result;
1509 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001510 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001511
Christian Heimesaf98da12008-01-27 15:18:18 +00001512 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001513 result = set_copy(so);
1514 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001515 return NULL;
1516 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001517 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001518 Py_DECREF(result);
1519 return NULL;
1520 }
1521
Christian Heimes90aa7642007-12-19 02:45:37 +00001522 result = make_new_set(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001523 if (result == NULL)
1524 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001525
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001526 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001527 while (set_next(so, &pos, &entry)) {
1528 setentry entrycopy;
1529 entrycopy.hash = entry->hash;
1530 entrycopy.key = entry->key;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001531 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001532 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1533 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001534 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001535 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001536 }
1537 }
1538 return result;
1539 }
1540
Raymond Hettingerc991db22005-08-11 07:58:45 +00001541 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001542 int rv = set_contains_entry((PySetObject *)other, entry);
1543 if (rv == -1) {
1544 Py_DECREF(result);
1545 return NULL;
1546 }
1547 if (!rv) {
1548 if (set_add_entry((PySetObject *)result, entry) == -1) {
1549 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001550 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001551 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001552 }
1553 }
1554 return result;
1555}
1556
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001557static PyObject *
1558set_difference_multi(PySetObject *so, PyObject *args)
1559{
1560 Py_ssize_t i;
1561 PyObject *result, *other;
1562
1563 if (PyTuple_GET_SIZE(args) == 0)
1564 return set_copy(so);
1565
1566 other = PyTuple_GET_ITEM(args, 0);
1567 result = set_difference(so, other);
1568 if (result == NULL)
1569 return NULL;
1570
1571 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1572 other = PyTuple_GET_ITEM(args, i);
1573 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1574 Py_DECREF(result);
1575 return NULL;
1576 }
1577 }
1578 return result;
1579}
1580
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001581PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001582"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001583\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001584(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001585static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001586set_sub(PySetObject *so, PyObject *other)
1587{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001588 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001589 Py_INCREF(Py_NotImplemented);
1590 return Py_NotImplemented;
1591 }
1592 return set_difference(so, other);
1593}
1594
1595static PyObject *
1596set_isub(PySetObject *so, PyObject *other)
1597{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001598 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001599 Py_INCREF(Py_NotImplemented);
1600 return Py_NotImplemented;
1601 }
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001602 if (set_difference_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001603 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001604 Py_INCREF(so);
1605 return (PyObject *)so;
1606}
1607
1608static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001609set_symmetric_difference_update(PySetObject *so, PyObject *other)
1610{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001611 PySetObject *otherset;
1612 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001613 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001614 setentry *entry;
1615
1616 if ((PyObject *)so == other)
1617 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001618
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001619 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001620 PyObject *value;
1621 int rv;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001622 long hash;
1623 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001624 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001625
Thomas Wouters89f507f2006-12-13 04:49:30 +00001626 an_entry.hash = hash;
1627 an_entry.key = key;
1628 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001629 if (rv == -1)
1630 return NULL;
1631 if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001632 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001633 return NULL;
1634 }
1635 }
1636 Py_RETURN_NONE;
1637 }
1638
Christian Heimesaf98da12008-01-27 15:18:18 +00001639 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001640 Py_INCREF(other);
1641 otherset = (PySetObject *)other;
1642 } else {
Christian Heimes90aa7642007-12-19 02:45:37 +00001643 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001644 if (otherset == NULL)
1645 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001646 }
1647
Raymond Hettingerc991db22005-08-11 07:58:45 +00001648 while (set_next(otherset, &pos, &entry)) {
1649 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001650 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001651 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001652 return NULL;
1653 }
1654 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001655 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001656 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001657 return NULL;
1658 }
1659 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001660 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001661 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001662 Py_RETURN_NONE;
1663}
1664
1665PyDoc_STRVAR(symmetric_difference_update_doc,
1666"Update a set with the symmetric difference of itself and another.");
1667
1668static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001669set_symmetric_difference(PySetObject *so, PyObject *other)
1670{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001671 PyObject *rv;
1672 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001673
Christian Heimes90aa7642007-12-19 02:45:37 +00001674 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001675 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001676 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001677 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1678 if (rv == NULL)
1679 return NULL;
1680 Py_DECREF(rv);
1681 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001682}
1683
1684PyDoc_STRVAR(symmetric_difference_doc,
1685"Return the symmetric difference of two sets as a new set.\n\
1686\n\
1687(i.e. all elements that are in exactly one of the sets.)");
1688
1689static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001690set_xor(PySetObject *so, PyObject *other)
1691{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001692 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001693 Py_INCREF(Py_NotImplemented);
1694 return Py_NotImplemented;
1695 }
1696 return set_symmetric_difference(so, other);
1697}
1698
1699static PyObject *
1700set_ixor(PySetObject *so, PyObject *other)
1701{
1702 PyObject *result;
1703
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001704 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001705 Py_INCREF(Py_NotImplemented);
1706 return Py_NotImplemented;
1707 }
1708 result = set_symmetric_difference_update(so, other);
1709 if (result == NULL)
1710 return NULL;
1711 Py_DECREF(result);
1712 Py_INCREF(so);
1713 return (PyObject *)so;
1714}
1715
1716static PyObject *
1717set_issubset(PySetObject *so, PyObject *other)
1718{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001719 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001720 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001721
Christian Heimesaf98da12008-01-27 15:18:18 +00001722 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001723 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001724 tmp = make_new_set(&PySet_Type, other);
1725 if (tmp == NULL)
1726 return NULL;
1727 result = set_issubset(so, tmp);
1728 Py_DECREF(tmp);
1729 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001730 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001731 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001732 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001733
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001734 while (set_next(so, &pos, &entry)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001735 int rv = set_contains_entry((PySetObject *)other, entry);
1736 if (rv == -1)
1737 return NULL;
1738 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001739 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001740 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001741 Py_RETURN_TRUE;
1742}
1743
1744PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1745
1746static PyObject *
1747set_issuperset(PySetObject *so, PyObject *other)
1748{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001749 PyObject *tmp, *result;
1750
Christian Heimesaf98da12008-01-27 15:18:18 +00001751 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001752 tmp = make_new_set(&PySet_Type, other);
1753 if (tmp == NULL)
1754 return NULL;
1755 result = set_issuperset(so, tmp);
1756 Py_DECREF(tmp);
1757 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001758 }
1759 return set_issubset((PySetObject *)other, (PyObject *)so);
1760}
1761
1762PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1763
Raymond Hettingera690a992003-11-16 16:17:49 +00001764static PyObject *
1765set_richcompare(PySetObject *v, PyObject *w, int op)
1766{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001767 PyObject *r1, *r2;
1768
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001769 if(!PyAnySet_Check(w)) {
Guido van Rossum10ab4ae2007-08-23 23:57:24 +00001770 Py_INCREF(Py_NotImplemented);
1771 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001772 }
1773 switch (op) {
1774 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001775 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001776 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001777 if (v->hash != -1 &&
1778 ((PySetObject *)w)->hash != -1 &&
1779 v->hash != ((PySetObject *)w)->hash)
1780 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001781 return set_issubset(v, w);
1782 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001783 r1 = set_richcompare(v, w, Py_EQ);
1784 if (r1 == NULL)
1785 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001786 r2 = PyBool_FromLong(PyObject_Not(r1));
1787 Py_DECREF(r1);
1788 return r2;
1789 case Py_LE:
1790 return set_issubset(v, w);
1791 case Py_GE:
1792 return set_issuperset(v, w);
1793 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001794 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001795 Py_RETURN_FALSE;
1796 return set_issubset(v, w);
1797 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001798 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001799 Py_RETURN_FALSE;
1800 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001801 }
1802 Py_INCREF(Py_NotImplemented);
1803 return Py_NotImplemented;
1804}
1805
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001806static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001807set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001808{
1809 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1810 return -1;
1811}
1812
Raymond Hettingera690a992003-11-16 16:17:49 +00001813static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001814set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001815{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001816 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001817 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001818 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001819}
1820
1821PyDoc_STRVAR(add_doc,
1822"Add an element to a set.\n\
1823\n\
1824This has no effect if the element is already present.");
1825
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001826static int
1827set_contains(PySetObject *so, PyObject *key)
1828{
1829 PyObject *tmpkey;
1830 int rv;
1831
1832 rv = set_contains_key(so, key);
1833 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001834 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001835 return -1;
1836 PyErr_Clear();
1837 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1838 if (tmpkey == NULL)
1839 return -1;
1840 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1841 rv = set_contains(so, tmpkey);
1842 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1843 Py_DECREF(tmpkey);
1844 }
1845 return rv;
1846}
1847
1848static PyObject *
1849set_direct_contains(PySetObject *so, PyObject *key)
1850{
1851 long result;
1852
1853 result = set_contains(so, key);
1854 if (result == -1)
1855 return NULL;
1856 return PyBool_FromLong(result);
1857}
1858
1859PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1860
Raymond Hettingera690a992003-11-16 16:17:49 +00001861static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001862set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001863{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001864 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001865 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001866
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001867 rv = set_discard_key(so, key);
1868 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001869 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001870 return NULL;
1871 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001872 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1873 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001874 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001875 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001876 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001877 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001878 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001879 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001880 } else if (rv == DISCARD_NOTFOUND) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001881 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001882 return NULL;
1883 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001884 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001885}
1886
1887PyDoc_STRVAR(remove_doc,
1888"Remove an element from a set; it must be a member.\n\
1889\n\
1890If the element is not a member, raise a KeyError.");
1891
1892static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001893set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001894{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001895 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001896 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001897
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001898 rv = set_discard_key(so, key);
1899 if (rv == -1) {
Raymond Hettinger10956ea2008-05-08 16:02:10 +00001900 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001901 return NULL;
1902 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001903 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1904 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001905 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001906 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001907 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001908 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001909 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001910 return result;
1911 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001912 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001913}
1914
1915PyDoc_STRVAR(discard_doc,
1916"Remove an element from a set if it is a member.\n\
1917\n\
1918If the element is not a member, do nothing.");
1919
1920static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001921set_reduce(PySetObject *so)
1922{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001923 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001924
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001925 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001926 if (keys == NULL)
1927 goto done;
1928 args = PyTuple_Pack(1, keys);
1929 if (args == NULL)
1930 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001931 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1932 if (dict == NULL) {
1933 PyErr_Clear();
1934 dict = Py_None;
1935 Py_INCREF(dict);
1936 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001937 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001938done:
1939 Py_XDECREF(args);
1940 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001941 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001942 return result;
1943}
1944
1945PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1946
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001947static PyObject *
1948set_sizeof(PySetObject *so)
1949{
1950 Py_ssize_t res;
1951
1952 res = sizeof(PySetObject);
1953 if (so->table != so->smalltable)
1954 res = res + (so->mask + 1) * sizeof(setentry);
1955 return PyLong_FromSsize_t(res);
1956}
1957
1958PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001959static int
1960set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1961{
1962 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001963
1964 if (!PyAnySet_Check(self))
1965 return -1;
Christian Heimes90aa7642007-12-19 02:45:37 +00001966 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001967 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001968 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001969 self->hash = -1;
1970 if (iterable == NULL)
1971 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001972 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001973}
1974
Raymond Hettingera690a992003-11-16 16:17:49 +00001975static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001976 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001977 0, /* sq_concat */
1978 0, /* sq_repeat */
1979 0, /* sq_item */
1980 0, /* sq_slice */
1981 0, /* sq_ass_item */
1982 0, /* sq_ass_slice */
1983 (objobjproc)set_contains, /* sq_contains */
1984};
1985
1986/* set object ********************************************************/
1987
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001988#ifdef Py_DEBUG
1989static PyObject *test_c_api(PySetObject *so);
1990
1991PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1992All is well if assertions don't fail.");
1993#endif
1994
Raymond Hettingera690a992003-11-16 16:17:49 +00001995static PyMethodDef set_methods[] = {
1996 {"add", (PyCFunction)set_add, METH_O,
1997 add_doc},
1998 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1999 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00002000 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002001 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002002 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2003 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002004 {"discard", (PyCFunction)set_discard, METH_O,
2005 discard_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002006 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002007 difference_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002008 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002009 difference_update_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002010 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002011 intersection_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002012 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002013 intersection_update_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002014 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2015 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002016 {"issubset", (PyCFunction)set_issubset, METH_O,
2017 issubset_doc},
2018 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2019 issuperset_doc},
2020 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2021 pop_doc},
2022 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2023 reduce_doc},
2024 {"remove", (PyCFunction)set_remove, METH_O,
2025 remove_doc},
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002026 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2027 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002028 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2029 symmetric_difference_doc},
2030 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2031 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002032#ifdef Py_DEBUG
2033 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2034 test_c_api_doc},
2035#endif
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002036 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002037 union_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002038 {"update", (PyCFunction)set_update, METH_VARARGS,
Raymond Hettingera38123e2003-11-24 22:18:49 +00002039 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002040 {NULL, NULL} /* sentinel */
2041};
2042
2043static PyNumberMethods set_as_number = {
2044 0, /*nb_add*/
2045 (binaryfunc)set_sub, /*nb_subtract*/
2046 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002047 0, /*nb_remainder*/
2048 0, /*nb_divmod*/
2049 0, /*nb_power*/
2050 0, /*nb_negative*/
2051 0, /*nb_positive*/
2052 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002053 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002054 0, /*nb_invert*/
2055 0, /*nb_lshift*/
2056 0, /*nb_rshift*/
2057 (binaryfunc)set_and, /*nb_and*/
2058 (binaryfunc)set_xor, /*nb_xor*/
2059 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002060 0, /*nb_int*/
2061 0, /*nb_long*/
2062 0, /*nb_float*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002063 0, /*nb_inplace_add*/
2064 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2065 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002066 0, /*nb_inplace_remainder*/
2067 0, /*nb_inplace_power*/
2068 0, /*nb_inplace_lshift*/
2069 0, /*nb_inplace_rshift*/
2070 (binaryfunc)set_iand, /*nb_inplace_and*/
2071 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2072 (binaryfunc)set_ior, /*nb_inplace_or*/
2073};
2074
2075PyDoc_STRVAR(set_doc,
2076"set(iterable) --> set object\n\
2077\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002078Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002079
2080PyTypeObject PySet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002081 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002082 "set", /* tp_name */
2083 sizeof(PySetObject), /* tp_basicsize */
2084 0, /* tp_itemsize */
2085 /* methods */
2086 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002087 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002088 0, /* tp_getattr */
2089 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002090 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002091 (reprfunc)set_repr, /* tp_repr */
2092 &set_as_number, /* tp_as_number */
2093 &set_as_sequence, /* tp_as_sequence */
2094 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002095 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00002096 0, /* tp_call */
2097 0, /* tp_str */
2098 PyObject_GenericGetAttr, /* tp_getattro */
2099 0, /* tp_setattro */
2100 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002101 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002102 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002103 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002104 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002105 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002106 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002107 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002108 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002109 0, /* tp_iternext */
2110 set_methods, /* tp_methods */
2111 0, /* tp_members */
2112 0, /* tp_getset */
2113 0, /* tp_base */
2114 0, /* tp_dict */
2115 0, /* tp_descr_get */
2116 0, /* tp_descr_set */
2117 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002118 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002119 PyType_GenericAlloc, /* tp_alloc */
2120 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002121 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002122};
2123
2124/* frozenset object ********************************************************/
2125
2126
2127static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002128 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002129 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002130 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002131 copy_doc},
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002132 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002133 difference_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002134 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002135 intersection_doc},
Guido van Rossum58da9312007-11-10 23:39:45 +00002136 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2137 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002138 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002139 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002140 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002141 issuperset_doc},
2142 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2143 reduce_doc},
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002144 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2145 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002146 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2147 symmetric_difference_doc},
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002148 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002149 union_doc},
2150 {NULL, NULL} /* sentinel */
2151};
2152
2153static PyNumberMethods frozenset_as_number = {
2154 0, /*nb_add*/
2155 (binaryfunc)set_sub, /*nb_subtract*/
2156 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002157 0, /*nb_remainder*/
2158 0, /*nb_divmod*/
2159 0, /*nb_power*/
2160 0, /*nb_negative*/
2161 0, /*nb_positive*/
2162 0, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00002163 0, /*nb_bool*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002164 0, /*nb_invert*/
2165 0, /*nb_lshift*/
2166 0, /*nb_rshift*/
2167 (binaryfunc)set_and, /*nb_and*/
2168 (binaryfunc)set_xor, /*nb_xor*/
2169 (binaryfunc)set_or, /*nb_or*/
2170};
2171
2172PyDoc_STRVAR(frozenset_doc,
2173"frozenset(iterable) --> frozenset object\n\
2174\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002175Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002176
2177PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002178 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002179 "frozenset", /* tp_name */
2180 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002181 0, /* tp_itemsize */
2182 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002183 (destructor)set_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002184 0, /* tp_print */
Raymond Hettingera690a992003-11-16 16:17:49 +00002185 0, /* tp_getattr */
2186 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002187 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002188 (reprfunc)set_repr, /* tp_repr */
2189 &frozenset_as_number, /* tp_as_number */
2190 &set_as_sequence, /* tp_as_sequence */
2191 0, /* tp_as_mapping */
2192 frozenset_hash, /* tp_hash */
2193 0, /* tp_call */
2194 0, /* tp_str */
2195 PyObject_GenericGetAttr, /* tp_getattro */
2196 0, /* tp_setattro */
2197 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002198 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002199 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002200 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002201 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002202 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002203 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002204 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002205 (getiterfunc)set_iter, /* tp_iter */
2206 0, /* tp_iternext */
2207 frozenset_methods, /* tp_methods */
2208 0, /* tp_members */
2209 0, /* tp_getset */
2210 0, /* tp_base */
2211 0, /* tp_dict */
2212 0, /* tp_descr_get */
2213 0, /* tp_descr_set */
2214 0, /* tp_dictoffset */
2215 0, /* tp_init */
2216 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002217 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002218 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002219};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002220
2221
2222/***** C API functions *************************************************/
2223
2224PyObject *
2225PySet_New(PyObject *iterable)
2226{
2227 return make_new_set(&PySet_Type, iterable);
2228}
2229
2230PyObject *
2231PyFrozenSet_New(PyObject *iterable)
2232{
Christian Heimesfd66e512008-01-29 12:18:50 +00002233 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002234}
2235
Neal Norwitz8c49c822006-03-04 18:41:19 +00002236Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002237PySet_Size(PyObject *anyset)
2238{
2239 if (!PyAnySet_Check(anyset)) {
2240 PyErr_BadInternalCall();
2241 return -1;
2242 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002243 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002244}
2245
2246int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002247PySet_Clear(PyObject *set)
2248{
Christian Heimesfd66e512008-01-29 12:18:50 +00002249 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002250 PyErr_BadInternalCall();
2251 return -1;
2252 }
2253 return set_clear_internal((PySetObject *)set);
2254}
2255
2256int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002257PySet_Contains(PyObject *anyset, PyObject *key)
2258{
2259 if (!PyAnySet_Check(anyset)) {
2260 PyErr_BadInternalCall();
2261 return -1;
2262 }
2263 return set_contains_key((PySetObject *)anyset, key);
2264}
2265
2266int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002267PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002268{
Christian Heimesfd66e512008-01-29 12:18:50 +00002269 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002270 PyErr_BadInternalCall();
2271 return -1;
2272 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002273 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002274}
2275
2276int
Christian Heimesfd66e512008-01-29 12:18:50 +00002277PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002278{
Christian Heimes15ebc882008-02-04 18:48:49 +00002279 if (!PySet_Check(anyset) &&
2280 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002281 PyErr_BadInternalCall();
2282 return -1;
2283 }
Christian Heimesfd66e512008-01-29 12:18:50 +00002284 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002285}
2286
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002287int
Guido van Rossumd8faa362007-04-27 19:54:29 +00002288_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2289{
2290 setentry *entry;
2291
2292 if (!PyAnySet_Check(set)) {
2293 PyErr_BadInternalCall();
2294 return -1;
2295 }
2296 if (set_next((PySetObject *)set, pos, &entry) == 0)
2297 return 0;
2298 *key = entry->key;
2299 *hash = entry->hash;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002300 return 1;
2301}
2302
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002303PyObject *
2304PySet_Pop(PyObject *set)
2305{
Christian Heimesfd66e512008-01-29 12:18:50 +00002306 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002307 PyErr_BadInternalCall();
2308 return NULL;
2309 }
2310 return set_pop((PySetObject *)set);
2311}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002312
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002313int
2314_PySet_Update(PyObject *set, PyObject *iterable)
2315{
Christian Heimesfd66e512008-01-29 12:18:50 +00002316 if (!PySet_Check(set)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002317 PyErr_BadInternalCall();
2318 return -1;
2319 }
2320 return set_update_internal((PySetObject *)set, iterable);
2321}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002322
2323#ifdef Py_DEBUG
2324
2325/* Test code to be called with any three element set.
2326 Returns True and original set is restored. */
2327
2328#define assertRaises(call_return_value, exception) \
2329 do { \
2330 assert(call_return_value); \
2331 assert(PyErr_ExceptionMatches(exception)); \
2332 PyErr_Clear(); \
2333 } while(0)
2334
2335static PyObject *
2336test_c_api(PySetObject *so)
2337{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002338 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002339 char *s;
2340 Py_ssize_t i;
Guido van Rossum3b116a32007-05-10 17:35:11 +00002341 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002342 PyObject *ob = (PyObject *)so;
Christian Heimesdb967892008-01-31 01:08:32 +00002343 long hash;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002344
2345 /* Verify preconditions and exercise type/size checks */
2346 assert(PyAnySet_Check(ob));
2347 assert(PyAnySet_CheckExact(ob));
2348 assert(!PyFrozenSet_CheckExact(ob));
2349 assert(PySet_Size(ob) == 3);
2350 assert(PySet_GET_SIZE(ob) == 3);
2351
2352 /* Raise TypeError for non-iterable constructor arguments */
2353 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2354 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2355
2356 /* Raise TypeError for unhashable key */
2357 dup = PySet_New(ob);
2358 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2359 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2360 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2361
2362 /* Exercise successful pop, contains, add, and discard */
2363 elem = PySet_Pop(ob);
2364 assert(PySet_Contains(ob, elem) == 0);
2365 assert(PySet_GET_SIZE(ob) == 2);
2366 assert(PySet_Add(ob, elem) == 0);
2367 assert(PySet_Contains(ob, elem) == 1);
2368 assert(PySet_GET_SIZE(ob) == 3);
2369 assert(PySet_Discard(ob, elem) == 1);
2370 assert(PySet_GET_SIZE(ob) == 2);
2371 assert(PySet_Discard(ob, elem) == 0);
2372 assert(PySet_GET_SIZE(ob) == 2);
2373
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002374 /* Exercise clear */
2375 dup2 = PySet_New(dup);
2376 assert(PySet_Clear(dup2) == 0);
2377 assert(PySet_Size(dup2) == 0);
2378 Py_DECREF(dup2);
2379
2380 /* Raise SystemError on clear or update of frozen set */
2381 f = PyFrozenSet_New(dup);
2382 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2383 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Christian Heimes15ebc882008-02-04 18:48:49 +00002384 assert(PySet_Add(f, elem) == 0);
2385 Py_INCREF(f);
2386 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2387 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002388 Py_DECREF(f);
2389
2390 /* Exercise direct iteration */
2391 i = 0, count = 0;
Christian Heimesdb967892008-01-31 01:08:32 +00002392 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
Amaury Forgeot d'Arc39599dc2007-11-22 02:48:12 +00002393 s = PyUnicode_AsString(x);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002394 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2395 count++;
2396 }
2397 assert(count == 3);
2398
2399 /* Exercise updates */
2400 dup2 = PySet_New(NULL);
2401 assert(_PySet_Update(dup2, dup) == 0);
2402 assert(PySet_Size(dup2) == 3);
2403 assert(_PySet_Update(dup2, dup) == 0);
2404 assert(PySet_Size(dup2) == 3);
2405 Py_DECREF(dup2);
2406
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002407 /* Raise SystemError when self argument is not a set or frozenset. */
2408 t = PyTuple_New(0);
2409 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2410 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2411 Py_DECREF(t);
2412
2413 /* Raise SystemError when self argument is not a set. */
2414 f = PyFrozenSet_New(dup);
2415 assert(PySet_Size(f) == 3);
2416 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002417 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2418 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2419 Py_DECREF(f);
2420
2421 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002422 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2423 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002424 assert(PySet_GET_SIZE(ob) == 0);
2425 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2426
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002427 /* Restore the set from the copy using the PyNumber API */
2428 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2429 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002430
2431 /* Verify constructors accept NULL arguments */
2432 f = PySet_New(NULL);
2433 assert(f != NULL);
2434 assert(PySet_GET_SIZE(f) == 0);
2435 Py_DECREF(f);
2436 f = PyFrozenSet_New(NULL);
2437 assert(f != NULL);
2438 assert(PyFrozenSet_CheckExact(f));
2439 assert(PySet_GET_SIZE(f) == 0);
2440 Py_DECREF(f);
2441
2442 Py_DECREF(elem);
2443 Py_DECREF(dup);
2444 Py_RETURN_TRUE;
2445}
2446
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002447#undef assertRaises
2448
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002449#endif