blob: 7364fca73731427112dd7d360348f57d14c5c5e9 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettinger6c3c1cc2013-08-31 21:34:24 -07007 Copyright (c) 2003-2013 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
26 Unlike the dictionary implementation, the lookkey functions can return
27 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettingerc56e0e32013-09-02 16:32:27 -070034/* This must be >= 1 */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035#define PERTURB_SHIFT 5
Raymond Hettingerc56e0e32013-09-02 16:32:27 -070036
37/* This should be >= PySet_MINSIZE - 1 */
Raymond Hettingera35adf52013-09-02 03:23:21 -070038#define LINEAR_PROBES 9
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
40/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050041static PyObject _dummy_struct;
42
43#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000044
Antoine Pitrou9d952542013-08-24 21:07:07 +020045/* Exported for the gdb plugin's benefit. */
46PyObject *_PySet_Dummy = dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000048
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070049/* ======================================================================== */
50/* ======= Begin logic for probing the hash table ========================= */
51
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020053set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070056 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020057 setentry *entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070058 setentry *limit;
Raymond Hettingerafe89092013-08-28 20:59:31 -070059 size_t perturb = hash;
60 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070061 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
62 size_t j;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020063 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000064
Raymond Hettingera35adf52013-09-02 03:23:21 -070065 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070066 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070068
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070069 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070071 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070072 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070073 PyObject *startkey = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 Py_INCREF(startkey);
75 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
76 Py_DECREF(startkey);
77 if (cmp < 0)
78 return NULL;
Raymond Hettingerafe89092013-08-28 20:59:31 -070079 if (table != so->table || entry->key != startkey)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 return set_lookkey(so, key, hash);
Raymond Hettingerafe89092013-08-28 20:59:31 -070081 if (cmp > 0)
82 return entry;
83 }
84 if (entry->key == dummy && freeslot == NULL)
85 freeslot = entry;
86
Raymond Hettingera35adf52013-09-02 03:23:21 -070087 limit = &table[mask];
88 for (j = 0 ; j < LINEAR_PROBES ; j++) {
89 entry = (entry == limit) ? &table[0] : entry + 1;
90 if (entry->key == NULL)
91 goto found_null;
92 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070093 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070094 if (entry->hash == hash && entry->key != dummy) {
95 PyObject *startkey = entry->key;
96 Py_INCREF(startkey);
97 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
98 Py_DECREF(startkey);
99 if (cmp < 0)
100 return NULL;
101 if (table != so->table || entry->key != startkey)
102 return set_lookkey(so, key, hash);
103 if (cmp > 0)
104 return entry;
105 }
106 if (entry->key == dummy && freeslot == NULL)
107 freeslot = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700109
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700110 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700111 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700112
Raymond Hettingera35adf52013-09-02 03:23:21 -0700113 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700114 if (entry->key == NULL)
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700115 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700117 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700118 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000119}
120
121/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000122 * Hacked up version of set_lookkey which can assume keys are always unicode;
123 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000124 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000125 */
126static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200127set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700130 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200131 setentry *entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700132 setentry *limit;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700133 size_t perturb = hash;
134 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700135 size_t i = (size_t)hash;
136 size_t j;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 /* Make sure this function doesn't have to handle non-unicode keys,
139 including subclasses of str; e.g., one reason to subclass
140 strings is to override __eq__, and for speed we don't cater to
141 that here. */
142 if (!PyUnicode_CheckExact(key)) {
143 so->lookup = set_lookkey;
144 return set_lookkey(so, key, hash);
145 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700146
Raymond Hettingera35adf52013-09-02 03:23:21 -0700147 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700148 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000150
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700151 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700152 if (entry->key == key
153 || (entry->hash == hash
154 && entry->key != dummy
155 && unicode_eq(entry->key, key)))
156 return entry;
157 if (entry->key == dummy && freeslot == NULL)
158 freeslot = entry;
159
Raymond Hettingera35adf52013-09-02 03:23:21 -0700160 limit = &table[mask];
161 for (j = 0 ; j < LINEAR_PROBES ; j++) {
162 entry = (entry == limit) ? &table[0] : entry + 1;
163 if (entry->key == NULL)
164 goto found_null;
165 if (entry->key == key
166 || (entry->hash == hash
167 && entry->key != dummy
168 && unicode_eq(entry->key, key)))
169 return entry;
170 if (entry->key == dummy && freeslot == NULL)
171 freeslot = entry;
172 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700173
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700174 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700175 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700176
Raymond Hettingera35adf52013-09-02 03:23:21 -0700177 entry = &table[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 if (entry->key == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -0700179 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700181 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700182 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000183}
184
185/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186Internal routine used by set_table_resize() to insert an item which is
187known to be absent from the set. This routine also assumes that
188the set contains no deleted entries. Besides the performance benefit,
189using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
190Note that no refcounts are changed by this routine; if needed, the caller
191is responsible for incref'ing `key`.
192*/
193static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200194set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200197 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700198 size_t perturb = hash;
199 size_t mask = (size_t)so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700200 size_t i = (size_t)hash;
201 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000202
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700203 while (1) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700204 entry = &table[i & mask];
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700205 if (entry->key == NULL)
Raymond Hettingera35adf52013-09-02 03:23:21 -0700206 goto found_null;
207 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
208 entry = &table[(i + j) & mask];
209 if (entry->key == NULL)
210 goto found_null;
211 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700212 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700213 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700215 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 so->fill++;
217 entry->key = key;
218 entry->hash = hash;
219 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000220}
221
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700222/* ======== End logic for probing the hash table ========================== */
223/* ======================================================================== */
224
225
226/*
227Internal routine to insert a new key into the table.
228Used by the public insert routine.
229Eats a reference to key.
230*/
231static int
232set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
233{
234 setentry *entry;
235
236 assert(so->lookup != NULL);
237 entry = so->lookup(so, key, hash);
238 if (entry == NULL)
239 return -1;
240 if (entry->key == NULL) {
241 /* UNUSED */
242 so->fill++;
243 entry->key = key;
244 entry->hash = hash;
245 so->used++;
246 } else if (entry->key == dummy) {
247 /* DUMMY */
248 entry->key = key;
249 entry->hash = hash;
250 so->used++;
251 } else {
252 /* ACTIVE */
253 Py_DECREF(key);
254 }
255 return 0;
256}
257
Thomas Wouters89f507f2006-12-13 04:49:30 +0000258/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000259Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000260keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000261actually be smaller than the old one.
262*/
263static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 Py_ssize_t newsize;
267 setentry *oldtable, *newtable, *entry;
268 Py_ssize_t i;
269 int is_oldtable_malloced;
270 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* Find the smallest table size > minused. */
275 for (newsize = PySet_MINSIZE;
276 newsize <= minused && newsize > 0;
277 newsize <<= 1)
278 ;
279 if (newsize <= 0) {
280 PyErr_NoMemory();
281 return -1;
282 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 /* Get space for a new table. */
285 oldtable = so->table;
286 assert(oldtable != NULL);
287 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 if (newsize == PySet_MINSIZE) {
290 /* A large table is shrinking, or we can't get any smaller. */
291 newtable = so->smalltable;
292 if (newtable == oldtable) {
293 if (so->fill == so->used) {
294 /* No dummies, so no point doing anything. */
295 return 0;
296 }
297 /* We're not going to resize it, but rebuild the
298 table anyway to purge old dummy entries.
299 Subtle: This is *necessary* if fill==size,
300 as set_lookkey needs at least one virgin slot to
301 terminate failing searches. If fill < size, it's
302 merely desirable, as dummies slow searches. */
303 assert(so->fill > so->used);
304 memcpy(small_copy, oldtable, sizeof(small_copy));
305 oldtable = small_copy;
306 }
307 }
308 else {
309 newtable = PyMem_NEW(setentry, newsize);
310 if (newtable == NULL) {
311 PyErr_NoMemory();
312 return -1;
313 }
314 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 /* Make the set empty, using the new table. */
317 assert(newtable != oldtable);
318 so->table = newtable;
319 so->mask = newsize - 1;
320 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700321 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 /* Copy the data over; this is refcount-neutral for active entries;
326 dummy entries aren't copied over, of course */
327 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500328 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000330 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 }
332 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 if (is_oldtable_malloced)
335 PyMem_DEL(oldtable);
336 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337}
338
Raymond Hettingerc991db22005-08-11 07:58:45 +0000339/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
340
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200342set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000343{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200344 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000345 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100346 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 assert(so->fill <= so->mask); /* at least one empty slot */
349 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000350 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100351 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000352 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 return -1;
354 }
355 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
356 return 0;
357 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000358}
359
360static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200361set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000362{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200363 Py_hash_t hash;
364 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200367 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 hash = PyObject_Hash(key);
369 if (hash == -1)
370 return -1;
371 }
372 assert(so->fill <= so->mask); /* at least one empty slot */
373 n_used = so->used;
374 Py_INCREF(key);
375 if (set_insert_key(so, key, hash) == -1) {
376 Py_DECREF(key);
377 return -1;
378 }
379 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
380 return 0;
381 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382}
383
384#define DISCARD_NOTFOUND 0
385#define DISCARD_FOUND 1
386
387static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000388set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200389{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000391
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000392 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (entry == NULL)
394 return -1;
395 if (entry->key == NULL || entry->key == dummy)
396 return DISCARD_NOTFOUND;
397 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 entry->key = dummy;
399 so->used--;
400 Py_DECREF(old_key);
401 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000402}
403
404static int
405set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000406{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200407 Py_hash_t hash;
408 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200414 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 hash = PyObject_Hash(key);
416 if (hash == -1)
417 return -1;
418 }
419 entry = (so->lookup)(so, key, hash);
420 if (entry == NULL)
421 return -1;
422 if (entry->key == NULL || entry->key == dummy)
423 return DISCARD_NOTFOUND;
424 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 entry->key = dummy;
426 so->used--;
427 Py_DECREF(old_key);
428 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000429}
430
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700431static void
432set_empty_to_minsize(PySetObject *so)
433{
434 memset(so->smalltable, 0, sizeof(so->smalltable));
435 so->fill = 0;
436 so->used = 0;
437 so->mask = PySet_MINSIZE - 1;
438 so->table = so->smalltable;
439 so->hash = -1;
440}
441
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000442static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443set_clear_internal(PySetObject *so)
444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 setentry *entry, *table;
446 int table_is_malloced;
447 Py_ssize_t fill;
448 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 Py_ssize_t i, n;
451 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 n = so->mask + 1;
454 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455#endif
456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 table = so->table;
458 assert(table != NULL);
459 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 /* This is delicate. During the process of clearing the set,
462 * decrefs can cause the set to mutate. To avoid fatal confusion
463 * (voice of experience), we have to make the set empty before
464 * clearing the slots, and never refer to anything via so->ref while
465 * clearing.
466 */
467 fill = so->fill;
468 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700469 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 else if (fill > 0) {
472 /* It's a small table with something that needs to be cleared.
473 * Afraid the only safe way is to copy the set entries into
474 * another small table first.
475 */
476 memcpy(small_copy, table, sizeof(small_copy));
477 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700478 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 }
480 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 /* Now we can finally clear things. If C had refcounts, we could
483 * assert that the refcount on table is 1 now, i.e. that this function
484 * has unique access to it, so decref side-effects can't alter it.
485 */
486 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 assert(i < n);
489 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000490#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (entry->key) {
492 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700493 if (entry->key != dummy)
494 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 else
498 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (table_is_malloced)
503 PyMem_DEL(table);
504 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505}
506
507/*
508 * Iterate over a set table. Use like so:
509 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000511 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000512 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000513 * while (set_next(yourset, &pos, &entry)) {
514 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515 * }
516 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000517 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519 */
520static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000521set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 Py_ssize_t i;
524 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200525 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 assert (PyAnySet_Check(so));
528 i = *pos_ptr;
529 assert(i >= 0);
530 table = so->table;
531 mask = so->mask;
532 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
533 i++;
534 *pos_ptr = i+1;
535 if (i > mask)
536 return 0;
537 assert(table[i].key != NULL);
538 *entry_ptr = &table[i];
539 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540}
541
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000542static void
543set_dealloc(PySetObject *so)
544{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200545 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 Py_ssize_t fill = so->fill;
547 PyObject_GC_UnTrack(so);
548 Py_TRASHCAN_SAFE_BEGIN(so)
549 if (so->weakreflist != NULL)
550 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 for (entry = so->table; fill > 0; entry++) {
553 if (entry->key) {
554 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700555 if (entry->key != dummy)
556 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 }
558 }
559 if (so->table != so->smalltable)
560 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700561 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000563}
564
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000565static PyObject *
566set_repr(PySetObject *so)
567{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200568 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (status != 0) {
572 if (status < 0)
573 return NULL;
574 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
575 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 /* shortcut for the empty set */
578 if (!so->used) {
579 Py_ReprLeave((PyObject*)so);
580 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
581 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 keys = PySequence_List((PyObject *)so);
584 if (keys == NULL)
585 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000586
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200587 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 listrepr = PyObject_Repr(keys);
589 Py_DECREF(keys);
590 if (listrepr == NULL)
591 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200594 if (tmp == NULL)
595 goto done;
596 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200597
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200598 if (Py_TYPE(so) != &PySet_Type)
599 result = PyUnicode_FromFormat("%s({%U})",
600 Py_TYPE(so)->tp_name,
601 listrepr);
602 else
603 result = PyUnicode_FromFormat("{%U}", listrepr);
604 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000605done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 Py_ReprLeave((PyObject*)so);
607 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000608}
609
Martin v. Löwis18e16552006-02-15 17:27:45 +0000610static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000611set_len(PyObject *so)
612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000614}
615
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000616static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000617set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000620 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100621 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200622 Py_ssize_t i;
623 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 assert (PyAnySet_Check(so));
626 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 other = (PySetObject*)otherset;
629 if (other == so || other->used == 0)
630 /* a.update(a) or a.update({}); nothing to do */
631 return 0;
632 /* Do one big resize at the start, rather than
633 * incrementally resizing as we insert new keys. Expect
634 * that there will be no (or few) overlapping keys.
635 */
636 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
637 if (set_table_resize(so, (so->used + other->used)*2) != 0)
638 return -1;
639 }
640 for (i = 0; i <= other->mask; i++) {
641 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000642 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100643 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000644 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500645 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000646 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100647 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000648 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 return -1;
650 }
651 }
652 }
653 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000654}
655
656static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000657set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000658{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000659 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200663 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 hash = PyObject_Hash(key);
665 if (hash == -1)
666 return -1;
667 }
668 entry = (so->lookup)(so, key, hash);
669 if (entry == NULL)
670 return -1;
671 key = entry->key;
672 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673}
674
Raymond Hettingerc991db22005-08-11 07:58:45 +0000675static int
676set_contains_entry(PySetObject *so, setentry *entry)
677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 PyObject *key;
679 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000680
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000681 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (lu_entry == NULL)
683 return -1;
684 key = lu_entry->key;
685 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000686}
687
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000688static PyObject *
689set_pop(PySetObject *so)
690{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200691 Py_ssize_t i = 0;
692 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 assert (PyAnySet_Check(so));
696 if (so->used == 0) {
697 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
698 return NULL;
699 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 /* Set entry to "the first" unused or dummy set entry. We abuse
702 * the hash field of slot 0 to hold a search finger:
703 * If slot 0 has a value, use slot 0.
704 * Else slot 0 is being used to hold a search finger,
705 * and we use its hash value as the first index to look.
706 */
707 entry = &so->table[0];
708 if (entry->key == NULL || entry->key == dummy) {
709 i = entry->hash;
710 /* The hash field may be a real hash value, or it may be a
711 * legit search finger, or it may be a once-legit search
712 * finger that's out of bounds now because it wrapped around
713 * or the table shrunk -- simply make sure it's in bounds now.
714 */
715 if (i > so->mask || i < 1)
716 i = 1; /* skip slot 0 */
717 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
718 i++;
719 if (i > so->mask)
720 i = 1;
721 }
722 }
723 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 entry->key = dummy;
725 so->used--;
726 so->table[0].hash = i + 1; /* next place to start */
727 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000728}
729
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000730PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
731Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000732
733static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000734set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 Py_ssize_t pos = 0;
737 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 while (set_next(so, &pos, &entry))
740 Py_VISIT(entry->key);
741 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000742}
743
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000744static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000745frozenset_hash(PyObject *self)
746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800748 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 setentry *entry;
750 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 if (so->hash != -1)
753 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000754
Mark Dickinson57e683e2011-09-24 18:18:40 +0100755 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 while (set_next(so, &pos, &entry)) {
757 /* Work to increase the bit dispersion for closely spaced hash
758 values. The is important because some use cases have many
759 combinations of a small number of elements with nearby
760 hashes so that many distinct combinations collapse to only
761 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000762 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800763 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800765 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800767 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 so->hash = hash;
769 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000770}
771
Raymond Hettingera9d99362005-08-05 00:01:15 +0000772/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000773
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000774typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 PyObject_HEAD
776 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
777 Py_ssize_t si_used;
778 Py_ssize_t si_pos;
779 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000780} setiterobject;
781
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000782static void
783setiter_dealloc(setiterobject *si)
784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 Py_XDECREF(si->si_set);
786 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000787}
788
789static int
790setiter_traverse(setiterobject *si, visitproc visit, void *arg)
791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 Py_VISIT(si->si_set);
793 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794}
795
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000796static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797setiter_len(setiterobject *si)
798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 Py_ssize_t len = 0;
800 if (si->si_set != NULL && si->si_used == si->si_set->used)
801 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000802 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000803}
804
Armin Rigof5b3e362006-02-11 21:32:43 +0000805PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000806
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000807static PyObject *setiter_iternext(setiterobject *si);
808
809static PyObject *
810setiter_reduce(setiterobject *si)
811{
812 PyObject *list;
813 setiterobject tmp;
814
815 list = PyList_New(0);
816 if (!list)
817 return NULL;
818
Ezio Melotti0e1af282012-09-28 16:43:40 +0300819 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000820 tmp = *si;
821 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300822
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000823 /* iterate the temporary into a list */
824 for(;;) {
825 PyObject *element = setiter_iternext(&tmp);
826 if (element) {
827 if (PyList_Append(list, element)) {
828 Py_DECREF(element);
829 Py_DECREF(list);
830 Py_XDECREF(tmp.si_set);
831 return NULL;
832 }
833 Py_DECREF(element);
834 } else
835 break;
836 }
837 Py_XDECREF(tmp.si_set);
838 /* check for error */
839 if (tmp.si_set != NULL) {
840 /* we have an error */
841 Py_DECREF(list);
842 return NULL;
843 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200844 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000845}
846
847PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
848
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000849static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000851 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853};
854
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000855static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200858 Py_ssize_t i, mask;
859 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 if (so == NULL)
863 return NULL;
864 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 if (si->si_used != so->used) {
867 PyErr_SetString(PyExc_RuntimeError,
868 "Set changed size during iteration");
869 si->si_used = -1; /* Make this state sticky */
870 return NULL;
871 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 i = si->si_pos;
874 assert(i>=0);
875 entry = so->table;
876 mask = so->mask;
877 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
878 i++;
879 si->si_pos = i+1;
880 if (i > mask)
881 goto fail;
882 si->len--;
883 key = entry[i].key;
884 Py_INCREF(key);
885 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000886
887fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_DECREF(so);
889 si->si_set = NULL;
890 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891}
892
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000893PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyVarObject_HEAD_INIT(&PyType_Type, 0)
895 "set_iterator", /* tp_name */
896 sizeof(setiterobject), /* tp_basicsize */
897 0, /* tp_itemsize */
898 /* methods */
899 (destructor)setiter_dealloc, /* tp_dealloc */
900 0, /* tp_print */
901 0, /* tp_getattr */
902 0, /* tp_setattr */
903 0, /* tp_reserved */
904 0, /* tp_repr */
905 0, /* tp_as_number */
906 0, /* tp_as_sequence */
907 0, /* tp_as_mapping */
908 0, /* tp_hash */
909 0, /* tp_call */
910 0, /* tp_str */
911 PyObject_GenericGetAttr, /* tp_getattro */
912 0, /* tp_setattro */
913 0, /* tp_as_buffer */
914 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
915 0, /* tp_doc */
916 (traverseproc)setiter_traverse, /* tp_traverse */
917 0, /* tp_clear */
918 0, /* tp_richcompare */
919 0, /* tp_weaklistoffset */
920 PyObject_SelfIter, /* tp_iter */
921 (iternextfunc)setiter_iternext, /* tp_iternext */
922 setiter_methods, /* tp_methods */
923 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000924};
925
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000926static PyObject *
927set_iter(PySetObject *so)
928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
930 if (si == NULL)
931 return NULL;
932 Py_INCREF(so);
933 si->si_set = so;
934 si->si_used = so->used;
935 si->si_pos = 0;
936 si->len = so->used;
937 _PyObject_GC_TRACK(si);
938 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939}
940
Raymond Hettingerd7946662005-08-01 21:39:29 +0000941static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 if (PyAnySet_Check(other))
947 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (PyDict_CheckExact(other)) {
950 PyObject *value;
951 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000952 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 /* Do one big resize at the start, rather than
956 * incrementally resizing as we insert new keys. Expect
957 * that there will be no (or few) overlapping keys.
958 */
959 if (dictsize == -1)
960 return -1;
961 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
962 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
963 return -1;
964 }
965 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
966 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 an_entry.hash = hash;
969 an_entry.key = key;
970 if (set_add_entry(so, &an_entry) == -1)
971 return -1;
972 }
973 return 0;
974 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 it = PyObject_GetIter(other);
977 if (it == NULL)
978 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 while ((key = PyIter_Next(it)) != NULL) {
981 if (set_add_key(so, key) == -1) {
982 Py_DECREF(it);
983 Py_DECREF(key);
984 return -1;
985 }
986 Py_DECREF(key);
987 }
988 Py_DECREF(it);
989 if (PyErr_Occurred())
990 return -1;
991 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000992}
993
994static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000995set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1000 PyObject *other = PyTuple_GET_ITEM(args, i);
1001 if (set_update_internal(so, other) == -1)
1002 return NULL;
1003 }
1004 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001005}
1006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001008"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001009
1010static PyObject *
1011make_new_set(PyTypeObject *type, PyObject *iterable)
1012{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001013 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001016 so = (PySetObject *)type->tp_alloc(type, 0);
1017 if (so == NULL)
1018 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001019
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001020 so->fill = 0;
1021 so->used = 0;
1022 so->mask = PySet_MINSIZE - 1;
1023 so->table = so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 so->lookup = set_lookkey_unicode;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001025 so->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (iterable != NULL) {
1029 if (set_update_internal(so, iterable) == -1) {
1030 Py_DECREF(so);
1031 return NULL;
1032 }
1033 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001036}
1037
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001038static PyObject *
1039make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1042 if (PyType_IsSubtype(type, &PySet_Type))
1043 type = &PySet_Type;
1044 else
1045 type = &PyFrozenSet_Type;
1046 }
1047 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001048}
1049
Raymond Hettingerd7946662005-08-01 21:39:29 +00001050/* The empty frozenset is a singleton */
1051static PyObject *emptyfrozenset = NULL;
1052
Raymond Hettingera690a992003-11-16 16:17:49 +00001053static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001054frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1059 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1062 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (type != &PyFrozenSet_Type)
1065 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (iterable != NULL) {
1068 /* frozenset(f) is idempotent */
1069 if (PyFrozenSet_CheckExact(iterable)) {
1070 Py_INCREF(iterable);
1071 return iterable;
1072 }
1073 result = make_new_set(type, iterable);
1074 if (result == NULL || PySet_GET_SIZE(result))
1075 return result;
1076 Py_DECREF(result);
1077 }
1078 /* The empty frozenset is a singleton */
1079 if (emptyfrozenset == NULL)
1080 emptyfrozenset = make_new_set(type, NULL);
1081 Py_XINCREF(emptyfrozenset);
1082 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001083}
1084
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001085int
1086PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001087{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001088 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001089}
1090
1091void
1092PySet_Fini(void)
1093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001095}
1096
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001097static PyObject *
1098set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1101 return NULL;
1102
1103 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001104}
1105
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001106/* set_swap_bodies() switches the contents of any two sets by moving their
1107 internal data pointers and, if needed, copying the internal smalltables.
1108 Semantically equivalent to:
1109
1110 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1111
1112 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001114 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001116 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001117*/
1118
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001119static void
1120set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 Py_ssize_t t;
1123 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001124 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001126 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 t = a->fill; a->fill = b->fill; b->fill = t;
1129 t = a->used; a->used = b->used; b->used = t;
1130 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 u = a->table;
1133 if (a->table == a->smalltable)
1134 u = b->smalltable;
1135 a->table = b->table;
1136 if (b->table == b->smalltable)
1137 a->table = a->smalltable;
1138 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 if (a->table == a->smalltable || b->table == b->smalltable) {
1143 memcpy(tab, a->smalltable, sizeof(tab));
1144 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1145 memcpy(b->smalltable, tab, sizeof(tab));
1146 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1149 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1150 h = a->hash; a->hash = b->hash; b->hash = h;
1151 } else {
1152 a->hash = -1;
1153 b->hash = -1;
1154 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001155}
1156
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001157static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001158set_copy(PySetObject *so)
1159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001161}
1162
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001163static PyObject *
1164frozenset_copy(PySetObject *so)
1165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 if (PyFrozenSet_CheckExact(so)) {
1167 Py_INCREF(so);
1168 return (PyObject *)so;
1169 }
1170 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001171}
1172
Raymond Hettingera690a992003-11-16 16:17:49 +00001173PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1174
1175static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001176set_clear(PySetObject *so)
1177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 set_clear_internal(so);
1179 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001180}
1181
1182PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1183
1184static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001185set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 PySetObject *result;
1188 PyObject *other;
1189 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 result = (PySetObject *)set_copy(so);
1192 if (result == NULL)
1193 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1196 other = PyTuple_GET_ITEM(args, i);
1197 if ((PyObject *)so == other)
1198 continue;
1199 if (set_update_internal(result, other) == -1) {
1200 Py_DECREF(result);
1201 return NULL;
1202 }
1203 }
1204 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001205}
1206
1207PyDoc_STRVAR(union_doc,
1208 "Return the union of sets as a new set.\n\
1209\n\
1210(i.e. all elements that are in either set.)");
1211
1212static PyObject *
1213set_or(PySetObject *so, PyObject *other)
1214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001216
Brian Curtindfc80e32011-08-10 20:28:54 -05001217 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1218 Py_RETURN_NOTIMPLEMENTED;
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{
Brian Curtindfc80e32011-08-10 20:28:54 -05001235 if (!PyAnySet_Check(other))
1236 Py_RETURN_NOTIMPLEMENTED;
1237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 if (set_update_internal(so, other) == -1)
1239 return NULL;
1240 Py_INCREF(so);
1241 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001242}
1243
1244static PyObject *
1245set_intersection(PySetObject *so, PyObject *other)
1246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 PySetObject *result;
1248 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if ((PyObject *)so == other)
1251 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1254 if (result == NULL)
1255 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (PyAnySet_Check(other)) {
1258 Py_ssize_t pos = 0;
1259 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1262 tmp = (PyObject *)so;
1263 so = (PySetObject *)other;
1264 other = tmp;
1265 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 while (set_next((PySetObject *)other, &pos, &entry)) {
1268 int rv = set_contains_entry(so, entry);
1269 if (rv == -1) {
1270 Py_DECREF(result);
1271 return NULL;
1272 }
1273 if (rv) {
1274 if (set_add_entry(result, entry) == -1) {
1275 Py_DECREF(result);
1276 return NULL;
1277 }
1278 }
1279 }
1280 return (PyObject *)result;
1281 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 it = PyObject_GetIter(other);
1284 if (it == NULL) {
1285 Py_DECREF(result);
1286 return NULL;
1287 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 while ((key = PyIter_Next(it)) != NULL) {
1290 int rv;
1291 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001292 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (hash == -1) {
1295 Py_DECREF(it);
1296 Py_DECREF(result);
1297 Py_DECREF(key);
1298 return NULL;
1299 }
1300 entry.hash = hash;
1301 entry.key = key;
1302 rv = set_contains_entry(so, &entry);
1303 if (rv == -1) {
1304 Py_DECREF(it);
1305 Py_DECREF(result);
1306 Py_DECREF(key);
1307 return NULL;
1308 }
1309 if (rv) {
1310 if (set_add_entry(result, &entry) == -1) {
1311 Py_DECREF(it);
1312 Py_DECREF(result);
1313 Py_DECREF(key);
1314 return NULL;
1315 }
1316 }
1317 Py_DECREF(key);
1318 }
1319 Py_DECREF(it);
1320 if (PyErr_Occurred()) {
1321 Py_DECREF(result);
1322 return NULL;
1323 }
1324 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001325}
1326
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001327static PyObject *
1328set_intersection_multi(PySetObject *so, PyObject *args)
1329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 Py_ssize_t i;
1331 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (PyTuple_GET_SIZE(args) == 0)
1334 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 Py_INCREF(so);
1337 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1338 PyObject *other = PyTuple_GET_ITEM(args, i);
1339 PyObject *newresult = set_intersection((PySetObject *)result, other);
1340 if (newresult == NULL) {
1341 Py_DECREF(result);
1342 return NULL;
1343 }
1344 Py_DECREF(result);
1345 result = newresult;
1346 }
1347 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001348}
1349
Raymond Hettingera690a992003-11-16 16:17:49 +00001350PyDoc_STRVAR(intersection_doc,
1351"Return the intersection of two sets as a new set.\n\
1352\n\
1353(i.e. all elements that are in both sets.)");
1354
1355static PyObject *
1356set_intersection_update(PySetObject *so, PyObject *other)
1357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 tmp = set_intersection(so, other);
1361 if (tmp == NULL)
1362 return NULL;
1363 set_swap_bodies(so, (PySetObject *)tmp);
1364 Py_DECREF(tmp);
1365 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001366}
1367
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001368static PyObject *
1369set_intersection_update_multi(PySetObject *so, PyObject *args)
1370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 tmp = set_intersection_multi(so, args);
1374 if (tmp == NULL)
1375 return NULL;
1376 set_swap_bodies(so, (PySetObject *)tmp);
1377 Py_DECREF(tmp);
1378 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001379}
1380
Raymond Hettingera690a992003-11-16 16:17:49 +00001381PyDoc_STRVAR(intersection_update_doc,
1382"Update a set with the intersection of itself and another.");
1383
1384static PyObject *
1385set_and(PySetObject *so, PyObject *other)
1386{
Brian Curtindfc80e32011-08-10 20:28:54 -05001387 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1388 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001390}
1391
1392static PyObject *
1393set_iand(PySetObject *so, PyObject *other)
1394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001396
Brian Curtindfc80e32011-08-10 20:28:54 -05001397 if (!PyAnySet_Check(other))
1398 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 result = set_intersection_update(so, other);
1400 if (result == NULL)
1401 return NULL;
1402 Py_DECREF(result);
1403 Py_INCREF(so);
1404 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001405}
1406
Guido van Rossum58da9312007-11-10 23:39:45 +00001407static PyObject *
1408set_isdisjoint(PySetObject *so, PyObject *other)
1409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if ((PyObject *)so == other) {
1413 if (PySet_GET_SIZE(so) == 0)
1414 Py_RETURN_TRUE;
1415 else
1416 Py_RETURN_FALSE;
1417 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (PyAnySet_CheckExact(other)) {
1420 Py_ssize_t pos = 0;
1421 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1424 tmp = (PyObject *)so;
1425 so = (PySetObject *)other;
1426 other = tmp;
1427 }
1428 while (set_next((PySetObject *)other, &pos, &entry)) {
1429 int rv = set_contains_entry(so, entry);
1430 if (rv == -1)
1431 return NULL;
1432 if (rv)
1433 Py_RETURN_FALSE;
1434 }
1435 Py_RETURN_TRUE;
1436 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 it = PyObject_GetIter(other);
1439 if (it == NULL)
1440 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 while ((key = PyIter_Next(it)) != NULL) {
1443 int rv;
1444 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001445 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (hash == -1) {
1448 Py_DECREF(key);
1449 Py_DECREF(it);
1450 return NULL;
1451 }
1452 entry.hash = hash;
1453 entry.key = key;
1454 rv = set_contains_entry(so, &entry);
1455 Py_DECREF(key);
1456 if (rv == -1) {
1457 Py_DECREF(it);
1458 return NULL;
1459 }
1460 if (rv) {
1461 Py_DECREF(it);
1462 Py_RETURN_FALSE;
1463 }
1464 }
1465 Py_DECREF(it);
1466 if (PyErr_Occurred())
1467 return NULL;
1468 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001469}
1470
1471PyDoc_STRVAR(isdisjoint_doc,
1472"Return True if two sets have a null intersection.");
1473
Neal Norwitz6576bd82005-11-13 18:41:28 +00001474static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001475set_difference_update_internal(PySetObject *so, PyObject *other)
1476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if ((PyObject *)so == other)
1478 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 if (PyAnySet_Check(other)) {
1481 setentry *entry;
1482 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 while (set_next((PySetObject *)other, &pos, &entry))
1485 if (set_discard_entry(so, entry) == -1)
1486 return -1;
1487 } else {
1488 PyObject *key, *it;
1489 it = PyObject_GetIter(other);
1490 if (it == NULL)
1491 return -1;
1492
1493 while ((key = PyIter_Next(it)) != NULL) {
1494 if (set_discard_key(so, key) == -1) {
1495 Py_DECREF(it);
1496 Py_DECREF(key);
1497 return -1;
1498 }
1499 Py_DECREF(key);
1500 }
1501 Py_DECREF(it);
1502 if (PyErr_Occurred())
1503 return -1;
1504 }
1505 /* If more than 1/5 are dummies, then resize them away. */
1506 if ((so->fill - so->used) * 5 < so->mask)
1507 return 0;
1508 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001509}
1510
Raymond Hettingera690a992003-11-16 16:17:49 +00001511static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001512set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1517 PyObject *other = PyTuple_GET_ITEM(args, i);
1518 if (set_difference_update_internal(so, other) == -1)
1519 return NULL;
1520 }
1521 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001522}
1523
1524PyDoc_STRVAR(difference_update_doc,
1525"Remove all elements of another set from this set.");
1526
1527static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001528set_copy_and_difference(PySetObject *so, PyObject *other)
1529{
1530 PyObject *result;
1531
1532 result = set_copy(so);
1533 if (result == NULL)
1534 return NULL;
1535 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1536 return result;
1537 Py_DECREF(result);
1538 return NULL;
1539}
1540
1541static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001542set_difference(PySetObject *so, PyObject *other)
1543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 PyObject *result;
1545 setentry *entry;
1546 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001549 return set_copy_and_difference(so, other);
1550 }
1551
1552 /* If len(so) much more than len(other), it's more efficient to simply copy
1553 * so and then iterate other looking for common elements. */
1554 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1555 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 result = make_new_set_basetype(Py_TYPE(so), NULL);
1559 if (result == NULL)
1560 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (PyDict_CheckExact(other)) {
1563 while (set_next(so, &pos, &entry)) {
1564 setentry entrycopy;
1565 entrycopy.hash = entry->hash;
1566 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001567 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1569 Py_DECREF(result);
1570 return NULL;
1571 }
1572 }
1573 }
1574 return result;
1575 }
1576
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001577 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 while (set_next(so, &pos, &entry)) {
1579 int rv = set_contains_entry((PySetObject *)other, entry);
1580 if (rv == -1) {
1581 Py_DECREF(result);
1582 return NULL;
1583 }
1584 if (!rv) {
1585 if (set_add_entry((PySetObject *)result, entry) == -1) {
1586 Py_DECREF(result);
1587 return NULL;
1588 }
1589 }
1590 }
1591 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001592}
1593
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001594static PyObject *
1595set_difference_multi(PySetObject *so, PyObject *args)
1596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 Py_ssize_t i;
1598 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 if (PyTuple_GET_SIZE(args) == 0)
1601 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 other = PyTuple_GET_ITEM(args, 0);
1604 result = set_difference(so, other);
1605 if (result == NULL)
1606 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1609 other = PyTuple_GET_ITEM(args, i);
1610 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1611 Py_DECREF(result);
1612 return NULL;
1613 }
1614 }
1615 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001616}
1617
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001618PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001619"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001620\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001621(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001622static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001623set_sub(PySetObject *so, PyObject *other)
1624{
Brian Curtindfc80e32011-08-10 20:28:54 -05001625 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1626 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001628}
1629
1630static PyObject *
1631set_isub(PySetObject *so, PyObject *other)
1632{
Brian Curtindfc80e32011-08-10 20:28:54 -05001633 if (!PyAnySet_Check(other))
1634 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 if (set_difference_update_internal(so, other) == -1)
1636 return NULL;
1637 Py_INCREF(so);
1638 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001639}
1640
1641static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001642set_symmetric_difference_update(PySetObject *so, PyObject *other)
1643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 PySetObject *otherset;
1645 PyObject *key;
1646 Py_ssize_t pos = 0;
1647 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if ((PyObject *)so == other)
1650 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 if (PyDict_CheckExact(other)) {
1653 PyObject *value;
1654 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001655 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1657 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001658
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001659 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 an_entry.hash = hash;
1661 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001664 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001665 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001668 if (rv == DISCARD_NOTFOUND) {
1669 if (set_add_entry(so, &an_entry) == -1) {
1670 Py_DECREF(key);
1671 return NULL;
1672 }
1673 }
Georg Brandl2d444492010-09-03 10:52:55 +00001674 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 }
1676 Py_RETURN_NONE;
1677 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 if (PyAnySet_Check(other)) {
1680 Py_INCREF(other);
1681 otherset = (PySetObject *)other;
1682 } else {
1683 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1684 if (otherset == NULL)
1685 return NULL;
1686 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 while (set_next(otherset, &pos, &entry)) {
1689 int rv = set_discard_entry(so, entry);
1690 if (rv == -1) {
1691 Py_DECREF(otherset);
1692 return NULL;
1693 }
1694 if (rv == DISCARD_NOTFOUND) {
1695 if (set_add_entry(so, entry) == -1) {
1696 Py_DECREF(otherset);
1697 return NULL;
1698 }
1699 }
1700 }
1701 Py_DECREF(otherset);
1702 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001703}
1704
1705PyDoc_STRVAR(symmetric_difference_update_doc,
1706"Update a set with the symmetric difference of itself and another.");
1707
1708static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001709set_symmetric_difference(PySetObject *so, PyObject *other)
1710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 PyObject *rv;
1712 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1715 if (otherset == NULL)
1716 return NULL;
1717 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1718 if (rv == NULL)
1719 return NULL;
1720 Py_DECREF(rv);
1721 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001722}
1723
1724PyDoc_STRVAR(symmetric_difference_doc,
1725"Return the symmetric difference of two sets as a new set.\n\
1726\n\
1727(i.e. all elements that are in exactly one of the sets.)");
1728
1729static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001730set_xor(PySetObject *so, PyObject *other)
1731{
Brian Curtindfc80e32011-08-10 20:28:54 -05001732 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1733 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001735}
1736
1737static PyObject *
1738set_ixor(PySetObject *so, PyObject *other)
1739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001741
Brian Curtindfc80e32011-08-10 20:28:54 -05001742 if (!PyAnySet_Check(other))
1743 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 result = set_symmetric_difference_update(so, other);
1745 if (result == NULL)
1746 return NULL;
1747 Py_DECREF(result);
1748 Py_INCREF(so);
1749 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001750}
1751
1752static PyObject *
1753set_issubset(PySetObject *so, PyObject *other)
1754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 setentry *entry;
1756 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 if (!PyAnySet_Check(other)) {
1759 PyObject *tmp, *result;
1760 tmp = make_new_set(&PySet_Type, other);
1761 if (tmp == NULL)
1762 return NULL;
1763 result = set_issubset(so, tmp);
1764 Py_DECREF(tmp);
1765 return result;
1766 }
1767 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1768 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 while (set_next(so, &pos, &entry)) {
1771 int rv = set_contains_entry((PySetObject *)other, entry);
1772 if (rv == -1)
1773 return NULL;
1774 if (!rv)
1775 Py_RETURN_FALSE;
1776 }
1777 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001778}
1779
1780PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1781
1782static PyObject *
1783set_issuperset(PySetObject *so, PyObject *other)
1784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (!PyAnySet_Check(other)) {
1788 tmp = make_new_set(&PySet_Type, other);
1789 if (tmp == NULL)
1790 return NULL;
1791 result = set_issuperset(so, tmp);
1792 Py_DECREF(tmp);
1793 return result;
1794 }
1795 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001796}
1797
1798PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1799
Raymond Hettingera690a992003-11-16 16:17:49 +00001800static PyObject *
1801set_richcompare(PySetObject *v, PyObject *w, int op)
1802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001804
Brian Curtindfc80e32011-08-10 20:28:54 -05001805 if(!PyAnySet_Check(w))
1806 Py_RETURN_NOTIMPLEMENTED;
1807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 switch (op) {
1809 case Py_EQ:
1810 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1811 Py_RETURN_FALSE;
1812 if (v->hash != -1 &&
1813 ((PySetObject *)w)->hash != -1 &&
1814 v->hash != ((PySetObject *)w)->hash)
1815 Py_RETURN_FALSE;
1816 return set_issubset(v, w);
1817 case Py_NE:
1818 r1 = set_richcompare(v, w, Py_EQ);
1819 if (r1 == NULL)
1820 return NULL;
1821 r2 = PyBool_FromLong(PyObject_Not(r1));
1822 Py_DECREF(r1);
1823 return r2;
1824 case Py_LE:
1825 return set_issubset(v, w);
1826 case Py_GE:
1827 return set_issuperset(v, w);
1828 case Py_LT:
1829 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1830 Py_RETURN_FALSE;
1831 return set_issubset(v, w);
1832 case Py_GT:
1833 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1834 Py_RETURN_FALSE;
1835 return set_issuperset(v, w);
1836 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001837 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001838}
1839
Raymond Hettingera690a992003-11-16 16:17:49 +00001840static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001841set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 if (set_add_key(so, key) == -1)
1844 return NULL;
1845 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001846}
1847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001849"Add an element to a set.\n\
1850\n\
1851This has no effect if the element is already present.");
1852
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001853static int
1854set_contains(PySetObject *so, PyObject *key)
1855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 PyObject *tmpkey;
1857 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 rv = set_contains_key(so, key);
1860 if (rv == -1) {
1861 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1862 return -1;
1863 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001864 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 if (tmpkey == NULL)
1866 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001867 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 Py_DECREF(tmpkey);
1869 }
1870 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001871}
1872
1873static PyObject *
1874set_direct_contains(PySetObject *so, PyObject *key)
1875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 result = set_contains(so, key);
1879 if (result == -1)
1880 return NULL;
1881 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001882}
1883
1884PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1885
Raymond Hettingera690a992003-11-16 16:17:49 +00001886static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001887set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 PyObject *tmpkey;
1890 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 rv = set_discard_key(so, key);
1893 if (rv == -1) {
1894 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1895 return NULL;
1896 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001897 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 if (tmpkey == NULL)
1899 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 Py_DECREF(tmpkey);
1902 if (rv == -1)
1903 return NULL;
1904 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001907 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 return NULL;
1909 }
1910 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001911}
1912
1913PyDoc_STRVAR(remove_doc,
1914"Remove an element from a set; it must be a member.\n\
1915\n\
1916If the element is not a member, raise a KeyError.");
1917
1918static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001919set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001920{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001921 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 rv = set_discard_key(so, key);
1925 if (rv == -1) {
1926 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1927 return NULL;
1928 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001929 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 if (tmpkey == NULL)
1931 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001932 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001934 if (rv == -1)
1935 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 }
1937 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001938}
1939
1940PyDoc_STRVAR(discard_doc,
1941"Remove an element from a set if it is a member.\n\
1942\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001944
1945static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001946set_reduce(PySetObject *so)
1947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001949 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 keys = PySequence_List((PyObject *)so);
1952 if (keys == NULL)
1953 goto done;
1954 args = PyTuple_Pack(1, keys);
1955 if (args == NULL)
1956 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001957 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 if (dict == NULL) {
1959 PyErr_Clear();
1960 dict = Py_None;
1961 Py_INCREF(dict);
1962 }
1963 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001964done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 Py_XDECREF(args);
1966 Py_XDECREF(keys);
1967 Py_XDECREF(dict);
1968 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001969}
1970
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001971static PyObject *
1972set_sizeof(PySetObject *so)
1973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 res = sizeof(PySetObject);
1977 if (so->table != so->smalltable)
1978 res = res + (so->mask + 1) * sizeof(setentry);
1979 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001980}
1981
1982PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001983static int
1984set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 if (!PyAnySet_Check(self))
1989 return -1;
1990 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1991 return -1;
1992 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1993 return -1;
1994 set_clear_internal(self);
1995 self->hash = -1;
1996 if (iterable == NULL)
1997 return 0;
1998 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001999}
2000
Raymond Hettingera690a992003-11-16 16:17:49 +00002001static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 set_len, /* sq_length */
2003 0, /* sq_concat */
2004 0, /* sq_repeat */
2005 0, /* sq_item */
2006 0, /* sq_slice */
2007 0, /* sq_ass_item */
2008 0, /* sq_ass_slice */
2009 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002010};
2011
2012/* set object ********************************************************/
2013
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002014#ifdef Py_DEBUG
2015static PyObject *test_c_api(PySetObject *so);
2016
2017PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2018All is well if assertions don't fail.");
2019#endif
2020
Raymond Hettingera690a992003-11-16 16:17:49 +00002021static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 {"add", (PyCFunction)set_add, METH_O,
2023 add_doc},
2024 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2025 clear_doc},
2026 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2027 contains_doc},
2028 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2029 copy_doc},
2030 {"discard", (PyCFunction)set_discard, METH_O,
2031 discard_doc},
2032 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2033 difference_doc},
2034 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2035 difference_update_doc},
2036 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2037 intersection_doc},
2038 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2039 intersection_update_doc},
2040 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2041 isdisjoint_doc},
2042 {"issubset", (PyCFunction)set_issubset, METH_O,
2043 issubset_doc},
2044 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2045 issuperset_doc},
2046 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2047 pop_doc},
2048 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2049 reduce_doc},
2050 {"remove", (PyCFunction)set_remove, METH_O,
2051 remove_doc},
2052 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2053 sizeof_doc},
2054 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2055 symmetric_difference_doc},
2056 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2057 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002058#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2060 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002061#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 {"union", (PyCFunction)set_union, METH_VARARGS,
2063 union_doc},
2064 {"update", (PyCFunction)set_update, METH_VARARGS,
2065 update_doc},
2066 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002067};
2068
2069static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 0, /*nb_add*/
2071 (binaryfunc)set_sub, /*nb_subtract*/
2072 0, /*nb_multiply*/
2073 0, /*nb_remainder*/
2074 0, /*nb_divmod*/
2075 0, /*nb_power*/
2076 0, /*nb_negative*/
2077 0, /*nb_positive*/
2078 0, /*nb_absolute*/
2079 0, /*nb_bool*/
2080 0, /*nb_invert*/
2081 0, /*nb_lshift*/
2082 0, /*nb_rshift*/
2083 (binaryfunc)set_and, /*nb_and*/
2084 (binaryfunc)set_xor, /*nb_xor*/
2085 (binaryfunc)set_or, /*nb_or*/
2086 0, /*nb_int*/
2087 0, /*nb_reserved*/
2088 0, /*nb_float*/
2089 0, /*nb_inplace_add*/
2090 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2091 0, /*nb_inplace_multiply*/
2092 0, /*nb_inplace_remainder*/
2093 0, /*nb_inplace_power*/
2094 0, /*nb_inplace_lshift*/
2095 0, /*nb_inplace_rshift*/
2096 (binaryfunc)set_iand, /*nb_inplace_and*/
2097 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2098 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002099};
2100
2101PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002102"set() -> new empty set object\n\
2103set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002104\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002105Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002106
2107PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2109 "set", /* tp_name */
2110 sizeof(PySetObject), /* tp_basicsize */
2111 0, /* tp_itemsize */
2112 /* methods */
2113 (destructor)set_dealloc, /* tp_dealloc */
2114 0, /* tp_print */
2115 0, /* tp_getattr */
2116 0, /* tp_setattr */
2117 0, /* tp_reserved */
2118 (reprfunc)set_repr, /* tp_repr */
2119 &set_as_number, /* tp_as_number */
2120 &set_as_sequence, /* tp_as_sequence */
2121 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002122 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 0, /* tp_call */
2124 0, /* tp_str */
2125 PyObject_GenericGetAttr, /* tp_getattro */
2126 0, /* tp_setattro */
2127 0, /* tp_as_buffer */
2128 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2129 Py_TPFLAGS_BASETYPE, /* tp_flags */
2130 set_doc, /* tp_doc */
2131 (traverseproc)set_traverse, /* tp_traverse */
2132 (inquiry)set_clear_internal, /* tp_clear */
2133 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002134 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2135 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 0, /* tp_iternext */
2137 set_methods, /* tp_methods */
2138 0, /* tp_members */
2139 0, /* tp_getset */
2140 0, /* tp_base */
2141 0, /* tp_dict */
2142 0, /* tp_descr_get */
2143 0, /* tp_descr_set */
2144 0, /* tp_dictoffset */
2145 (initproc)set_init, /* tp_init */
2146 PyType_GenericAlloc, /* tp_alloc */
2147 set_new, /* tp_new */
2148 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002149};
2150
2151/* frozenset object ********************************************************/
2152
2153
2154static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2156 contains_doc},
2157 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2158 copy_doc},
2159 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2160 difference_doc},
2161 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2162 intersection_doc},
2163 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2164 isdisjoint_doc},
2165 {"issubset", (PyCFunction)set_issubset, METH_O,
2166 issubset_doc},
2167 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2168 issuperset_doc},
2169 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2170 reduce_doc},
2171 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2172 sizeof_doc},
2173 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2174 symmetric_difference_doc},
2175 {"union", (PyCFunction)set_union, METH_VARARGS,
2176 union_doc},
2177 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002178};
2179
2180static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 0, /*nb_add*/
2182 (binaryfunc)set_sub, /*nb_subtract*/
2183 0, /*nb_multiply*/
2184 0, /*nb_remainder*/
2185 0, /*nb_divmod*/
2186 0, /*nb_power*/
2187 0, /*nb_negative*/
2188 0, /*nb_positive*/
2189 0, /*nb_absolute*/
2190 0, /*nb_bool*/
2191 0, /*nb_invert*/
2192 0, /*nb_lshift*/
2193 0, /*nb_rshift*/
2194 (binaryfunc)set_and, /*nb_and*/
2195 (binaryfunc)set_xor, /*nb_xor*/
2196 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002197};
2198
2199PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002200"frozenset() -> empty frozenset object\n\
2201frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002202\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002203Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002204
2205PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2207 "frozenset", /* tp_name */
2208 sizeof(PySetObject), /* tp_basicsize */
2209 0, /* tp_itemsize */
2210 /* methods */
2211 (destructor)set_dealloc, /* tp_dealloc */
2212 0, /* tp_print */
2213 0, /* tp_getattr */
2214 0, /* tp_setattr */
2215 0, /* tp_reserved */
2216 (reprfunc)set_repr, /* tp_repr */
2217 &frozenset_as_number, /* tp_as_number */
2218 &set_as_sequence, /* tp_as_sequence */
2219 0, /* tp_as_mapping */
2220 frozenset_hash, /* tp_hash */
2221 0, /* tp_call */
2222 0, /* tp_str */
2223 PyObject_GenericGetAttr, /* tp_getattro */
2224 0, /* tp_setattro */
2225 0, /* tp_as_buffer */
2226 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2227 Py_TPFLAGS_BASETYPE, /* tp_flags */
2228 frozenset_doc, /* tp_doc */
2229 (traverseproc)set_traverse, /* tp_traverse */
2230 (inquiry)set_clear_internal, /* tp_clear */
2231 (richcmpfunc)set_richcompare, /* tp_richcompare */
2232 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2233 (getiterfunc)set_iter, /* tp_iter */
2234 0, /* tp_iternext */
2235 frozenset_methods, /* tp_methods */
2236 0, /* tp_members */
2237 0, /* tp_getset */
2238 0, /* tp_base */
2239 0, /* tp_dict */
2240 0, /* tp_descr_get */
2241 0, /* tp_descr_set */
2242 0, /* tp_dictoffset */
2243 0, /* tp_init */
2244 PyType_GenericAlloc, /* tp_alloc */
2245 frozenset_new, /* tp_new */
2246 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002247};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002248
2249
2250/***** C API functions *************************************************/
2251
2252PyObject *
2253PySet_New(PyObject *iterable)
2254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002256}
2257
2258PyObject *
2259PyFrozenSet_New(PyObject *iterable)
2260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002262}
2263
Neal Norwitz8c49c822006-03-04 18:41:19 +00002264Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002265PySet_Size(PyObject *anyset)
2266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 if (!PyAnySet_Check(anyset)) {
2268 PyErr_BadInternalCall();
2269 return -1;
2270 }
2271 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002272}
2273
2274int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002275PySet_Clear(PyObject *set)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 if (!PySet_Check(set)) {
2278 PyErr_BadInternalCall();
2279 return -1;
2280 }
2281 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002282}
2283
2284int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002285PySet_Contains(PyObject *anyset, PyObject *key)
2286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 if (!PyAnySet_Check(anyset)) {
2288 PyErr_BadInternalCall();
2289 return -1;
2290 }
2291 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002292}
2293
2294int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002295PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 if (!PySet_Check(set)) {
2298 PyErr_BadInternalCall();
2299 return -1;
2300 }
2301 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002302}
2303
2304int
Christian Heimesfd66e512008-01-29 12:18:50 +00002305PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (!PySet_Check(anyset) &&
2308 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2309 PyErr_BadInternalCall();
2310 return -1;
2311 }
2312 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002313}
2314
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002315int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002316_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 if (!PyAnySet_Check(set)) {
2321 PyErr_BadInternalCall();
2322 return -1;
2323 }
2324 if (set_next((PySetObject *)set, pos, &entry) == 0)
2325 return 0;
2326 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002327 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002329}
2330
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002331PyObject *
2332PySet_Pop(PyObject *set)
2333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 if (!PySet_Check(set)) {
2335 PyErr_BadInternalCall();
2336 return NULL;
2337 }
2338 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002339}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002340
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002341int
2342_PySet_Update(PyObject *set, PyObject *iterable)
2343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 if (!PySet_Check(set)) {
2345 PyErr_BadInternalCall();
2346 return -1;
2347 }
2348 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002349}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002350
2351#ifdef Py_DEBUG
2352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002354 Returns True and original set is restored. */
2355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356#define assertRaises(call_return_value, exception) \
2357 do { \
2358 assert(call_return_value); \
2359 assert(PyErr_ExceptionMatches(exception)); \
2360 PyErr_Clear(); \
2361 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002362
2363static PyObject *
2364test_c_api(PySetObject *so)
2365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 Py_ssize_t count;
2367 char *s;
2368 Py_ssize_t i;
2369 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2370 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002371 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 /* Verify preconditions */
2375 assert(PyAnySet_Check(ob));
2376 assert(PyAnySet_CheckExact(ob));
2377 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 /* so.clear(); so |= set("abc"); */
2380 str = PyUnicode_FromString("abc");
2381 if (str == NULL)
2382 return NULL;
2383 set_clear_internal(so);
2384 if (set_update_internal(so, str) == -1) {
2385 Py_DECREF(str);
2386 return NULL;
2387 }
2388 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 /* Exercise type/size checks */
2391 assert(PySet_Size(ob) == 3);
2392 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 /* Raise TypeError for non-iterable constructor arguments */
2395 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2396 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 /* Raise TypeError for unhashable key */
2399 dup = PySet_New(ob);
2400 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2401 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2402 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 /* Exercise successful pop, contains, add, and discard */
2405 elem = PySet_Pop(ob);
2406 assert(PySet_Contains(ob, elem) == 0);
2407 assert(PySet_GET_SIZE(ob) == 2);
2408 assert(PySet_Add(ob, elem) == 0);
2409 assert(PySet_Contains(ob, elem) == 1);
2410 assert(PySet_GET_SIZE(ob) == 3);
2411 assert(PySet_Discard(ob, elem) == 1);
2412 assert(PySet_GET_SIZE(ob) == 2);
2413 assert(PySet_Discard(ob, elem) == 0);
2414 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* Exercise clear */
2417 dup2 = PySet_New(dup);
2418 assert(PySet_Clear(dup2) == 0);
2419 assert(PySet_Size(dup2) == 0);
2420 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 /* Raise SystemError on clear or update of frozen set */
2423 f = PyFrozenSet_New(dup);
2424 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2425 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2426 assert(PySet_Add(f, elem) == 0);
2427 Py_INCREF(f);
2428 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2429 Py_DECREF(f);
2430 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 /* Exercise direct iteration */
2433 i = 0, count = 0;
2434 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2435 s = _PyUnicode_AsString(x);
2436 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2437 count++;
2438 }
2439 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 /* Exercise updates */
2442 dup2 = PySet_New(NULL);
2443 assert(_PySet_Update(dup2, dup) == 0);
2444 assert(PySet_Size(dup2) == 3);
2445 assert(_PySet_Update(dup2, dup) == 0);
2446 assert(PySet_Size(dup2) == 3);
2447 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 /* Raise SystemError when self argument is not a set or frozenset. */
2450 t = PyTuple_New(0);
2451 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2452 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2453 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 /* Raise SystemError when self argument is not a set. */
2456 f = PyFrozenSet_New(dup);
2457 assert(PySet_Size(f) == 3);
2458 assert(PyFrozenSet_CheckExact(f));
2459 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2460 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2461 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 /* Raise KeyError when popping from an empty set */
2464 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2465 Py_DECREF(ob);
2466 assert(PySet_GET_SIZE(ob) == 0);
2467 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 /* Restore the set from the copy using the PyNumber API */
2470 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2471 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 /* Verify constructors accept NULL arguments */
2474 f = PySet_New(NULL);
2475 assert(f != NULL);
2476 assert(PySet_GET_SIZE(f) == 0);
2477 Py_DECREF(f);
2478 f = PyFrozenSet_New(NULL);
2479 assert(f != NULL);
2480 assert(PyFrozenSet_CheckExact(f));
2481 assert(PySet_GET_SIZE(f) == 0);
2482 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 Py_DECREF(elem);
2485 Py_DECREF(dup);
2486 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002487}
2488
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002489#undef assertRaises
2490
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002491#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002492
2493/***** Dummy Struct *************************************************/
2494
2495static PyObject *
2496dummy_repr(PyObject *op)
2497{
2498 return PyUnicode_FromString("<dummy key>");
2499}
2500
2501static void
2502dummy_dealloc(PyObject* ignore)
2503{
2504 Py_FatalError("deallocating <dummy key>");
2505}
2506
2507static PyTypeObject _PySetDummy_Type = {
2508 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2509 "<dummy key> type",
2510 0,
2511 0,
2512 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2513 0, /*tp_print*/
2514 0, /*tp_getattr*/
2515 0, /*tp_setattr*/
2516 0, /*tp_reserved*/
2517 dummy_repr, /*tp_repr*/
2518 0, /*tp_as_number*/
2519 0, /*tp_as_sequence*/
2520 0, /*tp_as_mapping*/
2521 0, /*tp_hash */
2522 0, /*tp_call */
2523 0, /*tp_str */
2524 0, /*tp_getattro */
2525 0, /*tp_setattro */
2526 0, /*tp_as_buffer */
2527 Py_TPFLAGS_DEFAULT, /*tp_flags */
2528};
2529
2530static PyObject _dummy_struct = {
2531 _PyObject_EXTRA_INIT
2532 2, &_PySetDummy_Type
2533};
2534