blob: b903fbee8052f034fd1724ffc118454a7a7c1e35 [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{
Mark Dickinson57e683e2011-09-24 18:18:40 +010080 register size_t i;
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{
Mark Dickinson57e683e2011-09-24 18:18:40 +0100162 register size_t i;
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;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100763 Py_uhash_t h, hash = 1927868237U;
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;
Mark Dickinson57e683e2011-09-24 18:18:40 +0100778 hash ^= (h ^ (h << 16) ^ 89869747U) * 3644798167U;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100780 hash = hash * 69069U + 907133923U;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 if (hash == -1)
Mark Dickinson57e683e2011-09-24 18:18:40 +0100782 hash = 590923713U;
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
834 /* copy the itertor state */
835 tmp = *si;
836 Py_XINCREF(tmp.si_set);
837
838 /* 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
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001136static PyObject *
1137set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1140 return NULL;
1141
1142 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001143}
1144
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001145/* set_swap_bodies() switches the contents of any two sets by moving their
1146 internal data pointers and, if needed, copying the internal smalltables.
1147 Semantically equivalent to:
1148
1149 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1150
1151 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001153 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001155 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001156*/
1157
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001158static void
1159set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 Py_ssize_t t;
1162 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001163 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001165 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 t = a->fill; a->fill = b->fill; b->fill = t;
1168 t = a->used; a->used = b->used; b->used = t;
1169 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 u = a->table;
1172 if (a->table == a->smalltable)
1173 u = b->smalltable;
1174 a->table = b->table;
1175 if (b->table == b->smalltable)
1176 a->table = a->smalltable;
1177 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 if (a->table == a->smalltable || b->table == b->smalltable) {
1182 memcpy(tab, a->smalltable, sizeof(tab));
1183 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1184 memcpy(b->smalltable, tab, sizeof(tab));
1185 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1188 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1189 h = a->hash; a->hash = b->hash; b->hash = h;
1190 } else {
1191 a->hash = -1;
1192 b->hash = -1;
1193 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001194}
1195
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001196static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001197set_copy(PySetObject *so)
1198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001200}
1201
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001202static PyObject *
1203frozenset_copy(PySetObject *so)
1204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 if (PyFrozenSet_CheckExact(so)) {
1206 Py_INCREF(so);
1207 return (PyObject *)so;
1208 }
1209 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001210}
1211
Raymond Hettingera690a992003-11-16 16:17:49 +00001212PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1213
1214static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001215set_clear(PySetObject *so)
1216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 set_clear_internal(so);
1218 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001219}
1220
1221PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1222
1223static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001224set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 PySetObject *result;
1227 PyObject *other;
1228 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 result = (PySetObject *)set_copy(so);
1231 if (result == NULL)
1232 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1235 other = PyTuple_GET_ITEM(args, i);
1236 if ((PyObject *)so == other)
1237 continue;
1238 if (set_update_internal(result, other) == -1) {
1239 Py_DECREF(result);
1240 return NULL;
1241 }
1242 }
1243 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001244}
1245
1246PyDoc_STRVAR(union_doc,
1247 "Return the union of sets as a new set.\n\
1248\n\
1249(i.e. all elements that are in either set.)");
1250
1251static PyObject *
1252set_or(PySetObject *so, PyObject *other)
1253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001255
Brian Curtindfc80e32011-08-10 20:28:54 -05001256 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1257 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 result = (PySetObject *)set_copy(so);
1260 if (result == NULL)
1261 return NULL;
1262 if ((PyObject *)so == other)
1263 return (PyObject *)result;
1264 if (set_update_internal(result, other) == -1) {
1265 Py_DECREF(result);
1266 return NULL;
1267 }
1268 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001269}
1270
Raymond Hettingera690a992003-11-16 16:17:49 +00001271static PyObject *
1272set_ior(PySetObject *so, PyObject *other)
1273{
Brian Curtindfc80e32011-08-10 20:28:54 -05001274 if (!PyAnySet_Check(other))
1275 Py_RETURN_NOTIMPLEMENTED;
1276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (set_update_internal(so, other) == -1)
1278 return NULL;
1279 Py_INCREF(so);
1280 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001281}
1282
1283static PyObject *
1284set_intersection(PySetObject *so, PyObject *other)
1285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 PySetObject *result;
1287 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if ((PyObject *)so == other)
1290 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1293 if (result == NULL)
1294 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 if (PyAnySet_Check(other)) {
1297 Py_ssize_t pos = 0;
1298 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1301 tmp = (PyObject *)so;
1302 so = (PySetObject *)other;
1303 other = tmp;
1304 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 while (set_next((PySetObject *)other, &pos, &entry)) {
1307 int rv = set_contains_entry(so, entry);
1308 if (rv == -1) {
1309 Py_DECREF(result);
1310 return NULL;
1311 }
1312 if (rv) {
1313 if (set_add_entry(result, entry) == -1) {
1314 Py_DECREF(result);
1315 return NULL;
1316 }
1317 }
1318 }
1319 return (PyObject *)result;
1320 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 it = PyObject_GetIter(other);
1323 if (it == NULL) {
1324 Py_DECREF(result);
1325 return NULL;
1326 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 while ((key = PyIter_Next(it)) != NULL) {
1329 int rv;
1330 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001331 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (hash == -1) {
1334 Py_DECREF(it);
1335 Py_DECREF(result);
1336 Py_DECREF(key);
1337 return NULL;
1338 }
1339 entry.hash = hash;
1340 entry.key = key;
1341 rv = set_contains_entry(so, &entry);
1342 if (rv == -1) {
1343 Py_DECREF(it);
1344 Py_DECREF(result);
1345 Py_DECREF(key);
1346 return NULL;
1347 }
1348 if (rv) {
1349 if (set_add_entry(result, &entry) == -1) {
1350 Py_DECREF(it);
1351 Py_DECREF(result);
1352 Py_DECREF(key);
1353 return NULL;
1354 }
1355 }
1356 Py_DECREF(key);
1357 }
1358 Py_DECREF(it);
1359 if (PyErr_Occurred()) {
1360 Py_DECREF(result);
1361 return NULL;
1362 }
1363 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001364}
1365
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001366static PyObject *
1367set_intersection_multi(PySetObject *so, PyObject *args)
1368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 Py_ssize_t i;
1370 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 if (PyTuple_GET_SIZE(args) == 0)
1373 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 Py_INCREF(so);
1376 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1377 PyObject *other = PyTuple_GET_ITEM(args, i);
1378 PyObject *newresult = set_intersection((PySetObject *)result, other);
1379 if (newresult == NULL) {
1380 Py_DECREF(result);
1381 return NULL;
1382 }
1383 Py_DECREF(result);
1384 result = newresult;
1385 }
1386 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001387}
1388
Raymond Hettingera690a992003-11-16 16:17:49 +00001389PyDoc_STRVAR(intersection_doc,
1390"Return the intersection of two sets as a new set.\n\
1391\n\
1392(i.e. all elements that are in both sets.)");
1393
1394static PyObject *
1395set_intersection_update(PySetObject *so, PyObject *other)
1396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 tmp = set_intersection(so, other);
1400 if (tmp == NULL)
1401 return NULL;
1402 set_swap_bodies(so, (PySetObject *)tmp);
1403 Py_DECREF(tmp);
1404 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001405}
1406
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001407static PyObject *
1408set_intersection_update_multi(PySetObject *so, PyObject *args)
1409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 tmp = set_intersection_multi(so, args);
1413 if (tmp == NULL)
1414 return NULL;
1415 set_swap_bodies(so, (PySetObject *)tmp);
1416 Py_DECREF(tmp);
1417 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001418}
1419
Raymond Hettingera690a992003-11-16 16:17:49 +00001420PyDoc_STRVAR(intersection_update_doc,
1421"Update a set with the intersection of itself and another.");
1422
1423static PyObject *
1424set_and(PySetObject *so, PyObject *other)
1425{
Brian Curtindfc80e32011-08-10 20:28:54 -05001426 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1427 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001429}
1430
1431static PyObject *
1432set_iand(PySetObject *so, PyObject *other)
1433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001435
Brian Curtindfc80e32011-08-10 20:28:54 -05001436 if (!PyAnySet_Check(other))
1437 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 result = set_intersection_update(so, other);
1439 if (result == NULL)
1440 return NULL;
1441 Py_DECREF(result);
1442 Py_INCREF(so);
1443 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001444}
1445
Guido van Rossum58da9312007-11-10 23:39:45 +00001446static PyObject *
1447set_isdisjoint(PySetObject *so, PyObject *other)
1448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 if ((PyObject *)so == other) {
1452 if (PySet_GET_SIZE(so) == 0)
1453 Py_RETURN_TRUE;
1454 else
1455 Py_RETURN_FALSE;
1456 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 if (PyAnySet_CheckExact(other)) {
1459 Py_ssize_t pos = 0;
1460 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1463 tmp = (PyObject *)so;
1464 so = (PySetObject *)other;
1465 other = tmp;
1466 }
1467 while (set_next((PySetObject *)other, &pos, &entry)) {
1468 int rv = set_contains_entry(so, entry);
1469 if (rv == -1)
1470 return NULL;
1471 if (rv)
1472 Py_RETURN_FALSE;
1473 }
1474 Py_RETURN_TRUE;
1475 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 it = PyObject_GetIter(other);
1478 if (it == NULL)
1479 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 while ((key = PyIter_Next(it)) != NULL) {
1482 int rv;
1483 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001484 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (hash == -1) {
1487 Py_DECREF(key);
1488 Py_DECREF(it);
1489 return NULL;
1490 }
1491 entry.hash = hash;
1492 entry.key = key;
1493 rv = set_contains_entry(so, &entry);
1494 Py_DECREF(key);
1495 if (rv == -1) {
1496 Py_DECREF(it);
1497 return NULL;
1498 }
1499 if (rv) {
1500 Py_DECREF(it);
1501 Py_RETURN_FALSE;
1502 }
1503 }
1504 Py_DECREF(it);
1505 if (PyErr_Occurred())
1506 return NULL;
1507 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001508}
1509
1510PyDoc_STRVAR(isdisjoint_doc,
1511"Return True if two sets have a null intersection.");
1512
Neal Norwitz6576bd82005-11-13 18:41:28 +00001513static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001514set_difference_update_internal(PySetObject *so, PyObject *other)
1515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if ((PyObject *)so == other)
1517 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 if (PyAnySet_Check(other)) {
1520 setentry *entry;
1521 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 while (set_next((PySetObject *)other, &pos, &entry))
1524 if (set_discard_entry(so, entry) == -1)
1525 return -1;
1526 } else {
1527 PyObject *key, *it;
1528 it = PyObject_GetIter(other);
1529 if (it == NULL)
1530 return -1;
1531
1532 while ((key = PyIter_Next(it)) != NULL) {
1533 if (set_discard_key(so, key) == -1) {
1534 Py_DECREF(it);
1535 Py_DECREF(key);
1536 return -1;
1537 }
1538 Py_DECREF(key);
1539 }
1540 Py_DECREF(it);
1541 if (PyErr_Occurred())
1542 return -1;
1543 }
1544 /* If more than 1/5 are dummies, then resize them away. */
1545 if ((so->fill - so->used) * 5 < so->mask)
1546 return 0;
1547 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001548}
1549
Raymond Hettingera690a992003-11-16 16:17:49 +00001550static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001551set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1556 PyObject *other = PyTuple_GET_ITEM(args, i);
1557 if (set_difference_update_internal(so, other) == -1)
1558 return NULL;
1559 }
1560 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001561}
1562
1563PyDoc_STRVAR(difference_update_doc,
1564"Remove all elements of another set from this set.");
1565
1566static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001567set_copy_and_difference(PySetObject *so, PyObject *other)
1568{
1569 PyObject *result;
1570
1571 result = set_copy(so);
1572 if (result == NULL)
1573 return NULL;
1574 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1575 return result;
1576 Py_DECREF(result);
1577 return NULL;
1578}
1579
1580static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001581set_difference(PySetObject *so, PyObject *other)
1582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 PyObject *result;
1584 setentry *entry;
1585 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001588 return set_copy_and_difference(so, other);
1589 }
1590
1591 /* If len(so) much more than len(other), it's more efficient to simply copy
1592 * so and then iterate other looking for common elements. */
1593 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1594 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 result = make_new_set_basetype(Py_TYPE(so), NULL);
1598 if (result == NULL)
1599 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 if (PyDict_CheckExact(other)) {
1602 while (set_next(so, &pos, &entry)) {
1603 setentry entrycopy;
1604 entrycopy.hash = entry->hash;
1605 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001606 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1608 Py_DECREF(result);
1609 return NULL;
1610 }
1611 }
1612 }
1613 return result;
1614 }
1615
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001616 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 while (set_next(so, &pos, &entry)) {
1618 int rv = set_contains_entry((PySetObject *)other, entry);
1619 if (rv == -1) {
1620 Py_DECREF(result);
1621 return NULL;
1622 }
1623 if (!rv) {
1624 if (set_add_entry((PySetObject *)result, entry) == -1) {
1625 Py_DECREF(result);
1626 return NULL;
1627 }
1628 }
1629 }
1630 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001631}
1632
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001633static PyObject *
1634set_difference_multi(PySetObject *so, PyObject *args)
1635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 Py_ssize_t i;
1637 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 if (PyTuple_GET_SIZE(args) == 0)
1640 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 other = PyTuple_GET_ITEM(args, 0);
1643 result = set_difference(so, other);
1644 if (result == NULL)
1645 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1648 other = PyTuple_GET_ITEM(args, i);
1649 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1650 Py_DECREF(result);
1651 return NULL;
1652 }
1653 }
1654 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001655}
1656
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001657PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001658"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001659\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001660(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001661static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001662set_sub(PySetObject *so, PyObject *other)
1663{
Brian Curtindfc80e32011-08-10 20:28:54 -05001664 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1665 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001667}
1668
1669static PyObject *
1670set_isub(PySetObject *so, PyObject *other)
1671{
Brian Curtindfc80e32011-08-10 20:28:54 -05001672 if (!PyAnySet_Check(other))
1673 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 if (set_difference_update_internal(so, other) == -1)
1675 return NULL;
1676 Py_INCREF(so);
1677 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001678}
1679
1680static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001681set_symmetric_difference_update(PySetObject *so, PyObject *other)
1682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 PySetObject *otherset;
1684 PyObject *key;
1685 Py_ssize_t pos = 0;
1686 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if ((PyObject *)so == other)
1689 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 if (PyDict_CheckExact(other)) {
1692 PyObject *value;
1693 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001694 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1696 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001697
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001698 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 an_entry.hash = hash;
1700 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001703 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001704 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001707 if (rv == DISCARD_NOTFOUND) {
1708 if (set_add_entry(so, &an_entry) == -1) {
1709 Py_DECREF(key);
1710 return NULL;
1711 }
1712 }
Georg Brandl2d444492010-09-03 10:52:55 +00001713 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 }
1715 Py_RETURN_NONE;
1716 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 if (PyAnySet_Check(other)) {
1719 Py_INCREF(other);
1720 otherset = (PySetObject *)other;
1721 } else {
1722 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1723 if (otherset == NULL)
1724 return NULL;
1725 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 while (set_next(otherset, &pos, &entry)) {
1728 int rv = set_discard_entry(so, entry);
1729 if (rv == -1) {
1730 Py_DECREF(otherset);
1731 return NULL;
1732 }
1733 if (rv == DISCARD_NOTFOUND) {
1734 if (set_add_entry(so, entry) == -1) {
1735 Py_DECREF(otherset);
1736 return NULL;
1737 }
1738 }
1739 }
1740 Py_DECREF(otherset);
1741 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001742}
1743
1744PyDoc_STRVAR(symmetric_difference_update_doc,
1745"Update a set with the symmetric difference of itself and another.");
1746
1747static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001748set_symmetric_difference(PySetObject *so, PyObject *other)
1749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 PyObject *rv;
1751 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1754 if (otherset == NULL)
1755 return NULL;
1756 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1757 if (rv == NULL)
1758 return NULL;
1759 Py_DECREF(rv);
1760 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001761}
1762
1763PyDoc_STRVAR(symmetric_difference_doc,
1764"Return the symmetric difference of two sets as a new set.\n\
1765\n\
1766(i.e. all elements that are in exactly one of the sets.)");
1767
1768static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001769set_xor(PySetObject *so, PyObject *other)
1770{
Brian Curtindfc80e32011-08-10 20:28:54 -05001771 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1772 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001774}
1775
1776static PyObject *
1777set_ixor(PySetObject *so, PyObject *other)
1778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001780
Brian Curtindfc80e32011-08-10 20:28:54 -05001781 if (!PyAnySet_Check(other))
1782 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 result = set_symmetric_difference_update(so, other);
1784 if (result == NULL)
1785 return NULL;
1786 Py_DECREF(result);
1787 Py_INCREF(so);
1788 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001789}
1790
1791static PyObject *
1792set_issubset(PySetObject *so, PyObject *other)
1793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 setentry *entry;
1795 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 if (!PyAnySet_Check(other)) {
1798 PyObject *tmp, *result;
1799 tmp = make_new_set(&PySet_Type, other);
1800 if (tmp == NULL)
1801 return NULL;
1802 result = set_issubset(so, tmp);
1803 Py_DECREF(tmp);
1804 return result;
1805 }
1806 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1807 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 while (set_next(so, &pos, &entry)) {
1810 int rv = set_contains_entry((PySetObject *)other, entry);
1811 if (rv == -1)
1812 return NULL;
1813 if (!rv)
1814 Py_RETURN_FALSE;
1815 }
1816 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001817}
1818
1819PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1820
1821static PyObject *
1822set_issuperset(PySetObject *so, PyObject *other)
1823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 if (!PyAnySet_Check(other)) {
1827 tmp = make_new_set(&PySet_Type, other);
1828 if (tmp == NULL)
1829 return NULL;
1830 result = set_issuperset(so, tmp);
1831 Py_DECREF(tmp);
1832 return result;
1833 }
1834 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001835}
1836
1837PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1838
Raymond Hettingera690a992003-11-16 16:17:49 +00001839static PyObject *
1840set_richcompare(PySetObject *v, PyObject *w, int op)
1841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001843
Brian Curtindfc80e32011-08-10 20:28:54 -05001844 if(!PyAnySet_Check(w))
1845 Py_RETURN_NOTIMPLEMENTED;
1846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 switch (op) {
1848 case Py_EQ:
1849 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1850 Py_RETURN_FALSE;
1851 if (v->hash != -1 &&
1852 ((PySetObject *)w)->hash != -1 &&
1853 v->hash != ((PySetObject *)w)->hash)
1854 Py_RETURN_FALSE;
1855 return set_issubset(v, w);
1856 case Py_NE:
1857 r1 = set_richcompare(v, w, Py_EQ);
1858 if (r1 == NULL)
1859 return NULL;
1860 r2 = PyBool_FromLong(PyObject_Not(r1));
1861 Py_DECREF(r1);
1862 return r2;
1863 case Py_LE:
1864 return set_issubset(v, w);
1865 case Py_GE:
1866 return set_issuperset(v, w);
1867 case Py_LT:
1868 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1869 Py_RETURN_FALSE;
1870 return set_issubset(v, w);
1871 case Py_GT:
1872 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1873 Py_RETURN_FALSE;
1874 return set_issuperset(v, w);
1875 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001876 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001877}
1878
Raymond Hettingera690a992003-11-16 16:17:49 +00001879static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001880set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 if (set_add_key(so, key) == -1)
1883 return NULL;
1884 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001885}
1886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001888"Add an element to a set.\n\
1889\n\
1890This has no effect if the element is already present.");
1891
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001892static int
1893set_contains(PySetObject *so, PyObject *key)
1894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 PyObject *tmpkey;
1896 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 rv = set_contains_key(so, key);
1899 if (rv == -1) {
1900 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1901 return -1;
1902 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001903 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 if (tmpkey == NULL)
1905 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001906 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 Py_DECREF(tmpkey);
1908 }
1909 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001910}
1911
1912static PyObject *
1913set_direct_contains(PySetObject *so, PyObject *key)
1914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 result = set_contains(so, key);
1918 if (result == -1)
1919 return NULL;
1920 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001921}
1922
1923PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1924
Raymond Hettingera690a992003-11-16 16:17:49 +00001925static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001926set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 PyObject *tmpkey;
1929 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 rv = set_discard_key(so, key);
1932 if (rv == -1) {
1933 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1934 return NULL;
1935 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001936 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 if (tmpkey == NULL)
1938 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 Py_DECREF(tmpkey);
1941 if (rv == -1)
1942 return NULL;
1943 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 if (rv == DISCARD_NOTFOUND) {
1946 set_key_error(key);
1947 return NULL;
1948 }
1949 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001950}
1951
1952PyDoc_STRVAR(remove_doc,
1953"Remove an element from a set; it must be a member.\n\
1954\n\
1955If the element is not a member, raise a KeyError.");
1956
1957static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001958set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001959{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001960 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 rv = set_discard_key(so, key);
1964 if (rv == -1) {
1965 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1966 return NULL;
1967 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001968 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 if (tmpkey == NULL)
1970 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001971 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001973 if (rv == -1)
1974 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 }
1976 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001977}
1978
1979PyDoc_STRVAR(discard_doc,
1980"Remove an element from a set if it is a member.\n\
1981\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001983
1984static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001985set_reduce(PySetObject *so)
1986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001988 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 keys = PySequence_List((PyObject *)so);
1991 if (keys == NULL)
1992 goto done;
1993 args = PyTuple_Pack(1, keys);
1994 if (args == NULL)
1995 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001996 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 if (dict == NULL) {
1998 PyErr_Clear();
1999 dict = Py_None;
2000 Py_INCREF(dict);
2001 }
2002 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002003done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 Py_XDECREF(args);
2005 Py_XDECREF(keys);
2006 Py_XDECREF(dict);
2007 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002008}
2009
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002010static PyObject *
2011set_sizeof(PySetObject *so)
2012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 res = sizeof(PySetObject);
2016 if (so->table != so->smalltable)
2017 res = res + (so->mask + 1) * sizeof(setentry);
2018 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002019}
2020
2021PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002022static int
2023set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 if (!PyAnySet_Check(self))
2028 return -1;
2029 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2030 return -1;
2031 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2032 return -1;
2033 set_clear_internal(self);
2034 self->hash = -1;
2035 if (iterable == NULL)
2036 return 0;
2037 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002038}
2039
Raymond Hettingera690a992003-11-16 16:17:49 +00002040static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 set_len, /* sq_length */
2042 0, /* sq_concat */
2043 0, /* sq_repeat */
2044 0, /* sq_item */
2045 0, /* sq_slice */
2046 0, /* sq_ass_item */
2047 0, /* sq_ass_slice */
2048 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002049};
2050
2051/* set object ********************************************************/
2052
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002053#ifdef Py_DEBUG
2054static PyObject *test_c_api(PySetObject *so);
2055
2056PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2057All is well if assertions don't fail.");
2058#endif
2059
Raymond Hettingera690a992003-11-16 16:17:49 +00002060static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 {"add", (PyCFunction)set_add, METH_O,
2062 add_doc},
2063 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2064 clear_doc},
2065 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2066 contains_doc},
2067 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2068 copy_doc},
2069 {"discard", (PyCFunction)set_discard, METH_O,
2070 discard_doc},
2071 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2072 difference_doc},
2073 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2074 difference_update_doc},
2075 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2076 intersection_doc},
2077 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2078 intersection_update_doc},
2079 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2080 isdisjoint_doc},
2081 {"issubset", (PyCFunction)set_issubset, METH_O,
2082 issubset_doc},
2083 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2084 issuperset_doc},
2085 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2086 pop_doc},
2087 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2088 reduce_doc},
2089 {"remove", (PyCFunction)set_remove, METH_O,
2090 remove_doc},
2091 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2092 sizeof_doc},
2093 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2094 symmetric_difference_doc},
2095 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2096 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002097#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2099 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002100#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 {"union", (PyCFunction)set_union, METH_VARARGS,
2102 union_doc},
2103 {"update", (PyCFunction)set_update, METH_VARARGS,
2104 update_doc},
2105 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002106};
2107
2108static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 0, /*nb_add*/
2110 (binaryfunc)set_sub, /*nb_subtract*/
2111 0, /*nb_multiply*/
2112 0, /*nb_remainder*/
2113 0, /*nb_divmod*/
2114 0, /*nb_power*/
2115 0, /*nb_negative*/
2116 0, /*nb_positive*/
2117 0, /*nb_absolute*/
2118 0, /*nb_bool*/
2119 0, /*nb_invert*/
2120 0, /*nb_lshift*/
2121 0, /*nb_rshift*/
2122 (binaryfunc)set_and, /*nb_and*/
2123 (binaryfunc)set_xor, /*nb_xor*/
2124 (binaryfunc)set_or, /*nb_or*/
2125 0, /*nb_int*/
2126 0, /*nb_reserved*/
2127 0, /*nb_float*/
2128 0, /*nb_inplace_add*/
2129 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2130 0, /*nb_inplace_multiply*/
2131 0, /*nb_inplace_remainder*/
2132 0, /*nb_inplace_power*/
2133 0, /*nb_inplace_lshift*/
2134 0, /*nb_inplace_rshift*/
2135 (binaryfunc)set_iand, /*nb_inplace_and*/
2136 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2137 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002138};
2139
2140PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002141"set() -> new empty set object\n\
2142set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002143\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002144Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002145
2146PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2148 "set", /* tp_name */
2149 sizeof(PySetObject), /* tp_basicsize */
2150 0, /* tp_itemsize */
2151 /* methods */
2152 (destructor)set_dealloc, /* tp_dealloc */
2153 0, /* tp_print */
2154 0, /* tp_getattr */
2155 0, /* tp_setattr */
2156 0, /* tp_reserved */
2157 (reprfunc)set_repr, /* tp_repr */
2158 &set_as_number, /* tp_as_number */
2159 &set_as_sequence, /* tp_as_sequence */
2160 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002161 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 0, /* tp_call */
2163 0, /* tp_str */
2164 PyObject_GenericGetAttr, /* tp_getattro */
2165 0, /* tp_setattro */
2166 0, /* tp_as_buffer */
2167 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2168 Py_TPFLAGS_BASETYPE, /* tp_flags */
2169 set_doc, /* tp_doc */
2170 (traverseproc)set_traverse, /* tp_traverse */
2171 (inquiry)set_clear_internal, /* tp_clear */
2172 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002173 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2174 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 0, /* tp_iternext */
2176 set_methods, /* tp_methods */
2177 0, /* tp_members */
2178 0, /* tp_getset */
2179 0, /* tp_base */
2180 0, /* tp_dict */
2181 0, /* tp_descr_get */
2182 0, /* tp_descr_set */
2183 0, /* tp_dictoffset */
2184 (initproc)set_init, /* tp_init */
2185 PyType_GenericAlloc, /* tp_alloc */
2186 set_new, /* tp_new */
2187 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002188};
2189
2190/* frozenset object ********************************************************/
2191
2192
2193static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2195 contains_doc},
2196 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2197 copy_doc},
2198 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2199 difference_doc},
2200 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2201 intersection_doc},
2202 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2203 isdisjoint_doc},
2204 {"issubset", (PyCFunction)set_issubset, METH_O,
2205 issubset_doc},
2206 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2207 issuperset_doc},
2208 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2209 reduce_doc},
2210 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2211 sizeof_doc},
2212 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2213 symmetric_difference_doc},
2214 {"union", (PyCFunction)set_union, METH_VARARGS,
2215 union_doc},
2216 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002217};
2218
2219static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 0, /*nb_add*/
2221 (binaryfunc)set_sub, /*nb_subtract*/
2222 0, /*nb_multiply*/
2223 0, /*nb_remainder*/
2224 0, /*nb_divmod*/
2225 0, /*nb_power*/
2226 0, /*nb_negative*/
2227 0, /*nb_positive*/
2228 0, /*nb_absolute*/
2229 0, /*nb_bool*/
2230 0, /*nb_invert*/
2231 0, /*nb_lshift*/
2232 0, /*nb_rshift*/
2233 (binaryfunc)set_and, /*nb_and*/
2234 (binaryfunc)set_xor, /*nb_xor*/
2235 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002236};
2237
2238PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002239"frozenset() -> empty frozenset object\n\
2240frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002241\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002242Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002243
2244PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2246 "frozenset", /* tp_name */
2247 sizeof(PySetObject), /* tp_basicsize */
2248 0, /* tp_itemsize */
2249 /* methods */
2250 (destructor)set_dealloc, /* tp_dealloc */
2251 0, /* tp_print */
2252 0, /* tp_getattr */
2253 0, /* tp_setattr */
2254 0, /* tp_reserved */
2255 (reprfunc)set_repr, /* tp_repr */
2256 &frozenset_as_number, /* tp_as_number */
2257 &set_as_sequence, /* tp_as_sequence */
2258 0, /* tp_as_mapping */
2259 frozenset_hash, /* tp_hash */
2260 0, /* tp_call */
2261 0, /* tp_str */
2262 PyObject_GenericGetAttr, /* tp_getattro */
2263 0, /* tp_setattro */
2264 0, /* tp_as_buffer */
2265 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2266 Py_TPFLAGS_BASETYPE, /* tp_flags */
2267 frozenset_doc, /* tp_doc */
2268 (traverseproc)set_traverse, /* tp_traverse */
2269 (inquiry)set_clear_internal, /* tp_clear */
2270 (richcmpfunc)set_richcompare, /* tp_richcompare */
2271 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2272 (getiterfunc)set_iter, /* tp_iter */
2273 0, /* tp_iternext */
2274 frozenset_methods, /* tp_methods */
2275 0, /* tp_members */
2276 0, /* tp_getset */
2277 0, /* tp_base */
2278 0, /* tp_dict */
2279 0, /* tp_descr_get */
2280 0, /* tp_descr_set */
2281 0, /* tp_dictoffset */
2282 0, /* tp_init */
2283 PyType_GenericAlloc, /* tp_alloc */
2284 frozenset_new, /* tp_new */
2285 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002286};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002287
2288
2289/***** C API functions *************************************************/
2290
2291PyObject *
2292PySet_New(PyObject *iterable)
2293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002295}
2296
2297PyObject *
2298PyFrozenSet_New(PyObject *iterable)
2299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002301}
2302
Neal Norwitz8c49c822006-03-04 18:41:19 +00002303Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002304PySet_Size(PyObject *anyset)
2305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 if (!PyAnySet_Check(anyset)) {
2307 PyErr_BadInternalCall();
2308 return -1;
2309 }
2310 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002311}
2312
2313int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002314PySet_Clear(PyObject *set)
2315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 if (!PySet_Check(set)) {
2317 PyErr_BadInternalCall();
2318 return -1;
2319 }
2320 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002321}
2322
2323int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002324PySet_Contains(PyObject *anyset, PyObject *key)
2325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 if (!PyAnySet_Check(anyset)) {
2327 PyErr_BadInternalCall();
2328 return -1;
2329 }
2330 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002331}
2332
2333int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002334PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 if (!PySet_Check(set)) {
2337 PyErr_BadInternalCall();
2338 return -1;
2339 }
2340 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002341}
2342
2343int
Christian Heimesfd66e512008-01-29 12:18:50 +00002344PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 if (!PySet_Check(anyset) &&
2347 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2348 PyErr_BadInternalCall();
2349 return -1;
2350 }
2351 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002352}
2353
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002354int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002355_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 if (!PyAnySet_Check(set)) {
2360 PyErr_BadInternalCall();
2361 return -1;
2362 }
2363 if (set_next((PySetObject *)set, pos, &entry) == 0)
2364 return 0;
2365 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002366 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002368}
2369
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002370PyObject *
2371PySet_Pop(PyObject *set)
2372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 if (!PySet_Check(set)) {
2374 PyErr_BadInternalCall();
2375 return NULL;
2376 }
2377 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002378}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002379
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002380int
2381_PySet_Update(PyObject *set, PyObject *iterable)
2382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 if (!PySet_Check(set)) {
2384 PyErr_BadInternalCall();
2385 return -1;
2386 }
2387 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002388}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002389
2390#ifdef Py_DEBUG
2391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002393 Returns True and original set is restored. */
2394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395#define assertRaises(call_return_value, exception) \
2396 do { \
2397 assert(call_return_value); \
2398 assert(PyErr_ExceptionMatches(exception)); \
2399 PyErr_Clear(); \
2400 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002401
2402static PyObject *
2403test_c_api(PySetObject *so)
2404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 Py_ssize_t count;
2406 char *s;
2407 Py_ssize_t i;
2408 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2409 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002410 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Verify preconditions */
2414 assert(PyAnySet_Check(ob));
2415 assert(PyAnySet_CheckExact(ob));
2416 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 /* so.clear(); so |= set("abc"); */
2419 str = PyUnicode_FromString("abc");
2420 if (str == NULL)
2421 return NULL;
2422 set_clear_internal(so);
2423 if (set_update_internal(so, str) == -1) {
2424 Py_DECREF(str);
2425 return NULL;
2426 }
2427 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* Exercise type/size checks */
2430 assert(PySet_Size(ob) == 3);
2431 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 /* Raise TypeError for non-iterable constructor arguments */
2434 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2435 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 /* Raise TypeError for unhashable key */
2438 dup = PySet_New(ob);
2439 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2440 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2441 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 /* Exercise successful pop, contains, add, and discard */
2444 elem = PySet_Pop(ob);
2445 assert(PySet_Contains(ob, elem) == 0);
2446 assert(PySet_GET_SIZE(ob) == 2);
2447 assert(PySet_Add(ob, elem) == 0);
2448 assert(PySet_Contains(ob, elem) == 1);
2449 assert(PySet_GET_SIZE(ob) == 3);
2450 assert(PySet_Discard(ob, elem) == 1);
2451 assert(PySet_GET_SIZE(ob) == 2);
2452 assert(PySet_Discard(ob, elem) == 0);
2453 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 /* Exercise clear */
2456 dup2 = PySet_New(dup);
2457 assert(PySet_Clear(dup2) == 0);
2458 assert(PySet_Size(dup2) == 0);
2459 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 /* Raise SystemError on clear or update of frozen set */
2462 f = PyFrozenSet_New(dup);
2463 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2464 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2465 assert(PySet_Add(f, elem) == 0);
2466 Py_INCREF(f);
2467 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2468 Py_DECREF(f);
2469 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 /* Exercise direct iteration */
2472 i = 0, count = 0;
2473 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2474 s = _PyUnicode_AsString(x);
2475 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2476 count++;
2477 }
2478 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 /* Exercise updates */
2481 dup2 = PySet_New(NULL);
2482 assert(_PySet_Update(dup2, dup) == 0);
2483 assert(PySet_Size(dup2) == 3);
2484 assert(_PySet_Update(dup2, dup) == 0);
2485 assert(PySet_Size(dup2) == 3);
2486 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 /* Raise SystemError when self argument is not a set or frozenset. */
2489 t = PyTuple_New(0);
2490 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2491 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2492 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 /* Raise SystemError when self argument is not a set. */
2495 f = PyFrozenSet_New(dup);
2496 assert(PySet_Size(f) == 3);
2497 assert(PyFrozenSet_CheckExact(f));
2498 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2499 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2500 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 /* Raise KeyError when popping from an empty set */
2503 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2504 Py_DECREF(ob);
2505 assert(PySet_GET_SIZE(ob) == 0);
2506 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 /* Restore the set from the copy using the PyNumber API */
2509 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2510 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 /* Verify constructors accept NULL arguments */
2513 f = PySet_New(NULL);
2514 assert(f != NULL);
2515 assert(PySet_GET_SIZE(f) == 0);
2516 Py_DECREF(f);
2517 f = PyFrozenSet_New(NULL);
2518 assert(f != NULL);
2519 assert(PyFrozenSet_CheckExact(f));
2520 assert(PySet_GET_SIZE(f) == 0);
2521 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 Py_DECREF(elem);
2524 Py_DECREF(dup);
2525 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002526}
2527
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002528#undef assertRaises
2529
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002530#endif