blob: 34d8204ffed5980220dfecdffed3322eae78960c [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
822static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
824 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825};
826
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000827static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 PyObject *key;
830 register Py_ssize_t i, mask;
831 register setentry *entry;
832 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 if (so == NULL)
835 return NULL;
836 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (si->si_used != so->used) {
839 PyErr_SetString(PyExc_RuntimeError,
840 "Set changed size during iteration");
841 si->si_used = -1; /* Make this state sticky */
842 return NULL;
843 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 i = si->si_pos;
846 assert(i>=0);
847 entry = so->table;
848 mask = so->mask;
849 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
850 i++;
851 si->si_pos = i+1;
852 if (i > mask)
853 goto fail;
854 si->len--;
855 key = entry[i].key;
856 Py_INCREF(key);
857 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000858
859fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 Py_DECREF(so);
861 si->si_set = NULL;
862 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000863}
864
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000865PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 PyVarObject_HEAD_INIT(&PyType_Type, 0)
867 "set_iterator", /* tp_name */
868 sizeof(setiterobject), /* tp_basicsize */
869 0, /* tp_itemsize */
870 /* methods */
871 (destructor)setiter_dealloc, /* tp_dealloc */
872 0, /* tp_print */
873 0, /* tp_getattr */
874 0, /* tp_setattr */
875 0, /* tp_reserved */
876 0, /* tp_repr */
877 0, /* tp_as_number */
878 0, /* tp_as_sequence */
879 0, /* tp_as_mapping */
880 0, /* tp_hash */
881 0, /* tp_call */
882 0, /* tp_str */
883 PyObject_GenericGetAttr, /* tp_getattro */
884 0, /* tp_setattro */
885 0, /* tp_as_buffer */
886 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
887 0, /* tp_doc */
888 (traverseproc)setiter_traverse, /* tp_traverse */
889 0, /* tp_clear */
890 0, /* tp_richcompare */
891 0, /* tp_weaklistoffset */
892 PyObject_SelfIter, /* tp_iter */
893 (iternextfunc)setiter_iternext, /* tp_iternext */
894 setiter_methods, /* tp_methods */
895 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000896};
897
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000898static PyObject *
899set_iter(PySetObject *so)
900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
902 if (si == NULL)
903 return NULL;
904 Py_INCREF(so);
905 si->si_set = so;
906 si->si_used = so->used;
907 si->si_pos = 0;
908 si->len = so->used;
909 _PyObject_GC_TRACK(si);
910 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000911}
912
Raymond Hettingerd7946662005-08-01 21:39:29 +0000913static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000914set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (PyAnySet_Check(other))
919 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if (PyDict_CheckExact(other)) {
922 PyObject *value;
923 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000924 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 /* Do one big resize at the start, rather than
928 * incrementally resizing as we insert new keys. Expect
929 * that there will be no (or few) overlapping keys.
930 */
931 if (dictsize == -1)
932 return -1;
933 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
934 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
935 return -1;
936 }
937 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
938 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 an_entry.hash = hash;
941 an_entry.key = key;
942 if (set_add_entry(so, &an_entry) == -1)
943 return -1;
944 }
945 return 0;
946 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 it = PyObject_GetIter(other);
949 if (it == NULL)
950 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 while ((key = PyIter_Next(it)) != NULL) {
953 if (set_add_key(so, key) == -1) {
954 Py_DECREF(it);
955 Py_DECREF(key);
956 return -1;
957 }
958 Py_DECREF(key);
959 }
960 Py_DECREF(it);
961 if (PyErr_Occurred())
962 return -1;
963 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000964}
965
966static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000967set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
972 PyObject *other = PyTuple_GET_ITEM(args, i);
973 if (set_update_internal(so, other) == -1)
974 return NULL;
975 }
976 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000977}
978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000980"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000981
982static PyObject *
983make_new_set(PyTypeObject *type, PyObject *iterable)
984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 if (dummy == NULL) { /* Auto-initialize dummy */
988 dummy = PyUnicode_FromString("<dummy key>");
989 if (dummy == NULL)
990 return NULL;
991 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 /* create PySetObject structure */
994 if (numfree &&
995 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
996 so = free_list[--numfree];
997 assert (so != NULL && PyAnySet_CheckExact(so));
998 Py_TYPE(so) = type;
999 _Py_NewReference((PyObject *)so);
1000 EMPTY_TO_MINSIZE(so);
1001 PyObject_GC_Track(so);
1002 } else {
1003 so = (PySetObject *)type->tp_alloc(type, 0);
1004 if (so == NULL)
1005 return NULL;
1006 /* tp_alloc has already zeroed the structure */
1007 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1008 INIT_NONZERO_SET_SLOTS(so);
1009 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 so->lookup = set_lookkey_unicode;
1012 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 if (iterable != NULL) {
1015 if (set_update_internal(so, iterable) == -1) {
1016 Py_DECREF(so);
1017 return NULL;
1018 }
1019 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001022}
1023
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001024static PyObject *
1025make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1028 if (PyType_IsSubtype(type, &PySet_Type))
1029 type = &PySet_Type;
1030 else
1031 type = &PyFrozenSet_Type;
1032 }
1033 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001034}
1035
Raymond Hettingerd7946662005-08-01 21:39:29 +00001036/* The empty frozenset is a singleton */
1037static PyObject *emptyfrozenset = NULL;
1038
Raymond Hettingera690a992003-11-16 16:17:49 +00001039static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001040frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1045 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1048 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (type != &PyFrozenSet_Type)
1051 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (iterable != NULL) {
1054 /* frozenset(f) is idempotent */
1055 if (PyFrozenSet_CheckExact(iterable)) {
1056 Py_INCREF(iterable);
1057 return iterable;
1058 }
1059 result = make_new_set(type, iterable);
1060 if (result == NULL || PySet_GET_SIZE(result))
1061 return result;
1062 Py_DECREF(result);
1063 }
1064 /* The empty frozenset is a singleton */
1065 if (emptyfrozenset == NULL)
1066 emptyfrozenset = make_new_set(type, NULL);
1067 Py_XINCREF(emptyfrozenset);
1068 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001069}
1070
1071void
1072PySet_Fini(void)
1073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 while (numfree) {
1077 numfree--;
1078 so = free_list[numfree];
1079 PyObject_GC_Del(so);
1080 }
1081 Py_CLEAR(dummy);
1082 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001083}
1084
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001085static PyObject *
1086set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1089 return NULL;
1090
1091 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001092}
1093
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001094/* set_swap_bodies() switches the contents of any two sets by moving their
1095 internal data pointers and, if needed, copying the internal smalltables.
1096 Semantically equivalent to:
1097
1098 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1099
1100 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001102 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001104 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001105*/
1106
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001107static void
1108set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 Py_ssize_t t;
1111 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001112 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001114 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 t = a->fill; a->fill = b->fill; b->fill = t;
1117 t = a->used; a->used = b->used; b->used = t;
1118 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 u = a->table;
1121 if (a->table == a->smalltable)
1122 u = b->smalltable;
1123 a->table = b->table;
1124 if (b->table == b->smalltable)
1125 a->table = a->smalltable;
1126 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (a->table == a->smalltable || b->table == b->smalltable) {
1131 memcpy(tab, a->smalltable, sizeof(tab));
1132 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1133 memcpy(b->smalltable, tab, sizeof(tab));
1134 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1137 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1138 h = a->hash; a->hash = b->hash; b->hash = h;
1139 } else {
1140 a->hash = -1;
1141 b->hash = -1;
1142 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001143}
1144
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001145static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001146set_copy(PySetObject *so)
1147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001149}
1150
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001151static PyObject *
1152frozenset_copy(PySetObject *so)
1153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (PyFrozenSet_CheckExact(so)) {
1155 Py_INCREF(so);
1156 return (PyObject *)so;
1157 }
1158 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001159}
1160
Raymond Hettingera690a992003-11-16 16:17:49 +00001161PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1162
1163static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001164set_clear(PySetObject *so)
1165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 set_clear_internal(so);
1167 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001168}
1169
1170PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1171
1172static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001173set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 PySetObject *result;
1176 PyObject *other;
1177 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 result = (PySetObject *)set_copy(so);
1180 if (result == NULL)
1181 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1184 other = PyTuple_GET_ITEM(args, i);
1185 if ((PyObject *)so == other)
1186 continue;
1187 if (set_update_internal(result, other) == -1) {
1188 Py_DECREF(result);
1189 return NULL;
1190 }
1191 }
1192 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001193}
1194
1195PyDoc_STRVAR(union_doc,
1196 "Return the union of sets as a new set.\n\
1197\n\
1198(i.e. all elements that are in either set.)");
1199
1200static PyObject *
1201set_or(PySetObject *so, PyObject *other)
1202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001204
Brian Curtindfc80e32011-08-10 20:28:54 -05001205 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1206 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 result = (PySetObject *)set_copy(so);
1209 if (result == NULL)
1210 return NULL;
1211 if ((PyObject *)so == other)
1212 return (PyObject *)result;
1213 if (set_update_internal(result, other) == -1) {
1214 Py_DECREF(result);
1215 return NULL;
1216 }
1217 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001218}
1219
Raymond Hettingera690a992003-11-16 16:17:49 +00001220static PyObject *
1221set_ior(PySetObject *so, PyObject *other)
1222{
Brian Curtindfc80e32011-08-10 20:28:54 -05001223 if (!PyAnySet_Check(other))
1224 Py_RETURN_NOTIMPLEMENTED;
1225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 if (set_update_internal(so, other) == -1)
1227 return NULL;
1228 Py_INCREF(so);
1229 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001230}
1231
1232static PyObject *
1233set_intersection(PySetObject *so, PyObject *other)
1234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 PySetObject *result;
1236 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 if ((PyObject *)so == other)
1239 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1242 if (result == NULL)
1243 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (PyAnySet_Check(other)) {
1246 Py_ssize_t pos = 0;
1247 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1250 tmp = (PyObject *)so;
1251 so = (PySetObject *)other;
1252 other = tmp;
1253 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 while (set_next((PySetObject *)other, &pos, &entry)) {
1256 int rv = set_contains_entry(so, entry);
1257 if (rv == -1) {
1258 Py_DECREF(result);
1259 return NULL;
1260 }
1261 if (rv) {
1262 if (set_add_entry(result, entry) == -1) {
1263 Py_DECREF(result);
1264 return NULL;
1265 }
1266 }
1267 }
1268 return (PyObject *)result;
1269 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 it = PyObject_GetIter(other);
1272 if (it == NULL) {
1273 Py_DECREF(result);
1274 return NULL;
1275 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 while ((key = PyIter_Next(it)) != NULL) {
1278 int rv;
1279 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001280 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (hash == -1) {
1283 Py_DECREF(it);
1284 Py_DECREF(result);
1285 Py_DECREF(key);
1286 return NULL;
1287 }
1288 entry.hash = hash;
1289 entry.key = key;
1290 rv = set_contains_entry(so, &entry);
1291 if (rv == -1) {
1292 Py_DECREF(it);
1293 Py_DECREF(result);
1294 Py_DECREF(key);
1295 return NULL;
1296 }
1297 if (rv) {
1298 if (set_add_entry(result, &entry) == -1) {
1299 Py_DECREF(it);
1300 Py_DECREF(result);
1301 Py_DECREF(key);
1302 return NULL;
1303 }
1304 }
1305 Py_DECREF(key);
1306 }
1307 Py_DECREF(it);
1308 if (PyErr_Occurred()) {
1309 Py_DECREF(result);
1310 return NULL;
1311 }
1312 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001313}
1314
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001315static PyObject *
1316set_intersection_multi(PySetObject *so, PyObject *args)
1317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 Py_ssize_t i;
1319 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 if (PyTuple_GET_SIZE(args) == 0)
1322 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 Py_INCREF(so);
1325 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1326 PyObject *other = PyTuple_GET_ITEM(args, i);
1327 PyObject *newresult = set_intersection((PySetObject *)result, other);
1328 if (newresult == NULL) {
1329 Py_DECREF(result);
1330 return NULL;
1331 }
1332 Py_DECREF(result);
1333 result = newresult;
1334 }
1335 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001336}
1337
Raymond Hettingera690a992003-11-16 16:17:49 +00001338PyDoc_STRVAR(intersection_doc,
1339"Return the intersection of two sets as a new set.\n\
1340\n\
1341(i.e. all elements that are in both sets.)");
1342
1343static PyObject *
1344set_intersection_update(PySetObject *so, PyObject *other)
1345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 tmp = set_intersection(so, other);
1349 if (tmp == NULL)
1350 return NULL;
1351 set_swap_bodies(so, (PySetObject *)tmp);
1352 Py_DECREF(tmp);
1353 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001354}
1355
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001356static PyObject *
1357set_intersection_update_multi(PySetObject *so, PyObject *args)
1358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 tmp = set_intersection_multi(so, args);
1362 if (tmp == NULL)
1363 return NULL;
1364 set_swap_bodies(so, (PySetObject *)tmp);
1365 Py_DECREF(tmp);
1366 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001367}
1368
Raymond Hettingera690a992003-11-16 16:17:49 +00001369PyDoc_STRVAR(intersection_update_doc,
1370"Update a set with the intersection of itself and another.");
1371
1372static PyObject *
1373set_and(PySetObject *so, PyObject *other)
1374{
Brian Curtindfc80e32011-08-10 20:28:54 -05001375 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1376 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001378}
1379
1380static PyObject *
1381set_iand(PySetObject *so, PyObject *other)
1382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001384
Brian Curtindfc80e32011-08-10 20:28:54 -05001385 if (!PyAnySet_Check(other))
1386 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 result = set_intersection_update(so, other);
1388 if (result == NULL)
1389 return NULL;
1390 Py_DECREF(result);
1391 Py_INCREF(so);
1392 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001393}
1394
Guido van Rossum58da9312007-11-10 23:39:45 +00001395static PyObject *
1396set_isdisjoint(PySetObject *so, PyObject *other)
1397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 if ((PyObject *)so == other) {
1401 if (PySet_GET_SIZE(so) == 0)
1402 Py_RETURN_TRUE;
1403 else
1404 Py_RETURN_FALSE;
1405 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 if (PyAnySet_CheckExact(other)) {
1408 Py_ssize_t pos = 0;
1409 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1412 tmp = (PyObject *)so;
1413 so = (PySetObject *)other;
1414 other = tmp;
1415 }
1416 while (set_next((PySetObject *)other, &pos, &entry)) {
1417 int rv = set_contains_entry(so, entry);
1418 if (rv == -1)
1419 return NULL;
1420 if (rv)
1421 Py_RETURN_FALSE;
1422 }
1423 Py_RETURN_TRUE;
1424 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 it = PyObject_GetIter(other);
1427 if (it == NULL)
1428 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 while ((key = PyIter_Next(it)) != NULL) {
1431 int rv;
1432 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001433 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (hash == -1) {
1436 Py_DECREF(key);
1437 Py_DECREF(it);
1438 return NULL;
1439 }
1440 entry.hash = hash;
1441 entry.key = key;
1442 rv = set_contains_entry(so, &entry);
1443 Py_DECREF(key);
1444 if (rv == -1) {
1445 Py_DECREF(it);
1446 return NULL;
1447 }
1448 if (rv) {
1449 Py_DECREF(it);
1450 Py_RETURN_FALSE;
1451 }
1452 }
1453 Py_DECREF(it);
1454 if (PyErr_Occurred())
1455 return NULL;
1456 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001457}
1458
1459PyDoc_STRVAR(isdisjoint_doc,
1460"Return True if two sets have a null intersection.");
1461
Neal Norwitz6576bd82005-11-13 18:41:28 +00001462static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001463set_difference_update_internal(PySetObject *so, PyObject *other)
1464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 if ((PyObject *)so == other)
1466 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 if (PyAnySet_Check(other)) {
1469 setentry *entry;
1470 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 while (set_next((PySetObject *)other, &pos, &entry))
1473 if (set_discard_entry(so, entry) == -1)
1474 return -1;
1475 } else {
1476 PyObject *key, *it;
1477 it = PyObject_GetIter(other);
1478 if (it == NULL)
1479 return -1;
1480
1481 while ((key = PyIter_Next(it)) != NULL) {
1482 if (set_discard_key(so, key) == -1) {
1483 Py_DECREF(it);
1484 Py_DECREF(key);
1485 return -1;
1486 }
1487 Py_DECREF(key);
1488 }
1489 Py_DECREF(it);
1490 if (PyErr_Occurred())
1491 return -1;
1492 }
1493 /* If more than 1/5 are dummies, then resize them away. */
1494 if ((so->fill - so->used) * 5 < so->mask)
1495 return 0;
1496 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001497}
1498
Raymond Hettingera690a992003-11-16 16:17:49 +00001499static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001500set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1505 PyObject *other = PyTuple_GET_ITEM(args, i);
1506 if (set_difference_update_internal(so, other) == -1)
1507 return NULL;
1508 }
1509 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001510}
1511
1512PyDoc_STRVAR(difference_update_doc,
1513"Remove all elements of another set from this set.");
1514
1515static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001516set_copy_and_difference(PySetObject *so, PyObject *other)
1517{
1518 PyObject *result;
1519
1520 result = set_copy(so);
1521 if (result == NULL)
1522 return NULL;
1523 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1524 return result;
1525 Py_DECREF(result);
1526 return NULL;
1527}
1528
1529static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001530set_difference(PySetObject *so, PyObject *other)
1531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 PyObject *result;
1533 setentry *entry;
1534 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001537 return set_copy_and_difference(so, other);
1538 }
1539
1540 /* If len(so) much more than len(other), it's more efficient to simply copy
1541 * so and then iterate other looking for common elements. */
1542 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1543 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 result = make_new_set_basetype(Py_TYPE(so), NULL);
1547 if (result == NULL)
1548 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (PyDict_CheckExact(other)) {
1551 while (set_next(so, &pos, &entry)) {
1552 setentry entrycopy;
1553 entrycopy.hash = entry->hash;
1554 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001555 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1557 Py_DECREF(result);
1558 return NULL;
1559 }
1560 }
1561 }
1562 return result;
1563 }
1564
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001565 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 while (set_next(so, &pos, &entry)) {
1567 int rv = set_contains_entry((PySetObject *)other, entry);
1568 if (rv == -1) {
1569 Py_DECREF(result);
1570 return NULL;
1571 }
1572 if (!rv) {
1573 if (set_add_entry((PySetObject *)result, entry) == -1) {
1574 Py_DECREF(result);
1575 return NULL;
1576 }
1577 }
1578 }
1579 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001580}
1581
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001582static PyObject *
1583set_difference_multi(PySetObject *so, PyObject *args)
1584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 Py_ssize_t i;
1586 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 if (PyTuple_GET_SIZE(args) == 0)
1589 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 other = PyTuple_GET_ITEM(args, 0);
1592 result = set_difference(so, other);
1593 if (result == NULL)
1594 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1597 other = PyTuple_GET_ITEM(args, i);
1598 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1599 Py_DECREF(result);
1600 return NULL;
1601 }
1602 }
1603 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001604}
1605
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001606PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001607"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001608\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001609(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001610static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001611set_sub(PySetObject *so, PyObject *other)
1612{
Brian Curtindfc80e32011-08-10 20:28:54 -05001613 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1614 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001616}
1617
1618static PyObject *
1619set_isub(PySetObject *so, PyObject *other)
1620{
Brian Curtindfc80e32011-08-10 20:28:54 -05001621 if (!PyAnySet_Check(other))
1622 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 if (set_difference_update_internal(so, other) == -1)
1624 return NULL;
1625 Py_INCREF(so);
1626 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001627}
1628
1629static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001630set_symmetric_difference_update(PySetObject *so, PyObject *other)
1631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 PySetObject *otherset;
1633 PyObject *key;
1634 Py_ssize_t pos = 0;
1635 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 if ((PyObject *)so == other)
1638 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (PyDict_CheckExact(other)) {
1641 PyObject *value;
1642 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001643 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1645 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001646
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001647 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 an_entry.hash = hash;
1649 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001652 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001653 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001656 if (rv == DISCARD_NOTFOUND) {
1657 if (set_add_entry(so, &an_entry) == -1) {
1658 Py_DECREF(key);
1659 return NULL;
1660 }
1661 }
Georg Brandl2d444492010-09-03 10:52:55 +00001662 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 }
1664 Py_RETURN_NONE;
1665 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 if (PyAnySet_Check(other)) {
1668 Py_INCREF(other);
1669 otherset = (PySetObject *)other;
1670 } else {
1671 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1672 if (otherset == NULL)
1673 return NULL;
1674 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 while (set_next(otherset, &pos, &entry)) {
1677 int rv = set_discard_entry(so, entry);
1678 if (rv == -1) {
1679 Py_DECREF(otherset);
1680 return NULL;
1681 }
1682 if (rv == DISCARD_NOTFOUND) {
1683 if (set_add_entry(so, entry) == -1) {
1684 Py_DECREF(otherset);
1685 return NULL;
1686 }
1687 }
1688 }
1689 Py_DECREF(otherset);
1690 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001691}
1692
1693PyDoc_STRVAR(symmetric_difference_update_doc,
1694"Update a set with the symmetric difference of itself and another.");
1695
1696static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001697set_symmetric_difference(PySetObject *so, PyObject *other)
1698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 PyObject *rv;
1700 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1703 if (otherset == NULL)
1704 return NULL;
1705 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1706 if (rv == NULL)
1707 return NULL;
1708 Py_DECREF(rv);
1709 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001710}
1711
1712PyDoc_STRVAR(symmetric_difference_doc,
1713"Return the symmetric difference of two sets as a new set.\n\
1714\n\
1715(i.e. all elements that are in exactly one of the sets.)");
1716
1717static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001718set_xor(PySetObject *so, PyObject *other)
1719{
Brian Curtindfc80e32011-08-10 20:28:54 -05001720 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1721 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001723}
1724
1725static PyObject *
1726set_ixor(PySetObject *so, PyObject *other)
1727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001729
Brian Curtindfc80e32011-08-10 20:28:54 -05001730 if (!PyAnySet_Check(other))
1731 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 result = set_symmetric_difference_update(so, other);
1733 if (result == NULL)
1734 return NULL;
1735 Py_DECREF(result);
1736 Py_INCREF(so);
1737 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001738}
1739
1740static PyObject *
1741set_issubset(PySetObject *so, PyObject *other)
1742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 setentry *entry;
1744 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 if (!PyAnySet_Check(other)) {
1747 PyObject *tmp, *result;
1748 tmp = make_new_set(&PySet_Type, other);
1749 if (tmp == NULL)
1750 return NULL;
1751 result = set_issubset(so, tmp);
1752 Py_DECREF(tmp);
1753 return result;
1754 }
1755 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1756 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 while (set_next(so, &pos, &entry)) {
1759 int rv = set_contains_entry((PySetObject *)other, entry);
1760 if (rv == -1)
1761 return NULL;
1762 if (!rv)
1763 Py_RETURN_FALSE;
1764 }
1765 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001766}
1767
1768PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1769
1770static PyObject *
1771set_issuperset(PySetObject *so, PyObject *other)
1772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 if (!PyAnySet_Check(other)) {
1776 tmp = make_new_set(&PySet_Type, other);
1777 if (tmp == NULL)
1778 return NULL;
1779 result = set_issuperset(so, tmp);
1780 Py_DECREF(tmp);
1781 return result;
1782 }
1783 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001784}
1785
1786PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1787
Raymond Hettingera690a992003-11-16 16:17:49 +00001788static PyObject *
1789set_richcompare(PySetObject *v, PyObject *w, int op)
1790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001792
Brian Curtindfc80e32011-08-10 20:28:54 -05001793 if(!PyAnySet_Check(w))
1794 Py_RETURN_NOTIMPLEMENTED;
1795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 switch (op) {
1797 case Py_EQ:
1798 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1799 Py_RETURN_FALSE;
1800 if (v->hash != -1 &&
1801 ((PySetObject *)w)->hash != -1 &&
1802 v->hash != ((PySetObject *)w)->hash)
1803 Py_RETURN_FALSE;
1804 return set_issubset(v, w);
1805 case Py_NE:
1806 r1 = set_richcompare(v, w, Py_EQ);
1807 if (r1 == NULL)
1808 return NULL;
1809 r2 = PyBool_FromLong(PyObject_Not(r1));
1810 Py_DECREF(r1);
1811 return r2;
1812 case Py_LE:
1813 return set_issubset(v, w);
1814 case Py_GE:
1815 return set_issuperset(v, w);
1816 case Py_LT:
1817 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1818 Py_RETURN_FALSE;
1819 return set_issubset(v, w);
1820 case Py_GT:
1821 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1822 Py_RETURN_FALSE;
1823 return set_issuperset(v, w);
1824 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001825 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001826}
1827
Raymond Hettingera690a992003-11-16 16:17:49 +00001828static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001829set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 if (set_add_key(so, key) == -1)
1832 return NULL;
1833 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001834}
1835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001837"Add an element to a set.\n\
1838\n\
1839This has no effect if the element is already present.");
1840
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001841static int
1842set_contains(PySetObject *so, PyObject *key)
1843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 PyObject *tmpkey;
1845 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 rv = set_contains_key(so, key);
1848 if (rv == -1) {
1849 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1850 return -1;
1851 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001852 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 if (tmpkey == NULL)
1854 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 rv = set_contains(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 Py_DECREF(tmpkey);
1857 }
1858 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001859}
1860
1861static PyObject *
1862set_direct_contains(PySetObject *so, PyObject *key)
1863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 result = set_contains(so, key);
1867 if (result == -1)
1868 return NULL;
1869 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001870}
1871
1872PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1873
Raymond Hettingera690a992003-11-16 16:17:49 +00001874static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001875set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 PyObject *tmpkey;
1878 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 rv = set_discard_key(so, key);
1881 if (rv == -1) {
1882 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1883 return NULL;
1884 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001885 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 if (tmpkey == NULL)
1887 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 Py_DECREF(tmpkey);
1890 if (rv == -1)
1891 return NULL;
1892 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 if (rv == DISCARD_NOTFOUND) {
1895 set_key_error(key);
1896 return NULL;
1897 }
1898 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001899}
1900
1901PyDoc_STRVAR(remove_doc,
1902"Remove an element from a set; it must be a member.\n\
1903\n\
1904If the element is not a member, raise a KeyError.");
1905
1906static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001907set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 PyObject *tmpkey, *result;
1910 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 rv = set_discard_key(so, key);
1913 if (rv == -1) {
1914 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1915 return NULL;
1916 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001917 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 if (tmpkey == NULL)
1919 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 result = set_discard(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 Py_DECREF(tmpkey);
1922 return result;
1923 }
1924 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001925}
1926
1927PyDoc_STRVAR(discard_doc,
1928"Remove an element from a set if it is a member.\n\
1929\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001931
1932static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001933set_reduce(PySetObject *so)
1934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 keys = PySequence_List((PyObject *)so);
1938 if (keys == NULL)
1939 goto done;
1940 args = PyTuple_Pack(1, keys);
1941 if (args == NULL)
1942 goto done;
1943 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1944 if (dict == NULL) {
1945 PyErr_Clear();
1946 dict = Py_None;
1947 Py_INCREF(dict);
1948 }
1949 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001950done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 Py_XDECREF(args);
1952 Py_XDECREF(keys);
1953 Py_XDECREF(dict);
1954 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001955}
1956
1957PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1958
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001959static PyObject *
1960set_sizeof(PySetObject *so)
1961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 res = sizeof(PySetObject);
1965 if (so->table != so->smalltable)
1966 res = res + (so->mask + 1) * sizeof(setentry);
1967 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001968}
1969
1970PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001971static int
1972set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 if (!PyAnySet_Check(self))
1977 return -1;
1978 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1979 return -1;
1980 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1981 return -1;
1982 set_clear_internal(self);
1983 self->hash = -1;
1984 if (iterable == NULL)
1985 return 0;
1986 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001987}
1988
Raymond Hettingera690a992003-11-16 16:17:49 +00001989static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 set_len, /* sq_length */
1991 0, /* sq_concat */
1992 0, /* sq_repeat */
1993 0, /* sq_item */
1994 0, /* sq_slice */
1995 0, /* sq_ass_item */
1996 0, /* sq_ass_slice */
1997 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001998};
1999
2000/* set object ********************************************************/
2001
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002002#ifdef Py_DEBUG
2003static PyObject *test_c_api(PySetObject *so);
2004
2005PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2006All is well if assertions don't fail.");
2007#endif
2008
Raymond Hettingera690a992003-11-16 16:17:49 +00002009static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 {"add", (PyCFunction)set_add, METH_O,
2011 add_doc},
2012 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2013 clear_doc},
2014 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2015 contains_doc},
2016 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2017 copy_doc},
2018 {"discard", (PyCFunction)set_discard, METH_O,
2019 discard_doc},
2020 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2021 difference_doc},
2022 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2023 difference_update_doc},
2024 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2025 intersection_doc},
2026 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2027 intersection_update_doc},
2028 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2029 isdisjoint_doc},
2030 {"issubset", (PyCFunction)set_issubset, METH_O,
2031 issubset_doc},
2032 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2033 issuperset_doc},
2034 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2035 pop_doc},
2036 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2037 reduce_doc},
2038 {"remove", (PyCFunction)set_remove, METH_O,
2039 remove_doc},
2040 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2041 sizeof_doc},
2042 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2043 symmetric_difference_doc},
2044 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2045 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002046#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2048 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002049#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 {"union", (PyCFunction)set_union, METH_VARARGS,
2051 union_doc},
2052 {"update", (PyCFunction)set_update, METH_VARARGS,
2053 update_doc},
2054 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002055};
2056
2057static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 0, /*nb_add*/
2059 (binaryfunc)set_sub, /*nb_subtract*/
2060 0, /*nb_multiply*/
2061 0, /*nb_remainder*/
2062 0, /*nb_divmod*/
2063 0, /*nb_power*/
2064 0, /*nb_negative*/
2065 0, /*nb_positive*/
2066 0, /*nb_absolute*/
2067 0, /*nb_bool*/
2068 0, /*nb_invert*/
2069 0, /*nb_lshift*/
2070 0, /*nb_rshift*/
2071 (binaryfunc)set_and, /*nb_and*/
2072 (binaryfunc)set_xor, /*nb_xor*/
2073 (binaryfunc)set_or, /*nb_or*/
2074 0, /*nb_int*/
2075 0, /*nb_reserved*/
2076 0, /*nb_float*/
2077 0, /*nb_inplace_add*/
2078 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2079 0, /*nb_inplace_multiply*/
2080 0, /*nb_inplace_remainder*/
2081 0, /*nb_inplace_power*/
2082 0, /*nb_inplace_lshift*/
2083 0, /*nb_inplace_rshift*/
2084 (binaryfunc)set_iand, /*nb_inplace_and*/
2085 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2086 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002087};
2088
2089PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002090"set() -> new empty set object\n\
2091set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002092\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002093Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002094
2095PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2097 "set", /* tp_name */
2098 sizeof(PySetObject), /* tp_basicsize */
2099 0, /* tp_itemsize */
2100 /* methods */
2101 (destructor)set_dealloc, /* tp_dealloc */
2102 0, /* tp_print */
2103 0, /* tp_getattr */
2104 0, /* tp_setattr */
2105 0, /* tp_reserved */
2106 (reprfunc)set_repr, /* tp_repr */
2107 &set_as_number, /* tp_as_number */
2108 &set_as_sequence, /* tp_as_sequence */
2109 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002110 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 0, /* tp_call */
2112 0, /* tp_str */
2113 PyObject_GenericGetAttr, /* tp_getattro */
2114 0, /* tp_setattro */
2115 0, /* tp_as_buffer */
2116 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2117 Py_TPFLAGS_BASETYPE, /* tp_flags */
2118 set_doc, /* tp_doc */
2119 (traverseproc)set_traverse, /* tp_traverse */
2120 (inquiry)set_clear_internal, /* tp_clear */
2121 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002122 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2123 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 0, /* tp_iternext */
2125 set_methods, /* tp_methods */
2126 0, /* tp_members */
2127 0, /* tp_getset */
2128 0, /* tp_base */
2129 0, /* tp_dict */
2130 0, /* tp_descr_get */
2131 0, /* tp_descr_set */
2132 0, /* tp_dictoffset */
2133 (initproc)set_init, /* tp_init */
2134 PyType_GenericAlloc, /* tp_alloc */
2135 set_new, /* tp_new */
2136 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002137};
2138
2139/* frozenset object ********************************************************/
2140
2141
2142static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2144 contains_doc},
2145 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2146 copy_doc},
2147 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2148 difference_doc},
2149 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2150 intersection_doc},
2151 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2152 isdisjoint_doc},
2153 {"issubset", (PyCFunction)set_issubset, METH_O,
2154 issubset_doc},
2155 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2156 issuperset_doc},
2157 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2158 reduce_doc},
2159 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2160 sizeof_doc},
2161 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2162 symmetric_difference_doc},
2163 {"union", (PyCFunction)set_union, METH_VARARGS,
2164 union_doc},
2165 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002166};
2167
2168static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 0, /*nb_add*/
2170 (binaryfunc)set_sub, /*nb_subtract*/
2171 0, /*nb_multiply*/
2172 0, /*nb_remainder*/
2173 0, /*nb_divmod*/
2174 0, /*nb_power*/
2175 0, /*nb_negative*/
2176 0, /*nb_positive*/
2177 0, /*nb_absolute*/
2178 0, /*nb_bool*/
2179 0, /*nb_invert*/
2180 0, /*nb_lshift*/
2181 0, /*nb_rshift*/
2182 (binaryfunc)set_and, /*nb_and*/
2183 (binaryfunc)set_xor, /*nb_xor*/
2184 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002185};
2186
2187PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002188"frozenset() -> empty frozenset object\n\
2189frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002190\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002191Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002192
2193PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2195 "frozenset", /* tp_name */
2196 sizeof(PySetObject), /* tp_basicsize */
2197 0, /* tp_itemsize */
2198 /* methods */
2199 (destructor)set_dealloc, /* tp_dealloc */
2200 0, /* tp_print */
2201 0, /* tp_getattr */
2202 0, /* tp_setattr */
2203 0, /* tp_reserved */
2204 (reprfunc)set_repr, /* tp_repr */
2205 &frozenset_as_number, /* tp_as_number */
2206 &set_as_sequence, /* tp_as_sequence */
2207 0, /* tp_as_mapping */
2208 frozenset_hash, /* tp_hash */
2209 0, /* tp_call */
2210 0, /* tp_str */
2211 PyObject_GenericGetAttr, /* tp_getattro */
2212 0, /* tp_setattro */
2213 0, /* tp_as_buffer */
2214 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2215 Py_TPFLAGS_BASETYPE, /* tp_flags */
2216 frozenset_doc, /* tp_doc */
2217 (traverseproc)set_traverse, /* tp_traverse */
2218 (inquiry)set_clear_internal, /* tp_clear */
2219 (richcmpfunc)set_richcompare, /* tp_richcompare */
2220 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2221 (getiterfunc)set_iter, /* tp_iter */
2222 0, /* tp_iternext */
2223 frozenset_methods, /* tp_methods */
2224 0, /* tp_members */
2225 0, /* tp_getset */
2226 0, /* tp_base */
2227 0, /* tp_dict */
2228 0, /* tp_descr_get */
2229 0, /* tp_descr_set */
2230 0, /* tp_dictoffset */
2231 0, /* tp_init */
2232 PyType_GenericAlloc, /* tp_alloc */
2233 frozenset_new, /* tp_new */
2234 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002235};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002236
2237
2238/***** C API functions *************************************************/
2239
2240PyObject *
2241PySet_New(PyObject *iterable)
2242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002244}
2245
2246PyObject *
2247PyFrozenSet_New(PyObject *iterable)
2248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002250}
2251
Neal Norwitz8c49c822006-03-04 18:41:19 +00002252Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002253PySet_Size(PyObject *anyset)
2254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 if (!PyAnySet_Check(anyset)) {
2256 PyErr_BadInternalCall();
2257 return -1;
2258 }
2259 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002260}
2261
2262int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002263PySet_Clear(PyObject *set)
2264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 if (!PySet_Check(set)) {
2266 PyErr_BadInternalCall();
2267 return -1;
2268 }
2269 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002270}
2271
2272int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002273PySet_Contains(PyObject *anyset, PyObject *key)
2274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 if (!PyAnySet_Check(anyset)) {
2276 PyErr_BadInternalCall();
2277 return -1;
2278 }
2279 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002280}
2281
2282int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002283PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 if (!PySet_Check(set)) {
2286 PyErr_BadInternalCall();
2287 return -1;
2288 }
2289 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002290}
2291
2292int
Christian Heimesfd66e512008-01-29 12:18:50 +00002293PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 if (!PySet_Check(anyset) &&
2296 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2297 PyErr_BadInternalCall();
2298 return -1;
2299 }
2300 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002301}
2302
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002303int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002304_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 if (!PyAnySet_Check(set)) {
2309 PyErr_BadInternalCall();
2310 return -1;
2311 }
2312 if (set_next((PySetObject *)set, pos, &entry) == 0)
2313 return 0;
2314 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002315 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002317}
2318
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002319PyObject *
2320PySet_Pop(PyObject *set)
2321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 if (!PySet_Check(set)) {
2323 PyErr_BadInternalCall();
2324 return NULL;
2325 }
2326 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002327}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002328
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002329int
2330_PySet_Update(PyObject *set, PyObject *iterable)
2331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 if (!PySet_Check(set)) {
2333 PyErr_BadInternalCall();
2334 return -1;
2335 }
2336 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002337}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002338
2339#ifdef Py_DEBUG
2340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002342 Returns True and original set is restored. */
2343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344#define assertRaises(call_return_value, exception) \
2345 do { \
2346 assert(call_return_value); \
2347 assert(PyErr_ExceptionMatches(exception)); \
2348 PyErr_Clear(); \
2349 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002350
2351static PyObject *
2352test_c_api(PySetObject *so)
2353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 Py_ssize_t count;
2355 char *s;
2356 Py_ssize_t i;
2357 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2358 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002359 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 /* Verify preconditions */
2363 assert(PyAnySet_Check(ob));
2364 assert(PyAnySet_CheckExact(ob));
2365 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 /* so.clear(); so |= set("abc"); */
2368 str = PyUnicode_FromString("abc");
2369 if (str == NULL)
2370 return NULL;
2371 set_clear_internal(so);
2372 if (set_update_internal(so, str) == -1) {
2373 Py_DECREF(str);
2374 return NULL;
2375 }
2376 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 /* Exercise type/size checks */
2379 assert(PySet_Size(ob) == 3);
2380 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 /* Raise TypeError for non-iterable constructor arguments */
2383 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2384 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 /* Raise TypeError for unhashable key */
2387 dup = PySet_New(ob);
2388 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2389 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2390 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* Exercise successful pop, contains, add, and discard */
2393 elem = PySet_Pop(ob);
2394 assert(PySet_Contains(ob, elem) == 0);
2395 assert(PySet_GET_SIZE(ob) == 2);
2396 assert(PySet_Add(ob, elem) == 0);
2397 assert(PySet_Contains(ob, elem) == 1);
2398 assert(PySet_GET_SIZE(ob) == 3);
2399 assert(PySet_Discard(ob, elem) == 1);
2400 assert(PySet_GET_SIZE(ob) == 2);
2401 assert(PySet_Discard(ob, elem) == 0);
2402 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 /* Exercise clear */
2405 dup2 = PySet_New(dup);
2406 assert(PySet_Clear(dup2) == 0);
2407 assert(PySet_Size(dup2) == 0);
2408 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 /* Raise SystemError on clear or update of frozen set */
2411 f = PyFrozenSet_New(dup);
2412 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2413 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2414 assert(PySet_Add(f, elem) == 0);
2415 Py_INCREF(f);
2416 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2417 Py_DECREF(f);
2418 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 /* Exercise direct iteration */
2421 i = 0, count = 0;
2422 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2423 s = _PyUnicode_AsString(x);
2424 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2425 count++;
2426 }
2427 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* Exercise updates */
2430 dup2 = PySet_New(NULL);
2431 assert(_PySet_Update(dup2, dup) == 0);
2432 assert(PySet_Size(dup2) == 3);
2433 assert(_PySet_Update(dup2, dup) == 0);
2434 assert(PySet_Size(dup2) == 3);
2435 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 /* Raise SystemError when self argument is not a set or frozenset. */
2438 t = PyTuple_New(0);
2439 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2440 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2441 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 /* Raise SystemError when self argument is not a set. */
2444 f = PyFrozenSet_New(dup);
2445 assert(PySet_Size(f) == 3);
2446 assert(PyFrozenSet_CheckExact(f));
2447 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2448 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2449 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 /* Raise KeyError when popping from an empty set */
2452 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2453 Py_DECREF(ob);
2454 assert(PySet_GET_SIZE(ob) == 0);
2455 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 /* Restore the set from the copy using the PyNumber API */
2458 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2459 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 /* Verify constructors accept NULL arguments */
2462 f = PySet_New(NULL);
2463 assert(f != NULL);
2464 assert(PySet_GET_SIZE(f) == 0);
2465 Py_DECREF(f);
2466 f = PyFrozenSet_New(NULL);
2467 assert(f != NULL);
2468 assert(PyFrozenSet_CheckExact(f));
2469 assert(PySet_GET_SIZE(f) == 0);
2470 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 Py_DECREF(elem);
2473 Py_DECREF(dup);
2474 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002475}
2476
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002477#undef assertRaises
2478
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002479#endif