blob: 91f341cb0165bbe2b2b1f9698972eedfa13b73b2 [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 Hettinger426d9952014-05-18 21:40:20 +01007 Copyright (c) 2003-2014 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 Hettinger9cd6a782015-01-18 16:06:18 -0800674 i &= so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 }
676 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 entry->key = dummy;
678 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800679 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000681}
682
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000683PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
684Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000685
686static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000687set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 Py_ssize_t pos = 0;
690 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 while (set_next(so, &pos, &entry))
693 Py_VISIT(entry->key);
694 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000695}
696
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000697static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000698frozenset_hash(PyObject *self)
699{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800700 /* Most of the constants in this hash algorithm are randomly choosen
701 large primes with "interesting bit patterns" and that passed
702 tests for good collision statistics on a variety of problematic
703 datasets such as:
704
705 ps = []
706 for r in range(21):
707 ps += itertools.combinations(range(20), r)
708 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
709
710 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800712 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 setentry *entry;
714 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (so->hash != -1)
717 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000718
Mark Dickinson57e683e2011-09-24 18:18:40 +0100719 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 while (set_next(so, &pos, &entry)) {
721 /* Work to increase the bit dispersion for closely spaced hash
722 values. The is important because some use cases have many
723 combinations of a small number of elements with nearby
724 hashes so that many distinct combinations collapse to only
725 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000726 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800727 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800729 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500730 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800731 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200732 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800733 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 so->hash = hash;
735 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000736}
737
Raymond Hettingera9d99362005-08-05 00:01:15 +0000738/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000739
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000740typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 PyObject_HEAD
742 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
743 Py_ssize_t si_used;
744 Py_ssize_t si_pos;
745 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000746} setiterobject;
747
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000748static void
749setiter_dealloc(setiterobject *si)
750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 Py_XDECREF(si->si_set);
752 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000753}
754
755static int
756setiter_traverse(setiterobject *si, visitproc visit, void *arg)
757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 Py_VISIT(si->si_set);
759 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000760}
761
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000762static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000763setiter_len(setiterobject *si)
764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 Py_ssize_t len = 0;
766 if (si->si_set != NULL && si->si_used == si->si_set->used)
767 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000768 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000769}
770
Armin Rigof5b3e362006-02-11 21:32:43 +0000771PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000772
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000773static PyObject *setiter_iternext(setiterobject *si);
774
775static PyObject *
776setiter_reduce(setiterobject *si)
777{
778 PyObject *list;
779 setiterobject tmp;
780
781 list = PyList_New(0);
782 if (!list)
783 return NULL;
784
Ezio Melotti0e1af282012-09-28 16:43:40 +0300785 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000786 tmp = *si;
787 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300788
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000789 /* iterate the temporary into a list */
790 for(;;) {
791 PyObject *element = setiter_iternext(&tmp);
792 if (element) {
793 if (PyList_Append(list, element)) {
794 Py_DECREF(element);
795 Py_DECREF(list);
796 Py_XDECREF(tmp.si_set);
797 return NULL;
798 }
799 Py_DECREF(element);
800 } else
801 break;
802 }
803 Py_XDECREF(tmp.si_set);
804 /* check for error */
805 if (tmp.si_set != NULL) {
806 /* we have an error */
807 Py_DECREF(list);
808 return NULL;
809 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200810 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000811}
812
813PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
814
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000815static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000817 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000819};
820
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000821static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200824 Py_ssize_t i, mask;
825 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 if (so == NULL)
829 return NULL;
830 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 if (si->si_used != so->used) {
833 PyErr_SetString(PyExc_RuntimeError,
834 "Set changed size during iteration");
835 si->si_used = -1; /* Make this state sticky */
836 return NULL;
837 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 i = si->si_pos;
840 assert(i>=0);
841 entry = so->table;
842 mask = so->mask;
843 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
844 i++;
845 si->si_pos = i+1;
846 if (i > mask)
847 goto fail;
848 si->len--;
849 key = entry[i].key;
850 Py_INCREF(key);
851 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000852
853fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 Py_DECREF(so);
855 si->si_set = NULL;
856 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000857}
858
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000859PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PyVarObject_HEAD_INIT(&PyType_Type, 0)
861 "set_iterator", /* tp_name */
862 sizeof(setiterobject), /* tp_basicsize */
863 0, /* tp_itemsize */
864 /* methods */
865 (destructor)setiter_dealloc, /* tp_dealloc */
866 0, /* tp_print */
867 0, /* tp_getattr */
868 0, /* tp_setattr */
869 0, /* tp_reserved */
870 0, /* tp_repr */
871 0, /* tp_as_number */
872 0, /* tp_as_sequence */
873 0, /* tp_as_mapping */
874 0, /* tp_hash */
875 0, /* tp_call */
876 0, /* tp_str */
877 PyObject_GenericGetAttr, /* tp_getattro */
878 0, /* tp_setattro */
879 0, /* tp_as_buffer */
880 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
881 0, /* tp_doc */
882 (traverseproc)setiter_traverse, /* tp_traverse */
883 0, /* tp_clear */
884 0, /* tp_richcompare */
885 0, /* tp_weaklistoffset */
886 PyObject_SelfIter, /* tp_iter */
887 (iternextfunc)setiter_iternext, /* tp_iternext */
888 setiter_methods, /* tp_methods */
889 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000890};
891
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000892static PyObject *
893set_iter(PySetObject *so)
894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
896 if (si == NULL)
897 return NULL;
898 Py_INCREF(so);
899 si->si_set = so;
900 si->si_used = so->used;
901 si->si_pos = 0;
902 si->len = so->used;
903 _PyObject_GC_TRACK(si);
904 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905}
906
Raymond Hettingerd7946662005-08-01 21:39:29 +0000907static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000908set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 if (PyAnySet_Check(other))
913 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 if (PyDict_CheckExact(other)) {
916 PyObject *value;
917 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000918 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 /* Do one big resize at the start, rather than
922 * incrementally resizing as we insert new keys. Expect
923 * that there will be no (or few) overlapping keys.
924 */
925 if (dictsize == -1)
926 return -1;
927 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
928 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
929 return -1;
930 }
931 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
932 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 an_entry.hash = hash;
935 an_entry.key = key;
936 if (set_add_entry(so, &an_entry) == -1)
937 return -1;
938 }
939 return 0;
940 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 it = PyObject_GetIter(other);
943 if (it == NULL)
944 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 while ((key = PyIter_Next(it)) != NULL) {
947 if (set_add_key(so, key) == -1) {
948 Py_DECREF(it);
949 Py_DECREF(key);
950 return -1;
951 }
952 Py_DECREF(key);
953 }
954 Py_DECREF(it);
955 if (PyErr_Occurred())
956 return -1;
957 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000958}
959
960static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000961set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
966 PyObject *other = PyTuple_GET_ITEM(args, i);
967 if (set_update_internal(so, other) == -1)
968 return NULL;
969 }
970 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000971}
972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000974"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000975
Raymond Hettinger426d9952014-05-18 21:40:20 +0100976/* XXX Todo:
977 If aligned memory allocations become available, make the
978 set object 64 byte aligned so that most of the fields
979 can be retrieved or updated in a single cache line.
980*/
981
Raymond Hettingera38123e2003-11-24 22:18:49 +0000982static PyObject *
983make_new_set(PyTypeObject *type, PyObject *iterable)
984{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200985 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700988 so = (PySetObject *)type->tp_alloc(type, 0);
989 if (so == NULL)
990 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000991
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700992 so->fill = 0;
993 so->used = 0;
994 so->mask = PySet_MINSIZE - 1;
995 so->table = so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 so->lookup = set_lookkey_unicode;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700997 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800998 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 if (iterable != NULL) {
1002 if (set_update_internal(so, iterable) == -1) {
1003 Py_DECREF(so);
1004 return NULL;
1005 }
1006 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001009}
1010
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001011static PyObject *
1012make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1015 if (PyType_IsSubtype(type, &PySet_Type))
1016 type = &PySet_Type;
1017 else
1018 type = &PyFrozenSet_Type;
1019 }
1020 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001021}
1022
Raymond Hettingerd7946662005-08-01 21:39:29 +00001023/* The empty frozenset is a singleton */
1024static PyObject *emptyfrozenset = NULL;
1025
Raymond Hettingera690a992003-11-16 16:17:49 +00001026static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001027frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1032 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1035 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 if (type != &PyFrozenSet_Type)
1038 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 if (iterable != NULL) {
1041 /* frozenset(f) is idempotent */
1042 if (PyFrozenSet_CheckExact(iterable)) {
1043 Py_INCREF(iterable);
1044 return iterable;
1045 }
1046 result = make_new_set(type, iterable);
1047 if (result == NULL || PySet_GET_SIZE(result))
1048 return result;
1049 Py_DECREF(result);
1050 }
1051 /* The empty frozenset is a singleton */
1052 if (emptyfrozenset == NULL)
1053 emptyfrozenset = make_new_set(type, NULL);
1054 Py_XINCREF(emptyfrozenset);
1055 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001056}
1057
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001058int
1059PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001060{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001061 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001062}
1063
1064void
1065PySet_Fini(void)
1066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001068}
1069
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001070static PyObject *
1071set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1074 return NULL;
1075
1076 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001077}
1078
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001079/* set_swap_bodies() switches the contents of any two sets by moving their
1080 internal data pointers and, if needed, copying the internal smalltables.
1081 Semantically equivalent to:
1082
1083 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1084
1085 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001087 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001089 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001090*/
1091
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001092static void
1093set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 Py_ssize_t t;
1096 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001097 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001099 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 t = a->fill; a->fill = b->fill; b->fill = t;
1102 t = a->used; a->used = b->used; b->used = t;
1103 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 u = a->table;
1106 if (a->table == a->smalltable)
1107 u = b->smalltable;
1108 a->table = b->table;
1109 if (b->table == b->smalltable)
1110 a->table = a->smalltable;
1111 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 if (a->table == a->smalltable || b->table == b->smalltable) {
1116 memcpy(tab, a->smalltable, sizeof(tab));
1117 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1118 memcpy(b->smalltable, tab, sizeof(tab));
1119 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1122 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1123 h = a->hash; a->hash = b->hash; b->hash = h;
1124 } else {
1125 a->hash = -1;
1126 b->hash = -1;
1127 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001128}
1129
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001130static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001131set_copy(PySetObject *so)
1132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001134}
1135
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001136static PyObject *
1137frozenset_copy(PySetObject *so)
1138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (PyFrozenSet_CheckExact(so)) {
1140 Py_INCREF(so);
1141 return (PyObject *)so;
1142 }
1143 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001144}
1145
Raymond Hettingera690a992003-11-16 16:17:49 +00001146PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1147
1148static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001149set_clear(PySetObject *so)
1150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 set_clear_internal(so);
1152 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001153}
1154
1155PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1156
1157static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001158set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 PySetObject *result;
1161 PyObject *other;
1162 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 result = (PySetObject *)set_copy(so);
1165 if (result == NULL)
1166 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1169 other = PyTuple_GET_ITEM(args, i);
1170 if ((PyObject *)so == other)
1171 continue;
1172 if (set_update_internal(result, other) == -1) {
1173 Py_DECREF(result);
1174 return NULL;
1175 }
1176 }
1177 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001178}
1179
1180PyDoc_STRVAR(union_doc,
1181 "Return the union of sets as a new set.\n\
1182\n\
1183(i.e. all elements that are in either set.)");
1184
1185static PyObject *
1186set_or(PySetObject *so, PyObject *other)
1187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001189
Brian Curtindfc80e32011-08-10 20:28:54 -05001190 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1191 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 result = (PySetObject *)set_copy(so);
1194 if (result == NULL)
1195 return NULL;
1196 if ((PyObject *)so == other)
1197 return (PyObject *)result;
1198 if (set_update_internal(result, other) == -1) {
1199 Py_DECREF(result);
1200 return NULL;
1201 }
1202 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001203}
1204
Raymond Hettingera690a992003-11-16 16:17:49 +00001205static PyObject *
1206set_ior(PySetObject *so, PyObject *other)
1207{
Brian Curtindfc80e32011-08-10 20:28:54 -05001208 if (!PyAnySet_Check(other))
1209 Py_RETURN_NOTIMPLEMENTED;
1210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (set_update_internal(so, other) == -1)
1212 return NULL;
1213 Py_INCREF(so);
1214 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001215}
1216
1217static PyObject *
1218set_intersection(PySetObject *so, PyObject *other)
1219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 PySetObject *result;
1221 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 if ((PyObject *)so == other)
1224 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1227 if (result == NULL)
1228 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 if (PyAnySet_Check(other)) {
1231 Py_ssize_t pos = 0;
1232 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1235 tmp = (PyObject *)so;
1236 so = (PySetObject *)other;
1237 other = tmp;
1238 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 while (set_next((PySetObject *)other, &pos, &entry)) {
1241 int rv = set_contains_entry(so, entry);
1242 if (rv == -1) {
1243 Py_DECREF(result);
1244 return NULL;
1245 }
1246 if (rv) {
1247 if (set_add_entry(result, entry) == -1) {
1248 Py_DECREF(result);
1249 return NULL;
1250 }
1251 }
1252 }
1253 return (PyObject *)result;
1254 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 it = PyObject_GetIter(other);
1257 if (it == NULL) {
1258 Py_DECREF(result);
1259 return NULL;
1260 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 while ((key = PyIter_Next(it)) != NULL) {
1263 int rv;
1264 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001265 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 if (hash == -1) {
1268 Py_DECREF(it);
1269 Py_DECREF(result);
1270 Py_DECREF(key);
1271 return NULL;
1272 }
1273 entry.hash = hash;
1274 entry.key = key;
1275 rv = set_contains_entry(so, &entry);
1276 if (rv == -1) {
1277 Py_DECREF(it);
1278 Py_DECREF(result);
1279 Py_DECREF(key);
1280 return NULL;
1281 }
1282 if (rv) {
1283 if (set_add_entry(result, &entry) == -1) {
1284 Py_DECREF(it);
1285 Py_DECREF(result);
1286 Py_DECREF(key);
1287 return NULL;
1288 }
1289 }
1290 Py_DECREF(key);
1291 }
1292 Py_DECREF(it);
1293 if (PyErr_Occurred()) {
1294 Py_DECREF(result);
1295 return NULL;
1296 }
1297 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001298}
1299
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001300static PyObject *
1301set_intersection_multi(PySetObject *so, PyObject *args)
1302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 Py_ssize_t i;
1304 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 if (PyTuple_GET_SIZE(args) == 0)
1307 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 Py_INCREF(so);
1310 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1311 PyObject *other = PyTuple_GET_ITEM(args, i);
1312 PyObject *newresult = set_intersection((PySetObject *)result, other);
1313 if (newresult == NULL) {
1314 Py_DECREF(result);
1315 return NULL;
1316 }
1317 Py_DECREF(result);
1318 result = newresult;
1319 }
1320 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001321}
1322
Raymond Hettingera690a992003-11-16 16:17:49 +00001323PyDoc_STRVAR(intersection_doc,
1324"Return the intersection of two sets as a new set.\n\
1325\n\
1326(i.e. all elements that are in both sets.)");
1327
1328static PyObject *
1329set_intersection_update(PySetObject *so, PyObject *other)
1330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 tmp = set_intersection(so, other);
1334 if (tmp == NULL)
1335 return NULL;
1336 set_swap_bodies(so, (PySetObject *)tmp);
1337 Py_DECREF(tmp);
1338 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001339}
1340
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001341static PyObject *
1342set_intersection_update_multi(PySetObject *so, PyObject *args)
1343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 tmp = set_intersection_multi(so, args);
1347 if (tmp == NULL)
1348 return NULL;
1349 set_swap_bodies(so, (PySetObject *)tmp);
1350 Py_DECREF(tmp);
1351 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001352}
1353
Raymond Hettingera690a992003-11-16 16:17:49 +00001354PyDoc_STRVAR(intersection_update_doc,
1355"Update a set with the intersection of itself and another.");
1356
1357static PyObject *
1358set_and(PySetObject *so, PyObject *other)
1359{
Brian Curtindfc80e32011-08-10 20:28:54 -05001360 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1361 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001363}
1364
1365static PyObject *
1366set_iand(PySetObject *so, PyObject *other)
1367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001369
Brian Curtindfc80e32011-08-10 20:28:54 -05001370 if (!PyAnySet_Check(other))
1371 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 result = set_intersection_update(so, other);
1373 if (result == NULL)
1374 return NULL;
1375 Py_DECREF(result);
1376 Py_INCREF(so);
1377 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001378}
1379
Guido van Rossum58da9312007-11-10 23:39:45 +00001380static PyObject *
1381set_isdisjoint(PySetObject *so, PyObject *other)
1382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 if ((PyObject *)so == other) {
1386 if (PySet_GET_SIZE(so) == 0)
1387 Py_RETURN_TRUE;
1388 else
1389 Py_RETURN_FALSE;
1390 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 if (PyAnySet_CheckExact(other)) {
1393 Py_ssize_t pos = 0;
1394 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1397 tmp = (PyObject *)so;
1398 so = (PySetObject *)other;
1399 other = tmp;
1400 }
1401 while (set_next((PySetObject *)other, &pos, &entry)) {
1402 int rv = set_contains_entry(so, entry);
1403 if (rv == -1)
1404 return NULL;
1405 if (rv)
1406 Py_RETURN_FALSE;
1407 }
1408 Py_RETURN_TRUE;
1409 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 it = PyObject_GetIter(other);
1412 if (it == NULL)
1413 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 while ((key = PyIter_Next(it)) != NULL) {
1416 int rv;
1417 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001418 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (hash == -1) {
1421 Py_DECREF(key);
1422 Py_DECREF(it);
1423 return NULL;
1424 }
1425 entry.hash = hash;
1426 entry.key = key;
1427 rv = set_contains_entry(so, &entry);
1428 Py_DECREF(key);
1429 if (rv == -1) {
1430 Py_DECREF(it);
1431 return NULL;
1432 }
1433 if (rv) {
1434 Py_DECREF(it);
1435 Py_RETURN_FALSE;
1436 }
1437 }
1438 Py_DECREF(it);
1439 if (PyErr_Occurred())
1440 return NULL;
1441 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001442}
1443
1444PyDoc_STRVAR(isdisjoint_doc,
1445"Return True if two sets have a null intersection.");
1446
Neal Norwitz6576bd82005-11-13 18:41:28 +00001447static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001448set_difference_update_internal(PySetObject *so, PyObject *other)
1449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 if ((PyObject *)so == other)
1451 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 if (PyAnySet_Check(other)) {
1454 setentry *entry;
1455 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 while (set_next((PySetObject *)other, &pos, &entry))
1458 if (set_discard_entry(so, entry) == -1)
1459 return -1;
1460 } else {
1461 PyObject *key, *it;
1462 it = PyObject_GetIter(other);
1463 if (it == NULL)
1464 return -1;
1465
1466 while ((key = PyIter_Next(it)) != NULL) {
1467 if (set_discard_key(so, key) == -1) {
1468 Py_DECREF(it);
1469 Py_DECREF(key);
1470 return -1;
1471 }
1472 Py_DECREF(key);
1473 }
1474 Py_DECREF(it);
1475 if (PyErr_Occurred())
1476 return -1;
1477 }
1478 /* If more than 1/5 are dummies, then resize them away. */
1479 if ((so->fill - so->used) * 5 < so->mask)
1480 return 0;
1481 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001482}
1483
Raymond Hettingera690a992003-11-16 16:17:49 +00001484static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001485set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1490 PyObject *other = PyTuple_GET_ITEM(args, i);
1491 if (set_difference_update_internal(so, other) == -1)
1492 return NULL;
1493 }
1494 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001495}
1496
1497PyDoc_STRVAR(difference_update_doc,
1498"Remove all elements of another set from this set.");
1499
1500static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001501set_copy_and_difference(PySetObject *so, PyObject *other)
1502{
1503 PyObject *result;
1504
1505 result = set_copy(so);
1506 if (result == NULL)
1507 return NULL;
1508 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1509 return result;
1510 Py_DECREF(result);
1511 return NULL;
1512}
1513
1514static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001515set_difference(PySetObject *so, PyObject *other)
1516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 PyObject *result;
1518 setentry *entry;
1519 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001522 return set_copy_and_difference(so, other);
1523 }
1524
1525 /* If len(so) much more than len(other), it's more efficient to simply copy
1526 * so and then iterate other looking for common elements. */
1527 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1528 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 result = make_new_set_basetype(Py_TYPE(so), NULL);
1532 if (result == NULL)
1533 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 if (PyDict_CheckExact(other)) {
1536 while (set_next(so, &pos, &entry)) {
1537 setentry entrycopy;
1538 entrycopy.hash = entry->hash;
1539 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001540 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1542 Py_DECREF(result);
1543 return NULL;
1544 }
1545 }
1546 }
1547 return result;
1548 }
1549
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001550 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 while (set_next(so, &pos, &entry)) {
1552 int rv = set_contains_entry((PySetObject *)other, entry);
1553 if (rv == -1) {
1554 Py_DECREF(result);
1555 return NULL;
1556 }
1557 if (!rv) {
1558 if (set_add_entry((PySetObject *)result, entry) == -1) {
1559 Py_DECREF(result);
1560 return NULL;
1561 }
1562 }
1563 }
1564 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001565}
1566
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001567static PyObject *
1568set_difference_multi(PySetObject *so, PyObject *args)
1569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 Py_ssize_t i;
1571 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 if (PyTuple_GET_SIZE(args) == 0)
1574 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 other = PyTuple_GET_ITEM(args, 0);
1577 result = set_difference(so, other);
1578 if (result == NULL)
1579 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1582 other = PyTuple_GET_ITEM(args, i);
1583 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1584 Py_DECREF(result);
1585 return NULL;
1586 }
1587 }
1588 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001589}
1590
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001591PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001592"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001593\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001594(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001595static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001596set_sub(PySetObject *so, PyObject *other)
1597{
Brian Curtindfc80e32011-08-10 20:28:54 -05001598 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1599 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001601}
1602
1603static PyObject *
1604set_isub(PySetObject *so, PyObject *other)
1605{
Brian Curtindfc80e32011-08-10 20:28:54 -05001606 if (!PyAnySet_Check(other))
1607 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 if (set_difference_update_internal(so, other) == -1)
1609 return NULL;
1610 Py_INCREF(so);
1611 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001612}
1613
1614static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001615set_symmetric_difference_update(PySetObject *so, PyObject *other)
1616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 PySetObject *otherset;
1618 PyObject *key;
1619 Py_ssize_t pos = 0;
1620 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if ((PyObject *)so == other)
1623 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (PyDict_CheckExact(other)) {
1626 PyObject *value;
1627 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001628 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1630 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001631
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001632 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 an_entry.hash = hash;
1634 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001637 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001638 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001641 if (rv == DISCARD_NOTFOUND) {
1642 if (set_add_entry(so, &an_entry) == -1) {
1643 Py_DECREF(key);
1644 return NULL;
1645 }
1646 }
Georg Brandl2d444492010-09-03 10:52:55 +00001647 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 }
1649 Py_RETURN_NONE;
1650 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 if (PyAnySet_Check(other)) {
1653 Py_INCREF(other);
1654 otherset = (PySetObject *)other;
1655 } else {
1656 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1657 if (otherset == NULL)
1658 return NULL;
1659 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 while (set_next(otherset, &pos, &entry)) {
1662 int rv = set_discard_entry(so, entry);
1663 if (rv == -1) {
1664 Py_DECREF(otherset);
1665 return NULL;
1666 }
1667 if (rv == DISCARD_NOTFOUND) {
1668 if (set_add_entry(so, entry) == -1) {
1669 Py_DECREF(otherset);
1670 return NULL;
1671 }
1672 }
1673 }
1674 Py_DECREF(otherset);
1675 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001676}
1677
1678PyDoc_STRVAR(symmetric_difference_update_doc,
1679"Update a set with the symmetric difference of itself and another.");
1680
1681static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001682set_symmetric_difference(PySetObject *so, PyObject *other)
1683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 PyObject *rv;
1685 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1688 if (otherset == NULL)
1689 return NULL;
1690 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1691 if (rv == NULL)
1692 return NULL;
1693 Py_DECREF(rv);
1694 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001695}
1696
1697PyDoc_STRVAR(symmetric_difference_doc,
1698"Return the symmetric difference of two sets as a new set.\n\
1699\n\
1700(i.e. all elements that are in exactly one of the sets.)");
1701
1702static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001703set_xor(PySetObject *so, PyObject *other)
1704{
Brian Curtindfc80e32011-08-10 20:28:54 -05001705 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1706 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001708}
1709
1710static PyObject *
1711set_ixor(PySetObject *so, PyObject *other)
1712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001714
Brian Curtindfc80e32011-08-10 20:28:54 -05001715 if (!PyAnySet_Check(other))
1716 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 result = set_symmetric_difference_update(so, other);
1718 if (result == NULL)
1719 return NULL;
1720 Py_DECREF(result);
1721 Py_INCREF(so);
1722 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001723}
1724
1725static PyObject *
1726set_issubset(PySetObject *so, PyObject *other)
1727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 setentry *entry;
1729 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 if (!PyAnySet_Check(other)) {
1732 PyObject *tmp, *result;
1733 tmp = make_new_set(&PySet_Type, other);
1734 if (tmp == NULL)
1735 return NULL;
1736 result = set_issubset(so, tmp);
1737 Py_DECREF(tmp);
1738 return result;
1739 }
1740 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1741 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 while (set_next(so, &pos, &entry)) {
1744 int rv = set_contains_entry((PySetObject *)other, entry);
1745 if (rv == -1)
1746 return NULL;
1747 if (!rv)
1748 Py_RETURN_FALSE;
1749 }
1750 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001751}
1752
1753PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1754
1755static PyObject *
1756set_issuperset(PySetObject *so, PyObject *other)
1757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 if (!PyAnySet_Check(other)) {
1761 tmp = make_new_set(&PySet_Type, other);
1762 if (tmp == NULL)
1763 return NULL;
1764 result = set_issuperset(so, tmp);
1765 Py_DECREF(tmp);
1766 return result;
1767 }
1768 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001769}
1770
1771PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1772
Raymond Hettingera690a992003-11-16 16:17:49 +00001773static PyObject *
1774set_richcompare(PySetObject *v, PyObject *w, int op)
1775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001777
Brian Curtindfc80e32011-08-10 20:28:54 -05001778 if(!PyAnySet_Check(w))
1779 Py_RETURN_NOTIMPLEMENTED;
1780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 switch (op) {
1782 case Py_EQ:
1783 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1784 Py_RETURN_FALSE;
1785 if (v->hash != -1 &&
1786 ((PySetObject *)w)->hash != -1 &&
1787 v->hash != ((PySetObject *)w)->hash)
1788 Py_RETURN_FALSE;
1789 return set_issubset(v, w);
1790 case Py_NE:
1791 r1 = set_richcompare(v, w, Py_EQ);
1792 if (r1 == NULL)
1793 return NULL;
1794 r2 = PyBool_FromLong(PyObject_Not(r1));
1795 Py_DECREF(r1);
1796 return r2;
1797 case Py_LE:
1798 return set_issubset(v, w);
1799 case Py_GE:
1800 return set_issuperset(v, w);
1801 case Py_LT:
1802 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1803 Py_RETURN_FALSE;
1804 return set_issubset(v, w);
1805 case Py_GT:
1806 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1807 Py_RETURN_FALSE;
1808 return set_issuperset(v, w);
1809 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001810 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001811}
1812
Raymond Hettingera690a992003-11-16 16:17:49 +00001813static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001814set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 if (set_add_key(so, key) == -1)
1817 return NULL;
1818 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001819}
1820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001822"Add an element to a set.\n\
1823\n\
1824This has no effect if the element is already present.");
1825
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001826static int
1827set_contains(PySetObject *so, PyObject *key)
1828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 PyObject *tmpkey;
1830 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 rv = set_contains_key(so, key);
1833 if (rv == -1) {
1834 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1835 return -1;
1836 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001837 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 if (tmpkey == NULL)
1839 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001840 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 Py_DECREF(tmpkey);
1842 }
1843 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001844}
1845
1846static PyObject *
1847set_direct_contains(PySetObject *so, PyObject *key)
1848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 result = set_contains(so, key);
1852 if (result == -1)
1853 return NULL;
1854 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001855}
1856
1857PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1858
Raymond Hettingera690a992003-11-16 16:17:49 +00001859static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001860set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 PyObject *tmpkey;
1863 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 rv = set_discard_key(so, key);
1866 if (rv == -1) {
1867 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1868 return NULL;
1869 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001870 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 if (tmpkey == NULL)
1872 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 Py_DECREF(tmpkey);
1875 if (rv == -1)
1876 return NULL;
1877 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001880 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 return NULL;
1882 }
1883 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001884}
1885
1886PyDoc_STRVAR(remove_doc,
1887"Remove an element from a set; it must be a member.\n\
1888\n\
1889If the element is not a member, raise a KeyError.");
1890
1891static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001892set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001893{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001894 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 rv = set_discard_key(so, key);
1898 if (rv == -1) {
1899 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1900 return NULL;
1901 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001902 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 if (tmpkey == NULL)
1904 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001905 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001907 if (rv == -1)
1908 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 }
1910 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001911}
1912
1913PyDoc_STRVAR(discard_doc,
1914"Remove an element from a set if it is a member.\n\
1915\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001917
1918static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001919set_reduce(PySetObject *so)
1920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001922 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 keys = PySequence_List((PyObject *)so);
1925 if (keys == NULL)
1926 goto done;
1927 args = PyTuple_Pack(1, keys);
1928 if (args == NULL)
1929 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001930 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 if (dict == NULL) {
1932 PyErr_Clear();
1933 dict = Py_None;
1934 Py_INCREF(dict);
1935 }
1936 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001937done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 Py_XDECREF(args);
1939 Py_XDECREF(keys);
1940 Py_XDECREF(dict);
1941 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001942}
1943
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001944static PyObject *
1945set_sizeof(PySetObject *so)
1946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 res = sizeof(PySetObject);
1950 if (so->table != so->smalltable)
1951 res = res + (so->mask + 1) * sizeof(setentry);
1952 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001953}
1954
1955PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001956static int
1957set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (!PyAnySet_Check(self))
1962 return -1;
1963 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1964 return -1;
1965 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1966 return -1;
1967 set_clear_internal(self);
1968 self->hash = -1;
1969 if (iterable == NULL)
1970 return 0;
1971 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001972}
1973
Raymond Hettingera690a992003-11-16 16:17:49 +00001974static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 set_len, /* sq_length */
1976 0, /* sq_concat */
1977 0, /* sq_repeat */
1978 0, /* sq_item */
1979 0, /* sq_slice */
1980 0, /* sq_ass_item */
1981 0, /* sq_ass_slice */
1982 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001983};
1984
1985/* set object ********************************************************/
1986
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001987#ifdef Py_DEBUG
1988static PyObject *test_c_api(PySetObject *so);
1989
1990PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1991All is well if assertions don't fail.");
1992#endif
1993
Raymond Hettingera690a992003-11-16 16:17:49 +00001994static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 {"add", (PyCFunction)set_add, METH_O,
1996 add_doc},
1997 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1998 clear_doc},
1999 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2000 contains_doc},
2001 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2002 copy_doc},
2003 {"discard", (PyCFunction)set_discard, METH_O,
2004 discard_doc},
2005 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2006 difference_doc},
2007 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2008 difference_update_doc},
2009 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2010 intersection_doc},
2011 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2012 intersection_update_doc},
2013 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2014 isdisjoint_doc},
2015 {"issubset", (PyCFunction)set_issubset, METH_O,
2016 issubset_doc},
2017 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2018 issuperset_doc},
2019 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2020 pop_doc},
2021 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2022 reduce_doc},
2023 {"remove", (PyCFunction)set_remove, METH_O,
2024 remove_doc},
2025 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2026 sizeof_doc},
2027 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2028 symmetric_difference_doc},
2029 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2030 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002031#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2033 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002034#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 {"union", (PyCFunction)set_union, METH_VARARGS,
2036 union_doc},
2037 {"update", (PyCFunction)set_update, METH_VARARGS,
2038 update_doc},
2039 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002040};
2041
2042static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 0, /*nb_add*/
2044 (binaryfunc)set_sub, /*nb_subtract*/
2045 0, /*nb_multiply*/
2046 0, /*nb_remainder*/
2047 0, /*nb_divmod*/
2048 0, /*nb_power*/
2049 0, /*nb_negative*/
2050 0, /*nb_positive*/
2051 0, /*nb_absolute*/
2052 0, /*nb_bool*/
2053 0, /*nb_invert*/
2054 0, /*nb_lshift*/
2055 0, /*nb_rshift*/
2056 (binaryfunc)set_and, /*nb_and*/
2057 (binaryfunc)set_xor, /*nb_xor*/
2058 (binaryfunc)set_or, /*nb_or*/
2059 0, /*nb_int*/
2060 0, /*nb_reserved*/
2061 0, /*nb_float*/
2062 0, /*nb_inplace_add*/
2063 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2064 0, /*nb_inplace_multiply*/
2065 0, /*nb_inplace_remainder*/
2066 0, /*nb_inplace_power*/
2067 0, /*nb_inplace_lshift*/
2068 0, /*nb_inplace_rshift*/
2069 (binaryfunc)set_iand, /*nb_inplace_and*/
2070 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2071 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002072};
2073
2074PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002075"set() -> new empty set object\n\
2076set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002077\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002078Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002079
2080PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2082 "set", /* tp_name */
2083 sizeof(PySetObject), /* tp_basicsize */
2084 0, /* tp_itemsize */
2085 /* methods */
2086 (destructor)set_dealloc, /* tp_dealloc */
2087 0, /* tp_print */
2088 0, /* tp_getattr */
2089 0, /* tp_setattr */
2090 0, /* tp_reserved */
2091 (reprfunc)set_repr, /* tp_repr */
2092 &set_as_number, /* tp_as_number */
2093 &set_as_sequence, /* tp_as_sequence */
2094 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002095 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 0, /* tp_call */
2097 0, /* tp_str */
2098 PyObject_GenericGetAttr, /* tp_getattro */
2099 0, /* tp_setattro */
2100 0, /* tp_as_buffer */
2101 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2102 Py_TPFLAGS_BASETYPE, /* tp_flags */
2103 set_doc, /* tp_doc */
2104 (traverseproc)set_traverse, /* tp_traverse */
2105 (inquiry)set_clear_internal, /* tp_clear */
2106 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002107 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2108 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 0, /* tp_iternext */
2110 set_methods, /* tp_methods */
2111 0, /* tp_members */
2112 0, /* tp_getset */
2113 0, /* tp_base */
2114 0, /* tp_dict */
2115 0, /* tp_descr_get */
2116 0, /* tp_descr_set */
2117 0, /* tp_dictoffset */
2118 (initproc)set_init, /* tp_init */
2119 PyType_GenericAlloc, /* tp_alloc */
2120 set_new, /* tp_new */
2121 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002122};
2123
2124/* frozenset object ********************************************************/
2125
2126
2127static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2129 contains_doc},
2130 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2131 copy_doc},
2132 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2133 difference_doc},
2134 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2135 intersection_doc},
2136 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2137 isdisjoint_doc},
2138 {"issubset", (PyCFunction)set_issubset, METH_O,
2139 issubset_doc},
2140 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2141 issuperset_doc},
2142 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2143 reduce_doc},
2144 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2145 sizeof_doc},
2146 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2147 symmetric_difference_doc},
2148 {"union", (PyCFunction)set_union, METH_VARARGS,
2149 union_doc},
2150 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002151};
2152
2153static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 0, /*nb_add*/
2155 (binaryfunc)set_sub, /*nb_subtract*/
2156 0, /*nb_multiply*/
2157 0, /*nb_remainder*/
2158 0, /*nb_divmod*/
2159 0, /*nb_power*/
2160 0, /*nb_negative*/
2161 0, /*nb_positive*/
2162 0, /*nb_absolute*/
2163 0, /*nb_bool*/
2164 0, /*nb_invert*/
2165 0, /*nb_lshift*/
2166 0, /*nb_rshift*/
2167 (binaryfunc)set_and, /*nb_and*/
2168 (binaryfunc)set_xor, /*nb_xor*/
2169 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002170};
2171
2172PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002173"frozenset() -> empty frozenset object\n\
2174frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002175\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002176Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002177
2178PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2180 "frozenset", /* tp_name */
2181 sizeof(PySetObject), /* tp_basicsize */
2182 0, /* tp_itemsize */
2183 /* methods */
2184 (destructor)set_dealloc, /* tp_dealloc */
2185 0, /* tp_print */
2186 0, /* tp_getattr */
2187 0, /* tp_setattr */
2188 0, /* tp_reserved */
2189 (reprfunc)set_repr, /* tp_repr */
2190 &frozenset_as_number, /* tp_as_number */
2191 &set_as_sequence, /* tp_as_sequence */
2192 0, /* tp_as_mapping */
2193 frozenset_hash, /* tp_hash */
2194 0, /* tp_call */
2195 0, /* tp_str */
2196 PyObject_GenericGetAttr, /* tp_getattro */
2197 0, /* tp_setattro */
2198 0, /* tp_as_buffer */
2199 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2200 Py_TPFLAGS_BASETYPE, /* tp_flags */
2201 frozenset_doc, /* tp_doc */
2202 (traverseproc)set_traverse, /* tp_traverse */
2203 (inquiry)set_clear_internal, /* tp_clear */
2204 (richcmpfunc)set_richcompare, /* tp_richcompare */
2205 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2206 (getiterfunc)set_iter, /* tp_iter */
2207 0, /* tp_iternext */
2208 frozenset_methods, /* tp_methods */
2209 0, /* tp_members */
2210 0, /* tp_getset */
2211 0, /* tp_base */
2212 0, /* tp_dict */
2213 0, /* tp_descr_get */
2214 0, /* tp_descr_set */
2215 0, /* tp_dictoffset */
2216 0, /* tp_init */
2217 PyType_GenericAlloc, /* tp_alloc */
2218 frozenset_new, /* tp_new */
2219 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002220};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002221
2222
2223/***** C API functions *************************************************/
2224
2225PyObject *
2226PySet_New(PyObject *iterable)
2227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002229}
2230
2231PyObject *
2232PyFrozenSet_New(PyObject *iterable)
2233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002235}
2236
Neal Norwitz8c49c822006-03-04 18:41:19 +00002237Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002238PySet_Size(PyObject *anyset)
2239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 if (!PyAnySet_Check(anyset)) {
2241 PyErr_BadInternalCall();
2242 return -1;
2243 }
2244 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002245}
2246
2247int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002248PySet_Clear(PyObject *set)
2249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 if (!PySet_Check(set)) {
2251 PyErr_BadInternalCall();
2252 return -1;
2253 }
2254 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002255}
2256
2257int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002258PySet_Contains(PyObject *anyset, PyObject *key)
2259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 if (!PyAnySet_Check(anyset)) {
2261 PyErr_BadInternalCall();
2262 return -1;
2263 }
2264 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002265}
2266
2267int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002268PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 if (!PySet_Check(set)) {
2271 PyErr_BadInternalCall();
2272 return -1;
2273 }
2274 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002275}
2276
2277int
Christian Heimesfd66e512008-01-29 12:18:50 +00002278PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 if (!PySet_Check(anyset) &&
2281 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2282 PyErr_BadInternalCall();
2283 return -1;
2284 }
2285 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002286}
2287
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002288int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002289_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 if (!PyAnySet_Check(set)) {
2294 PyErr_BadInternalCall();
2295 return -1;
2296 }
2297 if (set_next((PySetObject *)set, pos, &entry) == 0)
2298 return 0;
2299 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002300 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002302}
2303
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002304PyObject *
2305PySet_Pop(PyObject *set)
2306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (!PySet_Check(set)) {
2308 PyErr_BadInternalCall();
2309 return NULL;
2310 }
2311 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002312}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002313
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002314int
2315_PySet_Update(PyObject *set, PyObject *iterable)
2316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!PySet_Check(set)) {
2318 PyErr_BadInternalCall();
2319 return -1;
2320 }
2321 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002322}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002323
Raymond Hettingere259f132013-12-15 11:56:14 -08002324/* Exported for the gdb plugin's benefit. */
2325PyObject *_PySet_Dummy = dummy;
2326
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002327#ifdef Py_DEBUG
2328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002330 Returns True and original set is restored. */
2331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332#define assertRaises(call_return_value, exception) \
2333 do { \
2334 assert(call_return_value); \
2335 assert(PyErr_ExceptionMatches(exception)); \
2336 PyErr_Clear(); \
2337 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002338
2339static PyObject *
2340test_c_api(PySetObject *so)
2341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 Py_ssize_t count;
2343 char *s;
2344 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002345 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002347 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 /* Verify preconditions */
2351 assert(PyAnySet_Check(ob));
2352 assert(PyAnySet_CheckExact(ob));
2353 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 /* so.clear(); so |= set("abc"); */
2356 str = PyUnicode_FromString("abc");
2357 if (str == NULL)
2358 return NULL;
2359 set_clear_internal(so);
2360 if (set_update_internal(so, str) == -1) {
2361 Py_DECREF(str);
2362 return NULL;
2363 }
2364 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 /* Exercise type/size checks */
2367 assert(PySet_Size(ob) == 3);
2368 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 /* Raise TypeError for non-iterable constructor arguments */
2371 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2372 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 /* Raise TypeError for unhashable key */
2375 dup = PySet_New(ob);
2376 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2377 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2378 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 /* Exercise successful pop, contains, add, and discard */
2381 elem = PySet_Pop(ob);
2382 assert(PySet_Contains(ob, elem) == 0);
2383 assert(PySet_GET_SIZE(ob) == 2);
2384 assert(PySet_Add(ob, elem) == 0);
2385 assert(PySet_Contains(ob, elem) == 1);
2386 assert(PySet_GET_SIZE(ob) == 3);
2387 assert(PySet_Discard(ob, elem) == 1);
2388 assert(PySet_GET_SIZE(ob) == 2);
2389 assert(PySet_Discard(ob, elem) == 0);
2390 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* Exercise clear */
2393 dup2 = PySet_New(dup);
2394 assert(PySet_Clear(dup2) == 0);
2395 assert(PySet_Size(dup2) == 0);
2396 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 /* Raise SystemError on clear or update of frozen set */
2399 f = PyFrozenSet_New(dup);
2400 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2401 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2402 assert(PySet_Add(f, elem) == 0);
2403 Py_INCREF(f);
2404 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2405 Py_DECREF(f);
2406 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 /* Exercise direct iteration */
2409 i = 0, count = 0;
2410 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2411 s = _PyUnicode_AsString(x);
2412 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2413 count++;
2414 }
2415 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 /* Exercise updates */
2418 dup2 = PySet_New(NULL);
2419 assert(_PySet_Update(dup2, dup) == 0);
2420 assert(PySet_Size(dup2) == 3);
2421 assert(_PySet_Update(dup2, dup) == 0);
2422 assert(PySet_Size(dup2) == 3);
2423 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 /* Raise SystemError when self argument is not a set or frozenset. */
2426 t = PyTuple_New(0);
2427 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2428 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2429 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 /* Raise SystemError when self argument is not a set. */
2432 f = PyFrozenSet_New(dup);
2433 assert(PySet_Size(f) == 3);
2434 assert(PyFrozenSet_CheckExact(f));
2435 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2436 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2437 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* Raise KeyError when popping from an empty set */
2440 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2441 Py_DECREF(ob);
2442 assert(PySet_GET_SIZE(ob) == 0);
2443 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 /* Restore the set from the copy using the PyNumber API */
2446 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2447 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 /* Verify constructors accept NULL arguments */
2450 f = PySet_New(NULL);
2451 assert(f != NULL);
2452 assert(PySet_GET_SIZE(f) == 0);
2453 Py_DECREF(f);
2454 f = PyFrozenSet_New(NULL);
2455 assert(f != NULL);
2456 assert(PyFrozenSet_CheckExact(f));
2457 assert(PySet_GET_SIZE(f) == 0);
2458 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 Py_DECREF(elem);
2461 Py_DECREF(dup);
2462 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002463}
2464
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002465#undef assertRaises
2466
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002467#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002468
2469/***** Dummy Struct *************************************************/
2470
2471static PyObject *
2472dummy_repr(PyObject *op)
2473{
2474 return PyUnicode_FromString("<dummy key>");
2475}
2476
2477static void
2478dummy_dealloc(PyObject* ignore)
2479{
2480 Py_FatalError("deallocating <dummy key>");
2481}
2482
2483static PyTypeObject _PySetDummy_Type = {
2484 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2485 "<dummy key> type",
2486 0,
2487 0,
2488 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2489 0, /*tp_print*/
2490 0, /*tp_getattr*/
2491 0, /*tp_setattr*/
2492 0, /*tp_reserved*/
2493 dummy_repr, /*tp_repr*/
2494 0, /*tp_as_number*/
2495 0, /*tp_as_sequence*/
2496 0, /*tp_as_mapping*/
2497 0, /*tp_hash */
2498 0, /*tp_call */
2499 0, /*tp_str */
2500 0, /*tp_getattro */
2501 0, /*tp_setattro */
2502 0, /*tp_as_buffer */
2503 Py_TPFLAGS_DEFAULT, /*tp_flags */
2504};
2505
2506static PyObject _dummy_struct = {
2507 _PyObject_EXTRA_INIT
2508 2, &_PySetDummy_Type
2509};
2510