blob: ebfddb398ccc5afa2d87e073fe2521a5902f60e4 [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 register Py_ssize_t i;
81 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 i = hash & mask;
90 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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 register Py_ssize_t i;
163 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 }
177 i = hash & mask;
178 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 i = hash & mask;
260 entry = &table[i];
261 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
262 i = (i << 2) + i + perturb + 1;
263 entry = &table[i & mask];
264 }
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
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) ||
389 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
390 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) ||
437 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
438 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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 PyObject *keys, *result=NULL;
583 Py_UNICODE *u;
584 int status = Py_ReprEnter((PyObject*)so);
585 PyObject *listrepr;
586 Py_ssize_t newsize;
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 if (status != 0) {
589 if (status < 0)
590 return NULL;
591 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
592 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 /* shortcut for the empty set */
595 if (!so->used) {
596 Py_ReprLeave((PyObject*)so);
597 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
598 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 keys = PySequence_List((PyObject *)so);
601 if (keys == NULL)
602 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 listrepr = PyObject_Repr(keys);
605 Py_DECREF(keys);
606 if (listrepr == NULL)
607 goto done;
608 newsize = PyUnicode_GET_SIZE(listrepr);
609 result = PyUnicode_FromUnicode(NULL, newsize);
Victor Stinnera1a807b2011-05-26 14:24:30 +0200610 if (result == NULL)
611 goto done;
612
613 u = PyUnicode_AS_UNICODE(result);
614 *u++ = '{';
615 /* Omit the brackets from the listrepr */
616 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
617 newsize-2);
618 u += newsize-2;
619 *u = '}';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 Py_DECREF(listrepr);
Victor Stinnera1a807b2011-05-26 14:24:30 +0200621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 if (Py_TYPE(so) != &PySet_Type) {
623 PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
624 Py_TYPE(so)->tp_name,
625 result);
626 Py_DECREF(result);
627 result = tmp;
628 }
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000629done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 Py_ReprLeave((PyObject*)so);
631 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000632}
633
Martin v. Löwis18e16552006-02-15 17:27:45 +0000634static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000635set_len(PyObject *so)
636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000638}
639
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000640static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000641set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000644 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100645 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 register Py_ssize_t i;
647 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 assert (PyAnySet_Check(so));
650 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 other = (PySetObject*)otherset;
653 if (other == so || other->used == 0)
654 /* a.update(a) or a.update({}); nothing to do */
655 return 0;
656 /* Do one big resize at the start, rather than
657 * incrementally resizing as we insert new keys. Expect
658 * that there will be no (or few) overlapping keys.
659 */
660 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
661 if (set_table_resize(so, (so->used + other->used)*2) != 0)
662 return -1;
663 }
664 for (i = 0; i <= other->mask; i++) {
665 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000666 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100667 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000668 if (key != NULL &&
669 key != dummy) {
670 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100671 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000672 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 return -1;
674 }
675 }
676 }
677 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000678}
679
680static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000681set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000682{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000683 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (!PyUnicode_CheckExact(key) ||
687 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
688 hash = PyObject_Hash(key);
689 if (hash == -1)
690 return -1;
691 }
692 entry = (so->lookup)(so, key, hash);
693 if (entry == NULL)
694 return -1;
695 key = entry->key;
696 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000697}
698
Raymond Hettingerc991db22005-08-11 07:58:45 +0000699static int
700set_contains_entry(PySetObject *so, setentry *entry)
701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 PyObject *key;
703 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000704
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000705 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (lu_entry == NULL)
707 return -1;
708 key = lu_entry->key;
709 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000710}
711
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000712static PyObject *
713set_pop(PySetObject *so)
714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 register Py_ssize_t i = 0;
716 register setentry *entry;
717 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 assert (PyAnySet_Check(so));
720 if (so->used == 0) {
721 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
722 return NULL;
723 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 /* Set entry to "the first" unused or dummy set entry. We abuse
726 * the hash field of slot 0 to hold a search finger:
727 * If slot 0 has a value, use slot 0.
728 * Else slot 0 is being used to hold a search finger,
729 * and we use its hash value as the first index to look.
730 */
731 entry = &so->table[0];
732 if (entry->key == NULL || entry->key == dummy) {
733 i = entry->hash;
734 /* The hash field may be a real hash value, or it may be a
735 * legit search finger, or it may be a once-legit search
736 * finger that's out of bounds now because it wrapped around
737 * or the table shrunk -- simply make sure it's in bounds now.
738 */
739 if (i > so->mask || i < 1)
740 i = 1; /* skip slot 0 */
741 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
742 i++;
743 if (i > so->mask)
744 i = 1;
745 }
746 }
747 key = entry->key;
748 Py_INCREF(dummy);
749 entry->key = dummy;
750 so->used--;
751 so->table[0].hash = i + 1; /* next place to start */
752 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000753}
754
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000755PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
756Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000757
758static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000759set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 Py_ssize_t pos = 0;
762 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 while (set_next(so, &pos, &entry))
765 Py_VISIT(entry->key);
766 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000767}
768
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000769static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000770frozenset_hash(PyObject *self)
771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 PySetObject *so = (PySetObject *)self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000773 Py_hash_t h, hash = 1927868237L;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 setentry *entry;
775 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 if (so->hash != -1)
778 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000779
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000780 hash *= PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 while (set_next(so, &pos, &entry)) {
782 /* Work to increase the bit dispersion for closely spaced hash
783 values. The is important because some use cases have many
784 combinations of a small number of elements with nearby
785 hashes so that many distinct combinations collapse to only
786 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000787 h = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
789 }
790 hash = hash * 69069L + 907133923L;
791 if (hash == -1)
792 hash = 590923713L;
793 so->hash = hash;
794 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000795}
796
Raymond Hettingera9d99362005-08-05 00:01:15 +0000797/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000798
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 PyObject_HEAD
801 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
802 Py_ssize_t si_used;
803 Py_ssize_t si_pos;
804 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805} setiterobject;
806
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807static void
808setiter_dealloc(setiterobject *si)
809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 Py_XDECREF(si->si_set);
811 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000812}
813
814static int
815setiter_traverse(setiterobject *si, visitproc visit, void *arg)
816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 Py_VISIT(si->si_set);
818 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000819}
820
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000821static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822setiter_len(setiterobject *si)
823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 Py_ssize_t len = 0;
825 if (si->si_set != NULL && si->si_used == si->si_set->used)
826 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000827 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828}
829
Armin Rigof5b3e362006-02-11 21:32:43 +0000830PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000831
832static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
834 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835};
836
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000837static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 PyObject *key;
840 register Py_ssize_t i, mask;
841 register setentry *entry;
842 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 if (so == NULL)
845 return NULL;
846 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 if (si->si_used != so->used) {
849 PyErr_SetString(PyExc_RuntimeError,
850 "Set changed size during iteration");
851 si->si_used = -1; /* Make this state sticky */
852 return NULL;
853 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 i = si->si_pos;
856 assert(i>=0);
857 entry = so->table;
858 mask = so->mask;
859 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
860 i++;
861 si->si_pos = i+1;
862 if (i > mask)
863 goto fail;
864 si->len--;
865 key = entry[i].key;
866 Py_INCREF(key);
867 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868
869fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 Py_DECREF(so);
871 si->si_set = NULL;
872 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000873}
874
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000875PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 PyVarObject_HEAD_INIT(&PyType_Type, 0)
877 "set_iterator", /* tp_name */
878 sizeof(setiterobject), /* tp_basicsize */
879 0, /* tp_itemsize */
880 /* methods */
881 (destructor)setiter_dealloc, /* tp_dealloc */
882 0, /* tp_print */
883 0, /* tp_getattr */
884 0, /* tp_setattr */
885 0, /* tp_reserved */
886 0, /* tp_repr */
887 0, /* tp_as_number */
888 0, /* tp_as_sequence */
889 0, /* tp_as_mapping */
890 0, /* tp_hash */
891 0, /* tp_call */
892 0, /* tp_str */
893 PyObject_GenericGetAttr, /* tp_getattro */
894 0, /* tp_setattro */
895 0, /* tp_as_buffer */
896 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
897 0, /* tp_doc */
898 (traverseproc)setiter_traverse, /* tp_traverse */
899 0, /* tp_clear */
900 0, /* tp_richcompare */
901 0, /* tp_weaklistoffset */
902 PyObject_SelfIter, /* tp_iter */
903 (iternextfunc)setiter_iternext, /* tp_iternext */
904 setiter_methods, /* tp_methods */
905 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000906};
907
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000908static PyObject *
909set_iter(PySetObject *so)
910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
912 if (si == NULL)
913 return NULL;
914 Py_INCREF(so);
915 si->si_set = so;
916 si->si_used = so->used;
917 si->si_pos = 0;
918 si->len = so->used;
919 _PyObject_GC_TRACK(si);
920 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921}
922
Raymond Hettingerd7946662005-08-01 21:39:29 +0000923static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000924set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 if (PyAnySet_Check(other))
929 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 if (PyDict_CheckExact(other)) {
932 PyObject *value;
933 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000934 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 /* Do one big resize at the start, rather than
938 * incrementally resizing as we insert new keys. Expect
939 * that there will be no (or few) overlapping keys.
940 */
941 if (dictsize == -1)
942 return -1;
943 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
944 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
945 return -1;
946 }
947 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
948 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 an_entry.hash = hash;
951 an_entry.key = key;
952 if (set_add_entry(so, &an_entry) == -1)
953 return -1;
954 }
955 return 0;
956 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 it = PyObject_GetIter(other);
959 if (it == NULL)
960 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 while ((key = PyIter_Next(it)) != NULL) {
963 if (set_add_key(so, key) == -1) {
964 Py_DECREF(it);
965 Py_DECREF(key);
966 return -1;
967 }
968 Py_DECREF(key);
969 }
970 Py_DECREF(it);
971 if (PyErr_Occurred())
972 return -1;
973 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000974}
975
976static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000977set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
982 PyObject *other = PyTuple_GET_ITEM(args, i);
983 if (set_update_internal(so, other) == -1)
984 return NULL;
985 }
986 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000987}
988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000990"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000991
992static PyObject *
993make_new_set(PyTypeObject *type, PyObject *iterable)
994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (dummy == NULL) { /* Auto-initialize dummy */
998 dummy = PyUnicode_FromString("<dummy key>");
999 if (dummy == NULL)
1000 return NULL;
1001 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 /* create PySetObject structure */
1004 if (numfree &&
1005 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1006 so = free_list[--numfree];
1007 assert (so != NULL && PyAnySet_CheckExact(so));
1008 Py_TYPE(so) = type;
1009 _Py_NewReference((PyObject *)so);
1010 EMPTY_TO_MINSIZE(so);
1011 PyObject_GC_Track(so);
1012 } else {
1013 so = (PySetObject *)type->tp_alloc(type, 0);
1014 if (so == NULL)
1015 return NULL;
1016 /* tp_alloc has already zeroed the structure */
1017 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1018 INIT_NONZERO_SET_SLOTS(so);
1019 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 so->lookup = set_lookkey_unicode;
1022 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (iterable != NULL) {
1025 if (set_update_internal(so, iterable) == -1) {
1026 Py_DECREF(so);
1027 return NULL;
1028 }
1029 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001032}
1033
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001034static PyObject *
1035make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1038 if (PyType_IsSubtype(type, &PySet_Type))
1039 type = &PySet_Type;
1040 else
1041 type = &PyFrozenSet_Type;
1042 }
1043 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001044}
1045
Raymond Hettingerd7946662005-08-01 21:39:29 +00001046/* The empty frozenset is a singleton */
1047static PyObject *emptyfrozenset = NULL;
1048
Raymond Hettingera690a992003-11-16 16:17:49 +00001049static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001050frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1055 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1058 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 if (type != &PyFrozenSet_Type)
1061 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (iterable != NULL) {
1064 /* frozenset(f) is idempotent */
1065 if (PyFrozenSet_CheckExact(iterable)) {
1066 Py_INCREF(iterable);
1067 return iterable;
1068 }
1069 result = make_new_set(type, iterable);
1070 if (result == NULL || PySet_GET_SIZE(result))
1071 return result;
1072 Py_DECREF(result);
1073 }
1074 /* The empty frozenset is a singleton */
1075 if (emptyfrozenset == NULL)
1076 emptyfrozenset = make_new_set(type, NULL);
1077 Py_XINCREF(emptyfrozenset);
1078 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001079}
1080
1081void
1082PySet_Fini(void)
1083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 while (numfree) {
1087 numfree--;
1088 so = free_list[numfree];
1089 PyObject_GC_Del(so);
1090 }
1091 Py_CLEAR(dummy);
1092 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001093}
1094
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001095static PyObject *
1096set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1099 return NULL;
1100
1101 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001102}
1103
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001104/* set_swap_bodies() switches the contents of any two sets by moving their
1105 internal data pointers and, if needed, copying the internal smalltables.
1106 Semantically equivalent to:
1107
1108 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1109
1110 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001112 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001114 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001115*/
1116
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001117static void
1118set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 Py_ssize_t t;
1121 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001122 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001124 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 t = a->fill; a->fill = b->fill; b->fill = t;
1127 t = a->used; a->used = b->used; b->used = t;
1128 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 u = a->table;
1131 if (a->table == a->smalltable)
1132 u = b->smalltable;
1133 a->table = b->table;
1134 if (b->table == b->smalltable)
1135 a->table = a->smalltable;
1136 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 if (a->table == a->smalltable || b->table == b->smalltable) {
1141 memcpy(tab, a->smalltable, sizeof(tab));
1142 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1143 memcpy(b->smalltable, tab, sizeof(tab));
1144 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1147 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1148 h = a->hash; a->hash = b->hash; b->hash = h;
1149 } else {
1150 a->hash = -1;
1151 b->hash = -1;
1152 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001153}
1154
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001155static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001156set_copy(PySetObject *so)
1157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001159}
1160
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001161static PyObject *
1162frozenset_copy(PySetObject *so)
1163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 if (PyFrozenSet_CheckExact(so)) {
1165 Py_INCREF(so);
1166 return (PyObject *)so;
1167 }
1168 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001169}
1170
Raymond Hettingera690a992003-11-16 16:17:49 +00001171PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1172
1173static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001174set_clear(PySetObject *so)
1175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 set_clear_internal(so);
1177 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001178}
1179
1180PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1181
1182static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001183set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 PySetObject *result;
1186 PyObject *other;
1187 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 result = (PySetObject *)set_copy(so);
1190 if (result == NULL)
1191 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1194 other = PyTuple_GET_ITEM(args, i);
1195 if ((PyObject *)so == other)
1196 continue;
1197 if (set_update_internal(result, other) == -1) {
1198 Py_DECREF(result);
1199 return NULL;
1200 }
1201 }
1202 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001203}
1204
1205PyDoc_STRVAR(union_doc,
1206 "Return the union of sets as a new set.\n\
1207\n\
1208(i.e. all elements that are in either set.)");
1209
1210static PyObject *
1211set_or(PySetObject *so, PyObject *other)
1212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1216 Py_INCREF(Py_NotImplemented);
1217 return Py_NotImplemented;
1218 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 result = (PySetObject *)set_copy(so);
1221 if (result == NULL)
1222 return NULL;
1223 if ((PyObject *)so == other)
1224 return (PyObject *)result;
1225 if (set_update_internal(result, other) == -1) {
1226 Py_DECREF(result);
1227 return NULL;
1228 }
1229 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001230}
1231
Raymond Hettingera690a992003-11-16 16:17:49 +00001232static PyObject *
1233set_ior(PySetObject *so, PyObject *other)
1234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 if (!PyAnySet_Check(other)) {
1236 Py_INCREF(Py_NotImplemented);
1237 return Py_NotImplemented;
1238 }
1239 if (set_update_internal(so, other) == -1)
1240 return NULL;
1241 Py_INCREF(so);
1242 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001243}
1244
1245static PyObject *
1246set_intersection(PySetObject *so, PyObject *other)
1247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 PySetObject *result;
1249 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if ((PyObject *)so == other)
1252 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1255 if (result == NULL)
1256 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (PyAnySet_Check(other)) {
1259 Py_ssize_t pos = 0;
1260 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1263 tmp = (PyObject *)so;
1264 so = (PySetObject *)other;
1265 other = tmp;
1266 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 while (set_next((PySetObject *)other, &pos, &entry)) {
1269 int rv = set_contains_entry(so, entry);
1270 if (rv == -1) {
1271 Py_DECREF(result);
1272 return NULL;
1273 }
1274 if (rv) {
1275 if (set_add_entry(result, entry) == -1) {
1276 Py_DECREF(result);
1277 return NULL;
1278 }
1279 }
1280 }
1281 return (PyObject *)result;
1282 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 it = PyObject_GetIter(other);
1285 if (it == NULL) {
1286 Py_DECREF(result);
1287 return NULL;
1288 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 while ((key = PyIter_Next(it)) != NULL) {
1291 int rv;
1292 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001293 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if (hash == -1) {
1296 Py_DECREF(it);
1297 Py_DECREF(result);
1298 Py_DECREF(key);
1299 return NULL;
1300 }
1301 entry.hash = hash;
1302 entry.key = key;
1303 rv = set_contains_entry(so, &entry);
1304 if (rv == -1) {
1305 Py_DECREF(it);
1306 Py_DECREF(result);
1307 Py_DECREF(key);
1308 return NULL;
1309 }
1310 if (rv) {
1311 if (set_add_entry(result, &entry) == -1) {
1312 Py_DECREF(it);
1313 Py_DECREF(result);
1314 Py_DECREF(key);
1315 return NULL;
1316 }
1317 }
1318 Py_DECREF(key);
1319 }
1320 Py_DECREF(it);
1321 if (PyErr_Occurred()) {
1322 Py_DECREF(result);
1323 return NULL;
1324 }
1325 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001326}
1327
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001328static PyObject *
1329set_intersection_multi(PySetObject *so, PyObject *args)
1330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 Py_ssize_t i;
1332 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (PyTuple_GET_SIZE(args) == 0)
1335 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 Py_INCREF(so);
1338 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1339 PyObject *other = PyTuple_GET_ITEM(args, i);
1340 PyObject *newresult = set_intersection((PySetObject *)result, other);
1341 if (newresult == NULL) {
1342 Py_DECREF(result);
1343 return NULL;
1344 }
1345 Py_DECREF(result);
1346 result = newresult;
1347 }
1348 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001349}
1350
Raymond Hettingera690a992003-11-16 16:17:49 +00001351PyDoc_STRVAR(intersection_doc,
1352"Return the intersection of two sets as a new set.\n\
1353\n\
1354(i.e. all elements that are in both sets.)");
1355
1356static PyObject *
1357set_intersection_update(PySetObject *so, PyObject *other)
1358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 tmp = set_intersection(so, other);
1362 if (tmp == NULL)
1363 return NULL;
1364 set_swap_bodies(so, (PySetObject *)tmp);
1365 Py_DECREF(tmp);
1366 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001367}
1368
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001369static PyObject *
1370set_intersection_update_multi(PySetObject *so, PyObject *args)
1371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 tmp = set_intersection_multi(so, args);
1375 if (tmp == NULL)
1376 return NULL;
1377 set_swap_bodies(so, (PySetObject *)tmp);
1378 Py_DECREF(tmp);
1379 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001380}
1381
Raymond Hettingera690a992003-11-16 16:17:49 +00001382PyDoc_STRVAR(intersection_update_doc,
1383"Update a set with the intersection of itself and another.");
1384
1385static PyObject *
1386set_and(PySetObject *so, PyObject *other)
1387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1389 Py_INCREF(Py_NotImplemented);
1390 return Py_NotImplemented;
1391 }
1392 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001393}
1394
1395static PyObject *
1396set_iand(PySetObject *so, PyObject *other)
1397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 if (!PyAnySet_Check(other)) {
1401 Py_INCREF(Py_NotImplemented);
1402 return Py_NotImplemented;
1403 }
1404 result = set_intersection_update(so, other);
1405 if (result == NULL)
1406 return NULL;
1407 Py_DECREF(result);
1408 Py_INCREF(so);
1409 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001410}
1411
Guido van Rossum58da9312007-11-10 23:39:45 +00001412static PyObject *
1413set_isdisjoint(PySetObject *so, PyObject *other)
1414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 if ((PyObject *)so == other) {
1418 if (PySet_GET_SIZE(so) == 0)
1419 Py_RETURN_TRUE;
1420 else
1421 Py_RETURN_FALSE;
1422 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 if (PyAnySet_CheckExact(other)) {
1425 Py_ssize_t pos = 0;
1426 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1429 tmp = (PyObject *)so;
1430 so = (PySetObject *)other;
1431 other = tmp;
1432 }
1433 while (set_next((PySetObject *)other, &pos, &entry)) {
1434 int rv = set_contains_entry(so, entry);
1435 if (rv == -1)
1436 return NULL;
1437 if (rv)
1438 Py_RETURN_FALSE;
1439 }
1440 Py_RETURN_TRUE;
1441 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 it = PyObject_GetIter(other);
1444 if (it == NULL)
1445 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 while ((key = PyIter_Next(it)) != NULL) {
1448 int rv;
1449 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001450 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (hash == -1) {
1453 Py_DECREF(key);
1454 Py_DECREF(it);
1455 return NULL;
1456 }
1457 entry.hash = hash;
1458 entry.key = key;
1459 rv = set_contains_entry(so, &entry);
1460 Py_DECREF(key);
1461 if (rv == -1) {
1462 Py_DECREF(it);
1463 return NULL;
1464 }
1465 if (rv) {
1466 Py_DECREF(it);
1467 Py_RETURN_FALSE;
1468 }
1469 }
1470 Py_DECREF(it);
1471 if (PyErr_Occurred())
1472 return NULL;
1473 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001474}
1475
1476PyDoc_STRVAR(isdisjoint_doc,
1477"Return True if two sets have a null intersection.");
1478
Neal Norwitz6576bd82005-11-13 18:41:28 +00001479static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001480set_difference_update_internal(PySetObject *so, PyObject *other)
1481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 if ((PyObject *)so == other)
1483 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 if (PyAnySet_Check(other)) {
1486 setentry *entry;
1487 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 while (set_next((PySetObject *)other, &pos, &entry))
1490 if (set_discard_entry(so, entry) == -1)
1491 return -1;
1492 } else {
1493 PyObject *key, *it;
1494 it = PyObject_GetIter(other);
1495 if (it == NULL)
1496 return -1;
1497
1498 while ((key = PyIter_Next(it)) != NULL) {
1499 if (set_discard_key(so, key) == -1) {
1500 Py_DECREF(it);
1501 Py_DECREF(key);
1502 return -1;
1503 }
1504 Py_DECREF(key);
1505 }
1506 Py_DECREF(it);
1507 if (PyErr_Occurred())
1508 return -1;
1509 }
1510 /* If more than 1/5 are dummies, then resize them away. */
1511 if ((so->fill - so->used) * 5 < so->mask)
1512 return 0;
1513 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001514}
1515
Raymond Hettingera690a992003-11-16 16:17:49 +00001516static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001517set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1522 PyObject *other = PyTuple_GET_ITEM(args, i);
1523 if (set_difference_update_internal(so, other) == -1)
1524 return NULL;
1525 }
1526 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001527}
1528
1529PyDoc_STRVAR(difference_update_doc,
1530"Remove all elements of another set from this set.");
1531
1532static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001533set_copy_and_difference(PySetObject *so, PyObject *other)
1534{
1535 PyObject *result;
1536
1537 result = set_copy(so);
1538 if (result == NULL)
1539 return NULL;
1540 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1541 return result;
1542 Py_DECREF(result);
1543 return NULL;
1544}
1545
1546static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001547set_difference(PySetObject *so, PyObject *other)
1548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 PyObject *result;
1550 setentry *entry;
1551 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001554 return set_copy_and_difference(so, other);
1555 }
1556
1557 /* If len(so) much more than len(other), it's more efficient to simply copy
1558 * so and then iterate other looking for common elements. */
1559 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1560 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 result = make_new_set_basetype(Py_TYPE(so), NULL);
1564 if (result == NULL)
1565 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 if (PyDict_CheckExact(other)) {
1568 while (set_next(so, &pos, &entry)) {
1569 setentry entrycopy;
1570 entrycopy.hash = entry->hash;
1571 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001572 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1574 Py_DECREF(result);
1575 return NULL;
1576 }
1577 }
1578 }
1579 return result;
1580 }
1581
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001582 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 while (set_next(so, &pos, &entry)) {
1584 int rv = set_contains_entry((PySetObject *)other, entry);
1585 if (rv == -1) {
1586 Py_DECREF(result);
1587 return NULL;
1588 }
1589 if (!rv) {
1590 if (set_add_entry((PySetObject *)result, entry) == -1) {
1591 Py_DECREF(result);
1592 return NULL;
1593 }
1594 }
1595 }
1596 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001597}
1598
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001599static PyObject *
1600set_difference_multi(PySetObject *so, PyObject *args)
1601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 Py_ssize_t i;
1603 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 if (PyTuple_GET_SIZE(args) == 0)
1606 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 other = PyTuple_GET_ITEM(args, 0);
1609 result = set_difference(so, other);
1610 if (result == NULL)
1611 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1614 other = PyTuple_GET_ITEM(args, i);
1615 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1616 Py_DECREF(result);
1617 return NULL;
1618 }
1619 }
1620 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001621}
1622
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001623PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001624"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001625\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001626(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001627static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001628set_sub(PySetObject *so, PyObject *other)
1629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1631 Py_INCREF(Py_NotImplemented);
1632 return Py_NotImplemented;
1633 }
1634 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001635}
1636
1637static PyObject *
1638set_isub(PySetObject *so, PyObject *other)
1639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (!PyAnySet_Check(other)) {
1641 Py_INCREF(Py_NotImplemented);
1642 return Py_NotImplemented;
1643 }
1644 if (set_difference_update_internal(so, other) == -1)
1645 return NULL;
1646 Py_INCREF(so);
1647 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001648}
1649
1650static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001651set_symmetric_difference_update(PySetObject *so, PyObject *other)
1652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 PySetObject *otherset;
1654 PyObject *key;
1655 Py_ssize_t pos = 0;
1656 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if ((PyObject *)so == other)
1659 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (PyDict_CheckExact(other)) {
1662 PyObject *value;
1663 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001664 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1666 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001667
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001668 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 an_entry.hash = hash;
1670 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001673 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001674 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001677 if (rv == DISCARD_NOTFOUND) {
1678 if (set_add_entry(so, &an_entry) == -1) {
1679 Py_DECREF(key);
1680 return NULL;
1681 }
1682 }
Georg Brandl2d444492010-09-03 10:52:55 +00001683 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 }
1685 Py_RETURN_NONE;
1686 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if (PyAnySet_Check(other)) {
1689 Py_INCREF(other);
1690 otherset = (PySetObject *)other;
1691 } else {
1692 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1693 if (otherset == NULL)
1694 return NULL;
1695 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 while (set_next(otherset, &pos, &entry)) {
1698 int rv = set_discard_entry(so, entry);
1699 if (rv == -1) {
1700 Py_DECREF(otherset);
1701 return NULL;
1702 }
1703 if (rv == DISCARD_NOTFOUND) {
1704 if (set_add_entry(so, entry) == -1) {
1705 Py_DECREF(otherset);
1706 return NULL;
1707 }
1708 }
1709 }
1710 Py_DECREF(otherset);
1711 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001712}
1713
1714PyDoc_STRVAR(symmetric_difference_update_doc,
1715"Update a set with the symmetric difference of itself and another.");
1716
1717static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001718set_symmetric_difference(PySetObject *so, PyObject *other)
1719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 PyObject *rv;
1721 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1724 if (otherset == NULL)
1725 return NULL;
1726 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1727 if (rv == NULL)
1728 return NULL;
1729 Py_DECREF(rv);
1730 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001731}
1732
1733PyDoc_STRVAR(symmetric_difference_doc,
1734"Return the symmetric difference of two sets as a new set.\n\
1735\n\
1736(i.e. all elements that are in exactly one of the sets.)");
1737
1738static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001739set_xor(PySetObject *so, PyObject *other)
1740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1742 Py_INCREF(Py_NotImplemented);
1743 return Py_NotImplemented;
1744 }
1745 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001746}
1747
1748static PyObject *
1749set_ixor(PySetObject *so, PyObject *other)
1750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (!PyAnySet_Check(other)) {
1754 Py_INCREF(Py_NotImplemented);
1755 return Py_NotImplemented;
1756 }
1757 result = set_symmetric_difference_update(so, other);
1758 if (result == NULL)
1759 return NULL;
1760 Py_DECREF(result);
1761 Py_INCREF(so);
1762 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001763}
1764
1765static PyObject *
1766set_issubset(PySetObject *so, PyObject *other)
1767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 setentry *entry;
1769 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (!PyAnySet_Check(other)) {
1772 PyObject *tmp, *result;
1773 tmp = make_new_set(&PySet_Type, other);
1774 if (tmp == NULL)
1775 return NULL;
1776 result = set_issubset(so, tmp);
1777 Py_DECREF(tmp);
1778 return result;
1779 }
1780 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1781 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 while (set_next(so, &pos, &entry)) {
1784 int rv = set_contains_entry((PySetObject *)other, entry);
1785 if (rv == -1)
1786 return NULL;
1787 if (!rv)
1788 Py_RETURN_FALSE;
1789 }
1790 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001791}
1792
1793PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1794
1795static PyObject *
1796set_issuperset(PySetObject *so, PyObject *other)
1797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 if (!PyAnySet_Check(other)) {
1801 tmp = make_new_set(&PySet_Type, other);
1802 if (tmp == NULL)
1803 return NULL;
1804 result = set_issuperset(so, tmp);
1805 Py_DECREF(tmp);
1806 return result;
1807 }
1808 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001809}
1810
1811PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1812
Raymond Hettingera690a992003-11-16 16:17:49 +00001813static PyObject *
1814set_richcompare(PySetObject *v, PyObject *w, int op)
1815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if(!PyAnySet_Check(w)) {
1819 Py_INCREF(Py_NotImplemented);
1820 return Py_NotImplemented;
1821 }
1822 switch (op) {
1823 case Py_EQ:
1824 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1825 Py_RETURN_FALSE;
1826 if (v->hash != -1 &&
1827 ((PySetObject *)w)->hash != -1 &&
1828 v->hash != ((PySetObject *)w)->hash)
1829 Py_RETURN_FALSE;
1830 return set_issubset(v, w);
1831 case Py_NE:
1832 r1 = set_richcompare(v, w, Py_EQ);
1833 if (r1 == NULL)
1834 return NULL;
1835 r2 = PyBool_FromLong(PyObject_Not(r1));
1836 Py_DECREF(r1);
1837 return r2;
1838 case Py_LE:
1839 return set_issubset(v, w);
1840 case Py_GE:
1841 return set_issuperset(v, w);
1842 case Py_LT:
1843 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1844 Py_RETURN_FALSE;
1845 return set_issubset(v, w);
1846 case Py_GT:
1847 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1848 Py_RETURN_FALSE;
1849 return set_issuperset(v, w);
1850 }
1851 Py_INCREF(Py_NotImplemented);
1852 return Py_NotImplemented;
Raymond Hettingera690a992003-11-16 16:17:49 +00001853}
1854
Raymond Hettingera690a992003-11-16 16:17:49 +00001855static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001856set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 if (set_add_key(so, key) == -1)
1859 return NULL;
1860 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001861}
1862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001864"Add an element to a set.\n\
1865\n\
1866This has no effect if the element is already present.");
1867
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001868static int
1869set_contains(PySetObject *so, PyObject *key)
1870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 PyObject *tmpkey;
1872 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 rv = set_contains_key(so, key);
1875 if (rv == -1) {
1876 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1877 return -1;
1878 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001879 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 if (tmpkey == NULL)
1881 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 rv = set_contains(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 Py_DECREF(tmpkey);
1884 }
1885 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001886}
1887
1888static PyObject *
1889set_direct_contains(PySetObject *so, PyObject *key)
1890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 result = set_contains(so, key);
1894 if (result == -1)
1895 return NULL;
1896 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001897}
1898
1899PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1900
Raymond Hettingera690a992003-11-16 16:17:49 +00001901static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001902set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 PyObject *tmpkey;
1905 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 rv = set_discard_key(so, key);
1908 if (rv == -1) {
1909 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1910 return NULL;
1911 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001912 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 if (tmpkey == NULL)
1914 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 Py_DECREF(tmpkey);
1917 if (rv == -1)
1918 return NULL;
1919 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (rv == DISCARD_NOTFOUND) {
1922 set_key_error(key);
1923 return NULL;
1924 }
1925 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001926}
1927
1928PyDoc_STRVAR(remove_doc,
1929"Remove an element from a set; it must be a member.\n\
1930\n\
1931If the element is not a member, raise a KeyError.");
1932
1933static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001934set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 PyObject *tmpkey, *result;
1937 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 rv = set_discard_key(so, key);
1940 if (rv == -1) {
1941 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1942 return NULL;
1943 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001944 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 if (tmpkey == NULL)
1946 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 result = set_discard(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 Py_DECREF(tmpkey);
1949 return result;
1950 }
1951 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001952}
1953
1954PyDoc_STRVAR(discard_doc,
1955"Remove an element from a set if it is a member.\n\
1956\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001958
1959static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001960set_reduce(PySetObject *so)
1961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 keys = PySequence_List((PyObject *)so);
1965 if (keys == NULL)
1966 goto done;
1967 args = PyTuple_Pack(1, keys);
1968 if (args == NULL)
1969 goto done;
1970 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1971 if (dict == NULL) {
1972 PyErr_Clear();
1973 dict = Py_None;
1974 Py_INCREF(dict);
1975 }
1976 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001977done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 Py_XDECREF(args);
1979 Py_XDECREF(keys);
1980 Py_XDECREF(dict);
1981 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001982}
1983
1984PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1985
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001986static PyObject *
1987set_sizeof(PySetObject *so)
1988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 res = sizeof(PySetObject);
1992 if (so->table != so->smalltable)
1993 res = res + (so->mask + 1) * sizeof(setentry);
1994 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001995}
1996
1997PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001998static int
1999set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 if (!PyAnySet_Check(self))
2004 return -1;
2005 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2006 return -1;
2007 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2008 return -1;
2009 set_clear_internal(self);
2010 self->hash = -1;
2011 if (iterable == NULL)
2012 return 0;
2013 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002014}
2015
Raymond Hettingera690a992003-11-16 16:17:49 +00002016static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 set_len, /* sq_length */
2018 0, /* sq_concat */
2019 0, /* sq_repeat */
2020 0, /* sq_item */
2021 0, /* sq_slice */
2022 0, /* sq_ass_item */
2023 0, /* sq_ass_slice */
2024 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002025};
2026
2027/* set object ********************************************************/
2028
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002029#ifdef Py_DEBUG
2030static PyObject *test_c_api(PySetObject *so);
2031
2032PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2033All is well if assertions don't fail.");
2034#endif
2035
Raymond Hettingera690a992003-11-16 16:17:49 +00002036static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 {"add", (PyCFunction)set_add, METH_O,
2038 add_doc},
2039 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2040 clear_doc},
2041 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2042 contains_doc},
2043 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2044 copy_doc},
2045 {"discard", (PyCFunction)set_discard, METH_O,
2046 discard_doc},
2047 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2048 difference_doc},
2049 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2050 difference_update_doc},
2051 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2052 intersection_doc},
2053 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2054 intersection_update_doc},
2055 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2056 isdisjoint_doc},
2057 {"issubset", (PyCFunction)set_issubset, METH_O,
2058 issubset_doc},
2059 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2060 issuperset_doc},
2061 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2062 pop_doc},
2063 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2064 reduce_doc},
2065 {"remove", (PyCFunction)set_remove, METH_O,
2066 remove_doc},
2067 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2068 sizeof_doc},
2069 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2070 symmetric_difference_doc},
2071 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2072 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002073#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2075 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002076#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 {"union", (PyCFunction)set_union, METH_VARARGS,
2078 union_doc},
2079 {"update", (PyCFunction)set_update, METH_VARARGS,
2080 update_doc},
2081 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002082};
2083
2084static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 0, /*nb_add*/
2086 (binaryfunc)set_sub, /*nb_subtract*/
2087 0, /*nb_multiply*/
2088 0, /*nb_remainder*/
2089 0, /*nb_divmod*/
2090 0, /*nb_power*/
2091 0, /*nb_negative*/
2092 0, /*nb_positive*/
2093 0, /*nb_absolute*/
2094 0, /*nb_bool*/
2095 0, /*nb_invert*/
2096 0, /*nb_lshift*/
2097 0, /*nb_rshift*/
2098 (binaryfunc)set_and, /*nb_and*/
2099 (binaryfunc)set_xor, /*nb_xor*/
2100 (binaryfunc)set_or, /*nb_or*/
2101 0, /*nb_int*/
2102 0, /*nb_reserved*/
2103 0, /*nb_float*/
2104 0, /*nb_inplace_add*/
2105 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2106 0, /*nb_inplace_multiply*/
2107 0, /*nb_inplace_remainder*/
2108 0, /*nb_inplace_power*/
2109 0, /*nb_inplace_lshift*/
2110 0, /*nb_inplace_rshift*/
2111 (binaryfunc)set_iand, /*nb_inplace_and*/
2112 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2113 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002114};
2115
2116PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002117"set() -> new empty set object\n\
2118set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002119\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002120Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002121
2122PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2124 "set", /* tp_name */
2125 sizeof(PySetObject), /* tp_basicsize */
2126 0, /* tp_itemsize */
2127 /* methods */
2128 (destructor)set_dealloc, /* tp_dealloc */
2129 0, /* tp_print */
2130 0, /* tp_getattr */
2131 0, /* tp_setattr */
2132 0, /* tp_reserved */
2133 (reprfunc)set_repr, /* tp_repr */
2134 &set_as_number, /* tp_as_number */
2135 &set_as_sequence, /* tp_as_sequence */
2136 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002137 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 0, /* tp_call */
2139 0, /* tp_str */
2140 PyObject_GenericGetAttr, /* tp_getattro */
2141 0, /* tp_setattro */
2142 0, /* tp_as_buffer */
2143 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2144 Py_TPFLAGS_BASETYPE, /* tp_flags */
2145 set_doc, /* tp_doc */
2146 (traverseproc)set_traverse, /* tp_traverse */
2147 (inquiry)set_clear_internal, /* tp_clear */
2148 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002149 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2150 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 0, /* tp_iternext */
2152 set_methods, /* tp_methods */
2153 0, /* tp_members */
2154 0, /* tp_getset */
2155 0, /* tp_base */
2156 0, /* tp_dict */
2157 0, /* tp_descr_get */
2158 0, /* tp_descr_set */
2159 0, /* tp_dictoffset */
2160 (initproc)set_init, /* tp_init */
2161 PyType_GenericAlloc, /* tp_alloc */
2162 set_new, /* tp_new */
2163 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002164};
2165
2166/* frozenset object ********************************************************/
2167
2168
2169static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2171 contains_doc},
2172 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2173 copy_doc},
2174 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2175 difference_doc},
2176 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2177 intersection_doc},
2178 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2179 isdisjoint_doc},
2180 {"issubset", (PyCFunction)set_issubset, METH_O,
2181 issubset_doc},
2182 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2183 issuperset_doc},
2184 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2185 reduce_doc},
2186 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2187 sizeof_doc},
2188 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2189 symmetric_difference_doc},
2190 {"union", (PyCFunction)set_union, METH_VARARGS,
2191 union_doc},
2192 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002193};
2194
2195static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 0, /*nb_add*/
2197 (binaryfunc)set_sub, /*nb_subtract*/
2198 0, /*nb_multiply*/
2199 0, /*nb_remainder*/
2200 0, /*nb_divmod*/
2201 0, /*nb_power*/
2202 0, /*nb_negative*/
2203 0, /*nb_positive*/
2204 0, /*nb_absolute*/
2205 0, /*nb_bool*/
2206 0, /*nb_invert*/
2207 0, /*nb_lshift*/
2208 0, /*nb_rshift*/
2209 (binaryfunc)set_and, /*nb_and*/
2210 (binaryfunc)set_xor, /*nb_xor*/
2211 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002212};
2213
2214PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002215"frozenset() -> empty frozenset object\n\
2216frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002217\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002218Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002219
2220PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2222 "frozenset", /* tp_name */
2223 sizeof(PySetObject), /* tp_basicsize */
2224 0, /* tp_itemsize */
2225 /* methods */
2226 (destructor)set_dealloc, /* tp_dealloc */
2227 0, /* tp_print */
2228 0, /* tp_getattr */
2229 0, /* tp_setattr */
2230 0, /* tp_reserved */
2231 (reprfunc)set_repr, /* tp_repr */
2232 &frozenset_as_number, /* tp_as_number */
2233 &set_as_sequence, /* tp_as_sequence */
2234 0, /* tp_as_mapping */
2235 frozenset_hash, /* tp_hash */
2236 0, /* tp_call */
2237 0, /* tp_str */
2238 PyObject_GenericGetAttr, /* tp_getattro */
2239 0, /* tp_setattro */
2240 0, /* tp_as_buffer */
2241 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2242 Py_TPFLAGS_BASETYPE, /* tp_flags */
2243 frozenset_doc, /* tp_doc */
2244 (traverseproc)set_traverse, /* tp_traverse */
2245 (inquiry)set_clear_internal, /* tp_clear */
2246 (richcmpfunc)set_richcompare, /* tp_richcompare */
2247 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2248 (getiterfunc)set_iter, /* tp_iter */
2249 0, /* tp_iternext */
2250 frozenset_methods, /* tp_methods */
2251 0, /* tp_members */
2252 0, /* tp_getset */
2253 0, /* tp_base */
2254 0, /* tp_dict */
2255 0, /* tp_descr_get */
2256 0, /* tp_descr_set */
2257 0, /* tp_dictoffset */
2258 0, /* tp_init */
2259 PyType_GenericAlloc, /* tp_alloc */
2260 frozenset_new, /* tp_new */
2261 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002262};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002263
2264
2265/***** C API functions *************************************************/
2266
2267PyObject *
2268PySet_New(PyObject *iterable)
2269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002271}
2272
2273PyObject *
2274PyFrozenSet_New(PyObject *iterable)
2275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002277}
2278
Neal Norwitz8c49c822006-03-04 18:41:19 +00002279Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002280PySet_Size(PyObject *anyset)
2281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 if (!PyAnySet_Check(anyset)) {
2283 PyErr_BadInternalCall();
2284 return -1;
2285 }
2286 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002287}
2288
2289int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002290PySet_Clear(PyObject *set)
2291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 if (!PySet_Check(set)) {
2293 PyErr_BadInternalCall();
2294 return -1;
2295 }
2296 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002297}
2298
2299int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002300PySet_Contains(PyObject *anyset, PyObject *key)
2301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 if (!PyAnySet_Check(anyset)) {
2303 PyErr_BadInternalCall();
2304 return -1;
2305 }
2306 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002307}
2308
2309int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002310PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 if (!PySet_Check(set)) {
2313 PyErr_BadInternalCall();
2314 return -1;
2315 }
2316 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002317}
2318
2319int
Christian Heimesfd66e512008-01-29 12:18:50 +00002320PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 if (!PySet_Check(anyset) &&
2323 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2324 PyErr_BadInternalCall();
2325 return -1;
2326 }
2327 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002328}
2329
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002330int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002331_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 if (!PyAnySet_Check(set)) {
2336 PyErr_BadInternalCall();
2337 return -1;
2338 }
2339 if (set_next((PySetObject *)set, pos, &entry) == 0)
2340 return 0;
2341 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002342 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002344}
2345
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002346PyObject *
2347PySet_Pop(PyObject *set)
2348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 if (!PySet_Check(set)) {
2350 PyErr_BadInternalCall();
2351 return NULL;
2352 }
2353 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002354}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002355
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002356int
2357_PySet_Update(PyObject *set, PyObject *iterable)
2358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 if (!PySet_Check(set)) {
2360 PyErr_BadInternalCall();
2361 return -1;
2362 }
2363 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002364}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002365
2366#ifdef Py_DEBUG
2367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002369 Returns True and original set is restored. */
2370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371#define assertRaises(call_return_value, exception) \
2372 do { \
2373 assert(call_return_value); \
2374 assert(PyErr_ExceptionMatches(exception)); \
2375 PyErr_Clear(); \
2376 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002377
2378static PyObject *
2379test_c_api(PySetObject *so)
2380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 Py_ssize_t count;
2382 char *s;
2383 Py_ssize_t i;
2384 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2385 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002386 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 /* Verify preconditions */
2390 assert(PyAnySet_Check(ob));
2391 assert(PyAnySet_CheckExact(ob));
2392 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 /* so.clear(); so |= set("abc"); */
2395 str = PyUnicode_FromString("abc");
2396 if (str == NULL)
2397 return NULL;
2398 set_clear_internal(so);
2399 if (set_update_internal(so, str) == -1) {
2400 Py_DECREF(str);
2401 return NULL;
2402 }
2403 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* Exercise type/size checks */
2406 assert(PySet_Size(ob) == 3);
2407 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* Raise TypeError for non-iterable constructor arguments */
2410 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2411 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Raise TypeError for unhashable key */
2414 dup = PySet_New(ob);
2415 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2416 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2417 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* Exercise successful pop, contains, add, and discard */
2420 elem = PySet_Pop(ob);
2421 assert(PySet_Contains(ob, elem) == 0);
2422 assert(PySet_GET_SIZE(ob) == 2);
2423 assert(PySet_Add(ob, elem) == 0);
2424 assert(PySet_Contains(ob, elem) == 1);
2425 assert(PySet_GET_SIZE(ob) == 3);
2426 assert(PySet_Discard(ob, elem) == 1);
2427 assert(PySet_GET_SIZE(ob) == 2);
2428 assert(PySet_Discard(ob, elem) == 0);
2429 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 /* Exercise clear */
2432 dup2 = PySet_New(dup);
2433 assert(PySet_Clear(dup2) == 0);
2434 assert(PySet_Size(dup2) == 0);
2435 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 /* Raise SystemError on clear or update of frozen set */
2438 f = PyFrozenSet_New(dup);
2439 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2440 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2441 assert(PySet_Add(f, elem) == 0);
2442 Py_INCREF(f);
2443 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2444 Py_DECREF(f);
2445 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 /* Exercise direct iteration */
2448 i = 0, count = 0;
2449 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2450 s = _PyUnicode_AsString(x);
2451 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2452 count++;
2453 }
2454 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 /* Exercise updates */
2457 dup2 = PySet_New(NULL);
2458 assert(_PySet_Update(dup2, dup) == 0);
2459 assert(PySet_Size(dup2) == 3);
2460 assert(_PySet_Update(dup2, dup) == 0);
2461 assert(PySet_Size(dup2) == 3);
2462 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 /* Raise SystemError when self argument is not a set or frozenset. */
2465 t = PyTuple_New(0);
2466 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2467 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2468 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 /* Raise SystemError when self argument is not a set. */
2471 f = PyFrozenSet_New(dup);
2472 assert(PySet_Size(f) == 3);
2473 assert(PyFrozenSet_CheckExact(f));
2474 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2475 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2476 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 /* Raise KeyError when popping from an empty set */
2479 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2480 Py_DECREF(ob);
2481 assert(PySet_GET_SIZE(ob) == 0);
2482 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 /* Restore the set from the copy using the PyNumber API */
2485 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2486 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 /* Verify constructors accept NULL arguments */
2489 f = PySet_New(NULL);
2490 assert(f != NULL);
2491 assert(PySet_GET_SIZE(f) == 0);
2492 Py_DECREF(f);
2493 f = PyFrozenSet_New(NULL);
2494 assert(f != NULL);
2495 assert(PyFrozenSet_CheckExact(f));
2496 assert(PySet_GET_SIZE(f) == 0);
2497 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 Py_DECREF(elem);
2500 Py_DECREF(dup);
2501 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002502}
2503
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002504#undef assertRaises
2505
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002506#endif