blob: b0803f6b198d3a9f4b4b6bcf5ac586c25165f418 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettinger6c3c1cc2013-08-31 21:34:24 -07007 Copyright (c) 2003-2013 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
26 Unlike the dictionary implementation, the lookkey functions can return
27 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070055 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t perturb = hash;
58 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070059 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070060 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070061 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062
Raymond Hettingera35adf52013-09-02 03:23:21 -070063 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070064 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070066
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070067 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070069 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070070 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070071 PyObject *startkey = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 Py_INCREF(startkey);
73 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
74 Py_DECREF(startkey);
75 if (cmp < 0)
76 return NULL;
Raymond Hettingerafe89092013-08-28 20:59:31 -070077 if (table != so->table || entry->key != startkey)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 return set_lookkey(so, key, hash);
Raymond Hettingerafe89092013-08-28 20:59:31 -070079 if (cmp > 0)
80 return entry;
81 }
82 if (entry->key == dummy && freeslot == NULL)
83 freeslot = entry;
84
Raymond Hettingerc70a2b72013-09-21 14:02:55 -070085 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
86 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -070087 if (entry->key == NULL)
88 goto found_null;
89 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070090 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070091 if (entry->hash == hash && entry->key != dummy) {
92 PyObject *startkey = entry->key;
93 Py_INCREF(startkey);
94 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
95 Py_DECREF(startkey);
96 if (cmp < 0)
97 return NULL;
98 if (table != so->table || entry->key != startkey)
99 return set_lookkey(so, key, hash);
100 if (cmp > 0)
101 return entry;
102 }
103 if (entry->key == dummy && freeslot == NULL)
104 freeslot = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700106
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700107 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700108 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700109
Raymond Hettingera35adf52013-09-02 03:23:21 -0700110 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700111 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700112 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700114 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700115 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000116}
117
118/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000119 * Hacked up version of set_lookkey which can assume keys are always unicode;
120 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000121 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 */
123static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200124set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700127 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200128 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700129 size_t perturb = hash;
130 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700131 size_t i = (size_t)hash;
132 size_t j;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 /* Make sure this function doesn't have to handle non-unicode keys,
135 including subclasses of str; e.g., one reason to subclass
136 strings is to override __eq__, and for speed we don't cater to
137 that here. */
138 if (!PyUnicode_CheckExact(key)) {
139 so->lookup = set_lookkey;
140 return set_lookkey(so, key, hash);
141 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700142
Raymond Hettingera35adf52013-09-02 03:23:21 -0700143 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700144 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000146
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700147 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700148 if (entry->key == key
149 || (entry->hash == hash
Raymond Hettinger742d8712013-09-08 00:25:57 -0700150 && entry->key != dummy
151 && unicode_eq(entry->key, key)))
Raymond Hettingerafe89092013-08-28 20:59:31 -0700152 return entry;
153 if (entry->key == dummy && freeslot == NULL)
154 freeslot = entry;
155
Raymond Hettingerc70a2b72013-09-21 14:02:55 -0700156 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
157 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -0700158 if (entry->key == NULL)
159 goto found_null;
160 if (entry->key == key
161 || (entry->hash == hash
162 && entry->key != dummy
163 && unicode_eq(entry->key, key)))
164 return entry;
165 if (entry->key == dummy && freeslot == NULL)
166 freeslot = entry;
167 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700168
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700169 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700170 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700171
Raymond Hettingera35adf52013-09-02 03:23:21 -0700172 entry = &table[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700174 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700176 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700177 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000178}
179
180/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000181Internal routine used by set_table_resize() to insert an item which is
182known to be absent from the set. This routine also assumes that
183the set contains no deleted entries. Besides the performance benefit,
184using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
185Note that no refcounts are changed by this routine; if needed, the caller
186is responsible for incref'ing `key`.
187*/
188static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200189set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200192 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700193 size_t perturb = hash;
194 size_t mask = (size_t)so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700195 size_t i = (size_t)hash;
196 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000197
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700198 while (1) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700199 entry = &table[i & mask];
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700200 if (entry->key == NULL)
Raymond Hettingera35adf52013-09-02 03:23:21 -0700201 goto found_null;
202 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
203 entry = &table[(i + j) & mask];
204 if (entry->key == NULL)
205 goto found_null;
206 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700207 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700208 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700210 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 entry->key = key;
212 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700213 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000215}
216
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700217/* ======== End logic for probing the hash table ========================== */
218/* ======================================================================== */
219
220
221/*
222Internal routine to insert a new key into the table.
223Used by the public insert routine.
224Eats a reference to key.
225*/
226static int
227set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
228{
229 setentry *entry;
230
231 assert(so->lookup != NULL);
232 entry = so->lookup(so, key, hash);
233 if (entry == NULL)
234 return -1;
235 if (entry->key == NULL) {
236 /* UNUSED */
237 so->fill++;
238 entry->key = key;
239 entry->hash = hash;
240 so->used++;
241 } else if (entry->key == dummy) {
242 /* DUMMY */
243 entry->key = key;
244 entry->hash = hash;
245 so->used++;
246 } else {
247 /* ACTIVE */
248 Py_DECREF(key);
249 }
250 return 0;
251}
252
Thomas Wouters89f507f2006-12-13 04:49:30 +0000253/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000254Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000255keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000256actually be smaller than the old one.
257*/
258static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 Py_ssize_t newsize;
262 setentry *oldtable, *newtable, *entry;
263 Py_ssize_t i;
264 int is_oldtable_malloced;
265 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 /* Find the smallest table size > minused. */
270 for (newsize = PySet_MINSIZE;
271 newsize <= minused && newsize > 0;
272 newsize <<= 1)
273 ;
274 if (newsize <= 0) {
275 PyErr_NoMemory();
276 return -1;
277 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 /* Get space for a new table. */
280 oldtable = so->table;
281 assert(oldtable != NULL);
282 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 if (newsize == PySet_MINSIZE) {
285 /* A large table is shrinking, or we can't get any smaller. */
286 newtable = so->smalltable;
287 if (newtable == oldtable) {
288 if (so->fill == so->used) {
289 /* No dummies, so no point doing anything. */
290 return 0;
291 }
292 /* We're not going to resize it, but rebuild the
293 table anyway to purge old dummy entries.
294 Subtle: This is *necessary* if fill==size,
295 as set_lookkey needs at least one virgin slot to
296 terminate failing searches. If fill < size, it's
297 merely desirable, as dummies slow searches. */
298 assert(so->fill > so->used);
299 memcpy(small_copy, oldtable, sizeof(small_copy));
300 oldtable = small_copy;
301 }
302 }
303 else {
304 newtable = PyMem_NEW(setentry, newsize);
305 if (newtable == NULL) {
306 PyErr_NoMemory();
307 return -1;
308 }
309 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 /* Make the set empty, using the new table. */
312 assert(newtable != oldtable);
313 so->table = newtable;
314 so->mask = newsize - 1;
315 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700316 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* Copy the data over; this is refcount-neutral for active entries;
321 dummy entries aren't copied over, of course */
322 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500323 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000325 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 }
327 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 if (is_oldtable_malloced)
330 PyMem_DEL(oldtable);
331 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000332}
333
Raymond Hettingerc991db22005-08-11 07:58:45 +0000334/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
335
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000336static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200337set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000338{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200339 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000340 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100341 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 assert(so->fill <= so->mask); /* at least one empty slot */
344 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000345 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100346 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000347 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 return -1;
349 }
350 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
351 return 0;
352 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000353}
354
355static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200356set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000357{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200358 Py_hash_t hash;
359 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200362 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 hash = PyObject_Hash(key);
364 if (hash == -1)
365 return -1;
366 }
367 assert(so->fill <= so->mask); /* at least one empty slot */
368 n_used = so->used;
369 Py_INCREF(key);
370 if (set_insert_key(so, key, hash) == -1) {
371 Py_DECREF(key);
372 return -1;
373 }
374 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
375 return 0;
376 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377}
378
379#define DISCARD_NOTFOUND 0
380#define DISCARD_FOUND 1
381
382static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000383set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200384{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000386
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000387 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (entry == NULL)
389 return -1;
390 if (entry->key == NULL || entry->key == dummy)
391 return DISCARD_NOTFOUND;
392 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 entry->key = dummy;
394 so->used--;
395 Py_DECREF(old_key);
396 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000397}
398
399static int
400set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000401{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200402 Py_hash_t hash;
403 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200409 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 hash = PyObject_Hash(key);
411 if (hash == -1)
412 return -1;
413 }
414 entry = (so->lookup)(so, key, hash);
415 if (entry == NULL)
416 return -1;
417 if (entry->key == NULL || entry->key == dummy)
418 return DISCARD_NOTFOUND;
419 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 entry->key = dummy;
421 so->used--;
422 Py_DECREF(old_key);
423 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424}
425
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700426static void
427set_empty_to_minsize(PySetObject *so)
428{
429 memset(so->smalltable, 0, sizeof(so->smalltable));
430 so->fill = 0;
431 so->used = 0;
432 so->mask = PySet_MINSIZE - 1;
433 so->table = so->smalltable;
434 so->hash = -1;
435}
436
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000437static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000438set_clear_internal(PySetObject *so)
439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 setentry *entry, *table;
441 int table_is_malloced;
442 Py_ssize_t fill;
443 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000444
Raymond Hettinger583cd032013-09-07 22:06:35 -0700445#ifdef Py_DEBUG
446 Py_ssize_t i = 0;
447 Py_ssize_t n = so->mask + 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448#endif
449
Raymond Hettinger583cd032013-09-07 22:06:35 -0700450 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 table = so->table;
452 assert(table != NULL);
453 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 /* This is delicate. During the process of clearing the set,
456 * decrefs can cause the set to mutate. To avoid fatal confusion
457 * (voice of experience), we have to make the set empty before
458 * clearing the slots, and never refer to anything via so->ref while
459 * clearing.
460 */
461 fill = so->fill;
462 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700463 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 else if (fill > 0) {
466 /* It's a small table with something that needs to be cleared.
467 * Afraid the only safe way is to copy the set entries into
468 * another small table first.
469 */
470 memcpy(small_copy, table, sizeof(small_copy));
471 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700472 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 }
474 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 /* Now we can finally clear things. If C had refcounts, we could
477 * assert that the refcount on table is 1 now, i.e. that this function
478 * has unique access to it, so decref side-effects can't alter it.
479 */
480 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000481#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 assert(i < n);
483 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (entry->key) {
486 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700487 if (entry->key != dummy)
488 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000490#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 else
492 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if (table_is_malloced)
497 PyMem_DEL(table);
498 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499}
500
501/*
502 * Iterate over a set table. Use like so:
503 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000504 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000505 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000506 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000507 * while (set_next(yourset, &pos, &entry)) {
508 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509 * }
510 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000511 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513 */
514static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000515set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 Py_ssize_t i;
518 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200519 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 assert (PyAnySet_Check(so));
522 i = *pos_ptr;
523 assert(i >= 0);
524 table = so->table;
525 mask = so->mask;
526 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
527 i++;
528 *pos_ptr = i+1;
529 if (i > mask)
530 return 0;
531 assert(table[i].key != NULL);
532 *entry_ptr = &table[i];
533 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534}
535
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000536static void
537set_dealloc(PySetObject *so)
538{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200539 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 Py_ssize_t fill = so->fill;
541 PyObject_GC_UnTrack(so);
542 Py_TRASHCAN_SAFE_BEGIN(so)
543 if (so->weakreflist != NULL)
544 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 for (entry = so->table; fill > 0; entry++) {
547 if (entry->key) {
548 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700549 if (entry->key != dummy)
550 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 }
552 }
553 if (so->table != so->smalltable)
554 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700555 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000557}
558
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000559static PyObject *
560set_repr(PySetObject *so)
561{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200562 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 if (status != 0) {
566 if (status < 0)
567 return NULL;
568 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
569 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 /* shortcut for the empty set */
572 if (!so->used) {
573 Py_ReprLeave((PyObject*)so);
574 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
575 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 keys = PySequence_List((PyObject *)so);
578 if (keys == NULL)
579 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000580
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200581 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 listrepr = PyObject_Repr(keys);
583 Py_DECREF(keys);
584 if (listrepr == NULL)
585 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200586 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200588 if (tmp == NULL)
589 goto done;
590 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200591
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 if (Py_TYPE(so) != &PySet_Type)
593 result = PyUnicode_FromFormat("%s({%U})",
594 Py_TYPE(so)->tp_name,
595 listrepr);
596 else
597 result = PyUnicode_FromFormat("{%U}", listrepr);
598 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000599done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 Py_ReprLeave((PyObject*)so);
601 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000602}
603
Martin v. Löwis18e16552006-02-15 17:27:45 +0000604static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000605set_len(PyObject *so)
606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000608}
609
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000610static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000611set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000614 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100615 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200616 Py_ssize_t i;
617 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 assert (PyAnySet_Check(so));
620 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 other = (PySetObject*)otherset;
623 if (other == so || other->used == 0)
624 /* a.update(a) or a.update({}); nothing to do */
625 return 0;
626 /* Do one big resize at the start, rather than
627 * incrementally resizing as we insert new keys. Expect
628 * that there will be no (or few) overlapping keys.
629 */
630 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
631 if (set_table_resize(so, (so->used + other->used)*2) != 0)
632 return -1;
633 }
634 for (i = 0; i <= other->mask; i++) {
635 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000636 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100637 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000638 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500639 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000640 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100641 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000642 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 return -1;
644 }
645 }
646 }
647 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648}
649
650static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000651set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000652{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000653 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200657 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 hash = PyObject_Hash(key);
659 if (hash == -1)
660 return -1;
661 }
662 entry = (so->lookup)(so, key, hash);
663 if (entry == NULL)
664 return -1;
665 key = entry->key;
666 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000667}
668
Raymond Hettingerc991db22005-08-11 07:58:45 +0000669static int
670set_contains_entry(PySetObject *so, setentry *entry)
671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 PyObject *key;
673 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000674
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000675 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 if (lu_entry == NULL)
677 return -1;
678 key = lu_entry->key;
679 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000680}
681
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000682static PyObject *
683set_pop(PySetObject *so)
684{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200685 Py_ssize_t i = 0;
686 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 assert (PyAnySet_Check(so));
690 if (so->used == 0) {
691 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
692 return NULL;
693 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 /* Set entry to "the first" unused or dummy set entry. We abuse
696 * the hash field of slot 0 to hold a search finger:
697 * If slot 0 has a value, use slot 0.
698 * Else slot 0 is being used to hold a search finger,
699 * and we use its hash value as the first index to look.
700 */
701 entry = &so->table[0];
702 if (entry->key == NULL || entry->key == dummy) {
703 i = entry->hash;
704 /* The hash field may be a real hash value, or it may be a
705 * legit search finger, or it may be a once-legit search
706 * finger that's out of bounds now because it wrapped around
707 * or the table shrunk -- simply make sure it's in bounds now.
708 */
709 if (i > so->mask || i < 1)
710 i = 1; /* skip slot 0 */
711 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
712 i++;
713 if (i > so->mask)
714 i = 1;
715 }
716 }
717 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 entry->key = dummy;
719 so->used--;
720 so->table[0].hash = i + 1; /* next place to start */
721 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000722}
723
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000724PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
725Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000726
727static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000728set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 Py_ssize_t pos = 0;
731 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 while (set_next(so, &pos, &entry))
734 Py_VISIT(entry->key);
735 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000736}
737
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000738static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000739frozenset_hash(PyObject *self)
740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800742 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 setentry *entry;
744 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 if (so->hash != -1)
747 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000748
Mark Dickinson57e683e2011-09-24 18:18:40 +0100749 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 while (set_next(so, &pos, &entry)) {
751 /* Work to increase the bit dispersion for closely spaced hash
752 values. The is important because some use cases have many
753 combinations of a small number of elements with nearby
754 hashes so that many distinct combinations collapse to only
755 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000756 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800757 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800759 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800761 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 so->hash = hash;
763 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000764}
765
Raymond Hettingera9d99362005-08-05 00:01:15 +0000766/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000767
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000768typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyObject_HEAD
770 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
771 Py_ssize_t si_used;
772 Py_ssize_t si_pos;
773 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000774} setiterobject;
775
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000776static void
777setiter_dealloc(setiterobject *si)
778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 Py_XDECREF(si->si_set);
780 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000781}
782
783static int
784setiter_traverse(setiterobject *si, visitproc visit, void *arg)
785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 Py_VISIT(si->si_set);
787 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788}
789
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000790static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791setiter_len(setiterobject *si)
792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 Py_ssize_t len = 0;
794 if (si->si_set != NULL && si->si_used == si->si_set->used)
795 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000796 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797}
798
Armin Rigof5b3e362006-02-11 21:32:43 +0000799PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000800
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000801static PyObject *setiter_iternext(setiterobject *si);
802
803static PyObject *
804setiter_reduce(setiterobject *si)
805{
806 PyObject *list;
807 setiterobject tmp;
808
809 list = PyList_New(0);
810 if (!list)
811 return NULL;
812
Ezio Melotti0e1af282012-09-28 16:43:40 +0300813 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000814 tmp = *si;
815 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300816
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000817 /* iterate the temporary into a list */
818 for(;;) {
819 PyObject *element = setiter_iternext(&tmp);
820 if (element) {
821 if (PyList_Append(list, element)) {
822 Py_DECREF(element);
823 Py_DECREF(list);
824 Py_XDECREF(tmp.si_set);
825 return NULL;
826 }
827 Py_DECREF(element);
828 } else
829 break;
830 }
831 Py_XDECREF(tmp.si_set);
832 /* check for error */
833 if (tmp.si_set != NULL) {
834 /* we have an error */
835 Py_DECREF(list);
836 return NULL;
837 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200838 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000839}
840
841PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
842
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000843static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000845 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847};
848
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000849static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200852 Py_ssize_t i, mask;
853 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if (so == NULL)
857 return NULL;
858 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 if (si->si_used != so->used) {
861 PyErr_SetString(PyExc_RuntimeError,
862 "Set changed size during iteration");
863 si->si_used = -1; /* Make this state sticky */
864 return NULL;
865 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 i = si->si_pos;
868 assert(i>=0);
869 entry = so->table;
870 mask = so->mask;
871 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
872 i++;
873 si->si_pos = i+1;
874 if (i > mask)
875 goto fail;
876 si->len--;
877 key = entry[i].key;
878 Py_INCREF(key);
879 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880
881fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 Py_DECREF(so);
883 si->si_set = NULL;
884 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000885}
886
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000887PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 PyVarObject_HEAD_INIT(&PyType_Type, 0)
889 "set_iterator", /* tp_name */
890 sizeof(setiterobject), /* tp_basicsize */
891 0, /* tp_itemsize */
892 /* methods */
893 (destructor)setiter_dealloc, /* tp_dealloc */
894 0, /* tp_print */
895 0, /* tp_getattr */
896 0, /* tp_setattr */
897 0, /* tp_reserved */
898 0, /* tp_repr */
899 0, /* tp_as_number */
900 0, /* tp_as_sequence */
901 0, /* tp_as_mapping */
902 0, /* tp_hash */
903 0, /* tp_call */
904 0, /* tp_str */
905 PyObject_GenericGetAttr, /* tp_getattro */
906 0, /* tp_setattro */
907 0, /* tp_as_buffer */
908 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
909 0, /* tp_doc */
910 (traverseproc)setiter_traverse, /* tp_traverse */
911 0, /* tp_clear */
912 0, /* tp_richcompare */
913 0, /* tp_weaklistoffset */
914 PyObject_SelfIter, /* tp_iter */
915 (iternextfunc)setiter_iternext, /* tp_iternext */
916 setiter_methods, /* tp_methods */
917 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000918};
919
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000920static PyObject *
921set_iter(PySetObject *so)
922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
924 if (si == NULL)
925 return NULL;
926 Py_INCREF(so);
927 si->si_set = so;
928 si->si_used = so->used;
929 si->si_pos = 0;
930 si->len = so->used;
931 _PyObject_GC_TRACK(si);
932 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933}
934
Raymond Hettingerd7946662005-08-01 21:39:29 +0000935static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000936set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 if (PyAnySet_Check(other))
941 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 if (PyDict_CheckExact(other)) {
944 PyObject *value;
945 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000946 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* Do one big resize at the start, rather than
950 * incrementally resizing as we insert new keys. Expect
951 * that there will be no (or few) overlapping keys.
952 */
953 if (dictsize == -1)
954 return -1;
955 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
956 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
957 return -1;
958 }
959 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
960 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 an_entry.hash = hash;
963 an_entry.key = key;
964 if (set_add_entry(so, &an_entry) == -1)
965 return -1;
966 }
967 return 0;
968 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 it = PyObject_GetIter(other);
971 if (it == NULL)
972 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 while ((key = PyIter_Next(it)) != NULL) {
975 if (set_add_key(so, key) == -1) {
976 Py_DECREF(it);
977 Py_DECREF(key);
978 return -1;
979 }
980 Py_DECREF(key);
981 }
982 Py_DECREF(it);
983 if (PyErr_Occurred())
984 return -1;
985 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000986}
987
988static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000989set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
994 PyObject *other = PyTuple_GET_ITEM(args, i);
995 if (set_update_internal(so, other) == -1)
996 return NULL;
997 }
998 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000999}
1000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001002"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001003
1004static PyObject *
1005make_new_set(PyTypeObject *type, PyObject *iterable)
1006{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001007 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001010 so = (PySetObject *)type->tp_alloc(type, 0);
1011 if (so == NULL)
1012 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001013
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001014 so->fill = 0;
1015 so->used = 0;
1016 so->mask = PySet_MINSIZE - 1;
1017 so->table = so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 so->lookup = set_lookkey_unicode;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001019 so->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 if (iterable != NULL) {
1023 if (set_update_internal(so, iterable) == -1) {
1024 Py_DECREF(so);
1025 return NULL;
1026 }
1027 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001030}
1031
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001032static PyObject *
1033make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1036 if (PyType_IsSubtype(type, &PySet_Type))
1037 type = &PySet_Type;
1038 else
1039 type = &PyFrozenSet_Type;
1040 }
1041 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001042}
1043
Raymond Hettingerd7946662005-08-01 21:39:29 +00001044/* The empty frozenset is a singleton */
1045static PyObject *emptyfrozenset = NULL;
1046
Raymond Hettingera690a992003-11-16 16:17:49 +00001047static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001048frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1053 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1056 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 if (type != &PyFrozenSet_Type)
1059 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 if (iterable != NULL) {
1062 /* frozenset(f) is idempotent */
1063 if (PyFrozenSet_CheckExact(iterable)) {
1064 Py_INCREF(iterable);
1065 return iterable;
1066 }
1067 result = make_new_set(type, iterable);
1068 if (result == NULL || PySet_GET_SIZE(result))
1069 return result;
1070 Py_DECREF(result);
1071 }
1072 /* The empty frozenset is a singleton */
1073 if (emptyfrozenset == NULL)
1074 emptyfrozenset = make_new_set(type, NULL);
1075 Py_XINCREF(emptyfrozenset);
1076 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001077}
1078
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001079int
1080PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001081{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001082 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001083}
1084
1085void
1086PySet_Fini(void)
1087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001089}
1090
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001091static PyObject *
1092set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1095 return NULL;
1096
1097 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001098}
1099
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001100/* set_swap_bodies() switches the contents of any two sets by moving their
1101 internal data pointers and, if needed, copying the internal smalltables.
1102 Semantically equivalent to:
1103
1104 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1105
1106 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001108 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001110 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001111*/
1112
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001113static void
1114set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 Py_ssize_t t;
1117 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001118 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001120 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 t = a->fill; a->fill = b->fill; b->fill = t;
1123 t = a->used; a->used = b->used; b->used = t;
1124 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 u = a->table;
1127 if (a->table == a->smalltable)
1128 u = b->smalltable;
1129 a->table = b->table;
1130 if (b->table == b->smalltable)
1131 a->table = a->smalltable;
1132 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 if (a->table == a->smalltable || b->table == b->smalltable) {
1137 memcpy(tab, a->smalltable, sizeof(tab));
1138 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1139 memcpy(b->smalltable, tab, sizeof(tab));
1140 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1143 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1144 h = a->hash; a->hash = b->hash; b->hash = h;
1145 } else {
1146 a->hash = -1;
1147 b->hash = -1;
1148 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001149}
1150
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001151static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001152set_copy(PySetObject *so)
1153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001155}
1156
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001157static PyObject *
1158frozenset_copy(PySetObject *so)
1159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 if (PyFrozenSet_CheckExact(so)) {
1161 Py_INCREF(so);
1162 return (PyObject *)so;
1163 }
1164 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001165}
1166
Raymond Hettingera690a992003-11-16 16:17:49 +00001167PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1168
1169static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001170set_clear(PySetObject *so)
1171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 set_clear_internal(so);
1173 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001174}
1175
1176PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1177
1178static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001179set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 PySetObject *result;
1182 PyObject *other;
1183 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 result = (PySetObject *)set_copy(so);
1186 if (result == NULL)
1187 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1190 other = PyTuple_GET_ITEM(args, i);
1191 if ((PyObject *)so == other)
1192 continue;
1193 if (set_update_internal(result, other) == -1) {
1194 Py_DECREF(result);
1195 return NULL;
1196 }
1197 }
1198 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001199}
1200
1201PyDoc_STRVAR(union_doc,
1202 "Return the union of sets as a new set.\n\
1203\n\
1204(i.e. all elements that are in either set.)");
1205
1206static PyObject *
1207set_or(PySetObject *so, PyObject *other)
1208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001210
Brian Curtindfc80e32011-08-10 20:28:54 -05001211 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1212 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 result = (PySetObject *)set_copy(so);
1215 if (result == NULL)
1216 return NULL;
1217 if ((PyObject *)so == other)
1218 return (PyObject *)result;
1219 if (set_update_internal(result, other) == -1) {
1220 Py_DECREF(result);
1221 return NULL;
1222 }
1223 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001224}
1225
Raymond Hettingera690a992003-11-16 16:17:49 +00001226static PyObject *
1227set_ior(PySetObject *so, PyObject *other)
1228{
Brian Curtindfc80e32011-08-10 20:28:54 -05001229 if (!PyAnySet_Check(other))
1230 Py_RETURN_NOTIMPLEMENTED;
1231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 if (set_update_internal(so, other) == -1)
1233 return NULL;
1234 Py_INCREF(so);
1235 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001236}
1237
1238static PyObject *
1239set_intersection(PySetObject *so, PyObject *other)
1240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 PySetObject *result;
1242 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 if ((PyObject *)so == other)
1245 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1248 if (result == NULL)
1249 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (PyAnySet_Check(other)) {
1252 Py_ssize_t pos = 0;
1253 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1256 tmp = (PyObject *)so;
1257 so = (PySetObject *)other;
1258 other = tmp;
1259 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 while (set_next((PySetObject *)other, &pos, &entry)) {
1262 int rv = set_contains_entry(so, entry);
1263 if (rv == -1) {
1264 Py_DECREF(result);
1265 return NULL;
1266 }
1267 if (rv) {
1268 if (set_add_entry(result, entry) == -1) {
1269 Py_DECREF(result);
1270 return NULL;
1271 }
1272 }
1273 }
1274 return (PyObject *)result;
1275 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 it = PyObject_GetIter(other);
1278 if (it == NULL) {
1279 Py_DECREF(result);
1280 return NULL;
1281 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 while ((key = PyIter_Next(it)) != NULL) {
1284 int rv;
1285 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001286 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (hash == -1) {
1289 Py_DECREF(it);
1290 Py_DECREF(result);
1291 Py_DECREF(key);
1292 return NULL;
1293 }
1294 entry.hash = hash;
1295 entry.key = key;
1296 rv = set_contains_entry(so, &entry);
1297 if (rv == -1) {
1298 Py_DECREF(it);
1299 Py_DECREF(result);
1300 Py_DECREF(key);
1301 return NULL;
1302 }
1303 if (rv) {
1304 if (set_add_entry(result, &entry) == -1) {
1305 Py_DECREF(it);
1306 Py_DECREF(result);
1307 Py_DECREF(key);
1308 return NULL;
1309 }
1310 }
1311 Py_DECREF(key);
1312 }
1313 Py_DECREF(it);
1314 if (PyErr_Occurred()) {
1315 Py_DECREF(result);
1316 return NULL;
1317 }
1318 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001319}
1320
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001321static PyObject *
1322set_intersection_multi(PySetObject *so, PyObject *args)
1323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 Py_ssize_t i;
1325 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 if (PyTuple_GET_SIZE(args) == 0)
1328 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 Py_INCREF(so);
1331 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1332 PyObject *other = PyTuple_GET_ITEM(args, i);
1333 PyObject *newresult = set_intersection((PySetObject *)result, other);
1334 if (newresult == NULL) {
1335 Py_DECREF(result);
1336 return NULL;
1337 }
1338 Py_DECREF(result);
1339 result = newresult;
1340 }
1341 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001342}
1343
Raymond Hettingera690a992003-11-16 16:17:49 +00001344PyDoc_STRVAR(intersection_doc,
1345"Return the intersection of two sets as a new set.\n\
1346\n\
1347(i.e. all elements that are in both sets.)");
1348
1349static PyObject *
1350set_intersection_update(PySetObject *so, PyObject *other)
1351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 tmp = set_intersection(so, other);
1355 if (tmp == NULL)
1356 return NULL;
1357 set_swap_bodies(so, (PySetObject *)tmp);
1358 Py_DECREF(tmp);
1359 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001360}
1361
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001362static PyObject *
1363set_intersection_update_multi(PySetObject *so, PyObject *args)
1364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 tmp = set_intersection_multi(so, args);
1368 if (tmp == NULL)
1369 return NULL;
1370 set_swap_bodies(so, (PySetObject *)tmp);
1371 Py_DECREF(tmp);
1372 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001373}
1374
Raymond Hettingera690a992003-11-16 16:17:49 +00001375PyDoc_STRVAR(intersection_update_doc,
1376"Update a set with the intersection of itself and another.");
1377
1378static PyObject *
1379set_and(PySetObject *so, PyObject *other)
1380{
Brian Curtindfc80e32011-08-10 20:28:54 -05001381 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1382 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001384}
1385
1386static PyObject *
1387set_iand(PySetObject *so, PyObject *other)
1388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001390
Brian Curtindfc80e32011-08-10 20:28:54 -05001391 if (!PyAnySet_Check(other))
1392 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 result = set_intersection_update(so, other);
1394 if (result == NULL)
1395 return NULL;
1396 Py_DECREF(result);
1397 Py_INCREF(so);
1398 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001399}
1400
Guido van Rossum58da9312007-11-10 23:39:45 +00001401static PyObject *
1402set_isdisjoint(PySetObject *so, PyObject *other)
1403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if ((PyObject *)so == other) {
1407 if (PySet_GET_SIZE(so) == 0)
1408 Py_RETURN_TRUE;
1409 else
1410 Py_RETURN_FALSE;
1411 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 if (PyAnySet_CheckExact(other)) {
1414 Py_ssize_t pos = 0;
1415 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1418 tmp = (PyObject *)so;
1419 so = (PySetObject *)other;
1420 other = tmp;
1421 }
1422 while (set_next((PySetObject *)other, &pos, &entry)) {
1423 int rv = set_contains_entry(so, entry);
1424 if (rv == -1)
1425 return NULL;
1426 if (rv)
1427 Py_RETURN_FALSE;
1428 }
1429 Py_RETURN_TRUE;
1430 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 it = PyObject_GetIter(other);
1433 if (it == NULL)
1434 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 while ((key = PyIter_Next(it)) != NULL) {
1437 int rv;
1438 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001439 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 if (hash == -1) {
1442 Py_DECREF(key);
1443 Py_DECREF(it);
1444 return NULL;
1445 }
1446 entry.hash = hash;
1447 entry.key = key;
1448 rv = set_contains_entry(so, &entry);
1449 Py_DECREF(key);
1450 if (rv == -1) {
1451 Py_DECREF(it);
1452 return NULL;
1453 }
1454 if (rv) {
1455 Py_DECREF(it);
1456 Py_RETURN_FALSE;
1457 }
1458 }
1459 Py_DECREF(it);
1460 if (PyErr_Occurred())
1461 return NULL;
1462 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001463}
1464
1465PyDoc_STRVAR(isdisjoint_doc,
1466"Return True if two sets have a null intersection.");
1467
Neal Norwitz6576bd82005-11-13 18:41:28 +00001468static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001469set_difference_update_internal(PySetObject *so, PyObject *other)
1470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 if ((PyObject *)so == other)
1472 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 if (PyAnySet_Check(other)) {
1475 setentry *entry;
1476 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 while (set_next((PySetObject *)other, &pos, &entry))
1479 if (set_discard_entry(so, entry) == -1)
1480 return -1;
1481 } else {
1482 PyObject *key, *it;
1483 it = PyObject_GetIter(other);
1484 if (it == NULL)
1485 return -1;
1486
1487 while ((key = PyIter_Next(it)) != NULL) {
1488 if (set_discard_key(so, key) == -1) {
1489 Py_DECREF(it);
1490 Py_DECREF(key);
1491 return -1;
1492 }
1493 Py_DECREF(key);
1494 }
1495 Py_DECREF(it);
1496 if (PyErr_Occurred())
1497 return -1;
1498 }
1499 /* If more than 1/5 are dummies, then resize them away. */
1500 if ((so->fill - so->used) * 5 < so->mask)
1501 return 0;
1502 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001503}
1504
Raymond Hettingera690a992003-11-16 16:17:49 +00001505static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001506set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1511 PyObject *other = PyTuple_GET_ITEM(args, i);
1512 if (set_difference_update_internal(so, other) == -1)
1513 return NULL;
1514 }
1515 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001516}
1517
1518PyDoc_STRVAR(difference_update_doc,
1519"Remove all elements of another set from this set.");
1520
1521static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001522set_copy_and_difference(PySetObject *so, PyObject *other)
1523{
1524 PyObject *result;
1525
1526 result = set_copy(so);
1527 if (result == NULL)
1528 return NULL;
1529 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1530 return result;
1531 Py_DECREF(result);
1532 return NULL;
1533}
1534
1535static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001536set_difference(PySetObject *so, PyObject *other)
1537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 PyObject *result;
1539 setentry *entry;
1540 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001543 return set_copy_and_difference(so, other);
1544 }
1545
1546 /* If len(so) much more than len(other), it's more efficient to simply copy
1547 * so and then iterate other looking for common elements. */
1548 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1549 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 result = make_new_set_basetype(Py_TYPE(so), NULL);
1553 if (result == NULL)
1554 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (PyDict_CheckExact(other)) {
1557 while (set_next(so, &pos, &entry)) {
1558 setentry entrycopy;
1559 entrycopy.hash = entry->hash;
1560 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001561 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1563 Py_DECREF(result);
1564 return NULL;
1565 }
1566 }
1567 }
1568 return result;
1569 }
1570
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001571 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 while (set_next(so, &pos, &entry)) {
1573 int rv = set_contains_entry((PySetObject *)other, entry);
1574 if (rv == -1) {
1575 Py_DECREF(result);
1576 return NULL;
1577 }
1578 if (!rv) {
1579 if (set_add_entry((PySetObject *)result, entry) == -1) {
1580 Py_DECREF(result);
1581 return NULL;
1582 }
1583 }
1584 }
1585 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001586}
1587
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001588static PyObject *
1589set_difference_multi(PySetObject *so, PyObject *args)
1590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 Py_ssize_t i;
1592 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (PyTuple_GET_SIZE(args) == 0)
1595 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 other = PyTuple_GET_ITEM(args, 0);
1598 result = set_difference(so, other);
1599 if (result == NULL)
1600 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1603 other = PyTuple_GET_ITEM(args, i);
1604 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1605 Py_DECREF(result);
1606 return NULL;
1607 }
1608 }
1609 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001610}
1611
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001612PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001613"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001614\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001615(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001616static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001617set_sub(PySetObject *so, PyObject *other)
1618{
Brian Curtindfc80e32011-08-10 20:28:54 -05001619 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1620 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001622}
1623
1624static PyObject *
1625set_isub(PySetObject *so, PyObject *other)
1626{
Brian Curtindfc80e32011-08-10 20:28:54 -05001627 if (!PyAnySet_Check(other))
1628 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 if (set_difference_update_internal(so, other) == -1)
1630 return NULL;
1631 Py_INCREF(so);
1632 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001633}
1634
1635static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001636set_symmetric_difference_update(PySetObject *so, PyObject *other)
1637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 PySetObject *otherset;
1639 PyObject *key;
1640 Py_ssize_t pos = 0;
1641 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 if ((PyObject *)so == other)
1644 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 if (PyDict_CheckExact(other)) {
1647 PyObject *value;
1648 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001649 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1651 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001652
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001653 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 an_entry.hash = hash;
1655 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001658 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001659 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001662 if (rv == DISCARD_NOTFOUND) {
1663 if (set_add_entry(so, &an_entry) == -1) {
1664 Py_DECREF(key);
1665 return NULL;
1666 }
1667 }
Georg Brandl2d444492010-09-03 10:52:55 +00001668 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 }
1670 Py_RETURN_NONE;
1671 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 if (PyAnySet_Check(other)) {
1674 Py_INCREF(other);
1675 otherset = (PySetObject *)other;
1676 } else {
1677 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1678 if (otherset == NULL)
1679 return NULL;
1680 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 while (set_next(otherset, &pos, &entry)) {
1683 int rv = set_discard_entry(so, entry);
1684 if (rv == -1) {
1685 Py_DECREF(otherset);
1686 return NULL;
1687 }
1688 if (rv == DISCARD_NOTFOUND) {
1689 if (set_add_entry(so, entry) == -1) {
1690 Py_DECREF(otherset);
1691 return NULL;
1692 }
1693 }
1694 }
1695 Py_DECREF(otherset);
1696 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001697}
1698
1699PyDoc_STRVAR(symmetric_difference_update_doc,
1700"Update a set with the symmetric difference of itself and another.");
1701
1702static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001703set_symmetric_difference(PySetObject *so, PyObject *other)
1704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 PyObject *rv;
1706 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1709 if (otherset == NULL)
1710 return NULL;
1711 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1712 if (rv == NULL)
1713 return NULL;
1714 Py_DECREF(rv);
1715 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001716}
1717
1718PyDoc_STRVAR(symmetric_difference_doc,
1719"Return the symmetric difference of two sets as a new set.\n\
1720\n\
1721(i.e. all elements that are in exactly one of the sets.)");
1722
1723static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001724set_xor(PySetObject *so, PyObject *other)
1725{
Brian Curtindfc80e32011-08-10 20:28:54 -05001726 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1727 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001729}
1730
1731static PyObject *
1732set_ixor(PySetObject *so, PyObject *other)
1733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001735
Brian Curtindfc80e32011-08-10 20:28:54 -05001736 if (!PyAnySet_Check(other))
1737 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 result = set_symmetric_difference_update(so, other);
1739 if (result == NULL)
1740 return NULL;
1741 Py_DECREF(result);
1742 Py_INCREF(so);
1743 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001744}
1745
1746static PyObject *
1747set_issubset(PySetObject *so, PyObject *other)
1748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 setentry *entry;
1750 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 if (!PyAnySet_Check(other)) {
1753 PyObject *tmp, *result;
1754 tmp = make_new_set(&PySet_Type, other);
1755 if (tmp == NULL)
1756 return NULL;
1757 result = set_issubset(so, tmp);
1758 Py_DECREF(tmp);
1759 return result;
1760 }
1761 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1762 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 while (set_next(so, &pos, &entry)) {
1765 int rv = set_contains_entry((PySetObject *)other, entry);
1766 if (rv == -1)
1767 return NULL;
1768 if (!rv)
1769 Py_RETURN_FALSE;
1770 }
1771 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001772}
1773
1774PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1775
1776static PyObject *
1777set_issuperset(PySetObject *so, PyObject *other)
1778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 if (!PyAnySet_Check(other)) {
1782 tmp = make_new_set(&PySet_Type, other);
1783 if (tmp == NULL)
1784 return NULL;
1785 result = set_issuperset(so, tmp);
1786 Py_DECREF(tmp);
1787 return result;
1788 }
1789 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001790}
1791
1792PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1793
Raymond Hettingera690a992003-11-16 16:17:49 +00001794static PyObject *
1795set_richcompare(PySetObject *v, PyObject *w, int op)
1796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001798
Brian Curtindfc80e32011-08-10 20:28:54 -05001799 if(!PyAnySet_Check(w))
1800 Py_RETURN_NOTIMPLEMENTED;
1801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 switch (op) {
1803 case Py_EQ:
1804 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1805 Py_RETURN_FALSE;
1806 if (v->hash != -1 &&
1807 ((PySetObject *)w)->hash != -1 &&
1808 v->hash != ((PySetObject *)w)->hash)
1809 Py_RETURN_FALSE;
1810 return set_issubset(v, w);
1811 case Py_NE:
1812 r1 = set_richcompare(v, w, Py_EQ);
1813 if (r1 == NULL)
1814 return NULL;
1815 r2 = PyBool_FromLong(PyObject_Not(r1));
1816 Py_DECREF(r1);
1817 return r2;
1818 case Py_LE:
1819 return set_issubset(v, w);
1820 case Py_GE:
1821 return set_issuperset(v, w);
1822 case Py_LT:
1823 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1824 Py_RETURN_FALSE;
1825 return set_issubset(v, w);
1826 case Py_GT:
1827 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1828 Py_RETURN_FALSE;
1829 return set_issuperset(v, w);
1830 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001831 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001832}
1833
Raymond Hettingera690a992003-11-16 16:17:49 +00001834static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001835set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 if (set_add_key(so, key) == -1)
1838 return NULL;
1839 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001840}
1841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001843"Add an element to a set.\n\
1844\n\
1845This has no effect if the element is already present.");
1846
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001847static int
1848set_contains(PySetObject *so, PyObject *key)
1849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 PyObject *tmpkey;
1851 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 rv = set_contains_key(so, key);
1854 if (rv == -1) {
1855 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1856 return -1;
1857 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001858 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 if (tmpkey == NULL)
1860 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001861 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 Py_DECREF(tmpkey);
1863 }
1864 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001865}
1866
1867static PyObject *
1868set_direct_contains(PySetObject *so, PyObject *key)
1869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 result = set_contains(so, key);
1873 if (result == -1)
1874 return NULL;
1875 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001876}
1877
1878PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1879
Raymond Hettingera690a992003-11-16 16:17:49 +00001880static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001881set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 PyObject *tmpkey;
1884 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 rv = set_discard_key(so, key);
1887 if (rv == -1) {
1888 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1889 return NULL;
1890 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001891 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 if (tmpkey == NULL)
1893 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 Py_DECREF(tmpkey);
1896 if (rv == -1)
1897 return NULL;
1898 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001901 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 return NULL;
1903 }
1904 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001905}
1906
1907PyDoc_STRVAR(remove_doc,
1908"Remove an element from a set; it must be a member.\n\
1909\n\
1910If the element is not a member, raise a KeyError.");
1911
1912static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001913set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001914{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001915 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 rv = set_discard_key(so, key);
1919 if (rv == -1) {
1920 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1921 return NULL;
1922 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001923 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 if (tmpkey == NULL)
1925 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001926 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001928 if (rv == -1)
1929 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 }
1931 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001932}
1933
1934PyDoc_STRVAR(discard_doc,
1935"Remove an element from a set if it is a member.\n\
1936\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001938
1939static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001940set_reduce(PySetObject *so)
1941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001943 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 keys = PySequence_List((PyObject *)so);
1946 if (keys == NULL)
1947 goto done;
1948 args = PyTuple_Pack(1, keys);
1949 if (args == NULL)
1950 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001951 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (dict == NULL) {
1953 PyErr_Clear();
1954 dict = Py_None;
1955 Py_INCREF(dict);
1956 }
1957 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001958done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 Py_XDECREF(args);
1960 Py_XDECREF(keys);
1961 Py_XDECREF(dict);
1962 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001963}
1964
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001965static PyObject *
1966set_sizeof(PySetObject *so)
1967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 res = sizeof(PySetObject);
1971 if (so->table != so->smalltable)
1972 res = res + (so->mask + 1) * sizeof(setentry);
1973 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001974}
1975
1976PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001977static int
1978set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 if (!PyAnySet_Check(self))
1983 return -1;
1984 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1985 return -1;
1986 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1987 return -1;
1988 set_clear_internal(self);
1989 self->hash = -1;
1990 if (iterable == NULL)
1991 return 0;
1992 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001993}
1994
Raymond Hettingera690a992003-11-16 16:17:49 +00001995static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 set_len, /* sq_length */
1997 0, /* sq_concat */
1998 0, /* sq_repeat */
1999 0, /* sq_item */
2000 0, /* sq_slice */
2001 0, /* sq_ass_item */
2002 0, /* sq_ass_slice */
2003 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002004};
2005
2006/* set object ********************************************************/
2007
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002008#ifdef Py_DEBUG
2009static PyObject *test_c_api(PySetObject *so);
2010
2011PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2012All is well if assertions don't fail.");
2013#endif
2014
Raymond Hettingera690a992003-11-16 16:17:49 +00002015static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 {"add", (PyCFunction)set_add, METH_O,
2017 add_doc},
2018 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2019 clear_doc},
2020 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2021 contains_doc},
2022 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2023 copy_doc},
2024 {"discard", (PyCFunction)set_discard, METH_O,
2025 discard_doc},
2026 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2027 difference_doc},
2028 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2029 difference_update_doc},
2030 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2031 intersection_doc},
2032 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2033 intersection_update_doc},
2034 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2035 isdisjoint_doc},
2036 {"issubset", (PyCFunction)set_issubset, METH_O,
2037 issubset_doc},
2038 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2039 issuperset_doc},
2040 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2041 pop_doc},
2042 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2043 reduce_doc},
2044 {"remove", (PyCFunction)set_remove, METH_O,
2045 remove_doc},
2046 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2047 sizeof_doc},
2048 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2049 symmetric_difference_doc},
2050 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2051 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002052#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2054 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002055#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 {"union", (PyCFunction)set_union, METH_VARARGS,
2057 union_doc},
2058 {"update", (PyCFunction)set_update, METH_VARARGS,
2059 update_doc},
2060 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002061};
2062
2063static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 0, /*nb_add*/
2065 (binaryfunc)set_sub, /*nb_subtract*/
2066 0, /*nb_multiply*/
2067 0, /*nb_remainder*/
2068 0, /*nb_divmod*/
2069 0, /*nb_power*/
2070 0, /*nb_negative*/
2071 0, /*nb_positive*/
2072 0, /*nb_absolute*/
2073 0, /*nb_bool*/
2074 0, /*nb_invert*/
2075 0, /*nb_lshift*/
2076 0, /*nb_rshift*/
2077 (binaryfunc)set_and, /*nb_and*/
2078 (binaryfunc)set_xor, /*nb_xor*/
2079 (binaryfunc)set_or, /*nb_or*/
2080 0, /*nb_int*/
2081 0, /*nb_reserved*/
2082 0, /*nb_float*/
2083 0, /*nb_inplace_add*/
2084 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2085 0, /*nb_inplace_multiply*/
2086 0, /*nb_inplace_remainder*/
2087 0, /*nb_inplace_power*/
2088 0, /*nb_inplace_lshift*/
2089 0, /*nb_inplace_rshift*/
2090 (binaryfunc)set_iand, /*nb_inplace_and*/
2091 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2092 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002093};
2094
2095PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002096"set() -> new empty set object\n\
2097set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002098\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002099Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002100
2101PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2103 "set", /* tp_name */
2104 sizeof(PySetObject), /* tp_basicsize */
2105 0, /* tp_itemsize */
2106 /* methods */
2107 (destructor)set_dealloc, /* tp_dealloc */
2108 0, /* tp_print */
2109 0, /* tp_getattr */
2110 0, /* tp_setattr */
2111 0, /* tp_reserved */
2112 (reprfunc)set_repr, /* tp_repr */
2113 &set_as_number, /* tp_as_number */
2114 &set_as_sequence, /* tp_as_sequence */
2115 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002116 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 0, /* tp_call */
2118 0, /* tp_str */
2119 PyObject_GenericGetAttr, /* tp_getattro */
2120 0, /* tp_setattro */
2121 0, /* tp_as_buffer */
2122 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2123 Py_TPFLAGS_BASETYPE, /* tp_flags */
2124 set_doc, /* tp_doc */
2125 (traverseproc)set_traverse, /* tp_traverse */
2126 (inquiry)set_clear_internal, /* tp_clear */
2127 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002128 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2129 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 0, /* tp_iternext */
2131 set_methods, /* tp_methods */
2132 0, /* tp_members */
2133 0, /* tp_getset */
2134 0, /* tp_base */
2135 0, /* tp_dict */
2136 0, /* tp_descr_get */
2137 0, /* tp_descr_set */
2138 0, /* tp_dictoffset */
2139 (initproc)set_init, /* tp_init */
2140 PyType_GenericAlloc, /* tp_alloc */
2141 set_new, /* tp_new */
2142 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002143};
2144
2145/* frozenset object ********************************************************/
2146
2147
2148static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2150 contains_doc},
2151 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2152 copy_doc},
2153 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2154 difference_doc},
2155 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2156 intersection_doc},
2157 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2158 isdisjoint_doc},
2159 {"issubset", (PyCFunction)set_issubset, METH_O,
2160 issubset_doc},
2161 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2162 issuperset_doc},
2163 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2164 reduce_doc},
2165 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2166 sizeof_doc},
2167 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2168 symmetric_difference_doc},
2169 {"union", (PyCFunction)set_union, METH_VARARGS,
2170 union_doc},
2171 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002172};
2173
2174static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 0, /*nb_add*/
2176 (binaryfunc)set_sub, /*nb_subtract*/
2177 0, /*nb_multiply*/
2178 0, /*nb_remainder*/
2179 0, /*nb_divmod*/
2180 0, /*nb_power*/
2181 0, /*nb_negative*/
2182 0, /*nb_positive*/
2183 0, /*nb_absolute*/
2184 0, /*nb_bool*/
2185 0, /*nb_invert*/
2186 0, /*nb_lshift*/
2187 0, /*nb_rshift*/
2188 (binaryfunc)set_and, /*nb_and*/
2189 (binaryfunc)set_xor, /*nb_xor*/
2190 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002191};
2192
2193PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002194"frozenset() -> empty frozenset object\n\
2195frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002196\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002197Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002198
2199PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2201 "frozenset", /* tp_name */
2202 sizeof(PySetObject), /* tp_basicsize */
2203 0, /* tp_itemsize */
2204 /* methods */
2205 (destructor)set_dealloc, /* tp_dealloc */
2206 0, /* tp_print */
2207 0, /* tp_getattr */
2208 0, /* tp_setattr */
2209 0, /* tp_reserved */
2210 (reprfunc)set_repr, /* tp_repr */
2211 &frozenset_as_number, /* tp_as_number */
2212 &set_as_sequence, /* tp_as_sequence */
2213 0, /* tp_as_mapping */
2214 frozenset_hash, /* tp_hash */
2215 0, /* tp_call */
2216 0, /* tp_str */
2217 PyObject_GenericGetAttr, /* tp_getattro */
2218 0, /* tp_setattro */
2219 0, /* tp_as_buffer */
2220 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2221 Py_TPFLAGS_BASETYPE, /* tp_flags */
2222 frozenset_doc, /* tp_doc */
2223 (traverseproc)set_traverse, /* tp_traverse */
2224 (inquiry)set_clear_internal, /* tp_clear */
2225 (richcmpfunc)set_richcompare, /* tp_richcompare */
2226 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2227 (getiterfunc)set_iter, /* tp_iter */
2228 0, /* tp_iternext */
2229 frozenset_methods, /* tp_methods */
2230 0, /* tp_members */
2231 0, /* tp_getset */
2232 0, /* tp_base */
2233 0, /* tp_dict */
2234 0, /* tp_descr_get */
2235 0, /* tp_descr_set */
2236 0, /* tp_dictoffset */
2237 0, /* tp_init */
2238 PyType_GenericAlloc, /* tp_alloc */
2239 frozenset_new, /* tp_new */
2240 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002241};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002242
2243
2244/***** C API functions *************************************************/
2245
2246PyObject *
2247PySet_New(PyObject *iterable)
2248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002250}
2251
2252PyObject *
2253PyFrozenSet_New(PyObject *iterable)
2254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002256}
2257
Neal Norwitz8c49c822006-03-04 18:41:19 +00002258Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002259PySet_Size(PyObject *anyset)
2260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 if (!PyAnySet_Check(anyset)) {
2262 PyErr_BadInternalCall();
2263 return -1;
2264 }
2265 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002266}
2267
2268int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002269PySet_Clear(PyObject *set)
2270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 if (!PySet_Check(set)) {
2272 PyErr_BadInternalCall();
2273 return -1;
2274 }
2275 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002276}
2277
2278int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002279PySet_Contains(PyObject *anyset, PyObject *key)
2280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 if (!PyAnySet_Check(anyset)) {
2282 PyErr_BadInternalCall();
2283 return -1;
2284 }
2285 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002286}
2287
2288int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002289PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 if (!PySet_Check(set)) {
2292 PyErr_BadInternalCall();
2293 return -1;
2294 }
2295 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002296}
2297
2298int
Christian Heimesfd66e512008-01-29 12:18:50 +00002299PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (!PySet_Check(anyset) &&
2302 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2303 PyErr_BadInternalCall();
2304 return -1;
2305 }
2306 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002307}
2308
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002309int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002310_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 if (!PyAnySet_Check(set)) {
2315 PyErr_BadInternalCall();
2316 return -1;
2317 }
2318 if (set_next((PySetObject *)set, pos, &entry) == 0)
2319 return 0;
2320 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002321 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002323}
2324
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002325PyObject *
2326PySet_Pop(PyObject *set)
2327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 if (!PySet_Check(set)) {
2329 PyErr_BadInternalCall();
2330 return NULL;
2331 }
2332 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002333}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002334
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002335int
2336_PySet_Update(PyObject *set, PyObject *iterable)
2337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 if (!PySet_Check(set)) {
2339 PyErr_BadInternalCall();
2340 return -1;
2341 }
2342 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002343}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002344
Raymond Hettingere259f132013-12-15 11:56:14 -08002345/* Exported for the gdb plugin's benefit. */
2346PyObject *_PySet_Dummy = dummy;
2347
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002348#ifdef Py_DEBUG
2349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002351 Returns True and original set is restored. */
2352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353#define assertRaises(call_return_value, exception) \
2354 do { \
2355 assert(call_return_value); \
2356 assert(PyErr_ExceptionMatches(exception)); \
2357 PyErr_Clear(); \
2358 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002359
2360static PyObject *
2361test_c_api(PySetObject *so)
2362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 Py_ssize_t count;
2364 char *s;
2365 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002366 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002368 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 /* Verify preconditions */
2372 assert(PyAnySet_Check(ob));
2373 assert(PyAnySet_CheckExact(ob));
2374 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 /* so.clear(); so |= set("abc"); */
2377 str = PyUnicode_FromString("abc");
2378 if (str == NULL)
2379 return NULL;
2380 set_clear_internal(so);
2381 if (set_update_internal(so, str) == -1) {
2382 Py_DECREF(str);
2383 return NULL;
2384 }
2385 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 /* Exercise type/size checks */
2388 assert(PySet_Size(ob) == 3);
2389 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* Raise TypeError for non-iterable constructor arguments */
2392 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2393 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 /* Raise TypeError for unhashable key */
2396 dup = PySet_New(ob);
2397 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2398 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2399 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* Exercise successful pop, contains, add, and discard */
2402 elem = PySet_Pop(ob);
2403 assert(PySet_Contains(ob, elem) == 0);
2404 assert(PySet_GET_SIZE(ob) == 2);
2405 assert(PySet_Add(ob, elem) == 0);
2406 assert(PySet_Contains(ob, elem) == 1);
2407 assert(PySet_GET_SIZE(ob) == 3);
2408 assert(PySet_Discard(ob, elem) == 1);
2409 assert(PySet_GET_SIZE(ob) == 2);
2410 assert(PySet_Discard(ob, elem) == 0);
2411 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Exercise clear */
2414 dup2 = PySet_New(dup);
2415 assert(PySet_Clear(dup2) == 0);
2416 assert(PySet_Size(dup2) == 0);
2417 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* Raise SystemError on clear or update of frozen set */
2420 f = PyFrozenSet_New(dup);
2421 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2422 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2423 assert(PySet_Add(f, elem) == 0);
2424 Py_INCREF(f);
2425 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2426 Py_DECREF(f);
2427 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* Exercise direct iteration */
2430 i = 0, count = 0;
2431 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2432 s = _PyUnicode_AsString(x);
2433 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2434 count++;
2435 }
2436 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 /* Exercise updates */
2439 dup2 = PySet_New(NULL);
2440 assert(_PySet_Update(dup2, dup) == 0);
2441 assert(PySet_Size(dup2) == 3);
2442 assert(_PySet_Update(dup2, dup) == 0);
2443 assert(PySet_Size(dup2) == 3);
2444 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 /* Raise SystemError when self argument is not a set or frozenset. */
2447 t = PyTuple_New(0);
2448 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2449 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2450 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 /* Raise SystemError when self argument is not a set. */
2453 f = PyFrozenSet_New(dup);
2454 assert(PySet_Size(f) == 3);
2455 assert(PyFrozenSet_CheckExact(f));
2456 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2457 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2458 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* Raise KeyError when popping from an empty set */
2461 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2462 Py_DECREF(ob);
2463 assert(PySet_GET_SIZE(ob) == 0);
2464 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 /* Restore the set from the copy using the PyNumber API */
2467 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2468 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 /* Verify constructors accept NULL arguments */
2471 f = PySet_New(NULL);
2472 assert(f != NULL);
2473 assert(PySet_GET_SIZE(f) == 0);
2474 Py_DECREF(f);
2475 f = PyFrozenSet_New(NULL);
2476 assert(f != NULL);
2477 assert(PyFrozenSet_CheckExact(f));
2478 assert(PySet_GET_SIZE(f) == 0);
2479 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 Py_DECREF(elem);
2482 Py_DECREF(dup);
2483 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002484}
2485
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002486#undef assertRaises
2487
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002488#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002489
2490/***** Dummy Struct *************************************************/
2491
2492static PyObject *
2493dummy_repr(PyObject *op)
2494{
2495 return PyUnicode_FromString("<dummy key>");
2496}
2497
2498static void
2499dummy_dealloc(PyObject* ignore)
2500{
2501 Py_FatalError("deallocating <dummy key>");
2502}
2503
2504static PyTypeObject _PySetDummy_Type = {
2505 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2506 "<dummy key> type",
2507 0,
2508 0,
2509 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2510 0, /*tp_print*/
2511 0, /*tp_getattr*/
2512 0, /*tp_setattr*/
2513 0, /*tp_reserved*/
2514 dummy_repr, /*tp_repr*/
2515 0, /*tp_as_number*/
2516 0, /*tp_as_sequence*/
2517 0, /*tp_as_mapping*/
2518 0, /*tp_hash */
2519 0, /*tp_call */
2520 0, /*tp_str */
2521 0, /*tp_getattro */
2522 0, /*tp_setattro */
2523 0, /*tp_as_buffer */
2524 Py_TPFLAGS_DEFAULT, /*tp_flags */
2525};
2526
2527static PyObject _dummy_struct = {
2528 _PyObject_EXTRA_INIT
2529 2, &_PySetDummy_Type
2530};
2531