blob: b63a96684e83966380e6ae0594f350526f7e36e9 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettinger6c3c1cc2013-08-31 21:34:24 -07007 Copyright (c) 2003-2013 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
26 Unlike the dictionary implementation, the lookkey functions can return
27 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettingerc56e0e32013-09-02 16:32:27 -070034/* This must be >= 1 */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035#define PERTURB_SHIFT 5
Raymond Hettingerc56e0e32013-09-02 16:32:27 -070036
37/* This should be >= PySet_MINSIZE - 1 */
Raymond Hettingera35adf52013-09-02 03:23:21 -070038#define LINEAR_PROBES 9
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
40/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050041static PyObject _dummy_struct;
42
43#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000044
Antoine Pitrou9d952542013-08-24 21:07:07 +020045/* Exported for the gdb plugin's benefit. */
46PyObject *_PySet_Dummy = dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define INIT_NONZERO_SET_SLOTS(so) do { \
49 (so)->table = (so)->smalltable; \
50 (so)->mask = PySet_MINSIZE - 1; \
51 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000052 } while(0)
53
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054#define EMPTY_TO_MINSIZE(so) do { \
55 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
56 (so)->used = (so)->fill = 0; \
57 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000058 } while(0)
59
Raymond Hettingerbc841a12005-08-07 13:02:53 +000060/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000061#ifndef PySet_MAXFREELIST
62#define PySet_MAXFREELIST 80
63#endif
64static PySetObject *free_list[PySet_MAXFREELIST];
65static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000066
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000067static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020068set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070071 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020072 setentry *entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070073 setentry *limit;
Raymond Hettingerafe89092013-08-28 20:59:31 -070074 size_t perturb = hash;
75 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070076 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
77 size_t j;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020078 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000079
Raymond Hettingera35adf52013-09-02 03:23:21 -070080 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070081 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070083
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070084 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070086 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070087 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070088 PyObject *startkey = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 Py_INCREF(startkey);
90 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
91 Py_DECREF(startkey);
92 if (cmp < 0)
93 return NULL;
Raymond Hettingerafe89092013-08-28 20:59:31 -070094 if (table != so->table || entry->key != startkey)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 return set_lookkey(so, key, hash);
Raymond Hettingerafe89092013-08-28 20:59:31 -070096 if (cmp > 0)
97 return entry;
98 }
99 if (entry->key == dummy && freeslot == NULL)
100 freeslot = entry;
101
Raymond Hettingera35adf52013-09-02 03:23:21 -0700102 limit = &table[mask];
103 for (j = 0 ; j < LINEAR_PROBES ; j++) {
104 entry = (entry == limit) ? &table[0] : entry + 1;
105 if (entry->key == NULL)
106 goto found_null;
107 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -0700108 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700109 if (entry->hash == hash && entry->key != dummy) {
110 PyObject *startkey = entry->key;
111 Py_INCREF(startkey);
112 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
113 Py_DECREF(startkey);
114 if (cmp < 0)
115 return NULL;
116 if (table != so->table || entry->key != startkey)
117 return set_lookkey(so, key, hash);
118 if (cmp > 0)
119 return entry;
120 }
121 if (entry->key == dummy && freeslot == NULL)
122 freeslot = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700124
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700125 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700126 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700127
Raymond Hettingera35adf52013-09-02 03:23:21 -0700128 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700129 if (entry->key == NULL)
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700130 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700132 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700133 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000134}
135
136/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000137 * Hacked up version of set_lookkey which can assume keys are always unicode;
138 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000139 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000140 */
141static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200142set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700145 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200146 setentry *entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700147 setentry *limit;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700148 size_t perturb = hash;
149 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700150 size_t i = (size_t)hash;
151 size_t j;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 /* Make sure this function doesn't have to handle non-unicode keys,
154 including subclasses of str; e.g., one reason to subclass
155 strings is to override __eq__, and for speed we don't cater to
156 that here. */
157 if (!PyUnicode_CheckExact(key)) {
158 so->lookup = set_lookkey;
159 return set_lookkey(so, key, hash);
160 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700161
Raymond Hettingera35adf52013-09-02 03:23:21 -0700162 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700163 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000165
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700166 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700167 if (entry->key == key
168 || (entry->hash == hash
169 && entry->key != dummy
170 && unicode_eq(entry->key, key)))
171 return entry;
172 if (entry->key == dummy && freeslot == NULL)
173 freeslot = entry;
174
Raymond Hettingera35adf52013-09-02 03:23:21 -0700175 limit = &table[mask];
176 for (j = 0 ; j < LINEAR_PROBES ; j++) {
177 entry = (entry == limit) ? &table[0] : entry + 1;
178 if (entry->key == NULL)
179 goto found_null;
180 if (entry->key == key
181 || (entry->hash == hash
182 && entry->key != dummy
183 && unicode_eq(entry->key, key)))
184 return entry;
185 if (entry->key == dummy && freeslot == NULL)
186 freeslot = entry;
187 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700188
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700189 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700190 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700191
Raymond Hettingera35adf52013-09-02 03:23:21 -0700192 entry = &table[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (entry->key == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -0700194 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700196 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700197 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000198}
199
200/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000201Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000202Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000203Eats a reference to key.
204*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000205static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200206set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000207{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200208 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 assert(so->lookup != NULL);
211 entry = so->lookup(so, key, hash);
212 if (entry == NULL)
213 return -1;
214 if (entry->key == NULL) {
215 /* UNUSED */
216 so->fill++;
217 entry->key = key;
218 entry->hash = hash;
219 so->used++;
220 } else if (entry->key == dummy) {
221 /* DUMMY */
222 entry->key = key;
223 entry->hash = hash;
224 so->used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 } else {
226 /* ACTIVE */
227 Py_DECREF(key);
228 }
229 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000230}
231
232/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000233Internal routine used by set_table_resize() to insert an item which is
234known to be absent from the set. This routine also assumes that
235the set contains no deleted entries. Besides the performance benefit,
236using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
237Note that no refcounts are changed by this routine; if needed, the caller
238is responsible for incref'ing `key`.
239*/
240static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200241set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200244 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700245 size_t perturb = hash;
246 size_t mask = (size_t)so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700247 size_t i = (size_t)hash;
248 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000249
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700250 while (1) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700251 entry = &table[i & mask];
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700252 if (entry->key == NULL)
Raymond Hettingera35adf52013-09-02 03:23:21 -0700253 goto found_null;
254 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
255 entry = &table[(i + j) & mask];
256 if (entry->key == NULL)
257 goto found_null;
258 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700259 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700260 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700262 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 so->fill++;
264 entry->key = key;
265 entry->hash = hash;
266 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000267}
268
269/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000271keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272actually be smaller than the old one.
273*/
274static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_ssize_t newsize;
278 setentry *oldtable, *newtable, *entry;
279 Py_ssize_t i;
280 int is_oldtable_malloced;
281 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 /* Find the smallest table size > minused. */
286 for (newsize = PySet_MINSIZE;
287 newsize <= minused && newsize > 0;
288 newsize <<= 1)
289 ;
290 if (newsize <= 0) {
291 PyErr_NoMemory();
292 return -1;
293 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* Get space for a new table. */
296 oldtable = so->table;
297 assert(oldtable != NULL);
298 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 if (newsize == PySet_MINSIZE) {
301 /* A large table is shrinking, or we can't get any smaller. */
302 newtable = so->smalltable;
303 if (newtable == oldtable) {
304 if (so->fill == so->used) {
305 /* No dummies, so no point doing anything. */
306 return 0;
307 }
308 /* We're not going to resize it, but rebuild the
309 table anyway to purge old dummy entries.
310 Subtle: This is *necessary* if fill==size,
311 as set_lookkey needs at least one virgin slot to
312 terminate failing searches. If fill < size, it's
313 merely desirable, as dummies slow searches. */
314 assert(so->fill > so->used);
315 memcpy(small_copy, oldtable, sizeof(small_copy));
316 oldtable = small_copy;
317 }
318 }
319 else {
320 newtable = PyMem_NEW(setentry, newsize);
321 if (newtable == NULL) {
322 PyErr_NoMemory();
323 return -1;
324 }
325 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 /* Make the set empty, using the new table. */
328 assert(newtable != oldtable);
329 so->table = newtable;
330 so->mask = newsize - 1;
331 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700332 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* Copy the data over; this is refcount-neutral for active entries;
337 dummy entries aren't copied over, of course */
338 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500339 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000341 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 }
343 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 if (is_oldtable_malloced)
346 PyMem_DEL(oldtable);
347 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000348}
349
Raymond Hettingerc991db22005-08-11 07:58:45 +0000350/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
351
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000352static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200353set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000354{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200355 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000356 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100357 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 assert(so->fill <= so->mask); /* at least one empty slot */
360 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000361 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100362 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000363 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 return -1;
365 }
366 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
367 return 0;
368 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000369}
370
371static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200372set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000373{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200374 Py_hash_t hash;
375 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200378 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 hash = PyObject_Hash(key);
380 if (hash == -1)
381 return -1;
382 }
383 assert(so->fill <= so->mask); /* at least one empty slot */
384 n_used = so->used;
385 Py_INCREF(key);
386 if (set_insert_key(so, key, hash) == -1) {
387 Py_DECREF(key);
388 return -1;
389 }
390 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
391 return 0;
392 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000393}
394
395#define DISCARD_NOTFOUND 0
396#define DISCARD_FOUND 1
397
398static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000399set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200400{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000402
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000403 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 if (entry == NULL)
405 return -1;
406 if (entry->key == NULL || entry->key == dummy)
407 return DISCARD_NOTFOUND;
408 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 entry->key = dummy;
410 so->used--;
411 Py_DECREF(old_key);
412 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000413}
414
415static int
416set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000417{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200418 Py_hash_t hash;
419 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200425 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 hash = PyObject_Hash(key);
427 if (hash == -1)
428 return -1;
429 }
430 entry = (so->lookup)(so, key, hash);
431 if (entry == NULL)
432 return -1;
433 if (entry->key == NULL || entry->key == dummy)
434 return DISCARD_NOTFOUND;
435 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 entry->key = dummy;
437 so->used--;
438 Py_DECREF(old_key);
439 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000440}
441
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000442static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443set_clear_internal(PySetObject *so)
444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 setentry *entry, *table;
446 int table_is_malloced;
447 Py_ssize_t fill;
448 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 Py_ssize_t i, n;
451 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 n = so->mask + 1;
454 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455#endif
456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 table = so->table;
458 assert(table != NULL);
459 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 /* This is delicate. During the process of clearing the set,
462 * decrefs can cause the set to mutate. To avoid fatal confusion
463 * (voice of experience), we have to make the set empty before
464 * clearing the slots, and never refer to anything via so->ref while
465 * clearing.
466 */
467 fill = so->fill;
468 if (table_is_malloced)
469 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 else if (fill > 0) {
472 /* It's a small table with something that needs to be cleared.
473 * Afraid the only safe way is to copy the set entries into
474 * another small table first.
475 */
476 memcpy(small_copy, table, sizeof(small_copy));
477 table = small_copy;
478 EMPTY_TO_MINSIZE(so);
479 }
480 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 /* Now we can finally clear things. If C had refcounts, we could
483 * assert that the refcount on table is 1 now, i.e. that this function
484 * has unique access to it, so decref side-effects can't alter it.
485 */
486 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 assert(i < n);
489 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000490#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (entry->key) {
492 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700493 if (entry->key != dummy)
494 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 else
498 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (table_is_malloced)
503 PyMem_DEL(table);
504 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505}
506
507/*
508 * Iterate over a set table. Use like so:
509 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000511 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000512 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000513 * while (set_next(yourset, &pos, &entry)) {
514 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515 * }
516 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000517 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519 */
520static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000521set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 Py_ssize_t i;
524 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200525 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 assert (PyAnySet_Check(so));
528 i = *pos_ptr;
529 assert(i >= 0);
530 table = so->table;
531 mask = so->mask;
532 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
533 i++;
534 *pos_ptr = i+1;
535 if (i > mask)
536 return 0;
537 assert(table[i].key != NULL);
538 *entry_ptr = &table[i];
539 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540}
541
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000542static void
543set_dealloc(PySetObject *so)
544{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200545 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 Py_ssize_t fill = so->fill;
547 PyObject_GC_UnTrack(so);
548 Py_TRASHCAN_SAFE_BEGIN(so)
549 if (so->weakreflist != NULL)
550 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 for (entry = so->table; fill > 0; entry++) {
553 if (entry->key) {
554 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700555 if (entry->key != dummy)
556 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 }
558 }
559 if (so->table != so->smalltable)
560 PyMem_DEL(so->table);
561 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
562 free_list[numfree++] = so;
563 else
564 Py_TYPE(so)->tp_free(so);
565 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000566}
567
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568static PyObject *
569set_repr(PySetObject *so)
570{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200571 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (status != 0) {
575 if (status < 0)
576 return NULL;
577 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
578 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 /* shortcut for the empty set */
581 if (!so->used) {
582 Py_ReprLeave((PyObject*)so);
583 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
584 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 keys = PySequence_List((PyObject *)so);
587 if (keys == NULL)
588 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000589
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200590 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 listrepr = PyObject_Repr(keys);
592 Py_DECREF(keys);
593 if (listrepr == NULL)
594 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200595 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200597 if (tmp == NULL)
598 goto done;
599 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200600
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200601 if (Py_TYPE(so) != &PySet_Type)
602 result = PyUnicode_FromFormat("%s({%U})",
603 Py_TYPE(so)->tp_name,
604 listrepr);
605 else
606 result = PyUnicode_FromFormat("{%U}", listrepr);
607 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000608done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 Py_ReprLeave((PyObject*)so);
610 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000611}
612
Martin v. Löwis18e16552006-02-15 17:27:45 +0000613static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000614set_len(PyObject *so)
615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000617}
618
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000619static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000620set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000623 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100624 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200625 Py_ssize_t i;
626 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 assert (PyAnySet_Check(so));
629 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 other = (PySetObject*)otherset;
632 if (other == so || other->used == 0)
633 /* a.update(a) or a.update({}); nothing to do */
634 return 0;
635 /* Do one big resize at the start, rather than
636 * incrementally resizing as we insert new keys. Expect
637 * that there will be no (or few) overlapping keys.
638 */
639 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
640 if (set_table_resize(so, (so->used + other->used)*2) != 0)
641 return -1;
642 }
643 for (i = 0; i <= other->mask; i++) {
644 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000645 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100646 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000647 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500648 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000649 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100650 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000651 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 return -1;
653 }
654 }
655 }
656 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000657}
658
659static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000660set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000661{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000662 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200666 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 hash = PyObject_Hash(key);
668 if (hash == -1)
669 return -1;
670 }
671 entry = (so->lookup)(so, key, hash);
672 if (entry == NULL)
673 return -1;
674 key = entry->key;
675 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000676}
677
Raymond Hettingerc991db22005-08-11 07:58:45 +0000678static int
679set_contains_entry(PySetObject *so, setentry *entry)
680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 PyObject *key;
682 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000683
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000684 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 if (lu_entry == NULL)
686 return -1;
687 key = lu_entry->key;
688 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000689}
690
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000691static PyObject *
692set_pop(PySetObject *so)
693{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200694 Py_ssize_t i = 0;
695 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 assert (PyAnySet_Check(so));
699 if (so->used == 0) {
700 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
701 return NULL;
702 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 /* Set entry to "the first" unused or dummy set entry. We abuse
705 * the hash field of slot 0 to hold a search finger:
706 * If slot 0 has a value, use slot 0.
707 * Else slot 0 is being used to hold a search finger,
708 * and we use its hash value as the first index to look.
709 */
710 entry = &so->table[0];
711 if (entry->key == NULL || entry->key == dummy) {
712 i = entry->hash;
713 /* The hash field may be a real hash value, or it may be a
714 * legit search finger, or it may be a once-legit search
715 * finger that's out of bounds now because it wrapped around
716 * or the table shrunk -- simply make sure it's in bounds now.
717 */
718 if (i > so->mask || i < 1)
719 i = 1; /* skip slot 0 */
720 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
721 i++;
722 if (i > so->mask)
723 i = 1;
724 }
725 }
726 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 entry->key = dummy;
728 so->used--;
729 so->table[0].hash = i + 1; /* next place to start */
730 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000731}
732
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000733PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
734Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000735
736static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000737set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 Py_ssize_t pos = 0;
740 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 while (set_next(so, &pos, &entry))
743 Py_VISIT(entry->key);
744 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000745}
746
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000747static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000748frozenset_hash(PyObject *self)
749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800751 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 setentry *entry;
753 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 if (so->hash != -1)
756 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000757
Mark Dickinson57e683e2011-09-24 18:18:40 +0100758 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 while (set_next(so, &pos, &entry)) {
760 /* Work to increase the bit dispersion for closely spaced hash
761 values. The is important because some use cases have many
762 combinations of a small number of elements with nearby
763 hashes so that many distinct combinations collapse to only
764 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000765 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800766 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800768 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800770 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 so->hash = hash;
772 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000773}
774
Raymond Hettingera9d99362005-08-05 00:01:15 +0000775/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000776
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 PyObject_HEAD
779 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
780 Py_ssize_t si_used;
781 Py_ssize_t si_pos;
782 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000783} setiterobject;
784
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000785static void
786setiter_dealloc(setiterobject *si)
787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 Py_XDECREF(si->si_set);
789 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000790}
791
792static int
793setiter_traverse(setiterobject *si, visitproc visit, void *arg)
794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 Py_VISIT(si->si_set);
796 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797}
798
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000799static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800setiter_len(setiterobject *si)
801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 Py_ssize_t len = 0;
803 if (si->si_set != NULL && si->si_used == si->si_set->used)
804 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000805 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000806}
807
Armin Rigof5b3e362006-02-11 21:32:43 +0000808PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000809
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000810static PyObject *setiter_iternext(setiterobject *si);
811
812static PyObject *
813setiter_reduce(setiterobject *si)
814{
815 PyObject *list;
816 setiterobject tmp;
817
818 list = PyList_New(0);
819 if (!list)
820 return NULL;
821
Ezio Melotti0e1af282012-09-28 16:43:40 +0300822 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000823 tmp = *si;
824 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300825
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000826 /* iterate the temporary into a list */
827 for(;;) {
828 PyObject *element = setiter_iternext(&tmp);
829 if (element) {
830 if (PyList_Append(list, element)) {
831 Py_DECREF(element);
832 Py_DECREF(list);
833 Py_XDECREF(tmp.si_set);
834 return NULL;
835 }
836 Py_DECREF(element);
837 } else
838 break;
839 }
840 Py_XDECREF(tmp.si_set);
841 /* check for error */
842 if (tmp.si_set != NULL) {
843 /* we have an error */
844 Py_DECREF(list);
845 return NULL;
846 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200847 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000848}
849
850PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
851
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000852static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000854 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856};
857
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000858static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200861 Py_ssize_t i, mask;
862 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 if (so == NULL)
866 return NULL;
867 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (si->si_used != so->used) {
870 PyErr_SetString(PyExc_RuntimeError,
871 "Set changed size during iteration");
872 si->si_used = -1; /* Make this state sticky */
873 return NULL;
874 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 i = si->si_pos;
877 assert(i>=0);
878 entry = so->table;
879 mask = so->mask;
880 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
881 i++;
882 si->si_pos = i+1;
883 if (i > mask)
884 goto fail;
885 si->len--;
886 key = entry[i].key;
887 Py_INCREF(key);
888 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000889
890fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 Py_DECREF(so);
892 si->si_set = NULL;
893 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000894}
895
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000896PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PyVarObject_HEAD_INIT(&PyType_Type, 0)
898 "set_iterator", /* tp_name */
899 sizeof(setiterobject), /* tp_basicsize */
900 0, /* tp_itemsize */
901 /* methods */
902 (destructor)setiter_dealloc, /* tp_dealloc */
903 0, /* tp_print */
904 0, /* tp_getattr */
905 0, /* tp_setattr */
906 0, /* tp_reserved */
907 0, /* tp_repr */
908 0, /* tp_as_number */
909 0, /* tp_as_sequence */
910 0, /* tp_as_mapping */
911 0, /* tp_hash */
912 0, /* tp_call */
913 0, /* tp_str */
914 PyObject_GenericGetAttr, /* tp_getattro */
915 0, /* tp_setattro */
916 0, /* tp_as_buffer */
917 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
918 0, /* tp_doc */
919 (traverseproc)setiter_traverse, /* tp_traverse */
920 0, /* tp_clear */
921 0, /* tp_richcompare */
922 0, /* tp_weaklistoffset */
923 PyObject_SelfIter, /* tp_iter */
924 (iternextfunc)setiter_iternext, /* tp_iternext */
925 setiter_methods, /* tp_methods */
926 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000927};
928
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000929static PyObject *
930set_iter(PySetObject *so)
931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
933 if (si == NULL)
934 return NULL;
935 Py_INCREF(so);
936 si->si_set = so;
937 si->si_used = so->used;
938 si->si_pos = 0;
939 si->len = so->used;
940 _PyObject_GC_TRACK(si);
941 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000942}
943
Raymond Hettingerd7946662005-08-01 21:39:29 +0000944static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000945set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (PyAnySet_Check(other))
950 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 if (PyDict_CheckExact(other)) {
953 PyObject *value;
954 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000955 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 /* Do one big resize at the start, rather than
959 * incrementally resizing as we insert new keys. Expect
960 * that there will be no (or few) overlapping keys.
961 */
962 if (dictsize == -1)
963 return -1;
964 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
965 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
966 return -1;
967 }
968 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
969 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 an_entry.hash = hash;
972 an_entry.key = key;
973 if (set_add_entry(so, &an_entry) == -1)
974 return -1;
975 }
976 return 0;
977 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 it = PyObject_GetIter(other);
980 if (it == NULL)
981 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 while ((key = PyIter_Next(it)) != NULL) {
984 if (set_add_key(so, key) == -1) {
985 Py_DECREF(it);
986 Py_DECREF(key);
987 return -1;
988 }
989 Py_DECREF(key);
990 }
991 Py_DECREF(it);
992 if (PyErr_Occurred())
993 return -1;
994 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000995}
996
997static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000998set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1003 PyObject *other = PyTuple_GET_ITEM(args, i);
1004 if (set_update_internal(so, other) == -1)
1005 return NULL;
1006 }
1007 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001008}
1009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001011"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001012
1013static PyObject *
1014make_new_set(PyTypeObject *type, PyObject *iterable)
1015{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001016 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 /* create PySetObject structure */
1019 if (numfree &&
1020 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1021 so = free_list[--numfree];
1022 assert (so != NULL && PyAnySet_CheckExact(so));
1023 Py_TYPE(so) = type;
1024 _Py_NewReference((PyObject *)so);
1025 EMPTY_TO_MINSIZE(so);
1026 PyObject_GC_Track(so);
1027 } else {
1028 so = (PySetObject *)type->tp_alloc(type, 0);
1029 if (so == NULL)
1030 return NULL;
1031 /* tp_alloc has already zeroed the structure */
1032 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1033 INIT_NONZERO_SET_SLOTS(so);
1034 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 so->lookup = set_lookkey_unicode;
1037 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 if (iterable != NULL) {
1040 if (set_update_internal(so, iterable) == -1) {
1041 Py_DECREF(so);
1042 return NULL;
1043 }
1044 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001047}
1048
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001049static PyObject *
1050make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1053 if (PyType_IsSubtype(type, &PySet_Type))
1054 type = &PySet_Type;
1055 else
1056 type = &PyFrozenSet_Type;
1057 }
1058 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001059}
1060
Raymond Hettingerd7946662005-08-01 21:39:29 +00001061/* The empty frozenset is a singleton */
1062static PyObject *emptyfrozenset = NULL;
1063
Raymond Hettingera690a992003-11-16 16:17:49 +00001064static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001065frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1070 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1073 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (type != &PyFrozenSet_Type)
1076 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (iterable != NULL) {
1079 /* frozenset(f) is idempotent */
1080 if (PyFrozenSet_CheckExact(iterable)) {
1081 Py_INCREF(iterable);
1082 return iterable;
1083 }
1084 result = make_new_set(type, iterable);
1085 if (result == NULL || PySet_GET_SIZE(result))
1086 return result;
1087 Py_DECREF(result);
1088 }
1089 /* The empty frozenset is a singleton */
1090 if (emptyfrozenset == NULL)
1091 emptyfrozenset = make_new_set(type, NULL);
1092 Py_XINCREF(emptyfrozenset);
1093 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001094}
1095
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001096int
1097PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001098{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001099 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 while (numfree) {
1103 numfree--;
1104 so = free_list[numfree];
1105 PyObject_GC_Del(so);
1106 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001107 return freelist_size;
1108}
1109
1110void
1111PySet_Fini(void)
1112{
1113 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001115}
1116
David Malcolm49526f42012-06-22 14:55:41 -04001117/* Print summary info about the state of the optimized allocator */
1118void
1119_PySet_DebugMallocStats(FILE *out)
1120{
1121 _PyDebugAllocatorStats(out,
1122 "free PySetObject",
1123 numfree, sizeof(PySetObject));
1124}
1125
1126
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001127static PyObject *
1128set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1131 return NULL;
1132
1133 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001134}
1135
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001136/* set_swap_bodies() switches the contents of any two sets by moving their
1137 internal data pointers and, if needed, copying the internal smalltables.
1138 Semantically equivalent to:
1139
1140 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1141
1142 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001144 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001146 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001147*/
1148
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001149static void
1150set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 Py_ssize_t t;
1153 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001154 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001156 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 t = a->fill; a->fill = b->fill; b->fill = t;
1159 t = a->used; a->used = b->used; b->used = t;
1160 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 u = a->table;
1163 if (a->table == a->smalltable)
1164 u = b->smalltable;
1165 a->table = b->table;
1166 if (b->table == b->smalltable)
1167 a->table = a->smalltable;
1168 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 if (a->table == a->smalltable || b->table == b->smalltable) {
1173 memcpy(tab, a->smalltable, sizeof(tab));
1174 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1175 memcpy(b->smalltable, tab, sizeof(tab));
1176 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1179 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1180 h = a->hash; a->hash = b->hash; b->hash = h;
1181 } else {
1182 a->hash = -1;
1183 b->hash = -1;
1184 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001185}
1186
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001187static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001188set_copy(PySetObject *so)
1189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001191}
1192
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001193static PyObject *
1194frozenset_copy(PySetObject *so)
1195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 if (PyFrozenSet_CheckExact(so)) {
1197 Py_INCREF(so);
1198 return (PyObject *)so;
1199 }
1200 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001201}
1202
Raymond Hettingera690a992003-11-16 16:17:49 +00001203PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1204
1205static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001206set_clear(PySetObject *so)
1207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 set_clear_internal(so);
1209 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001210}
1211
1212PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1213
1214static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001215set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 PySetObject *result;
1218 PyObject *other;
1219 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 result = (PySetObject *)set_copy(so);
1222 if (result == NULL)
1223 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1226 other = PyTuple_GET_ITEM(args, i);
1227 if ((PyObject *)so == other)
1228 continue;
1229 if (set_update_internal(result, other) == -1) {
1230 Py_DECREF(result);
1231 return NULL;
1232 }
1233 }
1234 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001235}
1236
1237PyDoc_STRVAR(union_doc,
1238 "Return the union of sets as a new set.\n\
1239\n\
1240(i.e. all elements that are in either set.)");
1241
1242static PyObject *
1243set_or(PySetObject *so, PyObject *other)
1244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001246
Brian Curtindfc80e32011-08-10 20:28:54 -05001247 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1248 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 result = (PySetObject *)set_copy(so);
1251 if (result == NULL)
1252 return NULL;
1253 if ((PyObject *)so == other)
1254 return (PyObject *)result;
1255 if (set_update_internal(result, other) == -1) {
1256 Py_DECREF(result);
1257 return NULL;
1258 }
1259 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001260}
1261
Raymond Hettingera690a992003-11-16 16:17:49 +00001262static PyObject *
1263set_ior(PySetObject *so, PyObject *other)
1264{
Brian Curtindfc80e32011-08-10 20:28:54 -05001265 if (!PyAnySet_Check(other))
1266 Py_RETURN_NOTIMPLEMENTED;
1267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (set_update_internal(so, other) == -1)
1269 return NULL;
1270 Py_INCREF(so);
1271 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001272}
1273
1274static PyObject *
1275set_intersection(PySetObject *so, PyObject *other)
1276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 PySetObject *result;
1278 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 if ((PyObject *)so == other)
1281 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1284 if (result == NULL)
1285 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (PyAnySet_Check(other)) {
1288 Py_ssize_t pos = 0;
1289 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1292 tmp = (PyObject *)so;
1293 so = (PySetObject *)other;
1294 other = tmp;
1295 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 while (set_next((PySetObject *)other, &pos, &entry)) {
1298 int rv = set_contains_entry(so, entry);
1299 if (rv == -1) {
1300 Py_DECREF(result);
1301 return NULL;
1302 }
1303 if (rv) {
1304 if (set_add_entry(result, entry) == -1) {
1305 Py_DECREF(result);
1306 return NULL;
1307 }
1308 }
1309 }
1310 return (PyObject *)result;
1311 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 it = PyObject_GetIter(other);
1314 if (it == NULL) {
1315 Py_DECREF(result);
1316 return NULL;
1317 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 while ((key = PyIter_Next(it)) != NULL) {
1320 int rv;
1321 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001322 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 if (hash == -1) {
1325 Py_DECREF(it);
1326 Py_DECREF(result);
1327 Py_DECREF(key);
1328 return NULL;
1329 }
1330 entry.hash = hash;
1331 entry.key = key;
1332 rv = set_contains_entry(so, &entry);
1333 if (rv == -1) {
1334 Py_DECREF(it);
1335 Py_DECREF(result);
1336 Py_DECREF(key);
1337 return NULL;
1338 }
1339 if (rv) {
1340 if (set_add_entry(result, &entry) == -1) {
1341 Py_DECREF(it);
1342 Py_DECREF(result);
1343 Py_DECREF(key);
1344 return NULL;
1345 }
1346 }
1347 Py_DECREF(key);
1348 }
1349 Py_DECREF(it);
1350 if (PyErr_Occurred()) {
1351 Py_DECREF(result);
1352 return NULL;
1353 }
1354 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001355}
1356
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001357static PyObject *
1358set_intersection_multi(PySetObject *so, PyObject *args)
1359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 Py_ssize_t i;
1361 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 if (PyTuple_GET_SIZE(args) == 0)
1364 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 Py_INCREF(so);
1367 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1368 PyObject *other = PyTuple_GET_ITEM(args, i);
1369 PyObject *newresult = set_intersection((PySetObject *)result, other);
1370 if (newresult == NULL) {
1371 Py_DECREF(result);
1372 return NULL;
1373 }
1374 Py_DECREF(result);
1375 result = newresult;
1376 }
1377 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001378}
1379
Raymond Hettingera690a992003-11-16 16:17:49 +00001380PyDoc_STRVAR(intersection_doc,
1381"Return the intersection of two sets as a new set.\n\
1382\n\
1383(i.e. all elements that are in both sets.)");
1384
1385static PyObject *
1386set_intersection_update(PySetObject *so, PyObject *other)
1387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 tmp = set_intersection(so, other);
1391 if (tmp == NULL)
1392 return NULL;
1393 set_swap_bodies(so, (PySetObject *)tmp);
1394 Py_DECREF(tmp);
1395 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001396}
1397
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001398static PyObject *
1399set_intersection_update_multi(PySetObject *so, PyObject *args)
1400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 tmp = set_intersection_multi(so, args);
1404 if (tmp == NULL)
1405 return NULL;
1406 set_swap_bodies(so, (PySetObject *)tmp);
1407 Py_DECREF(tmp);
1408 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001409}
1410
Raymond Hettingera690a992003-11-16 16:17:49 +00001411PyDoc_STRVAR(intersection_update_doc,
1412"Update a set with the intersection of itself and another.");
1413
1414static PyObject *
1415set_and(PySetObject *so, PyObject *other)
1416{
Brian Curtindfc80e32011-08-10 20:28:54 -05001417 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1418 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001420}
1421
1422static PyObject *
1423set_iand(PySetObject *so, PyObject *other)
1424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001426
Brian Curtindfc80e32011-08-10 20:28:54 -05001427 if (!PyAnySet_Check(other))
1428 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 result = set_intersection_update(so, other);
1430 if (result == NULL)
1431 return NULL;
1432 Py_DECREF(result);
1433 Py_INCREF(so);
1434 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001435}
1436
Guido van Rossum58da9312007-11-10 23:39:45 +00001437static PyObject *
1438set_isdisjoint(PySetObject *so, PyObject *other)
1439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if ((PyObject *)so == other) {
1443 if (PySet_GET_SIZE(so) == 0)
1444 Py_RETURN_TRUE;
1445 else
1446 Py_RETURN_FALSE;
1447 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 if (PyAnySet_CheckExact(other)) {
1450 Py_ssize_t pos = 0;
1451 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1454 tmp = (PyObject *)so;
1455 so = (PySetObject *)other;
1456 other = tmp;
1457 }
1458 while (set_next((PySetObject *)other, &pos, &entry)) {
1459 int rv = set_contains_entry(so, entry);
1460 if (rv == -1)
1461 return NULL;
1462 if (rv)
1463 Py_RETURN_FALSE;
1464 }
1465 Py_RETURN_TRUE;
1466 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 it = PyObject_GetIter(other);
1469 if (it == NULL)
1470 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 while ((key = PyIter_Next(it)) != NULL) {
1473 int rv;
1474 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001475 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (hash == -1) {
1478 Py_DECREF(key);
1479 Py_DECREF(it);
1480 return NULL;
1481 }
1482 entry.hash = hash;
1483 entry.key = key;
1484 rv = set_contains_entry(so, &entry);
1485 Py_DECREF(key);
1486 if (rv == -1) {
1487 Py_DECREF(it);
1488 return NULL;
1489 }
1490 if (rv) {
1491 Py_DECREF(it);
1492 Py_RETURN_FALSE;
1493 }
1494 }
1495 Py_DECREF(it);
1496 if (PyErr_Occurred())
1497 return NULL;
1498 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001499}
1500
1501PyDoc_STRVAR(isdisjoint_doc,
1502"Return True if two sets have a null intersection.");
1503
Neal Norwitz6576bd82005-11-13 18:41:28 +00001504static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001505set_difference_update_internal(PySetObject *so, PyObject *other)
1506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 if ((PyObject *)so == other)
1508 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (PyAnySet_Check(other)) {
1511 setentry *entry;
1512 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 while (set_next((PySetObject *)other, &pos, &entry))
1515 if (set_discard_entry(so, entry) == -1)
1516 return -1;
1517 } else {
1518 PyObject *key, *it;
1519 it = PyObject_GetIter(other);
1520 if (it == NULL)
1521 return -1;
1522
1523 while ((key = PyIter_Next(it)) != NULL) {
1524 if (set_discard_key(so, key) == -1) {
1525 Py_DECREF(it);
1526 Py_DECREF(key);
1527 return -1;
1528 }
1529 Py_DECREF(key);
1530 }
1531 Py_DECREF(it);
1532 if (PyErr_Occurred())
1533 return -1;
1534 }
1535 /* If more than 1/5 are dummies, then resize them away. */
1536 if ((so->fill - so->used) * 5 < so->mask)
1537 return 0;
1538 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001539}
1540
Raymond Hettingera690a992003-11-16 16:17:49 +00001541static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001542set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1547 PyObject *other = PyTuple_GET_ITEM(args, i);
1548 if (set_difference_update_internal(so, other) == -1)
1549 return NULL;
1550 }
1551 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001552}
1553
1554PyDoc_STRVAR(difference_update_doc,
1555"Remove all elements of another set from this set.");
1556
1557static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001558set_copy_and_difference(PySetObject *so, PyObject *other)
1559{
1560 PyObject *result;
1561
1562 result = set_copy(so);
1563 if (result == NULL)
1564 return NULL;
1565 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1566 return result;
1567 Py_DECREF(result);
1568 return NULL;
1569}
1570
1571static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001572set_difference(PySetObject *so, PyObject *other)
1573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 PyObject *result;
1575 setentry *entry;
1576 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001579 return set_copy_and_difference(so, other);
1580 }
1581
1582 /* If len(so) much more than len(other), it's more efficient to simply copy
1583 * so and then iterate other looking for common elements. */
1584 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1585 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 result = make_new_set_basetype(Py_TYPE(so), NULL);
1589 if (result == NULL)
1590 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 if (PyDict_CheckExact(other)) {
1593 while (set_next(so, &pos, &entry)) {
1594 setentry entrycopy;
1595 entrycopy.hash = entry->hash;
1596 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001597 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1599 Py_DECREF(result);
1600 return NULL;
1601 }
1602 }
1603 }
1604 return result;
1605 }
1606
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001607 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 while (set_next(so, &pos, &entry)) {
1609 int rv = set_contains_entry((PySetObject *)other, entry);
1610 if (rv == -1) {
1611 Py_DECREF(result);
1612 return NULL;
1613 }
1614 if (!rv) {
1615 if (set_add_entry((PySetObject *)result, entry) == -1) {
1616 Py_DECREF(result);
1617 return NULL;
1618 }
1619 }
1620 }
1621 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001622}
1623
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001624static PyObject *
1625set_difference_multi(PySetObject *so, PyObject *args)
1626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 Py_ssize_t i;
1628 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (PyTuple_GET_SIZE(args) == 0)
1631 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 other = PyTuple_GET_ITEM(args, 0);
1634 result = set_difference(so, other);
1635 if (result == NULL)
1636 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1639 other = PyTuple_GET_ITEM(args, i);
1640 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1641 Py_DECREF(result);
1642 return NULL;
1643 }
1644 }
1645 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001646}
1647
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001648PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001649"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001650\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001651(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001652static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001653set_sub(PySetObject *so, PyObject *other)
1654{
Brian Curtindfc80e32011-08-10 20:28:54 -05001655 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1656 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001658}
1659
1660static PyObject *
1661set_isub(PySetObject *so, PyObject *other)
1662{
Brian Curtindfc80e32011-08-10 20:28:54 -05001663 if (!PyAnySet_Check(other))
1664 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 if (set_difference_update_internal(so, other) == -1)
1666 return NULL;
1667 Py_INCREF(so);
1668 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001669}
1670
1671static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001672set_symmetric_difference_update(PySetObject *so, PyObject *other)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 PySetObject *otherset;
1675 PyObject *key;
1676 Py_ssize_t pos = 0;
1677 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 if ((PyObject *)so == other)
1680 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 if (PyDict_CheckExact(other)) {
1683 PyObject *value;
1684 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001685 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1687 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001688
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001689 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 an_entry.hash = hash;
1691 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001694 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001695 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001698 if (rv == DISCARD_NOTFOUND) {
1699 if (set_add_entry(so, &an_entry) == -1) {
1700 Py_DECREF(key);
1701 return NULL;
1702 }
1703 }
Georg Brandl2d444492010-09-03 10:52:55 +00001704 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 }
1706 Py_RETURN_NONE;
1707 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 if (PyAnySet_Check(other)) {
1710 Py_INCREF(other);
1711 otherset = (PySetObject *)other;
1712 } else {
1713 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1714 if (otherset == NULL)
1715 return NULL;
1716 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 while (set_next(otherset, &pos, &entry)) {
1719 int rv = set_discard_entry(so, entry);
1720 if (rv == -1) {
1721 Py_DECREF(otherset);
1722 return NULL;
1723 }
1724 if (rv == DISCARD_NOTFOUND) {
1725 if (set_add_entry(so, entry) == -1) {
1726 Py_DECREF(otherset);
1727 return NULL;
1728 }
1729 }
1730 }
1731 Py_DECREF(otherset);
1732 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001733}
1734
1735PyDoc_STRVAR(symmetric_difference_update_doc,
1736"Update a set with the symmetric difference of itself and another.");
1737
1738static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001739set_symmetric_difference(PySetObject *so, PyObject *other)
1740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 PyObject *rv;
1742 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1745 if (otherset == NULL)
1746 return NULL;
1747 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1748 if (rv == NULL)
1749 return NULL;
1750 Py_DECREF(rv);
1751 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001752}
1753
1754PyDoc_STRVAR(symmetric_difference_doc,
1755"Return the symmetric difference of two sets as a new set.\n\
1756\n\
1757(i.e. all elements that are in exactly one of the sets.)");
1758
1759static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001760set_xor(PySetObject *so, PyObject *other)
1761{
Brian Curtindfc80e32011-08-10 20:28:54 -05001762 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1763 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001765}
1766
1767static PyObject *
1768set_ixor(PySetObject *so, PyObject *other)
1769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001771
Brian Curtindfc80e32011-08-10 20:28:54 -05001772 if (!PyAnySet_Check(other))
1773 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 result = set_symmetric_difference_update(so, other);
1775 if (result == NULL)
1776 return NULL;
1777 Py_DECREF(result);
1778 Py_INCREF(so);
1779 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001780}
1781
1782static PyObject *
1783set_issubset(PySetObject *so, PyObject *other)
1784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 setentry *entry;
1786 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 if (!PyAnySet_Check(other)) {
1789 PyObject *tmp, *result;
1790 tmp = make_new_set(&PySet_Type, other);
1791 if (tmp == NULL)
1792 return NULL;
1793 result = set_issubset(so, tmp);
1794 Py_DECREF(tmp);
1795 return result;
1796 }
1797 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1798 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 while (set_next(so, &pos, &entry)) {
1801 int rv = set_contains_entry((PySetObject *)other, entry);
1802 if (rv == -1)
1803 return NULL;
1804 if (!rv)
1805 Py_RETURN_FALSE;
1806 }
1807 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001808}
1809
1810PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1811
1812static PyObject *
1813set_issuperset(PySetObject *so, PyObject *other)
1814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 if (!PyAnySet_Check(other)) {
1818 tmp = make_new_set(&PySet_Type, other);
1819 if (tmp == NULL)
1820 return NULL;
1821 result = set_issuperset(so, tmp);
1822 Py_DECREF(tmp);
1823 return result;
1824 }
1825 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001826}
1827
1828PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1829
Raymond Hettingera690a992003-11-16 16:17:49 +00001830static PyObject *
1831set_richcompare(PySetObject *v, PyObject *w, int op)
1832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001834
Brian Curtindfc80e32011-08-10 20:28:54 -05001835 if(!PyAnySet_Check(w))
1836 Py_RETURN_NOTIMPLEMENTED;
1837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 switch (op) {
1839 case Py_EQ:
1840 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1841 Py_RETURN_FALSE;
1842 if (v->hash != -1 &&
1843 ((PySetObject *)w)->hash != -1 &&
1844 v->hash != ((PySetObject *)w)->hash)
1845 Py_RETURN_FALSE;
1846 return set_issubset(v, w);
1847 case Py_NE:
1848 r1 = set_richcompare(v, w, Py_EQ);
1849 if (r1 == NULL)
1850 return NULL;
1851 r2 = PyBool_FromLong(PyObject_Not(r1));
1852 Py_DECREF(r1);
1853 return r2;
1854 case Py_LE:
1855 return set_issubset(v, w);
1856 case Py_GE:
1857 return set_issuperset(v, w);
1858 case Py_LT:
1859 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1860 Py_RETURN_FALSE;
1861 return set_issubset(v, w);
1862 case Py_GT:
1863 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1864 Py_RETURN_FALSE;
1865 return set_issuperset(v, w);
1866 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001867 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001868}
1869
Raymond Hettingera690a992003-11-16 16:17:49 +00001870static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001871set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 if (set_add_key(so, key) == -1)
1874 return NULL;
1875 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001876}
1877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001879"Add an element to a set.\n\
1880\n\
1881This has no effect if the element is already present.");
1882
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001883static int
1884set_contains(PySetObject *so, PyObject *key)
1885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 PyObject *tmpkey;
1887 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 rv = set_contains_key(so, key);
1890 if (rv == -1) {
1891 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1892 return -1;
1893 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001894 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 if (tmpkey == NULL)
1896 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001897 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 Py_DECREF(tmpkey);
1899 }
1900 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001901}
1902
1903static PyObject *
1904set_direct_contains(PySetObject *so, PyObject *key)
1905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 result = set_contains(so, key);
1909 if (result == -1)
1910 return NULL;
1911 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001912}
1913
1914PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1915
Raymond Hettingera690a992003-11-16 16:17:49 +00001916static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001917set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 PyObject *tmpkey;
1920 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 rv = set_discard_key(so, key);
1923 if (rv == -1) {
1924 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1925 return NULL;
1926 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001927 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 if (tmpkey == NULL)
1929 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 Py_DECREF(tmpkey);
1932 if (rv == -1)
1933 return NULL;
1934 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001937 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 return NULL;
1939 }
1940 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001941}
1942
1943PyDoc_STRVAR(remove_doc,
1944"Remove an element from a set; it must be a member.\n\
1945\n\
1946If the element is not a member, raise a KeyError.");
1947
1948static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001949set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001950{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001951 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 rv = set_discard_key(so, key);
1955 if (rv == -1) {
1956 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1957 return NULL;
1958 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001959 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 if (tmpkey == NULL)
1961 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001962 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001964 if (rv == -1)
1965 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 }
1967 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001968}
1969
1970PyDoc_STRVAR(discard_doc,
1971"Remove an element from a set if it is a member.\n\
1972\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001974
1975static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001976set_reduce(PySetObject *so)
1977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001979 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 keys = PySequence_List((PyObject *)so);
1982 if (keys == NULL)
1983 goto done;
1984 args = PyTuple_Pack(1, keys);
1985 if (args == NULL)
1986 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001987 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 if (dict == NULL) {
1989 PyErr_Clear();
1990 dict = Py_None;
1991 Py_INCREF(dict);
1992 }
1993 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001994done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 Py_XDECREF(args);
1996 Py_XDECREF(keys);
1997 Py_XDECREF(dict);
1998 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001999}
2000
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002001static PyObject *
2002set_sizeof(PySetObject *so)
2003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 res = sizeof(PySetObject);
2007 if (so->table != so->smalltable)
2008 res = res + (so->mask + 1) * sizeof(setentry);
2009 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002010}
2011
2012PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002013static int
2014set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 if (!PyAnySet_Check(self))
2019 return -1;
2020 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2021 return -1;
2022 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2023 return -1;
2024 set_clear_internal(self);
2025 self->hash = -1;
2026 if (iterable == NULL)
2027 return 0;
2028 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002029}
2030
Raymond Hettingera690a992003-11-16 16:17:49 +00002031static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 set_len, /* sq_length */
2033 0, /* sq_concat */
2034 0, /* sq_repeat */
2035 0, /* sq_item */
2036 0, /* sq_slice */
2037 0, /* sq_ass_item */
2038 0, /* sq_ass_slice */
2039 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002040};
2041
2042/* set object ********************************************************/
2043
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002044#ifdef Py_DEBUG
2045static PyObject *test_c_api(PySetObject *so);
2046
2047PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2048All is well if assertions don't fail.");
2049#endif
2050
Raymond Hettingera690a992003-11-16 16:17:49 +00002051static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 {"add", (PyCFunction)set_add, METH_O,
2053 add_doc},
2054 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2055 clear_doc},
2056 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2057 contains_doc},
2058 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2059 copy_doc},
2060 {"discard", (PyCFunction)set_discard, METH_O,
2061 discard_doc},
2062 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2063 difference_doc},
2064 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2065 difference_update_doc},
2066 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2067 intersection_doc},
2068 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2069 intersection_update_doc},
2070 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2071 isdisjoint_doc},
2072 {"issubset", (PyCFunction)set_issubset, METH_O,
2073 issubset_doc},
2074 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2075 issuperset_doc},
2076 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2077 pop_doc},
2078 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2079 reduce_doc},
2080 {"remove", (PyCFunction)set_remove, METH_O,
2081 remove_doc},
2082 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2083 sizeof_doc},
2084 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2085 symmetric_difference_doc},
2086 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2087 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002088#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2090 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002091#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 {"union", (PyCFunction)set_union, METH_VARARGS,
2093 union_doc},
2094 {"update", (PyCFunction)set_update, METH_VARARGS,
2095 update_doc},
2096 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002097};
2098
2099static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 0, /*nb_add*/
2101 (binaryfunc)set_sub, /*nb_subtract*/
2102 0, /*nb_multiply*/
2103 0, /*nb_remainder*/
2104 0, /*nb_divmod*/
2105 0, /*nb_power*/
2106 0, /*nb_negative*/
2107 0, /*nb_positive*/
2108 0, /*nb_absolute*/
2109 0, /*nb_bool*/
2110 0, /*nb_invert*/
2111 0, /*nb_lshift*/
2112 0, /*nb_rshift*/
2113 (binaryfunc)set_and, /*nb_and*/
2114 (binaryfunc)set_xor, /*nb_xor*/
2115 (binaryfunc)set_or, /*nb_or*/
2116 0, /*nb_int*/
2117 0, /*nb_reserved*/
2118 0, /*nb_float*/
2119 0, /*nb_inplace_add*/
2120 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2121 0, /*nb_inplace_multiply*/
2122 0, /*nb_inplace_remainder*/
2123 0, /*nb_inplace_power*/
2124 0, /*nb_inplace_lshift*/
2125 0, /*nb_inplace_rshift*/
2126 (binaryfunc)set_iand, /*nb_inplace_and*/
2127 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2128 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002129};
2130
2131PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002132"set() -> new empty set object\n\
2133set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002134\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002135Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002136
2137PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2139 "set", /* tp_name */
2140 sizeof(PySetObject), /* tp_basicsize */
2141 0, /* tp_itemsize */
2142 /* methods */
2143 (destructor)set_dealloc, /* tp_dealloc */
2144 0, /* tp_print */
2145 0, /* tp_getattr */
2146 0, /* tp_setattr */
2147 0, /* tp_reserved */
2148 (reprfunc)set_repr, /* tp_repr */
2149 &set_as_number, /* tp_as_number */
2150 &set_as_sequence, /* tp_as_sequence */
2151 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002152 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 0, /* tp_call */
2154 0, /* tp_str */
2155 PyObject_GenericGetAttr, /* tp_getattro */
2156 0, /* tp_setattro */
2157 0, /* tp_as_buffer */
2158 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2159 Py_TPFLAGS_BASETYPE, /* tp_flags */
2160 set_doc, /* tp_doc */
2161 (traverseproc)set_traverse, /* tp_traverse */
2162 (inquiry)set_clear_internal, /* tp_clear */
2163 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002164 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2165 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 0, /* tp_iternext */
2167 set_methods, /* tp_methods */
2168 0, /* tp_members */
2169 0, /* tp_getset */
2170 0, /* tp_base */
2171 0, /* tp_dict */
2172 0, /* tp_descr_get */
2173 0, /* tp_descr_set */
2174 0, /* tp_dictoffset */
2175 (initproc)set_init, /* tp_init */
2176 PyType_GenericAlloc, /* tp_alloc */
2177 set_new, /* tp_new */
2178 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002179};
2180
2181/* frozenset object ********************************************************/
2182
2183
2184static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2186 contains_doc},
2187 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2188 copy_doc},
2189 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2190 difference_doc},
2191 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2192 intersection_doc},
2193 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2194 isdisjoint_doc},
2195 {"issubset", (PyCFunction)set_issubset, METH_O,
2196 issubset_doc},
2197 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2198 issuperset_doc},
2199 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2200 reduce_doc},
2201 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2202 sizeof_doc},
2203 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2204 symmetric_difference_doc},
2205 {"union", (PyCFunction)set_union, METH_VARARGS,
2206 union_doc},
2207 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002208};
2209
2210static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 0, /*nb_add*/
2212 (binaryfunc)set_sub, /*nb_subtract*/
2213 0, /*nb_multiply*/
2214 0, /*nb_remainder*/
2215 0, /*nb_divmod*/
2216 0, /*nb_power*/
2217 0, /*nb_negative*/
2218 0, /*nb_positive*/
2219 0, /*nb_absolute*/
2220 0, /*nb_bool*/
2221 0, /*nb_invert*/
2222 0, /*nb_lshift*/
2223 0, /*nb_rshift*/
2224 (binaryfunc)set_and, /*nb_and*/
2225 (binaryfunc)set_xor, /*nb_xor*/
2226 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002227};
2228
2229PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002230"frozenset() -> empty frozenset object\n\
2231frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002232\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002233Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002234
2235PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2237 "frozenset", /* tp_name */
2238 sizeof(PySetObject), /* tp_basicsize */
2239 0, /* tp_itemsize */
2240 /* methods */
2241 (destructor)set_dealloc, /* tp_dealloc */
2242 0, /* tp_print */
2243 0, /* tp_getattr */
2244 0, /* tp_setattr */
2245 0, /* tp_reserved */
2246 (reprfunc)set_repr, /* tp_repr */
2247 &frozenset_as_number, /* tp_as_number */
2248 &set_as_sequence, /* tp_as_sequence */
2249 0, /* tp_as_mapping */
2250 frozenset_hash, /* tp_hash */
2251 0, /* tp_call */
2252 0, /* tp_str */
2253 PyObject_GenericGetAttr, /* tp_getattro */
2254 0, /* tp_setattro */
2255 0, /* tp_as_buffer */
2256 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2257 Py_TPFLAGS_BASETYPE, /* tp_flags */
2258 frozenset_doc, /* tp_doc */
2259 (traverseproc)set_traverse, /* tp_traverse */
2260 (inquiry)set_clear_internal, /* tp_clear */
2261 (richcmpfunc)set_richcompare, /* tp_richcompare */
2262 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2263 (getiterfunc)set_iter, /* tp_iter */
2264 0, /* tp_iternext */
2265 frozenset_methods, /* tp_methods */
2266 0, /* tp_members */
2267 0, /* tp_getset */
2268 0, /* tp_base */
2269 0, /* tp_dict */
2270 0, /* tp_descr_get */
2271 0, /* tp_descr_set */
2272 0, /* tp_dictoffset */
2273 0, /* tp_init */
2274 PyType_GenericAlloc, /* tp_alloc */
2275 frozenset_new, /* tp_new */
2276 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002277};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002278
2279
2280/***** C API functions *************************************************/
2281
2282PyObject *
2283PySet_New(PyObject *iterable)
2284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002286}
2287
2288PyObject *
2289PyFrozenSet_New(PyObject *iterable)
2290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002292}
2293
Neal Norwitz8c49c822006-03-04 18:41:19 +00002294Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002295PySet_Size(PyObject *anyset)
2296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 if (!PyAnySet_Check(anyset)) {
2298 PyErr_BadInternalCall();
2299 return -1;
2300 }
2301 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002302}
2303
2304int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002305PySet_Clear(PyObject *set)
2306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (!PySet_Check(set)) {
2308 PyErr_BadInternalCall();
2309 return -1;
2310 }
2311 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002312}
2313
2314int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002315PySet_Contains(PyObject *anyset, PyObject *key)
2316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!PyAnySet_Check(anyset)) {
2318 PyErr_BadInternalCall();
2319 return -1;
2320 }
2321 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002322}
2323
2324int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002325PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 if (!PySet_Check(set)) {
2328 PyErr_BadInternalCall();
2329 return -1;
2330 }
2331 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002332}
2333
2334int
Christian Heimesfd66e512008-01-29 12:18:50 +00002335PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 if (!PySet_Check(anyset) &&
2338 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2339 PyErr_BadInternalCall();
2340 return -1;
2341 }
2342 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002343}
2344
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002345int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002346_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 if (!PyAnySet_Check(set)) {
2351 PyErr_BadInternalCall();
2352 return -1;
2353 }
2354 if (set_next((PySetObject *)set, pos, &entry) == 0)
2355 return 0;
2356 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002357 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002359}
2360
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002361PyObject *
2362PySet_Pop(PyObject *set)
2363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 if (!PySet_Check(set)) {
2365 PyErr_BadInternalCall();
2366 return NULL;
2367 }
2368 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002369}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002370
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002371int
2372_PySet_Update(PyObject *set, PyObject *iterable)
2373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 if (!PySet_Check(set)) {
2375 PyErr_BadInternalCall();
2376 return -1;
2377 }
2378 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002379}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002380
2381#ifdef Py_DEBUG
2382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002384 Returns True and original set is restored. */
2385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386#define assertRaises(call_return_value, exception) \
2387 do { \
2388 assert(call_return_value); \
2389 assert(PyErr_ExceptionMatches(exception)); \
2390 PyErr_Clear(); \
2391 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002392
2393static PyObject *
2394test_c_api(PySetObject *so)
2395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 Py_ssize_t count;
2397 char *s;
2398 Py_ssize_t i;
2399 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2400 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002401 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 /* Verify preconditions */
2405 assert(PyAnySet_Check(ob));
2406 assert(PyAnySet_CheckExact(ob));
2407 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* so.clear(); so |= set("abc"); */
2410 str = PyUnicode_FromString("abc");
2411 if (str == NULL)
2412 return NULL;
2413 set_clear_internal(so);
2414 if (set_update_internal(so, str) == -1) {
2415 Py_DECREF(str);
2416 return NULL;
2417 }
2418 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 /* Exercise type/size checks */
2421 assert(PySet_Size(ob) == 3);
2422 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* Raise TypeError for non-iterable constructor arguments */
2425 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2426 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 /* Raise TypeError for unhashable key */
2429 dup = PySet_New(ob);
2430 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2431 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2432 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 /* Exercise successful pop, contains, add, and discard */
2435 elem = PySet_Pop(ob);
2436 assert(PySet_Contains(ob, elem) == 0);
2437 assert(PySet_GET_SIZE(ob) == 2);
2438 assert(PySet_Add(ob, elem) == 0);
2439 assert(PySet_Contains(ob, elem) == 1);
2440 assert(PySet_GET_SIZE(ob) == 3);
2441 assert(PySet_Discard(ob, elem) == 1);
2442 assert(PySet_GET_SIZE(ob) == 2);
2443 assert(PySet_Discard(ob, elem) == 0);
2444 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 /* Exercise clear */
2447 dup2 = PySet_New(dup);
2448 assert(PySet_Clear(dup2) == 0);
2449 assert(PySet_Size(dup2) == 0);
2450 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 /* Raise SystemError on clear or update of frozen set */
2453 f = PyFrozenSet_New(dup);
2454 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2455 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2456 assert(PySet_Add(f, elem) == 0);
2457 Py_INCREF(f);
2458 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2459 Py_DECREF(f);
2460 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 /* Exercise direct iteration */
2463 i = 0, count = 0;
2464 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2465 s = _PyUnicode_AsString(x);
2466 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2467 count++;
2468 }
2469 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 /* Exercise updates */
2472 dup2 = PySet_New(NULL);
2473 assert(_PySet_Update(dup2, dup) == 0);
2474 assert(PySet_Size(dup2) == 3);
2475 assert(_PySet_Update(dup2, dup) == 0);
2476 assert(PySet_Size(dup2) == 3);
2477 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 /* Raise SystemError when self argument is not a set or frozenset. */
2480 t = PyTuple_New(0);
2481 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2482 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2483 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 /* Raise SystemError when self argument is not a set. */
2486 f = PyFrozenSet_New(dup);
2487 assert(PySet_Size(f) == 3);
2488 assert(PyFrozenSet_CheckExact(f));
2489 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2490 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2491 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 /* Raise KeyError when popping from an empty set */
2494 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2495 Py_DECREF(ob);
2496 assert(PySet_GET_SIZE(ob) == 0);
2497 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 /* Restore the set from the copy using the PyNumber API */
2500 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2501 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 /* Verify constructors accept NULL arguments */
2504 f = PySet_New(NULL);
2505 assert(f != NULL);
2506 assert(PySet_GET_SIZE(f) == 0);
2507 Py_DECREF(f);
2508 f = PyFrozenSet_New(NULL);
2509 assert(f != NULL);
2510 assert(PyFrozenSet_CheckExact(f));
2511 assert(PySet_GET_SIZE(f) == 0);
2512 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 Py_DECREF(elem);
2515 Py_DECREF(dup);
2516 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002517}
2518
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002519#undef assertRaises
2520
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002521#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002522
2523/***** Dummy Struct *************************************************/
2524
2525static PyObject *
2526dummy_repr(PyObject *op)
2527{
2528 return PyUnicode_FromString("<dummy key>");
2529}
2530
2531static void
2532dummy_dealloc(PyObject* ignore)
2533{
2534 Py_FatalError("deallocating <dummy key>");
2535}
2536
2537static PyTypeObject _PySetDummy_Type = {
2538 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2539 "<dummy key> type",
2540 0,
2541 0,
2542 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2543 0, /*tp_print*/
2544 0, /*tp_getattr*/
2545 0, /*tp_setattr*/
2546 0, /*tp_reserved*/
2547 dummy_repr, /*tp_repr*/
2548 0, /*tp_as_number*/
2549 0, /*tp_as_sequence*/
2550 0, /*tp_as_mapping*/
2551 0, /*tp_hash */
2552 0, /*tp_call */
2553 0, /*tp_str */
2554 0, /*tp_getattro */
2555 0, /*tp_setattro */
2556 0, /*tp_as_buffer */
2557 Py_TPFLAGS_DEFAULT, /*tp_flags */
2558};
2559
2560static PyObject _dummy_struct = {
2561 _PyObject_EXTRA_INIT
2562 2, &_PySetDummy_Type
2563};
2564