blob: 304519c5d472af70209542e47fb11490da545fe0 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettinger6c3c1cc2013-08-31 21:34:24 -07007 Copyright (c) 2003-2013 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
26 Unlike the dictionary implementation, the lookkey functions can return
27 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070055 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t perturb = hash;
58 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070059 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070060 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070061 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062
Raymond Hettingera35adf52013-09-02 03:23:21 -070063 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070064 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070066
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070067 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070069 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070070 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070071 PyObject *startkey = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 Py_INCREF(startkey);
73 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
74 Py_DECREF(startkey);
75 if (cmp < 0)
76 return NULL;
Raymond Hettingerafe89092013-08-28 20:59:31 -070077 if (table != so->table || entry->key != startkey)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 return set_lookkey(so, key, hash);
Raymond Hettingerafe89092013-08-28 20:59:31 -070079 if (cmp > 0)
80 return entry;
81 }
82 if (entry->key == dummy && freeslot == NULL)
83 freeslot = entry;
84
Raymond Hettingerc70a2b72013-09-21 14:02:55 -070085 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
86 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -070087 if (entry->key == NULL)
88 goto found_null;
89 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070090 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070091 if (entry->hash == hash && entry->key != dummy) {
92 PyObject *startkey = entry->key;
93 Py_INCREF(startkey);
94 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
95 Py_DECREF(startkey);
96 if (cmp < 0)
97 return NULL;
98 if (table != so->table || entry->key != startkey)
99 return set_lookkey(so, key, hash);
100 if (cmp > 0)
101 return entry;
102 }
103 if (entry->key == dummy && freeslot == NULL)
104 freeslot = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700106
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700107 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700108 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700109
Raymond Hettingera35adf52013-09-02 03:23:21 -0700110 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700111 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700112 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700114 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700115 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000116}
117
118/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000119 * Hacked up version of set_lookkey which can assume keys are always unicode;
120 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000121 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 */
123static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200124set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700127 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200128 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700129 size_t perturb = hash;
130 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700131 size_t i = (size_t)hash;
132 size_t j;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 /* Make sure this function doesn't have to handle non-unicode keys,
135 including subclasses of str; e.g., one reason to subclass
136 strings is to override __eq__, and for speed we don't cater to
137 that here. */
138 if (!PyUnicode_CheckExact(key)) {
139 so->lookup = set_lookkey;
140 return set_lookkey(so, key, hash);
141 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700142
Raymond Hettingera35adf52013-09-02 03:23:21 -0700143 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700144 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000146
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700147 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700148 if (entry->key == key
149 || (entry->hash == hash
Raymond Hettinger742d8712013-09-08 00:25:57 -0700150 && entry->key != dummy
151 && unicode_eq(entry->key, key)))
Raymond Hettingerafe89092013-08-28 20:59:31 -0700152 return entry;
153 if (entry->key == dummy && freeslot == NULL)
154 freeslot = entry;
155
Raymond Hettingerc70a2b72013-09-21 14:02:55 -0700156 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
157 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -0700158 if (entry->key == NULL)
159 goto found_null;
160 if (entry->key == key
161 || (entry->hash == hash
162 && entry->key != dummy
163 && unicode_eq(entry->key, key)))
164 return entry;
165 if (entry->key == dummy && freeslot == NULL)
166 freeslot = entry;
167 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700168
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700169 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700170 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700171
Raymond Hettingera35adf52013-09-02 03:23:21 -0700172 entry = &table[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700174 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700176 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700177 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000178}
179
180/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000181Internal routine used by set_table_resize() to insert an item which is
182known to be absent from the set. This routine also assumes that
183the set contains no deleted entries. Besides the performance benefit,
184using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
185Note that no refcounts are changed by this routine; if needed, the caller
186is responsible for incref'ing `key`.
187*/
188static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200189set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200192 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700193 size_t perturb = hash;
194 size_t mask = (size_t)so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700195 size_t i = (size_t)hash;
196 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000197
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700198 while (1) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700199 entry = &table[i & mask];
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700200 if (entry->key == NULL)
Raymond Hettingera35adf52013-09-02 03:23:21 -0700201 goto found_null;
202 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
203 entry = &table[(i + j) & mask];
204 if (entry->key == NULL)
205 goto found_null;
206 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700207 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700208 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700210 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 entry->key = key;
212 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700213 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000215}
216
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700217/* ======== End logic for probing the hash table ========================== */
218/* ======================================================================== */
219
220
221/*
222Internal routine to insert a new key into the table.
223Used by the public insert routine.
224Eats a reference to key.
225*/
226static int
227set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
228{
229 setentry *entry;
230
231 assert(so->lookup != NULL);
232 entry = so->lookup(so, key, hash);
233 if (entry == NULL)
234 return -1;
235 if (entry->key == NULL) {
236 /* UNUSED */
237 so->fill++;
238 entry->key = key;
239 entry->hash = hash;
240 so->used++;
241 } else if (entry->key == dummy) {
242 /* DUMMY */
243 entry->key = key;
244 entry->hash = hash;
245 so->used++;
246 } else {
247 /* ACTIVE */
248 Py_DECREF(key);
249 }
250 return 0;
251}
252
Thomas Wouters89f507f2006-12-13 04:49:30 +0000253/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000254Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000255keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000256actually be smaller than the old one.
257*/
258static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 Py_ssize_t newsize;
262 setentry *oldtable, *newtable, *entry;
263 Py_ssize_t i;
264 int is_oldtable_malloced;
265 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 /* Find the smallest table size > minused. */
270 for (newsize = PySet_MINSIZE;
271 newsize <= minused && newsize > 0;
272 newsize <<= 1)
273 ;
274 if (newsize <= 0) {
275 PyErr_NoMemory();
276 return -1;
277 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 /* Get space for a new table. */
280 oldtable = so->table;
281 assert(oldtable != NULL);
282 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 if (newsize == PySet_MINSIZE) {
285 /* A large table is shrinking, or we can't get any smaller. */
286 newtable = so->smalltable;
287 if (newtable == oldtable) {
288 if (so->fill == so->used) {
289 /* No dummies, so no point doing anything. */
290 return 0;
291 }
292 /* We're not going to resize it, but rebuild the
293 table anyway to purge old dummy entries.
294 Subtle: This is *necessary* if fill==size,
295 as set_lookkey needs at least one virgin slot to
296 terminate failing searches. If fill < size, it's
297 merely desirable, as dummies slow searches. */
298 assert(so->fill > so->used);
299 memcpy(small_copy, oldtable, sizeof(small_copy));
300 oldtable = small_copy;
301 }
302 }
303 else {
304 newtable = PyMem_NEW(setentry, newsize);
305 if (newtable == NULL) {
306 PyErr_NoMemory();
307 return -1;
308 }
309 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 /* Make the set empty, using the new table. */
312 assert(newtable != oldtable);
313 so->table = newtable;
314 so->mask = newsize - 1;
315 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700316 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* Copy the data over; this is refcount-neutral for active entries;
321 dummy entries aren't copied over, of course */
322 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500323 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000325 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 }
327 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 if (is_oldtable_malloced)
330 PyMem_DEL(oldtable);
331 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000332}
333
Raymond Hettingerc991db22005-08-11 07:58:45 +0000334/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
335
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000336static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200337set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000338{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200339 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000340 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100341 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 assert(so->fill <= so->mask); /* at least one empty slot */
344 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000345 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100346 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000347 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 return -1;
349 }
350 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
351 return 0;
352 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000353}
354
355static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200356set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000357{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200358 Py_hash_t hash;
359 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200362 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 hash = PyObject_Hash(key);
364 if (hash == -1)
365 return -1;
366 }
367 assert(so->fill <= so->mask); /* at least one empty slot */
368 n_used = so->used;
369 Py_INCREF(key);
370 if (set_insert_key(so, key, hash) == -1) {
371 Py_DECREF(key);
372 return -1;
373 }
374 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
375 return 0;
376 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377}
378
379#define DISCARD_NOTFOUND 0
380#define DISCARD_FOUND 1
381
382static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000383set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200384{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000386
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000387 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (entry == NULL)
389 return -1;
390 if (entry->key == NULL || entry->key == dummy)
391 return DISCARD_NOTFOUND;
392 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 entry->key = dummy;
394 so->used--;
395 Py_DECREF(old_key);
396 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000397}
398
399static int
400set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000401{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200402 Py_hash_t hash;
403 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200409 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 hash = PyObject_Hash(key);
411 if (hash == -1)
412 return -1;
413 }
414 entry = (so->lookup)(so, key, hash);
415 if (entry == NULL)
416 return -1;
417 if (entry->key == NULL || entry->key == dummy)
418 return DISCARD_NOTFOUND;
419 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 entry->key = dummy;
421 so->used--;
422 Py_DECREF(old_key);
423 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424}
425
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700426static void
427set_empty_to_minsize(PySetObject *so)
428{
429 memset(so->smalltable, 0, sizeof(so->smalltable));
430 so->fill = 0;
431 so->used = 0;
432 so->mask = PySet_MINSIZE - 1;
433 so->table = so->smalltable;
434 so->hash = -1;
435}
436
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000437static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000438set_clear_internal(PySetObject *so)
439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 setentry *entry, *table;
441 int table_is_malloced;
442 Py_ssize_t fill;
443 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000444
Raymond Hettinger583cd032013-09-07 22:06:35 -0700445#ifdef Py_DEBUG
446 Py_ssize_t i = 0;
447 Py_ssize_t n = so->mask + 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448#endif
449
Raymond Hettinger583cd032013-09-07 22:06:35 -0700450 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 table = so->table;
452 assert(table != NULL);
453 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 /* This is delicate. During the process of clearing the set,
456 * decrefs can cause the set to mutate. To avoid fatal confusion
457 * (voice of experience), we have to make the set empty before
458 * clearing the slots, and never refer to anything via so->ref while
459 * clearing.
460 */
461 fill = so->fill;
462 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700463 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 else if (fill > 0) {
466 /* It's a small table with something that needs to be cleared.
467 * Afraid the only safe way is to copy the set entries into
468 * another small table first.
469 */
470 memcpy(small_copy, table, sizeof(small_copy));
471 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700472 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 }
474 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 /* Now we can finally clear things. If C had refcounts, we could
477 * assert that the refcount on table is 1 now, i.e. that this function
478 * has unique access to it, so decref side-effects can't alter it.
479 */
480 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000481#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 assert(i < n);
483 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (entry->key) {
486 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700487 if (entry->key != dummy)
488 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000490#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 else
492 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if (table_is_malloced)
497 PyMem_DEL(table);
498 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499}
500
501/*
502 * Iterate over a set table. Use like so:
503 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000504 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000505 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000506 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000507 * while (set_next(yourset, &pos, &entry)) {
508 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509 * }
510 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000511 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513 */
514static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000515set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 Py_ssize_t i;
518 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200519 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 assert (PyAnySet_Check(so));
522 i = *pos_ptr;
523 assert(i >= 0);
524 table = so->table;
525 mask = so->mask;
526 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
527 i++;
528 *pos_ptr = i+1;
529 if (i > mask)
530 return 0;
531 assert(table[i].key != NULL);
532 *entry_ptr = &table[i];
533 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534}
535
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000536static void
537set_dealloc(PySetObject *so)
538{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200539 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 Py_ssize_t fill = so->fill;
541 PyObject_GC_UnTrack(so);
542 Py_TRASHCAN_SAFE_BEGIN(so)
543 if (so->weakreflist != NULL)
544 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 for (entry = so->table; fill > 0; entry++) {
547 if (entry->key) {
548 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700549 if (entry->key != dummy)
550 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 }
552 }
553 if (so->table != so->smalltable)
554 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700555 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000557}
558
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000559static PyObject *
560set_repr(PySetObject *so)
561{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200562 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 if (status != 0) {
566 if (status < 0)
567 return NULL;
568 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
569 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 /* shortcut for the empty set */
572 if (!so->used) {
573 Py_ReprLeave((PyObject*)so);
574 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
575 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 keys = PySequence_List((PyObject *)so);
578 if (keys == NULL)
579 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000580
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200581 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 listrepr = PyObject_Repr(keys);
583 Py_DECREF(keys);
584 if (listrepr == NULL)
585 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200586 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200588 if (tmp == NULL)
589 goto done;
590 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200591
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 if (Py_TYPE(so) != &PySet_Type)
593 result = PyUnicode_FromFormat("%s({%U})",
594 Py_TYPE(so)->tp_name,
595 listrepr);
596 else
597 result = PyUnicode_FromFormat("{%U}", listrepr);
598 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000599done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 Py_ReprLeave((PyObject*)so);
601 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000602}
603
Martin v. Löwis18e16552006-02-15 17:27:45 +0000604static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000605set_len(PyObject *so)
606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000608}
609
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000610static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000611set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000614 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100615 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200616 Py_ssize_t i;
617 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 assert (PyAnySet_Check(so));
620 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 other = (PySetObject*)otherset;
623 if (other == so || other->used == 0)
624 /* a.update(a) or a.update({}); nothing to do */
625 return 0;
626 /* Do one big resize at the start, rather than
627 * incrementally resizing as we insert new keys. Expect
628 * that there will be no (or few) overlapping keys.
629 */
630 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
631 if (set_table_resize(so, (so->used + other->used)*2) != 0)
632 return -1;
633 }
634 for (i = 0; i <= other->mask; i++) {
635 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000636 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100637 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000638 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500639 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000640 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100641 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000642 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 return -1;
644 }
645 }
646 }
647 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648}
649
650static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000651set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000652{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000653 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200657 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 hash = PyObject_Hash(key);
659 if (hash == -1)
660 return -1;
661 }
662 entry = (so->lookup)(so, key, hash);
663 if (entry == NULL)
664 return -1;
665 key = entry->key;
666 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000667}
668
Raymond Hettingerc991db22005-08-11 07:58:45 +0000669static int
670set_contains_entry(PySetObject *so, setentry *entry)
671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 PyObject *key;
673 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000674
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000675 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 if (lu_entry == NULL)
677 return -1;
678 key = lu_entry->key;
679 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000680}
681
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000682static PyObject *
683set_pop(PySetObject *so)
684{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200685 Py_ssize_t i = 0;
686 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 assert (PyAnySet_Check(so));
690 if (so->used == 0) {
691 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
692 return NULL;
693 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 /* Set entry to "the first" unused or dummy set entry. We abuse
696 * the hash field of slot 0 to hold a search finger:
697 * If slot 0 has a value, use slot 0.
698 * Else slot 0 is being used to hold a search finger,
699 * and we use its hash value as the first index to look.
700 */
701 entry = &so->table[0];
702 if (entry->key == NULL || entry->key == dummy) {
703 i = entry->hash;
704 /* The hash field may be a real hash value, or it may be a
705 * legit search finger, or it may be a once-legit search
706 * finger that's out of bounds now because it wrapped around
707 * or the table shrunk -- simply make sure it's in bounds now.
708 */
709 if (i > so->mask || i < 1)
710 i = 1; /* skip slot 0 */
711 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
712 i++;
713 if (i > so->mask)
714 i = 1;
715 }
716 }
717 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 entry->key = dummy;
719 so->used--;
720 so->table[0].hash = i + 1; /* next place to start */
721 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000722}
723
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000724PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
725Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000726
727static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000728set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 Py_ssize_t pos = 0;
731 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 while (set_next(so, &pos, &entry))
734 Py_VISIT(entry->key);
735 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000736}
737
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000738static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000739frozenset_hash(PyObject *self)
740{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800741 /* Most of the constants in this hash algorithm are randomly choosen
742 large primes with "interesting bit patterns" and that passed
743 tests for good collision statistics on a variety of problematic
744 datasets such as:
745
746 ps = []
747 for r in range(21):
748 ps += itertools.combinations(range(20), r)
749 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
750
751 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800753 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 setentry *entry;
755 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (so->hash != -1)
758 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000759
Mark Dickinson57e683e2011-09-24 18:18:40 +0100760 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 while (set_next(so, &pos, &entry)) {
762 /* Work to increase the bit dispersion for closely spaced hash
763 values. The is important because some use cases have many
764 combinations of a small number of elements with nearby
765 hashes so that many distinct combinations collapse to only
766 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000767 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800768 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800770 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500771 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800772 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800774 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 so->hash = hash;
776 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000777}
778
Raymond Hettingera9d99362005-08-05 00:01:15 +0000779/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000780
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000781typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 PyObject_HEAD
783 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
784 Py_ssize_t si_used;
785 Py_ssize_t si_pos;
786 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787} setiterobject;
788
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789static void
790setiter_dealloc(setiterobject *si)
791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 Py_XDECREF(si->si_set);
793 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000794}
795
796static int
797setiter_traverse(setiterobject *si, visitproc visit, void *arg)
798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 Py_VISIT(si->si_set);
800 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801}
802
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000803static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804setiter_len(setiterobject *si)
805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 Py_ssize_t len = 0;
807 if (si->si_set != NULL && si->si_used == si->si_set->used)
808 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000809 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810}
811
Armin Rigof5b3e362006-02-11 21:32:43 +0000812PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000813
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000814static PyObject *setiter_iternext(setiterobject *si);
815
816static PyObject *
817setiter_reduce(setiterobject *si)
818{
819 PyObject *list;
820 setiterobject tmp;
821
822 list = PyList_New(0);
823 if (!list)
824 return NULL;
825
Ezio Melotti0e1af282012-09-28 16:43:40 +0300826 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000827 tmp = *si;
828 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300829
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000830 /* iterate the temporary into a list */
831 for(;;) {
832 PyObject *element = setiter_iternext(&tmp);
833 if (element) {
834 if (PyList_Append(list, element)) {
835 Py_DECREF(element);
836 Py_DECREF(list);
837 Py_XDECREF(tmp.si_set);
838 return NULL;
839 }
840 Py_DECREF(element);
841 } else
842 break;
843 }
844 Py_XDECREF(tmp.si_set);
845 /* check for error */
846 if (tmp.si_set != NULL) {
847 /* we have an error */
848 Py_DECREF(list);
849 return NULL;
850 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200851 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000852}
853
854PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
855
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000856static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000858 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000860};
861
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000862static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200865 Py_ssize_t i, mask;
866 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (so == NULL)
870 return NULL;
871 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (si->si_used != so->used) {
874 PyErr_SetString(PyExc_RuntimeError,
875 "Set changed size during iteration");
876 si->si_used = -1; /* Make this state sticky */
877 return NULL;
878 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 i = si->si_pos;
881 assert(i>=0);
882 entry = so->table;
883 mask = so->mask;
884 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
885 i++;
886 si->si_pos = i+1;
887 if (i > mask)
888 goto fail;
889 si->len--;
890 key = entry[i].key;
891 Py_INCREF(key);
892 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000893
894fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 Py_DECREF(so);
896 si->si_set = NULL;
897 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000898}
899
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000900PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyVarObject_HEAD_INIT(&PyType_Type, 0)
902 "set_iterator", /* tp_name */
903 sizeof(setiterobject), /* tp_basicsize */
904 0, /* tp_itemsize */
905 /* methods */
906 (destructor)setiter_dealloc, /* tp_dealloc */
907 0, /* tp_print */
908 0, /* tp_getattr */
909 0, /* tp_setattr */
910 0, /* tp_reserved */
911 0, /* tp_repr */
912 0, /* tp_as_number */
913 0, /* tp_as_sequence */
914 0, /* tp_as_mapping */
915 0, /* tp_hash */
916 0, /* tp_call */
917 0, /* tp_str */
918 PyObject_GenericGetAttr, /* tp_getattro */
919 0, /* tp_setattro */
920 0, /* tp_as_buffer */
921 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
922 0, /* tp_doc */
923 (traverseproc)setiter_traverse, /* tp_traverse */
924 0, /* tp_clear */
925 0, /* tp_richcompare */
926 0, /* tp_weaklistoffset */
927 PyObject_SelfIter, /* tp_iter */
928 (iternextfunc)setiter_iternext, /* tp_iternext */
929 setiter_methods, /* tp_methods */
930 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000931};
932
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933static PyObject *
934set_iter(PySetObject *so)
935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
937 if (si == NULL)
938 return NULL;
939 Py_INCREF(so);
940 si->si_set = so;
941 si->si_used = so->used;
942 si->si_pos = 0;
943 si->len = so->used;
944 _PyObject_GC_TRACK(si);
945 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000946}
947
Raymond Hettingerd7946662005-08-01 21:39:29 +0000948static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 if (PyAnySet_Check(other))
954 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (PyDict_CheckExact(other)) {
957 PyObject *value;
958 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000959 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 /* Do one big resize at the start, rather than
963 * incrementally resizing as we insert new keys. Expect
964 * that there will be no (or few) overlapping keys.
965 */
966 if (dictsize == -1)
967 return -1;
968 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
969 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
970 return -1;
971 }
972 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
973 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 an_entry.hash = hash;
976 an_entry.key = key;
977 if (set_add_entry(so, &an_entry) == -1)
978 return -1;
979 }
980 return 0;
981 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 it = PyObject_GetIter(other);
984 if (it == NULL)
985 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 while ((key = PyIter_Next(it)) != NULL) {
988 if (set_add_key(so, key) == -1) {
989 Py_DECREF(it);
990 Py_DECREF(key);
991 return -1;
992 }
993 Py_DECREF(key);
994 }
995 Py_DECREF(it);
996 if (PyErr_Occurred())
997 return -1;
998 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000999}
1000
1001static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001002set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1007 PyObject *other = PyTuple_GET_ITEM(args, i);
1008 if (set_update_internal(so, other) == -1)
1009 return NULL;
1010 }
1011 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001012}
1013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001015"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001016
1017static PyObject *
1018make_new_set(PyTypeObject *type, PyObject *iterable)
1019{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001020 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001023 so = (PySetObject *)type->tp_alloc(type, 0);
1024 if (so == NULL)
1025 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001026
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001027 so->fill = 0;
1028 so->used = 0;
1029 so->mask = PySet_MINSIZE - 1;
1030 so->table = so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 so->lookup = set_lookkey_unicode;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001032 so->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (iterable != NULL) {
1036 if (set_update_internal(so, iterable) == -1) {
1037 Py_DECREF(so);
1038 return NULL;
1039 }
1040 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001043}
1044
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001045static PyObject *
1046make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1049 if (PyType_IsSubtype(type, &PySet_Type))
1050 type = &PySet_Type;
1051 else
1052 type = &PyFrozenSet_Type;
1053 }
1054 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001055}
1056
Raymond Hettingerd7946662005-08-01 21:39:29 +00001057/* The empty frozenset is a singleton */
1058static PyObject *emptyfrozenset = NULL;
1059
Raymond Hettingera690a992003-11-16 16:17:49 +00001060static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001061frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1066 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1069 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 if (type != &PyFrozenSet_Type)
1072 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (iterable != NULL) {
1075 /* frozenset(f) is idempotent */
1076 if (PyFrozenSet_CheckExact(iterable)) {
1077 Py_INCREF(iterable);
1078 return iterable;
1079 }
1080 result = make_new_set(type, iterable);
1081 if (result == NULL || PySet_GET_SIZE(result))
1082 return result;
1083 Py_DECREF(result);
1084 }
1085 /* The empty frozenset is a singleton */
1086 if (emptyfrozenset == NULL)
1087 emptyfrozenset = make_new_set(type, NULL);
1088 Py_XINCREF(emptyfrozenset);
1089 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001090}
1091
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001092int
1093PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001094{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001095 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001096}
1097
1098void
1099PySet_Fini(void)
1100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001102}
1103
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001104static PyObject *
1105set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1108 return NULL;
1109
1110 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001111}
1112
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001113/* set_swap_bodies() switches the contents of any two sets by moving their
1114 internal data pointers and, if needed, copying the internal smalltables.
1115 Semantically equivalent to:
1116
1117 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1118
1119 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001121 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001123 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001124*/
1125
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001126static void
1127set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 Py_ssize_t t;
1130 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001131 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001133 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 t = a->fill; a->fill = b->fill; b->fill = t;
1136 t = a->used; a->used = b->used; b->used = t;
1137 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 u = a->table;
1140 if (a->table == a->smalltable)
1141 u = b->smalltable;
1142 a->table = b->table;
1143 if (b->table == b->smalltable)
1144 a->table = a->smalltable;
1145 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (a->table == a->smalltable || b->table == b->smalltable) {
1150 memcpy(tab, a->smalltable, sizeof(tab));
1151 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1152 memcpy(b->smalltable, tab, sizeof(tab));
1153 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1156 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1157 h = a->hash; a->hash = b->hash; b->hash = h;
1158 } else {
1159 a->hash = -1;
1160 b->hash = -1;
1161 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001162}
1163
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001164static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001165set_copy(PySetObject *so)
1166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001168}
1169
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001170static PyObject *
1171frozenset_copy(PySetObject *so)
1172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 if (PyFrozenSet_CheckExact(so)) {
1174 Py_INCREF(so);
1175 return (PyObject *)so;
1176 }
1177 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001178}
1179
Raymond Hettingera690a992003-11-16 16:17:49 +00001180PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1181
1182static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001183set_clear(PySetObject *so)
1184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 set_clear_internal(so);
1186 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001187}
1188
1189PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1190
1191static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001192set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 PySetObject *result;
1195 PyObject *other;
1196 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 result = (PySetObject *)set_copy(so);
1199 if (result == NULL)
1200 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1203 other = PyTuple_GET_ITEM(args, i);
1204 if ((PyObject *)so == other)
1205 continue;
1206 if (set_update_internal(result, other) == -1) {
1207 Py_DECREF(result);
1208 return NULL;
1209 }
1210 }
1211 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001212}
1213
1214PyDoc_STRVAR(union_doc,
1215 "Return the union of sets as a new set.\n\
1216\n\
1217(i.e. all elements that are in either set.)");
1218
1219static PyObject *
1220set_or(PySetObject *so, PyObject *other)
1221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001223
Brian Curtindfc80e32011-08-10 20:28:54 -05001224 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1225 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 result = (PySetObject *)set_copy(so);
1228 if (result == NULL)
1229 return NULL;
1230 if ((PyObject *)so == other)
1231 return (PyObject *)result;
1232 if (set_update_internal(result, other) == -1) {
1233 Py_DECREF(result);
1234 return NULL;
1235 }
1236 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001237}
1238
Raymond Hettingera690a992003-11-16 16:17:49 +00001239static PyObject *
1240set_ior(PySetObject *so, PyObject *other)
1241{
Brian Curtindfc80e32011-08-10 20:28:54 -05001242 if (!PyAnySet_Check(other))
1243 Py_RETURN_NOTIMPLEMENTED;
1244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (set_update_internal(so, other) == -1)
1246 return NULL;
1247 Py_INCREF(so);
1248 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001249}
1250
1251static PyObject *
1252set_intersection(PySetObject *so, PyObject *other)
1253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 PySetObject *result;
1255 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if ((PyObject *)so == other)
1258 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1261 if (result == NULL)
1262 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (PyAnySet_Check(other)) {
1265 Py_ssize_t pos = 0;
1266 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1269 tmp = (PyObject *)so;
1270 so = (PySetObject *)other;
1271 other = tmp;
1272 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 while (set_next((PySetObject *)other, &pos, &entry)) {
1275 int rv = set_contains_entry(so, entry);
1276 if (rv == -1) {
1277 Py_DECREF(result);
1278 return NULL;
1279 }
1280 if (rv) {
1281 if (set_add_entry(result, entry) == -1) {
1282 Py_DECREF(result);
1283 return NULL;
1284 }
1285 }
1286 }
1287 return (PyObject *)result;
1288 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 it = PyObject_GetIter(other);
1291 if (it == NULL) {
1292 Py_DECREF(result);
1293 return NULL;
1294 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 while ((key = PyIter_Next(it)) != NULL) {
1297 int rv;
1298 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001299 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 if (hash == -1) {
1302 Py_DECREF(it);
1303 Py_DECREF(result);
1304 Py_DECREF(key);
1305 return NULL;
1306 }
1307 entry.hash = hash;
1308 entry.key = key;
1309 rv = set_contains_entry(so, &entry);
1310 if (rv == -1) {
1311 Py_DECREF(it);
1312 Py_DECREF(result);
1313 Py_DECREF(key);
1314 return NULL;
1315 }
1316 if (rv) {
1317 if (set_add_entry(result, &entry) == -1) {
1318 Py_DECREF(it);
1319 Py_DECREF(result);
1320 Py_DECREF(key);
1321 return NULL;
1322 }
1323 }
1324 Py_DECREF(key);
1325 }
1326 Py_DECREF(it);
1327 if (PyErr_Occurred()) {
1328 Py_DECREF(result);
1329 return NULL;
1330 }
1331 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001332}
1333
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001334static PyObject *
1335set_intersection_multi(PySetObject *so, PyObject *args)
1336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 Py_ssize_t i;
1338 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (PyTuple_GET_SIZE(args) == 0)
1341 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 Py_INCREF(so);
1344 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1345 PyObject *other = PyTuple_GET_ITEM(args, i);
1346 PyObject *newresult = set_intersection((PySetObject *)result, other);
1347 if (newresult == NULL) {
1348 Py_DECREF(result);
1349 return NULL;
1350 }
1351 Py_DECREF(result);
1352 result = newresult;
1353 }
1354 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001355}
1356
Raymond Hettingera690a992003-11-16 16:17:49 +00001357PyDoc_STRVAR(intersection_doc,
1358"Return the intersection of two sets as a new set.\n\
1359\n\
1360(i.e. all elements that are in both sets.)");
1361
1362static PyObject *
1363set_intersection_update(PySetObject *so, PyObject *other)
1364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 tmp = set_intersection(so, other);
1368 if (tmp == NULL)
1369 return NULL;
1370 set_swap_bodies(so, (PySetObject *)tmp);
1371 Py_DECREF(tmp);
1372 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001373}
1374
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001375static PyObject *
1376set_intersection_update_multi(PySetObject *so, PyObject *args)
1377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 tmp = set_intersection_multi(so, args);
1381 if (tmp == NULL)
1382 return NULL;
1383 set_swap_bodies(so, (PySetObject *)tmp);
1384 Py_DECREF(tmp);
1385 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001386}
1387
Raymond Hettingera690a992003-11-16 16:17:49 +00001388PyDoc_STRVAR(intersection_update_doc,
1389"Update a set with the intersection of itself and another.");
1390
1391static PyObject *
1392set_and(PySetObject *so, PyObject *other)
1393{
Brian Curtindfc80e32011-08-10 20:28:54 -05001394 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1395 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001397}
1398
1399static PyObject *
1400set_iand(PySetObject *so, PyObject *other)
1401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001403
Brian Curtindfc80e32011-08-10 20:28:54 -05001404 if (!PyAnySet_Check(other))
1405 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 result = set_intersection_update(so, other);
1407 if (result == NULL)
1408 return NULL;
1409 Py_DECREF(result);
1410 Py_INCREF(so);
1411 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001412}
1413
Guido van Rossum58da9312007-11-10 23:39:45 +00001414static PyObject *
1415set_isdisjoint(PySetObject *so, PyObject *other)
1416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if ((PyObject *)so == other) {
1420 if (PySet_GET_SIZE(so) == 0)
1421 Py_RETURN_TRUE;
1422 else
1423 Py_RETURN_FALSE;
1424 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if (PyAnySet_CheckExact(other)) {
1427 Py_ssize_t pos = 0;
1428 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1431 tmp = (PyObject *)so;
1432 so = (PySetObject *)other;
1433 other = tmp;
1434 }
1435 while (set_next((PySetObject *)other, &pos, &entry)) {
1436 int rv = set_contains_entry(so, entry);
1437 if (rv == -1)
1438 return NULL;
1439 if (rv)
1440 Py_RETURN_FALSE;
1441 }
1442 Py_RETURN_TRUE;
1443 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 it = PyObject_GetIter(other);
1446 if (it == NULL)
1447 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 while ((key = PyIter_Next(it)) != NULL) {
1450 int rv;
1451 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001452 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 if (hash == -1) {
1455 Py_DECREF(key);
1456 Py_DECREF(it);
1457 return NULL;
1458 }
1459 entry.hash = hash;
1460 entry.key = key;
1461 rv = set_contains_entry(so, &entry);
1462 Py_DECREF(key);
1463 if (rv == -1) {
1464 Py_DECREF(it);
1465 return NULL;
1466 }
1467 if (rv) {
1468 Py_DECREF(it);
1469 Py_RETURN_FALSE;
1470 }
1471 }
1472 Py_DECREF(it);
1473 if (PyErr_Occurred())
1474 return NULL;
1475 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001476}
1477
1478PyDoc_STRVAR(isdisjoint_doc,
1479"Return True if two sets have a null intersection.");
1480
Neal Norwitz6576bd82005-11-13 18:41:28 +00001481static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001482set_difference_update_internal(PySetObject *so, PyObject *other)
1483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 if ((PyObject *)so == other)
1485 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 if (PyAnySet_Check(other)) {
1488 setentry *entry;
1489 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 while (set_next((PySetObject *)other, &pos, &entry))
1492 if (set_discard_entry(so, entry) == -1)
1493 return -1;
1494 } else {
1495 PyObject *key, *it;
1496 it = PyObject_GetIter(other);
1497 if (it == NULL)
1498 return -1;
1499
1500 while ((key = PyIter_Next(it)) != NULL) {
1501 if (set_discard_key(so, key) == -1) {
1502 Py_DECREF(it);
1503 Py_DECREF(key);
1504 return -1;
1505 }
1506 Py_DECREF(key);
1507 }
1508 Py_DECREF(it);
1509 if (PyErr_Occurred())
1510 return -1;
1511 }
1512 /* If more than 1/5 are dummies, then resize them away. */
1513 if ((so->fill - so->used) * 5 < so->mask)
1514 return 0;
1515 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001516}
1517
Raymond Hettingera690a992003-11-16 16:17:49 +00001518static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001519set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1524 PyObject *other = PyTuple_GET_ITEM(args, i);
1525 if (set_difference_update_internal(so, other) == -1)
1526 return NULL;
1527 }
1528 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001529}
1530
1531PyDoc_STRVAR(difference_update_doc,
1532"Remove all elements of another set from this set.");
1533
1534static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001535set_copy_and_difference(PySetObject *so, PyObject *other)
1536{
1537 PyObject *result;
1538
1539 result = set_copy(so);
1540 if (result == NULL)
1541 return NULL;
1542 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1543 return result;
1544 Py_DECREF(result);
1545 return NULL;
1546}
1547
1548static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001549set_difference(PySetObject *so, PyObject *other)
1550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 PyObject *result;
1552 setentry *entry;
1553 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001556 return set_copy_and_difference(so, other);
1557 }
1558
1559 /* If len(so) much more than len(other), it's more efficient to simply copy
1560 * so and then iterate other looking for common elements. */
1561 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1562 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 result = make_new_set_basetype(Py_TYPE(so), NULL);
1566 if (result == NULL)
1567 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 if (PyDict_CheckExact(other)) {
1570 while (set_next(so, &pos, &entry)) {
1571 setentry entrycopy;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001572 int rv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 entrycopy.hash = entry->hash;
1574 entrycopy.key = entry->key;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001575 rv = _PyDict_Contains(other, entry->key, entry->hash);
1576 if (rv < 0) {
1577 Py_DECREF(result);
1578 return NULL;
1579 }
1580 if (!rv) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1582 Py_DECREF(result);
1583 return NULL;
1584 }
1585 }
1586 }
1587 return result;
1588 }
1589
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001590 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 while (set_next(so, &pos, &entry)) {
1592 int rv = set_contains_entry((PySetObject *)other, entry);
1593 if (rv == -1) {
1594 Py_DECREF(result);
1595 return NULL;
1596 }
1597 if (!rv) {
1598 if (set_add_entry((PySetObject *)result, entry) == -1) {
1599 Py_DECREF(result);
1600 return NULL;
1601 }
1602 }
1603 }
1604 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001605}
1606
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001607static PyObject *
1608set_difference_multi(PySetObject *so, PyObject *args)
1609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 Py_ssize_t i;
1611 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if (PyTuple_GET_SIZE(args) == 0)
1614 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 other = PyTuple_GET_ITEM(args, 0);
1617 result = set_difference(so, other);
1618 if (result == NULL)
1619 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1622 other = PyTuple_GET_ITEM(args, i);
1623 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1624 Py_DECREF(result);
1625 return NULL;
1626 }
1627 }
1628 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001629}
1630
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001631PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001632"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001633\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001634(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001635static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001636set_sub(PySetObject *so, PyObject *other)
1637{
Brian Curtindfc80e32011-08-10 20:28:54 -05001638 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1639 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001641}
1642
1643static PyObject *
1644set_isub(PySetObject *so, PyObject *other)
1645{
Brian Curtindfc80e32011-08-10 20:28:54 -05001646 if (!PyAnySet_Check(other))
1647 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (set_difference_update_internal(so, other) == -1)
1649 return NULL;
1650 Py_INCREF(so);
1651 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001652}
1653
1654static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001655set_symmetric_difference_update(PySetObject *so, PyObject *other)
1656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 PySetObject *otherset;
1658 PyObject *key;
1659 Py_ssize_t pos = 0;
1660 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 if ((PyObject *)so == other)
1663 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 if (PyDict_CheckExact(other)) {
1666 PyObject *value;
1667 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001668 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1670 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001671
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001672 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 an_entry.hash = hash;
1674 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001677 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001678 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001681 if (rv == DISCARD_NOTFOUND) {
1682 if (set_add_entry(so, &an_entry) == -1) {
1683 Py_DECREF(key);
1684 return NULL;
1685 }
1686 }
Georg Brandl2d444492010-09-03 10:52:55 +00001687 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 }
1689 Py_RETURN_NONE;
1690 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 if (PyAnySet_Check(other)) {
1693 Py_INCREF(other);
1694 otherset = (PySetObject *)other;
1695 } else {
1696 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1697 if (otherset == NULL)
1698 return NULL;
1699 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 while (set_next(otherset, &pos, &entry)) {
1702 int rv = set_discard_entry(so, entry);
1703 if (rv == -1) {
1704 Py_DECREF(otherset);
1705 return NULL;
1706 }
1707 if (rv == DISCARD_NOTFOUND) {
1708 if (set_add_entry(so, entry) == -1) {
1709 Py_DECREF(otherset);
1710 return NULL;
1711 }
1712 }
1713 }
1714 Py_DECREF(otherset);
1715 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001716}
1717
1718PyDoc_STRVAR(symmetric_difference_update_doc,
1719"Update a set with the symmetric difference of itself and another.");
1720
1721static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001722set_symmetric_difference(PySetObject *so, PyObject *other)
1723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 PyObject *rv;
1725 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1728 if (otherset == NULL)
1729 return NULL;
1730 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1731 if (rv == NULL)
1732 return NULL;
1733 Py_DECREF(rv);
1734 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001735}
1736
1737PyDoc_STRVAR(symmetric_difference_doc,
1738"Return the symmetric difference of two sets as a new set.\n\
1739\n\
1740(i.e. all elements that are in exactly one of the sets.)");
1741
1742static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001743set_xor(PySetObject *so, PyObject *other)
1744{
Brian Curtindfc80e32011-08-10 20:28:54 -05001745 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1746 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001748}
1749
1750static PyObject *
1751set_ixor(PySetObject *so, PyObject *other)
1752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001754
Brian Curtindfc80e32011-08-10 20:28:54 -05001755 if (!PyAnySet_Check(other))
1756 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 result = set_symmetric_difference_update(so, other);
1758 if (result == NULL)
1759 return NULL;
1760 Py_DECREF(result);
1761 Py_INCREF(so);
1762 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001763}
1764
1765static PyObject *
1766set_issubset(PySetObject *so, PyObject *other)
1767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 setentry *entry;
1769 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (!PyAnySet_Check(other)) {
1772 PyObject *tmp, *result;
1773 tmp = make_new_set(&PySet_Type, other);
1774 if (tmp == NULL)
1775 return NULL;
1776 result = set_issubset(so, tmp);
1777 Py_DECREF(tmp);
1778 return result;
1779 }
1780 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1781 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 while (set_next(so, &pos, &entry)) {
1784 int rv = set_contains_entry((PySetObject *)other, entry);
1785 if (rv == -1)
1786 return NULL;
1787 if (!rv)
1788 Py_RETURN_FALSE;
1789 }
1790 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001791}
1792
1793PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1794
1795static PyObject *
1796set_issuperset(PySetObject *so, PyObject *other)
1797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 if (!PyAnySet_Check(other)) {
1801 tmp = make_new_set(&PySet_Type, other);
1802 if (tmp == NULL)
1803 return NULL;
1804 result = set_issuperset(so, tmp);
1805 Py_DECREF(tmp);
1806 return result;
1807 }
1808 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001809}
1810
1811PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1812
Raymond Hettingera690a992003-11-16 16:17:49 +00001813static PyObject *
1814set_richcompare(PySetObject *v, PyObject *w, int op)
1815{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001816 PyObject *r1;
1817 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001818
Brian Curtindfc80e32011-08-10 20:28:54 -05001819 if(!PyAnySet_Check(w))
1820 Py_RETURN_NOTIMPLEMENTED;
1821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 switch (op) {
1823 case Py_EQ:
1824 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1825 Py_RETURN_FALSE;
1826 if (v->hash != -1 &&
1827 ((PySetObject *)w)->hash != -1 &&
1828 v->hash != ((PySetObject *)w)->hash)
1829 Py_RETURN_FALSE;
1830 return set_issubset(v, w);
1831 case Py_NE:
1832 r1 = set_richcompare(v, w, Py_EQ);
1833 if (r1 == NULL)
1834 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001835 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001837 if (r2 < 0)
1838 return NULL;
1839 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 case Py_LE:
1841 return set_issubset(v, w);
1842 case Py_GE:
1843 return set_issuperset(v, w);
1844 case Py_LT:
1845 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1846 Py_RETURN_FALSE;
1847 return set_issubset(v, w);
1848 case Py_GT:
1849 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1850 Py_RETURN_FALSE;
1851 return set_issuperset(v, w);
1852 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001853 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001854}
1855
Raymond Hettingera690a992003-11-16 16:17:49 +00001856static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001857set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 if (set_add_key(so, key) == -1)
1860 return NULL;
1861 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001862}
1863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001865"Add an element to a set.\n\
1866\n\
1867This has no effect if the element is already present.");
1868
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001869static int
1870set_contains(PySetObject *so, PyObject *key)
1871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 PyObject *tmpkey;
1873 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 rv = set_contains_key(so, key);
1876 if (rv == -1) {
1877 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1878 return -1;
1879 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001880 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 if (tmpkey == NULL)
1882 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001883 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 Py_DECREF(tmpkey);
1885 }
1886 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001887}
1888
1889static PyObject *
1890set_direct_contains(PySetObject *so, PyObject *key)
1891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 result = set_contains(so, key);
1895 if (result == -1)
1896 return NULL;
1897 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001898}
1899
1900PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1901
Raymond Hettingera690a992003-11-16 16:17:49 +00001902static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001903set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 PyObject *tmpkey;
1906 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 rv = set_discard_key(so, key);
1909 if (rv == -1) {
1910 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1911 return NULL;
1912 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001913 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 if (tmpkey == NULL)
1915 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 Py_DECREF(tmpkey);
1918 if (rv == -1)
1919 return NULL;
1920 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001923 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 return NULL;
1925 }
1926 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001927}
1928
1929PyDoc_STRVAR(remove_doc,
1930"Remove an element from a set; it must be a member.\n\
1931\n\
1932If the element is not a member, raise a KeyError.");
1933
1934static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001935set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001936{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001937 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 rv = set_discard_key(so, key);
1941 if (rv == -1) {
1942 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1943 return NULL;
1944 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001945 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 if (tmpkey == NULL)
1947 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001948 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001950 if (rv == -1)
1951 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 }
1953 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001954}
1955
1956PyDoc_STRVAR(discard_doc,
1957"Remove an element from a set if it is a member.\n\
1958\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001960
1961static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001962set_reduce(PySetObject *so)
1963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001965 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 keys = PySequence_List((PyObject *)so);
1968 if (keys == NULL)
1969 goto done;
1970 args = PyTuple_Pack(1, keys);
1971 if (args == NULL)
1972 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001973 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 if (dict == NULL) {
1975 PyErr_Clear();
1976 dict = Py_None;
1977 Py_INCREF(dict);
1978 }
1979 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001980done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 Py_XDECREF(args);
1982 Py_XDECREF(keys);
1983 Py_XDECREF(dict);
1984 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001985}
1986
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001987static PyObject *
1988set_sizeof(PySetObject *so)
1989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 res = sizeof(PySetObject);
1993 if (so->table != so->smalltable)
1994 res = res + (so->mask + 1) * sizeof(setentry);
1995 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001996}
1997
1998PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001999static int
2000set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 if (!PyAnySet_Check(self))
2005 return -1;
2006 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2007 return -1;
2008 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2009 return -1;
2010 set_clear_internal(self);
2011 self->hash = -1;
2012 if (iterable == NULL)
2013 return 0;
2014 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002015}
2016
Raymond Hettingera690a992003-11-16 16:17:49 +00002017static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 set_len, /* sq_length */
2019 0, /* sq_concat */
2020 0, /* sq_repeat */
2021 0, /* sq_item */
2022 0, /* sq_slice */
2023 0, /* sq_ass_item */
2024 0, /* sq_ass_slice */
2025 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002026};
2027
2028/* set object ********************************************************/
2029
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002030#ifdef Py_DEBUG
2031static PyObject *test_c_api(PySetObject *so);
2032
2033PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2034All is well if assertions don't fail.");
2035#endif
2036
Raymond Hettingera690a992003-11-16 16:17:49 +00002037static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 {"add", (PyCFunction)set_add, METH_O,
2039 add_doc},
2040 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2041 clear_doc},
2042 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2043 contains_doc},
2044 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2045 copy_doc},
2046 {"discard", (PyCFunction)set_discard, METH_O,
2047 discard_doc},
2048 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2049 difference_doc},
2050 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2051 difference_update_doc},
2052 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2053 intersection_doc},
2054 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2055 intersection_update_doc},
2056 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2057 isdisjoint_doc},
2058 {"issubset", (PyCFunction)set_issubset, METH_O,
2059 issubset_doc},
2060 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2061 issuperset_doc},
2062 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2063 pop_doc},
2064 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2065 reduce_doc},
2066 {"remove", (PyCFunction)set_remove, METH_O,
2067 remove_doc},
2068 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2069 sizeof_doc},
2070 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2071 symmetric_difference_doc},
2072 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2073 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002074#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2076 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002077#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 {"union", (PyCFunction)set_union, METH_VARARGS,
2079 union_doc},
2080 {"update", (PyCFunction)set_update, METH_VARARGS,
2081 update_doc},
2082 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002083};
2084
2085static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 0, /*nb_add*/
2087 (binaryfunc)set_sub, /*nb_subtract*/
2088 0, /*nb_multiply*/
2089 0, /*nb_remainder*/
2090 0, /*nb_divmod*/
2091 0, /*nb_power*/
2092 0, /*nb_negative*/
2093 0, /*nb_positive*/
2094 0, /*nb_absolute*/
2095 0, /*nb_bool*/
2096 0, /*nb_invert*/
2097 0, /*nb_lshift*/
2098 0, /*nb_rshift*/
2099 (binaryfunc)set_and, /*nb_and*/
2100 (binaryfunc)set_xor, /*nb_xor*/
2101 (binaryfunc)set_or, /*nb_or*/
2102 0, /*nb_int*/
2103 0, /*nb_reserved*/
2104 0, /*nb_float*/
2105 0, /*nb_inplace_add*/
2106 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2107 0, /*nb_inplace_multiply*/
2108 0, /*nb_inplace_remainder*/
2109 0, /*nb_inplace_power*/
2110 0, /*nb_inplace_lshift*/
2111 0, /*nb_inplace_rshift*/
2112 (binaryfunc)set_iand, /*nb_inplace_and*/
2113 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2114 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002115};
2116
2117PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002118"set() -> new empty set object\n\
2119set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002120\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002121Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002122
2123PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2125 "set", /* tp_name */
2126 sizeof(PySetObject), /* tp_basicsize */
2127 0, /* tp_itemsize */
2128 /* methods */
2129 (destructor)set_dealloc, /* tp_dealloc */
2130 0, /* tp_print */
2131 0, /* tp_getattr */
2132 0, /* tp_setattr */
2133 0, /* tp_reserved */
2134 (reprfunc)set_repr, /* tp_repr */
2135 &set_as_number, /* tp_as_number */
2136 &set_as_sequence, /* tp_as_sequence */
2137 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002138 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 0, /* tp_call */
2140 0, /* tp_str */
2141 PyObject_GenericGetAttr, /* tp_getattro */
2142 0, /* tp_setattro */
2143 0, /* tp_as_buffer */
2144 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2145 Py_TPFLAGS_BASETYPE, /* tp_flags */
2146 set_doc, /* tp_doc */
2147 (traverseproc)set_traverse, /* tp_traverse */
2148 (inquiry)set_clear_internal, /* tp_clear */
2149 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002150 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2151 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 0, /* tp_iternext */
2153 set_methods, /* tp_methods */
2154 0, /* tp_members */
2155 0, /* tp_getset */
2156 0, /* tp_base */
2157 0, /* tp_dict */
2158 0, /* tp_descr_get */
2159 0, /* tp_descr_set */
2160 0, /* tp_dictoffset */
2161 (initproc)set_init, /* tp_init */
2162 PyType_GenericAlloc, /* tp_alloc */
2163 set_new, /* tp_new */
2164 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002165};
2166
2167/* frozenset object ********************************************************/
2168
2169
2170static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2172 contains_doc},
2173 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2174 copy_doc},
2175 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2176 difference_doc},
2177 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2178 intersection_doc},
2179 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2180 isdisjoint_doc},
2181 {"issubset", (PyCFunction)set_issubset, METH_O,
2182 issubset_doc},
2183 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2184 issuperset_doc},
2185 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2186 reduce_doc},
2187 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2188 sizeof_doc},
2189 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2190 symmetric_difference_doc},
2191 {"union", (PyCFunction)set_union, METH_VARARGS,
2192 union_doc},
2193 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002194};
2195
2196static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 0, /*nb_add*/
2198 (binaryfunc)set_sub, /*nb_subtract*/
2199 0, /*nb_multiply*/
2200 0, /*nb_remainder*/
2201 0, /*nb_divmod*/
2202 0, /*nb_power*/
2203 0, /*nb_negative*/
2204 0, /*nb_positive*/
2205 0, /*nb_absolute*/
2206 0, /*nb_bool*/
2207 0, /*nb_invert*/
2208 0, /*nb_lshift*/
2209 0, /*nb_rshift*/
2210 (binaryfunc)set_and, /*nb_and*/
2211 (binaryfunc)set_xor, /*nb_xor*/
2212 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002213};
2214
2215PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002216"frozenset() -> empty frozenset object\n\
2217frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002218\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002219Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002220
2221PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2223 "frozenset", /* tp_name */
2224 sizeof(PySetObject), /* tp_basicsize */
2225 0, /* tp_itemsize */
2226 /* methods */
2227 (destructor)set_dealloc, /* tp_dealloc */
2228 0, /* tp_print */
2229 0, /* tp_getattr */
2230 0, /* tp_setattr */
2231 0, /* tp_reserved */
2232 (reprfunc)set_repr, /* tp_repr */
2233 &frozenset_as_number, /* tp_as_number */
2234 &set_as_sequence, /* tp_as_sequence */
2235 0, /* tp_as_mapping */
2236 frozenset_hash, /* tp_hash */
2237 0, /* tp_call */
2238 0, /* tp_str */
2239 PyObject_GenericGetAttr, /* tp_getattro */
2240 0, /* tp_setattro */
2241 0, /* tp_as_buffer */
2242 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2243 Py_TPFLAGS_BASETYPE, /* tp_flags */
2244 frozenset_doc, /* tp_doc */
2245 (traverseproc)set_traverse, /* tp_traverse */
2246 (inquiry)set_clear_internal, /* tp_clear */
2247 (richcmpfunc)set_richcompare, /* tp_richcompare */
2248 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2249 (getiterfunc)set_iter, /* tp_iter */
2250 0, /* tp_iternext */
2251 frozenset_methods, /* tp_methods */
2252 0, /* tp_members */
2253 0, /* tp_getset */
2254 0, /* tp_base */
2255 0, /* tp_dict */
2256 0, /* tp_descr_get */
2257 0, /* tp_descr_set */
2258 0, /* tp_dictoffset */
2259 0, /* tp_init */
2260 PyType_GenericAlloc, /* tp_alloc */
2261 frozenset_new, /* tp_new */
2262 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002263};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002264
2265
2266/***** C API functions *************************************************/
2267
2268PyObject *
2269PySet_New(PyObject *iterable)
2270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002272}
2273
2274PyObject *
2275PyFrozenSet_New(PyObject *iterable)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002278}
2279
Neal Norwitz8c49c822006-03-04 18:41:19 +00002280Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002281PySet_Size(PyObject *anyset)
2282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 if (!PyAnySet_Check(anyset)) {
2284 PyErr_BadInternalCall();
2285 return -1;
2286 }
2287 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002288}
2289
2290int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002291PySet_Clear(PyObject *set)
2292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 if (!PySet_Check(set)) {
2294 PyErr_BadInternalCall();
2295 return -1;
2296 }
2297 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002298}
2299
2300int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002301PySet_Contains(PyObject *anyset, PyObject *key)
2302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 if (!PyAnySet_Check(anyset)) {
2304 PyErr_BadInternalCall();
2305 return -1;
2306 }
2307 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002308}
2309
2310int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002311PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 if (!PySet_Check(set)) {
2314 PyErr_BadInternalCall();
2315 return -1;
2316 }
2317 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002318}
2319
2320int
Christian Heimesfd66e512008-01-29 12:18:50 +00002321PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 if (!PySet_Check(anyset) &&
2324 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2325 PyErr_BadInternalCall();
2326 return -1;
2327 }
2328 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002329}
2330
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002331int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002332_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 if (!PyAnySet_Check(set)) {
2337 PyErr_BadInternalCall();
2338 return -1;
2339 }
2340 if (set_next((PySetObject *)set, pos, &entry) == 0)
2341 return 0;
2342 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002343 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002345}
2346
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002347PyObject *
2348PySet_Pop(PyObject *set)
2349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 if (!PySet_Check(set)) {
2351 PyErr_BadInternalCall();
2352 return NULL;
2353 }
2354 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002355}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002356
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002357int
2358_PySet_Update(PyObject *set, PyObject *iterable)
2359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 if (!PySet_Check(set)) {
2361 PyErr_BadInternalCall();
2362 return -1;
2363 }
2364 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002365}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002366
Raymond Hettingere259f132013-12-15 11:56:14 -08002367/* Exported for the gdb plugin's benefit. */
2368PyObject *_PySet_Dummy = dummy;
2369
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002370#ifdef Py_DEBUG
2371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002373 Returns True and original set is restored. */
2374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375#define assertRaises(call_return_value, exception) \
2376 do { \
2377 assert(call_return_value); \
2378 assert(PyErr_ExceptionMatches(exception)); \
2379 PyErr_Clear(); \
2380 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002381
2382static PyObject *
2383test_c_api(PySetObject *so)
2384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 Py_ssize_t count;
2386 char *s;
2387 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002388 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002390 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* Verify preconditions */
2394 assert(PyAnySet_Check(ob));
2395 assert(PyAnySet_CheckExact(ob));
2396 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 /* so.clear(); so |= set("abc"); */
2399 str = PyUnicode_FromString("abc");
2400 if (str == NULL)
2401 return NULL;
2402 set_clear_internal(so);
2403 if (set_update_internal(so, str) == -1) {
2404 Py_DECREF(str);
2405 return NULL;
2406 }
2407 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* Exercise type/size checks */
2410 assert(PySet_Size(ob) == 3);
2411 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Raise TypeError for non-iterable constructor arguments */
2414 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2415 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 /* Raise TypeError for unhashable key */
2418 dup = PySet_New(ob);
2419 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2420 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2421 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 /* Exercise successful pop, contains, add, and discard */
2424 elem = PySet_Pop(ob);
2425 assert(PySet_Contains(ob, elem) == 0);
2426 assert(PySet_GET_SIZE(ob) == 2);
2427 assert(PySet_Add(ob, elem) == 0);
2428 assert(PySet_Contains(ob, elem) == 1);
2429 assert(PySet_GET_SIZE(ob) == 3);
2430 assert(PySet_Discard(ob, elem) == 1);
2431 assert(PySet_GET_SIZE(ob) == 2);
2432 assert(PySet_Discard(ob, elem) == 0);
2433 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* Exercise clear */
2436 dup2 = PySet_New(dup);
2437 assert(PySet_Clear(dup2) == 0);
2438 assert(PySet_Size(dup2) == 0);
2439 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 /* Raise SystemError on clear or update of frozen set */
2442 f = PyFrozenSet_New(dup);
2443 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2444 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2445 assert(PySet_Add(f, elem) == 0);
2446 Py_INCREF(f);
2447 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2448 Py_DECREF(f);
2449 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 /* Exercise direct iteration */
2452 i = 0, count = 0;
2453 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2454 s = _PyUnicode_AsString(x);
2455 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2456 count++;
2457 }
2458 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* Exercise updates */
2461 dup2 = PySet_New(NULL);
2462 assert(_PySet_Update(dup2, dup) == 0);
2463 assert(PySet_Size(dup2) == 3);
2464 assert(_PySet_Update(dup2, dup) == 0);
2465 assert(PySet_Size(dup2) == 3);
2466 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 /* Raise SystemError when self argument is not a set or frozenset. */
2469 t = PyTuple_New(0);
2470 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2471 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2472 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 /* Raise SystemError when self argument is not a set. */
2475 f = PyFrozenSet_New(dup);
2476 assert(PySet_Size(f) == 3);
2477 assert(PyFrozenSet_CheckExact(f));
2478 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2479 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2480 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* Raise KeyError when popping from an empty set */
2483 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2484 Py_DECREF(ob);
2485 assert(PySet_GET_SIZE(ob) == 0);
2486 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 /* Restore the set from the copy using the PyNumber API */
2489 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2490 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 /* Verify constructors accept NULL arguments */
2493 f = PySet_New(NULL);
2494 assert(f != NULL);
2495 assert(PySet_GET_SIZE(f) == 0);
2496 Py_DECREF(f);
2497 f = PyFrozenSet_New(NULL);
2498 assert(f != NULL);
2499 assert(PyFrozenSet_CheckExact(f));
2500 assert(PySet_GET_SIZE(f) == 0);
2501 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 Py_DECREF(elem);
2504 Py_DECREF(dup);
2505 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002506}
2507
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002508#undef assertRaises
2509
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002510#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002511
2512/***** Dummy Struct *************************************************/
2513
2514static PyObject *
2515dummy_repr(PyObject *op)
2516{
2517 return PyUnicode_FromString("<dummy key>");
2518}
2519
2520static void
2521dummy_dealloc(PyObject* ignore)
2522{
2523 Py_FatalError("deallocating <dummy key>");
2524}
2525
2526static PyTypeObject _PySetDummy_Type = {
2527 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2528 "<dummy key> type",
2529 0,
2530 0,
2531 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2532 0, /*tp_print*/
2533 0, /*tp_getattr*/
2534 0, /*tp_setattr*/
2535 0, /*tp_reserved*/
2536 dummy_repr, /*tp_repr*/
2537 0, /*tp_as_number*/
2538 0, /*tp_as_sequence*/
2539 0, /*tp_as_mapping*/
2540 0, /*tp_hash */
2541 0, /*tp_call */
2542 0, /*tp_str */
2543 0, /*tp_getattro */
2544 0, /*tp_setattro */
2545 0, /*tp_as_buffer */
2546 Py_TPFLAGS_DEFAULT, /*tp_flags */
2547};
2548
2549static PyObject _dummy_struct = {
2550 _PyObject_EXTRA_INIT
2551 2, &_PySetDummy_Type
2552};
2553