blob: ece76bfc25ede6e55f320f81a906efa41e474f8a [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettinger6c3c1cc2013-08-31 21:34:24 -07007 Copyright (c) 2003-2013 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
26 Unlike the dictionary implementation, the lookkey functions can return
27 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Antoine Pitrou9d952542013-08-24 21:07:07 +020039/* Exported for the gdb plugin's benefit. */
40PyObject *_PySet_Dummy = dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000042
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070043/* ======================================================================== */
44/* ======= Begin logic for probing the hash table ========================= */
45
Raymond Hettinger742d8712013-09-08 00:25:57 -070046/* This should be >= PySet_MINSIZE - 1 */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070047#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070048#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070049#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070050
51/* This must be >= 1 */
52#define PERTURB_SHIFT 5
53
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000054static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020055set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070058 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020059 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070060 size_t perturb = hash;
61 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070062 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020063 int cmp;
Raymond Hettinger8408dc52013-09-15 14:57:15 -070064#if LINEAR_PROBES
65 setentry *limit;
66 size_t j;
67#endif
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000068
Raymond Hettingera35adf52013-09-02 03:23:21 -070069 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070070 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070072
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070073 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070075 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070076 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070077 PyObject *startkey = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 Py_INCREF(startkey);
79 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
80 Py_DECREF(startkey);
81 if (cmp < 0)
82 return NULL;
Raymond Hettingerafe89092013-08-28 20:59:31 -070083 if (table != so->table || entry->key != startkey)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 return set_lookkey(so, key, hash);
Raymond Hettingerafe89092013-08-28 20:59:31 -070085 if (cmp > 0)
86 return entry;
87 }
88 if (entry->key == dummy && freeslot == NULL)
89 freeslot = entry;
90
Raymond Hettinger8408dc52013-09-15 14:57:15 -070091#if LINEAR_PROBES
Raymond Hettingera35adf52013-09-02 03:23:21 -070092 limit = &table[mask];
93 for (j = 0 ; j < LINEAR_PROBES ; j++) {
94 entry = (entry == limit) ? &table[0] : entry + 1;
95 if (entry->key == NULL)
96 goto found_null;
97 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070098 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070099 if (entry->hash == hash && entry->key != dummy) {
100 PyObject *startkey = entry->key;
101 Py_INCREF(startkey);
102 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
103 Py_DECREF(startkey);
104 if (cmp < 0)
105 return NULL;
106 if (table != so->table || entry->key != startkey)
107 return set_lookkey(so, key, hash);
108 if (cmp > 0)
109 return entry;
110 }
111 if (entry->key == dummy && freeslot == NULL)
112 freeslot = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 }
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700114#endif
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700115
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700116 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700117 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700118
Raymond Hettingera35adf52013-09-02 03:23:21 -0700119 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700120 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700121 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700123 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700124 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000125}
126
127/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000128 * Hacked up version of set_lookkey which can assume keys are always unicode;
129 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000130 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000131 */
132static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200133set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700136 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200137 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700138 size_t perturb = hash;
139 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700140 size_t i = (size_t)hash;
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700141#if LINEAR_PROBES
142 setentry *limit;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700143 size_t j;
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700144#endif
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 /* Make sure this function doesn't have to handle non-unicode keys,
147 including subclasses of str; e.g., one reason to subclass
148 strings is to override __eq__, and for speed we don't cater to
149 that here. */
150 if (!PyUnicode_CheckExact(key)) {
151 so->lookup = set_lookkey;
152 return set_lookkey(so, key, hash);
153 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700154
Raymond Hettingera35adf52013-09-02 03:23:21 -0700155 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700156 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000158
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700159 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700160 if (entry->key == key
161 || (entry->hash == hash
Raymond Hettinger742d8712013-09-08 00:25:57 -0700162 && entry->key != dummy
163 && unicode_eq(entry->key, key)))
Raymond Hettingerafe89092013-08-28 20:59:31 -0700164 return entry;
165 if (entry->key == dummy && freeslot == NULL)
166 freeslot = entry;
167
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700168#if LINEAR_PROBES
Raymond Hettingera35adf52013-09-02 03:23:21 -0700169 limit = &table[mask];
170 for (j = 0 ; j < LINEAR_PROBES ; j++) {
171 entry = (entry == limit) ? &table[0] : entry + 1;
172 if (entry->key == NULL)
173 goto found_null;
174 if (entry->key == key
175 || (entry->hash == hash
176 && entry->key != dummy
177 && unicode_eq(entry->key, key)))
178 return entry;
179 if (entry->key == dummy && freeslot == NULL)
180 freeslot = entry;
181 }
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700182#endif
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700183
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700184 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700185 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700186
Raymond Hettingera35adf52013-09-02 03:23:21 -0700187 entry = &table[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700189 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700191 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700192 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000193}
194
195/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000196Internal routine used by set_table_resize() to insert an item which is
197known to be absent from the set. This routine also assumes that
198the set contains no deleted entries. Besides the performance benefit,
199using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
200Note that no refcounts are changed by this routine; if needed, the caller
201is responsible for incref'ing `key`.
202*/
203static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200204set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200207 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700208 size_t perturb = hash;
209 size_t mask = (size_t)so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700210 size_t i = (size_t)hash;
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700211#if LINEAR_PROBES
Raymond Hettingera35adf52013-09-02 03:23:21 -0700212 size_t j;
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700213#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +0000214
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700215 while (1) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700216 entry = &table[i & mask];
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700217 if (entry->key == NULL)
Raymond Hettingera35adf52013-09-02 03:23:21 -0700218 goto found_null;
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700219#if LINEAR_PROBES
Raymond Hettingera35adf52013-09-02 03:23:21 -0700220 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
221 entry = &table[(i + j) & mask];
222 if (entry->key == NULL)
223 goto found_null;
224 }
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700225#endif
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700226 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700227 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700229 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 so->fill++;
231 entry->key = key;
232 entry->hash = hash;
233 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000234}
235
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700236/* ======== End logic for probing the hash table ========================== */
237/* ======================================================================== */
238
239
240/*
241Internal routine to insert a new key into the table.
242Used by the public insert routine.
243Eats a reference to key.
244*/
245static int
246set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
247{
248 setentry *entry;
249
250 assert(so->lookup != NULL);
251 entry = so->lookup(so, key, hash);
252 if (entry == NULL)
253 return -1;
254 if (entry->key == NULL) {
255 /* UNUSED */
256 so->fill++;
257 entry->key = key;
258 entry->hash = hash;
259 so->used++;
260 } else if (entry->key == dummy) {
261 /* DUMMY */
262 entry->key = key;
263 entry->hash = hash;
264 so->used++;
265 } else {
266 /* ACTIVE */
267 Py_DECREF(key);
268 }
269 return 0;
270}
271
Thomas Wouters89f507f2006-12-13 04:49:30 +0000272/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000273Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000274keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000275actually be smaller than the old one.
276*/
277static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000278set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_ssize_t newsize;
281 setentry *oldtable, *newtable, *entry;
282 Py_ssize_t i;
283 int is_oldtable_malloced;
284 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 /* Find the smallest table size > minused. */
289 for (newsize = PySet_MINSIZE;
290 newsize <= minused && newsize > 0;
291 newsize <<= 1)
292 ;
293 if (newsize <= 0) {
294 PyErr_NoMemory();
295 return -1;
296 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 /* Get space for a new table. */
299 oldtable = so->table;
300 assert(oldtable != NULL);
301 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (newsize == PySet_MINSIZE) {
304 /* A large table is shrinking, or we can't get any smaller. */
305 newtable = so->smalltable;
306 if (newtable == oldtable) {
307 if (so->fill == so->used) {
308 /* No dummies, so no point doing anything. */
309 return 0;
310 }
311 /* We're not going to resize it, but rebuild the
312 table anyway to purge old dummy entries.
313 Subtle: This is *necessary* if fill==size,
314 as set_lookkey needs at least one virgin slot to
315 terminate failing searches. If fill < size, it's
316 merely desirable, as dummies slow searches. */
317 assert(so->fill > so->used);
318 memcpy(small_copy, oldtable, sizeof(small_copy));
319 oldtable = small_copy;
320 }
321 }
322 else {
323 newtable = PyMem_NEW(setentry, newsize);
324 if (newtable == NULL) {
325 PyErr_NoMemory();
326 return -1;
327 }
328 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 /* Make the set empty, using the new table. */
331 assert(newtable != oldtable);
332 so->table = newtable;
333 so->mask = newsize - 1;
334 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700335 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 /* Copy the data over; this is refcount-neutral for active entries;
340 dummy entries aren't copied over, of course */
341 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500342 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000344 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 }
346 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 if (is_oldtable_malloced)
349 PyMem_DEL(oldtable);
350 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000351}
352
Raymond Hettingerc991db22005-08-11 07:58:45 +0000353/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
354
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200356set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000357{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200358 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000359 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100360 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 assert(so->fill <= so->mask); /* at least one empty slot */
363 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000364 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100365 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000366 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 return -1;
368 }
369 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
370 return 0;
371 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000372}
373
374static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200375set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200377 Py_hash_t hash;
378 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200381 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 hash = PyObject_Hash(key);
383 if (hash == -1)
384 return -1;
385 }
386 assert(so->fill <= so->mask); /* at least one empty slot */
387 n_used = so->used;
388 Py_INCREF(key);
389 if (set_insert_key(so, key, hash) == -1) {
390 Py_DECREF(key);
391 return -1;
392 }
393 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
394 return 0;
395 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000396}
397
398#define DISCARD_NOTFOUND 0
399#define DISCARD_FOUND 1
400
401static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000402set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200403{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000405
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000406 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (entry == NULL)
408 return -1;
409 if (entry->key == NULL || entry->key == dummy)
410 return DISCARD_NOTFOUND;
411 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 entry->key = dummy;
413 so->used--;
414 Py_DECREF(old_key);
415 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000416}
417
418static int
419set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000420{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200421 Py_hash_t hash;
422 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200428 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 hash = PyObject_Hash(key);
430 if (hash == -1)
431 return -1;
432 }
433 entry = (so->lookup)(so, key, hash);
434 if (entry == NULL)
435 return -1;
436 if (entry->key == NULL || entry->key == dummy)
437 return DISCARD_NOTFOUND;
438 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 entry->key = dummy;
440 so->used--;
441 Py_DECREF(old_key);
442 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443}
444
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700445static void
446set_empty_to_minsize(PySetObject *so)
447{
448 memset(so->smalltable, 0, sizeof(so->smalltable));
449 so->fill = 0;
450 so->used = 0;
451 so->mask = PySet_MINSIZE - 1;
452 so->table = so->smalltable;
453 so->hash = -1;
454}
455
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000456static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457set_clear_internal(PySetObject *so)
458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 setentry *entry, *table;
460 int table_is_malloced;
461 Py_ssize_t fill;
462 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000463
Raymond Hettinger583cd032013-09-07 22:06:35 -0700464#ifdef Py_DEBUG
465 Py_ssize_t i = 0;
466 Py_ssize_t n = so->mask + 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467#endif
468
Raymond Hettinger583cd032013-09-07 22:06:35 -0700469 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 table = so->table;
471 assert(table != NULL);
472 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 /* This is delicate. During the process of clearing the set,
475 * decrefs can cause the set to mutate. To avoid fatal confusion
476 * (voice of experience), we have to make the set empty before
477 * clearing the slots, and never refer to anything via so->ref while
478 * clearing.
479 */
480 fill = so->fill;
481 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700482 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 else if (fill > 0) {
485 /* It's a small table with something that needs to be cleared.
486 * Afraid the only safe way is to copy the set entries into
487 * another small table first.
488 */
489 memcpy(small_copy, table, sizeof(small_copy));
490 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700491 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 }
493 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 /* Now we can finally clear things. If C had refcounts, we could
496 * assert that the refcount on table is 1 now, i.e. that this function
497 * has unique access to it, so decref side-effects can't alter it.
498 */
499 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 assert(i < n);
502 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 if (entry->key) {
505 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700506 if (entry->key != dummy)
507 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 else
511 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if (table_is_malloced)
516 PyMem_DEL(table);
517 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000518}
519
520/*
521 * Iterate over a set table. Use like so:
522 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000523 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000524 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000525 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000526 * while (set_next(yourset, &pos, &entry)) {
527 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000528 * }
529 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000530 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532 */
533static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000534set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_ssize_t i;
537 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200538 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 assert (PyAnySet_Check(so));
541 i = *pos_ptr;
542 assert(i >= 0);
543 table = so->table;
544 mask = so->mask;
545 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
546 i++;
547 *pos_ptr = i+1;
548 if (i > mask)
549 return 0;
550 assert(table[i].key != NULL);
551 *entry_ptr = &table[i];
552 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000553}
554
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000555static void
556set_dealloc(PySetObject *so)
557{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200558 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 Py_ssize_t fill = so->fill;
560 PyObject_GC_UnTrack(so);
561 Py_TRASHCAN_SAFE_BEGIN(so)
562 if (so->weakreflist != NULL)
563 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 for (entry = so->table; fill > 0; entry++) {
566 if (entry->key) {
567 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700568 if (entry->key != dummy)
569 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 }
571 }
572 if (so->table != so->smalltable)
573 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700574 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000576}
577
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000578static PyObject *
579set_repr(PySetObject *so)
580{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200581 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (status != 0) {
585 if (status < 0)
586 return NULL;
587 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
588 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 /* shortcut for the empty set */
591 if (!so->used) {
592 Py_ReprLeave((PyObject*)so);
593 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
594 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 keys = PySequence_List((PyObject *)so);
597 if (keys == NULL)
598 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000599
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200600 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 listrepr = PyObject_Repr(keys);
602 Py_DECREF(keys);
603 if (listrepr == NULL)
604 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200605 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200607 if (tmp == NULL)
608 goto done;
609 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200610
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200611 if (Py_TYPE(so) != &PySet_Type)
612 result = PyUnicode_FromFormat("%s({%U})",
613 Py_TYPE(so)->tp_name,
614 listrepr);
615 else
616 result = PyUnicode_FromFormat("{%U}", listrepr);
617 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000618done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 Py_ReprLeave((PyObject*)so);
620 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000621}
622
Martin v. Löwis18e16552006-02-15 17:27:45 +0000623static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000624set_len(PyObject *so)
625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000627}
628
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000629static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000630set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000633 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100634 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200635 Py_ssize_t i;
636 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 assert (PyAnySet_Check(so));
639 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 other = (PySetObject*)otherset;
642 if (other == so || other->used == 0)
643 /* a.update(a) or a.update({}); nothing to do */
644 return 0;
645 /* Do one big resize at the start, rather than
646 * incrementally resizing as we insert new keys. Expect
647 * that there will be no (or few) overlapping keys.
648 */
649 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
650 if (set_table_resize(so, (so->used + other->used)*2) != 0)
651 return -1;
652 }
653 for (i = 0; i <= other->mask; i++) {
654 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000655 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100656 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000657 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500658 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000659 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100660 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000661 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 return -1;
663 }
664 }
665 }
666 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000667}
668
669static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000670set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000671{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000672 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200676 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 hash = PyObject_Hash(key);
678 if (hash == -1)
679 return -1;
680 }
681 entry = (so->lookup)(so, key, hash);
682 if (entry == NULL)
683 return -1;
684 key = entry->key;
685 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000686}
687
Raymond Hettingerc991db22005-08-11 07:58:45 +0000688static int
689set_contains_entry(PySetObject *so, setentry *entry)
690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 PyObject *key;
692 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000693
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000694 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 if (lu_entry == NULL)
696 return -1;
697 key = lu_entry->key;
698 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000699}
700
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000701static PyObject *
702set_pop(PySetObject *so)
703{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200704 Py_ssize_t i = 0;
705 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 assert (PyAnySet_Check(so));
709 if (so->used == 0) {
710 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
711 return NULL;
712 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* Set entry to "the first" unused or dummy set entry. We abuse
715 * the hash field of slot 0 to hold a search finger:
716 * If slot 0 has a value, use slot 0.
717 * Else slot 0 is being used to hold a search finger,
718 * and we use its hash value as the first index to look.
719 */
720 entry = &so->table[0];
721 if (entry->key == NULL || entry->key == dummy) {
722 i = entry->hash;
723 /* The hash field may be a real hash value, or it may be a
724 * legit search finger, or it may be a once-legit search
725 * finger that's out of bounds now because it wrapped around
726 * or the table shrunk -- simply make sure it's in bounds now.
727 */
728 if (i > so->mask || i < 1)
729 i = 1; /* skip slot 0 */
730 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
731 i++;
732 if (i > so->mask)
733 i = 1;
734 }
735 }
736 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 entry->key = dummy;
738 so->used--;
739 so->table[0].hash = i + 1; /* next place to start */
740 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000741}
742
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000743PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
744Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000745
746static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000747set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 Py_ssize_t pos = 0;
750 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 while (set_next(so, &pos, &entry))
753 Py_VISIT(entry->key);
754 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000755}
756
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000757static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000758frozenset_hash(PyObject *self)
759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800761 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 setentry *entry;
763 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 if (so->hash != -1)
766 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000767
Mark Dickinson57e683e2011-09-24 18:18:40 +0100768 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 while (set_next(so, &pos, &entry)) {
770 /* Work to increase the bit dispersion for closely spaced hash
771 values. The is important because some use cases have many
772 combinations of a small number of elements with nearby
773 hashes so that many distinct combinations collapse to only
774 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000775 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800776 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800778 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800780 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 so->hash = hash;
782 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000783}
784
Raymond Hettingera9d99362005-08-05 00:01:15 +0000785/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000786
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyObject_HEAD
789 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
790 Py_ssize_t si_used;
791 Py_ssize_t si_pos;
792 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793} setiterobject;
794
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000795static void
796setiter_dealloc(setiterobject *si)
797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 Py_XDECREF(si->si_set);
799 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000800}
801
802static int
803setiter_traverse(setiterobject *si, visitproc visit, void *arg)
804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 Py_VISIT(si->si_set);
806 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807}
808
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000809static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810setiter_len(setiterobject *si)
811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 Py_ssize_t len = 0;
813 if (si->si_set != NULL && si->si_used == si->si_set->used)
814 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000815 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000816}
817
Armin Rigof5b3e362006-02-11 21:32:43 +0000818PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000819
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000820static PyObject *setiter_iternext(setiterobject *si);
821
822static PyObject *
823setiter_reduce(setiterobject *si)
824{
825 PyObject *list;
826 setiterobject tmp;
827
828 list = PyList_New(0);
829 if (!list)
830 return NULL;
831
Ezio Melotti0e1af282012-09-28 16:43:40 +0300832 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000833 tmp = *si;
834 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300835
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000836 /* iterate the temporary into a list */
837 for(;;) {
838 PyObject *element = setiter_iternext(&tmp);
839 if (element) {
840 if (PyList_Append(list, element)) {
841 Py_DECREF(element);
842 Py_DECREF(list);
843 Py_XDECREF(tmp.si_set);
844 return NULL;
845 }
846 Py_DECREF(element);
847 } else
848 break;
849 }
850 Py_XDECREF(tmp.si_set);
851 /* check for error */
852 if (tmp.si_set != NULL) {
853 /* we have an error */
854 Py_DECREF(list);
855 return NULL;
856 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200857 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000858}
859
860PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
861
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000862static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000864 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000866};
867
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000868static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200871 Py_ssize_t i, mask;
872 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (so == NULL)
876 return NULL;
877 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 if (si->si_used != so->used) {
880 PyErr_SetString(PyExc_RuntimeError,
881 "Set changed size during iteration");
882 si->si_used = -1; /* Make this state sticky */
883 return NULL;
884 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 i = si->si_pos;
887 assert(i>=0);
888 entry = so->table;
889 mask = so->mask;
890 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
891 i++;
892 si->si_pos = i+1;
893 if (i > mask)
894 goto fail;
895 si->len--;
896 key = entry[i].key;
897 Py_INCREF(key);
898 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000899
900fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 Py_DECREF(so);
902 si->si_set = NULL;
903 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000904}
905
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000906PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 PyVarObject_HEAD_INIT(&PyType_Type, 0)
908 "set_iterator", /* tp_name */
909 sizeof(setiterobject), /* tp_basicsize */
910 0, /* tp_itemsize */
911 /* methods */
912 (destructor)setiter_dealloc, /* tp_dealloc */
913 0, /* tp_print */
914 0, /* tp_getattr */
915 0, /* tp_setattr */
916 0, /* tp_reserved */
917 0, /* tp_repr */
918 0, /* tp_as_number */
919 0, /* tp_as_sequence */
920 0, /* tp_as_mapping */
921 0, /* tp_hash */
922 0, /* tp_call */
923 0, /* tp_str */
924 PyObject_GenericGetAttr, /* tp_getattro */
925 0, /* tp_setattro */
926 0, /* tp_as_buffer */
927 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
928 0, /* tp_doc */
929 (traverseproc)setiter_traverse, /* tp_traverse */
930 0, /* tp_clear */
931 0, /* tp_richcompare */
932 0, /* tp_weaklistoffset */
933 PyObject_SelfIter, /* tp_iter */
934 (iternextfunc)setiter_iternext, /* tp_iternext */
935 setiter_methods, /* tp_methods */
936 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000937};
938
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939static PyObject *
940set_iter(PySetObject *so)
941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
943 if (si == NULL)
944 return NULL;
945 Py_INCREF(so);
946 si->si_set = so;
947 si->si_used = so->used;
948 si->si_pos = 0;
949 si->len = so->used;
950 _PyObject_GC_TRACK(si);
951 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000952}
953
Raymond Hettingerd7946662005-08-01 21:39:29 +0000954static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000955set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 if (PyAnySet_Check(other))
960 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (PyDict_CheckExact(other)) {
963 PyObject *value;
964 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000965 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 /* Do one big resize at the start, rather than
969 * incrementally resizing as we insert new keys. Expect
970 * that there will be no (or few) overlapping keys.
971 */
972 if (dictsize == -1)
973 return -1;
974 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
975 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
976 return -1;
977 }
978 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
979 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 an_entry.hash = hash;
982 an_entry.key = key;
983 if (set_add_entry(so, &an_entry) == -1)
984 return -1;
985 }
986 return 0;
987 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 it = PyObject_GetIter(other);
990 if (it == NULL)
991 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 while ((key = PyIter_Next(it)) != NULL) {
994 if (set_add_key(so, key) == -1) {
995 Py_DECREF(it);
996 Py_DECREF(key);
997 return -1;
998 }
999 Py_DECREF(key);
1000 }
1001 Py_DECREF(it);
1002 if (PyErr_Occurred())
1003 return -1;
1004 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001005}
1006
1007static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001008set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1013 PyObject *other = PyTuple_GET_ITEM(args, i);
1014 if (set_update_internal(so, other) == -1)
1015 return NULL;
1016 }
1017 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001018}
1019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001021"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001022
1023static PyObject *
1024make_new_set(PyTypeObject *type, PyObject *iterable)
1025{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001026 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001029 so = (PySetObject *)type->tp_alloc(type, 0);
1030 if (so == NULL)
1031 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001032
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001033 so->fill = 0;
1034 so->used = 0;
1035 so->mask = PySet_MINSIZE - 1;
1036 so->table = so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 so->lookup = set_lookkey_unicode;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001038 so->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (iterable != NULL) {
1042 if (set_update_internal(so, iterable) == -1) {
1043 Py_DECREF(so);
1044 return NULL;
1045 }
1046 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001049}
1050
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001051static PyObject *
1052make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1055 if (PyType_IsSubtype(type, &PySet_Type))
1056 type = &PySet_Type;
1057 else
1058 type = &PyFrozenSet_Type;
1059 }
1060 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001061}
1062
Raymond Hettingerd7946662005-08-01 21:39:29 +00001063/* The empty frozenset is a singleton */
1064static PyObject *emptyfrozenset = NULL;
1065
Raymond Hettingera690a992003-11-16 16:17:49 +00001066static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001067frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1072 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1075 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (type != &PyFrozenSet_Type)
1078 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (iterable != NULL) {
1081 /* frozenset(f) is idempotent */
1082 if (PyFrozenSet_CheckExact(iterable)) {
1083 Py_INCREF(iterable);
1084 return iterable;
1085 }
1086 result = make_new_set(type, iterable);
1087 if (result == NULL || PySet_GET_SIZE(result))
1088 return result;
1089 Py_DECREF(result);
1090 }
1091 /* The empty frozenset is a singleton */
1092 if (emptyfrozenset == NULL)
1093 emptyfrozenset = make_new_set(type, NULL);
1094 Py_XINCREF(emptyfrozenset);
1095 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001096}
1097
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001098int
1099PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001100{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001101 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001102}
1103
1104void
1105PySet_Fini(void)
1106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001108}
1109
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001110static PyObject *
1111set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1114 return NULL;
1115
1116 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001117}
1118
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001119/* set_swap_bodies() switches the contents of any two sets by moving their
1120 internal data pointers and, if needed, copying the internal smalltables.
1121 Semantically equivalent to:
1122
1123 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1124
1125 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001127 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001129 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001130*/
1131
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001132static void
1133set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 Py_ssize_t t;
1136 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001137 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001139 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 t = a->fill; a->fill = b->fill; b->fill = t;
1142 t = a->used; a->used = b->used; b->used = t;
1143 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 u = a->table;
1146 if (a->table == a->smalltable)
1147 u = b->smalltable;
1148 a->table = b->table;
1149 if (b->table == b->smalltable)
1150 a->table = a->smalltable;
1151 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (a->table == a->smalltable || b->table == b->smalltable) {
1156 memcpy(tab, a->smalltable, sizeof(tab));
1157 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1158 memcpy(b->smalltable, tab, sizeof(tab));
1159 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1162 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1163 h = a->hash; a->hash = b->hash; b->hash = h;
1164 } else {
1165 a->hash = -1;
1166 b->hash = -1;
1167 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001168}
1169
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001170static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001171set_copy(PySetObject *so)
1172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001174}
1175
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001176static PyObject *
1177frozenset_copy(PySetObject *so)
1178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (PyFrozenSet_CheckExact(so)) {
1180 Py_INCREF(so);
1181 return (PyObject *)so;
1182 }
1183 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001184}
1185
Raymond Hettingera690a992003-11-16 16:17:49 +00001186PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1187
1188static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001189set_clear(PySetObject *so)
1190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 set_clear_internal(so);
1192 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001193}
1194
1195PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1196
1197static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001198set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 PySetObject *result;
1201 PyObject *other;
1202 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 result = (PySetObject *)set_copy(so);
1205 if (result == NULL)
1206 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1209 other = PyTuple_GET_ITEM(args, i);
1210 if ((PyObject *)so == other)
1211 continue;
1212 if (set_update_internal(result, other) == -1) {
1213 Py_DECREF(result);
1214 return NULL;
1215 }
1216 }
1217 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001218}
1219
1220PyDoc_STRVAR(union_doc,
1221 "Return the union of sets as a new set.\n\
1222\n\
1223(i.e. all elements that are in either set.)");
1224
1225static PyObject *
1226set_or(PySetObject *so, PyObject *other)
1227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001229
Brian Curtindfc80e32011-08-10 20:28:54 -05001230 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1231 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 result = (PySetObject *)set_copy(so);
1234 if (result == NULL)
1235 return NULL;
1236 if ((PyObject *)so == other)
1237 return (PyObject *)result;
1238 if (set_update_internal(result, other) == -1) {
1239 Py_DECREF(result);
1240 return NULL;
1241 }
1242 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001243}
1244
Raymond Hettingera690a992003-11-16 16:17:49 +00001245static PyObject *
1246set_ior(PySetObject *so, PyObject *other)
1247{
Brian Curtindfc80e32011-08-10 20:28:54 -05001248 if (!PyAnySet_Check(other))
1249 Py_RETURN_NOTIMPLEMENTED;
1250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (set_update_internal(so, other) == -1)
1252 return NULL;
1253 Py_INCREF(so);
1254 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001255}
1256
1257static PyObject *
1258set_intersection(PySetObject *so, PyObject *other)
1259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 PySetObject *result;
1261 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 if ((PyObject *)so == other)
1264 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1267 if (result == NULL)
1268 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (PyAnySet_Check(other)) {
1271 Py_ssize_t pos = 0;
1272 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1275 tmp = (PyObject *)so;
1276 so = (PySetObject *)other;
1277 other = tmp;
1278 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 while (set_next((PySetObject *)other, &pos, &entry)) {
1281 int rv = set_contains_entry(so, entry);
1282 if (rv == -1) {
1283 Py_DECREF(result);
1284 return NULL;
1285 }
1286 if (rv) {
1287 if (set_add_entry(result, entry) == -1) {
1288 Py_DECREF(result);
1289 return NULL;
1290 }
1291 }
1292 }
1293 return (PyObject *)result;
1294 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 it = PyObject_GetIter(other);
1297 if (it == NULL) {
1298 Py_DECREF(result);
1299 return NULL;
1300 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 while ((key = PyIter_Next(it)) != NULL) {
1303 int rv;
1304 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001305 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (hash == -1) {
1308 Py_DECREF(it);
1309 Py_DECREF(result);
1310 Py_DECREF(key);
1311 return NULL;
1312 }
1313 entry.hash = hash;
1314 entry.key = key;
1315 rv = set_contains_entry(so, &entry);
1316 if (rv == -1) {
1317 Py_DECREF(it);
1318 Py_DECREF(result);
1319 Py_DECREF(key);
1320 return NULL;
1321 }
1322 if (rv) {
1323 if (set_add_entry(result, &entry) == -1) {
1324 Py_DECREF(it);
1325 Py_DECREF(result);
1326 Py_DECREF(key);
1327 return NULL;
1328 }
1329 }
1330 Py_DECREF(key);
1331 }
1332 Py_DECREF(it);
1333 if (PyErr_Occurred()) {
1334 Py_DECREF(result);
1335 return NULL;
1336 }
1337 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001338}
1339
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001340static PyObject *
1341set_intersection_multi(PySetObject *so, PyObject *args)
1342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 Py_ssize_t i;
1344 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (PyTuple_GET_SIZE(args) == 0)
1347 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 Py_INCREF(so);
1350 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1351 PyObject *other = PyTuple_GET_ITEM(args, i);
1352 PyObject *newresult = set_intersection((PySetObject *)result, other);
1353 if (newresult == NULL) {
1354 Py_DECREF(result);
1355 return NULL;
1356 }
1357 Py_DECREF(result);
1358 result = newresult;
1359 }
1360 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001361}
1362
Raymond Hettingera690a992003-11-16 16:17:49 +00001363PyDoc_STRVAR(intersection_doc,
1364"Return the intersection of two sets as a new set.\n\
1365\n\
1366(i.e. all elements that are in both sets.)");
1367
1368static PyObject *
1369set_intersection_update(PySetObject *so, PyObject *other)
1370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 tmp = set_intersection(so, other);
1374 if (tmp == NULL)
1375 return NULL;
1376 set_swap_bodies(so, (PySetObject *)tmp);
1377 Py_DECREF(tmp);
1378 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001379}
1380
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001381static PyObject *
1382set_intersection_update_multi(PySetObject *so, PyObject *args)
1383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 tmp = set_intersection_multi(so, args);
1387 if (tmp == NULL)
1388 return NULL;
1389 set_swap_bodies(so, (PySetObject *)tmp);
1390 Py_DECREF(tmp);
1391 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001392}
1393
Raymond Hettingera690a992003-11-16 16:17:49 +00001394PyDoc_STRVAR(intersection_update_doc,
1395"Update a set with the intersection of itself and another.");
1396
1397static PyObject *
1398set_and(PySetObject *so, PyObject *other)
1399{
Brian Curtindfc80e32011-08-10 20:28:54 -05001400 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1401 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001403}
1404
1405static PyObject *
1406set_iand(PySetObject *so, PyObject *other)
1407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001409
Brian Curtindfc80e32011-08-10 20:28:54 -05001410 if (!PyAnySet_Check(other))
1411 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 result = set_intersection_update(so, other);
1413 if (result == NULL)
1414 return NULL;
1415 Py_DECREF(result);
1416 Py_INCREF(so);
1417 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001418}
1419
Guido van Rossum58da9312007-11-10 23:39:45 +00001420static PyObject *
1421set_isdisjoint(PySetObject *so, PyObject *other)
1422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 if ((PyObject *)so == other) {
1426 if (PySet_GET_SIZE(so) == 0)
1427 Py_RETURN_TRUE;
1428 else
1429 Py_RETURN_FALSE;
1430 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if (PyAnySet_CheckExact(other)) {
1433 Py_ssize_t pos = 0;
1434 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1437 tmp = (PyObject *)so;
1438 so = (PySetObject *)other;
1439 other = tmp;
1440 }
1441 while (set_next((PySetObject *)other, &pos, &entry)) {
1442 int rv = set_contains_entry(so, entry);
1443 if (rv == -1)
1444 return NULL;
1445 if (rv)
1446 Py_RETURN_FALSE;
1447 }
1448 Py_RETURN_TRUE;
1449 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 it = PyObject_GetIter(other);
1452 if (it == NULL)
1453 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 while ((key = PyIter_Next(it)) != NULL) {
1456 int rv;
1457 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001458 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 if (hash == -1) {
1461 Py_DECREF(key);
1462 Py_DECREF(it);
1463 return NULL;
1464 }
1465 entry.hash = hash;
1466 entry.key = key;
1467 rv = set_contains_entry(so, &entry);
1468 Py_DECREF(key);
1469 if (rv == -1) {
1470 Py_DECREF(it);
1471 return NULL;
1472 }
1473 if (rv) {
1474 Py_DECREF(it);
1475 Py_RETURN_FALSE;
1476 }
1477 }
1478 Py_DECREF(it);
1479 if (PyErr_Occurred())
1480 return NULL;
1481 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001482}
1483
1484PyDoc_STRVAR(isdisjoint_doc,
1485"Return True if two sets have a null intersection.");
1486
Neal Norwitz6576bd82005-11-13 18:41:28 +00001487static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001488set_difference_update_internal(PySetObject *so, PyObject *other)
1489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 if ((PyObject *)so == other)
1491 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 if (PyAnySet_Check(other)) {
1494 setentry *entry;
1495 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 while (set_next((PySetObject *)other, &pos, &entry))
1498 if (set_discard_entry(so, entry) == -1)
1499 return -1;
1500 } else {
1501 PyObject *key, *it;
1502 it = PyObject_GetIter(other);
1503 if (it == NULL)
1504 return -1;
1505
1506 while ((key = PyIter_Next(it)) != NULL) {
1507 if (set_discard_key(so, key) == -1) {
1508 Py_DECREF(it);
1509 Py_DECREF(key);
1510 return -1;
1511 }
1512 Py_DECREF(key);
1513 }
1514 Py_DECREF(it);
1515 if (PyErr_Occurred())
1516 return -1;
1517 }
1518 /* If more than 1/5 are dummies, then resize them away. */
1519 if ((so->fill - so->used) * 5 < so->mask)
1520 return 0;
1521 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001522}
1523
Raymond Hettingera690a992003-11-16 16:17:49 +00001524static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001525set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1530 PyObject *other = PyTuple_GET_ITEM(args, i);
1531 if (set_difference_update_internal(so, other) == -1)
1532 return NULL;
1533 }
1534 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001535}
1536
1537PyDoc_STRVAR(difference_update_doc,
1538"Remove all elements of another set from this set.");
1539
1540static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001541set_copy_and_difference(PySetObject *so, PyObject *other)
1542{
1543 PyObject *result;
1544
1545 result = set_copy(so);
1546 if (result == NULL)
1547 return NULL;
1548 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1549 return result;
1550 Py_DECREF(result);
1551 return NULL;
1552}
1553
1554static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001555set_difference(PySetObject *so, PyObject *other)
1556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 PyObject *result;
1558 setentry *entry;
1559 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001562 return set_copy_and_difference(so, other);
1563 }
1564
1565 /* If len(so) much more than len(other), it's more efficient to simply copy
1566 * so and then iterate other looking for common elements. */
1567 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1568 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 result = make_new_set_basetype(Py_TYPE(so), NULL);
1572 if (result == NULL)
1573 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 if (PyDict_CheckExact(other)) {
1576 while (set_next(so, &pos, &entry)) {
1577 setentry entrycopy;
1578 entrycopy.hash = entry->hash;
1579 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001580 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1582 Py_DECREF(result);
1583 return NULL;
1584 }
1585 }
1586 }
1587 return result;
1588 }
1589
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001590 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 while (set_next(so, &pos, &entry)) {
1592 int rv = set_contains_entry((PySetObject *)other, entry);
1593 if (rv == -1) {
1594 Py_DECREF(result);
1595 return NULL;
1596 }
1597 if (!rv) {
1598 if (set_add_entry((PySetObject *)result, entry) == -1) {
1599 Py_DECREF(result);
1600 return NULL;
1601 }
1602 }
1603 }
1604 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001605}
1606
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001607static PyObject *
1608set_difference_multi(PySetObject *so, PyObject *args)
1609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 Py_ssize_t i;
1611 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if (PyTuple_GET_SIZE(args) == 0)
1614 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 other = PyTuple_GET_ITEM(args, 0);
1617 result = set_difference(so, other);
1618 if (result == NULL)
1619 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1622 other = PyTuple_GET_ITEM(args, i);
1623 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1624 Py_DECREF(result);
1625 return NULL;
1626 }
1627 }
1628 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001629}
1630
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001631PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001632"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001633\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001634(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001635static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001636set_sub(PySetObject *so, PyObject *other)
1637{
Brian Curtindfc80e32011-08-10 20:28:54 -05001638 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1639 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001641}
1642
1643static PyObject *
1644set_isub(PySetObject *so, PyObject *other)
1645{
Brian Curtindfc80e32011-08-10 20:28:54 -05001646 if (!PyAnySet_Check(other))
1647 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (set_difference_update_internal(so, other) == -1)
1649 return NULL;
1650 Py_INCREF(so);
1651 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001652}
1653
1654static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001655set_symmetric_difference_update(PySetObject *so, PyObject *other)
1656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 PySetObject *otherset;
1658 PyObject *key;
1659 Py_ssize_t pos = 0;
1660 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 if ((PyObject *)so == other)
1663 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 if (PyDict_CheckExact(other)) {
1666 PyObject *value;
1667 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001668 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1670 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001671
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001672 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 an_entry.hash = hash;
1674 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001677 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001678 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001681 if (rv == DISCARD_NOTFOUND) {
1682 if (set_add_entry(so, &an_entry) == -1) {
1683 Py_DECREF(key);
1684 return NULL;
1685 }
1686 }
Georg Brandl2d444492010-09-03 10:52:55 +00001687 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 }
1689 Py_RETURN_NONE;
1690 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 if (PyAnySet_Check(other)) {
1693 Py_INCREF(other);
1694 otherset = (PySetObject *)other;
1695 } else {
1696 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1697 if (otherset == NULL)
1698 return NULL;
1699 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 while (set_next(otherset, &pos, &entry)) {
1702 int rv = set_discard_entry(so, entry);
1703 if (rv == -1) {
1704 Py_DECREF(otherset);
1705 return NULL;
1706 }
1707 if (rv == DISCARD_NOTFOUND) {
1708 if (set_add_entry(so, entry) == -1) {
1709 Py_DECREF(otherset);
1710 return NULL;
1711 }
1712 }
1713 }
1714 Py_DECREF(otherset);
1715 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001716}
1717
1718PyDoc_STRVAR(symmetric_difference_update_doc,
1719"Update a set with the symmetric difference of itself and another.");
1720
1721static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001722set_symmetric_difference(PySetObject *so, PyObject *other)
1723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 PyObject *rv;
1725 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1728 if (otherset == NULL)
1729 return NULL;
1730 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1731 if (rv == NULL)
1732 return NULL;
1733 Py_DECREF(rv);
1734 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001735}
1736
1737PyDoc_STRVAR(symmetric_difference_doc,
1738"Return the symmetric difference of two sets as a new set.\n\
1739\n\
1740(i.e. all elements that are in exactly one of the sets.)");
1741
1742static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001743set_xor(PySetObject *so, PyObject *other)
1744{
Brian Curtindfc80e32011-08-10 20:28:54 -05001745 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1746 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001748}
1749
1750static PyObject *
1751set_ixor(PySetObject *so, PyObject *other)
1752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001754
Brian Curtindfc80e32011-08-10 20:28:54 -05001755 if (!PyAnySet_Check(other))
1756 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 result = set_symmetric_difference_update(so, other);
1758 if (result == NULL)
1759 return NULL;
1760 Py_DECREF(result);
1761 Py_INCREF(so);
1762 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001763}
1764
1765static PyObject *
1766set_issubset(PySetObject *so, PyObject *other)
1767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 setentry *entry;
1769 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (!PyAnySet_Check(other)) {
1772 PyObject *tmp, *result;
1773 tmp = make_new_set(&PySet_Type, other);
1774 if (tmp == NULL)
1775 return NULL;
1776 result = set_issubset(so, tmp);
1777 Py_DECREF(tmp);
1778 return result;
1779 }
1780 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1781 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 while (set_next(so, &pos, &entry)) {
1784 int rv = set_contains_entry((PySetObject *)other, entry);
1785 if (rv == -1)
1786 return NULL;
1787 if (!rv)
1788 Py_RETURN_FALSE;
1789 }
1790 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001791}
1792
1793PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1794
1795static PyObject *
1796set_issuperset(PySetObject *so, PyObject *other)
1797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 if (!PyAnySet_Check(other)) {
1801 tmp = make_new_set(&PySet_Type, other);
1802 if (tmp == NULL)
1803 return NULL;
1804 result = set_issuperset(so, tmp);
1805 Py_DECREF(tmp);
1806 return result;
1807 }
1808 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001809}
1810
1811PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1812
Raymond Hettingera690a992003-11-16 16:17:49 +00001813static PyObject *
1814set_richcompare(PySetObject *v, PyObject *w, int op)
1815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001817
Brian Curtindfc80e32011-08-10 20:28:54 -05001818 if(!PyAnySet_Check(w))
1819 Py_RETURN_NOTIMPLEMENTED;
1820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 switch (op) {
1822 case Py_EQ:
1823 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1824 Py_RETURN_FALSE;
1825 if (v->hash != -1 &&
1826 ((PySetObject *)w)->hash != -1 &&
1827 v->hash != ((PySetObject *)w)->hash)
1828 Py_RETURN_FALSE;
1829 return set_issubset(v, w);
1830 case Py_NE:
1831 r1 = set_richcompare(v, w, Py_EQ);
1832 if (r1 == NULL)
1833 return NULL;
1834 r2 = PyBool_FromLong(PyObject_Not(r1));
1835 Py_DECREF(r1);
1836 return r2;
1837 case Py_LE:
1838 return set_issubset(v, w);
1839 case Py_GE:
1840 return set_issuperset(v, w);
1841 case Py_LT:
1842 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1843 Py_RETURN_FALSE;
1844 return set_issubset(v, w);
1845 case Py_GT:
1846 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1847 Py_RETURN_FALSE;
1848 return set_issuperset(v, w);
1849 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001850 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001851}
1852
Raymond Hettingera690a992003-11-16 16:17:49 +00001853static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001854set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 if (set_add_key(so, key) == -1)
1857 return NULL;
1858 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001859}
1860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001862"Add an element to a set.\n\
1863\n\
1864This has no effect if the element is already present.");
1865
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001866static int
1867set_contains(PySetObject *so, PyObject *key)
1868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 PyObject *tmpkey;
1870 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 rv = set_contains_key(so, key);
1873 if (rv == -1) {
1874 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1875 return -1;
1876 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001877 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 if (tmpkey == NULL)
1879 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001880 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 Py_DECREF(tmpkey);
1882 }
1883 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001884}
1885
1886static PyObject *
1887set_direct_contains(PySetObject *so, PyObject *key)
1888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 result = set_contains(so, key);
1892 if (result == -1)
1893 return NULL;
1894 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001895}
1896
1897PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1898
Raymond Hettingera690a992003-11-16 16:17:49 +00001899static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001900set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 PyObject *tmpkey;
1903 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 rv = set_discard_key(so, key);
1906 if (rv == -1) {
1907 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1908 return NULL;
1909 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001910 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 if (tmpkey == NULL)
1912 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 Py_DECREF(tmpkey);
1915 if (rv == -1)
1916 return NULL;
1917 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001920 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 return NULL;
1922 }
1923 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001924}
1925
1926PyDoc_STRVAR(remove_doc,
1927"Remove an element from a set; it must be a member.\n\
1928\n\
1929If the element is not a member, raise a KeyError.");
1930
1931static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001932set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001933{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001934 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 rv = set_discard_key(so, key);
1938 if (rv == -1) {
1939 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1940 return NULL;
1941 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001942 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 if (tmpkey == NULL)
1944 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001945 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001947 if (rv == -1)
1948 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 }
1950 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001951}
1952
1953PyDoc_STRVAR(discard_doc,
1954"Remove an element from a set if it is a member.\n\
1955\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001957
1958static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001959set_reduce(PySetObject *so)
1960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001962 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 keys = PySequence_List((PyObject *)so);
1965 if (keys == NULL)
1966 goto done;
1967 args = PyTuple_Pack(1, keys);
1968 if (args == NULL)
1969 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001970 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 if (dict == NULL) {
1972 PyErr_Clear();
1973 dict = Py_None;
1974 Py_INCREF(dict);
1975 }
1976 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001977done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 Py_XDECREF(args);
1979 Py_XDECREF(keys);
1980 Py_XDECREF(dict);
1981 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001982}
1983
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001984static PyObject *
1985set_sizeof(PySetObject *so)
1986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 res = sizeof(PySetObject);
1990 if (so->table != so->smalltable)
1991 res = res + (so->mask + 1) * sizeof(setentry);
1992 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001993}
1994
1995PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001996static int
1997set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 if (!PyAnySet_Check(self))
2002 return -1;
2003 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2004 return -1;
2005 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2006 return -1;
2007 set_clear_internal(self);
2008 self->hash = -1;
2009 if (iterable == NULL)
2010 return 0;
2011 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002012}
2013
Raymond Hettingera690a992003-11-16 16:17:49 +00002014static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 set_len, /* sq_length */
2016 0, /* sq_concat */
2017 0, /* sq_repeat */
2018 0, /* sq_item */
2019 0, /* sq_slice */
2020 0, /* sq_ass_item */
2021 0, /* sq_ass_slice */
2022 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002023};
2024
2025/* set object ********************************************************/
2026
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002027#ifdef Py_DEBUG
2028static PyObject *test_c_api(PySetObject *so);
2029
2030PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2031All is well if assertions don't fail.");
2032#endif
2033
Raymond Hettingera690a992003-11-16 16:17:49 +00002034static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 {"add", (PyCFunction)set_add, METH_O,
2036 add_doc},
2037 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2038 clear_doc},
2039 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2040 contains_doc},
2041 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2042 copy_doc},
2043 {"discard", (PyCFunction)set_discard, METH_O,
2044 discard_doc},
2045 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2046 difference_doc},
2047 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2048 difference_update_doc},
2049 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2050 intersection_doc},
2051 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2052 intersection_update_doc},
2053 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2054 isdisjoint_doc},
2055 {"issubset", (PyCFunction)set_issubset, METH_O,
2056 issubset_doc},
2057 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2058 issuperset_doc},
2059 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2060 pop_doc},
2061 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2062 reduce_doc},
2063 {"remove", (PyCFunction)set_remove, METH_O,
2064 remove_doc},
2065 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2066 sizeof_doc},
2067 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2068 symmetric_difference_doc},
2069 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2070 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002071#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2073 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002074#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 {"union", (PyCFunction)set_union, METH_VARARGS,
2076 union_doc},
2077 {"update", (PyCFunction)set_update, METH_VARARGS,
2078 update_doc},
2079 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002080};
2081
2082static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 0, /*nb_add*/
2084 (binaryfunc)set_sub, /*nb_subtract*/
2085 0, /*nb_multiply*/
2086 0, /*nb_remainder*/
2087 0, /*nb_divmod*/
2088 0, /*nb_power*/
2089 0, /*nb_negative*/
2090 0, /*nb_positive*/
2091 0, /*nb_absolute*/
2092 0, /*nb_bool*/
2093 0, /*nb_invert*/
2094 0, /*nb_lshift*/
2095 0, /*nb_rshift*/
2096 (binaryfunc)set_and, /*nb_and*/
2097 (binaryfunc)set_xor, /*nb_xor*/
2098 (binaryfunc)set_or, /*nb_or*/
2099 0, /*nb_int*/
2100 0, /*nb_reserved*/
2101 0, /*nb_float*/
2102 0, /*nb_inplace_add*/
2103 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2104 0, /*nb_inplace_multiply*/
2105 0, /*nb_inplace_remainder*/
2106 0, /*nb_inplace_power*/
2107 0, /*nb_inplace_lshift*/
2108 0, /*nb_inplace_rshift*/
2109 (binaryfunc)set_iand, /*nb_inplace_and*/
2110 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2111 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002112};
2113
2114PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002115"set() -> new empty set object\n\
2116set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002117\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002118Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002119
2120PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2122 "set", /* tp_name */
2123 sizeof(PySetObject), /* tp_basicsize */
2124 0, /* tp_itemsize */
2125 /* methods */
2126 (destructor)set_dealloc, /* tp_dealloc */
2127 0, /* tp_print */
2128 0, /* tp_getattr */
2129 0, /* tp_setattr */
2130 0, /* tp_reserved */
2131 (reprfunc)set_repr, /* tp_repr */
2132 &set_as_number, /* tp_as_number */
2133 &set_as_sequence, /* tp_as_sequence */
2134 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002135 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 0, /* tp_call */
2137 0, /* tp_str */
2138 PyObject_GenericGetAttr, /* tp_getattro */
2139 0, /* tp_setattro */
2140 0, /* tp_as_buffer */
2141 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2142 Py_TPFLAGS_BASETYPE, /* tp_flags */
2143 set_doc, /* tp_doc */
2144 (traverseproc)set_traverse, /* tp_traverse */
2145 (inquiry)set_clear_internal, /* tp_clear */
2146 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002147 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2148 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 0, /* tp_iternext */
2150 set_methods, /* tp_methods */
2151 0, /* tp_members */
2152 0, /* tp_getset */
2153 0, /* tp_base */
2154 0, /* tp_dict */
2155 0, /* tp_descr_get */
2156 0, /* tp_descr_set */
2157 0, /* tp_dictoffset */
2158 (initproc)set_init, /* tp_init */
2159 PyType_GenericAlloc, /* tp_alloc */
2160 set_new, /* tp_new */
2161 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002162};
2163
2164/* frozenset object ********************************************************/
2165
2166
2167static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2169 contains_doc},
2170 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2171 copy_doc},
2172 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2173 difference_doc},
2174 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2175 intersection_doc},
2176 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2177 isdisjoint_doc},
2178 {"issubset", (PyCFunction)set_issubset, METH_O,
2179 issubset_doc},
2180 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2181 issuperset_doc},
2182 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2183 reduce_doc},
2184 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2185 sizeof_doc},
2186 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2187 symmetric_difference_doc},
2188 {"union", (PyCFunction)set_union, METH_VARARGS,
2189 union_doc},
2190 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002191};
2192
2193static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 0, /*nb_add*/
2195 (binaryfunc)set_sub, /*nb_subtract*/
2196 0, /*nb_multiply*/
2197 0, /*nb_remainder*/
2198 0, /*nb_divmod*/
2199 0, /*nb_power*/
2200 0, /*nb_negative*/
2201 0, /*nb_positive*/
2202 0, /*nb_absolute*/
2203 0, /*nb_bool*/
2204 0, /*nb_invert*/
2205 0, /*nb_lshift*/
2206 0, /*nb_rshift*/
2207 (binaryfunc)set_and, /*nb_and*/
2208 (binaryfunc)set_xor, /*nb_xor*/
2209 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002210};
2211
2212PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002213"frozenset() -> empty frozenset object\n\
2214frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002215\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002216Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002217
2218PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2220 "frozenset", /* tp_name */
2221 sizeof(PySetObject), /* tp_basicsize */
2222 0, /* tp_itemsize */
2223 /* methods */
2224 (destructor)set_dealloc, /* tp_dealloc */
2225 0, /* tp_print */
2226 0, /* tp_getattr */
2227 0, /* tp_setattr */
2228 0, /* tp_reserved */
2229 (reprfunc)set_repr, /* tp_repr */
2230 &frozenset_as_number, /* tp_as_number */
2231 &set_as_sequence, /* tp_as_sequence */
2232 0, /* tp_as_mapping */
2233 frozenset_hash, /* tp_hash */
2234 0, /* tp_call */
2235 0, /* tp_str */
2236 PyObject_GenericGetAttr, /* tp_getattro */
2237 0, /* tp_setattro */
2238 0, /* tp_as_buffer */
2239 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2240 Py_TPFLAGS_BASETYPE, /* tp_flags */
2241 frozenset_doc, /* tp_doc */
2242 (traverseproc)set_traverse, /* tp_traverse */
2243 (inquiry)set_clear_internal, /* tp_clear */
2244 (richcmpfunc)set_richcompare, /* tp_richcompare */
2245 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2246 (getiterfunc)set_iter, /* tp_iter */
2247 0, /* tp_iternext */
2248 frozenset_methods, /* tp_methods */
2249 0, /* tp_members */
2250 0, /* tp_getset */
2251 0, /* tp_base */
2252 0, /* tp_dict */
2253 0, /* tp_descr_get */
2254 0, /* tp_descr_set */
2255 0, /* tp_dictoffset */
2256 0, /* tp_init */
2257 PyType_GenericAlloc, /* tp_alloc */
2258 frozenset_new, /* tp_new */
2259 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002260};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002261
2262
2263/***** C API functions *************************************************/
2264
2265PyObject *
2266PySet_New(PyObject *iterable)
2267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002269}
2270
2271PyObject *
2272PyFrozenSet_New(PyObject *iterable)
2273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002275}
2276
Neal Norwitz8c49c822006-03-04 18:41:19 +00002277Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002278PySet_Size(PyObject *anyset)
2279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 if (!PyAnySet_Check(anyset)) {
2281 PyErr_BadInternalCall();
2282 return -1;
2283 }
2284 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002285}
2286
2287int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002288PySet_Clear(PyObject *set)
2289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 if (!PySet_Check(set)) {
2291 PyErr_BadInternalCall();
2292 return -1;
2293 }
2294 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002295}
2296
2297int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002298PySet_Contains(PyObject *anyset, PyObject *key)
2299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 if (!PyAnySet_Check(anyset)) {
2301 PyErr_BadInternalCall();
2302 return -1;
2303 }
2304 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002305}
2306
2307int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002308PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (!PySet_Check(set)) {
2311 PyErr_BadInternalCall();
2312 return -1;
2313 }
2314 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002315}
2316
2317int
Christian Heimesfd66e512008-01-29 12:18:50 +00002318PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 if (!PySet_Check(anyset) &&
2321 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2322 PyErr_BadInternalCall();
2323 return -1;
2324 }
2325 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002326}
2327
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002328int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002329_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 if (!PyAnySet_Check(set)) {
2334 PyErr_BadInternalCall();
2335 return -1;
2336 }
2337 if (set_next((PySetObject *)set, pos, &entry) == 0)
2338 return 0;
2339 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002340 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002342}
2343
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002344PyObject *
2345PySet_Pop(PyObject *set)
2346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 if (!PySet_Check(set)) {
2348 PyErr_BadInternalCall();
2349 return NULL;
2350 }
2351 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002352}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002353
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002354int
2355_PySet_Update(PyObject *set, PyObject *iterable)
2356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 if (!PySet_Check(set)) {
2358 PyErr_BadInternalCall();
2359 return -1;
2360 }
2361 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002362}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002363
2364#ifdef Py_DEBUG
2365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002367 Returns True and original set is restored. */
2368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369#define assertRaises(call_return_value, exception) \
2370 do { \
2371 assert(call_return_value); \
2372 assert(PyErr_ExceptionMatches(exception)); \
2373 PyErr_Clear(); \
2374 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002375
2376static PyObject *
2377test_c_api(PySetObject *so)
2378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 Py_ssize_t count;
2380 char *s;
2381 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002382 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002384 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 /* Verify preconditions */
2388 assert(PyAnySet_Check(ob));
2389 assert(PyAnySet_CheckExact(ob));
2390 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* so.clear(); so |= set("abc"); */
2393 str = PyUnicode_FromString("abc");
2394 if (str == NULL)
2395 return NULL;
2396 set_clear_internal(so);
2397 if (set_update_internal(so, str) == -1) {
2398 Py_DECREF(str);
2399 return NULL;
2400 }
2401 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 /* Exercise type/size checks */
2404 assert(PySet_Size(ob) == 3);
2405 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 /* Raise TypeError for non-iterable constructor arguments */
2408 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2409 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 /* Raise TypeError for unhashable key */
2412 dup = PySet_New(ob);
2413 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2414 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2415 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 /* Exercise successful pop, contains, add, and discard */
2418 elem = PySet_Pop(ob);
2419 assert(PySet_Contains(ob, elem) == 0);
2420 assert(PySet_GET_SIZE(ob) == 2);
2421 assert(PySet_Add(ob, elem) == 0);
2422 assert(PySet_Contains(ob, elem) == 1);
2423 assert(PySet_GET_SIZE(ob) == 3);
2424 assert(PySet_Discard(ob, elem) == 1);
2425 assert(PySet_GET_SIZE(ob) == 2);
2426 assert(PySet_Discard(ob, elem) == 0);
2427 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* Exercise clear */
2430 dup2 = PySet_New(dup);
2431 assert(PySet_Clear(dup2) == 0);
2432 assert(PySet_Size(dup2) == 0);
2433 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* Raise SystemError on clear or update of frozen set */
2436 f = PyFrozenSet_New(dup);
2437 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2438 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2439 assert(PySet_Add(f, elem) == 0);
2440 Py_INCREF(f);
2441 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2442 Py_DECREF(f);
2443 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 /* Exercise direct iteration */
2446 i = 0, count = 0;
2447 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2448 s = _PyUnicode_AsString(x);
2449 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2450 count++;
2451 }
2452 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 /* Exercise updates */
2455 dup2 = PySet_New(NULL);
2456 assert(_PySet_Update(dup2, dup) == 0);
2457 assert(PySet_Size(dup2) == 3);
2458 assert(_PySet_Update(dup2, dup) == 0);
2459 assert(PySet_Size(dup2) == 3);
2460 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 /* Raise SystemError when self argument is not a set or frozenset. */
2463 t = PyTuple_New(0);
2464 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2465 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2466 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 /* Raise SystemError when self argument is not a set. */
2469 f = PyFrozenSet_New(dup);
2470 assert(PySet_Size(f) == 3);
2471 assert(PyFrozenSet_CheckExact(f));
2472 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2473 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2474 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 /* Raise KeyError when popping from an empty set */
2477 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2478 Py_DECREF(ob);
2479 assert(PySet_GET_SIZE(ob) == 0);
2480 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* Restore the set from the copy using the PyNumber API */
2483 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2484 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* Verify constructors accept NULL arguments */
2487 f = PySet_New(NULL);
2488 assert(f != NULL);
2489 assert(PySet_GET_SIZE(f) == 0);
2490 Py_DECREF(f);
2491 f = PyFrozenSet_New(NULL);
2492 assert(f != NULL);
2493 assert(PyFrozenSet_CheckExact(f));
2494 assert(PySet_GET_SIZE(f) == 0);
2495 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 Py_DECREF(elem);
2498 Py_DECREF(dup);
2499 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002500}
2501
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002502#undef assertRaises
2503
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002504#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002505
2506/***** Dummy Struct *************************************************/
2507
2508static PyObject *
2509dummy_repr(PyObject *op)
2510{
2511 return PyUnicode_FromString("<dummy key>");
2512}
2513
2514static void
2515dummy_dealloc(PyObject* ignore)
2516{
2517 Py_FatalError("deallocating <dummy key>");
2518}
2519
2520static PyTypeObject _PySetDummy_Type = {
2521 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2522 "<dummy key> type",
2523 0,
2524 0,
2525 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2526 0, /*tp_print*/
2527 0, /*tp_getattr*/
2528 0, /*tp_setattr*/
2529 0, /*tp_reserved*/
2530 dummy_repr, /*tp_repr*/
2531 0, /*tp_as_number*/
2532 0, /*tp_as_sequence*/
2533 0, /*tp_as_mapping*/
2534 0, /*tp_hash */
2535 0, /*tp_call */
2536 0, /*tp_str */
2537 0, /*tp_getattro */
2538 0, /*tp_setattro */
2539 0, /*tp_as_buffer */
2540 Py_TPFLAGS_DEFAULT, /*tp_flags */
2541};
2542
2543static PyObject _dummy_struct = {
2544 _PyObject_EXTRA_INIT
2545 2, &_PySetDummy_Type
2546};
2547