blob: ddf6822519b5bb8789d1dfc7554975fd45d7978c [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 Hettingerbd9b2002015-01-18 16:10:30 -08007 Copyright (c) 2003-2015 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) {
Raymond Hettinger426d9952014-05-18 21:40:20 +010068 if (entry->hash == hash && entry->key != dummy) { /* dummy match unlikely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070069 PyObject *startkey = entry->key;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080070 if (startkey == key)
71 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 Py_INCREF(startkey);
73 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
74 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010075 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010077 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010079 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070080 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;
Raymond Hettingera35adf52013-09-02 03:23:21 -070089 if (entry->hash == hash && entry->key != dummy) {
90 PyObject *startkey = entry->key;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080091 if (startkey == key)
92 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070093 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. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100138 if (!PyUnicode_CheckExact(key)) { /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 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 Hettinger08e3dc02014-12-26 20:14:00 -0800148 if (entry->hash == hash
149 && (entry->key == key
150 || (entry->key != dummy /* unlikely */
151 && unicode_eq(entry->key, key)))) /* likely */
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;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800160 if (entry->hash == hash
161 && (entry->key == key
162 || (entry->key != dummy /* unlikely */
163 && unicode_eq(entry->key, key)))) /* likely */
Raymond Hettingera35adf52013-09-02 03:23:21 -0700164 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 Hettinger08e3dc02014-12-26 20:14:00 -0800199 for (j = 0 ; j <= LINEAR_PROBES ; j++) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700200 entry = &table[(i + j) & mask];
201 if (entry->key == NULL)
202 goto found_null;
203 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700204 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700205 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700207 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 entry->key = key;
209 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700210 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000212}
213
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700214/* ======== End logic for probing the hash table ========================== */
215/* ======================================================================== */
216
217
218/*
219Internal routine to insert a new key into the table.
220Used by the public insert routine.
221Eats a reference to key.
222*/
223static int
224set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
225{
226 setentry *entry;
227
228 assert(so->lookup != NULL);
229 entry = so->lookup(so, key, hash);
230 if (entry == NULL)
231 return -1;
232 if (entry->key == NULL) {
233 /* UNUSED */
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700234 entry->key = key;
235 entry->hash = hash;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800236 so->fill++;
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700237 so->used++;
238 } else if (entry->key == dummy) {
239 /* DUMMY */
240 entry->key = key;
241 entry->hash = hash;
242 so->used++;
243 } else {
244 /* ACTIVE */
245 Py_DECREF(key);
246 }
247 return 0;
248}
249
Thomas Wouters89f507f2006-12-13 04:49:30 +0000250/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000251Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000252keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000253actually be smaller than the old one.
254*/
255static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 Py_ssize_t newsize;
259 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800260 Py_ssize_t oldfill = so->fill;
261 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 int is_oldtable_malloced;
263 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100268 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 for (newsize = PySet_MINSIZE;
270 newsize <= minused && newsize > 0;
271 newsize <<= 1)
272 ;
273 if (newsize <= 0) {
274 PyErr_NoMemory();
275 return -1;
276 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 /* Get space for a new table. */
279 oldtable = so->table;
280 assert(oldtable != NULL);
281 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 if (newsize == PySet_MINSIZE) {
284 /* A large table is shrinking, or we can't get any smaller. */
285 newtable = so->smalltable;
286 if (newtable == oldtable) {
287 if (so->fill == so->used) {
288 /* No dummies, so no point doing anything. */
289 return 0;
290 }
291 /* We're not going to resize it, but rebuild the
292 table anyway to purge old dummy entries.
293 Subtle: This is *necessary* if fill==size,
294 as set_lookkey needs at least one virgin slot to
295 terminate failing searches. If fill < size, it's
296 merely desirable, as dummies slow searches. */
297 assert(so->fill > so->used);
298 memcpy(small_copy, oldtable, sizeof(small_copy));
299 oldtable = small_copy;
300 }
301 }
302 else {
303 newtable = PyMem_NEW(setentry, newsize);
304 if (newtable == NULL) {
305 PyErr_NoMemory();
306 return -1;
307 }
308 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Make the set empty, using the new table. */
311 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800314 so->used = 0;
315 so->mask = newsize - 1;
316 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 /* Copy the data over; this is refcount-neutral for active entries;
319 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800320 if (oldfill == oldused) {
321 for (entry = oldtable; oldused > 0; entry++) {
322 if (entry->key != NULL) {
323 oldused--;
324 set_insert_clean(so, entry->key, entry->hash);
325 }
326 }
327 } else {
328 for (entry = oldtable; oldused > 0; entry++) {
329 if (entry->key != NULL && entry->key != dummy) {
330 oldused--;
331 set_insert_clean(so, entry->key, entry->hash);
332 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 }
334 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 if (is_oldtable_malloced)
337 PyMem_DEL(oldtable);
338 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000339}
340
Raymond Hettingerc991db22005-08-11 07:58:45 +0000341/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
342
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000343static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200344set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000345{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200346 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000347 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100348 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 assert(so->fill <= so->mask); /* at least one empty slot */
351 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000352 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100353 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000354 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 return -1;
356 }
357 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
358 return 0;
359 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000360}
361
362static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200363set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000364{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800365 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200366 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200369 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 hash = PyObject_Hash(key);
371 if (hash == -1)
372 return -1;
373 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800374 entry.key = key;
375 entry.hash = hash;
376 return set_add_entry(so, &entry);
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)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800384{
385 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000387
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000388 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (entry == NULL)
390 return -1;
391 if (entry->key == NULL || entry->key == dummy)
392 return DISCARD_NOTFOUND;
393 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 entry->key = dummy;
395 so->used--;
396 Py_DECREF(old_key);
397 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000398}
399
400static int
401set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800403 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200404 Py_hash_t hash;
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 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800414 entry.key = key;
415 entry.hash = hash;
416 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000417}
418
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700419static void
420set_empty_to_minsize(PySetObject *so)
421{
422 memset(so->smalltable, 0, sizeof(so->smalltable));
423 so->fill = 0;
424 so->used = 0;
425 so->mask = PySet_MINSIZE - 1;
426 so->table = so->smalltable;
427 so->hash = -1;
428}
429
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000430static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431set_clear_internal(PySetObject *so)
432{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800433 setentry *entry;
434 setentry *table = so->table;
435 Py_ssize_t fill = so->fill;
436 Py_ssize_t used = so->used;
437 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000439
Raymond Hettinger583cd032013-09-07 22:06:35 -0700440 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 /* This is delicate. During the process of clearing the set,
444 * decrefs can cause the set to mutate. To avoid fatal confusion
445 * (voice of experience), we have to make the set empty before
446 * clearing the slots, and never refer to anything via so->ref while
447 * clearing.
448 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700450 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 else if (fill > 0) {
453 /* It's a small table with something that needs to be cleared.
454 * Afraid the only safe way is to copy the set entries into
455 * another small table first.
456 */
457 memcpy(small_copy, table, sizeof(small_copy));
458 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700459 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 }
461 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 /* Now we can finally clear things. If C had refcounts, we could
464 * assert that the refcount on table is 1 now, i.e. that this function
465 * has unique access to it, so decref side-effects can't alter it.
466 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800467 for (entry = table; used > 0; entry++) {
468 if (entry->key && entry->key != dummy) {
469 used--;
470 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (table_is_malloced)
475 PyMem_DEL(table);
476 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000477}
478
479/*
480 * Iterate over a set table. Use like so:
481 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000482 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000483 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000484 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000485 * while (set_next(yourset, &pos, &entry)) {
486 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487 * }
488 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000489 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491 */
492static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000493set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 Py_ssize_t i;
496 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200497 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 assert (PyAnySet_Check(so));
500 i = *pos_ptr;
501 assert(i >= 0);
502 table = so->table;
503 mask = so->mask;
504 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
505 i++;
506 *pos_ptr = i+1;
507 if (i > mask)
508 return 0;
509 assert(table[i].key != NULL);
510 *entry_ptr = &table[i];
511 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512}
513
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000514static void
515set_dealloc(PySetObject *so)
516{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200517 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800518 Py_ssize_t used = so->used;
519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 PyObject_GC_UnTrack(so);
521 Py_TRASHCAN_SAFE_BEGIN(so)
522 if (so->weakreflist != NULL)
523 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000524
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800525 for (entry = so->table; used > 0; entry++) {
526 if (entry->key && entry->key != dummy) {
527 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700528 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 }
530 }
531 if (so->table != so->smalltable)
532 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700533 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000535}
536
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000537static PyObject *
538set_repr(PySetObject *so)
539{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200540 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 if (status != 0) {
544 if (status < 0)
545 return NULL;
546 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
547 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 /* shortcut for the empty set */
550 if (!so->used) {
551 Py_ReprLeave((PyObject*)so);
552 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
553 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 keys = PySequence_List((PyObject *)so);
556 if (keys == NULL)
557 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000558
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200559 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 listrepr = PyObject_Repr(keys);
561 Py_DECREF(keys);
562 if (listrepr == NULL)
563 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200564 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200566 if (tmp == NULL)
567 goto done;
568 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200569
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200570 if (Py_TYPE(so) != &PySet_Type)
571 result = PyUnicode_FromFormat("%s({%U})",
572 Py_TYPE(so)->tp_name,
573 listrepr);
574 else
575 result = PyUnicode_FromFormat("{%U}", listrepr);
576 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000577done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 Py_ReprLeave((PyObject*)so);
579 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000580}
581
Martin v. Löwis18e16552006-02-15 17:27:45 +0000582static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000583set_len(PyObject *so)
584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000586}
587
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000588static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000589set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000592 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100593 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200594 Py_ssize_t i;
595 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 assert (PyAnySet_Check(so));
598 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 other = (PySetObject*)otherset;
601 if (other == so || other->used == 0)
602 /* a.update(a) or a.update({}); nothing to do */
603 return 0;
604 /* Do one big resize at the start, rather than
605 * incrementally resizing as we insert new keys. Expect
606 * that there will be no (or few) overlapping keys.
607 */
608 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
609 if (set_table_resize(so, (so->used + other->used)*2) != 0)
610 return -1;
611 }
612 for (i = 0; i <= other->mask; i++) {
613 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000614 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100615 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000616 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500617 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000618 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100619 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000620 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 return -1;
622 }
623 }
624 }
625 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000626}
627
628static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000629set_contains_entry(PySetObject *so, setentry *entry)
630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 PyObject *key;
632 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000633
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000634 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (lu_entry == NULL)
636 return -1;
637 key = lu_entry->key;
638 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000639}
640
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800641static int
642set_contains_key(PySetObject *so, PyObject *key)
643{
644 setentry entry;
645 Py_hash_t hash;
646
647 if (!PyUnicode_CheckExact(key) ||
648 (hash = ((PyASCIIObject *) key)->hash) == -1) {
649 hash = PyObject_Hash(key);
650 if (hash == -1)
651 return -1;
652 }
653 entry.key = key;
654 entry.hash = hash;
655 return set_contains_entry(so, &entry);
656}
657
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000658static PyObject *
659set_pop(PySetObject *so)
660{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800661 /* Make sure the search finger is in bounds */
662 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200663 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 assert (PyAnySet_Check(so));
667 if (so->used == 0) {
668 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
669 return NULL;
670 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000671
Raymond Hettinger1202a472015-01-18 13:12:42 -0800672 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
673 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800674 if (i > so->mask)
675 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 }
677 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 entry->key = dummy;
679 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800680 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000682}
683
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000684PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
685Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000686
687static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000688set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 Py_ssize_t pos = 0;
691 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 while (set_next(so, &pos, &entry))
694 Py_VISIT(entry->key);
695 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000696}
697
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000698static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000699frozenset_hash(PyObject *self)
700{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800701 /* Most of the constants in this hash algorithm are randomly choosen
702 large primes with "interesting bit patterns" and that passed
703 tests for good collision statistics on a variety of problematic
704 datasets such as:
705
706 ps = []
707 for r in range(21):
708 ps += itertools.combinations(range(20), r)
709 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
710
711 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800713 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 setentry *entry;
715 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 if (so->hash != -1)
718 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000719
Mark Dickinson57e683e2011-09-24 18:18:40 +0100720 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 while (set_next(so, &pos, &entry)) {
722 /* Work to increase the bit dispersion for closely spaced hash
723 values. The is important because some use cases have many
724 combinations of a small number of elements with nearby
725 hashes so that many distinct combinations collapse to only
726 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000727 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800728 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800730 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500731 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800732 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200733 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800734 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 so->hash = hash;
736 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000737}
738
Raymond Hettingera9d99362005-08-05 00:01:15 +0000739/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000740
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000741typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 PyObject_HEAD
743 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
744 Py_ssize_t si_used;
745 Py_ssize_t si_pos;
746 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000747} setiterobject;
748
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000749static void
750setiter_dealloc(setiterobject *si)
751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 Py_XDECREF(si->si_set);
753 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000754}
755
756static int
757setiter_traverse(setiterobject *si, visitproc visit, void *arg)
758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 Py_VISIT(si->si_set);
760 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000761}
762
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000763static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000764setiter_len(setiterobject *si)
765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 Py_ssize_t len = 0;
767 if (si->si_set != NULL && si->si_used == si->si_set->used)
768 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000769 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000770}
771
Armin Rigof5b3e362006-02-11 21:32:43 +0000772PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000773
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000774static PyObject *setiter_iternext(setiterobject *si);
775
776static PyObject *
777setiter_reduce(setiterobject *si)
778{
779 PyObject *list;
780 setiterobject tmp;
781
782 list = PyList_New(0);
783 if (!list)
784 return NULL;
785
Ezio Melotti0e1af282012-09-28 16:43:40 +0300786 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000787 tmp = *si;
788 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300789
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000790 /* iterate the temporary into a list */
791 for(;;) {
792 PyObject *element = setiter_iternext(&tmp);
793 if (element) {
794 if (PyList_Append(list, element)) {
795 Py_DECREF(element);
796 Py_DECREF(list);
797 Py_XDECREF(tmp.si_set);
798 return NULL;
799 }
800 Py_DECREF(element);
801 } else
802 break;
803 }
804 Py_XDECREF(tmp.si_set);
805 /* check for error */
806 if (tmp.si_set != NULL) {
807 /* we have an error */
808 Py_DECREF(list);
809 return NULL;
810 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200811 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000812}
813
814PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
815
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000816static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000818 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000820};
821
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000822static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200825 Py_ssize_t i, mask;
826 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 if (so == NULL)
830 return NULL;
831 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 if (si->si_used != so->used) {
834 PyErr_SetString(PyExc_RuntimeError,
835 "Set changed size during iteration");
836 si->si_used = -1; /* Make this state sticky */
837 return NULL;
838 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 i = si->si_pos;
841 assert(i>=0);
842 entry = so->table;
843 mask = so->mask;
844 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
845 i++;
846 si->si_pos = i+1;
847 if (i > mask)
848 goto fail;
849 si->len--;
850 key = entry[i].key;
851 Py_INCREF(key);
852 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853
854fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 Py_DECREF(so);
856 si->si_set = NULL;
857 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000858}
859
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000860PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 PyVarObject_HEAD_INIT(&PyType_Type, 0)
862 "set_iterator", /* tp_name */
863 sizeof(setiterobject), /* tp_basicsize */
864 0, /* tp_itemsize */
865 /* methods */
866 (destructor)setiter_dealloc, /* tp_dealloc */
867 0, /* tp_print */
868 0, /* tp_getattr */
869 0, /* tp_setattr */
870 0, /* tp_reserved */
871 0, /* tp_repr */
872 0, /* tp_as_number */
873 0, /* tp_as_sequence */
874 0, /* tp_as_mapping */
875 0, /* tp_hash */
876 0, /* tp_call */
877 0, /* tp_str */
878 PyObject_GenericGetAttr, /* tp_getattro */
879 0, /* tp_setattro */
880 0, /* tp_as_buffer */
881 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
882 0, /* tp_doc */
883 (traverseproc)setiter_traverse, /* tp_traverse */
884 0, /* tp_clear */
885 0, /* tp_richcompare */
886 0, /* tp_weaklistoffset */
887 PyObject_SelfIter, /* tp_iter */
888 (iternextfunc)setiter_iternext, /* tp_iternext */
889 setiter_methods, /* tp_methods */
890 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891};
892
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000893static PyObject *
894set_iter(PySetObject *so)
895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
897 if (si == NULL)
898 return NULL;
899 Py_INCREF(so);
900 si->si_set = so;
901 si->si_used = so->used;
902 si->si_pos = 0;
903 si->len = so->used;
904 _PyObject_GC_TRACK(si);
905 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000906}
907
Raymond Hettingerd7946662005-08-01 21:39:29 +0000908static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000909set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 if (PyAnySet_Check(other))
914 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 if (PyDict_CheckExact(other)) {
917 PyObject *value;
918 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000919 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 /* Do one big resize at the start, rather than
923 * incrementally resizing as we insert new keys. Expect
924 * that there will be no (or few) overlapping keys.
925 */
926 if (dictsize == -1)
927 return -1;
928 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
929 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
930 return -1;
931 }
932 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
933 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 an_entry.hash = hash;
936 an_entry.key = key;
937 if (set_add_entry(so, &an_entry) == -1)
938 return -1;
939 }
940 return 0;
941 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 it = PyObject_GetIter(other);
944 if (it == NULL)
945 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 while ((key = PyIter_Next(it)) != NULL) {
948 if (set_add_key(so, key) == -1) {
949 Py_DECREF(it);
950 Py_DECREF(key);
951 return -1;
952 }
953 Py_DECREF(key);
954 }
955 Py_DECREF(it);
956 if (PyErr_Occurred())
957 return -1;
958 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000959}
960
961static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000962set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
967 PyObject *other = PyTuple_GET_ITEM(args, i);
968 if (set_update_internal(so, other) == -1)
969 return NULL;
970 }
971 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000972}
973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000975"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000976
Raymond Hettinger426d9952014-05-18 21:40:20 +0100977/* XXX Todo:
978 If aligned memory allocations become available, make the
979 set object 64 byte aligned so that most of the fields
980 can be retrieved or updated in a single cache line.
981*/
982
Raymond Hettingera38123e2003-11-24 22:18:49 +0000983static PyObject *
984make_new_set(PyTypeObject *type, PyObject *iterable)
985{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200986 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700989 so = (PySetObject *)type->tp_alloc(type, 0);
990 if (so == NULL)
991 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000992
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700993 so->fill = 0;
994 so->used = 0;
995 so->mask = PySet_MINSIZE - 1;
996 so->table = so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 so->lookup = set_lookkey_unicode;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700998 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800999 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (iterable != NULL) {
1003 if (set_update_internal(so, iterable) == -1) {
1004 Py_DECREF(so);
1005 return NULL;
1006 }
1007 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001010}
1011
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001012static PyObject *
1013make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1016 if (PyType_IsSubtype(type, &PySet_Type))
1017 type = &PySet_Type;
1018 else
1019 type = &PyFrozenSet_Type;
1020 }
1021 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001022}
1023
Raymond Hettingerd7946662005-08-01 21:39:29 +00001024/* The empty frozenset is a singleton */
1025static PyObject *emptyfrozenset = NULL;
1026
Raymond Hettingera690a992003-11-16 16:17:49 +00001027static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001028frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1033 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1036 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 if (type != &PyFrozenSet_Type)
1039 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (iterable != NULL) {
1042 /* frozenset(f) is idempotent */
1043 if (PyFrozenSet_CheckExact(iterable)) {
1044 Py_INCREF(iterable);
1045 return iterable;
1046 }
1047 result = make_new_set(type, iterable);
1048 if (result == NULL || PySet_GET_SIZE(result))
1049 return result;
1050 Py_DECREF(result);
1051 }
1052 /* The empty frozenset is a singleton */
1053 if (emptyfrozenset == NULL)
1054 emptyfrozenset = make_new_set(type, NULL);
1055 Py_XINCREF(emptyfrozenset);
1056 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001057}
1058
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001059int
1060PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001061{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001062 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001063}
1064
1065void
1066PySet_Fini(void)
1067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001069}
1070
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001071static PyObject *
1072set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1075 return NULL;
1076
1077 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001078}
1079
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001080/* set_swap_bodies() switches the contents of any two sets by moving their
1081 internal data pointers and, if needed, copying the internal smalltables.
1082 Semantically equivalent to:
1083
1084 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1085
1086 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001088 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001090 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001091*/
1092
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001093static void
1094set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 Py_ssize_t t;
1097 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001098 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001100 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 t = a->fill; a->fill = b->fill; b->fill = t;
1103 t = a->used; a->used = b->used; b->used = t;
1104 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 u = a->table;
1107 if (a->table == a->smalltable)
1108 u = b->smalltable;
1109 a->table = b->table;
1110 if (b->table == b->smalltable)
1111 a->table = a->smalltable;
1112 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (a->table == a->smalltable || b->table == b->smalltable) {
1117 memcpy(tab, a->smalltable, sizeof(tab));
1118 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1119 memcpy(b->smalltable, tab, sizeof(tab));
1120 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1123 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1124 h = a->hash; a->hash = b->hash; b->hash = h;
1125 } else {
1126 a->hash = -1;
1127 b->hash = -1;
1128 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001129}
1130
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001131static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001132set_copy(PySetObject *so)
1133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001135}
1136
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001137static PyObject *
1138frozenset_copy(PySetObject *so)
1139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 if (PyFrozenSet_CheckExact(so)) {
1141 Py_INCREF(so);
1142 return (PyObject *)so;
1143 }
1144 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001145}
1146
Raymond Hettingera690a992003-11-16 16:17:49 +00001147PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1148
1149static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001150set_clear(PySetObject *so)
1151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 set_clear_internal(so);
1153 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001154}
1155
1156PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1157
1158static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001159set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 PySetObject *result;
1162 PyObject *other;
1163 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 result = (PySetObject *)set_copy(so);
1166 if (result == NULL)
1167 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1170 other = PyTuple_GET_ITEM(args, i);
1171 if ((PyObject *)so == other)
1172 continue;
1173 if (set_update_internal(result, other) == -1) {
1174 Py_DECREF(result);
1175 return NULL;
1176 }
1177 }
1178 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001179}
1180
1181PyDoc_STRVAR(union_doc,
1182 "Return the union of sets as a new set.\n\
1183\n\
1184(i.e. all elements that are in either set.)");
1185
1186static PyObject *
1187set_or(PySetObject *so, PyObject *other)
1188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001190
Brian Curtindfc80e32011-08-10 20:28:54 -05001191 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1192 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 result = (PySetObject *)set_copy(so);
1195 if (result == NULL)
1196 return NULL;
1197 if ((PyObject *)so == other)
1198 return (PyObject *)result;
1199 if (set_update_internal(result, other) == -1) {
1200 Py_DECREF(result);
1201 return NULL;
1202 }
1203 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001204}
1205
Raymond Hettingera690a992003-11-16 16:17:49 +00001206static PyObject *
1207set_ior(PySetObject *so, PyObject *other)
1208{
Brian Curtindfc80e32011-08-10 20:28:54 -05001209 if (!PyAnySet_Check(other))
1210 Py_RETURN_NOTIMPLEMENTED;
1211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 if (set_update_internal(so, other) == -1)
1213 return NULL;
1214 Py_INCREF(so);
1215 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001216}
1217
1218static PyObject *
1219set_intersection(PySetObject *so, PyObject *other)
1220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 PySetObject *result;
1222 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 if ((PyObject *)so == other)
1225 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1228 if (result == NULL)
1229 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 if (PyAnySet_Check(other)) {
1232 Py_ssize_t pos = 0;
1233 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1236 tmp = (PyObject *)so;
1237 so = (PySetObject *)other;
1238 other = tmp;
1239 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 while (set_next((PySetObject *)other, &pos, &entry)) {
1242 int rv = set_contains_entry(so, entry);
1243 if (rv == -1) {
1244 Py_DECREF(result);
1245 return NULL;
1246 }
1247 if (rv) {
1248 if (set_add_entry(result, entry) == -1) {
1249 Py_DECREF(result);
1250 return NULL;
1251 }
1252 }
1253 }
1254 return (PyObject *)result;
1255 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 it = PyObject_GetIter(other);
1258 if (it == NULL) {
1259 Py_DECREF(result);
1260 return NULL;
1261 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 while ((key = PyIter_Next(it)) != NULL) {
1264 int rv;
1265 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001266 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (hash == -1) {
1269 Py_DECREF(it);
1270 Py_DECREF(result);
1271 Py_DECREF(key);
1272 return NULL;
1273 }
1274 entry.hash = hash;
1275 entry.key = key;
1276 rv = set_contains_entry(so, &entry);
1277 if (rv == -1) {
1278 Py_DECREF(it);
1279 Py_DECREF(result);
1280 Py_DECREF(key);
1281 return NULL;
1282 }
1283 if (rv) {
1284 if (set_add_entry(result, &entry) == -1) {
1285 Py_DECREF(it);
1286 Py_DECREF(result);
1287 Py_DECREF(key);
1288 return NULL;
1289 }
1290 }
1291 Py_DECREF(key);
1292 }
1293 Py_DECREF(it);
1294 if (PyErr_Occurred()) {
1295 Py_DECREF(result);
1296 return NULL;
1297 }
1298 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001299}
1300
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001301static PyObject *
1302set_intersection_multi(PySetObject *so, PyObject *args)
1303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 Py_ssize_t i;
1305 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (PyTuple_GET_SIZE(args) == 0)
1308 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 Py_INCREF(so);
1311 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1312 PyObject *other = PyTuple_GET_ITEM(args, i);
1313 PyObject *newresult = set_intersection((PySetObject *)result, other);
1314 if (newresult == NULL) {
1315 Py_DECREF(result);
1316 return NULL;
1317 }
1318 Py_DECREF(result);
1319 result = newresult;
1320 }
1321 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001322}
1323
Raymond Hettingera690a992003-11-16 16:17:49 +00001324PyDoc_STRVAR(intersection_doc,
1325"Return the intersection of two sets as a new set.\n\
1326\n\
1327(i.e. all elements that are in both sets.)");
1328
1329static PyObject *
1330set_intersection_update(PySetObject *so, PyObject *other)
1331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 tmp = set_intersection(so, other);
1335 if (tmp == NULL)
1336 return NULL;
1337 set_swap_bodies(so, (PySetObject *)tmp);
1338 Py_DECREF(tmp);
1339 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001340}
1341
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001342static PyObject *
1343set_intersection_update_multi(PySetObject *so, PyObject *args)
1344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 tmp = set_intersection_multi(so, args);
1348 if (tmp == NULL)
1349 return NULL;
1350 set_swap_bodies(so, (PySetObject *)tmp);
1351 Py_DECREF(tmp);
1352 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001353}
1354
Raymond Hettingera690a992003-11-16 16:17:49 +00001355PyDoc_STRVAR(intersection_update_doc,
1356"Update a set with the intersection of itself and another.");
1357
1358static PyObject *
1359set_and(PySetObject *so, PyObject *other)
1360{
Brian Curtindfc80e32011-08-10 20:28:54 -05001361 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1362 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001364}
1365
1366static PyObject *
1367set_iand(PySetObject *so, PyObject *other)
1368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001370
Brian Curtindfc80e32011-08-10 20:28:54 -05001371 if (!PyAnySet_Check(other))
1372 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 result = set_intersection_update(so, other);
1374 if (result == NULL)
1375 return NULL;
1376 Py_DECREF(result);
1377 Py_INCREF(so);
1378 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001379}
1380
Guido van Rossum58da9312007-11-10 23:39:45 +00001381static PyObject *
1382set_isdisjoint(PySetObject *so, PyObject *other)
1383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if ((PyObject *)so == other) {
1387 if (PySet_GET_SIZE(so) == 0)
1388 Py_RETURN_TRUE;
1389 else
1390 Py_RETURN_FALSE;
1391 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 if (PyAnySet_CheckExact(other)) {
1394 Py_ssize_t pos = 0;
1395 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1398 tmp = (PyObject *)so;
1399 so = (PySetObject *)other;
1400 other = tmp;
1401 }
1402 while (set_next((PySetObject *)other, &pos, &entry)) {
1403 int rv = set_contains_entry(so, entry);
1404 if (rv == -1)
1405 return NULL;
1406 if (rv)
1407 Py_RETURN_FALSE;
1408 }
1409 Py_RETURN_TRUE;
1410 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 it = PyObject_GetIter(other);
1413 if (it == NULL)
1414 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 while ((key = PyIter_Next(it)) != NULL) {
1417 int rv;
1418 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001419 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 if (hash == -1) {
1422 Py_DECREF(key);
1423 Py_DECREF(it);
1424 return NULL;
1425 }
1426 entry.hash = hash;
1427 entry.key = key;
1428 rv = set_contains_entry(so, &entry);
1429 Py_DECREF(key);
1430 if (rv == -1) {
1431 Py_DECREF(it);
1432 return NULL;
1433 }
1434 if (rv) {
1435 Py_DECREF(it);
1436 Py_RETURN_FALSE;
1437 }
1438 }
1439 Py_DECREF(it);
1440 if (PyErr_Occurred())
1441 return NULL;
1442 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001443}
1444
1445PyDoc_STRVAR(isdisjoint_doc,
1446"Return True if two sets have a null intersection.");
1447
Neal Norwitz6576bd82005-11-13 18:41:28 +00001448static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001449set_difference_update_internal(PySetObject *so, PyObject *other)
1450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 if ((PyObject *)so == other)
1452 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 if (PyAnySet_Check(other)) {
1455 setentry *entry;
1456 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 while (set_next((PySetObject *)other, &pos, &entry))
1459 if (set_discard_entry(so, entry) == -1)
1460 return -1;
1461 } else {
1462 PyObject *key, *it;
1463 it = PyObject_GetIter(other);
1464 if (it == NULL)
1465 return -1;
1466
1467 while ((key = PyIter_Next(it)) != NULL) {
1468 if (set_discard_key(so, key) == -1) {
1469 Py_DECREF(it);
1470 Py_DECREF(key);
1471 return -1;
1472 }
1473 Py_DECREF(key);
1474 }
1475 Py_DECREF(it);
1476 if (PyErr_Occurred())
1477 return -1;
1478 }
1479 /* If more than 1/5 are dummies, then resize them away. */
1480 if ((so->fill - so->used) * 5 < so->mask)
1481 return 0;
1482 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001483}
1484
Raymond Hettingera690a992003-11-16 16:17:49 +00001485static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001486set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1491 PyObject *other = PyTuple_GET_ITEM(args, i);
1492 if (set_difference_update_internal(so, other) == -1)
1493 return NULL;
1494 }
1495 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001496}
1497
1498PyDoc_STRVAR(difference_update_doc,
1499"Remove all elements of another set from this set.");
1500
1501static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001502set_copy_and_difference(PySetObject *so, PyObject *other)
1503{
1504 PyObject *result;
1505
1506 result = set_copy(so);
1507 if (result == NULL)
1508 return NULL;
1509 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1510 return result;
1511 Py_DECREF(result);
1512 return NULL;
1513}
1514
1515static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001516set_difference(PySetObject *so, PyObject *other)
1517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 PyObject *result;
1519 setentry *entry;
1520 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001523 return set_copy_and_difference(so, other);
1524 }
1525
1526 /* If len(so) much more than len(other), it's more efficient to simply copy
1527 * so and then iterate other looking for common elements. */
1528 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1529 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 result = make_new_set_basetype(Py_TYPE(so), NULL);
1533 if (result == NULL)
1534 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 if (PyDict_CheckExact(other)) {
1537 while (set_next(so, &pos, &entry)) {
1538 setentry entrycopy;
1539 entrycopy.hash = entry->hash;
1540 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001541 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1543 Py_DECREF(result);
1544 return NULL;
1545 }
1546 }
1547 }
1548 return result;
1549 }
1550
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001551 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 while (set_next(so, &pos, &entry)) {
1553 int rv = set_contains_entry((PySetObject *)other, entry);
1554 if (rv == -1) {
1555 Py_DECREF(result);
1556 return NULL;
1557 }
1558 if (!rv) {
1559 if (set_add_entry((PySetObject *)result, entry) == -1) {
1560 Py_DECREF(result);
1561 return NULL;
1562 }
1563 }
1564 }
1565 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001566}
1567
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001568static PyObject *
1569set_difference_multi(PySetObject *so, PyObject *args)
1570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 Py_ssize_t i;
1572 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 if (PyTuple_GET_SIZE(args) == 0)
1575 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 other = PyTuple_GET_ITEM(args, 0);
1578 result = set_difference(so, other);
1579 if (result == NULL)
1580 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1583 other = PyTuple_GET_ITEM(args, i);
1584 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1585 Py_DECREF(result);
1586 return NULL;
1587 }
1588 }
1589 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001590}
1591
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001592PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001593"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001594\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001595(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001596static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001597set_sub(PySetObject *so, PyObject *other)
1598{
Brian Curtindfc80e32011-08-10 20:28:54 -05001599 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1600 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001602}
1603
1604static PyObject *
1605set_isub(PySetObject *so, PyObject *other)
1606{
Brian Curtindfc80e32011-08-10 20:28:54 -05001607 if (!PyAnySet_Check(other))
1608 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 if (set_difference_update_internal(so, other) == -1)
1610 return NULL;
1611 Py_INCREF(so);
1612 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001613}
1614
1615static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001616set_symmetric_difference_update(PySetObject *so, PyObject *other)
1617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 PySetObject *otherset;
1619 PyObject *key;
1620 Py_ssize_t pos = 0;
1621 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 if ((PyObject *)so == other)
1624 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 if (PyDict_CheckExact(other)) {
1627 PyObject *value;
1628 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001629 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1631 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001632
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001633 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 an_entry.hash = hash;
1635 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001638 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001639 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001642 if (rv == DISCARD_NOTFOUND) {
1643 if (set_add_entry(so, &an_entry) == -1) {
1644 Py_DECREF(key);
1645 return NULL;
1646 }
1647 }
Georg Brandl2d444492010-09-03 10:52:55 +00001648 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 }
1650 Py_RETURN_NONE;
1651 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 if (PyAnySet_Check(other)) {
1654 Py_INCREF(other);
1655 otherset = (PySetObject *)other;
1656 } else {
1657 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1658 if (otherset == NULL)
1659 return NULL;
1660 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 while (set_next(otherset, &pos, &entry)) {
1663 int rv = set_discard_entry(so, entry);
1664 if (rv == -1) {
1665 Py_DECREF(otherset);
1666 return NULL;
1667 }
1668 if (rv == DISCARD_NOTFOUND) {
1669 if (set_add_entry(so, entry) == -1) {
1670 Py_DECREF(otherset);
1671 return NULL;
1672 }
1673 }
1674 }
1675 Py_DECREF(otherset);
1676 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001677}
1678
1679PyDoc_STRVAR(symmetric_difference_update_doc,
1680"Update a set with the symmetric difference of itself and another.");
1681
1682static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001683set_symmetric_difference(PySetObject *so, PyObject *other)
1684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 PyObject *rv;
1686 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1689 if (otherset == NULL)
1690 return NULL;
1691 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1692 if (rv == NULL)
1693 return NULL;
1694 Py_DECREF(rv);
1695 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001696}
1697
1698PyDoc_STRVAR(symmetric_difference_doc,
1699"Return the symmetric difference of two sets as a new set.\n\
1700\n\
1701(i.e. all elements that are in exactly one of the sets.)");
1702
1703static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001704set_xor(PySetObject *so, PyObject *other)
1705{
Brian Curtindfc80e32011-08-10 20:28:54 -05001706 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1707 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001709}
1710
1711static PyObject *
1712set_ixor(PySetObject *so, PyObject *other)
1713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001715
Brian Curtindfc80e32011-08-10 20:28:54 -05001716 if (!PyAnySet_Check(other))
1717 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 result = set_symmetric_difference_update(so, other);
1719 if (result == NULL)
1720 return NULL;
1721 Py_DECREF(result);
1722 Py_INCREF(so);
1723 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001724}
1725
1726static PyObject *
1727set_issubset(PySetObject *so, PyObject *other)
1728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 setentry *entry;
1730 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 if (!PyAnySet_Check(other)) {
1733 PyObject *tmp, *result;
1734 tmp = make_new_set(&PySet_Type, other);
1735 if (tmp == NULL)
1736 return NULL;
1737 result = set_issubset(so, tmp);
1738 Py_DECREF(tmp);
1739 return result;
1740 }
1741 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1742 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 while (set_next(so, &pos, &entry)) {
1745 int rv = set_contains_entry((PySetObject *)other, entry);
1746 if (rv == -1)
1747 return NULL;
1748 if (!rv)
1749 Py_RETURN_FALSE;
1750 }
1751 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001752}
1753
1754PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1755
1756static PyObject *
1757set_issuperset(PySetObject *so, PyObject *other)
1758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 if (!PyAnySet_Check(other)) {
1762 tmp = make_new_set(&PySet_Type, other);
1763 if (tmp == NULL)
1764 return NULL;
1765 result = set_issuperset(so, tmp);
1766 Py_DECREF(tmp);
1767 return result;
1768 }
1769 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001770}
1771
1772PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1773
Raymond Hettingera690a992003-11-16 16:17:49 +00001774static PyObject *
1775set_richcompare(PySetObject *v, PyObject *w, int op)
1776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001778
Brian Curtindfc80e32011-08-10 20:28:54 -05001779 if(!PyAnySet_Check(w))
1780 Py_RETURN_NOTIMPLEMENTED;
1781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 switch (op) {
1783 case Py_EQ:
1784 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1785 Py_RETURN_FALSE;
1786 if (v->hash != -1 &&
1787 ((PySetObject *)w)->hash != -1 &&
1788 v->hash != ((PySetObject *)w)->hash)
1789 Py_RETURN_FALSE;
1790 return set_issubset(v, w);
1791 case Py_NE:
1792 r1 = set_richcompare(v, w, Py_EQ);
1793 if (r1 == NULL)
1794 return NULL;
1795 r2 = PyBool_FromLong(PyObject_Not(r1));
1796 Py_DECREF(r1);
1797 return r2;
1798 case Py_LE:
1799 return set_issubset(v, w);
1800 case Py_GE:
1801 return set_issuperset(v, w);
1802 case Py_LT:
1803 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1804 Py_RETURN_FALSE;
1805 return set_issubset(v, w);
1806 case Py_GT:
1807 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1808 Py_RETURN_FALSE;
1809 return set_issuperset(v, w);
1810 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001811 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001812}
1813
Raymond Hettingera690a992003-11-16 16:17:49 +00001814static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001815set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 if (set_add_key(so, key) == -1)
1818 return NULL;
1819 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001820}
1821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001823"Add an element to a set.\n\
1824\n\
1825This has no effect if the element is already present.");
1826
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001827static int
1828set_contains(PySetObject *so, PyObject *key)
1829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 PyObject *tmpkey;
1831 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 rv = set_contains_key(so, key);
1834 if (rv == -1) {
1835 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1836 return -1;
1837 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001838 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 if (tmpkey == NULL)
1840 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001841 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 Py_DECREF(tmpkey);
1843 }
1844 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001845}
1846
1847static PyObject *
1848set_direct_contains(PySetObject *so, PyObject *key)
1849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 result = set_contains(so, key);
1853 if (result == -1)
1854 return NULL;
1855 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001856}
1857
1858PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1859
Raymond Hettingera690a992003-11-16 16:17:49 +00001860static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001861set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 PyObject *tmpkey;
1864 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 rv = set_discard_key(so, key);
1867 if (rv == -1) {
1868 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1869 return NULL;
1870 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001871 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 if (tmpkey == NULL)
1873 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 Py_DECREF(tmpkey);
1876 if (rv == -1)
1877 return NULL;
1878 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001881 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 return NULL;
1883 }
1884 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001885}
1886
1887PyDoc_STRVAR(remove_doc,
1888"Remove an element from a set; it must be a member.\n\
1889\n\
1890If the element is not a member, raise a KeyError.");
1891
1892static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001893set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001894{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001895 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 rv = set_discard_key(so, key);
1899 if (rv == -1) {
1900 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1901 return NULL;
1902 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001903 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 if (tmpkey == NULL)
1905 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001906 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001908 if (rv == -1)
1909 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 }
1911 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001912}
1913
1914PyDoc_STRVAR(discard_doc,
1915"Remove an element from a set if it is a member.\n\
1916\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001918
1919static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001920set_reduce(PySetObject *so)
1921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001923 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 keys = PySequence_List((PyObject *)so);
1926 if (keys == NULL)
1927 goto done;
1928 args = PyTuple_Pack(1, keys);
1929 if (args == NULL)
1930 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001931 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 if (dict == NULL) {
1933 PyErr_Clear();
1934 dict = Py_None;
1935 Py_INCREF(dict);
1936 }
1937 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001938done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 Py_XDECREF(args);
1940 Py_XDECREF(keys);
1941 Py_XDECREF(dict);
1942 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001943}
1944
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001945static PyObject *
1946set_sizeof(PySetObject *so)
1947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 res = sizeof(PySetObject);
1951 if (so->table != so->smalltable)
1952 res = res + (so->mask + 1) * sizeof(setentry);
1953 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001954}
1955
1956PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001957static int
1958set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 if (!PyAnySet_Check(self))
1963 return -1;
1964 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1965 return -1;
1966 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1967 return -1;
1968 set_clear_internal(self);
1969 self->hash = -1;
1970 if (iterable == NULL)
1971 return 0;
1972 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001973}
1974
Raymond Hettingera690a992003-11-16 16:17:49 +00001975static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 set_len, /* sq_length */
1977 0, /* sq_concat */
1978 0, /* sq_repeat */
1979 0, /* sq_item */
1980 0, /* sq_slice */
1981 0, /* sq_ass_item */
1982 0, /* sq_ass_slice */
1983 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001984};
1985
1986/* set object ********************************************************/
1987
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001988#ifdef Py_DEBUG
1989static PyObject *test_c_api(PySetObject *so);
1990
1991PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1992All is well if assertions don't fail.");
1993#endif
1994
Raymond Hettingera690a992003-11-16 16:17:49 +00001995static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 {"add", (PyCFunction)set_add, METH_O,
1997 add_doc},
1998 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1999 clear_doc},
2000 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2001 contains_doc},
2002 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2003 copy_doc},
2004 {"discard", (PyCFunction)set_discard, METH_O,
2005 discard_doc},
2006 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2007 difference_doc},
2008 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2009 difference_update_doc},
2010 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2011 intersection_doc},
2012 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2013 intersection_update_doc},
2014 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2015 isdisjoint_doc},
2016 {"issubset", (PyCFunction)set_issubset, METH_O,
2017 issubset_doc},
2018 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2019 issuperset_doc},
2020 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2021 pop_doc},
2022 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2023 reduce_doc},
2024 {"remove", (PyCFunction)set_remove, METH_O,
2025 remove_doc},
2026 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2027 sizeof_doc},
2028 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2029 symmetric_difference_doc},
2030 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2031 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002032#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2034 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002035#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 {"union", (PyCFunction)set_union, METH_VARARGS,
2037 union_doc},
2038 {"update", (PyCFunction)set_update, METH_VARARGS,
2039 update_doc},
2040 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002041};
2042
2043static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 0, /*nb_add*/
2045 (binaryfunc)set_sub, /*nb_subtract*/
2046 0, /*nb_multiply*/
2047 0, /*nb_remainder*/
2048 0, /*nb_divmod*/
2049 0, /*nb_power*/
2050 0, /*nb_negative*/
2051 0, /*nb_positive*/
2052 0, /*nb_absolute*/
2053 0, /*nb_bool*/
2054 0, /*nb_invert*/
2055 0, /*nb_lshift*/
2056 0, /*nb_rshift*/
2057 (binaryfunc)set_and, /*nb_and*/
2058 (binaryfunc)set_xor, /*nb_xor*/
2059 (binaryfunc)set_or, /*nb_or*/
2060 0, /*nb_int*/
2061 0, /*nb_reserved*/
2062 0, /*nb_float*/
2063 0, /*nb_inplace_add*/
2064 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2065 0, /*nb_inplace_multiply*/
2066 0, /*nb_inplace_remainder*/
2067 0, /*nb_inplace_power*/
2068 0, /*nb_inplace_lshift*/
2069 0, /*nb_inplace_rshift*/
2070 (binaryfunc)set_iand, /*nb_inplace_and*/
2071 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2072 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002073};
2074
2075PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002076"set() -> new empty set object\n\
2077set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002078\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002079Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002080
2081PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2083 "set", /* tp_name */
2084 sizeof(PySetObject), /* tp_basicsize */
2085 0, /* tp_itemsize */
2086 /* methods */
2087 (destructor)set_dealloc, /* tp_dealloc */
2088 0, /* tp_print */
2089 0, /* tp_getattr */
2090 0, /* tp_setattr */
2091 0, /* tp_reserved */
2092 (reprfunc)set_repr, /* tp_repr */
2093 &set_as_number, /* tp_as_number */
2094 &set_as_sequence, /* tp_as_sequence */
2095 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002096 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 0, /* tp_call */
2098 0, /* tp_str */
2099 PyObject_GenericGetAttr, /* tp_getattro */
2100 0, /* tp_setattro */
2101 0, /* tp_as_buffer */
2102 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2103 Py_TPFLAGS_BASETYPE, /* tp_flags */
2104 set_doc, /* tp_doc */
2105 (traverseproc)set_traverse, /* tp_traverse */
2106 (inquiry)set_clear_internal, /* tp_clear */
2107 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002108 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2109 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 0, /* tp_iternext */
2111 set_methods, /* tp_methods */
2112 0, /* tp_members */
2113 0, /* tp_getset */
2114 0, /* tp_base */
2115 0, /* tp_dict */
2116 0, /* tp_descr_get */
2117 0, /* tp_descr_set */
2118 0, /* tp_dictoffset */
2119 (initproc)set_init, /* tp_init */
2120 PyType_GenericAlloc, /* tp_alloc */
2121 set_new, /* tp_new */
2122 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002123};
2124
2125/* frozenset object ********************************************************/
2126
2127
2128static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2130 contains_doc},
2131 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2132 copy_doc},
2133 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2134 difference_doc},
2135 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2136 intersection_doc},
2137 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2138 isdisjoint_doc},
2139 {"issubset", (PyCFunction)set_issubset, METH_O,
2140 issubset_doc},
2141 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2142 issuperset_doc},
2143 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2144 reduce_doc},
2145 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2146 sizeof_doc},
2147 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2148 symmetric_difference_doc},
2149 {"union", (PyCFunction)set_union, METH_VARARGS,
2150 union_doc},
2151 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002152};
2153
2154static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 0, /*nb_add*/
2156 (binaryfunc)set_sub, /*nb_subtract*/
2157 0, /*nb_multiply*/
2158 0, /*nb_remainder*/
2159 0, /*nb_divmod*/
2160 0, /*nb_power*/
2161 0, /*nb_negative*/
2162 0, /*nb_positive*/
2163 0, /*nb_absolute*/
2164 0, /*nb_bool*/
2165 0, /*nb_invert*/
2166 0, /*nb_lshift*/
2167 0, /*nb_rshift*/
2168 (binaryfunc)set_and, /*nb_and*/
2169 (binaryfunc)set_xor, /*nb_xor*/
2170 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002171};
2172
2173PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002174"frozenset() -> empty frozenset object\n\
2175frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002176\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002177Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002178
2179PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2181 "frozenset", /* tp_name */
2182 sizeof(PySetObject), /* tp_basicsize */
2183 0, /* tp_itemsize */
2184 /* methods */
2185 (destructor)set_dealloc, /* tp_dealloc */
2186 0, /* tp_print */
2187 0, /* tp_getattr */
2188 0, /* tp_setattr */
2189 0, /* tp_reserved */
2190 (reprfunc)set_repr, /* tp_repr */
2191 &frozenset_as_number, /* tp_as_number */
2192 &set_as_sequence, /* tp_as_sequence */
2193 0, /* tp_as_mapping */
2194 frozenset_hash, /* tp_hash */
2195 0, /* tp_call */
2196 0, /* tp_str */
2197 PyObject_GenericGetAttr, /* tp_getattro */
2198 0, /* tp_setattro */
2199 0, /* tp_as_buffer */
2200 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2201 Py_TPFLAGS_BASETYPE, /* tp_flags */
2202 frozenset_doc, /* tp_doc */
2203 (traverseproc)set_traverse, /* tp_traverse */
2204 (inquiry)set_clear_internal, /* tp_clear */
2205 (richcmpfunc)set_richcompare, /* tp_richcompare */
2206 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2207 (getiterfunc)set_iter, /* tp_iter */
2208 0, /* tp_iternext */
2209 frozenset_methods, /* tp_methods */
2210 0, /* tp_members */
2211 0, /* tp_getset */
2212 0, /* tp_base */
2213 0, /* tp_dict */
2214 0, /* tp_descr_get */
2215 0, /* tp_descr_set */
2216 0, /* tp_dictoffset */
2217 0, /* tp_init */
2218 PyType_GenericAlloc, /* tp_alloc */
2219 frozenset_new, /* tp_new */
2220 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002221};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002222
2223
2224/***** C API functions *************************************************/
2225
2226PyObject *
2227PySet_New(PyObject *iterable)
2228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002230}
2231
2232PyObject *
2233PyFrozenSet_New(PyObject *iterable)
2234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002236}
2237
Neal Norwitz8c49c822006-03-04 18:41:19 +00002238Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002239PySet_Size(PyObject *anyset)
2240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 if (!PyAnySet_Check(anyset)) {
2242 PyErr_BadInternalCall();
2243 return -1;
2244 }
2245 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002246}
2247
2248int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002249PySet_Clear(PyObject *set)
2250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 if (!PySet_Check(set)) {
2252 PyErr_BadInternalCall();
2253 return -1;
2254 }
2255 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002256}
2257
2258int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002259PySet_Contains(PyObject *anyset, PyObject *key)
2260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 if (!PyAnySet_Check(anyset)) {
2262 PyErr_BadInternalCall();
2263 return -1;
2264 }
2265 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002266}
2267
2268int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002269PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 if (!PySet_Check(set)) {
2272 PyErr_BadInternalCall();
2273 return -1;
2274 }
2275 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002276}
2277
2278int
Christian Heimesfd66e512008-01-29 12:18:50 +00002279PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 if (!PySet_Check(anyset) &&
2282 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2283 PyErr_BadInternalCall();
2284 return -1;
2285 }
2286 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002287}
2288
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002289int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002290_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 if (!PyAnySet_Check(set)) {
2295 PyErr_BadInternalCall();
2296 return -1;
2297 }
2298 if (set_next((PySetObject *)set, pos, &entry) == 0)
2299 return 0;
2300 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002301 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002303}
2304
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002305PyObject *
2306PySet_Pop(PyObject *set)
2307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 if (!PySet_Check(set)) {
2309 PyErr_BadInternalCall();
2310 return NULL;
2311 }
2312 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002313}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002314
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002315int
2316_PySet_Update(PyObject *set, PyObject *iterable)
2317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 if (!PySet_Check(set)) {
2319 PyErr_BadInternalCall();
2320 return -1;
2321 }
2322 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002323}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002324
Raymond Hettingere259f132013-12-15 11:56:14 -08002325/* Exported for the gdb plugin's benefit. */
2326PyObject *_PySet_Dummy = dummy;
2327
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002328#ifdef Py_DEBUG
2329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002331 Returns True and original set is restored. */
2332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333#define assertRaises(call_return_value, exception) \
2334 do { \
2335 assert(call_return_value); \
2336 assert(PyErr_ExceptionMatches(exception)); \
2337 PyErr_Clear(); \
2338 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002339
2340static PyObject *
2341test_c_api(PySetObject *so)
2342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 Py_ssize_t count;
2344 char *s;
2345 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002346 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002348 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 /* Verify preconditions */
2352 assert(PyAnySet_Check(ob));
2353 assert(PyAnySet_CheckExact(ob));
2354 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 /* so.clear(); so |= set("abc"); */
2357 str = PyUnicode_FromString("abc");
2358 if (str == NULL)
2359 return NULL;
2360 set_clear_internal(so);
2361 if (set_update_internal(so, str) == -1) {
2362 Py_DECREF(str);
2363 return NULL;
2364 }
2365 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 /* Exercise type/size checks */
2368 assert(PySet_Size(ob) == 3);
2369 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 /* Raise TypeError for non-iterable constructor arguments */
2372 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2373 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 /* Raise TypeError for unhashable key */
2376 dup = PySet_New(ob);
2377 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2378 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2379 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 /* Exercise successful pop, contains, add, and discard */
2382 elem = PySet_Pop(ob);
2383 assert(PySet_Contains(ob, elem) == 0);
2384 assert(PySet_GET_SIZE(ob) == 2);
2385 assert(PySet_Add(ob, elem) == 0);
2386 assert(PySet_Contains(ob, elem) == 1);
2387 assert(PySet_GET_SIZE(ob) == 3);
2388 assert(PySet_Discard(ob, elem) == 1);
2389 assert(PySet_GET_SIZE(ob) == 2);
2390 assert(PySet_Discard(ob, elem) == 0);
2391 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* Exercise clear */
2394 dup2 = PySet_New(dup);
2395 assert(PySet_Clear(dup2) == 0);
2396 assert(PySet_Size(dup2) == 0);
2397 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 /* Raise SystemError on clear or update of frozen set */
2400 f = PyFrozenSet_New(dup);
2401 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2402 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2403 assert(PySet_Add(f, elem) == 0);
2404 Py_INCREF(f);
2405 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2406 Py_DECREF(f);
2407 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* Exercise direct iteration */
2410 i = 0, count = 0;
2411 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2412 s = _PyUnicode_AsString(x);
2413 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2414 count++;
2415 }
2416 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 /* Exercise updates */
2419 dup2 = PySet_New(NULL);
2420 assert(_PySet_Update(dup2, dup) == 0);
2421 assert(PySet_Size(dup2) == 3);
2422 assert(_PySet_Update(dup2, dup) == 0);
2423 assert(PySet_Size(dup2) == 3);
2424 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 /* Raise SystemError when self argument is not a set or frozenset. */
2427 t = PyTuple_New(0);
2428 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2429 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2430 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 /* Raise SystemError when self argument is not a set. */
2433 f = PyFrozenSet_New(dup);
2434 assert(PySet_Size(f) == 3);
2435 assert(PyFrozenSet_CheckExact(f));
2436 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2437 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2438 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 /* Raise KeyError when popping from an empty set */
2441 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2442 Py_DECREF(ob);
2443 assert(PySet_GET_SIZE(ob) == 0);
2444 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 /* Restore the set from the copy using the PyNumber API */
2447 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2448 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 /* Verify constructors accept NULL arguments */
2451 f = PySet_New(NULL);
2452 assert(f != NULL);
2453 assert(PySet_GET_SIZE(f) == 0);
2454 Py_DECREF(f);
2455 f = PyFrozenSet_New(NULL);
2456 assert(f != NULL);
2457 assert(PyFrozenSet_CheckExact(f));
2458 assert(PySet_GET_SIZE(f) == 0);
2459 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 Py_DECREF(elem);
2462 Py_DECREF(dup);
2463 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002464}
2465
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002466#undef assertRaises
2467
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002468#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002469
2470/***** Dummy Struct *************************************************/
2471
2472static PyObject *
2473dummy_repr(PyObject *op)
2474{
2475 return PyUnicode_FromString("<dummy key>");
2476}
2477
2478static void
2479dummy_dealloc(PyObject* ignore)
2480{
2481 Py_FatalError("deallocating <dummy key>");
2482}
2483
2484static PyTypeObject _PySetDummy_Type = {
2485 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2486 "<dummy key> type",
2487 0,
2488 0,
2489 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2490 0, /*tp_print*/
2491 0, /*tp_getattr*/
2492 0, /*tp_setattr*/
2493 0, /*tp_reserved*/
2494 dummy_repr, /*tp_repr*/
2495 0, /*tp_as_number*/
2496 0, /*tp_as_sequence*/
2497 0, /*tp_as_mapping*/
2498 0, /*tp_hash */
2499 0, /*tp_call */
2500 0, /*tp_str */
2501 0, /*tp_getattro */
2502 0, /*tp_setattro */
2503 0, /*tp_as_buffer */
2504 Py_TPFLAGS_DEFAULT, /*tp_flags */
2505};
2506
2507static PyObject _dummy_struct = {
2508 _PyObject_EXTRA_INIT
2509 2, &_PySetDummy_Type
2510};
2511