blob: 017fcd88db3ad75d91eacae280bc176d8a1ce9f6 [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 Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Antoine Pitrou9d952542013-08-24 21:07:07 +020039/* Exported for the gdb plugin's benefit. */
40PyObject *_PySet_Dummy = dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000042
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070043/* ======================================================================== */
44/* ======= Begin logic for probing the hash table ========================= */
45
Raymond Hettinger742d8712013-09-08 00:25:57 -070046/* This should be >= PySet_MINSIZE - 1 */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070047#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070048#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070049#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070050
51/* This must be >= 1 */
52#define PERTURB_SHIFT 5
53
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000054static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020055set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070058 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020059 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070060 size_t perturb = hash;
61 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070062 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020063 int cmp;
Raymond Hettinger8408dc52013-09-15 14:57:15 -070064 size_t j;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000065
Raymond Hettingera35adf52013-09-02 03:23:21 -070066 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070067 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070069
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070070 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070072 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070073 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070074 PyObject *startkey = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 Py_INCREF(startkey);
76 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
77 Py_DECREF(startkey);
78 if (cmp < 0)
79 return NULL;
Raymond Hettingerafe89092013-08-28 20:59:31 -070080 if (table != so->table || entry->key != startkey)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 return set_lookkey(so, key, hash);
Raymond Hettingerafe89092013-08-28 20:59:31 -070082 if (cmp > 0)
83 return entry;
84 }
85 if (entry->key == dummy && freeslot == NULL)
86 freeslot = entry;
87
Raymond Hettingerc70a2b72013-09-21 14:02:55 -070088 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
89 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -070090 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 Hettinger8408dc52013-09-15 14:57:15 -0700115 goto found_null;
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 Hettingerafe89092013-08-28 20:59:31 -0700132 size_t perturb = hash;
133 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700134 size_t i = (size_t)hash;
135 size_t j;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 /* Make sure this function doesn't have to handle non-unicode keys,
138 including subclasses of str; e.g., one reason to subclass
139 strings is to override __eq__, and for speed we don't cater to
140 that here. */
141 if (!PyUnicode_CheckExact(key)) {
142 so->lookup = set_lookkey;
143 return set_lookkey(so, key, hash);
144 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700145
Raymond Hettingera35adf52013-09-02 03:23:21 -0700146 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700147 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000149
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700150 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700151 if (entry->key == key
152 || (entry->hash == hash
Raymond Hettinger742d8712013-09-08 00:25:57 -0700153 && entry->key != dummy
154 && unicode_eq(entry->key, key)))
Raymond Hettingerafe89092013-08-28 20:59:31 -0700155 return entry;
156 if (entry->key == dummy && freeslot == NULL)
157 freeslot = entry;
158
Raymond Hettingerc70a2b72013-09-21 14:02:55 -0700159 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
160 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -0700161 if (entry->key == NULL)
162 goto found_null;
163 if (entry->key == key
164 || (entry->hash == hash
165 && entry->key != dummy
166 && unicode_eq(entry->key, key)))
167 return entry;
168 if (entry->key == dummy && freeslot == NULL)
169 freeslot = entry;
170 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700171
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700172 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700173 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700174
Raymond Hettingera35adf52013-09-02 03:23:21 -0700175 entry = &table[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700177 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700179 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700180 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000181}
182
183/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000184Internal routine used by set_table_resize() to insert an item which is
185known to be absent from the set. This routine also assumes that
186the set contains no deleted entries. Besides the performance benefit,
187using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
188Note that no refcounts are changed by this routine; if needed, the caller
189is responsible for incref'ing `key`.
190*/
191static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200192set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200195 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700196 size_t perturb = hash;
197 size_t mask = (size_t)so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700198 size_t i = (size_t)hash;
199 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000200
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700201 while (1) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700202 entry = &table[i & mask];
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700203 if (entry->key == NULL)
Raymond Hettingera35adf52013-09-02 03:23:21 -0700204 goto found_null;
205 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
206 entry = &table[(i + j) & mask];
207 if (entry->key == NULL)
208 goto found_null;
209 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700210 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700211 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700213 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 so->fill++;
215 entry->key = key;
216 entry->hash = hash;
217 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000218}
219
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700220/* ======== End logic for probing the hash table ========================== */
221/* ======================================================================== */
222
223
224/*
225Internal routine to insert a new key into the table.
226Used by the public insert routine.
227Eats a reference to key.
228*/
229static int
230set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
231{
232 setentry *entry;
233
234 assert(so->lookup != NULL);
235 entry = so->lookup(so, key, hash);
236 if (entry == NULL)
237 return -1;
238 if (entry->key == NULL) {
239 /* UNUSED */
240 so->fill++;
241 entry->key = key;
242 entry->hash = hash;
243 so->used++;
244 } else if (entry->key == dummy) {
245 /* DUMMY */
246 entry->key = key;
247 entry->hash = hash;
248 so->used++;
249 } else {
250 /* ACTIVE */
251 Py_DECREF(key);
252 }
253 return 0;
254}
255
Thomas Wouters89f507f2006-12-13 04:49:30 +0000256/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000257Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000258keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000259actually be smaller than the old one.
260*/
261static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_ssize_t newsize;
265 setentry *oldtable, *newtable, *entry;
266 Py_ssize_t i;
267 int is_oldtable_malloced;
268 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* Find the smallest table size > minused. */
273 for (newsize = PySet_MINSIZE;
274 newsize <= minused && newsize > 0;
275 newsize <<= 1)
276 ;
277 if (newsize <= 0) {
278 PyErr_NoMemory();
279 return -1;
280 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 /* Get space for a new table. */
283 oldtable = so->table;
284 assert(oldtable != NULL);
285 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 if (newsize == PySet_MINSIZE) {
288 /* A large table is shrinking, or we can't get any smaller. */
289 newtable = so->smalltable;
290 if (newtable == oldtable) {
291 if (so->fill == so->used) {
292 /* No dummies, so no point doing anything. */
293 return 0;
294 }
295 /* We're not going to resize it, but rebuild the
296 table anyway to purge old dummy entries.
297 Subtle: This is *necessary* if fill==size,
298 as set_lookkey needs at least one virgin slot to
299 terminate failing searches. If fill < size, it's
300 merely desirable, as dummies slow searches. */
301 assert(so->fill > so->used);
302 memcpy(small_copy, oldtable, sizeof(small_copy));
303 oldtable = small_copy;
304 }
305 }
306 else {
307 newtable = PyMem_NEW(setentry, newsize);
308 if (newtable == NULL) {
309 PyErr_NoMemory();
310 return -1;
311 }
312 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 /* Make the set empty, using the new table. */
315 assert(newtable != oldtable);
316 so->table = newtable;
317 so->mask = newsize - 1;
318 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700319 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 /* Copy the data over; this is refcount-neutral for active entries;
324 dummy entries aren't copied over, of course */
325 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500326 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000328 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 }
330 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 if (is_oldtable_malloced)
333 PyMem_DEL(oldtable);
334 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000335}
336
Raymond Hettingerc991db22005-08-11 07:58:45 +0000337/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
338
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000339static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200340set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000341{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200342 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000343 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100344 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 assert(so->fill <= so->mask); /* at least one empty slot */
347 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000348 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100349 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000350 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 return -1;
352 }
353 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
354 return 0;
355 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356}
357
358static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200359set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200361 Py_hash_t hash;
362 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200365 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 hash = PyObject_Hash(key);
367 if (hash == -1)
368 return -1;
369 }
370 assert(so->fill <= so->mask); /* at least one empty slot */
371 n_used = so->used;
372 Py_INCREF(key);
373 if (set_insert_key(so, key, hash) == -1) {
374 Py_DECREF(key);
375 return -1;
376 }
377 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
378 return 0;
379 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380}
381
382#define DISCARD_NOTFOUND 0
383#define DISCARD_FOUND 1
384
385static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000386set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200387{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000389
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000390 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (entry == NULL)
392 return -1;
393 if (entry->key == NULL || entry->key == dummy)
394 return DISCARD_NOTFOUND;
395 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 entry->key = dummy;
397 so->used--;
398 Py_DECREF(old_key);
399 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000400}
401
402static int
403set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000404{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200405 Py_hash_t hash;
406 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200412 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 hash = PyObject_Hash(key);
414 if (hash == -1)
415 return -1;
416 }
417 entry = (so->lookup)(so, key, hash);
418 if (entry == NULL)
419 return -1;
420 if (entry->key == NULL || entry->key == dummy)
421 return DISCARD_NOTFOUND;
422 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 entry->key = dummy;
424 so->used--;
425 Py_DECREF(old_key);
426 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000427}
428
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700429static void
430set_empty_to_minsize(PySetObject *so)
431{
432 memset(so->smalltable, 0, sizeof(so->smalltable));
433 so->fill = 0;
434 so->used = 0;
435 so->mask = PySet_MINSIZE - 1;
436 so->table = so->smalltable;
437 so->hash = -1;
438}
439
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000440static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000441set_clear_internal(PySetObject *so)
442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 setentry *entry, *table;
444 int table_is_malloced;
445 Py_ssize_t fill;
446 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000447
Raymond Hettinger583cd032013-09-07 22:06:35 -0700448#ifdef Py_DEBUG
449 Py_ssize_t i = 0;
450 Py_ssize_t n = so->mask + 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451#endif
452
Raymond Hettinger583cd032013-09-07 22:06:35 -0700453 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 table = so->table;
455 assert(table != NULL);
456 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 /* This is delicate. During the process of clearing the set,
459 * decrefs can cause the set to mutate. To avoid fatal confusion
460 * (voice of experience), we have to make the set empty before
461 * clearing the slots, and never refer to anything via so->ref while
462 * clearing.
463 */
464 fill = so->fill;
465 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700466 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 else if (fill > 0) {
469 /* It's a small table with something that needs to be cleared.
470 * Afraid the only safe way is to copy the set entries into
471 * another small table first.
472 */
473 memcpy(small_copy, table, sizeof(small_copy));
474 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700475 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 }
477 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 /* Now we can finally clear things. If C had refcounts, we could
480 * assert that the refcount on table is 1 now, i.e. that this function
481 * has unique access to it, so decref side-effects can't alter it.
482 */
483 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 assert(i < n);
486 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (entry->key) {
489 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700490 if (entry->key != dummy)
491 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 else
495 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (table_is_malloced)
500 PyMem_DEL(table);
501 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502}
503
504/*
505 * Iterate over a set table. Use like so:
506 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000507 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000508 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000509 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000510 * while (set_next(yourset, &pos, &entry)) {
511 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512 * }
513 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000514 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516 */
517static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000518set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 Py_ssize_t i;
521 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200522 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 assert (PyAnySet_Check(so));
525 i = *pos_ptr;
526 assert(i >= 0);
527 table = so->table;
528 mask = so->mask;
529 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
530 i++;
531 *pos_ptr = i+1;
532 if (i > mask)
533 return 0;
534 assert(table[i].key != NULL);
535 *entry_ptr = &table[i];
536 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000537}
538
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000539static void
540set_dealloc(PySetObject *so)
541{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200542 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 Py_ssize_t fill = so->fill;
544 PyObject_GC_UnTrack(so);
545 Py_TRASHCAN_SAFE_BEGIN(so)
546 if (so->weakreflist != NULL)
547 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 for (entry = so->table; fill > 0; entry++) {
550 if (entry->key) {
551 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700552 if (entry->key != dummy)
553 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 }
555 }
556 if (so->table != so->smalltable)
557 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700558 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000560}
561
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000562static PyObject *
563set_repr(PySetObject *so)
564{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200565 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (status != 0) {
569 if (status < 0)
570 return NULL;
571 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
572 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 /* shortcut for the empty set */
575 if (!so->used) {
576 Py_ReprLeave((PyObject*)so);
577 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
578 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 keys = PySequence_List((PyObject *)so);
581 if (keys == NULL)
582 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000583
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200584 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 listrepr = PyObject_Repr(keys);
586 Py_DECREF(keys);
587 if (listrepr == NULL)
588 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200589 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200591 if (tmp == NULL)
592 goto done;
593 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200594
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200595 if (Py_TYPE(so) != &PySet_Type)
596 result = PyUnicode_FromFormat("%s({%U})",
597 Py_TYPE(so)->tp_name,
598 listrepr);
599 else
600 result = PyUnicode_FromFormat("{%U}", listrepr);
601 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000602done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_ReprLeave((PyObject*)so);
604 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000605}
606
Martin v. Löwis18e16552006-02-15 17:27:45 +0000607static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000608set_len(PyObject *so)
609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000611}
612
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000613static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000614set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000617 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100618 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200619 Py_ssize_t i;
620 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 assert (PyAnySet_Check(so));
623 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 other = (PySetObject*)otherset;
626 if (other == so || other->used == 0)
627 /* a.update(a) or a.update({}); nothing to do */
628 return 0;
629 /* Do one big resize at the start, rather than
630 * incrementally resizing as we insert new keys. Expect
631 * that there will be no (or few) overlapping keys.
632 */
633 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
634 if (set_table_resize(so, (so->used + other->used)*2) != 0)
635 return -1;
636 }
637 for (i = 0; i <= other->mask; i++) {
638 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000639 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100640 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000641 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500642 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000643 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100644 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000645 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 return -1;
647 }
648 }
649 }
650 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000651}
652
653static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000654set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000655{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000656 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200660 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 hash = PyObject_Hash(key);
662 if (hash == -1)
663 return -1;
664 }
665 entry = (so->lookup)(so, key, hash);
666 if (entry == NULL)
667 return -1;
668 key = entry->key;
669 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670}
671
Raymond Hettingerc991db22005-08-11 07:58:45 +0000672static int
673set_contains_entry(PySetObject *so, setentry *entry)
674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 PyObject *key;
676 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000677
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000678 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 if (lu_entry == NULL)
680 return -1;
681 key = lu_entry->key;
682 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000683}
684
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000685static PyObject *
686set_pop(PySetObject *so)
687{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200688 Py_ssize_t i = 0;
689 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 assert (PyAnySet_Check(so));
693 if (so->used == 0) {
694 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
695 return NULL;
696 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 /* Set entry to "the first" unused or dummy set entry. We abuse
699 * the hash field of slot 0 to hold a search finger:
700 * If slot 0 has a value, use slot 0.
701 * Else slot 0 is being used to hold a search finger,
702 * and we use its hash value as the first index to look.
703 */
704 entry = &so->table[0];
705 if (entry->key == NULL || entry->key == dummy) {
706 i = entry->hash;
707 /* The hash field may be a real hash value, or it may be a
708 * legit search finger, or it may be a once-legit search
709 * finger that's out of bounds now because it wrapped around
710 * or the table shrunk -- simply make sure it's in bounds now.
711 */
712 if (i > so->mask || i < 1)
713 i = 1; /* skip slot 0 */
714 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
715 i++;
716 if (i > so->mask)
717 i = 1;
718 }
719 }
720 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 entry->key = dummy;
722 so->used--;
723 so->table[0].hash = i + 1; /* next place to start */
724 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000725}
726
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000727PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
728Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000729
730static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000731set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 Py_ssize_t pos = 0;
734 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 while (set_next(so, &pos, &entry))
737 Py_VISIT(entry->key);
738 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000739}
740
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000741static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000742frozenset_hash(PyObject *self)
743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800745 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 setentry *entry;
747 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 if (so->hash != -1)
750 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000751
Mark Dickinson57e683e2011-09-24 18:18:40 +0100752 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 while (set_next(so, &pos, &entry)) {
754 /* Work to increase the bit dispersion for closely spaced hash
755 values. The is important because some use cases have many
756 combinations of a small number of elements with nearby
757 hashes so that many distinct combinations collapse to only
758 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000759 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800760 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800762 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800764 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 so->hash = hash;
766 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000767}
768
Raymond Hettingera9d99362005-08-05 00:01:15 +0000769/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000770
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000771typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 PyObject_HEAD
773 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
774 Py_ssize_t si_used;
775 Py_ssize_t si_pos;
776 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777} setiterobject;
778
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000779static void
780setiter_dealloc(setiterobject *si)
781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 Py_XDECREF(si->si_set);
783 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000784}
785
786static int
787setiter_traverse(setiterobject *si, visitproc visit, void *arg)
788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 Py_VISIT(si->si_set);
790 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791}
792
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000793static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794setiter_len(setiterobject *si)
795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 Py_ssize_t len = 0;
797 if (si->si_set != NULL && si->si_used == si->si_set->used)
798 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000799 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800}
801
Armin Rigof5b3e362006-02-11 21:32:43 +0000802PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000803
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000804static PyObject *setiter_iternext(setiterobject *si);
805
806static PyObject *
807setiter_reduce(setiterobject *si)
808{
809 PyObject *list;
810 setiterobject tmp;
811
812 list = PyList_New(0);
813 if (!list)
814 return NULL;
815
Ezio Melotti0e1af282012-09-28 16:43:40 +0300816 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000817 tmp = *si;
818 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300819
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000820 /* iterate the temporary into a list */
821 for(;;) {
822 PyObject *element = setiter_iternext(&tmp);
823 if (element) {
824 if (PyList_Append(list, element)) {
825 Py_DECREF(element);
826 Py_DECREF(list);
827 Py_XDECREF(tmp.si_set);
828 return NULL;
829 }
830 Py_DECREF(element);
831 } else
832 break;
833 }
834 Py_XDECREF(tmp.si_set);
835 /* check for error */
836 if (tmp.si_set != NULL) {
837 /* we have an error */
838 Py_DECREF(list);
839 return NULL;
840 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200841 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000842}
843
844PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
845
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000846static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000848 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850};
851
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000852static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200855 Py_ssize_t i, mask;
856 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (so == NULL)
860 return NULL;
861 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 if (si->si_used != so->used) {
864 PyErr_SetString(PyExc_RuntimeError,
865 "Set changed size during iteration");
866 si->si_used = -1; /* Make this state sticky */
867 return NULL;
868 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 i = si->si_pos;
871 assert(i>=0);
872 entry = so->table;
873 mask = so->mask;
874 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
875 i++;
876 si->si_pos = i+1;
877 if (i > mask)
878 goto fail;
879 si->len--;
880 key = entry[i].key;
881 Py_INCREF(key);
882 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000883
884fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 Py_DECREF(so);
886 si->si_set = NULL;
887 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000888}
889
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000890PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 PyVarObject_HEAD_INIT(&PyType_Type, 0)
892 "set_iterator", /* tp_name */
893 sizeof(setiterobject), /* tp_basicsize */
894 0, /* tp_itemsize */
895 /* methods */
896 (destructor)setiter_dealloc, /* tp_dealloc */
897 0, /* tp_print */
898 0, /* tp_getattr */
899 0, /* tp_setattr */
900 0, /* tp_reserved */
901 0, /* tp_repr */
902 0, /* tp_as_number */
903 0, /* tp_as_sequence */
904 0, /* tp_as_mapping */
905 0, /* tp_hash */
906 0, /* tp_call */
907 0, /* tp_str */
908 PyObject_GenericGetAttr, /* tp_getattro */
909 0, /* tp_setattro */
910 0, /* tp_as_buffer */
911 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
912 0, /* tp_doc */
913 (traverseproc)setiter_traverse, /* tp_traverse */
914 0, /* tp_clear */
915 0, /* tp_richcompare */
916 0, /* tp_weaklistoffset */
917 PyObject_SelfIter, /* tp_iter */
918 (iternextfunc)setiter_iternext, /* tp_iternext */
919 setiter_methods, /* tp_methods */
920 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000921};
922
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923static PyObject *
924set_iter(PySetObject *so)
925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
927 if (si == NULL)
928 return NULL;
929 Py_INCREF(so);
930 si->si_set = so;
931 si->si_used = so->used;
932 si->si_pos = 0;
933 si->len = so->used;
934 _PyObject_GC_TRACK(si);
935 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000936}
937
Raymond Hettingerd7946662005-08-01 21:39:29 +0000938static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000939set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 if (PyAnySet_Check(other))
944 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 if (PyDict_CheckExact(other)) {
947 PyObject *value;
948 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000949 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 /* Do one big resize at the start, rather than
953 * incrementally resizing as we insert new keys. Expect
954 * that there will be no (or few) overlapping keys.
955 */
956 if (dictsize == -1)
957 return -1;
958 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
959 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
960 return -1;
961 }
962 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
963 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 an_entry.hash = hash;
966 an_entry.key = key;
967 if (set_add_entry(so, &an_entry) == -1)
968 return -1;
969 }
970 return 0;
971 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 it = PyObject_GetIter(other);
974 if (it == NULL)
975 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 while ((key = PyIter_Next(it)) != NULL) {
978 if (set_add_key(so, key) == -1) {
979 Py_DECREF(it);
980 Py_DECREF(key);
981 return -1;
982 }
983 Py_DECREF(key);
984 }
985 Py_DECREF(it);
986 if (PyErr_Occurred())
987 return -1;
988 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000989}
990
991static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000992set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
997 PyObject *other = PyTuple_GET_ITEM(args, i);
998 if (set_update_internal(so, other) == -1)
999 return NULL;
1000 }
1001 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001002}
1003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001005"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001006
1007static PyObject *
1008make_new_set(PyTypeObject *type, PyObject *iterable)
1009{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001010 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001013 so = (PySetObject *)type->tp_alloc(type, 0);
1014 if (so == NULL)
1015 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001016
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001017 so->fill = 0;
1018 so->used = 0;
1019 so->mask = PySet_MINSIZE - 1;
1020 so->table = so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 so->lookup = set_lookkey_unicode;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001022 so->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 if (iterable != NULL) {
1026 if (set_update_internal(so, iterable) == -1) {
1027 Py_DECREF(so);
1028 return NULL;
1029 }
1030 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001033}
1034
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001035static PyObject *
1036make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1039 if (PyType_IsSubtype(type, &PySet_Type))
1040 type = &PySet_Type;
1041 else
1042 type = &PyFrozenSet_Type;
1043 }
1044 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001045}
1046
Raymond Hettingerd7946662005-08-01 21:39:29 +00001047/* The empty frozenset is a singleton */
1048static PyObject *emptyfrozenset = NULL;
1049
Raymond Hettingera690a992003-11-16 16:17:49 +00001050static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001051frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1056 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1059 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 if (type != &PyFrozenSet_Type)
1062 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (iterable != NULL) {
1065 /* frozenset(f) is idempotent */
1066 if (PyFrozenSet_CheckExact(iterable)) {
1067 Py_INCREF(iterable);
1068 return iterable;
1069 }
1070 result = make_new_set(type, iterable);
1071 if (result == NULL || PySet_GET_SIZE(result))
1072 return result;
1073 Py_DECREF(result);
1074 }
1075 /* The empty frozenset is a singleton */
1076 if (emptyfrozenset == NULL)
1077 emptyfrozenset = make_new_set(type, NULL);
1078 Py_XINCREF(emptyfrozenset);
1079 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001080}
1081
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001082int
1083PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001084{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001085 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001086}
1087
1088void
1089PySet_Fini(void)
1090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001092}
1093
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001094static PyObject *
1095set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1098 return NULL;
1099
1100 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001101}
1102
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001103/* set_swap_bodies() switches the contents of any two sets by moving their
1104 internal data pointers and, if needed, copying the internal smalltables.
1105 Semantically equivalent to:
1106
1107 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1108
1109 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001111 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001113 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001114*/
1115
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001116static void
1117set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 Py_ssize_t t;
1120 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001121 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001123 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 t = a->fill; a->fill = b->fill; b->fill = t;
1126 t = a->used; a->used = b->used; b->used = t;
1127 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 u = a->table;
1130 if (a->table == a->smalltable)
1131 u = b->smalltable;
1132 a->table = b->table;
1133 if (b->table == b->smalltable)
1134 a->table = a->smalltable;
1135 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (a->table == a->smalltable || b->table == b->smalltable) {
1140 memcpy(tab, a->smalltable, sizeof(tab));
1141 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1142 memcpy(b->smalltable, tab, sizeof(tab));
1143 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1146 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1147 h = a->hash; a->hash = b->hash; b->hash = h;
1148 } else {
1149 a->hash = -1;
1150 b->hash = -1;
1151 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001152}
1153
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001154static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001155set_copy(PySetObject *so)
1156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001158}
1159
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001160static PyObject *
1161frozenset_copy(PySetObject *so)
1162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (PyFrozenSet_CheckExact(so)) {
1164 Py_INCREF(so);
1165 return (PyObject *)so;
1166 }
1167 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001168}
1169
Raymond Hettingera690a992003-11-16 16:17:49 +00001170PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1171
1172static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001173set_clear(PySetObject *so)
1174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 set_clear_internal(so);
1176 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001177}
1178
1179PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1180
1181static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001182set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 PySetObject *result;
1185 PyObject *other;
1186 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 result = (PySetObject *)set_copy(so);
1189 if (result == NULL)
1190 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1193 other = PyTuple_GET_ITEM(args, i);
1194 if ((PyObject *)so == other)
1195 continue;
1196 if (set_update_internal(result, other) == -1) {
1197 Py_DECREF(result);
1198 return NULL;
1199 }
1200 }
1201 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001202}
1203
1204PyDoc_STRVAR(union_doc,
1205 "Return the union of sets as a new set.\n\
1206\n\
1207(i.e. all elements that are in either set.)");
1208
1209static PyObject *
1210set_or(PySetObject *so, PyObject *other)
1211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001213
Brian Curtindfc80e32011-08-10 20:28:54 -05001214 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1215 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 result = (PySetObject *)set_copy(so);
1218 if (result == NULL)
1219 return NULL;
1220 if ((PyObject *)so == other)
1221 return (PyObject *)result;
1222 if (set_update_internal(result, other) == -1) {
1223 Py_DECREF(result);
1224 return NULL;
1225 }
1226 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001227}
1228
Raymond Hettingera690a992003-11-16 16:17:49 +00001229static PyObject *
1230set_ior(PySetObject *so, PyObject *other)
1231{
Brian Curtindfc80e32011-08-10 20:28:54 -05001232 if (!PyAnySet_Check(other))
1233 Py_RETURN_NOTIMPLEMENTED;
1234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 if (set_update_internal(so, other) == -1)
1236 return NULL;
1237 Py_INCREF(so);
1238 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001239}
1240
1241static PyObject *
1242set_intersection(PySetObject *so, PyObject *other)
1243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 PySetObject *result;
1245 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if ((PyObject *)so == other)
1248 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1251 if (result == NULL)
1252 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 if (PyAnySet_Check(other)) {
1255 Py_ssize_t pos = 0;
1256 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1259 tmp = (PyObject *)so;
1260 so = (PySetObject *)other;
1261 other = tmp;
1262 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 while (set_next((PySetObject *)other, &pos, &entry)) {
1265 int rv = set_contains_entry(so, entry);
1266 if (rv == -1) {
1267 Py_DECREF(result);
1268 return NULL;
1269 }
1270 if (rv) {
1271 if (set_add_entry(result, entry) == -1) {
1272 Py_DECREF(result);
1273 return NULL;
1274 }
1275 }
1276 }
1277 return (PyObject *)result;
1278 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 it = PyObject_GetIter(other);
1281 if (it == NULL) {
1282 Py_DECREF(result);
1283 return NULL;
1284 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 while ((key = PyIter_Next(it)) != NULL) {
1287 int rv;
1288 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001289 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (hash == -1) {
1292 Py_DECREF(it);
1293 Py_DECREF(result);
1294 Py_DECREF(key);
1295 return NULL;
1296 }
1297 entry.hash = hash;
1298 entry.key = key;
1299 rv = set_contains_entry(so, &entry);
1300 if (rv == -1) {
1301 Py_DECREF(it);
1302 Py_DECREF(result);
1303 Py_DECREF(key);
1304 return NULL;
1305 }
1306 if (rv) {
1307 if (set_add_entry(result, &entry) == -1) {
1308 Py_DECREF(it);
1309 Py_DECREF(result);
1310 Py_DECREF(key);
1311 return NULL;
1312 }
1313 }
1314 Py_DECREF(key);
1315 }
1316 Py_DECREF(it);
1317 if (PyErr_Occurred()) {
1318 Py_DECREF(result);
1319 return NULL;
1320 }
1321 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001322}
1323
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001324static PyObject *
1325set_intersection_multi(PySetObject *so, PyObject *args)
1326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 Py_ssize_t i;
1328 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 if (PyTuple_GET_SIZE(args) == 0)
1331 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 Py_INCREF(so);
1334 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1335 PyObject *other = PyTuple_GET_ITEM(args, i);
1336 PyObject *newresult = set_intersection((PySetObject *)result, other);
1337 if (newresult == NULL) {
1338 Py_DECREF(result);
1339 return NULL;
1340 }
1341 Py_DECREF(result);
1342 result = newresult;
1343 }
1344 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001345}
1346
Raymond Hettingera690a992003-11-16 16:17:49 +00001347PyDoc_STRVAR(intersection_doc,
1348"Return the intersection of two sets as a new set.\n\
1349\n\
1350(i.e. all elements that are in both sets.)");
1351
1352static PyObject *
1353set_intersection_update(PySetObject *so, PyObject *other)
1354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 tmp = set_intersection(so, other);
1358 if (tmp == NULL)
1359 return NULL;
1360 set_swap_bodies(so, (PySetObject *)tmp);
1361 Py_DECREF(tmp);
1362 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001363}
1364
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001365static PyObject *
1366set_intersection_update_multi(PySetObject *so, PyObject *args)
1367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 tmp = set_intersection_multi(so, args);
1371 if (tmp == NULL)
1372 return NULL;
1373 set_swap_bodies(so, (PySetObject *)tmp);
1374 Py_DECREF(tmp);
1375 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001376}
1377
Raymond Hettingera690a992003-11-16 16:17:49 +00001378PyDoc_STRVAR(intersection_update_doc,
1379"Update a set with the intersection of itself and another.");
1380
1381static PyObject *
1382set_and(PySetObject *so, PyObject *other)
1383{
Brian Curtindfc80e32011-08-10 20:28:54 -05001384 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1385 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001387}
1388
1389static PyObject *
1390set_iand(PySetObject *so, PyObject *other)
1391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001393
Brian Curtindfc80e32011-08-10 20:28:54 -05001394 if (!PyAnySet_Check(other))
1395 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 result = set_intersection_update(so, other);
1397 if (result == NULL)
1398 return NULL;
1399 Py_DECREF(result);
1400 Py_INCREF(so);
1401 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001402}
1403
Guido van Rossum58da9312007-11-10 23:39:45 +00001404static PyObject *
1405set_isdisjoint(PySetObject *so, PyObject *other)
1406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 if ((PyObject *)so == other) {
1410 if (PySet_GET_SIZE(so) == 0)
1411 Py_RETURN_TRUE;
1412 else
1413 Py_RETURN_FALSE;
1414 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if (PyAnySet_CheckExact(other)) {
1417 Py_ssize_t pos = 0;
1418 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1421 tmp = (PyObject *)so;
1422 so = (PySetObject *)other;
1423 other = tmp;
1424 }
1425 while (set_next((PySetObject *)other, &pos, &entry)) {
1426 int rv = set_contains_entry(so, entry);
1427 if (rv == -1)
1428 return NULL;
1429 if (rv)
1430 Py_RETURN_FALSE;
1431 }
1432 Py_RETURN_TRUE;
1433 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 it = PyObject_GetIter(other);
1436 if (it == NULL)
1437 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 while ((key = PyIter_Next(it)) != NULL) {
1440 int rv;
1441 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001442 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 if (hash == -1) {
1445 Py_DECREF(key);
1446 Py_DECREF(it);
1447 return NULL;
1448 }
1449 entry.hash = hash;
1450 entry.key = key;
1451 rv = set_contains_entry(so, &entry);
1452 Py_DECREF(key);
1453 if (rv == -1) {
1454 Py_DECREF(it);
1455 return NULL;
1456 }
1457 if (rv) {
1458 Py_DECREF(it);
1459 Py_RETURN_FALSE;
1460 }
1461 }
1462 Py_DECREF(it);
1463 if (PyErr_Occurred())
1464 return NULL;
1465 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001466}
1467
1468PyDoc_STRVAR(isdisjoint_doc,
1469"Return True if two sets have a null intersection.");
1470
Neal Norwitz6576bd82005-11-13 18:41:28 +00001471static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001472set_difference_update_internal(PySetObject *so, PyObject *other)
1473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 if ((PyObject *)so == other)
1475 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (PyAnySet_Check(other)) {
1478 setentry *entry;
1479 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 while (set_next((PySetObject *)other, &pos, &entry))
1482 if (set_discard_entry(so, entry) == -1)
1483 return -1;
1484 } else {
1485 PyObject *key, *it;
1486 it = PyObject_GetIter(other);
1487 if (it == NULL)
1488 return -1;
1489
1490 while ((key = PyIter_Next(it)) != NULL) {
1491 if (set_discard_key(so, key) == -1) {
1492 Py_DECREF(it);
1493 Py_DECREF(key);
1494 return -1;
1495 }
1496 Py_DECREF(key);
1497 }
1498 Py_DECREF(it);
1499 if (PyErr_Occurred())
1500 return -1;
1501 }
1502 /* If more than 1/5 are dummies, then resize them away. */
1503 if ((so->fill - so->used) * 5 < so->mask)
1504 return 0;
1505 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001506}
1507
Raymond Hettingera690a992003-11-16 16:17:49 +00001508static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001509set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1514 PyObject *other = PyTuple_GET_ITEM(args, i);
1515 if (set_difference_update_internal(so, other) == -1)
1516 return NULL;
1517 }
1518 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001519}
1520
1521PyDoc_STRVAR(difference_update_doc,
1522"Remove all elements of another set from this set.");
1523
1524static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001525set_copy_and_difference(PySetObject *so, PyObject *other)
1526{
1527 PyObject *result;
1528
1529 result = set_copy(so);
1530 if (result == NULL)
1531 return NULL;
1532 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1533 return result;
1534 Py_DECREF(result);
1535 return NULL;
1536}
1537
1538static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001539set_difference(PySetObject *so, PyObject *other)
1540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 PyObject *result;
1542 setentry *entry;
1543 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001546 return set_copy_and_difference(so, other);
1547 }
1548
1549 /* If len(so) much more than len(other), it's more efficient to simply copy
1550 * so and then iterate other looking for common elements. */
1551 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1552 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 result = make_new_set_basetype(Py_TYPE(so), NULL);
1556 if (result == NULL)
1557 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 if (PyDict_CheckExact(other)) {
1560 while (set_next(so, &pos, &entry)) {
1561 setentry entrycopy;
1562 entrycopy.hash = entry->hash;
1563 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001564 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1566 Py_DECREF(result);
1567 return NULL;
1568 }
1569 }
1570 }
1571 return result;
1572 }
1573
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001574 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 while (set_next(so, &pos, &entry)) {
1576 int rv = set_contains_entry((PySetObject *)other, entry);
1577 if (rv == -1) {
1578 Py_DECREF(result);
1579 return NULL;
1580 }
1581 if (!rv) {
1582 if (set_add_entry((PySetObject *)result, entry) == -1) {
1583 Py_DECREF(result);
1584 return NULL;
1585 }
1586 }
1587 }
1588 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001589}
1590
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001591static PyObject *
1592set_difference_multi(PySetObject *so, PyObject *args)
1593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 Py_ssize_t i;
1595 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 if (PyTuple_GET_SIZE(args) == 0)
1598 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 other = PyTuple_GET_ITEM(args, 0);
1601 result = set_difference(so, other);
1602 if (result == NULL)
1603 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1606 other = PyTuple_GET_ITEM(args, i);
1607 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1608 Py_DECREF(result);
1609 return NULL;
1610 }
1611 }
1612 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001613}
1614
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001615PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001616"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001617\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001618(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001619static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001620set_sub(PySetObject *so, PyObject *other)
1621{
Brian Curtindfc80e32011-08-10 20:28:54 -05001622 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1623 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001625}
1626
1627static PyObject *
1628set_isub(PySetObject *so, PyObject *other)
1629{
Brian Curtindfc80e32011-08-10 20:28:54 -05001630 if (!PyAnySet_Check(other))
1631 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 if (set_difference_update_internal(so, other) == -1)
1633 return NULL;
1634 Py_INCREF(so);
1635 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001636}
1637
1638static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001639set_symmetric_difference_update(PySetObject *so, PyObject *other)
1640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 PySetObject *otherset;
1642 PyObject *key;
1643 Py_ssize_t pos = 0;
1644 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 if ((PyObject *)so == other)
1647 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (PyDict_CheckExact(other)) {
1650 PyObject *value;
1651 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001652 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1654 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001655
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001656 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 an_entry.hash = hash;
1658 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001661 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001662 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001665 if (rv == DISCARD_NOTFOUND) {
1666 if (set_add_entry(so, &an_entry) == -1) {
1667 Py_DECREF(key);
1668 return NULL;
1669 }
1670 }
Georg Brandl2d444492010-09-03 10:52:55 +00001671 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 }
1673 Py_RETURN_NONE;
1674 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 if (PyAnySet_Check(other)) {
1677 Py_INCREF(other);
1678 otherset = (PySetObject *)other;
1679 } else {
1680 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1681 if (otherset == NULL)
1682 return NULL;
1683 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 while (set_next(otherset, &pos, &entry)) {
1686 int rv = set_discard_entry(so, entry);
1687 if (rv == -1) {
1688 Py_DECREF(otherset);
1689 return NULL;
1690 }
1691 if (rv == DISCARD_NOTFOUND) {
1692 if (set_add_entry(so, entry) == -1) {
1693 Py_DECREF(otherset);
1694 return NULL;
1695 }
1696 }
1697 }
1698 Py_DECREF(otherset);
1699 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001700}
1701
1702PyDoc_STRVAR(symmetric_difference_update_doc,
1703"Update a set with the symmetric difference of itself and another.");
1704
1705static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001706set_symmetric_difference(PySetObject *so, PyObject *other)
1707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 PyObject *rv;
1709 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1712 if (otherset == NULL)
1713 return NULL;
1714 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1715 if (rv == NULL)
1716 return NULL;
1717 Py_DECREF(rv);
1718 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001719}
1720
1721PyDoc_STRVAR(symmetric_difference_doc,
1722"Return the symmetric difference of two sets as a new set.\n\
1723\n\
1724(i.e. all elements that are in exactly one of the sets.)");
1725
1726static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001727set_xor(PySetObject *so, PyObject *other)
1728{
Brian Curtindfc80e32011-08-10 20:28:54 -05001729 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1730 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001732}
1733
1734static PyObject *
1735set_ixor(PySetObject *so, PyObject *other)
1736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001738
Brian Curtindfc80e32011-08-10 20:28:54 -05001739 if (!PyAnySet_Check(other))
1740 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 result = set_symmetric_difference_update(so, other);
1742 if (result == NULL)
1743 return NULL;
1744 Py_DECREF(result);
1745 Py_INCREF(so);
1746 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001747}
1748
1749static PyObject *
1750set_issubset(PySetObject *so, PyObject *other)
1751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 setentry *entry;
1753 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 if (!PyAnySet_Check(other)) {
1756 PyObject *tmp, *result;
1757 tmp = make_new_set(&PySet_Type, other);
1758 if (tmp == NULL)
1759 return NULL;
1760 result = set_issubset(so, tmp);
1761 Py_DECREF(tmp);
1762 return result;
1763 }
1764 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1765 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 while (set_next(so, &pos, &entry)) {
1768 int rv = set_contains_entry((PySetObject *)other, entry);
1769 if (rv == -1)
1770 return NULL;
1771 if (!rv)
1772 Py_RETURN_FALSE;
1773 }
1774 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001775}
1776
1777PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1778
1779static PyObject *
1780set_issuperset(PySetObject *so, PyObject *other)
1781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 if (!PyAnySet_Check(other)) {
1785 tmp = make_new_set(&PySet_Type, other);
1786 if (tmp == NULL)
1787 return NULL;
1788 result = set_issuperset(so, tmp);
1789 Py_DECREF(tmp);
1790 return result;
1791 }
1792 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001793}
1794
1795PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1796
Raymond Hettingera690a992003-11-16 16:17:49 +00001797static PyObject *
1798set_richcompare(PySetObject *v, PyObject *w, int op)
1799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001801
Brian Curtindfc80e32011-08-10 20:28:54 -05001802 if(!PyAnySet_Check(w))
1803 Py_RETURN_NOTIMPLEMENTED;
1804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 switch (op) {
1806 case Py_EQ:
1807 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1808 Py_RETURN_FALSE;
1809 if (v->hash != -1 &&
1810 ((PySetObject *)w)->hash != -1 &&
1811 v->hash != ((PySetObject *)w)->hash)
1812 Py_RETURN_FALSE;
1813 return set_issubset(v, w);
1814 case Py_NE:
1815 r1 = set_richcompare(v, w, Py_EQ);
1816 if (r1 == NULL)
1817 return NULL;
1818 r2 = PyBool_FromLong(PyObject_Not(r1));
1819 Py_DECREF(r1);
1820 return r2;
1821 case Py_LE:
1822 return set_issubset(v, w);
1823 case Py_GE:
1824 return set_issuperset(v, w);
1825 case Py_LT:
1826 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1827 Py_RETURN_FALSE;
1828 return set_issubset(v, w);
1829 case Py_GT:
1830 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1831 Py_RETURN_FALSE;
1832 return set_issuperset(v, w);
1833 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001834 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001835}
1836
Raymond Hettingera690a992003-11-16 16:17:49 +00001837static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001838set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 if (set_add_key(so, key) == -1)
1841 return NULL;
1842 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001843}
1844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001846"Add an element to a set.\n\
1847\n\
1848This has no effect if the element is already present.");
1849
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001850static int
1851set_contains(PySetObject *so, PyObject *key)
1852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 PyObject *tmpkey;
1854 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 rv = set_contains_key(so, key);
1857 if (rv == -1) {
1858 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1859 return -1;
1860 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001861 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 if (tmpkey == NULL)
1863 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001864 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 Py_DECREF(tmpkey);
1866 }
1867 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001868}
1869
1870static PyObject *
1871set_direct_contains(PySetObject *so, PyObject *key)
1872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 result = set_contains(so, key);
1876 if (result == -1)
1877 return NULL;
1878 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001879}
1880
1881PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1882
Raymond Hettingera690a992003-11-16 16:17:49 +00001883static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001884set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 PyObject *tmpkey;
1887 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 rv = set_discard_key(so, key);
1890 if (rv == -1) {
1891 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1892 return NULL;
1893 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001894 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 if (tmpkey == NULL)
1896 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 Py_DECREF(tmpkey);
1899 if (rv == -1)
1900 return NULL;
1901 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001904 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 return NULL;
1906 }
1907 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001908}
1909
1910PyDoc_STRVAR(remove_doc,
1911"Remove an element from a set; it must be a member.\n\
1912\n\
1913If the element is not a member, raise a KeyError.");
1914
1915static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001916set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001917{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001918 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 rv = set_discard_key(so, key);
1922 if (rv == -1) {
1923 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1924 return NULL;
1925 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001926 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 if (tmpkey == NULL)
1928 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001929 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001931 if (rv == -1)
1932 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 }
1934 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001935}
1936
1937PyDoc_STRVAR(discard_doc,
1938"Remove an element from a set if it is a member.\n\
1939\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001941
1942static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001943set_reduce(PySetObject *so)
1944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001946 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 keys = PySequence_List((PyObject *)so);
1949 if (keys == NULL)
1950 goto done;
1951 args = PyTuple_Pack(1, keys);
1952 if (args == NULL)
1953 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001954 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (dict == NULL) {
1956 PyErr_Clear();
1957 dict = Py_None;
1958 Py_INCREF(dict);
1959 }
1960 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001961done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 Py_XDECREF(args);
1963 Py_XDECREF(keys);
1964 Py_XDECREF(dict);
1965 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001966}
1967
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001968static PyObject *
1969set_sizeof(PySetObject *so)
1970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 res = sizeof(PySetObject);
1974 if (so->table != so->smalltable)
1975 res = res + (so->mask + 1) * sizeof(setentry);
1976 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001977}
1978
1979PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001980static int
1981set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 if (!PyAnySet_Check(self))
1986 return -1;
1987 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1988 return -1;
1989 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1990 return -1;
1991 set_clear_internal(self);
1992 self->hash = -1;
1993 if (iterable == NULL)
1994 return 0;
1995 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001996}
1997
Raymond Hettingera690a992003-11-16 16:17:49 +00001998static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 set_len, /* sq_length */
2000 0, /* sq_concat */
2001 0, /* sq_repeat */
2002 0, /* sq_item */
2003 0, /* sq_slice */
2004 0, /* sq_ass_item */
2005 0, /* sq_ass_slice */
2006 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002007};
2008
2009/* set object ********************************************************/
2010
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002011#ifdef Py_DEBUG
2012static PyObject *test_c_api(PySetObject *so);
2013
2014PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2015All is well if assertions don't fail.");
2016#endif
2017
Raymond Hettingera690a992003-11-16 16:17:49 +00002018static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 {"add", (PyCFunction)set_add, METH_O,
2020 add_doc},
2021 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2022 clear_doc},
2023 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2024 contains_doc},
2025 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2026 copy_doc},
2027 {"discard", (PyCFunction)set_discard, METH_O,
2028 discard_doc},
2029 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2030 difference_doc},
2031 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2032 difference_update_doc},
2033 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2034 intersection_doc},
2035 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2036 intersection_update_doc},
2037 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2038 isdisjoint_doc},
2039 {"issubset", (PyCFunction)set_issubset, METH_O,
2040 issubset_doc},
2041 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2042 issuperset_doc},
2043 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2044 pop_doc},
2045 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2046 reduce_doc},
2047 {"remove", (PyCFunction)set_remove, METH_O,
2048 remove_doc},
2049 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2050 sizeof_doc},
2051 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2052 symmetric_difference_doc},
2053 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2054 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002055#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2057 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002058#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 {"union", (PyCFunction)set_union, METH_VARARGS,
2060 union_doc},
2061 {"update", (PyCFunction)set_update, METH_VARARGS,
2062 update_doc},
2063 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002064};
2065
2066static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 0, /*nb_add*/
2068 (binaryfunc)set_sub, /*nb_subtract*/
2069 0, /*nb_multiply*/
2070 0, /*nb_remainder*/
2071 0, /*nb_divmod*/
2072 0, /*nb_power*/
2073 0, /*nb_negative*/
2074 0, /*nb_positive*/
2075 0, /*nb_absolute*/
2076 0, /*nb_bool*/
2077 0, /*nb_invert*/
2078 0, /*nb_lshift*/
2079 0, /*nb_rshift*/
2080 (binaryfunc)set_and, /*nb_and*/
2081 (binaryfunc)set_xor, /*nb_xor*/
2082 (binaryfunc)set_or, /*nb_or*/
2083 0, /*nb_int*/
2084 0, /*nb_reserved*/
2085 0, /*nb_float*/
2086 0, /*nb_inplace_add*/
2087 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2088 0, /*nb_inplace_multiply*/
2089 0, /*nb_inplace_remainder*/
2090 0, /*nb_inplace_power*/
2091 0, /*nb_inplace_lshift*/
2092 0, /*nb_inplace_rshift*/
2093 (binaryfunc)set_iand, /*nb_inplace_and*/
2094 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2095 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002096};
2097
2098PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002099"set() -> new empty set object\n\
2100set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002101\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002102Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002103
2104PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2106 "set", /* tp_name */
2107 sizeof(PySetObject), /* tp_basicsize */
2108 0, /* tp_itemsize */
2109 /* methods */
2110 (destructor)set_dealloc, /* tp_dealloc */
2111 0, /* tp_print */
2112 0, /* tp_getattr */
2113 0, /* tp_setattr */
2114 0, /* tp_reserved */
2115 (reprfunc)set_repr, /* tp_repr */
2116 &set_as_number, /* tp_as_number */
2117 &set_as_sequence, /* tp_as_sequence */
2118 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002119 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 0, /* tp_call */
2121 0, /* tp_str */
2122 PyObject_GenericGetAttr, /* tp_getattro */
2123 0, /* tp_setattro */
2124 0, /* tp_as_buffer */
2125 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2126 Py_TPFLAGS_BASETYPE, /* tp_flags */
2127 set_doc, /* tp_doc */
2128 (traverseproc)set_traverse, /* tp_traverse */
2129 (inquiry)set_clear_internal, /* tp_clear */
2130 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002131 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2132 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 0, /* tp_iternext */
2134 set_methods, /* tp_methods */
2135 0, /* tp_members */
2136 0, /* tp_getset */
2137 0, /* tp_base */
2138 0, /* tp_dict */
2139 0, /* tp_descr_get */
2140 0, /* tp_descr_set */
2141 0, /* tp_dictoffset */
2142 (initproc)set_init, /* tp_init */
2143 PyType_GenericAlloc, /* tp_alloc */
2144 set_new, /* tp_new */
2145 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002146};
2147
2148/* frozenset object ********************************************************/
2149
2150
2151static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2153 contains_doc},
2154 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2155 copy_doc},
2156 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2157 difference_doc},
2158 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2159 intersection_doc},
2160 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2161 isdisjoint_doc},
2162 {"issubset", (PyCFunction)set_issubset, METH_O,
2163 issubset_doc},
2164 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2165 issuperset_doc},
2166 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2167 reduce_doc},
2168 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2169 sizeof_doc},
2170 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2171 symmetric_difference_doc},
2172 {"union", (PyCFunction)set_union, METH_VARARGS,
2173 union_doc},
2174 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002175};
2176
2177static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 0, /*nb_add*/
2179 (binaryfunc)set_sub, /*nb_subtract*/
2180 0, /*nb_multiply*/
2181 0, /*nb_remainder*/
2182 0, /*nb_divmod*/
2183 0, /*nb_power*/
2184 0, /*nb_negative*/
2185 0, /*nb_positive*/
2186 0, /*nb_absolute*/
2187 0, /*nb_bool*/
2188 0, /*nb_invert*/
2189 0, /*nb_lshift*/
2190 0, /*nb_rshift*/
2191 (binaryfunc)set_and, /*nb_and*/
2192 (binaryfunc)set_xor, /*nb_xor*/
2193 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002194};
2195
2196PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002197"frozenset() -> empty frozenset object\n\
2198frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002199\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002200Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002201
2202PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2204 "frozenset", /* tp_name */
2205 sizeof(PySetObject), /* tp_basicsize */
2206 0, /* tp_itemsize */
2207 /* methods */
2208 (destructor)set_dealloc, /* tp_dealloc */
2209 0, /* tp_print */
2210 0, /* tp_getattr */
2211 0, /* tp_setattr */
2212 0, /* tp_reserved */
2213 (reprfunc)set_repr, /* tp_repr */
2214 &frozenset_as_number, /* tp_as_number */
2215 &set_as_sequence, /* tp_as_sequence */
2216 0, /* tp_as_mapping */
2217 frozenset_hash, /* tp_hash */
2218 0, /* tp_call */
2219 0, /* tp_str */
2220 PyObject_GenericGetAttr, /* tp_getattro */
2221 0, /* tp_setattro */
2222 0, /* tp_as_buffer */
2223 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2224 Py_TPFLAGS_BASETYPE, /* tp_flags */
2225 frozenset_doc, /* tp_doc */
2226 (traverseproc)set_traverse, /* tp_traverse */
2227 (inquiry)set_clear_internal, /* tp_clear */
2228 (richcmpfunc)set_richcompare, /* tp_richcompare */
2229 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2230 (getiterfunc)set_iter, /* tp_iter */
2231 0, /* tp_iternext */
2232 frozenset_methods, /* tp_methods */
2233 0, /* tp_members */
2234 0, /* tp_getset */
2235 0, /* tp_base */
2236 0, /* tp_dict */
2237 0, /* tp_descr_get */
2238 0, /* tp_descr_set */
2239 0, /* tp_dictoffset */
2240 0, /* tp_init */
2241 PyType_GenericAlloc, /* tp_alloc */
2242 frozenset_new, /* tp_new */
2243 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002244};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002245
2246
2247/***** C API functions *************************************************/
2248
2249PyObject *
2250PySet_New(PyObject *iterable)
2251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002253}
2254
2255PyObject *
2256PyFrozenSet_New(PyObject *iterable)
2257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002259}
2260
Neal Norwitz8c49c822006-03-04 18:41:19 +00002261Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002262PySet_Size(PyObject *anyset)
2263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 if (!PyAnySet_Check(anyset)) {
2265 PyErr_BadInternalCall();
2266 return -1;
2267 }
2268 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002269}
2270
2271int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002272PySet_Clear(PyObject *set)
2273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 if (!PySet_Check(set)) {
2275 PyErr_BadInternalCall();
2276 return -1;
2277 }
2278 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002279}
2280
2281int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002282PySet_Contains(PyObject *anyset, PyObject *key)
2283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 if (!PyAnySet_Check(anyset)) {
2285 PyErr_BadInternalCall();
2286 return -1;
2287 }
2288 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002289}
2290
2291int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002292PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 if (!PySet_Check(set)) {
2295 PyErr_BadInternalCall();
2296 return -1;
2297 }
2298 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002299}
2300
2301int
Christian Heimesfd66e512008-01-29 12:18:50 +00002302PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 if (!PySet_Check(anyset) &&
2305 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2306 PyErr_BadInternalCall();
2307 return -1;
2308 }
2309 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002310}
2311
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002312int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002313_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!PyAnySet_Check(set)) {
2318 PyErr_BadInternalCall();
2319 return -1;
2320 }
2321 if (set_next((PySetObject *)set, pos, &entry) == 0)
2322 return 0;
2323 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002324 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002326}
2327
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002328PyObject *
2329PySet_Pop(PyObject *set)
2330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 if (!PySet_Check(set)) {
2332 PyErr_BadInternalCall();
2333 return NULL;
2334 }
2335 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002336}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002337
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002338int
2339_PySet_Update(PyObject *set, PyObject *iterable)
2340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 if (!PySet_Check(set)) {
2342 PyErr_BadInternalCall();
2343 return -1;
2344 }
2345 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002346}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002347
2348#ifdef Py_DEBUG
2349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002351 Returns True and original set is restored. */
2352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353#define assertRaises(call_return_value, exception) \
2354 do { \
2355 assert(call_return_value); \
2356 assert(PyErr_ExceptionMatches(exception)); \
2357 PyErr_Clear(); \
2358 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002359
2360static PyObject *
2361test_c_api(PySetObject *so)
2362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 Py_ssize_t count;
2364 char *s;
2365 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002366 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002368 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 /* Verify preconditions */
2372 assert(PyAnySet_Check(ob));
2373 assert(PyAnySet_CheckExact(ob));
2374 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 /* so.clear(); so |= set("abc"); */
2377 str = PyUnicode_FromString("abc");
2378 if (str == NULL)
2379 return NULL;
2380 set_clear_internal(so);
2381 if (set_update_internal(so, str) == -1) {
2382 Py_DECREF(str);
2383 return NULL;
2384 }
2385 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 /* Exercise type/size checks */
2388 assert(PySet_Size(ob) == 3);
2389 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* Raise TypeError for non-iterable constructor arguments */
2392 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2393 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 /* Raise TypeError for unhashable key */
2396 dup = PySet_New(ob);
2397 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2398 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2399 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* Exercise successful pop, contains, add, and discard */
2402 elem = PySet_Pop(ob);
2403 assert(PySet_Contains(ob, elem) == 0);
2404 assert(PySet_GET_SIZE(ob) == 2);
2405 assert(PySet_Add(ob, elem) == 0);
2406 assert(PySet_Contains(ob, elem) == 1);
2407 assert(PySet_GET_SIZE(ob) == 3);
2408 assert(PySet_Discard(ob, elem) == 1);
2409 assert(PySet_GET_SIZE(ob) == 2);
2410 assert(PySet_Discard(ob, elem) == 0);
2411 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Exercise clear */
2414 dup2 = PySet_New(dup);
2415 assert(PySet_Clear(dup2) == 0);
2416 assert(PySet_Size(dup2) == 0);
2417 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* Raise SystemError on clear or update of frozen set */
2420 f = PyFrozenSet_New(dup);
2421 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2422 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2423 assert(PySet_Add(f, elem) == 0);
2424 Py_INCREF(f);
2425 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2426 Py_DECREF(f);
2427 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* Exercise direct iteration */
2430 i = 0, count = 0;
2431 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2432 s = _PyUnicode_AsString(x);
2433 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2434 count++;
2435 }
2436 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 /* Exercise updates */
2439 dup2 = PySet_New(NULL);
2440 assert(_PySet_Update(dup2, dup) == 0);
2441 assert(PySet_Size(dup2) == 3);
2442 assert(_PySet_Update(dup2, dup) == 0);
2443 assert(PySet_Size(dup2) == 3);
2444 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 /* Raise SystemError when self argument is not a set or frozenset. */
2447 t = PyTuple_New(0);
2448 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2449 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2450 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 /* Raise SystemError when self argument is not a set. */
2453 f = PyFrozenSet_New(dup);
2454 assert(PySet_Size(f) == 3);
2455 assert(PyFrozenSet_CheckExact(f));
2456 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2457 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2458 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* Raise KeyError when popping from an empty set */
2461 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2462 Py_DECREF(ob);
2463 assert(PySet_GET_SIZE(ob) == 0);
2464 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 /* Restore the set from the copy using the PyNumber API */
2467 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2468 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 /* Verify constructors accept NULL arguments */
2471 f = PySet_New(NULL);
2472 assert(f != NULL);
2473 assert(PySet_GET_SIZE(f) == 0);
2474 Py_DECREF(f);
2475 f = PyFrozenSet_New(NULL);
2476 assert(f != NULL);
2477 assert(PyFrozenSet_CheckExact(f));
2478 assert(PySet_GET_SIZE(f) == 0);
2479 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 Py_DECREF(elem);
2482 Py_DECREF(dup);
2483 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002484}
2485
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002486#undef assertRaises
2487
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002488#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002489
2490/***** Dummy Struct *************************************************/
2491
2492static PyObject *
2493dummy_repr(PyObject *op)
2494{
2495 return PyUnicode_FromString("<dummy key>");
2496}
2497
2498static void
2499dummy_dealloc(PyObject* ignore)
2500{
2501 Py_FatalError("deallocating <dummy key>");
2502}
2503
2504static PyTypeObject _PySetDummy_Type = {
2505 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2506 "<dummy key> type",
2507 0,
2508 0,
2509 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2510 0, /*tp_print*/
2511 0, /*tp_getattr*/
2512 0, /*tp_setattr*/
2513 0, /*tp_reserved*/
2514 dummy_repr, /*tp_repr*/
2515 0, /*tp_as_number*/
2516 0, /*tp_as_sequence*/
2517 0, /*tp_as_mapping*/
2518 0, /*tp_hash */
2519 0, /*tp_call */
2520 0, /*tp_str */
2521 0, /*tp_getattro */
2522 0, /*tp_setattro */
2523 0, /*tp_as_buffer */
2524 Py_TPFLAGS_DEFAULT, /*tp_flags */
2525};
2526
2527static PyObject _dummy_struct = {
2528 _PyObject_EXTRA_INIT
2529 2, &_PySetDummy_Type
2530};
2531