blob: 362273ab49f26cbd51106339f3e1d353b7cdba7c [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettinger6c3c1cc2013-08-31 21:34:24 -07007 Copyright (c) 2003-2013 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
26 Unlike the dictionary implementation, the lookkey functions can return
27 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettingerc56e0e32013-09-02 16:32:27 -070034/* This must be >= 1 */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035#define PERTURB_SHIFT 5
Raymond Hettingerc56e0e32013-09-02 16:32:27 -070036
37/* This should be >= PySet_MINSIZE - 1 */
Raymond Hettingera35adf52013-09-02 03:23:21 -070038#define LINEAR_PROBES 9
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
40/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050041static PyObject _dummy_struct;
42
43#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000044
Antoine Pitrou9d952542013-08-24 21:07:07 +020045/* Exported for the gdb plugin's benefit. */
46PyObject *_PySet_Dummy = dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define INIT_NONZERO_SET_SLOTS(so) do { \
49 (so)->table = (so)->smalltable; \
50 (so)->mask = PySet_MINSIZE - 1; \
51 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000052 } while(0)
53
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054#define EMPTY_TO_MINSIZE(so) do { \
55 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
56 (so)->used = (so)->fill = 0; \
57 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000058 } while(0)
59
Raymond Hettingerbc841a12005-08-07 13:02:53 +000060/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000061#ifndef PySet_MAXFREELIST
62#define PySet_MAXFREELIST 80
63#endif
64static PySetObject *free_list[PySet_MAXFREELIST];
65static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000066
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070067/* ======================================================================== */
68/* ======= Begin logic for probing the hash table ========================= */
69
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020071set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070074 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020075 setentry *entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070076 setentry *limit;
Raymond Hettingerafe89092013-08-28 20:59:31 -070077 size_t perturb = hash;
78 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070079 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
80 size_t j;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020081 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082
Raymond Hettingera35adf52013-09-02 03:23:21 -070083 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070084 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070086
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070087 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070089 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070090 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070091 PyObject *startkey = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 Py_INCREF(startkey);
93 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
94 Py_DECREF(startkey);
95 if (cmp < 0)
96 return NULL;
Raymond Hettingerafe89092013-08-28 20:59:31 -070097 if (table != so->table || entry->key != startkey)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 return set_lookkey(so, key, hash);
Raymond Hettingerafe89092013-08-28 20:59:31 -070099 if (cmp > 0)
100 return entry;
101 }
102 if (entry->key == dummy && freeslot == NULL)
103 freeslot = entry;
104
Raymond Hettingera35adf52013-09-02 03:23:21 -0700105 limit = &table[mask];
106 for (j = 0 ; j < LINEAR_PROBES ; j++) {
107 entry = (entry == limit) ? &table[0] : entry + 1;
108 if (entry->key == NULL)
109 goto found_null;
110 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -0700111 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700112 if (entry->hash == hash && entry->key != dummy) {
113 PyObject *startkey = entry->key;
114 Py_INCREF(startkey);
115 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
116 Py_DECREF(startkey);
117 if (cmp < 0)
118 return NULL;
119 if (table != so->table || entry->key != startkey)
120 return set_lookkey(so, key, hash);
121 if (cmp > 0)
122 return entry;
123 }
124 if (entry->key == dummy && freeslot == NULL)
125 freeslot = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700127
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700128 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700129 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700130
Raymond Hettingera35adf52013-09-02 03:23:21 -0700131 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700132 if (entry->key == NULL)
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700133 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700135 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700136 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000137}
138
139/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000140 * Hacked up version of set_lookkey which can assume keys are always unicode;
141 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000142 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000143 */
144static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200145set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700148 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200149 setentry *entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700150 setentry *limit;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700151 size_t perturb = hash;
152 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700153 size_t i = (size_t)hash;
154 size_t j;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 /* Make sure this function doesn't have to handle non-unicode keys,
157 including subclasses of str; e.g., one reason to subclass
158 strings is to override __eq__, and for speed we don't cater to
159 that here. */
160 if (!PyUnicode_CheckExact(key)) {
161 so->lookup = set_lookkey;
162 return set_lookkey(so, key, hash);
163 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700164
Raymond Hettingera35adf52013-09-02 03:23:21 -0700165 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700166 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000168
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700169 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700170 if (entry->key == key
171 || (entry->hash == hash
172 && entry->key != dummy
173 && unicode_eq(entry->key, key)))
174 return entry;
175 if (entry->key == dummy && freeslot == NULL)
176 freeslot = entry;
177
Raymond Hettingera35adf52013-09-02 03:23:21 -0700178 limit = &table[mask];
179 for (j = 0 ; j < LINEAR_PROBES ; j++) {
180 entry = (entry == limit) ? &table[0] : entry + 1;
181 if (entry->key == NULL)
182 goto found_null;
183 if (entry->key == key
184 || (entry->hash == hash
185 && entry->key != dummy
186 && unicode_eq(entry->key, key)))
187 return entry;
188 if (entry->key == dummy && freeslot == NULL)
189 freeslot = entry;
190 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700191
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700192 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700193 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700194
Raymond Hettingera35adf52013-09-02 03:23:21 -0700195 entry = &table[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 if (entry->key == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -0700197 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700199 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700200 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000201}
202
203/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204Internal routine used by set_table_resize() to insert an item which is
205known to be absent from the set. This routine also assumes that
206the set contains no deleted entries. Besides the performance benefit,
207using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
208Note that no refcounts are changed by this routine; if needed, the caller
209is responsible for incref'ing `key`.
210*/
211static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200212set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200215 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700216 size_t perturb = hash;
217 size_t mask = (size_t)so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700218 size_t i = (size_t)hash;
219 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000220
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700221 while (1) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700222 entry = &table[i & mask];
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700223 if (entry->key == NULL)
Raymond Hettingera35adf52013-09-02 03:23:21 -0700224 goto found_null;
225 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
226 entry = &table[(i + j) & mask];
227 if (entry->key == NULL)
228 goto found_null;
229 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700230 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700231 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700233 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 so->fill++;
235 entry->key = key;
236 entry->hash = hash;
237 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000238}
239
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700240/* ======== End logic for probing the hash table ========================== */
241/* ======================================================================== */
242
243
244/*
245Internal routine to insert a new key into the table.
246Used by the public insert routine.
247Eats a reference to key.
248*/
249static int
250set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
251{
252 setentry *entry;
253
254 assert(so->lookup != NULL);
255 entry = so->lookup(so, key, hash);
256 if (entry == NULL)
257 return -1;
258 if (entry->key == NULL) {
259 /* UNUSED */
260 so->fill++;
261 entry->key = key;
262 entry->hash = hash;
263 so->used++;
264 } else if (entry->key == dummy) {
265 /* DUMMY */
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
269 } else {
270 /* ACTIVE */
271 Py_DECREF(key);
272 }
273 return 0;
274}
275
Thomas Wouters89f507f2006-12-13 04:49:30 +0000276/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000277Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000278keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000279actually be smaller than the old one.
280*/
281static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 Py_ssize_t newsize;
285 setentry *oldtable, *newtable, *entry;
286 Py_ssize_t i;
287 int is_oldtable_malloced;
288 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 /* Find the smallest table size > minused. */
293 for (newsize = PySet_MINSIZE;
294 newsize <= minused && newsize > 0;
295 newsize <<= 1)
296 ;
297 if (newsize <= 0) {
298 PyErr_NoMemory();
299 return -1;
300 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* Get space for a new table. */
303 oldtable = so->table;
304 assert(oldtable != NULL);
305 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (newsize == PySet_MINSIZE) {
308 /* A large table is shrinking, or we can't get any smaller. */
309 newtable = so->smalltable;
310 if (newtable == oldtable) {
311 if (so->fill == so->used) {
312 /* No dummies, so no point doing anything. */
313 return 0;
314 }
315 /* We're not going to resize it, but rebuild the
316 table anyway to purge old dummy entries.
317 Subtle: This is *necessary* if fill==size,
318 as set_lookkey needs at least one virgin slot to
319 terminate failing searches. If fill < size, it's
320 merely desirable, as dummies slow searches. */
321 assert(so->fill > so->used);
322 memcpy(small_copy, oldtable, sizeof(small_copy));
323 oldtable = small_copy;
324 }
325 }
326 else {
327 newtable = PyMem_NEW(setentry, newsize);
328 if (newtable == NULL) {
329 PyErr_NoMemory();
330 return -1;
331 }
332 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 /* Make the set empty, using the new table. */
335 assert(newtable != oldtable);
336 so->table = newtable;
337 so->mask = newsize - 1;
338 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700339 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 /* Copy the data over; this is refcount-neutral for active entries;
344 dummy entries aren't copied over, of course */
345 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500346 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000348 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 }
350 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (is_oldtable_malloced)
353 PyMem_DEL(oldtable);
354 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355}
356
Raymond Hettingerc991db22005-08-11 07:58:45 +0000357/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
358
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000359static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200360set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200362 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000363 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100364 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 assert(so->fill <= so->mask); /* at least one empty slot */
367 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000368 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100369 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000370 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 return -1;
372 }
373 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
374 return 0;
375 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000376}
377
378static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200379set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200381 Py_hash_t hash;
382 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200385 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 hash = PyObject_Hash(key);
387 if (hash == -1)
388 return -1;
389 }
390 assert(so->fill <= so->mask); /* at least one empty slot */
391 n_used = so->used;
392 Py_INCREF(key);
393 if (set_insert_key(so, key, hash) == -1) {
394 Py_DECREF(key);
395 return -1;
396 }
397 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
398 return 0;
399 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000400}
401
402#define DISCARD_NOTFOUND 0
403#define DISCARD_FOUND 1
404
405static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000406set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200407{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000409
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000410 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (entry == NULL)
412 return -1;
413 if (entry->key == NULL || entry->key == dummy)
414 return DISCARD_NOTFOUND;
415 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 entry->key = dummy;
417 so->used--;
418 Py_DECREF(old_key);
419 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000420}
421
422static int
423set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200425 Py_hash_t hash;
426 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200432 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 hash = PyObject_Hash(key);
434 if (hash == -1)
435 return -1;
436 }
437 entry = (so->lookup)(so, key, hash);
438 if (entry == NULL)
439 return -1;
440 if (entry->key == NULL || entry->key == dummy)
441 return DISCARD_NOTFOUND;
442 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 entry->key = dummy;
444 so->used--;
445 Py_DECREF(old_key);
446 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000447}
448
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000449static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450set_clear_internal(PySetObject *so)
451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 setentry *entry, *table;
453 int table_is_malloced;
454 Py_ssize_t fill;
455 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 Py_ssize_t i, n;
458 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 n = so->mask + 1;
461 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462#endif
463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 table = so->table;
465 assert(table != NULL);
466 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 /* This is delicate. During the process of clearing the set,
469 * decrefs can cause the set to mutate. To avoid fatal confusion
470 * (voice of experience), we have to make the set empty before
471 * clearing the slots, and never refer to anything via so->ref while
472 * clearing.
473 */
474 fill = so->fill;
475 if (table_is_malloced)
476 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 else if (fill > 0) {
479 /* It's a small table with something that needs to be cleared.
480 * Afraid the only safe way is to copy the set entries into
481 * another small table first.
482 */
483 memcpy(small_copy, table, sizeof(small_copy));
484 table = small_copy;
485 EMPTY_TO_MINSIZE(so);
486 }
487 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* Now we can finally clear things. If C had refcounts, we could
490 * assert that the refcount on table is 1 now, i.e. that this function
491 * has unique access to it, so decref side-effects can't alter it.
492 */
493 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 assert(i < n);
496 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000497#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (entry->key) {
499 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700500 if (entry->key != dummy)
501 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 else
505 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 if (table_is_malloced)
510 PyMem_DEL(table);
511 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512}
513
514/*
515 * Iterate over a set table. Use like so:
516 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000517 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000518 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000519 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000520 * while (set_next(yourset, &pos, &entry)) {
521 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522 * }
523 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000524 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000526 */
527static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000528set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 Py_ssize_t i;
531 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200532 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 assert (PyAnySet_Check(so));
535 i = *pos_ptr;
536 assert(i >= 0);
537 table = so->table;
538 mask = so->mask;
539 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
540 i++;
541 *pos_ptr = i+1;
542 if (i > mask)
543 return 0;
544 assert(table[i].key != NULL);
545 *entry_ptr = &table[i];
546 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000547}
548
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000549static void
550set_dealloc(PySetObject *so)
551{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200552 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 Py_ssize_t fill = so->fill;
554 PyObject_GC_UnTrack(so);
555 Py_TRASHCAN_SAFE_BEGIN(so)
556 if (so->weakreflist != NULL)
557 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 for (entry = so->table; fill > 0; entry++) {
560 if (entry->key) {
561 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700562 if (entry->key != dummy)
563 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 }
565 }
566 if (so->table != so->smalltable)
567 PyMem_DEL(so->table);
568 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
569 free_list[numfree++] = so;
570 else
571 Py_TYPE(so)->tp_free(so);
572 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000573}
574
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000575static PyObject *
576set_repr(PySetObject *so)
577{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200578 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (status != 0) {
582 if (status < 0)
583 return NULL;
584 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
585 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 /* shortcut for the empty set */
588 if (!so->used) {
589 Py_ReprLeave((PyObject*)so);
590 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
591 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 keys = PySequence_List((PyObject *)so);
594 if (keys == NULL)
595 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000596
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200597 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 listrepr = PyObject_Repr(keys);
599 Py_DECREF(keys);
600 if (listrepr == NULL)
601 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200602 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200604 if (tmp == NULL)
605 goto done;
606 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200607
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200608 if (Py_TYPE(so) != &PySet_Type)
609 result = PyUnicode_FromFormat("%s({%U})",
610 Py_TYPE(so)->tp_name,
611 listrepr);
612 else
613 result = PyUnicode_FromFormat("{%U}", listrepr);
614 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000615done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 Py_ReprLeave((PyObject*)so);
617 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000618}
619
Martin v. Löwis18e16552006-02-15 17:27:45 +0000620static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000621set_len(PyObject *so)
622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000624}
625
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000626static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000627set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000630 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100631 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200632 Py_ssize_t i;
633 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 assert (PyAnySet_Check(so));
636 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 other = (PySetObject*)otherset;
639 if (other == so || other->used == 0)
640 /* a.update(a) or a.update({}); nothing to do */
641 return 0;
642 /* Do one big resize at the start, rather than
643 * incrementally resizing as we insert new keys. Expect
644 * that there will be no (or few) overlapping keys.
645 */
646 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
647 if (set_table_resize(so, (so->used + other->used)*2) != 0)
648 return -1;
649 }
650 for (i = 0; i <= other->mask; i++) {
651 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000652 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100653 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000654 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500655 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000656 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100657 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000658 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 return -1;
660 }
661 }
662 }
663 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000664}
665
666static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000667set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000669 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200673 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 hash = PyObject_Hash(key);
675 if (hash == -1)
676 return -1;
677 }
678 entry = (so->lookup)(so, key, hash);
679 if (entry == NULL)
680 return -1;
681 key = entry->key;
682 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000683}
684
Raymond Hettingerc991db22005-08-11 07:58:45 +0000685static int
686set_contains_entry(PySetObject *so, setentry *entry)
687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyObject *key;
689 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000690
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000691 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (lu_entry == NULL)
693 return -1;
694 key = lu_entry->key;
695 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000696}
697
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000698static PyObject *
699set_pop(PySetObject *so)
700{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200701 Py_ssize_t i = 0;
702 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 assert (PyAnySet_Check(so));
706 if (so->used == 0) {
707 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
708 return NULL;
709 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 /* Set entry to "the first" unused or dummy set entry. We abuse
712 * the hash field of slot 0 to hold a search finger:
713 * If slot 0 has a value, use slot 0.
714 * Else slot 0 is being used to hold a search finger,
715 * and we use its hash value as the first index to look.
716 */
717 entry = &so->table[0];
718 if (entry->key == NULL || entry->key == dummy) {
719 i = entry->hash;
720 /* The hash field may be a real hash value, or it may be a
721 * legit search finger, or it may be a once-legit search
722 * finger that's out of bounds now because it wrapped around
723 * or the table shrunk -- simply make sure it's in bounds now.
724 */
725 if (i > so->mask || i < 1)
726 i = 1; /* skip slot 0 */
727 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
728 i++;
729 if (i > so->mask)
730 i = 1;
731 }
732 }
733 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 entry->key = dummy;
735 so->used--;
736 so->table[0].hash = i + 1; /* next place to start */
737 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000738}
739
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000740PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
741Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000742
743static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000744set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 Py_ssize_t pos = 0;
747 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 while (set_next(so, &pos, &entry))
750 Py_VISIT(entry->key);
751 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000752}
753
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000754static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000755frozenset_hash(PyObject *self)
756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800758 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 setentry *entry;
760 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 if (so->hash != -1)
763 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000764
Mark Dickinson57e683e2011-09-24 18:18:40 +0100765 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 while (set_next(so, &pos, &entry)) {
767 /* Work to increase the bit dispersion for closely spaced hash
768 values. The is important because some use cases have many
769 combinations of a small number of elements with nearby
770 hashes so that many distinct combinations collapse to only
771 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000772 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800773 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800775 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800777 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 so->hash = hash;
779 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000780}
781
Raymond Hettingera9d99362005-08-05 00:01:15 +0000782/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000783
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000784typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 PyObject_HEAD
786 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
787 Py_ssize_t si_used;
788 Py_ssize_t si_pos;
789 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790} setiterobject;
791
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792static void
793setiter_dealloc(setiterobject *si)
794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 Py_XDECREF(si->si_set);
796 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000797}
798
799static int
800setiter_traverse(setiterobject *si, visitproc visit, void *arg)
801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 Py_VISIT(si->si_set);
803 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804}
805
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000806static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807setiter_len(setiterobject *si)
808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 Py_ssize_t len = 0;
810 if (si->si_set != NULL && si->si_used == si->si_set->used)
811 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000812 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813}
814
Armin Rigof5b3e362006-02-11 21:32:43 +0000815PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000816
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000817static PyObject *setiter_iternext(setiterobject *si);
818
819static PyObject *
820setiter_reduce(setiterobject *si)
821{
822 PyObject *list;
823 setiterobject tmp;
824
825 list = PyList_New(0);
826 if (!list)
827 return NULL;
828
Ezio Melotti0e1af282012-09-28 16:43:40 +0300829 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000830 tmp = *si;
831 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300832
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000833 /* iterate the temporary into a list */
834 for(;;) {
835 PyObject *element = setiter_iternext(&tmp);
836 if (element) {
837 if (PyList_Append(list, element)) {
838 Py_DECREF(element);
839 Py_DECREF(list);
840 Py_XDECREF(tmp.si_set);
841 return NULL;
842 }
843 Py_DECREF(element);
844 } else
845 break;
846 }
847 Py_XDECREF(tmp.si_set);
848 /* check for error */
849 if (tmp.si_set != NULL) {
850 /* we have an error */
851 Py_DECREF(list);
852 return NULL;
853 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200854 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000855}
856
857PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
858
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000859static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000861 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000863};
864
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000865static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200868 Py_ssize_t i, mask;
869 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 if (so == NULL)
873 return NULL;
874 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 if (si->si_used != so->used) {
877 PyErr_SetString(PyExc_RuntimeError,
878 "Set changed size during iteration");
879 si->si_used = -1; /* Make this state sticky */
880 return NULL;
881 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 i = si->si_pos;
884 assert(i>=0);
885 entry = so->table;
886 mask = so->mask;
887 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
888 i++;
889 si->si_pos = i+1;
890 if (i > mask)
891 goto fail;
892 si->len--;
893 key = entry[i].key;
894 Py_INCREF(key);
895 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000896
897fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 Py_DECREF(so);
899 si->si_set = NULL;
900 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000901}
902
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000903PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 PyVarObject_HEAD_INIT(&PyType_Type, 0)
905 "set_iterator", /* tp_name */
906 sizeof(setiterobject), /* tp_basicsize */
907 0, /* tp_itemsize */
908 /* methods */
909 (destructor)setiter_dealloc, /* tp_dealloc */
910 0, /* tp_print */
911 0, /* tp_getattr */
912 0, /* tp_setattr */
913 0, /* tp_reserved */
914 0, /* tp_repr */
915 0, /* tp_as_number */
916 0, /* tp_as_sequence */
917 0, /* tp_as_mapping */
918 0, /* tp_hash */
919 0, /* tp_call */
920 0, /* tp_str */
921 PyObject_GenericGetAttr, /* tp_getattro */
922 0, /* tp_setattro */
923 0, /* tp_as_buffer */
924 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
925 0, /* tp_doc */
926 (traverseproc)setiter_traverse, /* tp_traverse */
927 0, /* tp_clear */
928 0, /* tp_richcompare */
929 0, /* tp_weaklistoffset */
930 PyObject_SelfIter, /* tp_iter */
931 (iternextfunc)setiter_iternext, /* tp_iternext */
932 setiter_methods, /* tp_methods */
933 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000934};
935
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000936static PyObject *
937set_iter(PySetObject *so)
938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
940 if (si == NULL)
941 return NULL;
942 Py_INCREF(so);
943 si->si_set = so;
944 si->si_used = so->used;
945 si->si_pos = 0;
946 si->len = so->used;
947 _PyObject_GC_TRACK(si);
948 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000949}
950
Raymond Hettingerd7946662005-08-01 21:39:29 +0000951static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (PyAnySet_Check(other))
957 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 if (PyDict_CheckExact(other)) {
960 PyObject *value;
961 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000962 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 /* Do one big resize at the start, rather than
966 * incrementally resizing as we insert new keys. Expect
967 * that there will be no (or few) overlapping keys.
968 */
969 if (dictsize == -1)
970 return -1;
971 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
972 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
973 return -1;
974 }
975 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
976 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 an_entry.hash = hash;
979 an_entry.key = key;
980 if (set_add_entry(so, &an_entry) == -1)
981 return -1;
982 }
983 return 0;
984 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 it = PyObject_GetIter(other);
987 if (it == NULL)
988 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 while ((key = PyIter_Next(it)) != NULL) {
991 if (set_add_key(so, key) == -1) {
992 Py_DECREF(it);
993 Py_DECREF(key);
994 return -1;
995 }
996 Py_DECREF(key);
997 }
998 Py_DECREF(it);
999 if (PyErr_Occurred())
1000 return -1;
1001 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001002}
1003
1004static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001005set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1010 PyObject *other = PyTuple_GET_ITEM(args, i);
1011 if (set_update_internal(so, other) == -1)
1012 return NULL;
1013 }
1014 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001015}
1016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001018"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001019
1020static PyObject *
1021make_new_set(PyTypeObject *type, PyObject *iterable)
1022{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001023 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 /* create PySetObject structure */
1026 if (numfree &&
1027 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1028 so = free_list[--numfree];
1029 assert (so != NULL && PyAnySet_CheckExact(so));
1030 Py_TYPE(so) = type;
1031 _Py_NewReference((PyObject *)so);
1032 EMPTY_TO_MINSIZE(so);
1033 PyObject_GC_Track(so);
1034 } else {
1035 so = (PySetObject *)type->tp_alloc(type, 0);
1036 if (so == NULL)
1037 return NULL;
1038 /* tp_alloc has already zeroed the structure */
1039 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1040 INIT_NONZERO_SET_SLOTS(so);
1041 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 so->lookup = set_lookkey_unicode;
1044 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 if (iterable != NULL) {
1047 if (set_update_internal(so, iterable) == -1) {
1048 Py_DECREF(so);
1049 return NULL;
1050 }
1051 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001054}
1055
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001056static PyObject *
1057make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1060 if (PyType_IsSubtype(type, &PySet_Type))
1061 type = &PySet_Type;
1062 else
1063 type = &PyFrozenSet_Type;
1064 }
1065 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001066}
1067
Raymond Hettingerd7946662005-08-01 21:39:29 +00001068/* The empty frozenset is a singleton */
1069static PyObject *emptyfrozenset = NULL;
1070
Raymond Hettingera690a992003-11-16 16:17:49 +00001071static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001072frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1077 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1080 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 if (type != &PyFrozenSet_Type)
1083 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 if (iterable != NULL) {
1086 /* frozenset(f) is idempotent */
1087 if (PyFrozenSet_CheckExact(iterable)) {
1088 Py_INCREF(iterable);
1089 return iterable;
1090 }
1091 result = make_new_set(type, iterable);
1092 if (result == NULL || PySet_GET_SIZE(result))
1093 return result;
1094 Py_DECREF(result);
1095 }
1096 /* The empty frozenset is a singleton */
1097 if (emptyfrozenset == NULL)
1098 emptyfrozenset = make_new_set(type, NULL);
1099 Py_XINCREF(emptyfrozenset);
1100 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001101}
1102
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001103int
1104PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001105{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001106 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 while (numfree) {
1110 numfree--;
1111 so = free_list[numfree];
1112 PyObject_GC_Del(so);
1113 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001114 return freelist_size;
1115}
1116
1117void
1118PySet_Fini(void)
1119{
1120 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001122}
1123
David Malcolm49526f42012-06-22 14:55:41 -04001124/* Print summary info about the state of the optimized allocator */
1125void
1126_PySet_DebugMallocStats(FILE *out)
1127{
1128 _PyDebugAllocatorStats(out,
1129 "free PySetObject",
1130 numfree, sizeof(PySetObject));
1131}
1132
1133
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001134static PyObject *
1135set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1138 return NULL;
1139
1140 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001141}
1142
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001143/* set_swap_bodies() switches the contents of any two sets by moving their
1144 internal data pointers and, if needed, copying the internal smalltables.
1145 Semantically equivalent to:
1146
1147 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1148
1149 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001151 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001153 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001154*/
1155
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001156static void
1157set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 Py_ssize_t t;
1160 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001161 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001163 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 t = a->fill; a->fill = b->fill; b->fill = t;
1166 t = a->used; a->used = b->used; b->used = t;
1167 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 u = a->table;
1170 if (a->table == a->smalltable)
1171 u = b->smalltable;
1172 a->table = b->table;
1173 if (b->table == b->smalltable)
1174 a->table = a->smalltable;
1175 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (a->table == a->smalltable || b->table == b->smalltable) {
1180 memcpy(tab, a->smalltable, sizeof(tab));
1181 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1182 memcpy(b->smalltable, tab, sizeof(tab));
1183 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1186 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1187 h = a->hash; a->hash = b->hash; b->hash = h;
1188 } else {
1189 a->hash = -1;
1190 b->hash = -1;
1191 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001192}
1193
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001194static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001195set_copy(PySetObject *so)
1196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001198}
1199
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001200static PyObject *
1201frozenset_copy(PySetObject *so)
1202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 if (PyFrozenSet_CheckExact(so)) {
1204 Py_INCREF(so);
1205 return (PyObject *)so;
1206 }
1207 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001208}
1209
Raymond Hettingera690a992003-11-16 16:17:49 +00001210PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1211
1212static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001213set_clear(PySetObject *so)
1214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 set_clear_internal(so);
1216 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001217}
1218
1219PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1220
1221static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001222set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 PySetObject *result;
1225 PyObject *other;
1226 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 result = (PySetObject *)set_copy(so);
1229 if (result == NULL)
1230 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1233 other = PyTuple_GET_ITEM(args, i);
1234 if ((PyObject *)so == other)
1235 continue;
1236 if (set_update_internal(result, other) == -1) {
1237 Py_DECREF(result);
1238 return NULL;
1239 }
1240 }
1241 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001242}
1243
1244PyDoc_STRVAR(union_doc,
1245 "Return the union of sets as a new set.\n\
1246\n\
1247(i.e. all elements that are in either set.)");
1248
1249static PyObject *
1250set_or(PySetObject *so, PyObject *other)
1251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001253
Brian Curtindfc80e32011-08-10 20:28:54 -05001254 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1255 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 result = (PySetObject *)set_copy(so);
1258 if (result == NULL)
1259 return NULL;
1260 if ((PyObject *)so == other)
1261 return (PyObject *)result;
1262 if (set_update_internal(result, other) == -1) {
1263 Py_DECREF(result);
1264 return NULL;
1265 }
1266 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001267}
1268
Raymond Hettingera690a992003-11-16 16:17:49 +00001269static PyObject *
1270set_ior(PySetObject *so, PyObject *other)
1271{
Brian Curtindfc80e32011-08-10 20:28:54 -05001272 if (!PyAnySet_Check(other))
1273 Py_RETURN_NOTIMPLEMENTED;
1274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 if (set_update_internal(so, other) == -1)
1276 return NULL;
1277 Py_INCREF(so);
1278 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001279}
1280
1281static PyObject *
1282set_intersection(PySetObject *so, PyObject *other)
1283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 PySetObject *result;
1285 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if ((PyObject *)so == other)
1288 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1291 if (result == NULL)
1292 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (PyAnySet_Check(other)) {
1295 Py_ssize_t pos = 0;
1296 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1299 tmp = (PyObject *)so;
1300 so = (PySetObject *)other;
1301 other = tmp;
1302 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 while (set_next((PySetObject *)other, &pos, &entry)) {
1305 int rv = set_contains_entry(so, entry);
1306 if (rv == -1) {
1307 Py_DECREF(result);
1308 return NULL;
1309 }
1310 if (rv) {
1311 if (set_add_entry(result, entry) == -1) {
1312 Py_DECREF(result);
1313 return NULL;
1314 }
1315 }
1316 }
1317 return (PyObject *)result;
1318 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 it = PyObject_GetIter(other);
1321 if (it == NULL) {
1322 Py_DECREF(result);
1323 return NULL;
1324 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 while ((key = PyIter_Next(it)) != NULL) {
1327 int rv;
1328 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001329 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 if (hash == -1) {
1332 Py_DECREF(it);
1333 Py_DECREF(result);
1334 Py_DECREF(key);
1335 return NULL;
1336 }
1337 entry.hash = hash;
1338 entry.key = key;
1339 rv = set_contains_entry(so, &entry);
1340 if (rv == -1) {
1341 Py_DECREF(it);
1342 Py_DECREF(result);
1343 Py_DECREF(key);
1344 return NULL;
1345 }
1346 if (rv) {
1347 if (set_add_entry(result, &entry) == -1) {
1348 Py_DECREF(it);
1349 Py_DECREF(result);
1350 Py_DECREF(key);
1351 return NULL;
1352 }
1353 }
1354 Py_DECREF(key);
1355 }
1356 Py_DECREF(it);
1357 if (PyErr_Occurred()) {
1358 Py_DECREF(result);
1359 return NULL;
1360 }
1361 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001362}
1363
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001364static PyObject *
1365set_intersection_multi(PySetObject *so, PyObject *args)
1366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 Py_ssize_t i;
1368 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if (PyTuple_GET_SIZE(args) == 0)
1371 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 Py_INCREF(so);
1374 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1375 PyObject *other = PyTuple_GET_ITEM(args, i);
1376 PyObject *newresult = set_intersection((PySetObject *)result, other);
1377 if (newresult == NULL) {
1378 Py_DECREF(result);
1379 return NULL;
1380 }
1381 Py_DECREF(result);
1382 result = newresult;
1383 }
1384 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001385}
1386
Raymond Hettingera690a992003-11-16 16:17:49 +00001387PyDoc_STRVAR(intersection_doc,
1388"Return the intersection of two sets as a new set.\n\
1389\n\
1390(i.e. all elements that are in both sets.)");
1391
1392static PyObject *
1393set_intersection_update(PySetObject *so, PyObject *other)
1394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 tmp = set_intersection(so, other);
1398 if (tmp == NULL)
1399 return NULL;
1400 set_swap_bodies(so, (PySetObject *)tmp);
1401 Py_DECREF(tmp);
1402 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001403}
1404
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001405static PyObject *
1406set_intersection_update_multi(PySetObject *so, PyObject *args)
1407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 tmp = set_intersection_multi(so, args);
1411 if (tmp == NULL)
1412 return NULL;
1413 set_swap_bodies(so, (PySetObject *)tmp);
1414 Py_DECREF(tmp);
1415 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001416}
1417
Raymond Hettingera690a992003-11-16 16:17:49 +00001418PyDoc_STRVAR(intersection_update_doc,
1419"Update a set with the intersection of itself and another.");
1420
1421static PyObject *
1422set_and(PySetObject *so, PyObject *other)
1423{
Brian Curtindfc80e32011-08-10 20:28:54 -05001424 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1425 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001427}
1428
1429static PyObject *
1430set_iand(PySetObject *so, PyObject *other)
1431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001433
Brian Curtindfc80e32011-08-10 20:28:54 -05001434 if (!PyAnySet_Check(other))
1435 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 result = set_intersection_update(so, other);
1437 if (result == NULL)
1438 return NULL;
1439 Py_DECREF(result);
1440 Py_INCREF(so);
1441 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001442}
1443
Guido van Rossum58da9312007-11-10 23:39:45 +00001444static PyObject *
1445set_isdisjoint(PySetObject *so, PyObject *other)
1446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 if ((PyObject *)so == other) {
1450 if (PySet_GET_SIZE(so) == 0)
1451 Py_RETURN_TRUE;
1452 else
1453 Py_RETURN_FALSE;
1454 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 if (PyAnySet_CheckExact(other)) {
1457 Py_ssize_t pos = 0;
1458 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1461 tmp = (PyObject *)so;
1462 so = (PySetObject *)other;
1463 other = tmp;
1464 }
1465 while (set_next((PySetObject *)other, &pos, &entry)) {
1466 int rv = set_contains_entry(so, entry);
1467 if (rv == -1)
1468 return NULL;
1469 if (rv)
1470 Py_RETURN_FALSE;
1471 }
1472 Py_RETURN_TRUE;
1473 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 it = PyObject_GetIter(other);
1476 if (it == NULL)
1477 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 while ((key = PyIter_Next(it)) != NULL) {
1480 int rv;
1481 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001482 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 if (hash == -1) {
1485 Py_DECREF(key);
1486 Py_DECREF(it);
1487 return NULL;
1488 }
1489 entry.hash = hash;
1490 entry.key = key;
1491 rv = set_contains_entry(so, &entry);
1492 Py_DECREF(key);
1493 if (rv == -1) {
1494 Py_DECREF(it);
1495 return NULL;
1496 }
1497 if (rv) {
1498 Py_DECREF(it);
1499 Py_RETURN_FALSE;
1500 }
1501 }
1502 Py_DECREF(it);
1503 if (PyErr_Occurred())
1504 return NULL;
1505 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001506}
1507
1508PyDoc_STRVAR(isdisjoint_doc,
1509"Return True if two sets have a null intersection.");
1510
Neal Norwitz6576bd82005-11-13 18:41:28 +00001511static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001512set_difference_update_internal(PySetObject *so, PyObject *other)
1513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 if ((PyObject *)so == other)
1515 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 if (PyAnySet_Check(other)) {
1518 setentry *entry;
1519 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 while (set_next((PySetObject *)other, &pos, &entry))
1522 if (set_discard_entry(so, entry) == -1)
1523 return -1;
1524 } else {
1525 PyObject *key, *it;
1526 it = PyObject_GetIter(other);
1527 if (it == NULL)
1528 return -1;
1529
1530 while ((key = PyIter_Next(it)) != NULL) {
1531 if (set_discard_key(so, key) == -1) {
1532 Py_DECREF(it);
1533 Py_DECREF(key);
1534 return -1;
1535 }
1536 Py_DECREF(key);
1537 }
1538 Py_DECREF(it);
1539 if (PyErr_Occurred())
1540 return -1;
1541 }
1542 /* If more than 1/5 are dummies, then resize them away. */
1543 if ((so->fill - so->used) * 5 < so->mask)
1544 return 0;
1545 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001546}
1547
Raymond Hettingera690a992003-11-16 16:17:49 +00001548static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001549set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1554 PyObject *other = PyTuple_GET_ITEM(args, i);
1555 if (set_difference_update_internal(so, other) == -1)
1556 return NULL;
1557 }
1558 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001559}
1560
1561PyDoc_STRVAR(difference_update_doc,
1562"Remove all elements of another set from this set.");
1563
1564static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001565set_copy_and_difference(PySetObject *so, PyObject *other)
1566{
1567 PyObject *result;
1568
1569 result = set_copy(so);
1570 if (result == NULL)
1571 return NULL;
1572 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1573 return result;
1574 Py_DECREF(result);
1575 return NULL;
1576}
1577
1578static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001579set_difference(PySetObject *so, PyObject *other)
1580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 PyObject *result;
1582 setentry *entry;
1583 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001586 return set_copy_and_difference(so, other);
1587 }
1588
1589 /* If len(so) much more than len(other), it's more efficient to simply copy
1590 * so and then iterate other looking for common elements. */
1591 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1592 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 result = make_new_set_basetype(Py_TYPE(so), NULL);
1596 if (result == NULL)
1597 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 if (PyDict_CheckExact(other)) {
1600 while (set_next(so, &pos, &entry)) {
1601 setentry entrycopy;
1602 entrycopy.hash = entry->hash;
1603 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001604 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1606 Py_DECREF(result);
1607 return NULL;
1608 }
1609 }
1610 }
1611 return result;
1612 }
1613
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001614 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 while (set_next(so, &pos, &entry)) {
1616 int rv = set_contains_entry((PySetObject *)other, entry);
1617 if (rv == -1) {
1618 Py_DECREF(result);
1619 return NULL;
1620 }
1621 if (!rv) {
1622 if (set_add_entry((PySetObject *)result, entry) == -1) {
1623 Py_DECREF(result);
1624 return NULL;
1625 }
1626 }
1627 }
1628 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001629}
1630
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001631static PyObject *
1632set_difference_multi(PySetObject *so, PyObject *args)
1633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 Py_ssize_t i;
1635 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 if (PyTuple_GET_SIZE(args) == 0)
1638 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 other = PyTuple_GET_ITEM(args, 0);
1641 result = set_difference(so, other);
1642 if (result == NULL)
1643 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1646 other = PyTuple_GET_ITEM(args, i);
1647 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1648 Py_DECREF(result);
1649 return NULL;
1650 }
1651 }
1652 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001653}
1654
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001655PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001656"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001657\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001658(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001659static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001660set_sub(PySetObject *so, PyObject *other)
1661{
Brian Curtindfc80e32011-08-10 20:28:54 -05001662 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1663 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001665}
1666
1667static PyObject *
1668set_isub(PySetObject *so, PyObject *other)
1669{
Brian Curtindfc80e32011-08-10 20:28:54 -05001670 if (!PyAnySet_Check(other))
1671 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 if (set_difference_update_internal(so, other) == -1)
1673 return NULL;
1674 Py_INCREF(so);
1675 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001676}
1677
1678static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001679set_symmetric_difference_update(PySetObject *so, PyObject *other)
1680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 PySetObject *otherset;
1682 PyObject *key;
1683 Py_ssize_t pos = 0;
1684 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 if ((PyObject *)so == other)
1687 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 if (PyDict_CheckExact(other)) {
1690 PyObject *value;
1691 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001692 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1694 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001695
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001696 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 an_entry.hash = hash;
1698 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001701 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001702 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001705 if (rv == DISCARD_NOTFOUND) {
1706 if (set_add_entry(so, &an_entry) == -1) {
1707 Py_DECREF(key);
1708 return NULL;
1709 }
1710 }
Georg Brandl2d444492010-09-03 10:52:55 +00001711 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 }
1713 Py_RETURN_NONE;
1714 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 if (PyAnySet_Check(other)) {
1717 Py_INCREF(other);
1718 otherset = (PySetObject *)other;
1719 } else {
1720 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1721 if (otherset == NULL)
1722 return NULL;
1723 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 while (set_next(otherset, &pos, &entry)) {
1726 int rv = set_discard_entry(so, entry);
1727 if (rv == -1) {
1728 Py_DECREF(otherset);
1729 return NULL;
1730 }
1731 if (rv == DISCARD_NOTFOUND) {
1732 if (set_add_entry(so, entry) == -1) {
1733 Py_DECREF(otherset);
1734 return NULL;
1735 }
1736 }
1737 }
1738 Py_DECREF(otherset);
1739 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001740}
1741
1742PyDoc_STRVAR(symmetric_difference_update_doc,
1743"Update a set with the symmetric difference of itself and another.");
1744
1745static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001746set_symmetric_difference(PySetObject *so, PyObject *other)
1747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 PyObject *rv;
1749 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1752 if (otherset == NULL)
1753 return NULL;
1754 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1755 if (rv == NULL)
1756 return NULL;
1757 Py_DECREF(rv);
1758 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001759}
1760
1761PyDoc_STRVAR(symmetric_difference_doc,
1762"Return the symmetric difference of two sets as a new set.\n\
1763\n\
1764(i.e. all elements that are in exactly one of the sets.)");
1765
1766static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001767set_xor(PySetObject *so, PyObject *other)
1768{
Brian Curtindfc80e32011-08-10 20:28:54 -05001769 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1770 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001772}
1773
1774static PyObject *
1775set_ixor(PySetObject *so, PyObject *other)
1776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001778
Brian Curtindfc80e32011-08-10 20:28:54 -05001779 if (!PyAnySet_Check(other))
1780 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 result = set_symmetric_difference_update(so, other);
1782 if (result == NULL)
1783 return NULL;
1784 Py_DECREF(result);
1785 Py_INCREF(so);
1786 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001787}
1788
1789static PyObject *
1790set_issubset(PySetObject *so, PyObject *other)
1791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 setentry *entry;
1793 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 if (!PyAnySet_Check(other)) {
1796 PyObject *tmp, *result;
1797 tmp = make_new_set(&PySet_Type, other);
1798 if (tmp == NULL)
1799 return NULL;
1800 result = set_issubset(so, tmp);
1801 Py_DECREF(tmp);
1802 return result;
1803 }
1804 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1805 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 while (set_next(so, &pos, &entry)) {
1808 int rv = set_contains_entry((PySetObject *)other, entry);
1809 if (rv == -1)
1810 return NULL;
1811 if (!rv)
1812 Py_RETURN_FALSE;
1813 }
1814 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001815}
1816
1817PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1818
1819static PyObject *
1820set_issuperset(PySetObject *so, PyObject *other)
1821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 if (!PyAnySet_Check(other)) {
1825 tmp = make_new_set(&PySet_Type, other);
1826 if (tmp == NULL)
1827 return NULL;
1828 result = set_issuperset(so, tmp);
1829 Py_DECREF(tmp);
1830 return result;
1831 }
1832 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001833}
1834
1835PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1836
Raymond Hettingera690a992003-11-16 16:17:49 +00001837static PyObject *
1838set_richcompare(PySetObject *v, PyObject *w, int op)
1839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001841
Brian Curtindfc80e32011-08-10 20:28:54 -05001842 if(!PyAnySet_Check(w))
1843 Py_RETURN_NOTIMPLEMENTED;
1844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 switch (op) {
1846 case Py_EQ:
1847 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1848 Py_RETURN_FALSE;
1849 if (v->hash != -1 &&
1850 ((PySetObject *)w)->hash != -1 &&
1851 v->hash != ((PySetObject *)w)->hash)
1852 Py_RETURN_FALSE;
1853 return set_issubset(v, w);
1854 case Py_NE:
1855 r1 = set_richcompare(v, w, Py_EQ);
1856 if (r1 == NULL)
1857 return NULL;
1858 r2 = PyBool_FromLong(PyObject_Not(r1));
1859 Py_DECREF(r1);
1860 return r2;
1861 case Py_LE:
1862 return set_issubset(v, w);
1863 case Py_GE:
1864 return set_issuperset(v, w);
1865 case Py_LT:
1866 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1867 Py_RETURN_FALSE;
1868 return set_issubset(v, w);
1869 case Py_GT:
1870 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1871 Py_RETURN_FALSE;
1872 return set_issuperset(v, w);
1873 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001874 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001875}
1876
Raymond Hettingera690a992003-11-16 16:17:49 +00001877static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001878set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 if (set_add_key(so, key) == -1)
1881 return NULL;
1882 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001883}
1884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001886"Add an element to a set.\n\
1887\n\
1888This has no effect if the element is already present.");
1889
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001890static int
1891set_contains(PySetObject *so, PyObject *key)
1892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 PyObject *tmpkey;
1894 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 rv = set_contains_key(so, key);
1897 if (rv == -1) {
1898 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1899 return -1;
1900 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001901 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 if (tmpkey == NULL)
1903 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001904 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 Py_DECREF(tmpkey);
1906 }
1907 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001908}
1909
1910static PyObject *
1911set_direct_contains(PySetObject *so, PyObject *key)
1912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 result = set_contains(so, key);
1916 if (result == -1)
1917 return NULL;
1918 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001919}
1920
1921PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1922
Raymond Hettingera690a992003-11-16 16:17:49 +00001923static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001924set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 PyObject *tmpkey;
1927 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 rv = set_discard_key(so, key);
1930 if (rv == -1) {
1931 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1932 return NULL;
1933 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001934 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 if (tmpkey == NULL)
1936 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 Py_DECREF(tmpkey);
1939 if (rv == -1)
1940 return NULL;
1941 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001944 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 return NULL;
1946 }
1947 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001948}
1949
1950PyDoc_STRVAR(remove_doc,
1951"Remove an element from a set; it must be a member.\n\
1952\n\
1953If the element is not a member, raise a KeyError.");
1954
1955static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001956set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001957{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001958 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 rv = set_discard_key(so, key);
1962 if (rv == -1) {
1963 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1964 return NULL;
1965 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001966 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 if (tmpkey == NULL)
1968 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001969 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001971 if (rv == -1)
1972 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 }
1974 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001975}
1976
1977PyDoc_STRVAR(discard_doc,
1978"Remove an element from a set if it is a member.\n\
1979\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001981
1982static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001983set_reduce(PySetObject *so)
1984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001986 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 keys = PySequence_List((PyObject *)so);
1989 if (keys == NULL)
1990 goto done;
1991 args = PyTuple_Pack(1, keys);
1992 if (args == NULL)
1993 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001994 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 if (dict == NULL) {
1996 PyErr_Clear();
1997 dict = Py_None;
1998 Py_INCREF(dict);
1999 }
2000 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002001done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 Py_XDECREF(args);
2003 Py_XDECREF(keys);
2004 Py_XDECREF(dict);
2005 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002006}
2007
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002008static PyObject *
2009set_sizeof(PySetObject *so)
2010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 res = sizeof(PySetObject);
2014 if (so->table != so->smalltable)
2015 res = res + (so->mask + 1) * sizeof(setentry);
2016 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002017}
2018
2019PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002020static int
2021set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 if (!PyAnySet_Check(self))
2026 return -1;
2027 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2028 return -1;
2029 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2030 return -1;
2031 set_clear_internal(self);
2032 self->hash = -1;
2033 if (iterable == NULL)
2034 return 0;
2035 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002036}
2037
Raymond Hettingera690a992003-11-16 16:17:49 +00002038static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 set_len, /* sq_length */
2040 0, /* sq_concat */
2041 0, /* sq_repeat */
2042 0, /* sq_item */
2043 0, /* sq_slice */
2044 0, /* sq_ass_item */
2045 0, /* sq_ass_slice */
2046 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002047};
2048
2049/* set object ********************************************************/
2050
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002051#ifdef Py_DEBUG
2052static PyObject *test_c_api(PySetObject *so);
2053
2054PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2055All is well if assertions don't fail.");
2056#endif
2057
Raymond Hettingera690a992003-11-16 16:17:49 +00002058static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 {"add", (PyCFunction)set_add, METH_O,
2060 add_doc},
2061 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2062 clear_doc},
2063 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2064 contains_doc},
2065 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2066 copy_doc},
2067 {"discard", (PyCFunction)set_discard, METH_O,
2068 discard_doc},
2069 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2070 difference_doc},
2071 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2072 difference_update_doc},
2073 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2074 intersection_doc},
2075 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2076 intersection_update_doc},
2077 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2078 isdisjoint_doc},
2079 {"issubset", (PyCFunction)set_issubset, METH_O,
2080 issubset_doc},
2081 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2082 issuperset_doc},
2083 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2084 pop_doc},
2085 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2086 reduce_doc},
2087 {"remove", (PyCFunction)set_remove, METH_O,
2088 remove_doc},
2089 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2090 sizeof_doc},
2091 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2092 symmetric_difference_doc},
2093 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2094 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002095#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2097 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002098#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 {"union", (PyCFunction)set_union, METH_VARARGS,
2100 union_doc},
2101 {"update", (PyCFunction)set_update, METH_VARARGS,
2102 update_doc},
2103 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002104};
2105
2106static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 0, /*nb_add*/
2108 (binaryfunc)set_sub, /*nb_subtract*/
2109 0, /*nb_multiply*/
2110 0, /*nb_remainder*/
2111 0, /*nb_divmod*/
2112 0, /*nb_power*/
2113 0, /*nb_negative*/
2114 0, /*nb_positive*/
2115 0, /*nb_absolute*/
2116 0, /*nb_bool*/
2117 0, /*nb_invert*/
2118 0, /*nb_lshift*/
2119 0, /*nb_rshift*/
2120 (binaryfunc)set_and, /*nb_and*/
2121 (binaryfunc)set_xor, /*nb_xor*/
2122 (binaryfunc)set_or, /*nb_or*/
2123 0, /*nb_int*/
2124 0, /*nb_reserved*/
2125 0, /*nb_float*/
2126 0, /*nb_inplace_add*/
2127 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2128 0, /*nb_inplace_multiply*/
2129 0, /*nb_inplace_remainder*/
2130 0, /*nb_inplace_power*/
2131 0, /*nb_inplace_lshift*/
2132 0, /*nb_inplace_rshift*/
2133 (binaryfunc)set_iand, /*nb_inplace_and*/
2134 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2135 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002136};
2137
2138PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002139"set() -> new empty set object\n\
2140set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002141\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002142Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002143
2144PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2146 "set", /* tp_name */
2147 sizeof(PySetObject), /* tp_basicsize */
2148 0, /* tp_itemsize */
2149 /* methods */
2150 (destructor)set_dealloc, /* tp_dealloc */
2151 0, /* tp_print */
2152 0, /* tp_getattr */
2153 0, /* tp_setattr */
2154 0, /* tp_reserved */
2155 (reprfunc)set_repr, /* tp_repr */
2156 &set_as_number, /* tp_as_number */
2157 &set_as_sequence, /* tp_as_sequence */
2158 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002159 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 0, /* tp_call */
2161 0, /* tp_str */
2162 PyObject_GenericGetAttr, /* tp_getattro */
2163 0, /* tp_setattro */
2164 0, /* tp_as_buffer */
2165 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2166 Py_TPFLAGS_BASETYPE, /* tp_flags */
2167 set_doc, /* tp_doc */
2168 (traverseproc)set_traverse, /* tp_traverse */
2169 (inquiry)set_clear_internal, /* tp_clear */
2170 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002171 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2172 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 0, /* tp_iternext */
2174 set_methods, /* tp_methods */
2175 0, /* tp_members */
2176 0, /* tp_getset */
2177 0, /* tp_base */
2178 0, /* tp_dict */
2179 0, /* tp_descr_get */
2180 0, /* tp_descr_set */
2181 0, /* tp_dictoffset */
2182 (initproc)set_init, /* tp_init */
2183 PyType_GenericAlloc, /* tp_alloc */
2184 set_new, /* tp_new */
2185 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002186};
2187
2188/* frozenset object ********************************************************/
2189
2190
2191static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2193 contains_doc},
2194 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2195 copy_doc},
2196 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2197 difference_doc},
2198 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2199 intersection_doc},
2200 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2201 isdisjoint_doc},
2202 {"issubset", (PyCFunction)set_issubset, METH_O,
2203 issubset_doc},
2204 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2205 issuperset_doc},
2206 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2207 reduce_doc},
2208 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2209 sizeof_doc},
2210 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2211 symmetric_difference_doc},
2212 {"union", (PyCFunction)set_union, METH_VARARGS,
2213 union_doc},
2214 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002215};
2216
2217static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 0, /*nb_add*/
2219 (binaryfunc)set_sub, /*nb_subtract*/
2220 0, /*nb_multiply*/
2221 0, /*nb_remainder*/
2222 0, /*nb_divmod*/
2223 0, /*nb_power*/
2224 0, /*nb_negative*/
2225 0, /*nb_positive*/
2226 0, /*nb_absolute*/
2227 0, /*nb_bool*/
2228 0, /*nb_invert*/
2229 0, /*nb_lshift*/
2230 0, /*nb_rshift*/
2231 (binaryfunc)set_and, /*nb_and*/
2232 (binaryfunc)set_xor, /*nb_xor*/
2233 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002234};
2235
2236PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002237"frozenset() -> empty frozenset object\n\
2238frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002239\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002240Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002241
2242PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2244 "frozenset", /* tp_name */
2245 sizeof(PySetObject), /* tp_basicsize */
2246 0, /* tp_itemsize */
2247 /* methods */
2248 (destructor)set_dealloc, /* tp_dealloc */
2249 0, /* tp_print */
2250 0, /* tp_getattr */
2251 0, /* tp_setattr */
2252 0, /* tp_reserved */
2253 (reprfunc)set_repr, /* tp_repr */
2254 &frozenset_as_number, /* tp_as_number */
2255 &set_as_sequence, /* tp_as_sequence */
2256 0, /* tp_as_mapping */
2257 frozenset_hash, /* tp_hash */
2258 0, /* tp_call */
2259 0, /* tp_str */
2260 PyObject_GenericGetAttr, /* tp_getattro */
2261 0, /* tp_setattro */
2262 0, /* tp_as_buffer */
2263 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2264 Py_TPFLAGS_BASETYPE, /* tp_flags */
2265 frozenset_doc, /* tp_doc */
2266 (traverseproc)set_traverse, /* tp_traverse */
2267 (inquiry)set_clear_internal, /* tp_clear */
2268 (richcmpfunc)set_richcompare, /* tp_richcompare */
2269 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2270 (getiterfunc)set_iter, /* tp_iter */
2271 0, /* tp_iternext */
2272 frozenset_methods, /* tp_methods */
2273 0, /* tp_members */
2274 0, /* tp_getset */
2275 0, /* tp_base */
2276 0, /* tp_dict */
2277 0, /* tp_descr_get */
2278 0, /* tp_descr_set */
2279 0, /* tp_dictoffset */
2280 0, /* tp_init */
2281 PyType_GenericAlloc, /* tp_alloc */
2282 frozenset_new, /* tp_new */
2283 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002284};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002285
2286
2287/***** C API functions *************************************************/
2288
2289PyObject *
2290PySet_New(PyObject *iterable)
2291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002293}
2294
2295PyObject *
2296PyFrozenSet_New(PyObject *iterable)
2297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002299}
2300
Neal Norwitz8c49c822006-03-04 18:41:19 +00002301Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002302PySet_Size(PyObject *anyset)
2303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 if (!PyAnySet_Check(anyset)) {
2305 PyErr_BadInternalCall();
2306 return -1;
2307 }
2308 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002309}
2310
2311int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002312PySet_Clear(PyObject *set)
2313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 if (!PySet_Check(set)) {
2315 PyErr_BadInternalCall();
2316 return -1;
2317 }
2318 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002319}
2320
2321int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002322PySet_Contains(PyObject *anyset, PyObject *key)
2323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 if (!PyAnySet_Check(anyset)) {
2325 PyErr_BadInternalCall();
2326 return -1;
2327 }
2328 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002329}
2330
2331int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002332PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 if (!PySet_Check(set)) {
2335 PyErr_BadInternalCall();
2336 return -1;
2337 }
2338 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002339}
2340
2341int
Christian Heimesfd66e512008-01-29 12:18:50 +00002342PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 if (!PySet_Check(anyset) &&
2345 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2346 PyErr_BadInternalCall();
2347 return -1;
2348 }
2349 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002350}
2351
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002352int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002353_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 if (!PyAnySet_Check(set)) {
2358 PyErr_BadInternalCall();
2359 return -1;
2360 }
2361 if (set_next((PySetObject *)set, pos, &entry) == 0)
2362 return 0;
2363 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002364 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002366}
2367
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002368PyObject *
2369PySet_Pop(PyObject *set)
2370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 if (!PySet_Check(set)) {
2372 PyErr_BadInternalCall();
2373 return NULL;
2374 }
2375 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002376}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002377
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002378int
2379_PySet_Update(PyObject *set, PyObject *iterable)
2380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 if (!PySet_Check(set)) {
2382 PyErr_BadInternalCall();
2383 return -1;
2384 }
2385 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002386}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002387
2388#ifdef Py_DEBUG
2389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002391 Returns True and original set is restored. */
2392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393#define assertRaises(call_return_value, exception) \
2394 do { \
2395 assert(call_return_value); \
2396 assert(PyErr_ExceptionMatches(exception)); \
2397 PyErr_Clear(); \
2398 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002399
2400static PyObject *
2401test_c_api(PySetObject *so)
2402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 Py_ssize_t count;
2404 char *s;
2405 Py_ssize_t i;
2406 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2407 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002408 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 /* Verify preconditions */
2412 assert(PyAnySet_Check(ob));
2413 assert(PyAnySet_CheckExact(ob));
2414 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* so.clear(); so |= set("abc"); */
2417 str = PyUnicode_FromString("abc");
2418 if (str == NULL)
2419 return NULL;
2420 set_clear_internal(so);
2421 if (set_update_internal(so, str) == -1) {
2422 Py_DECREF(str);
2423 return NULL;
2424 }
2425 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 /* Exercise type/size checks */
2428 assert(PySet_Size(ob) == 3);
2429 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 /* Raise TypeError for non-iterable constructor arguments */
2432 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2433 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* Raise TypeError for unhashable key */
2436 dup = PySet_New(ob);
2437 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2438 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2439 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 /* Exercise successful pop, contains, add, and discard */
2442 elem = PySet_Pop(ob);
2443 assert(PySet_Contains(ob, elem) == 0);
2444 assert(PySet_GET_SIZE(ob) == 2);
2445 assert(PySet_Add(ob, elem) == 0);
2446 assert(PySet_Contains(ob, elem) == 1);
2447 assert(PySet_GET_SIZE(ob) == 3);
2448 assert(PySet_Discard(ob, elem) == 1);
2449 assert(PySet_GET_SIZE(ob) == 2);
2450 assert(PySet_Discard(ob, elem) == 0);
2451 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 /* Exercise clear */
2454 dup2 = PySet_New(dup);
2455 assert(PySet_Clear(dup2) == 0);
2456 assert(PySet_Size(dup2) == 0);
2457 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 /* Raise SystemError on clear or update of frozen set */
2460 f = PyFrozenSet_New(dup);
2461 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2462 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2463 assert(PySet_Add(f, elem) == 0);
2464 Py_INCREF(f);
2465 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2466 Py_DECREF(f);
2467 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 /* Exercise direct iteration */
2470 i = 0, count = 0;
2471 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2472 s = _PyUnicode_AsString(x);
2473 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2474 count++;
2475 }
2476 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 /* Exercise updates */
2479 dup2 = PySet_New(NULL);
2480 assert(_PySet_Update(dup2, dup) == 0);
2481 assert(PySet_Size(dup2) == 3);
2482 assert(_PySet_Update(dup2, dup) == 0);
2483 assert(PySet_Size(dup2) == 3);
2484 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* Raise SystemError when self argument is not a set or frozenset. */
2487 t = PyTuple_New(0);
2488 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2489 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2490 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 /* Raise SystemError when self argument is not a set. */
2493 f = PyFrozenSet_New(dup);
2494 assert(PySet_Size(f) == 3);
2495 assert(PyFrozenSet_CheckExact(f));
2496 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2497 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2498 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 /* Raise KeyError when popping from an empty set */
2501 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2502 Py_DECREF(ob);
2503 assert(PySet_GET_SIZE(ob) == 0);
2504 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 /* Restore the set from the copy using the PyNumber API */
2507 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2508 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 /* Verify constructors accept NULL arguments */
2511 f = PySet_New(NULL);
2512 assert(f != NULL);
2513 assert(PySet_GET_SIZE(f) == 0);
2514 Py_DECREF(f);
2515 f = PyFrozenSet_New(NULL);
2516 assert(f != NULL);
2517 assert(PyFrozenSet_CheckExact(f));
2518 assert(PySet_GET_SIZE(f) == 0);
2519 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 Py_DECREF(elem);
2522 Py_DECREF(dup);
2523 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002524}
2525
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002526#undef assertRaises
2527
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002528#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002529
2530/***** Dummy Struct *************************************************/
2531
2532static PyObject *
2533dummy_repr(PyObject *op)
2534{
2535 return PyUnicode_FromString("<dummy key>");
2536}
2537
2538static void
2539dummy_dealloc(PyObject* ignore)
2540{
2541 Py_FatalError("deallocating <dummy key>");
2542}
2543
2544static PyTypeObject _PySetDummy_Type = {
2545 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2546 "<dummy key> type",
2547 0,
2548 0,
2549 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2550 0, /*tp_print*/
2551 0, /*tp_getattr*/
2552 0, /*tp_setattr*/
2553 0, /*tp_reserved*/
2554 dummy_repr, /*tp_repr*/
2555 0, /*tp_as_number*/
2556 0, /*tp_as_sequence*/
2557 0, /*tp_as_mapping*/
2558 0, /*tp_hash */
2559 0, /*tp_call */
2560 0, /*tp_str */
2561 0, /*tp_getattro */
2562 0, /*tp_setattro */
2563 0, /*tp_as_buffer */
2564 Py_TPFLAGS_DEFAULT, /*tp_flags */
2565};
2566
2567static PyObject _dummy_struct = {
2568 _PyObject_EXTRA_INIT
2569 2, &_PySetDummy_Type
2570};
2571