blob: c484dce41389e78493ac979633f4aeb33665b622 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingera9d99362005-08-05 00:01:15 +00003 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
Raymond Hettinger2212e522008-11-30 23:43:36 +00006 Copyright (c) 2003-2008 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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 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);
Thomas Wouters89f507f2006-12-13 04:49:30 +000026}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039}
40#endif
41
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042#define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
45 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000046 } while(0)
47
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
51 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 *
Benjamin Peterson8f67d082010-10-17 20:54:53 +000078set_lookkey(PySetObject *so, PyObject *key, register Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000079{
Gregory P. Smith27cbcd62012-12-10 18:15:46 -080080 register size_t i; /* Unsigned for defined overflow behavior. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 register size_t perturb;
82 register setentry *freeslot;
83 register size_t mask = so->mask;
84 setentry *table = so->table;
85 register setentry *entry;
86 register int cmp;
87 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000088
Mark Dickinson57e683e2011-09-24 18:18:40 +010089 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 entry = &table[i];
91 if (entry->key == NULL || entry->key == key)
92 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 if (entry->key == dummy)
95 freeslot = entry;
96 else {
97 if (entry->hash == hash) {
98 startkey = entry->key;
99 Py_INCREF(startkey);
100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
101 Py_DECREF(startkey);
102 if (cmp < 0)
103 return NULL;
104 if (table == so->table && entry->key == startkey) {
105 if (cmp > 0)
106 return entry;
107 }
108 else {
109 /* The compare did major nasty stuff to the
110 * set: start over.
111 */
112 return set_lookkey(so, key, hash);
113 }
114 }
115 freeslot = NULL;
116 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /* 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;
122 entry = &table[i & mask];
123 if (entry->key == NULL) {
124 if (freeslot != NULL)
125 entry = freeslot;
126 break;
127 }
128 if (entry->key == key)
129 break;
130 if (entry->hash == hash && entry->key != dummy) {
131 startkey = entry->key;
132 Py_INCREF(startkey);
133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
134 Py_DECREF(startkey);
135 if (cmp < 0)
136 return NULL;
137 if (table == so->table && entry->key == startkey) {
138 if (cmp > 0)
139 break;
140 }
141 else {
142 /* The compare did major nasty stuff to the
143 * set: start over.
144 */
145 return set_lookkey(so, key, hash);
146 }
147 }
148 else if (entry->key == dummy && freeslot == NULL)
149 freeslot = entry;
150 }
151 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 *
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000160set_lookkey_unicode(PySetObject *so, PyObject *key, register Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000161{
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800162 register size_t i; /* Unsigned for defined overflow behavior. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 register size_t perturb;
164 register setentry *freeslot;
165 register size_t mask = so->mask;
166 setentry *table = so->table;
167 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 /* Make sure this function doesn't have to handle non-unicode keys,
170 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. */
173 if (!PyUnicode_CheckExact(key)) {
174 so->lookup = set_lookkey;
175 return set_lookkey(so, key, hash);
176 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100177 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 entry = &table[i];
179 if (entry->key == NULL || entry->key == key)
180 return entry;
181 if (entry->key == dummy)
182 freeslot = entry;
183 else {
184 if (entry->hash == hash && unicode_eq(entry->key, key))
185 return entry;
186 freeslot = NULL;
187 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 /* 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
199 && unicode_eq(entry->key, key)))
200 return entry;
201 if (entry->key == dummy && freeslot == NULL)
202 freeslot = entry;
203 }
204 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
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000214set_insert_key(register PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 register setentry *entry;
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, Py_hash_t);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 assert(so->lookup != NULL);
220 entry = so->lookup(so, key, hash);
221 if (entry == NULL)
222 return -1;
223 if (entry->key == NULL) {
224 /* UNUSED */
225 so->fill++;
226 entry->key = key;
227 entry->hash = hash;
228 so->used++;
229 } else if (entry->key == dummy) {
230 /* DUMMY */
231 entry->key = key;
232 entry->hash = hash;
233 so->used++;
234 Py_DECREF(dummy);
235 } else {
236 /* ACTIVE */
237 Py_DECREF(key);
238 }
239 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
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000251set_insert_clean(register PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 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;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000258
Mark Dickinson57e683e2011-09-24 18:18:40 +0100259 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 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++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000269}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 Py_ssize_t newsize;
280 setentry *oldtable, *newtable, *entry;
281 Py_ssize_t i;
282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* 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 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL);
300 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 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 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* 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;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
340 for (entry = oldtable; i > 0; entry++) {
341 if (entry->key == NULL) {
342 /* UNUSED */
343 ;
344 } else if (entry->key == dummy) {
345 /* DUMMY */
346 --i;
347 assert(entry->key == dummy);
348 Py_DECREF(entry->key);
349 } else {
350 /* ACTIVE */
351 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000352 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 }
354 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (is_oldtable_malloced)
357 PyMem_DEL(oldtable);
358 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000359}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 register Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000367 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100368 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 assert(so->fill <= so->mask); /* at least one empty slot */
371 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000372 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100373 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000374 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return -1;
376 }
377 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
378 return 0;
379 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000380}
381
382static int
383set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000384{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000385 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200389 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 hash = PyObject_Hash(key);
391 if (hash == -1)
392 return -1;
393 }
394 assert(so->fill <= so->mask); /* at least one empty slot */
395 n_used = so->used;
396 Py_INCREF(key);
397 if (set_insert_key(so, key, hash) == -1) {
398 Py_DECREF(key);
399 return -1;
400 }
401 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
402 return 0;
403 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000404}
405
406#define DISCARD_NOTFOUND 0
407#define DISCARD_FOUND 1
408
409static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000410set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411{ register setentry *entry;
412 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000413
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000414 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (entry == NULL)
416 return -1;
417 if (entry->key == NULL || entry->key == dummy)
418 return DISCARD_NOTFOUND;
419 old_key = entry->key;
420 Py_INCREF(dummy);
421 entry->key = dummy;
422 so->used--;
423 Py_DECREF(old_key);
424 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000425}
426
427static int
428set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000429{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000430 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 register setentry *entry;
432 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200437 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 hash = PyObject_Hash(key);
439 if (hash == -1)
440 return -1;
441 }
442 entry = (so->lookup)(so, key, hash);
443 if (entry == NULL)
444 return -1;
445 if (entry->key == NULL || entry->key == dummy)
446 return DISCARD_NOTFOUND;
447 old_key = entry->key;
448 Py_INCREF(dummy);
449 entry->key = dummy;
450 so->used--;
451 Py_DECREF(old_key);
452 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453}
454
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000455static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456set_clear_internal(PySetObject *so)
457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 setentry *entry, *table;
459 int table_is_malloced;
460 Py_ssize_t fill;
461 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 Py_ssize_t i, n;
464 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 n = so->mask + 1;
467 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000468#endif
469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 table = so->table;
471 assert(table != NULL);
472 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 /* This is delicate. During the process of clearing the set,
475 * decrefs can cause the set to mutate. To avoid fatal confusion
476 * (voice of experience), we have to make the set empty before
477 * clearing the slots, and never refer to anything via so->ref while
478 * clearing.
479 */
480 fill = so->fill;
481 if (table_is_malloced)
482 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 else if (fill > 0) {
485 /* It's a small table with something that needs to be cleared.
486 * Afraid the only safe way is to copy the set entries into
487 * another small table first.
488 */
489 memcpy(small_copy, table, sizeof(small_copy));
490 table = small_copy;
491 EMPTY_TO_MINSIZE(so);
492 }
493 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 /* Now we can finally clear things. If C had refcounts, we could
496 * assert that the refcount on table is 1 now, i.e. that this function
497 * has unique access to it, so decref side-effects can't alter it.
498 */
499 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 assert(i < n);
502 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 if (entry->key) {
505 --fill;
506 Py_DECREF(entry->key);
507 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 else
510 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000511#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 if (table_is_malloced)
515 PyMem_DEL(table);
516 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000517}
518
519/*
520 * Iterate over a set table. Use like so:
521 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000522 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000523 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000524 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000525 * while (set_next(yourset, &pos, &entry)) {
526 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000527 * }
528 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000529 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000531 */
532static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000533set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 Py_ssize_t i;
536 Py_ssize_t mask;
537 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 assert (PyAnySet_Check(so));
540 i = *pos_ptr;
541 assert(i >= 0);
542 table = so->table;
543 mask = so->mask;
544 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
545 i++;
546 *pos_ptr = i+1;
547 if (i > mask)
548 return 0;
549 assert(table[i].key != NULL);
550 *entry_ptr = &table[i];
551 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000552}
553
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000554static void
555set_dealloc(PySetObject *so)
556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 register setentry *entry;
558 Py_ssize_t fill = so->fill;
559 PyObject_GC_UnTrack(so);
560 Py_TRASHCAN_SAFE_BEGIN(so)
561 if (so->weakreflist != NULL)
562 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 for (entry = so->table; fill > 0; entry++) {
565 if (entry->key) {
566 --fill;
567 Py_DECREF(entry->key);
568 }
569 }
570 if (so->table != so->smalltable)
571 PyMem_DEL(so->table);
572 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
573 free_list[numfree++] = so;
574 else
575 Py_TYPE(so)->tp_free(so);
576 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000577}
578
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000579static PyObject *
580set_repr(PySetObject *so)
581{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200582 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 if (status != 0) {
586 if (status < 0)
587 return NULL;
588 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
589 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /* shortcut for the empty set */
592 if (!so->used) {
593 Py_ReprLeave((PyObject*)so);
594 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
595 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 keys = PySequence_List((PyObject *)so);
598 if (keys == NULL)
599 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000600
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200601 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 listrepr = PyObject_Repr(keys);
603 Py_DECREF(keys);
604 if (listrepr == NULL)
605 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200606 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200608 if (tmp == NULL)
609 goto done;
610 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200611
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200612 if (Py_TYPE(so) != &PySet_Type)
613 result = PyUnicode_FromFormat("%s({%U})",
614 Py_TYPE(so)->tp_name,
615 listrepr);
616 else
617 result = PyUnicode_FromFormat("{%U}", listrepr);
618 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000619done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 Py_ReprLeave((PyObject*)so);
621 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000622}
623
Martin v. Löwis18e16552006-02-15 17:27:45 +0000624static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000625set_len(PyObject *so)
626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000628}
629
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000630static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000631set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000634 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100635 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 register Py_ssize_t i;
637 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 assert (PyAnySet_Check(so));
640 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 other = (PySetObject*)otherset;
643 if (other == so || other->used == 0)
644 /* a.update(a) or a.update({}); nothing to do */
645 return 0;
646 /* Do one big resize at the start, rather than
647 * incrementally resizing as we insert new keys. Expect
648 * that there will be no (or few) overlapping keys.
649 */
650 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
651 if (set_table_resize(so, (so->used + other->used)*2) != 0)
652 return -1;
653 }
654 for (i = 0; i <= other->mask; i++) {
655 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000656 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100657 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000658 if (key != NULL &&
659 key != dummy) {
660 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100661 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000662 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 return -1;
664 }
665 }
666 }
667 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668}
669
670static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000671set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000672{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000673 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200677 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 hash = PyObject_Hash(key);
679 if (hash == -1)
680 return -1;
681 }
682 entry = (so->lookup)(so, key, hash);
683 if (entry == NULL)
684 return -1;
685 key = entry->key;
686 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000687}
688
Raymond Hettingerc991db22005-08-11 07:58:45 +0000689static int
690set_contains_entry(PySetObject *so, setentry *entry)
691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 PyObject *key;
693 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000694
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000695 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 if (lu_entry == NULL)
697 return -1;
698 key = lu_entry->key;
699 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000700}
701
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000702static PyObject *
703set_pop(PySetObject *so)
704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 register Py_ssize_t i = 0;
706 register setentry *entry;
707 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 assert (PyAnySet_Check(so));
710 if (so->used == 0) {
711 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
712 return NULL;
713 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 /* Set entry to "the first" unused or dummy set entry. We abuse
716 * the hash field of slot 0 to hold a search finger:
717 * If slot 0 has a value, use slot 0.
718 * Else slot 0 is being used to hold a search finger,
719 * and we use its hash value as the first index to look.
720 */
721 entry = &so->table[0];
722 if (entry->key == NULL || entry->key == dummy) {
723 i = entry->hash;
724 /* The hash field may be a real hash value, or it may be a
725 * legit search finger, or it may be a once-legit search
726 * finger that's out of bounds now because it wrapped around
727 * or the table shrunk -- simply make sure it's in bounds now.
728 */
729 if (i > so->mask || i < 1)
730 i = 1; /* skip slot 0 */
731 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
732 i++;
733 if (i > so->mask)
734 i = 1;
735 }
736 }
737 key = entry->key;
738 Py_INCREF(dummy);
739 entry->key = dummy;
740 so->used--;
741 so->table[0].hash = i + 1; /* next place to start */
742 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000743}
744
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000745PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
746Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000747
748static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000749set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 Py_ssize_t pos = 0;
752 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 while (set_next(so, &pos, &entry))
755 Py_VISIT(entry->key);
756 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000757}
758
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000759static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000760frozenset_hash(PyObject *self)
761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800763 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 setentry *entry;
765 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (so->hash != -1)
768 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000769
Mark Dickinson57e683e2011-09-24 18:18:40 +0100770 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 while (set_next(so, &pos, &entry)) {
772 /* Work to increase the bit dispersion for closely spaced hash
773 values. The is important because some use cases have many
774 combinations of a small number of elements with nearby
775 hashes so that many distinct combinations collapse to only
776 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000777 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800778 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800780 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800782 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 so->hash = hash;
784 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000785}
786
Raymond Hettingera9d99362005-08-05 00:01:15 +0000787/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 PyObject_HEAD
791 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
792 Py_ssize_t si_used;
793 Py_ssize_t si_pos;
794 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000795} setiterobject;
796
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797static void
798setiter_dealloc(setiterobject *si)
799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 Py_XDECREF(si->si_set);
801 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000802}
803
804static int
805setiter_traverse(setiterobject *si, visitproc visit, void *arg)
806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 Py_VISIT(si->si_set);
808 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809}
810
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000811static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812setiter_len(setiterobject *si)
813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_ssize_t len = 0;
815 if (si->si_set != NULL && si->si_used == si->si_set->used)
816 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000817 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818}
819
Armin Rigof5b3e362006-02-11 21:32:43 +0000820PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000821
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000822static PyObject *setiter_iternext(setiterobject *si);
823
824static PyObject *
825setiter_reduce(setiterobject *si)
826{
827 PyObject *list;
828 setiterobject tmp;
829
830 list = PyList_New(0);
831 if (!list)
832 return NULL;
833
Ezio Melotti0e1af282012-09-28 16:43:40 +0300834 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000835 tmp = *si;
836 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300837
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000838 /* iterate the temporary into a list */
839 for(;;) {
840 PyObject *element = setiter_iternext(&tmp);
841 if (element) {
842 if (PyList_Append(list, element)) {
843 Py_DECREF(element);
844 Py_DECREF(list);
845 Py_XDECREF(tmp.si_set);
846 return NULL;
847 }
848 Py_DECREF(element);
849 } else
850 break;
851 }
852 Py_XDECREF(tmp.si_set);
853 /* check for error */
854 if (tmp.si_set != NULL) {
855 /* we have an error */
856 Py_DECREF(list);
857 return NULL;
858 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200859 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000860}
861
862PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
863
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000864static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000866 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868};
869
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000870static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 PyObject *key;
873 register Py_ssize_t i, mask;
874 register setentry *entry;
875 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (so == NULL)
878 return NULL;
879 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (si->si_used != so->used) {
882 PyErr_SetString(PyExc_RuntimeError,
883 "Set changed size during iteration");
884 si->si_used = -1; /* Make this state sticky */
885 return NULL;
886 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 i = si->si_pos;
889 assert(i>=0);
890 entry = so->table;
891 mask = so->mask;
892 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
893 i++;
894 si->si_pos = i+1;
895 if (i > mask)
896 goto fail;
897 si->len--;
898 key = entry[i].key;
899 Py_INCREF(key);
900 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000901
902fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 Py_DECREF(so);
904 si->si_set = NULL;
905 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000906}
907
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000908PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 PyVarObject_HEAD_INIT(&PyType_Type, 0)
910 "set_iterator", /* tp_name */
911 sizeof(setiterobject), /* tp_basicsize */
912 0, /* tp_itemsize */
913 /* methods */
914 (destructor)setiter_dealloc, /* tp_dealloc */
915 0, /* tp_print */
916 0, /* tp_getattr */
917 0, /* tp_setattr */
918 0, /* tp_reserved */
919 0, /* tp_repr */
920 0, /* tp_as_number */
921 0, /* tp_as_sequence */
922 0, /* tp_as_mapping */
923 0, /* tp_hash */
924 0, /* tp_call */
925 0, /* tp_str */
926 PyObject_GenericGetAttr, /* tp_getattro */
927 0, /* tp_setattro */
928 0, /* tp_as_buffer */
929 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
930 0, /* tp_doc */
931 (traverseproc)setiter_traverse, /* tp_traverse */
932 0, /* tp_clear */
933 0, /* tp_richcompare */
934 0, /* tp_weaklistoffset */
935 PyObject_SelfIter, /* tp_iter */
936 (iternextfunc)setiter_iternext, /* tp_iternext */
937 setiter_methods, /* tp_methods */
938 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000939};
940
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000941static PyObject *
942set_iter(PySetObject *so)
943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
945 if (si == NULL)
946 return NULL;
947 Py_INCREF(so);
948 si->si_set = so;
949 si->si_used = so->used;
950 si->si_pos = 0;
951 si->len = so->used;
952 _PyObject_GC_TRACK(si);
953 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000954}
955
Raymond Hettingerd7946662005-08-01 21:39:29 +0000956static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000957set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 if (PyAnySet_Check(other))
962 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 if (PyDict_CheckExact(other)) {
965 PyObject *value;
966 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000967 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 /* Do one big resize at the start, rather than
971 * incrementally resizing as we insert new keys. Expect
972 * that there will be no (or few) overlapping keys.
973 */
974 if (dictsize == -1)
975 return -1;
976 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
977 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
978 return -1;
979 }
980 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
981 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 an_entry.hash = hash;
984 an_entry.key = key;
985 if (set_add_entry(so, &an_entry) == -1)
986 return -1;
987 }
988 return 0;
989 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 it = PyObject_GetIter(other);
992 if (it == NULL)
993 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 while ((key = PyIter_Next(it)) != NULL) {
996 if (set_add_key(so, key) == -1) {
997 Py_DECREF(it);
998 Py_DECREF(key);
999 return -1;
1000 }
1001 Py_DECREF(key);
1002 }
1003 Py_DECREF(it);
1004 if (PyErr_Occurred())
1005 return -1;
1006 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001007}
1008
1009static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001010set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1015 PyObject *other = PyTuple_GET_ITEM(args, i);
1016 if (set_update_internal(so, other) == -1)
1017 return NULL;
1018 }
1019 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001020}
1021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001023"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001024
1025static PyObject *
1026make_new_set(PyTypeObject *type, PyObject *iterable)
1027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (dummy == NULL) { /* Auto-initialize dummy */
1031 dummy = PyUnicode_FromString("<dummy key>");
1032 if (dummy == NULL)
1033 return NULL;
1034 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 /* create PySetObject structure */
1037 if (numfree &&
1038 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1039 so = free_list[--numfree];
1040 assert (so != NULL && PyAnySet_CheckExact(so));
1041 Py_TYPE(so) = type;
1042 _Py_NewReference((PyObject *)so);
1043 EMPTY_TO_MINSIZE(so);
1044 PyObject_GC_Track(so);
1045 } else {
1046 so = (PySetObject *)type->tp_alloc(type, 0);
1047 if (so == NULL)
1048 return NULL;
1049 /* tp_alloc has already zeroed the structure */
1050 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1051 INIT_NONZERO_SET_SLOTS(so);
1052 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 so->lookup = set_lookkey_unicode;
1055 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 if (iterable != NULL) {
1058 if (set_update_internal(so, iterable) == -1) {
1059 Py_DECREF(so);
1060 return NULL;
1061 }
1062 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001065}
1066
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001067static PyObject *
1068make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1071 if (PyType_IsSubtype(type, &PySet_Type))
1072 type = &PySet_Type;
1073 else
1074 type = &PyFrozenSet_Type;
1075 }
1076 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001077}
1078
Raymond Hettingerd7946662005-08-01 21:39:29 +00001079/* The empty frozenset is a singleton */
1080static PyObject *emptyfrozenset = NULL;
1081
Raymond Hettingera690a992003-11-16 16:17:49 +00001082static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001083frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1088 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1091 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (type != &PyFrozenSet_Type)
1094 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 if (iterable != NULL) {
1097 /* frozenset(f) is idempotent */
1098 if (PyFrozenSet_CheckExact(iterable)) {
1099 Py_INCREF(iterable);
1100 return iterable;
1101 }
1102 result = make_new_set(type, iterable);
1103 if (result == NULL || PySet_GET_SIZE(result))
1104 return result;
1105 Py_DECREF(result);
1106 }
1107 /* The empty frozenset is a singleton */
1108 if (emptyfrozenset == NULL)
1109 emptyfrozenset = make_new_set(type, NULL);
1110 Py_XINCREF(emptyfrozenset);
1111 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001112}
1113
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001114int
1115PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001116{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001117 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 while (numfree) {
1121 numfree--;
1122 so = free_list[numfree];
1123 PyObject_GC_Del(so);
1124 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001125 return freelist_size;
1126}
1127
1128void
1129PySet_Fini(void)
1130{
1131 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 Py_CLEAR(dummy);
1133 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001134}
1135
David Malcolm49526f42012-06-22 14:55:41 -04001136/* Print summary info about the state of the optimized allocator */
1137void
1138_PySet_DebugMallocStats(FILE *out)
1139{
1140 _PyDebugAllocatorStats(out,
1141 "free PySetObject",
1142 numfree, sizeof(PySetObject));
1143}
1144
1145
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001146static PyObject *
1147set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1150 return NULL;
1151
1152 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001153}
1154
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001155/* set_swap_bodies() switches the contents of any two sets by moving their
1156 internal data pointers and, if needed, copying the internal smalltables.
1157 Semantically equivalent to:
1158
1159 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1160
1161 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001163 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001165 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001166*/
1167
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001168static void
1169set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 Py_ssize_t t;
1172 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001173 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001175 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 t = a->fill; a->fill = b->fill; b->fill = t;
1178 t = a->used; a->used = b->used; b->used = t;
1179 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 u = a->table;
1182 if (a->table == a->smalltable)
1183 u = b->smalltable;
1184 a->table = b->table;
1185 if (b->table == b->smalltable)
1186 a->table = a->smalltable;
1187 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 if (a->table == a->smalltable || b->table == b->smalltable) {
1192 memcpy(tab, a->smalltable, sizeof(tab));
1193 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1194 memcpy(b->smalltable, tab, sizeof(tab));
1195 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1198 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1199 h = a->hash; a->hash = b->hash; b->hash = h;
1200 } else {
1201 a->hash = -1;
1202 b->hash = -1;
1203 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001204}
1205
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001206static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001207set_copy(PySetObject *so)
1208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001210}
1211
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001212static PyObject *
1213frozenset_copy(PySetObject *so)
1214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (PyFrozenSet_CheckExact(so)) {
1216 Py_INCREF(so);
1217 return (PyObject *)so;
1218 }
1219 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001220}
1221
Raymond Hettingera690a992003-11-16 16:17:49 +00001222PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1223
1224static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001225set_clear(PySetObject *so)
1226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 set_clear_internal(so);
1228 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001229}
1230
1231PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1232
1233static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001234set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 PySetObject *result;
1237 PyObject *other;
1238 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 result = (PySetObject *)set_copy(so);
1241 if (result == NULL)
1242 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1245 other = PyTuple_GET_ITEM(args, i);
1246 if ((PyObject *)so == other)
1247 continue;
1248 if (set_update_internal(result, other) == -1) {
1249 Py_DECREF(result);
1250 return NULL;
1251 }
1252 }
1253 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001254}
1255
1256PyDoc_STRVAR(union_doc,
1257 "Return the union of sets as a new set.\n\
1258\n\
1259(i.e. all elements that are in either set.)");
1260
1261static PyObject *
1262set_or(PySetObject *so, PyObject *other)
1263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001265
Brian Curtindfc80e32011-08-10 20:28:54 -05001266 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1267 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 result = (PySetObject *)set_copy(so);
1270 if (result == NULL)
1271 return NULL;
1272 if ((PyObject *)so == other)
1273 return (PyObject *)result;
1274 if (set_update_internal(result, other) == -1) {
1275 Py_DECREF(result);
1276 return NULL;
1277 }
1278 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001279}
1280
Raymond Hettingera690a992003-11-16 16:17:49 +00001281static PyObject *
1282set_ior(PySetObject *so, PyObject *other)
1283{
Brian Curtindfc80e32011-08-10 20:28:54 -05001284 if (!PyAnySet_Check(other))
1285 Py_RETURN_NOTIMPLEMENTED;
1286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (set_update_internal(so, other) == -1)
1288 return NULL;
1289 Py_INCREF(so);
1290 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001291}
1292
1293static PyObject *
1294set_intersection(PySetObject *so, PyObject *other)
1295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 PySetObject *result;
1297 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 if ((PyObject *)so == other)
1300 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1303 if (result == NULL)
1304 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 if (PyAnySet_Check(other)) {
1307 Py_ssize_t pos = 0;
1308 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1311 tmp = (PyObject *)so;
1312 so = (PySetObject *)other;
1313 other = tmp;
1314 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 while (set_next((PySetObject *)other, &pos, &entry)) {
1317 int rv = set_contains_entry(so, entry);
1318 if (rv == -1) {
1319 Py_DECREF(result);
1320 return NULL;
1321 }
1322 if (rv) {
1323 if (set_add_entry(result, entry) == -1) {
1324 Py_DECREF(result);
1325 return NULL;
1326 }
1327 }
1328 }
1329 return (PyObject *)result;
1330 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 it = PyObject_GetIter(other);
1333 if (it == NULL) {
1334 Py_DECREF(result);
1335 return NULL;
1336 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 while ((key = PyIter_Next(it)) != NULL) {
1339 int rv;
1340 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001341 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (hash == -1) {
1344 Py_DECREF(it);
1345 Py_DECREF(result);
1346 Py_DECREF(key);
1347 return NULL;
1348 }
1349 entry.hash = hash;
1350 entry.key = key;
1351 rv = set_contains_entry(so, &entry);
1352 if (rv == -1) {
1353 Py_DECREF(it);
1354 Py_DECREF(result);
1355 Py_DECREF(key);
1356 return NULL;
1357 }
1358 if (rv) {
1359 if (set_add_entry(result, &entry) == -1) {
1360 Py_DECREF(it);
1361 Py_DECREF(result);
1362 Py_DECREF(key);
1363 return NULL;
1364 }
1365 }
1366 Py_DECREF(key);
1367 }
1368 Py_DECREF(it);
1369 if (PyErr_Occurred()) {
1370 Py_DECREF(result);
1371 return NULL;
1372 }
1373 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001374}
1375
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001376static PyObject *
1377set_intersection_multi(PySetObject *so, PyObject *args)
1378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 Py_ssize_t i;
1380 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (PyTuple_GET_SIZE(args) == 0)
1383 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 Py_INCREF(so);
1386 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1387 PyObject *other = PyTuple_GET_ITEM(args, i);
1388 PyObject *newresult = set_intersection((PySetObject *)result, other);
1389 if (newresult == NULL) {
1390 Py_DECREF(result);
1391 return NULL;
1392 }
1393 Py_DECREF(result);
1394 result = newresult;
1395 }
1396 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001397}
1398
Raymond Hettingera690a992003-11-16 16:17:49 +00001399PyDoc_STRVAR(intersection_doc,
1400"Return the intersection of two sets as a new set.\n\
1401\n\
1402(i.e. all elements that are in both sets.)");
1403
1404static PyObject *
1405set_intersection_update(PySetObject *so, PyObject *other)
1406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 tmp = set_intersection(so, other);
1410 if (tmp == NULL)
1411 return NULL;
1412 set_swap_bodies(so, (PySetObject *)tmp);
1413 Py_DECREF(tmp);
1414 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001415}
1416
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001417static PyObject *
1418set_intersection_update_multi(PySetObject *so, PyObject *args)
1419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 tmp = set_intersection_multi(so, args);
1423 if (tmp == NULL)
1424 return NULL;
1425 set_swap_bodies(so, (PySetObject *)tmp);
1426 Py_DECREF(tmp);
1427 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001428}
1429
Raymond Hettingera690a992003-11-16 16:17:49 +00001430PyDoc_STRVAR(intersection_update_doc,
1431"Update a set with the intersection of itself and another.");
1432
1433static PyObject *
1434set_and(PySetObject *so, PyObject *other)
1435{
Brian Curtindfc80e32011-08-10 20:28:54 -05001436 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1437 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001439}
1440
1441static PyObject *
1442set_iand(PySetObject *so, PyObject *other)
1443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001445
Brian Curtindfc80e32011-08-10 20:28:54 -05001446 if (!PyAnySet_Check(other))
1447 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 result = set_intersection_update(so, other);
1449 if (result == NULL)
1450 return NULL;
1451 Py_DECREF(result);
1452 Py_INCREF(so);
1453 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001454}
1455
Guido van Rossum58da9312007-11-10 23:39:45 +00001456static PyObject *
1457set_isdisjoint(PySetObject *so, PyObject *other)
1458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 if ((PyObject *)so == other) {
1462 if (PySet_GET_SIZE(so) == 0)
1463 Py_RETURN_TRUE;
1464 else
1465 Py_RETURN_FALSE;
1466 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 if (PyAnySet_CheckExact(other)) {
1469 Py_ssize_t pos = 0;
1470 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1473 tmp = (PyObject *)so;
1474 so = (PySetObject *)other;
1475 other = tmp;
1476 }
1477 while (set_next((PySetObject *)other, &pos, &entry)) {
1478 int rv = set_contains_entry(so, entry);
1479 if (rv == -1)
1480 return NULL;
1481 if (rv)
1482 Py_RETURN_FALSE;
1483 }
1484 Py_RETURN_TRUE;
1485 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 it = PyObject_GetIter(other);
1488 if (it == NULL)
1489 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 while ((key = PyIter_Next(it)) != NULL) {
1492 int rv;
1493 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001494 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 if (hash == -1) {
1497 Py_DECREF(key);
1498 Py_DECREF(it);
1499 return NULL;
1500 }
1501 entry.hash = hash;
1502 entry.key = key;
1503 rv = set_contains_entry(so, &entry);
1504 Py_DECREF(key);
1505 if (rv == -1) {
1506 Py_DECREF(it);
1507 return NULL;
1508 }
1509 if (rv) {
1510 Py_DECREF(it);
1511 Py_RETURN_FALSE;
1512 }
1513 }
1514 Py_DECREF(it);
1515 if (PyErr_Occurred())
1516 return NULL;
1517 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001518}
1519
1520PyDoc_STRVAR(isdisjoint_doc,
1521"Return True if two sets have a null intersection.");
1522
Neal Norwitz6576bd82005-11-13 18:41:28 +00001523static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001524set_difference_update_internal(PySetObject *so, PyObject *other)
1525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 if ((PyObject *)so == other)
1527 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 if (PyAnySet_Check(other)) {
1530 setentry *entry;
1531 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 while (set_next((PySetObject *)other, &pos, &entry))
1534 if (set_discard_entry(so, entry) == -1)
1535 return -1;
1536 } else {
1537 PyObject *key, *it;
1538 it = PyObject_GetIter(other);
1539 if (it == NULL)
1540 return -1;
1541
1542 while ((key = PyIter_Next(it)) != NULL) {
1543 if (set_discard_key(so, key) == -1) {
1544 Py_DECREF(it);
1545 Py_DECREF(key);
1546 return -1;
1547 }
1548 Py_DECREF(key);
1549 }
1550 Py_DECREF(it);
1551 if (PyErr_Occurred())
1552 return -1;
1553 }
1554 /* If more than 1/5 are dummies, then resize them away. */
1555 if ((so->fill - so->used) * 5 < so->mask)
1556 return 0;
1557 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001558}
1559
Raymond Hettingera690a992003-11-16 16:17:49 +00001560static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001561set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1566 PyObject *other = PyTuple_GET_ITEM(args, i);
1567 if (set_difference_update_internal(so, other) == -1)
1568 return NULL;
1569 }
1570 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001571}
1572
1573PyDoc_STRVAR(difference_update_doc,
1574"Remove all elements of another set from this set.");
1575
1576static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001577set_copy_and_difference(PySetObject *so, PyObject *other)
1578{
1579 PyObject *result;
1580
1581 result = set_copy(so);
1582 if (result == NULL)
1583 return NULL;
1584 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1585 return result;
1586 Py_DECREF(result);
1587 return NULL;
1588}
1589
1590static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001591set_difference(PySetObject *so, PyObject *other)
1592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 PyObject *result;
1594 setentry *entry;
1595 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001598 return set_copy_and_difference(so, other);
1599 }
1600
1601 /* If len(so) much more than len(other), it's more efficient to simply copy
1602 * so and then iterate other looking for common elements. */
1603 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1604 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 result = make_new_set_basetype(Py_TYPE(so), NULL);
1608 if (result == NULL)
1609 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 if (PyDict_CheckExact(other)) {
1612 while (set_next(so, &pos, &entry)) {
1613 setentry entrycopy;
1614 entrycopy.hash = entry->hash;
1615 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001616 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1618 Py_DECREF(result);
1619 return NULL;
1620 }
1621 }
1622 }
1623 return result;
1624 }
1625
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001626 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 while (set_next(so, &pos, &entry)) {
1628 int rv = set_contains_entry((PySetObject *)other, entry);
1629 if (rv == -1) {
1630 Py_DECREF(result);
1631 return NULL;
1632 }
1633 if (!rv) {
1634 if (set_add_entry((PySetObject *)result, entry) == -1) {
1635 Py_DECREF(result);
1636 return NULL;
1637 }
1638 }
1639 }
1640 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001641}
1642
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001643static PyObject *
1644set_difference_multi(PySetObject *so, PyObject *args)
1645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 Py_ssize_t i;
1647 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (PyTuple_GET_SIZE(args) == 0)
1650 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 other = PyTuple_GET_ITEM(args, 0);
1653 result = set_difference(so, other);
1654 if (result == NULL)
1655 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1658 other = PyTuple_GET_ITEM(args, i);
1659 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1660 Py_DECREF(result);
1661 return NULL;
1662 }
1663 }
1664 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001665}
1666
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001667PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001668"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001669\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001670(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001671static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001672set_sub(PySetObject *so, PyObject *other)
1673{
Brian Curtindfc80e32011-08-10 20:28:54 -05001674 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1675 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001677}
1678
1679static PyObject *
1680set_isub(PySetObject *so, PyObject *other)
1681{
Brian Curtindfc80e32011-08-10 20:28:54 -05001682 if (!PyAnySet_Check(other))
1683 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (set_difference_update_internal(so, other) == -1)
1685 return NULL;
1686 Py_INCREF(so);
1687 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001688}
1689
1690static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001691set_symmetric_difference_update(PySetObject *so, PyObject *other)
1692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 PySetObject *otherset;
1694 PyObject *key;
1695 Py_ssize_t pos = 0;
1696 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 if ((PyObject *)so == other)
1699 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 if (PyDict_CheckExact(other)) {
1702 PyObject *value;
1703 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001704 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1706 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001707
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001708 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 an_entry.hash = hash;
1710 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001713 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001714 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001717 if (rv == DISCARD_NOTFOUND) {
1718 if (set_add_entry(so, &an_entry) == -1) {
1719 Py_DECREF(key);
1720 return NULL;
1721 }
1722 }
Georg Brandl2d444492010-09-03 10:52:55 +00001723 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 }
1725 Py_RETURN_NONE;
1726 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 if (PyAnySet_Check(other)) {
1729 Py_INCREF(other);
1730 otherset = (PySetObject *)other;
1731 } else {
1732 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1733 if (otherset == NULL)
1734 return NULL;
1735 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 while (set_next(otherset, &pos, &entry)) {
1738 int rv = set_discard_entry(so, entry);
1739 if (rv == -1) {
1740 Py_DECREF(otherset);
1741 return NULL;
1742 }
1743 if (rv == DISCARD_NOTFOUND) {
1744 if (set_add_entry(so, entry) == -1) {
1745 Py_DECREF(otherset);
1746 return NULL;
1747 }
1748 }
1749 }
1750 Py_DECREF(otherset);
1751 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001752}
1753
1754PyDoc_STRVAR(symmetric_difference_update_doc,
1755"Update a set with the symmetric difference of itself and another.");
1756
1757static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001758set_symmetric_difference(PySetObject *so, PyObject *other)
1759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 PyObject *rv;
1761 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1764 if (otherset == NULL)
1765 return NULL;
1766 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1767 if (rv == NULL)
1768 return NULL;
1769 Py_DECREF(rv);
1770 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001771}
1772
1773PyDoc_STRVAR(symmetric_difference_doc,
1774"Return the symmetric difference of two sets as a new set.\n\
1775\n\
1776(i.e. all elements that are in exactly one of the sets.)");
1777
1778static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001779set_xor(PySetObject *so, PyObject *other)
1780{
Brian Curtindfc80e32011-08-10 20:28:54 -05001781 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1782 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001784}
1785
1786static PyObject *
1787set_ixor(PySetObject *so, PyObject *other)
1788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001790
Brian Curtindfc80e32011-08-10 20:28:54 -05001791 if (!PyAnySet_Check(other))
1792 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 result = set_symmetric_difference_update(so, other);
1794 if (result == NULL)
1795 return NULL;
1796 Py_DECREF(result);
1797 Py_INCREF(so);
1798 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001799}
1800
1801static PyObject *
1802set_issubset(PySetObject *so, PyObject *other)
1803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 setentry *entry;
1805 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 if (!PyAnySet_Check(other)) {
1808 PyObject *tmp, *result;
1809 tmp = make_new_set(&PySet_Type, other);
1810 if (tmp == NULL)
1811 return NULL;
1812 result = set_issubset(so, tmp);
1813 Py_DECREF(tmp);
1814 return result;
1815 }
1816 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1817 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 while (set_next(so, &pos, &entry)) {
1820 int rv = set_contains_entry((PySetObject *)other, entry);
1821 if (rv == -1)
1822 return NULL;
1823 if (!rv)
1824 Py_RETURN_FALSE;
1825 }
1826 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001827}
1828
1829PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1830
1831static PyObject *
1832set_issuperset(PySetObject *so, PyObject *other)
1833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 if (!PyAnySet_Check(other)) {
1837 tmp = make_new_set(&PySet_Type, other);
1838 if (tmp == NULL)
1839 return NULL;
1840 result = set_issuperset(so, tmp);
1841 Py_DECREF(tmp);
1842 return result;
1843 }
1844 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001845}
1846
1847PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1848
Raymond Hettingera690a992003-11-16 16:17:49 +00001849static PyObject *
1850set_richcompare(PySetObject *v, PyObject *w, int op)
1851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001853
Brian Curtindfc80e32011-08-10 20:28:54 -05001854 if(!PyAnySet_Check(w))
1855 Py_RETURN_NOTIMPLEMENTED;
1856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 switch (op) {
1858 case Py_EQ:
1859 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1860 Py_RETURN_FALSE;
1861 if (v->hash != -1 &&
1862 ((PySetObject *)w)->hash != -1 &&
1863 v->hash != ((PySetObject *)w)->hash)
1864 Py_RETURN_FALSE;
1865 return set_issubset(v, w);
1866 case Py_NE:
1867 r1 = set_richcompare(v, w, Py_EQ);
1868 if (r1 == NULL)
1869 return NULL;
1870 r2 = PyBool_FromLong(PyObject_Not(r1));
1871 Py_DECREF(r1);
1872 return r2;
1873 case Py_LE:
1874 return set_issubset(v, w);
1875 case Py_GE:
1876 return set_issuperset(v, w);
1877 case Py_LT:
1878 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1879 Py_RETURN_FALSE;
1880 return set_issubset(v, w);
1881 case Py_GT:
1882 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1883 Py_RETURN_FALSE;
1884 return set_issuperset(v, w);
1885 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001886 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001887}
1888
Raymond Hettingera690a992003-11-16 16:17:49 +00001889static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001890set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 if (set_add_key(so, key) == -1)
1893 return NULL;
1894 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001895}
1896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001898"Add an element to a set.\n\
1899\n\
1900This has no effect if the element is already present.");
1901
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001902static int
1903set_contains(PySetObject *so, PyObject *key)
1904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 PyObject *tmpkey;
1906 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 rv = set_contains_key(so, key);
1909 if (rv == -1) {
1910 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1911 return -1;
1912 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001913 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 if (tmpkey == NULL)
1915 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001916 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 Py_DECREF(tmpkey);
1918 }
1919 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001920}
1921
1922static PyObject *
1923set_direct_contains(PySetObject *so, PyObject *key)
1924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 result = set_contains(so, key);
1928 if (result == -1)
1929 return NULL;
1930 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001931}
1932
1933PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1934
Raymond Hettingera690a992003-11-16 16:17:49 +00001935static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001936set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 PyObject *tmpkey;
1939 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 rv = set_discard_key(so, key);
1942 if (rv == -1) {
1943 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1944 return NULL;
1945 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001946 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 if (tmpkey == NULL)
1948 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 Py_DECREF(tmpkey);
1951 if (rv == -1)
1952 return NULL;
1953 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (rv == DISCARD_NOTFOUND) {
1956 set_key_error(key);
1957 return NULL;
1958 }
1959 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001960}
1961
1962PyDoc_STRVAR(remove_doc,
1963"Remove an element from a set; it must be a member.\n\
1964\n\
1965If the element is not a member, raise a KeyError.");
1966
1967static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001968set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001969{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001970 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 rv = set_discard_key(so, key);
1974 if (rv == -1) {
1975 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1976 return NULL;
1977 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001978 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 if (tmpkey == NULL)
1980 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001981 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001983 if (rv == -1)
1984 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 }
1986 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001987}
1988
1989PyDoc_STRVAR(discard_doc,
1990"Remove an element from a set if it is a member.\n\
1991\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001993
1994static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001995set_reduce(PySetObject *so)
1996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001998 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 keys = PySequence_List((PyObject *)so);
2001 if (keys == NULL)
2002 goto done;
2003 args = PyTuple_Pack(1, keys);
2004 if (args == NULL)
2005 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002006 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 if (dict == NULL) {
2008 PyErr_Clear();
2009 dict = Py_None;
2010 Py_INCREF(dict);
2011 }
2012 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002013done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 Py_XDECREF(args);
2015 Py_XDECREF(keys);
2016 Py_XDECREF(dict);
2017 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002018}
2019
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002020static PyObject *
2021set_sizeof(PySetObject *so)
2022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 res = sizeof(PySetObject);
2026 if (so->table != so->smalltable)
2027 res = res + (so->mask + 1) * sizeof(setentry);
2028 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002029}
2030
2031PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002032static int
2033set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 if (!PyAnySet_Check(self))
2038 return -1;
2039 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2040 return -1;
2041 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2042 return -1;
2043 set_clear_internal(self);
2044 self->hash = -1;
2045 if (iterable == NULL)
2046 return 0;
2047 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002048}
2049
Raymond Hettingera690a992003-11-16 16:17:49 +00002050static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 set_len, /* sq_length */
2052 0, /* sq_concat */
2053 0, /* sq_repeat */
2054 0, /* sq_item */
2055 0, /* sq_slice */
2056 0, /* sq_ass_item */
2057 0, /* sq_ass_slice */
2058 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002059};
2060
2061/* set object ********************************************************/
2062
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002063#ifdef Py_DEBUG
2064static PyObject *test_c_api(PySetObject *so);
2065
2066PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2067All is well if assertions don't fail.");
2068#endif
2069
Raymond Hettingera690a992003-11-16 16:17:49 +00002070static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 {"add", (PyCFunction)set_add, METH_O,
2072 add_doc},
2073 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2074 clear_doc},
2075 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2076 contains_doc},
2077 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2078 copy_doc},
2079 {"discard", (PyCFunction)set_discard, METH_O,
2080 discard_doc},
2081 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2082 difference_doc},
2083 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2084 difference_update_doc},
2085 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2086 intersection_doc},
2087 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2088 intersection_update_doc},
2089 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2090 isdisjoint_doc},
2091 {"issubset", (PyCFunction)set_issubset, METH_O,
2092 issubset_doc},
2093 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2094 issuperset_doc},
2095 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2096 pop_doc},
2097 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2098 reduce_doc},
2099 {"remove", (PyCFunction)set_remove, METH_O,
2100 remove_doc},
2101 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2102 sizeof_doc},
2103 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2104 symmetric_difference_doc},
2105 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2106 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002107#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2109 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002110#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 {"union", (PyCFunction)set_union, METH_VARARGS,
2112 union_doc},
2113 {"update", (PyCFunction)set_update, METH_VARARGS,
2114 update_doc},
2115 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002116};
2117
2118static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 0, /*nb_add*/
2120 (binaryfunc)set_sub, /*nb_subtract*/
2121 0, /*nb_multiply*/
2122 0, /*nb_remainder*/
2123 0, /*nb_divmod*/
2124 0, /*nb_power*/
2125 0, /*nb_negative*/
2126 0, /*nb_positive*/
2127 0, /*nb_absolute*/
2128 0, /*nb_bool*/
2129 0, /*nb_invert*/
2130 0, /*nb_lshift*/
2131 0, /*nb_rshift*/
2132 (binaryfunc)set_and, /*nb_and*/
2133 (binaryfunc)set_xor, /*nb_xor*/
2134 (binaryfunc)set_or, /*nb_or*/
2135 0, /*nb_int*/
2136 0, /*nb_reserved*/
2137 0, /*nb_float*/
2138 0, /*nb_inplace_add*/
2139 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2140 0, /*nb_inplace_multiply*/
2141 0, /*nb_inplace_remainder*/
2142 0, /*nb_inplace_power*/
2143 0, /*nb_inplace_lshift*/
2144 0, /*nb_inplace_rshift*/
2145 (binaryfunc)set_iand, /*nb_inplace_and*/
2146 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2147 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002148};
2149
2150PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002151"set() -> new empty set object\n\
2152set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002153\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002154Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002155
2156PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2158 "set", /* tp_name */
2159 sizeof(PySetObject), /* tp_basicsize */
2160 0, /* tp_itemsize */
2161 /* methods */
2162 (destructor)set_dealloc, /* tp_dealloc */
2163 0, /* tp_print */
2164 0, /* tp_getattr */
2165 0, /* tp_setattr */
2166 0, /* tp_reserved */
2167 (reprfunc)set_repr, /* tp_repr */
2168 &set_as_number, /* tp_as_number */
2169 &set_as_sequence, /* tp_as_sequence */
2170 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002171 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 0, /* tp_call */
2173 0, /* tp_str */
2174 PyObject_GenericGetAttr, /* tp_getattro */
2175 0, /* tp_setattro */
2176 0, /* tp_as_buffer */
2177 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2178 Py_TPFLAGS_BASETYPE, /* tp_flags */
2179 set_doc, /* tp_doc */
2180 (traverseproc)set_traverse, /* tp_traverse */
2181 (inquiry)set_clear_internal, /* tp_clear */
2182 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002183 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2184 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 0, /* tp_iternext */
2186 set_methods, /* tp_methods */
2187 0, /* tp_members */
2188 0, /* tp_getset */
2189 0, /* tp_base */
2190 0, /* tp_dict */
2191 0, /* tp_descr_get */
2192 0, /* tp_descr_set */
2193 0, /* tp_dictoffset */
2194 (initproc)set_init, /* tp_init */
2195 PyType_GenericAlloc, /* tp_alloc */
2196 set_new, /* tp_new */
2197 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002198};
2199
2200/* frozenset object ********************************************************/
2201
2202
2203static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2205 contains_doc},
2206 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2207 copy_doc},
2208 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2209 difference_doc},
2210 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2211 intersection_doc},
2212 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2213 isdisjoint_doc},
2214 {"issubset", (PyCFunction)set_issubset, METH_O,
2215 issubset_doc},
2216 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2217 issuperset_doc},
2218 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2219 reduce_doc},
2220 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2221 sizeof_doc},
2222 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2223 symmetric_difference_doc},
2224 {"union", (PyCFunction)set_union, METH_VARARGS,
2225 union_doc},
2226 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002227};
2228
2229static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 0, /*nb_add*/
2231 (binaryfunc)set_sub, /*nb_subtract*/
2232 0, /*nb_multiply*/
2233 0, /*nb_remainder*/
2234 0, /*nb_divmod*/
2235 0, /*nb_power*/
2236 0, /*nb_negative*/
2237 0, /*nb_positive*/
2238 0, /*nb_absolute*/
2239 0, /*nb_bool*/
2240 0, /*nb_invert*/
2241 0, /*nb_lshift*/
2242 0, /*nb_rshift*/
2243 (binaryfunc)set_and, /*nb_and*/
2244 (binaryfunc)set_xor, /*nb_xor*/
2245 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002246};
2247
2248PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002249"frozenset() -> empty frozenset object\n\
2250frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002251\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002252Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002253
2254PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2256 "frozenset", /* tp_name */
2257 sizeof(PySetObject), /* tp_basicsize */
2258 0, /* tp_itemsize */
2259 /* methods */
2260 (destructor)set_dealloc, /* tp_dealloc */
2261 0, /* tp_print */
2262 0, /* tp_getattr */
2263 0, /* tp_setattr */
2264 0, /* tp_reserved */
2265 (reprfunc)set_repr, /* tp_repr */
2266 &frozenset_as_number, /* tp_as_number */
2267 &set_as_sequence, /* tp_as_sequence */
2268 0, /* tp_as_mapping */
2269 frozenset_hash, /* tp_hash */
2270 0, /* tp_call */
2271 0, /* tp_str */
2272 PyObject_GenericGetAttr, /* tp_getattro */
2273 0, /* tp_setattro */
2274 0, /* tp_as_buffer */
2275 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2276 Py_TPFLAGS_BASETYPE, /* tp_flags */
2277 frozenset_doc, /* tp_doc */
2278 (traverseproc)set_traverse, /* tp_traverse */
2279 (inquiry)set_clear_internal, /* tp_clear */
2280 (richcmpfunc)set_richcompare, /* tp_richcompare */
2281 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2282 (getiterfunc)set_iter, /* tp_iter */
2283 0, /* tp_iternext */
2284 frozenset_methods, /* tp_methods */
2285 0, /* tp_members */
2286 0, /* tp_getset */
2287 0, /* tp_base */
2288 0, /* tp_dict */
2289 0, /* tp_descr_get */
2290 0, /* tp_descr_set */
2291 0, /* tp_dictoffset */
2292 0, /* tp_init */
2293 PyType_GenericAlloc, /* tp_alloc */
2294 frozenset_new, /* tp_new */
2295 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002296};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002297
2298
2299/***** C API functions *************************************************/
2300
2301PyObject *
2302PySet_New(PyObject *iterable)
2303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002305}
2306
2307PyObject *
2308PyFrozenSet_New(PyObject *iterable)
2309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002311}
2312
Neal Norwitz8c49c822006-03-04 18:41:19 +00002313Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002314PySet_Size(PyObject *anyset)
2315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 if (!PyAnySet_Check(anyset)) {
2317 PyErr_BadInternalCall();
2318 return -1;
2319 }
2320 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002321}
2322
2323int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002324PySet_Clear(PyObject *set)
2325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 if (!PySet_Check(set)) {
2327 PyErr_BadInternalCall();
2328 return -1;
2329 }
2330 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002331}
2332
2333int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002334PySet_Contains(PyObject *anyset, PyObject *key)
2335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 if (!PyAnySet_Check(anyset)) {
2337 PyErr_BadInternalCall();
2338 return -1;
2339 }
2340 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002341}
2342
2343int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002344PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 if (!PySet_Check(set)) {
2347 PyErr_BadInternalCall();
2348 return -1;
2349 }
2350 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002351}
2352
2353int
Christian Heimesfd66e512008-01-29 12:18:50 +00002354PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if (!PySet_Check(anyset) &&
2357 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2358 PyErr_BadInternalCall();
2359 return -1;
2360 }
2361 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002362}
2363
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002364int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002365_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 if (!PyAnySet_Check(set)) {
2370 PyErr_BadInternalCall();
2371 return -1;
2372 }
2373 if (set_next((PySetObject *)set, pos, &entry) == 0)
2374 return 0;
2375 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002376 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002378}
2379
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002380PyObject *
2381PySet_Pop(PyObject *set)
2382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 if (!PySet_Check(set)) {
2384 PyErr_BadInternalCall();
2385 return NULL;
2386 }
2387 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002388}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002389
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002390int
2391_PySet_Update(PyObject *set, PyObject *iterable)
2392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 if (!PySet_Check(set)) {
2394 PyErr_BadInternalCall();
2395 return -1;
2396 }
2397 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002398}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002399
2400#ifdef Py_DEBUG
2401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002403 Returns True and original set is restored. */
2404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405#define assertRaises(call_return_value, exception) \
2406 do { \
2407 assert(call_return_value); \
2408 assert(PyErr_ExceptionMatches(exception)); \
2409 PyErr_Clear(); \
2410 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002411
2412static PyObject *
2413test_c_api(PySetObject *so)
2414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 Py_ssize_t count;
2416 char *s;
2417 Py_ssize_t i;
2418 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2419 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002420 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 /* Verify preconditions */
2424 assert(PyAnySet_Check(ob));
2425 assert(PyAnySet_CheckExact(ob));
2426 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 /* so.clear(); so |= set("abc"); */
2429 str = PyUnicode_FromString("abc");
2430 if (str == NULL)
2431 return NULL;
2432 set_clear_internal(so);
2433 if (set_update_internal(so, str) == -1) {
2434 Py_DECREF(str);
2435 return NULL;
2436 }
2437 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* Exercise type/size checks */
2440 assert(PySet_Size(ob) == 3);
2441 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 /* Raise TypeError for non-iterable constructor arguments */
2444 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2445 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 /* Raise TypeError for unhashable key */
2448 dup = PySet_New(ob);
2449 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2450 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2451 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 /* Exercise successful pop, contains, add, and discard */
2454 elem = PySet_Pop(ob);
2455 assert(PySet_Contains(ob, elem) == 0);
2456 assert(PySet_GET_SIZE(ob) == 2);
2457 assert(PySet_Add(ob, elem) == 0);
2458 assert(PySet_Contains(ob, elem) == 1);
2459 assert(PySet_GET_SIZE(ob) == 3);
2460 assert(PySet_Discard(ob, elem) == 1);
2461 assert(PySet_GET_SIZE(ob) == 2);
2462 assert(PySet_Discard(ob, elem) == 0);
2463 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 /* Exercise clear */
2466 dup2 = PySet_New(dup);
2467 assert(PySet_Clear(dup2) == 0);
2468 assert(PySet_Size(dup2) == 0);
2469 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 /* Raise SystemError on clear or update of frozen set */
2472 f = PyFrozenSet_New(dup);
2473 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2474 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2475 assert(PySet_Add(f, elem) == 0);
2476 Py_INCREF(f);
2477 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2478 Py_DECREF(f);
2479 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 /* Exercise direct iteration */
2482 i = 0, count = 0;
2483 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2484 s = _PyUnicode_AsString(x);
2485 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2486 count++;
2487 }
2488 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 /* Exercise updates */
2491 dup2 = PySet_New(NULL);
2492 assert(_PySet_Update(dup2, dup) == 0);
2493 assert(PySet_Size(dup2) == 3);
2494 assert(_PySet_Update(dup2, dup) == 0);
2495 assert(PySet_Size(dup2) == 3);
2496 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 /* Raise SystemError when self argument is not a set or frozenset. */
2499 t = PyTuple_New(0);
2500 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2501 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2502 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 /* Raise SystemError when self argument is not a set. */
2505 f = PyFrozenSet_New(dup);
2506 assert(PySet_Size(f) == 3);
2507 assert(PyFrozenSet_CheckExact(f));
2508 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2509 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2510 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 /* Raise KeyError when popping from an empty set */
2513 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2514 Py_DECREF(ob);
2515 assert(PySet_GET_SIZE(ob) == 0);
2516 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 /* Restore the set from the copy using the PyNumber API */
2519 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2520 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 /* Verify constructors accept NULL arguments */
2523 f = PySet_New(NULL);
2524 assert(f != NULL);
2525 assert(PySet_GET_SIZE(f) == 0);
2526 Py_DECREF(f);
2527 f = PyFrozenSet_New(NULL);
2528 assert(f != NULL);
2529 assert(PyFrozenSet_CheckExact(f));
2530 assert(PySet_GET_SIZE(f) == 0);
2531 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 Py_DECREF(elem);
2534 Py_DECREF(dup);
2535 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002536}
2537
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002538#undef assertRaises
2539
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002540#endif