blob: 8f7542c89ef654b2af5702713965a2ab3d881afa [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettinger426d9952014-05-18 21:40:20 +01007 Copyright (c) 2003-2014 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
26 Unlike the dictionary implementation, the lookkey functions can return
27 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070055 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t perturb = hash;
58 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070059 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070060 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070061 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062
Raymond Hettingera35adf52013-09-02 03:23:21 -070063 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070064 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070066
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070067 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070069 return entry;
Raymond Hettinger426d9952014-05-18 21:40:20 +010070 if (entry->hash == hash && entry->key != dummy) { /* dummy match unlikely */
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);
Raymond Hettinger426d9952014-05-18 21:40:20 +010075 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010077 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010079 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070080 return entry;
81 }
82 if (entry->key == dummy && freeslot == NULL)
83 freeslot = entry;
84
Raymond Hettingerc70a2b72013-09-21 14:02:55 -070085 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
86 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -070087 if (entry->key == NULL)
88 goto found_null;
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. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100138 if (!PyUnicode_CheckExact(key)) { /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 so->lookup = set_lookkey;
140 return set_lookkey(so, key, hash);
141 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700142
Raymond Hettingera35adf52013-09-02 03:23:21 -0700143 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700144 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000146
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700147 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700148 if (entry->key == key
149 || (entry->hash == hash
Raymond Hettinger426d9952014-05-18 21:40:20 +0100150 && entry->key != dummy /* unlikely */
151 && unicode_eq(entry->key, key))) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -0700152 return entry;
153 if (entry->key == dummy && freeslot == NULL)
154 freeslot = entry;
155
Raymond Hettingerc70a2b72013-09-21 14:02:55 -0700156 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
157 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -0700158 if (entry->key == NULL)
159 goto found_null;
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. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100270 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 for (newsize = PySet_MINSIZE;
272 newsize <= minused && newsize > 0;
273 newsize <<= 1)
274 ;
275 if (newsize <= 0) {
276 PyErr_NoMemory();
277 return -1;
278 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 /* Get space for a new table. */
281 oldtable = so->table;
282 assert(oldtable != NULL);
283 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (newsize == PySet_MINSIZE) {
286 /* A large table is shrinking, or we can't get any smaller. */
287 newtable = so->smalltable;
288 if (newtable == oldtable) {
289 if (so->fill == so->used) {
290 /* No dummies, so no point doing anything. */
291 return 0;
292 }
293 /* We're not going to resize it, but rebuild the
294 table anyway to purge old dummy entries.
295 Subtle: This is *necessary* if fill==size,
296 as set_lookkey needs at least one virgin slot to
297 terminate failing searches. If fill < size, it's
298 merely desirable, as dummies slow searches. */
299 assert(so->fill > so->used);
300 memcpy(small_copy, oldtable, sizeof(small_copy));
301 oldtable = small_copy;
302 }
303 }
304 else {
305 newtable = PyMem_NEW(setentry, newsize);
306 if (newtable == NULL) {
307 PyErr_NoMemory();
308 return -1;
309 }
310 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 /* Make the set empty, using the new table. */
313 assert(newtable != oldtable);
314 so->table = newtable;
315 so->mask = newsize - 1;
316 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700317 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 /* Copy the data over; this is refcount-neutral for active entries;
322 dummy entries aren't copied over, of course */
323 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500324 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000326 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 }
328 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 if (is_oldtable_malloced)
331 PyMem_DEL(oldtable);
332 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000333}
334
Raymond Hettingerc991db22005-08-11 07:58:45 +0000335/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
336
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200338set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000339{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200340 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000341 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100342 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 assert(so->fill <= so->mask); /* at least one empty slot */
345 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000346 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100347 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000348 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return -1;
350 }
351 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
352 return 0;
353 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000354}
355
356static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200357set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000358{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200359 Py_hash_t hash;
360 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200363 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 hash = PyObject_Hash(key);
365 if (hash == -1)
366 return -1;
367 }
368 assert(so->fill <= so->mask); /* at least one empty slot */
369 n_used = so->used;
370 Py_INCREF(key);
371 if (set_insert_key(so, key, hash) == -1) {
372 Py_DECREF(key);
373 return -1;
374 }
375 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
376 return 0;
377 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000378}
379
380#define DISCARD_NOTFOUND 0
381#define DISCARD_FOUND 1
382
383static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000384set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200385{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000387
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000388 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (entry == NULL)
390 return -1;
391 if (entry->key == NULL || entry->key == dummy)
392 return DISCARD_NOTFOUND;
393 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 entry->key = dummy;
395 so->used--;
396 Py_DECREF(old_key);
397 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000398}
399
400static int
401set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200403 Py_hash_t hash;
404 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200410 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 hash = PyObject_Hash(key);
412 if (hash == -1)
413 return -1;
414 }
415 entry = (so->lookup)(so, key, hash);
416 if (entry == NULL)
417 return -1;
418 if (entry->key == NULL || entry->key == dummy)
419 return DISCARD_NOTFOUND;
420 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 entry->key = dummy;
422 so->used--;
423 Py_DECREF(old_key);
424 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000425}
426
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700427static void
428set_empty_to_minsize(PySetObject *so)
429{
430 memset(so->smalltable, 0, sizeof(so->smalltable));
431 so->fill = 0;
432 so->used = 0;
433 so->mask = PySet_MINSIZE - 1;
434 so->table = so->smalltable;
435 so->hash = -1;
436}
437
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000438static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439set_clear_internal(PySetObject *so)
440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 setentry *entry, *table;
442 int table_is_malloced;
443 Py_ssize_t fill;
444 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000445
Raymond Hettinger583cd032013-09-07 22:06:35 -0700446#ifdef Py_DEBUG
447 Py_ssize_t i = 0;
448 Py_ssize_t n = so->mask + 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449#endif
450
Raymond Hettinger583cd032013-09-07 22:06:35 -0700451 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 table = so->table;
453 assert(table != NULL);
454 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 /* This is delicate. During the process of clearing the set,
457 * decrefs can cause the set to mutate. To avoid fatal confusion
458 * (voice of experience), we have to make the set empty before
459 * clearing the slots, and never refer to anything via so->ref while
460 * clearing.
461 */
462 fill = so->fill;
463 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700464 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 else if (fill > 0) {
467 /* It's a small table with something that needs to be cleared.
468 * Afraid the only safe way is to copy the set entries into
469 * another small table first.
470 */
471 memcpy(small_copy, table, sizeof(small_copy));
472 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700473 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 }
475 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 /* Now we can finally clear things. If C had refcounts, we could
478 * assert that the refcount on table is 1 now, i.e. that this function
479 * has unique access to it, so decref side-effects can't alter it.
480 */
481 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 assert(i < n);
484 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000485#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if (entry->key) {
487 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700488 if (entry->key != dummy)
489 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 else
493 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (table_is_malloced)
498 PyMem_DEL(table);
499 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500}
501
502/*
503 * Iterate over a set table. Use like so:
504 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000506 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000507 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000508 * while (set_next(yourset, &pos, &entry)) {
509 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000510 * }
511 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000512 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000514 */
515static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000516set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 Py_ssize_t i;
519 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200520 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 assert (PyAnySet_Check(so));
523 i = *pos_ptr;
524 assert(i >= 0);
525 table = so->table;
526 mask = so->mask;
527 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
528 i++;
529 *pos_ptr = i+1;
530 if (i > mask)
531 return 0;
532 assert(table[i].key != NULL);
533 *entry_ptr = &table[i];
534 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000535}
536
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000537static void
538set_dealloc(PySetObject *so)
539{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200540 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 Py_ssize_t fill = so->fill;
542 PyObject_GC_UnTrack(so);
543 Py_TRASHCAN_SAFE_BEGIN(so)
544 if (so->weakreflist != NULL)
545 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 for (entry = so->table; fill > 0; entry++) {
548 if (entry->key) {
549 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700550 if (entry->key != dummy)
551 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 }
553 }
554 if (so->table != so->smalltable)
555 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700556 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000558}
559
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000560static PyObject *
561set_repr(PySetObject *so)
562{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200563 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 if (status != 0) {
567 if (status < 0)
568 return NULL;
569 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
570 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 /* shortcut for the empty set */
573 if (!so->used) {
574 Py_ReprLeave((PyObject*)so);
575 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
576 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 keys = PySequence_List((PyObject *)so);
579 if (keys == NULL)
580 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000581
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200582 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 listrepr = PyObject_Repr(keys);
584 Py_DECREF(keys);
585 if (listrepr == NULL)
586 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200587 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200589 if (tmp == NULL)
590 goto done;
591 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200592
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200593 if (Py_TYPE(so) != &PySet_Type)
594 result = PyUnicode_FromFormat("%s({%U})",
595 Py_TYPE(so)->tp_name,
596 listrepr);
597 else
598 result = PyUnicode_FromFormat("{%U}", listrepr);
599 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000600done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 Py_ReprLeave((PyObject*)so);
602 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000603}
604
Martin v. Löwis18e16552006-02-15 17:27:45 +0000605static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000606set_len(PyObject *so)
607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000609}
610
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000611static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000612set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000615 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100616 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200617 Py_ssize_t i;
618 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 assert (PyAnySet_Check(so));
621 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 other = (PySetObject*)otherset;
624 if (other == so || other->used == 0)
625 /* a.update(a) or a.update({}); nothing to do */
626 return 0;
627 /* Do one big resize at the start, rather than
628 * incrementally resizing as we insert new keys. Expect
629 * that there will be no (or few) overlapping keys.
630 */
631 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
632 if (set_table_resize(so, (so->used + other->used)*2) != 0)
633 return -1;
634 }
635 for (i = 0; i <= other->mask; i++) {
636 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000637 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100638 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000639 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500640 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000641 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100642 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000643 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 return -1;
645 }
646 }
647 }
648 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000649}
650
651static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000652set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000653{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000654 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200658 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 hash = PyObject_Hash(key);
660 if (hash == -1)
661 return -1;
662 }
663 entry = (so->lookup)(so, key, hash);
664 if (entry == NULL)
665 return -1;
666 key = entry->key;
667 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668}
669
Raymond Hettingerc991db22005-08-11 07:58:45 +0000670static int
671set_contains_entry(PySetObject *so, setentry *entry)
672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 PyObject *key;
674 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000675
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000676 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 if (lu_entry == NULL)
678 return -1;
679 key = lu_entry->key;
680 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000681}
682
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000683static PyObject *
684set_pop(PySetObject *so)
685{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200686 Py_ssize_t i = 0;
687 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 assert (PyAnySet_Check(so));
691 if (so->used == 0) {
692 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
693 return NULL;
694 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 /* Set entry to "the first" unused or dummy set entry. We abuse
697 * the hash field of slot 0 to hold a search finger:
698 * If slot 0 has a value, use slot 0.
699 * Else slot 0 is being used to hold a search finger,
700 * and we use its hash value as the first index to look.
701 */
702 entry = &so->table[0];
703 if (entry->key == NULL || entry->key == dummy) {
704 i = entry->hash;
705 /* The hash field may be a real hash value, or it may be a
706 * legit search finger, or it may be a once-legit search
707 * finger that's out of bounds now because it wrapped around
708 * or the table shrunk -- simply make sure it's in bounds now.
709 */
710 if (i > so->mask || i < 1)
711 i = 1; /* skip slot 0 */
712 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
713 i++;
714 if (i > so->mask)
715 i = 1;
716 }
717 }
718 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 entry->key = dummy;
720 so->used--;
721 so->table[0].hash = i + 1; /* next place to start */
722 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000723}
724
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000725PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
726Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000727
728static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000729set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 Py_ssize_t pos = 0;
732 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 while (set_next(so, &pos, &entry))
735 Py_VISIT(entry->key);
736 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000737}
738
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000739static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000740frozenset_hash(PyObject *self)
741{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800742 /* Most of the constants in this hash algorithm are randomly choosen
743 large primes with "interesting bit patterns" and that passed
744 tests for good collision statistics on a variety of problematic
745 datasets such as:
746
747 ps = []
748 for r in range(21):
749 ps += itertools.combinations(range(20), r)
750 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
751
752 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800754 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 setentry *entry;
756 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 if (so->hash != -1)
759 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000760
Mark Dickinson57e683e2011-09-24 18:18:40 +0100761 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 while (set_next(so, &pos, &entry)) {
763 /* Work to increase the bit dispersion for closely spaced hash
764 values. The is important because some use cases have many
765 combinations of a small number of elements with nearby
766 hashes so that many distinct combinations collapse to only
767 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000768 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800769 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800771 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500772 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800773 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200774 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800775 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 so->hash = hash;
777 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000778}
779
Raymond Hettingera9d99362005-08-05 00:01:15 +0000780/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000781
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000782typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 PyObject_HEAD
784 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
785 Py_ssize_t si_used;
786 Py_ssize_t si_pos;
787 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788} setiterobject;
789
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790static void
791setiter_dealloc(setiterobject *si)
792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 Py_XDECREF(si->si_set);
794 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000795}
796
797static int
798setiter_traverse(setiterobject *si, visitproc visit, void *arg)
799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 Py_VISIT(si->si_set);
801 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802}
803
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000804static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805setiter_len(setiterobject *si)
806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 Py_ssize_t len = 0;
808 if (si->si_set != NULL && si->si_used == si->si_set->used)
809 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000810 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811}
812
Armin Rigof5b3e362006-02-11 21:32:43 +0000813PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000814
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000815static PyObject *setiter_iternext(setiterobject *si);
816
817static PyObject *
818setiter_reduce(setiterobject *si)
819{
820 PyObject *list;
821 setiterobject tmp;
822
823 list = PyList_New(0);
824 if (!list)
825 return NULL;
826
Ezio Melotti0e1af282012-09-28 16:43:40 +0300827 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000828 tmp = *si;
829 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300830
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000831 /* iterate the temporary into a list */
832 for(;;) {
833 PyObject *element = setiter_iternext(&tmp);
834 if (element) {
835 if (PyList_Append(list, element)) {
836 Py_DECREF(element);
837 Py_DECREF(list);
838 Py_XDECREF(tmp.si_set);
839 return NULL;
840 }
841 Py_DECREF(element);
842 } else
843 break;
844 }
845 Py_XDECREF(tmp.si_set);
846 /* check for error */
847 if (tmp.si_set != NULL) {
848 /* we have an error */
849 Py_DECREF(list);
850 return NULL;
851 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200852 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000853}
854
855PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
856
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000857static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000859 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861};
862
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000863static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200866 Py_ssize_t i, mask;
867 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 if (so == NULL)
871 return NULL;
872 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (si->si_used != so->used) {
875 PyErr_SetString(PyExc_RuntimeError,
876 "Set changed size during iteration");
877 si->si_used = -1; /* Make this state sticky */
878 return NULL;
879 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 i = si->si_pos;
882 assert(i>=0);
883 entry = so->table;
884 mask = so->mask;
885 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
886 i++;
887 si->si_pos = i+1;
888 if (i > mask)
889 goto fail;
890 si->len--;
891 key = entry[i].key;
892 Py_INCREF(key);
893 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000894
895fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 Py_DECREF(so);
897 si->si_set = NULL;
898 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000899}
900
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000901PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 PyVarObject_HEAD_INIT(&PyType_Type, 0)
903 "set_iterator", /* tp_name */
904 sizeof(setiterobject), /* tp_basicsize */
905 0, /* tp_itemsize */
906 /* methods */
907 (destructor)setiter_dealloc, /* tp_dealloc */
908 0, /* tp_print */
909 0, /* tp_getattr */
910 0, /* tp_setattr */
911 0, /* tp_reserved */
912 0, /* tp_repr */
913 0, /* tp_as_number */
914 0, /* tp_as_sequence */
915 0, /* tp_as_mapping */
916 0, /* tp_hash */
917 0, /* tp_call */
918 0, /* tp_str */
919 PyObject_GenericGetAttr, /* tp_getattro */
920 0, /* tp_setattro */
921 0, /* tp_as_buffer */
922 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
923 0, /* tp_doc */
924 (traverseproc)setiter_traverse, /* tp_traverse */
925 0, /* tp_clear */
926 0, /* tp_richcompare */
927 0, /* tp_weaklistoffset */
928 PyObject_SelfIter, /* tp_iter */
929 (iternextfunc)setiter_iternext, /* tp_iternext */
930 setiter_methods, /* tp_methods */
931 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000932};
933
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000934static PyObject *
935set_iter(PySetObject *so)
936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
938 if (si == NULL)
939 return NULL;
940 Py_INCREF(so);
941 si->si_set = so;
942 si->si_used = so->used;
943 si->si_pos = 0;
944 si->len = so->used;
945 _PyObject_GC_TRACK(si);
946 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000947}
948
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000950set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (PyAnySet_Check(other))
955 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 if (PyDict_CheckExact(other)) {
958 PyObject *value;
959 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000960 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 /* Do one big resize at the start, rather than
964 * incrementally resizing as we insert new keys. Expect
965 * that there will be no (or few) overlapping keys.
966 */
967 if (dictsize == -1)
968 return -1;
969 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
970 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
971 return -1;
972 }
973 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
974 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 an_entry.hash = hash;
977 an_entry.key = key;
978 if (set_add_entry(so, &an_entry) == -1)
979 return -1;
980 }
981 return 0;
982 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 it = PyObject_GetIter(other);
985 if (it == NULL)
986 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 while ((key = PyIter_Next(it)) != NULL) {
989 if (set_add_key(so, key) == -1) {
990 Py_DECREF(it);
991 Py_DECREF(key);
992 return -1;
993 }
994 Py_DECREF(key);
995 }
996 Py_DECREF(it);
997 if (PyErr_Occurred())
998 return -1;
999 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001000}
1001
1002static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001003set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1008 PyObject *other = PyTuple_GET_ITEM(args, i);
1009 if (set_update_internal(so, other) == -1)
1010 return NULL;
1011 }
1012 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001013}
1014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001016"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001017
Raymond Hettinger426d9952014-05-18 21:40:20 +01001018/* XXX Todo:
1019 If aligned memory allocations become available, make the
1020 set object 64 byte aligned so that most of the fields
1021 can be retrieved or updated in a single cache line.
1022*/
1023
Raymond Hettingera38123e2003-11-24 22:18:49 +00001024static PyObject *
1025make_new_set(PyTypeObject *type, PyObject *iterable)
1026{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001027 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001030 so = (PySetObject *)type->tp_alloc(type, 0);
1031 if (so == NULL)
1032 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001033
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001034 so->fill = 0;
1035 so->used = 0;
1036 so->mask = PySet_MINSIZE - 1;
1037 so->table = so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 so->lookup = set_lookkey_unicode;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001039 so->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 if (iterable != NULL) {
1043 if (set_update_internal(so, iterable) == -1) {
1044 Py_DECREF(so);
1045 return NULL;
1046 }
1047 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001050}
1051
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001052static PyObject *
1053make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1056 if (PyType_IsSubtype(type, &PySet_Type))
1057 type = &PySet_Type;
1058 else
1059 type = &PyFrozenSet_Type;
1060 }
1061 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001062}
1063
Raymond Hettingerd7946662005-08-01 21:39:29 +00001064/* The empty frozenset is a singleton */
1065static PyObject *emptyfrozenset = NULL;
1066
Raymond Hettingera690a992003-11-16 16:17:49 +00001067static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001068frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1073 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1076 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (type != &PyFrozenSet_Type)
1079 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (iterable != NULL) {
1082 /* frozenset(f) is idempotent */
1083 if (PyFrozenSet_CheckExact(iterable)) {
1084 Py_INCREF(iterable);
1085 return iterable;
1086 }
1087 result = make_new_set(type, iterable);
1088 if (result == NULL || PySet_GET_SIZE(result))
1089 return result;
1090 Py_DECREF(result);
1091 }
1092 /* The empty frozenset is a singleton */
1093 if (emptyfrozenset == NULL)
1094 emptyfrozenset = make_new_set(type, NULL);
1095 Py_XINCREF(emptyfrozenset);
1096 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001097}
1098
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001099int
1100PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001101{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001102 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001103}
1104
1105void
1106PySet_Fini(void)
1107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001109}
1110
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001111static PyObject *
1112set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1115 return NULL;
1116
1117 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001118}
1119
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001120/* set_swap_bodies() switches the contents of any two sets by moving their
1121 internal data pointers and, if needed, copying the internal smalltables.
1122 Semantically equivalent to:
1123
1124 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1125
1126 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001128 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001130 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001131*/
1132
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001133static void
1134set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 Py_ssize_t t;
1137 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001138 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001140 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 t = a->fill; a->fill = b->fill; b->fill = t;
1143 t = a->used; a->used = b->used; b->used = t;
1144 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 u = a->table;
1147 if (a->table == a->smalltable)
1148 u = b->smalltable;
1149 a->table = b->table;
1150 if (b->table == b->smalltable)
1151 a->table = a->smalltable;
1152 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (a->table == a->smalltable || b->table == b->smalltable) {
1157 memcpy(tab, a->smalltable, sizeof(tab));
1158 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1159 memcpy(b->smalltable, tab, sizeof(tab));
1160 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1163 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1164 h = a->hash; a->hash = b->hash; b->hash = h;
1165 } else {
1166 a->hash = -1;
1167 b->hash = -1;
1168 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001169}
1170
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001171static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001172set_copy(PySetObject *so)
1173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001175}
1176
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001177static PyObject *
1178frozenset_copy(PySetObject *so)
1179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (PyFrozenSet_CheckExact(so)) {
1181 Py_INCREF(so);
1182 return (PyObject *)so;
1183 }
1184 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001185}
1186
Raymond Hettingera690a992003-11-16 16:17:49 +00001187PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1188
1189static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001190set_clear(PySetObject *so)
1191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 set_clear_internal(so);
1193 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001194}
1195
1196PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1197
1198static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001199set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 PySetObject *result;
1202 PyObject *other;
1203 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 result = (PySetObject *)set_copy(so);
1206 if (result == NULL)
1207 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1210 other = PyTuple_GET_ITEM(args, i);
1211 if ((PyObject *)so == other)
1212 continue;
1213 if (set_update_internal(result, other) == -1) {
1214 Py_DECREF(result);
1215 return NULL;
1216 }
1217 }
1218 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001219}
1220
1221PyDoc_STRVAR(union_doc,
1222 "Return the union of sets as a new set.\n\
1223\n\
1224(i.e. all elements that are in either set.)");
1225
1226static PyObject *
1227set_or(PySetObject *so, PyObject *other)
1228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001230
Brian Curtindfc80e32011-08-10 20:28:54 -05001231 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1232 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 result = (PySetObject *)set_copy(so);
1235 if (result == NULL)
1236 return NULL;
1237 if ((PyObject *)so == other)
1238 return (PyObject *)result;
1239 if (set_update_internal(result, other) == -1) {
1240 Py_DECREF(result);
1241 return NULL;
1242 }
1243 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001244}
1245
Raymond Hettingera690a992003-11-16 16:17:49 +00001246static PyObject *
1247set_ior(PySetObject *so, PyObject *other)
1248{
Brian Curtindfc80e32011-08-10 20:28:54 -05001249 if (!PyAnySet_Check(other))
1250 Py_RETURN_NOTIMPLEMENTED;
1251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (set_update_internal(so, other) == -1)
1253 return NULL;
1254 Py_INCREF(so);
1255 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001256}
1257
1258static PyObject *
1259set_intersection(PySetObject *so, PyObject *other)
1260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 PySetObject *result;
1262 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if ((PyObject *)so == other)
1265 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1268 if (result == NULL)
1269 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 if (PyAnySet_Check(other)) {
1272 Py_ssize_t pos = 0;
1273 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1276 tmp = (PyObject *)so;
1277 so = (PySetObject *)other;
1278 other = tmp;
1279 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 while (set_next((PySetObject *)other, &pos, &entry)) {
1282 int rv = set_contains_entry(so, entry);
1283 if (rv == -1) {
1284 Py_DECREF(result);
1285 return NULL;
1286 }
1287 if (rv) {
1288 if (set_add_entry(result, entry) == -1) {
1289 Py_DECREF(result);
1290 return NULL;
1291 }
1292 }
1293 }
1294 return (PyObject *)result;
1295 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 it = PyObject_GetIter(other);
1298 if (it == NULL) {
1299 Py_DECREF(result);
1300 return NULL;
1301 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 while ((key = PyIter_Next(it)) != NULL) {
1304 int rv;
1305 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001306 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (hash == -1) {
1309 Py_DECREF(it);
1310 Py_DECREF(result);
1311 Py_DECREF(key);
1312 return NULL;
1313 }
1314 entry.hash = hash;
1315 entry.key = key;
1316 rv = set_contains_entry(so, &entry);
1317 if (rv == -1) {
1318 Py_DECREF(it);
1319 Py_DECREF(result);
1320 Py_DECREF(key);
1321 return NULL;
1322 }
1323 if (rv) {
1324 if (set_add_entry(result, &entry) == -1) {
1325 Py_DECREF(it);
1326 Py_DECREF(result);
1327 Py_DECREF(key);
1328 return NULL;
1329 }
1330 }
1331 Py_DECREF(key);
1332 }
1333 Py_DECREF(it);
1334 if (PyErr_Occurred()) {
1335 Py_DECREF(result);
1336 return NULL;
1337 }
1338 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001339}
1340
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001341static PyObject *
1342set_intersection_multi(PySetObject *so, PyObject *args)
1343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 Py_ssize_t i;
1345 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 if (PyTuple_GET_SIZE(args) == 0)
1348 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 Py_INCREF(so);
1351 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1352 PyObject *other = PyTuple_GET_ITEM(args, i);
1353 PyObject *newresult = set_intersection((PySetObject *)result, other);
1354 if (newresult == NULL) {
1355 Py_DECREF(result);
1356 return NULL;
1357 }
1358 Py_DECREF(result);
1359 result = newresult;
1360 }
1361 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001362}
1363
Raymond Hettingera690a992003-11-16 16:17:49 +00001364PyDoc_STRVAR(intersection_doc,
1365"Return the intersection of two sets as a new set.\n\
1366\n\
1367(i.e. all elements that are in both sets.)");
1368
1369static PyObject *
1370set_intersection_update(PySetObject *so, PyObject *other)
1371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 tmp = set_intersection(so, other);
1375 if (tmp == NULL)
1376 return NULL;
1377 set_swap_bodies(so, (PySetObject *)tmp);
1378 Py_DECREF(tmp);
1379 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001380}
1381
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001382static PyObject *
1383set_intersection_update_multi(PySetObject *so, PyObject *args)
1384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 tmp = set_intersection_multi(so, args);
1388 if (tmp == NULL)
1389 return NULL;
1390 set_swap_bodies(so, (PySetObject *)tmp);
1391 Py_DECREF(tmp);
1392 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001393}
1394
Raymond Hettingera690a992003-11-16 16:17:49 +00001395PyDoc_STRVAR(intersection_update_doc,
1396"Update a set with the intersection of itself and another.");
1397
1398static PyObject *
1399set_and(PySetObject *so, PyObject *other)
1400{
Brian Curtindfc80e32011-08-10 20:28:54 -05001401 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1402 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001404}
1405
1406static PyObject *
1407set_iand(PySetObject *so, PyObject *other)
1408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001410
Brian Curtindfc80e32011-08-10 20:28:54 -05001411 if (!PyAnySet_Check(other))
1412 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 result = set_intersection_update(so, other);
1414 if (result == NULL)
1415 return NULL;
1416 Py_DECREF(result);
1417 Py_INCREF(so);
1418 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001419}
1420
Guido van Rossum58da9312007-11-10 23:39:45 +00001421static PyObject *
1422set_isdisjoint(PySetObject *so, PyObject *other)
1423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if ((PyObject *)so == other) {
1427 if (PySet_GET_SIZE(so) == 0)
1428 Py_RETURN_TRUE;
1429 else
1430 Py_RETURN_FALSE;
1431 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (PyAnySet_CheckExact(other)) {
1434 Py_ssize_t pos = 0;
1435 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1438 tmp = (PyObject *)so;
1439 so = (PySetObject *)other;
1440 other = tmp;
1441 }
1442 while (set_next((PySetObject *)other, &pos, &entry)) {
1443 int rv = set_contains_entry(so, entry);
1444 if (rv == -1)
1445 return NULL;
1446 if (rv)
1447 Py_RETURN_FALSE;
1448 }
1449 Py_RETURN_TRUE;
1450 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 it = PyObject_GetIter(other);
1453 if (it == NULL)
1454 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 while ((key = PyIter_Next(it)) != NULL) {
1457 int rv;
1458 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001459 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 if (hash == -1) {
1462 Py_DECREF(key);
1463 Py_DECREF(it);
1464 return NULL;
1465 }
1466 entry.hash = hash;
1467 entry.key = key;
1468 rv = set_contains_entry(so, &entry);
1469 Py_DECREF(key);
1470 if (rv == -1) {
1471 Py_DECREF(it);
1472 return NULL;
1473 }
1474 if (rv) {
1475 Py_DECREF(it);
1476 Py_RETURN_FALSE;
1477 }
1478 }
1479 Py_DECREF(it);
1480 if (PyErr_Occurred())
1481 return NULL;
1482 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001483}
1484
1485PyDoc_STRVAR(isdisjoint_doc,
1486"Return True if two sets have a null intersection.");
1487
Neal Norwitz6576bd82005-11-13 18:41:28 +00001488static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001489set_difference_update_internal(PySetObject *so, PyObject *other)
1490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 if ((PyObject *)so == other)
1492 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 if (PyAnySet_Check(other)) {
1495 setentry *entry;
1496 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 while (set_next((PySetObject *)other, &pos, &entry))
1499 if (set_discard_entry(so, entry) == -1)
1500 return -1;
1501 } else {
1502 PyObject *key, *it;
1503 it = PyObject_GetIter(other);
1504 if (it == NULL)
1505 return -1;
1506
1507 while ((key = PyIter_Next(it)) != NULL) {
1508 if (set_discard_key(so, key) == -1) {
1509 Py_DECREF(it);
1510 Py_DECREF(key);
1511 return -1;
1512 }
1513 Py_DECREF(key);
1514 }
1515 Py_DECREF(it);
1516 if (PyErr_Occurred())
1517 return -1;
1518 }
1519 /* If more than 1/5 are dummies, then resize them away. */
1520 if ((so->fill - so->used) * 5 < so->mask)
1521 return 0;
1522 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001523}
1524
Raymond Hettingera690a992003-11-16 16:17:49 +00001525static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001526set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1531 PyObject *other = PyTuple_GET_ITEM(args, i);
1532 if (set_difference_update_internal(so, other) == -1)
1533 return NULL;
1534 }
1535 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001536}
1537
1538PyDoc_STRVAR(difference_update_doc,
1539"Remove all elements of another set from this set.");
1540
1541static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001542set_copy_and_difference(PySetObject *so, PyObject *other)
1543{
1544 PyObject *result;
1545
1546 result = set_copy(so);
1547 if (result == NULL)
1548 return NULL;
1549 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1550 return result;
1551 Py_DECREF(result);
1552 return NULL;
1553}
1554
1555static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001556set_difference(PySetObject *so, PyObject *other)
1557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 PyObject *result;
1559 setentry *entry;
1560 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001563 return set_copy_and_difference(so, other);
1564 }
1565
1566 /* If len(so) much more than len(other), it's more efficient to simply copy
1567 * so and then iterate other looking for common elements. */
1568 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1569 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 result = make_new_set_basetype(Py_TYPE(so), NULL);
1573 if (result == NULL)
1574 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 if (PyDict_CheckExact(other)) {
1577 while (set_next(so, &pos, &entry)) {
1578 setentry entrycopy;
1579 entrycopy.hash = entry->hash;
1580 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001581 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1583 Py_DECREF(result);
1584 return NULL;
1585 }
1586 }
1587 }
1588 return result;
1589 }
1590
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001591 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 while (set_next(so, &pos, &entry)) {
1593 int rv = set_contains_entry((PySetObject *)other, entry);
1594 if (rv == -1) {
1595 Py_DECREF(result);
1596 return NULL;
1597 }
1598 if (!rv) {
1599 if (set_add_entry((PySetObject *)result, entry) == -1) {
1600 Py_DECREF(result);
1601 return NULL;
1602 }
1603 }
1604 }
1605 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001606}
1607
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001608static PyObject *
1609set_difference_multi(PySetObject *so, PyObject *args)
1610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 Py_ssize_t i;
1612 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 if (PyTuple_GET_SIZE(args) == 0)
1615 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 other = PyTuple_GET_ITEM(args, 0);
1618 result = set_difference(so, other);
1619 if (result == NULL)
1620 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1623 other = PyTuple_GET_ITEM(args, i);
1624 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1625 Py_DECREF(result);
1626 return NULL;
1627 }
1628 }
1629 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001630}
1631
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001632PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001633"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001634\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001635(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001636static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001637set_sub(PySetObject *so, PyObject *other)
1638{
Brian Curtindfc80e32011-08-10 20:28:54 -05001639 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1640 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001642}
1643
1644static PyObject *
1645set_isub(PySetObject *so, PyObject *other)
1646{
Brian Curtindfc80e32011-08-10 20:28:54 -05001647 if (!PyAnySet_Check(other))
1648 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (set_difference_update_internal(so, other) == -1)
1650 return NULL;
1651 Py_INCREF(so);
1652 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001653}
1654
1655static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001656set_symmetric_difference_update(PySetObject *so, PyObject *other)
1657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 PySetObject *otherset;
1659 PyObject *key;
1660 Py_ssize_t pos = 0;
1661 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 if ((PyObject *)so == other)
1664 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 if (PyDict_CheckExact(other)) {
1667 PyObject *value;
1668 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001669 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1671 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001672
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001673 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 an_entry.hash = hash;
1675 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001678 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001679 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001682 if (rv == DISCARD_NOTFOUND) {
1683 if (set_add_entry(so, &an_entry) == -1) {
1684 Py_DECREF(key);
1685 return NULL;
1686 }
1687 }
Georg Brandl2d444492010-09-03 10:52:55 +00001688 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 }
1690 Py_RETURN_NONE;
1691 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 if (PyAnySet_Check(other)) {
1694 Py_INCREF(other);
1695 otherset = (PySetObject *)other;
1696 } else {
1697 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1698 if (otherset == NULL)
1699 return NULL;
1700 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 while (set_next(otherset, &pos, &entry)) {
1703 int rv = set_discard_entry(so, entry);
1704 if (rv == -1) {
1705 Py_DECREF(otherset);
1706 return NULL;
1707 }
1708 if (rv == DISCARD_NOTFOUND) {
1709 if (set_add_entry(so, entry) == -1) {
1710 Py_DECREF(otherset);
1711 return NULL;
1712 }
1713 }
1714 }
1715 Py_DECREF(otherset);
1716 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001717}
1718
1719PyDoc_STRVAR(symmetric_difference_update_doc,
1720"Update a set with the symmetric difference of itself and another.");
1721
1722static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001723set_symmetric_difference(PySetObject *so, PyObject *other)
1724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 PyObject *rv;
1726 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1729 if (otherset == NULL)
1730 return NULL;
1731 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1732 if (rv == NULL)
1733 return NULL;
1734 Py_DECREF(rv);
1735 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001736}
1737
1738PyDoc_STRVAR(symmetric_difference_doc,
1739"Return the symmetric difference of two sets as a new set.\n\
1740\n\
1741(i.e. all elements that are in exactly one of the sets.)");
1742
1743static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001744set_xor(PySetObject *so, PyObject *other)
1745{
Brian Curtindfc80e32011-08-10 20:28:54 -05001746 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1747 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001749}
1750
1751static PyObject *
1752set_ixor(PySetObject *so, PyObject *other)
1753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001755
Brian Curtindfc80e32011-08-10 20:28:54 -05001756 if (!PyAnySet_Check(other))
1757 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 result = set_symmetric_difference_update(so, other);
1759 if (result == NULL)
1760 return NULL;
1761 Py_DECREF(result);
1762 Py_INCREF(so);
1763 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001764}
1765
1766static PyObject *
1767set_issubset(PySetObject *so, PyObject *other)
1768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 setentry *entry;
1770 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 if (!PyAnySet_Check(other)) {
1773 PyObject *tmp, *result;
1774 tmp = make_new_set(&PySet_Type, other);
1775 if (tmp == NULL)
1776 return NULL;
1777 result = set_issubset(so, tmp);
1778 Py_DECREF(tmp);
1779 return result;
1780 }
1781 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1782 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 while (set_next(so, &pos, &entry)) {
1785 int rv = set_contains_entry((PySetObject *)other, entry);
1786 if (rv == -1)
1787 return NULL;
1788 if (!rv)
1789 Py_RETURN_FALSE;
1790 }
1791 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001792}
1793
1794PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1795
1796static PyObject *
1797set_issuperset(PySetObject *so, PyObject *other)
1798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 if (!PyAnySet_Check(other)) {
1802 tmp = make_new_set(&PySet_Type, other);
1803 if (tmp == NULL)
1804 return NULL;
1805 result = set_issuperset(so, tmp);
1806 Py_DECREF(tmp);
1807 return result;
1808 }
1809 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001810}
1811
1812PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1813
Raymond Hettingera690a992003-11-16 16:17:49 +00001814static PyObject *
1815set_richcompare(PySetObject *v, PyObject *w, int op)
1816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001818
Brian Curtindfc80e32011-08-10 20:28:54 -05001819 if(!PyAnySet_Check(w))
1820 Py_RETURN_NOTIMPLEMENTED;
1821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 switch (op) {
1823 case Py_EQ:
1824 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1825 Py_RETURN_FALSE;
1826 if (v->hash != -1 &&
1827 ((PySetObject *)w)->hash != -1 &&
1828 v->hash != ((PySetObject *)w)->hash)
1829 Py_RETURN_FALSE;
1830 return set_issubset(v, w);
1831 case Py_NE:
1832 r1 = set_richcompare(v, w, Py_EQ);
1833 if (r1 == NULL)
1834 return NULL;
1835 r2 = PyBool_FromLong(PyObject_Not(r1));
1836 Py_DECREF(r1);
1837 return r2;
1838 case Py_LE:
1839 return set_issubset(v, w);
1840 case Py_GE:
1841 return set_issuperset(v, w);
1842 case Py_LT:
1843 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1844 Py_RETURN_FALSE;
1845 return set_issubset(v, w);
1846 case Py_GT:
1847 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1848 Py_RETURN_FALSE;
1849 return set_issuperset(v, w);
1850 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001851 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001852}
1853
Raymond Hettingera690a992003-11-16 16:17:49 +00001854static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001855set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 if (set_add_key(so, key) == -1)
1858 return NULL;
1859 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001860}
1861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001863"Add an element to a set.\n\
1864\n\
1865This has no effect if the element is already present.");
1866
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001867static int
1868set_contains(PySetObject *so, PyObject *key)
1869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 PyObject *tmpkey;
1871 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 rv = set_contains_key(so, key);
1874 if (rv == -1) {
1875 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1876 return -1;
1877 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001878 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 if (tmpkey == NULL)
1880 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001881 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 Py_DECREF(tmpkey);
1883 }
1884 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001885}
1886
1887static PyObject *
1888set_direct_contains(PySetObject *so, PyObject *key)
1889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 result = set_contains(so, key);
1893 if (result == -1)
1894 return NULL;
1895 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001896}
1897
1898PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1899
Raymond Hettingera690a992003-11-16 16:17:49 +00001900static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001901set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 PyObject *tmpkey;
1904 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 rv = set_discard_key(so, key);
1907 if (rv == -1) {
1908 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1909 return NULL;
1910 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001911 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 if (tmpkey == NULL)
1913 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 Py_DECREF(tmpkey);
1916 if (rv == -1)
1917 return NULL;
1918 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001921 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 return NULL;
1923 }
1924 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001925}
1926
1927PyDoc_STRVAR(remove_doc,
1928"Remove an element from a set; it must be a member.\n\
1929\n\
1930If the element is not a member, raise a KeyError.");
1931
1932static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001933set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001934{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001935 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 rv = set_discard_key(so, key);
1939 if (rv == -1) {
1940 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1941 return NULL;
1942 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001943 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 if (tmpkey == NULL)
1945 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001946 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001948 if (rv == -1)
1949 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 }
1951 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001952}
1953
1954PyDoc_STRVAR(discard_doc,
1955"Remove an element from a set if it is a member.\n\
1956\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001958
1959static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001960set_reduce(PySetObject *so)
1961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001963 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 keys = PySequence_List((PyObject *)so);
1966 if (keys == NULL)
1967 goto done;
1968 args = PyTuple_Pack(1, keys);
1969 if (args == NULL)
1970 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001971 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 if (dict == NULL) {
1973 PyErr_Clear();
1974 dict = Py_None;
1975 Py_INCREF(dict);
1976 }
1977 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001978done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 Py_XDECREF(args);
1980 Py_XDECREF(keys);
1981 Py_XDECREF(dict);
1982 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001983}
1984
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001985static PyObject *
1986set_sizeof(PySetObject *so)
1987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 res = sizeof(PySetObject);
1991 if (so->table != so->smalltable)
1992 res = res + (so->mask + 1) * sizeof(setentry);
1993 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001994}
1995
1996PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001997static int
1998set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 if (!PyAnySet_Check(self))
2003 return -1;
2004 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2005 return -1;
2006 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2007 return -1;
2008 set_clear_internal(self);
2009 self->hash = -1;
2010 if (iterable == NULL)
2011 return 0;
2012 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002013}
2014
Raymond Hettingera690a992003-11-16 16:17:49 +00002015static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 set_len, /* sq_length */
2017 0, /* sq_concat */
2018 0, /* sq_repeat */
2019 0, /* sq_item */
2020 0, /* sq_slice */
2021 0, /* sq_ass_item */
2022 0, /* sq_ass_slice */
2023 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002024};
2025
2026/* set object ********************************************************/
2027
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002028#ifdef Py_DEBUG
2029static PyObject *test_c_api(PySetObject *so);
2030
2031PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2032All is well if assertions don't fail.");
2033#endif
2034
Raymond Hettingera690a992003-11-16 16:17:49 +00002035static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 {"add", (PyCFunction)set_add, METH_O,
2037 add_doc},
2038 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2039 clear_doc},
2040 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2041 contains_doc},
2042 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2043 copy_doc},
2044 {"discard", (PyCFunction)set_discard, METH_O,
2045 discard_doc},
2046 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2047 difference_doc},
2048 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2049 difference_update_doc},
2050 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2051 intersection_doc},
2052 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2053 intersection_update_doc},
2054 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2055 isdisjoint_doc},
2056 {"issubset", (PyCFunction)set_issubset, METH_O,
2057 issubset_doc},
2058 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2059 issuperset_doc},
2060 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2061 pop_doc},
2062 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2063 reduce_doc},
2064 {"remove", (PyCFunction)set_remove, METH_O,
2065 remove_doc},
2066 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2067 sizeof_doc},
2068 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2069 symmetric_difference_doc},
2070 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2071 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002072#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2074 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002075#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 {"union", (PyCFunction)set_union, METH_VARARGS,
2077 union_doc},
2078 {"update", (PyCFunction)set_update, METH_VARARGS,
2079 update_doc},
2080 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002081};
2082
2083static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 0, /*nb_add*/
2085 (binaryfunc)set_sub, /*nb_subtract*/
2086 0, /*nb_multiply*/
2087 0, /*nb_remainder*/
2088 0, /*nb_divmod*/
2089 0, /*nb_power*/
2090 0, /*nb_negative*/
2091 0, /*nb_positive*/
2092 0, /*nb_absolute*/
2093 0, /*nb_bool*/
2094 0, /*nb_invert*/
2095 0, /*nb_lshift*/
2096 0, /*nb_rshift*/
2097 (binaryfunc)set_and, /*nb_and*/
2098 (binaryfunc)set_xor, /*nb_xor*/
2099 (binaryfunc)set_or, /*nb_or*/
2100 0, /*nb_int*/
2101 0, /*nb_reserved*/
2102 0, /*nb_float*/
2103 0, /*nb_inplace_add*/
2104 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2105 0, /*nb_inplace_multiply*/
2106 0, /*nb_inplace_remainder*/
2107 0, /*nb_inplace_power*/
2108 0, /*nb_inplace_lshift*/
2109 0, /*nb_inplace_rshift*/
2110 (binaryfunc)set_iand, /*nb_inplace_and*/
2111 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2112 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002113};
2114
2115PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002116"set() -> new empty set object\n\
2117set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002118\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002119Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002120
2121PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2123 "set", /* tp_name */
2124 sizeof(PySetObject), /* tp_basicsize */
2125 0, /* tp_itemsize */
2126 /* methods */
2127 (destructor)set_dealloc, /* tp_dealloc */
2128 0, /* tp_print */
2129 0, /* tp_getattr */
2130 0, /* tp_setattr */
2131 0, /* tp_reserved */
2132 (reprfunc)set_repr, /* tp_repr */
2133 &set_as_number, /* tp_as_number */
2134 &set_as_sequence, /* tp_as_sequence */
2135 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002136 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 0, /* tp_call */
2138 0, /* tp_str */
2139 PyObject_GenericGetAttr, /* tp_getattro */
2140 0, /* tp_setattro */
2141 0, /* tp_as_buffer */
2142 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2143 Py_TPFLAGS_BASETYPE, /* tp_flags */
2144 set_doc, /* tp_doc */
2145 (traverseproc)set_traverse, /* tp_traverse */
2146 (inquiry)set_clear_internal, /* tp_clear */
2147 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002148 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2149 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 0, /* tp_iternext */
2151 set_methods, /* tp_methods */
2152 0, /* tp_members */
2153 0, /* tp_getset */
2154 0, /* tp_base */
2155 0, /* tp_dict */
2156 0, /* tp_descr_get */
2157 0, /* tp_descr_set */
2158 0, /* tp_dictoffset */
2159 (initproc)set_init, /* tp_init */
2160 PyType_GenericAlloc, /* tp_alloc */
2161 set_new, /* tp_new */
2162 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002163};
2164
2165/* frozenset object ********************************************************/
2166
2167
2168static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2170 contains_doc},
2171 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2172 copy_doc},
2173 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2174 difference_doc},
2175 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2176 intersection_doc},
2177 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2178 isdisjoint_doc},
2179 {"issubset", (PyCFunction)set_issubset, METH_O,
2180 issubset_doc},
2181 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2182 issuperset_doc},
2183 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2184 reduce_doc},
2185 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2186 sizeof_doc},
2187 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2188 symmetric_difference_doc},
2189 {"union", (PyCFunction)set_union, METH_VARARGS,
2190 union_doc},
2191 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002192};
2193
2194static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 0, /*nb_add*/
2196 (binaryfunc)set_sub, /*nb_subtract*/
2197 0, /*nb_multiply*/
2198 0, /*nb_remainder*/
2199 0, /*nb_divmod*/
2200 0, /*nb_power*/
2201 0, /*nb_negative*/
2202 0, /*nb_positive*/
2203 0, /*nb_absolute*/
2204 0, /*nb_bool*/
2205 0, /*nb_invert*/
2206 0, /*nb_lshift*/
2207 0, /*nb_rshift*/
2208 (binaryfunc)set_and, /*nb_and*/
2209 (binaryfunc)set_xor, /*nb_xor*/
2210 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002211};
2212
2213PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002214"frozenset() -> empty frozenset object\n\
2215frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002216\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002217Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002218
2219PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2221 "frozenset", /* tp_name */
2222 sizeof(PySetObject), /* tp_basicsize */
2223 0, /* tp_itemsize */
2224 /* methods */
2225 (destructor)set_dealloc, /* tp_dealloc */
2226 0, /* tp_print */
2227 0, /* tp_getattr */
2228 0, /* tp_setattr */
2229 0, /* tp_reserved */
2230 (reprfunc)set_repr, /* tp_repr */
2231 &frozenset_as_number, /* tp_as_number */
2232 &set_as_sequence, /* tp_as_sequence */
2233 0, /* tp_as_mapping */
2234 frozenset_hash, /* tp_hash */
2235 0, /* tp_call */
2236 0, /* tp_str */
2237 PyObject_GenericGetAttr, /* tp_getattro */
2238 0, /* tp_setattro */
2239 0, /* tp_as_buffer */
2240 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2241 Py_TPFLAGS_BASETYPE, /* tp_flags */
2242 frozenset_doc, /* tp_doc */
2243 (traverseproc)set_traverse, /* tp_traverse */
2244 (inquiry)set_clear_internal, /* tp_clear */
2245 (richcmpfunc)set_richcompare, /* tp_richcompare */
2246 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2247 (getiterfunc)set_iter, /* tp_iter */
2248 0, /* tp_iternext */
2249 frozenset_methods, /* tp_methods */
2250 0, /* tp_members */
2251 0, /* tp_getset */
2252 0, /* tp_base */
2253 0, /* tp_dict */
2254 0, /* tp_descr_get */
2255 0, /* tp_descr_set */
2256 0, /* tp_dictoffset */
2257 0, /* tp_init */
2258 PyType_GenericAlloc, /* tp_alloc */
2259 frozenset_new, /* tp_new */
2260 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002261};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002262
2263
2264/***** C API functions *************************************************/
2265
2266PyObject *
2267PySet_New(PyObject *iterable)
2268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002270}
2271
2272PyObject *
2273PyFrozenSet_New(PyObject *iterable)
2274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002276}
2277
Neal Norwitz8c49c822006-03-04 18:41:19 +00002278Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002279PySet_Size(PyObject *anyset)
2280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 if (!PyAnySet_Check(anyset)) {
2282 PyErr_BadInternalCall();
2283 return -1;
2284 }
2285 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002286}
2287
2288int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002289PySet_Clear(PyObject *set)
2290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 if (!PySet_Check(set)) {
2292 PyErr_BadInternalCall();
2293 return -1;
2294 }
2295 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002296}
2297
2298int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002299PySet_Contains(PyObject *anyset, PyObject *key)
2300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (!PyAnySet_Check(anyset)) {
2302 PyErr_BadInternalCall();
2303 return -1;
2304 }
2305 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002306}
2307
2308int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002309PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 if (!PySet_Check(set)) {
2312 PyErr_BadInternalCall();
2313 return -1;
2314 }
2315 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002316}
2317
2318int
Christian Heimesfd66e512008-01-29 12:18:50 +00002319PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 if (!PySet_Check(anyset) &&
2322 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2323 PyErr_BadInternalCall();
2324 return -1;
2325 }
2326 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002327}
2328
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002329int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002330_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 if (!PyAnySet_Check(set)) {
2335 PyErr_BadInternalCall();
2336 return -1;
2337 }
2338 if (set_next((PySetObject *)set, pos, &entry) == 0)
2339 return 0;
2340 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002341 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002343}
2344
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002345PyObject *
2346PySet_Pop(PyObject *set)
2347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 if (!PySet_Check(set)) {
2349 PyErr_BadInternalCall();
2350 return NULL;
2351 }
2352 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002353}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002354
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002355int
2356_PySet_Update(PyObject *set, PyObject *iterable)
2357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 if (!PySet_Check(set)) {
2359 PyErr_BadInternalCall();
2360 return -1;
2361 }
2362 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002363}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002364
Raymond Hettingere259f132013-12-15 11:56:14 -08002365/* Exported for the gdb plugin's benefit. */
2366PyObject *_PySet_Dummy = dummy;
2367
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002368#ifdef Py_DEBUG
2369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002371 Returns True and original set is restored. */
2372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373#define assertRaises(call_return_value, exception) \
2374 do { \
2375 assert(call_return_value); \
2376 assert(PyErr_ExceptionMatches(exception)); \
2377 PyErr_Clear(); \
2378 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002379
2380static PyObject *
2381test_c_api(PySetObject *so)
2382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 Py_ssize_t count;
2384 char *s;
2385 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002386 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002388 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* Verify preconditions */
2392 assert(PyAnySet_Check(ob));
2393 assert(PyAnySet_CheckExact(ob));
2394 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 /* so.clear(); so |= set("abc"); */
2397 str = PyUnicode_FromString("abc");
2398 if (str == NULL)
2399 return NULL;
2400 set_clear_internal(so);
2401 if (set_update_internal(so, str) == -1) {
2402 Py_DECREF(str);
2403 return NULL;
2404 }
2405 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 /* Exercise type/size checks */
2408 assert(PySet_Size(ob) == 3);
2409 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 /* Raise TypeError for non-iterable constructor arguments */
2412 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2413 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 /* Raise TypeError for unhashable key */
2416 dup = PySet_New(ob);
2417 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2418 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2419 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 /* Exercise successful pop, contains, add, and discard */
2422 elem = PySet_Pop(ob);
2423 assert(PySet_Contains(ob, elem) == 0);
2424 assert(PySet_GET_SIZE(ob) == 2);
2425 assert(PySet_Add(ob, elem) == 0);
2426 assert(PySet_Contains(ob, elem) == 1);
2427 assert(PySet_GET_SIZE(ob) == 3);
2428 assert(PySet_Discard(ob, elem) == 1);
2429 assert(PySet_GET_SIZE(ob) == 2);
2430 assert(PySet_Discard(ob, elem) == 0);
2431 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 /* Exercise clear */
2434 dup2 = PySet_New(dup);
2435 assert(PySet_Clear(dup2) == 0);
2436 assert(PySet_Size(dup2) == 0);
2437 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* Raise SystemError on clear or update of frozen set */
2440 f = PyFrozenSet_New(dup);
2441 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2442 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2443 assert(PySet_Add(f, elem) == 0);
2444 Py_INCREF(f);
2445 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2446 Py_DECREF(f);
2447 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 /* Exercise direct iteration */
2450 i = 0, count = 0;
2451 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2452 s = _PyUnicode_AsString(x);
2453 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2454 count++;
2455 }
2456 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 /* Exercise updates */
2459 dup2 = PySet_New(NULL);
2460 assert(_PySet_Update(dup2, dup) == 0);
2461 assert(PySet_Size(dup2) == 3);
2462 assert(_PySet_Update(dup2, dup) == 0);
2463 assert(PySet_Size(dup2) == 3);
2464 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 /* Raise SystemError when self argument is not a set or frozenset. */
2467 t = PyTuple_New(0);
2468 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2469 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2470 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 /* Raise SystemError when self argument is not a set. */
2473 f = PyFrozenSet_New(dup);
2474 assert(PySet_Size(f) == 3);
2475 assert(PyFrozenSet_CheckExact(f));
2476 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2477 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2478 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 /* Raise KeyError when popping from an empty set */
2481 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2482 Py_DECREF(ob);
2483 assert(PySet_GET_SIZE(ob) == 0);
2484 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* Restore the set from the copy using the PyNumber API */
2487 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2488 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 /* Verify constructors accept NULL arguments */
2491 f = PySet_New(NULL);
2492 assert(f != NULL);
2493 assert(PySet_GET_SIZE(f) == 0);
2494 Py_DECREF(f);
2495 f = PyFrozenSet_New(NULL);
2496 assert(f != NULL);
2497 assert(PyFrozenSet_CheckExact(f));
2498 assert(PySet_GET_SIZE(f) == 0);
2499 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 Py_DECREF(elem);
2502 Py_DECREF(dup);
2503 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002504}
2505
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002506#undef assertRaises
2507
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002508#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002509
2510/***** Dummy Struct *************************************************/
2511
2512static PyObject *
2513dummy_repr(PyObject *op)
2514{
2515 return PyUnicode_FromString("<dummy key>");
2516}
2517
2518static void
2519dummy_dealloc(PyObject* ignore)
2520{
2521 Py_FatalError("deallocating <dummy key>");
2522}
2523
2524static PyTypeObject _PySetDummy_Type = {
2525 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2526 "<dummy key> type",
2527 0,
2528 0,
2529 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2530 0, /*tp_print*/
2531 0, /*tp_getattr*/
2532 0, /*tp_setattr*/
2533 0, /*tp_reserved*/
2534 dummy_repr, /*tp_repr*/
2535 0, /*tp_as_number*/
2536 0, /*tp_as_sequence*/
2537 0, /*tp_as_mapping*/
2538 0, /*tp_hash */
2539 0, /*tp_call */
2540 0, /*tp_str */
2541 0, /*tp_getattro */
2542 0, /*tp_setattro */
2543 0, /*tp_as_buffer */
2544 Py_TPFLAGS_DEFAULT, /*tp_flags */
2545};
2546
2547static PyObject _dummy_struct = {
2548 _PyObject_EXTRA_INIT
2549 2, &_PySetDummy_Type
2550};
2551