blob: 4fba897b1f765fb929080eecd286c438595d54b3 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettinger426d9952014-05-18 21:40:20 +01007 Copyright (c) 2003-2014 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
26 Unlike the dictionary implementation, the lookkey functions can return
27 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070055 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t perturb = hash;
58 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070059 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070060 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070061 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062
Raymond Hettingera35adf52013-09-02 03:23:21 -070063 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070064 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070066
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070067 while (1) {
Raymond Hettinger426d9952014-05-18 21:40:20 +010068 if (entry->hash == hash && entry->key != dummy) { /* dummy match unlikely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070069 PyObject *startkey = entry->key;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080070 if (startkey == key)
71 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 Py_INCREF(startkey);
73 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
74 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010075 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010077 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010079 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070080 return entry;
81 }
82 if (entry->key == dummy && freeslot == NULL)
83 freeslot = entry;
84
Raymond Hettingerc70a2b72013-09-21 14:02:55 -070085 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
86 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -070087 if (entry->key == NULL)
88 goto found_null;
Raymond Hettingera35adf52013-09-02 03:23:21 -070089 if (entry->hash == hash && entry->key != dummy) {
90 PyObject *startkey = entry->key;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080091 if (startkey == key)
92 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070093 Py_INCREF(startkey);
94 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
95 Py_DECREF(startkey);
96 if (cmp < 0)
97 return NULL;
98 if (table != so->table || entry->key != startkey)
99 return set_lookkey(so, key, hash);
100 if (cmp > 0)
101 return entry;
102 }
103 if (entry->key == dummy && freeslot == NULL)
104 freeslot = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700106
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700107 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700108 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700109
Raymond Hettingera35adf52013-09-02 03:23:21 -0700110 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700111 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700112 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700114 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700115 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000116}
117
118/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000119 * Hacked up version of set_lookkey which can assume keys are always unicode;
120 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000121 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 */
123static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200124set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700127 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200128 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700129 size_t perturb = hash;
130 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700131 size_t i = (size_t)hash;
132 size_t j;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 /* Make sure this function doesn't have to handle non-unicode keys,
135 including subclasses of str; e.g., one reason to subclass
136 strings is to override __eq__, and for speed we don't cater to
137 that here. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100138 if (!PyUnicode_CheckExact(key)) { /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 so->lookup = set_lookkey;
140 return set_lookkey(so, key, hash);
141 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700142
Raymond Hettingera35adf52013-09-02 03:23:21 -0700143 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700144 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000146
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700147 while (1) {
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800148 if (entry->hash == hash
149 && (entry->key == key
150 || (entry->key != dummy /* unlikely */
151 && unicode_eq(entry->key, key)))) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -0700152 return entry;
153 if (entry->key == dummy && freeslot == NULL)
154 freeslot = entry;
155
Raymond Hettingerc70a2b72013-09-21 14:02:55 -0700156 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
157 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -0700158 if (entry->key == NULL)
159 goto found_null;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800160 if (entry->hash == hash
161 && (entry->key == key
162 || (entry->key != dummy /* unlikely */
163 && unicode_eq(entry->key, key)))) /* likely */
Raymond Hettingera35adf52013-09-02 03:23:21 -0700164 return entry;
165 if (entry->key == dummy && freeslot == NULL)
166 freeslot = entry;
167 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700168
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700169 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700170 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700171
Raymond Hettingera35adf52013-09-02 03:23:21 -0700172 entry = &table[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700174 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700176 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700177 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000178}
179
180/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000181Internal routine used by set_table_resize() to insert an item which is
182known to be absent from the set. This routine also assumes that
183the set contains no deleted entries. Besides the performance benefit,
184using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
185Note that no refcounts are changed by this routine; if needed, the caller
186is responsible for incref'ing `key`.
187*/
188static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200189set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200192 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700193 size_t perturb = hash;
194 size_t mask = (size_t)so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700195 size_t i = (size_t)hash;
196 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000197
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700198 while (1) {
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800199 for (j = 0 ; j <= LINEAR_PROBES ; j++) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700200 entry = &table[(i + j) & mask];
201 if (entry->key == NULL)
202 goto found_null;
203 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700204 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700205 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700207 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 entry->key = key;
209 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700210 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000212}
213
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700214/* ======== End logic for probing the hash table ========================== */
215/* ======================================================================== */
216
217
218/*
219Internal routine to insert a new key into the table.
220Used by the public insert routine.
221Eats a reference to key.
222*/
223static int
224set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
225{
226 setentry *entry;
227
228 assert(so->lookup != NULL);
229 entry = so->lookup(so, key, hash);
230 if (entry == NULL)
231 return -1;
232 if (entry->key == NULL) {
233 /* UNUSED */
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700234 entry->key = key;
235 entry->hash = hash;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800236 so->fill++;
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700237 so->used++;
238 } else if (entry->key == dummy) {
239 /* DUMMY */
240 entry->key = key;
241 entry->hash = hash;
242 so->used++;
243 } else {
244 /* ACTIVE */
245 Py_DECREF(key);
246 }
247 return 0;
248}
249
Thomas Wouters89f507f2006-12-13 04:49:30 +0000250/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000251Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000252keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000253actually be smaller than the old one.
254*/
255static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 Py_ssize_t newsize;
259 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800260 Py_ssize_t oldfill = so->fill;
261 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 int is_oldtable_malloced;
263 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100268 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 for (newsize = PySet_MINSIZE;
270 newsize <= minused && newsize > 0;
271 newsize <<= 1)
272 ;
273 if (newsize <= 0) {
274 PyErr_NoMemory();
275 return -1;
276 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 /* Get space for a new table. */
279 oldtable = so->table;
280 assert(oldtable != NULL);
281 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 if (newsize == PySet_MINSIZE) {
284 /* A large table is shrinking, or we can't get any smaller. */
285 newtable = so->smalltable;
286 if (newtable == oldtable) {
287 if (so->fill == so->used) {
288 /* No dummies, so no point doing anything. */
289 return 0;
290 }
291 /* We're not going to resize it, but rebuild the
292 table anyway to purge old dummy entries.
293 Subtle: This is *necessary* if fill==size,
294 as set_lookkey needs at least one virgin slot to
295 terminate failing searches. If fill < size, it's
296 merely desirable, as dummies slow searches. */
297 assert(so->fill > so->used);
298 memcpy(small_copy, oldtable, sizeof(small_copy));
299 oldtable = small_copy;
300 }
301 }
302 else {
303 newtable = PyMem_NEW(setentry, newsize);
304 if (newtable == NULL) {
305 PyErr_NoMemory();
306 return -1;
307 }
308 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Make the set empty, using the new table. */
311 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800314 so->used = 0;
315 so->mask = newsize - 1;
316 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 /* Copy the data over; this is refcount-neutral for active entries;
319 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800320 if (oldfill == oldused) {
321 for (entry = oldtable; oldused > 0; entry++) {
322 if (entry->key != NULL) {
323 oldused--;
324 set_insert_clean(so, entry->key, entry->hash);
325 }
326 }
327 } else {
328 for (entry = oldtable; oldused > 0; entry++) {
329 if (entry->key != NULL && entry->key != dummy) {
330 oldused--;
331 set_insert_clean(so, entry->key, entry->hash);
332 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 }
334 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 if (is_oldtable_malloced)
337 PyMem_DEL(oldtable);
338 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000339}
340
Raymond Hettingerc991db22005-08-11 07:58:45 +0000341/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
342
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000343static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200344set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000345{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200346 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000347 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100348 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 assert(so->fill <= so->mask); /* at least one empty slot */
351 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000352 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100353 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000354 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 return -1;
356 }
357 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
358 return 0;
359 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000360}
361
362static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200363set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000364{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800365 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200366 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200369 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 hash = PyObject_Hash(key);
371 if (hash == -1)
372 return -1;
373 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800374 entry.key = key;
375 entry.hash = hash;
376 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377}
378
379#define DISCARD_NOTFOUND 0
380#define DISCARD_FOUND 1
381
382static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000383set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800384{
385 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000387
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000388 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (entry == NULL)
390 return -1;
391 if (entry->key == NULL || entry->key == dummy)
392 return DISCARD_NOTFOUND;
393 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 entry->key = dummy;
395 so->used--;
396 Py_DECREF(old_key);
397 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000398}
399
400static int
401set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800403 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200404 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200409 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 hash = PyObject_Hash(key);
411 if (hash == -1)
412 return -1;
413 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800414 entry.key = key;
415 entry.hash = hash;
416 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000417}
418
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700419static void
420set_empty_to_minsize(PySetObject *so)
421{
422 memset(so->smalltable, 0, sizeof(so->smalltable));
423 so->fill = 0;
424 so->used = 0;
425 so->mask = PySet_MINSIZE - 1;
426 so->table = so->smalltable;
427 so->hash = -1;
428}
429
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000430static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431set_clear_internal(PySetObject *so)
432{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800433 setentry *entry;
434 setentry *table = so->table;
435 Py_ssize_t fill = so->fill;
436 Py_ssize_t used = so->used;
437 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000439
Raymond Hettinger583cd032013-09-07 22:06:35 -0700440 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 /* This is delicate. During the process of clearing the set,
444 * decrefs can cause the set to mutate. To avoid fatal confusion
445 * (voice of experience), we have to make the set empty before
446 * clearing the slots, and never refer to anything via so->ref while
447 * clearing.
448 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700450 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 else if (fill > 0) {
453 /* It's a small table with something that needs to be cleared.
454 * Afraid the only safe way is to copy the set entries into
455 * another small table first.
456 */
457 memcpy(small_copy, table, sizeof(small_copy));
458 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700459 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 }
461 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 /* Now we can finally clear things. If C had refcounts, we could
464 * assert that the refcount on table is 1 now, i.e. that this function
465 * has unique access to it, so decref side-effects can't alter it.
466 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800467 for (entry = table; used > 0; entry++) {
468 if (entry->key && entry->key != dummy) {
469 used--;
470 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (table_is_malloced)
475 PyMem_DEL(table);
476 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000477}
478
479/*
480 * Iterate over a set table. Use like so:
481 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000482 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000483 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000484 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000485 * while (set_next(yourset, &pos, &entry)) {
486 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487 * }
488 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000489 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491 */
492static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000493set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 Py_ssize_t i;
496 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200497 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 assert (PyAnySet_Check(so));
500 i = *pos_ptr;
501 assert(i >= 0);
502 table = so->table;
503 mask = so->mask;
504 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
505 i++;
506 *pos_ptr = i+1;
507 if (i > mask)
508 return 0;
509 assert(table[i].key != NULL);
510 *entry_ptr = &table[i];
511 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512}
513
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000514static void
515set_dealloc(PySetObject *so)
516{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200517 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800518 Py_ssize_t used = so->used;
519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 PyObject_GC_UnTrack(so);
521 Py_TRASHCAN_SAFE_BEGIN(so)
522 if (so->weakreflist != NULL)
523 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000524
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800525 for (entry = so->table; used > 0; entry++) {
526 if (entry->key && entry->key != dummy) {
527 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700528 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 }
530 }
531 if (so->table != so->smalltable)
532 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700533 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000535}
536
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000537static PyObject *
538set_repr(PySetObject *so)
539{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200540 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 if (status != 0) {
544 if (status < 0)
545 return NULL;
546 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
547 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 /* shortcut for the empty set */
550 if (!so->used) {
551 Py_ReprLeave((PyObject*)so);
552 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
553 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 keys = PySequence_List((PyObject *)so);
556 if (keys == NULL)
557 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000558
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200559 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 listrepr = PyObject_Repr(keys);
561 Py_DECREF(keys);
562 if (listrepr == NULL)
563 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200564 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200566 if (tmp == NULL)
567 goto done;
568 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200569
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200570 if (Py_TYPE(so) != &PySet_Type)
571 result = PyUnicode_FromFormat("%s({%U})",
572 Py_TYPE(so)->tp_name,
573 listrepr);
574 else
575 result = PyUnicode_FromFormat("{%U}", listrepr);
576 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000577done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 Py_ReprLeave((PyObject*)so);
579 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000580}
581
Martin v. Löwis18e16552006-02-15 17:27:45 +0000582static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000583set_len(PyObject *so)
584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000586}
587
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000588static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000589set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000592 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100593 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200594 Py_ssize_t i;
595 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 assert (PyAnySet_Check(so));
598 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 other = (PySetObject*)otherset;
601 if (other == so || other->used == 0)
602 /* a.update(a) or a.update({}); nothing to do */
603 return 0;
604 /* Do one big resize at the start, rather than
605 * incrementally resizing as we insert new keys. Expect
606 * that there will be no (or few) overlapping keys.
607 */
608 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
609 if (set_table_resize(so, (so->used + other->used)*2) != 0)
610 return -1;
611 }
612 for (i = 0; i <= other->mask; i++) {
613 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000614 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100615 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000616 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500617 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000618 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100619 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000620 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 return -1;
622 }
623 }
624 }
625 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000626}
627
628static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000629set_contains_entry(PySetObject *so, setentry *entry)
630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 PyObject *key;
632 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000633
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000634 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (lu_entry == NULL)
636 return -1;
637 key = lu_entry->key;
638 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000639}
640
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800641static int
642set_contains_key(PySetObject *so, PyObject *key)
643{
644 setentry entry;
645 Py_hash_t hash;
646
647 if (!PyUnicode_CheckExact(key) ||
648 (hash = ((PyASCIIObject *) key)->hash) == -1) {
649 hash = PyObject_Hash(key);
650 if (hash == -1)
651 return -1;
652 }
653 entry.key = key;
654 entry.hash = hash;
655 return set_contains_entry(so, &entry);
656}
657
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000658static PyObject *
659set_pop(PySetObject *so)
660{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200661 Py_ssize_t i = 0;
662 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 assert (PyAnySet_Check(so));
666 if (so->used == 0) {
667 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
668 return NULL;
669 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 /* Set entry to "the first" unused or dummy set entry. We abuse
672 * the hash field of slot 0 to hold a search finger:
673 * If slot 0 has a value, use slot 0.
674 * Else slot 0 is being used to hold a search finger,
675 * and we use its hash value as the first index to look.
676 */
677 entry = &so->table[0];
678 if (entry->key == NULL || entry->key == dummy) {
679 i = entry->hash;
680 /* The hash field may be a real hash value, or it may be a
681 * legit search finger, or it may be a once-legit search
682 * finger that's out of bounds now because it wrapped around
683 * or the table shrunk -- simply make sure it's in bounds now.
684 */
685 if (i > so->mask || i < 1)
686 i = 1; /* skip slot 0 */
687 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
688 i++;
689 if (i > so->mask)
690 i = 1;
691 }
692 }
693 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 entry->key = dummy;
695 so->used--;
696 so->table[0].hash = i + 1; /* next place to start */
697 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000698}
699
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000700PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
701Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000702
703static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000704set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 Py_ssize_t pos = 0;
707 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 while (set_next(so, &pos, &entry))
710 Py_VISIT(entry->key);
711 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000712}
713
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000714static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000715frozenset_hash(PyObject *self)
716{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800717 /* Most of the constants in this hash algorithm are randomly choosen
718 large primes with "interesting bit patterns" and that passed
719 tests for good collision statistics on a variety of problematic
720 datasets such as:
721
722 ps = []
723 for r in range(21):
724 ps += itertools.combinations(range(20), r)
725 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
726
727 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800729 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 setentry *entry;
731 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 if (so->hash != -1)
734 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000735
Mark Dickinson57e683e2011-09-24 18:18:40 +0100736 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 while (set_next(so, &pos, &entry)) {
738 /* Work to increase the bit dispersion for closely spaced hash
739 values. The is important because some use cases have many
740 combinations of a small number of elements with nearby
741 hashes so that many distinct combinations collapse to only
742 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000743 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800744 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800746 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500747 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800748 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200749 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800750 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 so->hash = hash;
752 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000753}
754
Raymond Hettingera9d99362005-08-05 00:01:15 +0000755/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000756
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000757typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 PyObject_HEAD
759 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
760 Py_ssize_t si_used;
761 Py_ssize_t si_pos;
762 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000763} setiterobject;
764
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000765static void
766setiter_dealloc(setiterobject *si)
767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 Py_XDECREF(si->si_set);
769 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000770}
771
772static int
773setiter_traverse(setiterobject *si, visitproc visit, void *arg)
774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 Py_VISIT(si->si_set);
776 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777}
778
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000779static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000780setiter_len(setiterobject *si)
781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 Py_ssize_t len = 0;
783 if (si->si_set != NULL && si->si_used == si->si_set->used)
784 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000785 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000786}
787
Armin Rigof5b3e362006-02-11 21:32:43 +0000788PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000789
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000790static PyObject *setiter_iternext(setiterobject *si);
791
792static PyObject *
793setiter_reduce(setiterobject *si)
794{
795 PyObject *list;
796 setiterobject tmp;
797
798 list = PyList_New(0);
799 if (!list)
800 return NULL;
801
Ezio Melotti0e1af282012-09-28 16:43:40 +0300802 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000803 tmp = *si;
804 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300805
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000806 /* iterate the temporary into a list */
807 for(;;) {
808 PyObject *element = setiter_iternext(&tmp);
809 if (element) {
810 if (PyList_Append(list, element)) {
811 Py_DECREF(element);
812 Py_DECREF(list);
813 Py_XDECREF(tmp.si_set);
814 return NULL;
815 }
816 Py_DECREF(element);
817 } else
818 break;
819 }
820 Py_XDECREF(tmp.si_set);
821 /* check for error */
822 if (tmp.si_set != NULL) {
823 /* we have an error */
824 Py_DECREF(list);
825 return NULL;
826 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200827 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000828}
829
830PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
831
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000832static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000834 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000836};
837
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000838static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200841 Py_ssize_t i, mask;
842 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 if (so == NULL)
846 return NULL;
847 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 if (si->si_used != so->used) {
850 PyErr_SetString(PyExc_RuntimeError,
851 "Set changed size during iteration");
852 si->si_used = -1; /* Make this state sticky */
853 return NULL;
854 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 i = si->si_pos;
857 assert(i>=0);
858 entry = so->table;
859 mask = so->mask;
860 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
861 i++;
862 si->si_pos = i+1;
863 if (i > mask)
864 goto fail;
865 si->len--;
866 key = entry[i].key;
867 Py_INCREF(key);
868 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000869
870fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 Py_DECREF(so);
872 si->si_set = NULL;
873 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000874}
875
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000876PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 PyVarObject_HEAD_INIT(&PyType_Type, 0)
878 "set_iterator", /* tp_name */
879 sizeof(setiterobject), /* tp_basicsize */
880 0, /* tp_itemsize */
881 /* methods */
882 (destructor)setiter_dealloc, /* tp_dealloc */
883 0, /* tp_print */
884 0, /* tp_getattr */
885 0, /* tp_setattr */
886 0, /* tp_reserved */
887 0, /* tp_repr */
888 0, /* tp_as_number */
889 0, /* tp_as_sequence */
890 0, /* tp_as_mapping */
891 0, /* tp_hash */
892 0, /* tp_call */
893 0, /* tp_str */
894 PyObject_GenericGetAttr, /* tp_getattro */
895 0, /* tp_setattro */
896 0, /* tp_as_buffer */
897 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
898 0, /* tp_doc */
899 (traverseproc)setiter_traverse, /* tp_traverse */
900 0, /* tp_clear */
901 0, /* tp_richcompare */
902 0, /* tp_weaklistoffset */
903 PyObject_SelfIter, /* tp_iter */
904 (iternextfunc)setiter_iternext, /* tp_iternext */
905 setiter_methods, /* tp_methods */
906 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000907};
908
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000909static PyObject *
910set_iter(PySetObject *so)
911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
913 if (si == NULL)
914 return NULL;
915 Py_INCREF(so);
916 si->si_set = so;
917 si->si_used = so->used;
918 si->si_pos = 0;
919 si->len = so->used;
920 _PyObject_GC_TRACK(si);
921 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000922}
923
Raymond Hettingerd7946662005-08-01 21:39:29 +0000924static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000925set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 if (PyAnySet_Check(other))
930 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 if (PyDict_CheckExact(other)) {
933 PyObject *value;
934 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000935 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 /* Do one big resize at the start, rather than
939 * incrementally resizing as we insert new keys. Expect
940 * that there will be no (or few) overlapping keys.
941 */
942 if (dictsize == -1)
943 return -1;
944 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
945 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
946 return -1;
947 }
948 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
949 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 an_entry.hash = hash;
952 an_entry.key = key;
953 if (set_add_entry(so, &an_entry) == -1)
954 return -1;
955 }
956 return 0;
957 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 it = PyObject_GetIter(other);
960 if (it == NULL)
961 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 while ((key = PyIter_Next(it)) != NULL) {
964 if (set_add_key(so, key) == -1) {
965 Py_DECREF(it);
966 Py_DECREF(key);
967 return -1;
968 }
969 Py_DECREF(key);
970 }
971 Py_DECREF(it);
972 if (PyErr_Occurred())
973 return -1;
974 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000975}
976
977static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000978set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
983 PyObject *other = PyTuple_GET_ITEM(args, i);
984 if (set_update_internal(so, other) == -1)
985 return NULL;
986 }
987 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000988}
989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000991"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000992
Raymond Hettinger426d9952014-05-18 21:40:20 +0100993/* XXX Todo:
994 If aligned memory allocations become available, make the
995 set object 64 byte aligned so that most of the fields
996 can be retrieved or updated in a single cache line.
997*/
998
Raymond Hettingera38123e2003-11-24 22:18:49 +0000999static PyObject *
1000make_new_set(PyTypeObject *type, PyObject *iterable)
1001{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001002 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001005 so = (PySetObject *)type->tp_alloc(type, 0);
1006 if (so == NULL)
1007 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001008
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001009 so->fill = 0;
1010 so->used = 0;
1011 so->mask = PySet_MINSIZE - 1;
1012 so->table = so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 so->lookup = set_lookkey_unicode;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001014 so->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 if (iterable != NULL) {
1018 if (set_update_internal(so, iterable) == -1) {
1019 Py_DECREF(so);
1020 return NULL;
1021 }
1022 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001025}
1026
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001027static PyObject *
1028make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1031 if (PyType_IsSubtype(type, &PySet_Type))
1032 type = &PySet_Type;
1033 else
1034 type = &PyFrozenSet_Type;
1035 }
1036 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001037}
1038
Raymond Hettingerd7946662005-08-01 21:39:29 +00001039/* The empty frozenset is a singleton */
1040static PyObject *emptyfrozenset = NULL;
1041
Raymond Hettingera690a992003-11-16 16:17:49 +00001042static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001043frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1048 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1051 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (type != &PyFrozenSet_Type)
1054 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 if (iterable != NULL) {
1057 /* frozenset(f) is idempotent */
1058 if (PyFrozenSet_CheckExact(iterable)) {
1059 Py_INCREF(iterable);
1060 return iterable;
1061 }
1062 result = make_new_set(type, iterable);
1063 if (result == NULL || PySet_GET_SIZE(result))
1064 return result;
1065 Py_DECREF(result);
1066 }
1067 /* The empty frozenset is a singleton */
1068 if (emptyfrozenset == NULL)
1069 emptyfrozenset = make_new_set(type, NULL);
1070 Py_XINCREF(emptyfrozenset);
1071 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001072}
1073
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001074int
1075PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001076{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001077 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001078}
1079
1080void
1081PySet_Fini(void)
1082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001084}
1085
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001086static PyObject *
1087set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1090 return NULL;
1091
1092 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001093}
1094
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001095/* set_swap_bodies() switches the contents of any two sets by moving their
1096 internal data pointers and, if needed, copying the internal smalltables.
1097 Semantically equivalent to:
1098
1099 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1100
1101 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001103 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001105 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001106*/
1107
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001108static void
1109set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 Py_ssize_t t;
1112 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001113 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001115 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 t = a->fill; a->fill = b->fill; b->fill = t;
1118 t = a->used; a->used = b->used; b->used = t;
1119 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 u = a->table;
1122 if (a->table == a->smalltable)
1123 u = b->smalltable;
1124 a->table = b->table;
1125 if (b->table == b->smalltable)
1126 a->table = a->smalltable;
1127 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (a->table == a->smalltable || b->table == b->smalltable) {
1132 memcpy(tab, a->smalltable, sizeof(tab));
1133 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1134 memcpy(b->smalltable, tab, sizeof(tab));
1135 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1138 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1139 h = a->hash; a->hash = b->hash; b->hash = h;
1140 } else {
1141 a->hash = -1;
1142 b->hash = -1;
1143 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001144}
1145
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001146static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001147set_copy(PySetObject *so)
1148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001150}
1151
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001152static PyObject *
1153frozenset_copy(PySetObject *so)
1154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (PyFrozenSet_CheckExact(so)) {
1156 Py_INCREF(so);
1157 return (PyObject *)so;
1158 }
1159 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001160}
1161
Raymond Hettingera690a992003-11-16 16:17:49 +00001162PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1163
1164static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001165set_clear(PySetObject *so)
1166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 set_clear_internal(so);
1168 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001169}
1170
1171PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1172
1173static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001174set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 PySetObject *result;
1177 PyObject *other;
1178 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 result = (PySetObject *)set_copy(so);
1181 if (result == NULL)
1182 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1185 other = PyTuple_GET_ITEM(args, i);
1186 if ((PyObject *)so == other)
1187 continue;
1188 if (set_update_internal(result, other) == -1) {
1189 Py_DECREF(result);
1190 return NULL;
1191 }
1192 }
1193 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001194}
1195
1196PyDoc_STRVAR(union_doc,
1197 "Return the union of sets as a new set.\n\
1198\n\
1199(i.e. all elements that are in either set.)");
1200
1201static PyObject *
1202set_or(PySetObject *so, PyObject *other)
1203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001205
Brian Curtindfc80e32011-08-10 20:28:54 -05001206 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1207 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 result = (PySetObject *)set_copy(so);
1210 if (result == NULL)
1211 return NULL;
1212 if ((PyObject *)so == other)
1213 return (PyObject *)result;
1214 if (set_update_internal(result, other) == -1) {
1215 Py_DECREF(result);
1216 return NULL;
1217 }
1218 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001219}
1220
Raymond Hettingera690a992003-11-16 16:17:49 +00001221static PyObject *
1222set_ior(PySetObject *so, PyObject *other)
1223{
Brian Curtindfc80e32011-08-10 20:28:54 -05001224 if (!PyAnySet_Check(other))
1225 Py_RETURN_NOTIMPLEMENTED;
1226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 if (set_update_internal(so, other) == -1)
1228 return NULL;
1229 Py_INCREF(so);
1230 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001231}
1232
1233static PyObject *
1234set_intersection(PySetObject *so, PyObject *other)
1235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 PySetObject *result;
1237 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 if ((PyObject *)so == other)
1240 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1243 if (result == NULL)
1244 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 if (PyAnySet_Check(other)) {
1247 Py_ssize_t pos = 0;
1248 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1251 tmp = (PyObject *)so;
1252 so = (PySetObject *)other;
1253 other = tmp;
1254 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 while (set_next((PySetObject *)other, &pos, &entry)) {
1257 int rv = set_contains_entry(so, entry);
1258 if (rv == -1) {
1259 Py_DECREF(result);
1260 return NULL;
1261 }
1262 if (rv) {
1263 if (set_add_entry(result, entry) == -1) {
1264 Py_DECREF(result);
1265 return NULL;
1266 }
1267 }
1268 }
1269 return (PyObject *)result;
1270 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 it = PyObject_GetIter(other);
1273 if (it == NULL) {
1274 Py_DECREF(result);
1275 return NULL;
1276 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 while ((key = PyIter_Next(it)) != NULL) {
1279 int rv;
1280 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001281 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (hash == -1) {
1284 Py_DECREF(it);
1285 Py_DECREF(result);
1286 Py_DECREF(key);
1287 return NULL;
1288 }
1289 entry.hash = hash;
1290 entry.key = key;
1291 rv = set_contains_entry(so, &entry);
1292 if (rv == -1) {
1293 Py_DECREF(it);
1294 Py_DECREF(result);
1295 Py_DECREF(key);
1296 return NULL;
1297 }
1298 if (rv) {
1299 if (set_add_entry(result, &entry) == -1) {
1300 Py_DECREF(it);
1301 Py_DECREF(result);
1302 Py_DECREF(key);
1303 return NULL;
1304 }
1305 }
1306 Py_DECREF(key);
1307 }
1308 Py_DECREF(it);
1309 if (PyErr_Occurred()) {
1310 Py_DECREF(result);
1311 return NULL;
1312 }
1313 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001314}
1315
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001316static PyObject *
1317set_intersection_multi(PySetObject *so, PyObject *args)
1318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 Py_ssize_t i;
1320 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 if (PyTuple_GET_SIZE(args) == 0)
1323 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 Py_INCREF(so);
1326 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1327 PyObject *other = PyTuple_GET_ITEM(args, i);
1328 PyObject *newresult = set_intersection((PySetObject *)result, other);
1329 if (newresult == NULL) {
1330 Py_DECREF(result);
1331 return NULL;
1332 }
1333 Py_DECREF(result);
1334 result = newresult;
1335 }
1336 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001337}
1338
Raymond Hettingera690a992003-11-16 16:17:49 +00001339PyDoc_STRVAR(intersection_doc,
1340"Return the intersection of two sets as a new set.\n\
1341\n\
1342(i.e. all elements that are in both sets.)");
1343
1344static PyObject *
1345set_intersection_update(PySetObject *so, PyObject *other)
1346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 tmp = set_intersection(so, other);
1350 if (tmp == NULL)
1351 return NULL;
1352 set_swap_bodies(so, (PySetObject *)tmp);
1353 Py_DECREF(tmp);
1354 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001355}
1356
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001357static PyObject *
1358set_intersection_update_multi(PySetObject *so, PyObject *args)
1359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 tmp = set_intersection_multi(so, args);
1363 if (tmp == NULL)
1364 return NULL;
1365 set_swap_bodies(so, (PySetObject *)tmp);
1366 Py_DECREF(tmp);
1367 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001368}
1369
Raymond Hettingera690a992003-11-16 16:17:49 +00001370PyDoc_STRVAR(intersection_update_doc,
1371"Update a set with the intersection of itself and another.");
1372
1373static PyObject *
1374set_and(PySetObject *so, PyObject *other)
1375{
Brian Curtindfc80e32011-08-10 20:28:54 -05001376 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1377 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001379}
1380
1381static PyObject *
1382set_iand(PySetObject *so, PyObject *other)
1383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001385
Brian Curtindfc80e32011-08-10 20:28:54 -05001386 if (!PyAnySet_Check(other))
1387 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 result = set_intersection_update(so, other);
1389 if (result == NULL)
1390 return NULL;
1391 Py_DECREF(result);
1392 Py_INCREF(so);
1393 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001394}
1395
Guido van Rossum58da9312007-11-10 23:39:45 +00001396static PyObject *
1397set_isdisjoint(PySetObject *so, PyObject *other)
1398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if ((PyObject *)so == other) {
1402 if (PySet_GET_SIZE(so) == 0)
1403 Py_RETURN_TRUE;
1404 else
1405 Py_RETURN_FALSE;
1406 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 if (PyAnySet_CheckExact(other)) {
1409 Py_ssize_t pos = 0;
1410 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1413 tmp = (PyObject *)so;
1414 so = (PySetObject *)other;
1415 other = tmp;
1416 }
1417 while (set_next((PySetObject *)other, &pos, &entry)) {
1418 int rv = set_contains_entry(so, entry);
1419 if (rv == -1)
1420 return NULL;
1421 if (rv)
1422 Py_RETURN_FALSE;
1423 }
1424 Py_RETURN_TRUE;
1425 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 it = PyObject_GetIter(other);
1428 if (it == NULL)
1429 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 while ((key = PyIter_Next(it)) != NULL) {
1432 int rv;
1433 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001434 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 if (hash == -1) {
1437 Py_DECREF(key);
1438 Py_DECREF(it);
1439 return NULL;
1440 }
1441 entry.hash = hash;
1442 entry.key = key;
1443 rv = set_contains_entry(so, &entry);
1444 Py_DECREF(key);
1445 if (rv == -1) {
1446 Py_DECREF(it);
1447 return NULL;
1448 }
1449 if (rv) {
1450 Py_DECREF(it);
1451 Py_RETURN_FALSE;
1452 }
1453 }
1454 Py_DECREF(it);
1455 if (PyErr_Occurred())
1456 return NULL;
1457 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001458}
1459
1460PyDoc_STRVAR(isdisjoint_doc,
1461"Return True if two sets have a null intersection.");
1462
Neal Norwitz6576bd82005-11-13 18:41:28 +00001463static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001464set_difference_update_internal(PySetObject *so, PyObject *other)
1465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 if ((PyObject *)so == other)
1467 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 if (PyAnySet_Check(other)) {
1470 setentry *entry;
1471 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 while (set_next((PySetObject *)other, &pos, &entry))
1474 if (set_discard_entry(so, entry) == -1)
1475 return -1;
1476 } else {
1477 PyObject *key, *it;
1478 it = PyObject_GetIter(other);
1479 if (it == NULL)
1480 return -1;
1481
1482 while ((key = PyIter_Next(it)) != NULL) {
1483 if (set_discard_key(so, key) == -1) {
1484 Py_DECREF(it);
1485 Py_DECREF(key);
1486 return -1;
1487 }
1488 Py_DECREF(key);
1489 }
1490 Py_DECREF(it);
1491 if (PyErr_Occurred())
1492 return -1;
1493 }
1494 /* If more than 1/5 are dummies, then resize them away. */
1495 if ((so->fill - so->used) * 5 < so->mask)
1496 return 0;
1497 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001498}
1499
Raymond Hettingera690a992003-11-16 16:17:49 +00001500static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001501set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1506 PyObject *other = PyTuple_GET_ITEM(args, i);
1507 if (set_difference_update_internal(so, other) == -1)
1508 return NULL;
1509 }
1510 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001511}
1512
1513PyDoc_STRVAR(difference_update_doc,
1514"Remove all elements of another set from this set.");
1515
1516static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001517set_copy_and_difference(PySetObject *so, PyObject *other)
1518{
1519 PyObject *result;
1520
1521 result = set_copy(so);
1522 if (result == NULL)
1523 return NULL;
1524 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1525 return result;
1526 Py_DECREF(result);
1527 return NULL;
1528}
1529
1530static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001531set_difference(PySetObject *so, PyObject *other)
1532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 PyObject *result;
1534 setentry *entry;
1535 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001538 return set_copy_and_difference(so, other);
1539 }
1540
1541 /* If len(so) much more than len(other), it's more efficient to simply copy
1542 * so and then iterate other looking for common elements. */
1543 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1544 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 result = make_new_set_basetype(Py_TYPE(so), NULL);
1548 if (result == NULL)
1549 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 if (PyDict_CheckExact(other)) {
1552 while (set_next(so, &pos, &entry)) {
1553 setentry entrycopy;
1554 entrycopy.hash = entry->hash;
1555 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001556 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1558 Py_DECREF(result);
1559 return NULL;
1560 }
1561 }
1562 }
1563 return result;
1564 }
1565
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001566 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 while (set_next(so, &pos, &entry)) {
1568 int rv = set_contains_entry((PySetObject *)other, entry);
1569 if (rv == -1) {
1570 Py_DECREF(result);
1571 return NULL;
1572 }
1573 if (!rv) {
1574 if (set_add_entry((PySetObject *)result, entry) == -1) {
1575 Py_DECREF(result);
1576 return NULL;
1577 }
1578 }
1579 }
1580 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001581}
1582
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001583static PyObject *
1584set_difference_multi(PySetObject *so, PyObject *args)
1585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 Py_ssize_t i;
1587 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 if (PyTuple_GET_SIZE(args) == 0)
1590 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 other = PyTuple_GET_ITEM(args, 0);
1593 result = set_difference(so, other);
1594 if (result == NULL)
1595 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1598 other = PyTuple_GET_ITEM(args, i);
1599 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1600 Py_DECREF(result);
1601 return NULL;
1602 }
1603 }
1604 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001605}
1606
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001607PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001608"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001609\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001610(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001611static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001612set_sub(PySetObject *so, PyObject *other)
1613{
Brian Curtindfc80e32011-08-10 20:28:54 -05001614 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1615 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001617}
1618
1619static PyObject *
1620set_isub(PySetObject *so, PyObject *other)
1621{
Brian Curtindfc80e32011-08-10 20:28:54 -05001622 if (!PyAnySet_Check(other))
1623 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (set_difference_update_internal(so, other) == -1)
1625 return NULL;
1626 Py_INCREF(so);
1627 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001628}
1629
1630static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001631set_symmetric_difference_update(PySetObject *so, PyObject *other)
1632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 PySetObject *otherset;
1634 PyObject *key;
1635 Py_ssize_t pos = 0;
1636 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if ((PyObject *)so == other)
1639 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 if (PyDict_CheckExact(other)) {
1642 PyObject *value;
1643 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001644 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1646 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001647
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001648 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 an_entry.hash = hash;
1650 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001653 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001654 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001657 if (rv == DISCARD_NOTFOUND) {
1658 if (set_add_entry(so, &an_entry) == -1) {
1659 Py_DECREF(key);
1660 return NULL;
1661 }
1662 }
Georg Brandl2d444492010-09-03 10:52:55 +00001663 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 }
1665 Py_RETURN_NONE;
1666 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 if (PyAnySet_Check(other)) {
1669 Py_INCREF(other);
1670 otherset = (PySetObject *)other;
1671 } else {
1672 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1673 if (otherset == NULL)
1674 return NULL;
1675 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 while (set_next(otherset, &pos, &entry)) {
1678 int rv = set_discard_entry(so, entry);
1679 if (rv == -1) {
1680 Py_DECREF(otherset);
1681 return NULL;
1682 }
1683 if (rv == DISCARD_NOTFOUND) {
1684 if (set_add_entry(so, entry) == -1) {
1685 Py_DECREF(otherset);
1686 return NULL;
1687 }
1688 }
1689 }
1690 Py_DECREF(otherset);
1691 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001692}
1693
1694PyDoc_STRVAR(symmetric_difference_update_doc,
1695"Update a set with the symmetric difference of itself and another.");
1696
1697static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001698set_symmetric_difference(PySetObject *so, PyObject *other)
1699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 PyObject *rv;
1701 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1704 if (otherset == NULL)
1705 return NULL;
1706 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1707 if (rv == NULL)
1708 return NULL;
1709 Py_DECREF(rv);
1710 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001711}
1712
1713PyDoc_STRVAR(symmetric_difference_doc,
1714"Return the symmetric difference of two sets as a new set.\n\
1715\n\
1716(i.e. all elements that are in exactly one of the sets.)");
1717
1718static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001719set_xor(PySetObject *so, PyObject *other)
1720{
Brian Curtindfc80e32011-08-10 20:28:54 -05001721 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1722 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001724}
1725
1726static PyObject *
1727set_ixor(PySetObject *so, PyObject *other)
1728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001730
Brian Curtindfc80e32011-08-10 20:28:54 -05001731 if (!PyAnySet_Check(other))
1732 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 result = set_symmetric_difference_update(so, other);
1734 if (result == NULL)
1735 return NULL;
1736 Py_DECREF(result);
1737 Py_INCREF(so);
1738 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001739}
1740
1741static PyObject *
1742set_issubset(PySetObject *so, PyObject *other)
1743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 setentry *entry;
1745 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 if (!PyAnySet_Check(other)) {
1748 PyObject *tmp, *result;
1749 tmp = make_new_set(&PySet_Type, other);
1750 if (tmp == NULL)
1751 return NULL;
1752 result = set_issubset(so, tmp);
1753 Py_DECREF(tmp);
1754 return result;
1755 }
1756 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1757 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 while (set_next(so, &pos, &entry)) {
1760 int rv = set_contains_entry((PySetObject *)other, entry);
1761 if (rv == -1)
1762 return NULL;
1763 if (!rv)
1764 Py_RETURN_FALSE;
1765 }
1766 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001767}
1768
1769PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1770
1771static PyObject *
1772set_issuperset(PySetObject *so, PyObject *other)
1773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 if (!PyAnySet_Check(other)) {
1777 tmp = make_new_set(&PySet_Type, other);
1778 if (tmp == NULL)
1779 return NULL;
1780 result = set_issuperset(so, tmp);
1781 Py_DECREF(tmp);
1782 return result;
1783 }
1784 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001785}
1786
1787PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1788
Raymond Hettingera690a992003-11-16 16:17:49 +00001789static PyObject *
1790set_richcompare(PySetObject *v, PyObject *w, int op)
1791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001793
Brian Curtindfc80e32011-08-10 20:28:54 -05001794 if(!PyAnySet_Check(w))
1795 Py_RETURN_NOTIMPLEMENTED;
1796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 switch (op) {
1798 case Py_EQ:
1799 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1800 Py_RETURN_FALSE;
1801 if (v->hash != -1 &&
1802 ((PySetObject *)w)->hash != -1 &&
1803 v->hash != ((PySetObject *)w)->hash)
1804 Py_RETURN_FALSE;
1805 return set_issubset(v, w);
1806 case Py_NE:
1807 r1 = set_richcompare(v, w, Py_EQ);
1808 if (r1 == NULL)
1809 return NULL;
1810 r2 = PyBool_FromLong(PyObject_Not(r1));
1811 Py_DECREF(r1);
1812 return r2;
1813 case Py_LE:
1814 return set_issubset(v, w);
1815 case Py_GE:
1816 return set_issuperset(v, w);
1817 case Py_LT:
1818 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1819 Py_RETURN_FALSE;
1820 return set_issubset(v, w);
1821 case Py_GT:
1822 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1823 Py_RETURN_FALSE;
1824 return set_issuperset(v, w);
1825 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001826 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001827}
1828
Raymond Hettingera690a992003-11-16 16:17:49 +00001829static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001830set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 if (set_add_key(so, key) == -1)
1833 return NULL;
1834 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001835}
1836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001838"Add an element to a set.\n\
1839\n\
1840This has no effect if the element is already present.");
1841
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001842static int
1843set_contains(PySetObject *so, PyObject *key)
1844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 PyObject *tmpkey;
1846 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 rv = set_contains_key(so, key);
1849 if (rv == -1) {
1850 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1851 return -1;
1852 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001853 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 if (tmpkey == NULL)
1855 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001856 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 Py_DECREF(tmpkey);
1858 }
1859 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001860}
1861
1862static PyObject *
1863set_direct_contains(PySetObject *so, PyObject *key)
1864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 result = set_contains(so, key);
1868 if (result == -1)
1869 return NULL;
1870 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001871}
1872
1873PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1874
Raymond Hettingera690a992003-11-16 16:17:49 +00001875static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001876set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 PyObject *tmpkey;
1879 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 rv = set_discard_key(so, key);
1882 if (rv == -1) {
1883 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1884 return NULL;
1885 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001886 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 if (tmpkey == NULL)
1888 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 Py_DECREF(tmpkey);
1891 if (rv == -1)
1892 return NULL;
1893 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001896 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 return NULL;
1898 }
1899 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001900}
1901
1902PyDoc_STRVAR(remove_doc,
1903"Remove an element from a set; it must be a member.\n\
1904\n\
1905If the element is not a member, raise a KeyError.");
1906
1907static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001908set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001909{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001910 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 rv = set_discard_key(so, key);
1914 if (rv == -1) {
1915 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1916 return NULL;
1917 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001918 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 if (tmpkey == NULL)
1920 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001921 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001923 if (rv == -1)
1924 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 }
1926 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001927}
1928
1929PyDoc_STRVAR(discard_doc,
1930"Remove an element from a set if it is a member.\n\
1931\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001933
1934static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001935set_reduce(PySetObject *so)
1936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001938 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 keys = PySequence_List((PyObject *)so);
1941 if (keys == NULL)
1942 goto done;
1943 args = PyTuple_Pack(1, keys);
1944 if (args == NULL)
1945 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001946 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 if (dict == NULL) {
1948 PyErr_Clear();
1949 dict = Py_None;
1950 Py_INCREF(dict);
1951 }
1952 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001953done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 Py_XDECREF(args);
1955 Py_XDECREF(keys);
1956 Py_XDECREF(dict);
1957 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001958}
1959
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001960static PyObject *
1961set_sizeof(PySetObject *so)
1962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 res = sizeof(PySetObject);
1966 if (so->table != so->smalltable)
1967 res = res + (so->mask + 1) * sizeof(setentry);
1968 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001969}
1970
1971PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001972static int
1973set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 if (!PyAnySet_Check(self))
1978 return -1;
1979 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1980 return -1;
1981 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1982 return -1;
1983 set_clear_internal(self);
1984 self->hash = -1;
1985 if (iterable == NULL)
1986 return 0;
1987 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001988}
1989
Raymond Hettingera690a992003-11-16 16:17:49 +00001990static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 set_len, /* sq_length */
1992 0, /* sq_concat */
1993 0, /* sq_repeat */
1994 0, /* sq_item */
1995 0, /* sq_slice */
1996 0, /* sq_ass_item */
1997 0, /* sq_ass_slice */
1998 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001999};
2000
2001/* set object ********************************************************/
2002
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002003#ifdef Py_DEBUG
2004static PyObject *test_c_api(PySetObject *so);
2005
2006PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2007All is well if assertions don't fail.");
2008#endif
2009
Raymond Hettingera690a992003-11-16 16:17:49 +00002010static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 {"add", (PyCFunction)set_add, METH_O,
2012 add_doc},
2013 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2014 clear_doc},
2015 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2016 contains_doc},
2017 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2018 copy_doc},
2019 {"discard", (PyCFunction)set_discard, METH_O,
2020 discard_doc},
2021 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2022 difference_doc},
2023 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2024 difference_update_doc},
2025 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2026 intersection_doc},
2027 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2028 intersection_update_doc},
2029 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2030 isdisjoint_doc},
2031 {"issubset", (PyCFunction)set_issubset, METH_O,
2032 issubset_doc},
2033 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2034 issuperset_doc},
2035 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2036 pop_doc},
2037 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2038 reduce_doc},
2039 {"remove", (PyCFunction)set_remove, METH_O,
2040 remove_doc},
2041 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2042 sizeof_doc},
2043 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2044 symmetric_difference_doc},
2045 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2046 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002047#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2049 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002050#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 {"union", (PyCFunction)set_union, METH_VARARGS,
2052 union_doc},
2053 {"update", (PyCFunction)set_update, METH_VARARGS,
2054 update_doc},
2055 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002056};
2057
2058static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 0, /*nb_add*/
2060 (binaryfunc)set_sub, /*nb_subtract*/
2061 0, /*nb_multiply*/
2062 0, /*nb_remainder*/
2063 0, /*nb_divmod*/
2064 0, /*nb_power*/
2065 0, /*nb_negative*/
2066 0, /*nb_positive*/
2067 0, /*nb_absolute*/
2068 0, /*nb_bool*/
2069 0, /*nb_invert*/
2070 0, /*nb_lshift*/
2071 0, /*nb_rshift*/
2072 (binaryfunc)set_and, /*nb_and*/
2073 (binaryfunc)set_xor, /*nb_xor*/
2074 (binaryfunc)set_or, /*nb_or*/
2075 0, /*nb_int*/
2076 0, /*nb_reserved*/
2077 0, /*nb_float*/
2078 0, /*nb_inplace_add*/
2079 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2080 0, /*nb_inplace_multiply*/
2081 0, /*nb_inplace_remainder*/
2082 0, /*nb_inplace_power*/
2083 0, /*nb_inplace_lshift*/
2084 0, /*nb_inplace_rshift*/
2085 (binaryfunc)set_iand, /*nb_inplace_and*/
2086 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2087 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002088};
2089
2090PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002091"set() -> new empty set object\n\
2092set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002093\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002094Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002095
2096PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2098 "set", /* tp_name */
2099 sizeof(PySetObject), /* tp_basicsize */
2100 0, /* tp_itemsize */
2101 /* methods */
2102 (destructor)set_dealloc, /* tp_dealloc */
2103 0, /* tp_print */
2104 0, /* tp_getattr */
2105 0, /* tp_setattr */
2106 0, /* tp_reserved */
2107 (reprfunc)set_repr, /* tp_repr */
2108 &set_as_number, /* tp_as_number */
2109 &set_as_sequence, /* tp_as_sequence */
2110 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002111 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 0, /* tp_call */
2113 0, /* tp_str */
2114 PyObject_GenericGetAttr, /* tp_getattro */
2115 0, /* tp_setattro */
2116 0, /* tp_as_buffer */
2117 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2118 Py_TPFLAGS_BASETYPE, /* tp_flags */
2119 set_doc, /* tp_doc */
2120 (traverseproc)set_traverse, /* tp_traverse */
2121 (inquiry)set_clear_internal, /* tp_clear */
2122 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002123 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2124 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 0, /* tp_iternext */
2126 set_methods, /* tp_methods */
2127 0, /* tp_members */
2128 0, /* tp_getset */
2129 0, /* tp_base */
2130 0, /* tp_dict */
2131 0, /* tp_descr_get */
2132 0, /* tp_descr_set */
2133 0, /* tp_dictoffset */
2134 (initproc)set_init, /* tp_init */
2135 PyType_GenericAlloc, /* tp_alloc */
2136 set_new, /* tp_new */
2137 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002138};
2139
2140/* frozenset object ********************************************************/
2141
2142
2143static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2145 contains_doc},
2146 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2147 copy_doc},
2148 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2149 difference_doc},
2150 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2151 intersection_doc},
2152 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2153 isdisjoint_doc},
2154 {"issubset", (PyCFunction)set_issubset, METH_O,
2155 issubset_doc},
2156 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2157 issuperset_doc},
2158 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2159 reduce_doc},
2160 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2161 sizeof_doc},
2162 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2163 symmetric_difference_doc},
2164 {"union", (PyCFunction)set_union, METH_VARARGS,
2165 union_doc},
2166 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002167};
2168
2169static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 0, /*nb_add*/
2171 (binaryfunc)set_sub, /*nb_subtract*/
2172 0, /*nb_multiply*/
2173 0, /*nb_remainder*/
2174 0, /*nb_divmod*/
2175 0, /*nb_power*/
2176 0, /*nb_negative*/
2177 0, /*nb_positive*/
2178 0, /*nb_absolute*/
2179 0, /*nb_bool*/
2180 0, /*nb_invert*/
2181 0, /*nb_lshift*/
2182 0, /*nb_rshift*/
2183 (binaryfunc)set_and, /*nb_and*/
2184 (binaryfunc)set_xor, /*nb_xor*/
2185 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002186};
2187
2188PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002189"frozenset() -> empty frozenset object\n\
2190frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002191\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002192Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002193
2194PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2196 "frozenset", /* tp_name */
2197 sizeof(PySetObject), /* tp_basicsize */
2198 0, /* tp_itemsize */
2199 /* methods */
2200 (destructor)set_dealloc, /* tp_dealloc */
2201 0, /* tp_print */
2202 0, /* tp_getattr */
2203 0, /* tp_setattr */
2204 0, /* tp_reserved */
2205 (reprfunc)set_repr, /* tp_repr */
2206 &frozenset_as_number, /* tp_as_number */
2207 &set_as_sequence, /* tp_as_sequence */
2208 0, /* tp_as_mapping */
2209 frozenset_hash, /* tp_hash */
2210 0, /* tp_call */
2211 0, /* tp_str */
2212 PyObject_GenericGetAttr, /* tp_getattro */
2213 0, /* tp_setattro */
2214 0, /* tp_as_buffer */
2215 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2216 Py_TPFLAGS_BASETYPE, /* tp_flags */
2217 frozenset_doc, /* tp_doc */
2218 (traverseproc)set_traverse, /* tp_traverse */
2219 (inquiry)set_clear_internal, /* tp_clear */
2220 (richcmpfunc)set_richcompare, /* tp_richcompare */
2221 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2222 (getiterfunc)set_iter, /* tp_iter */
2223 0, /* tp_iternext */
2224 frozenset_methods, /* tp_methods */
2225 0, /* tp_members */
2226 0, /* tp_getset */
2227 0, /* tp_base */
2228 0, /* tp_dict */
2229 0, /* tp_descr_get */
2230 0, /* tp_descr_set */
2231 0, /* tp_dictoffset */
2232 0, /* tp_init */
2233 PyType_GenericAlloc, /* tp_alloc */
2234 frozenset_new, /* tp_new */
2235 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002236};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002237
2238
2239/***** C API functions *************************************************/
2240
2241PyObject *
2242PySet_New(PyObject *iterable)
2243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002245}
2246
2247PyObject *
2248PyFrozenSet_New(PyObject *iterable)
2249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002251}
2252
Neal Norwitz8c49c822006-03-04 18:41:19 +00002253Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002254PySet_Size(PyObject *anyset)
2255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 if (!PyAnySet_Check(anyset)) {
2257 PyErr_BadInternalCall();
2258 return -1;
2259 }
2260 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002261}
2262
2263int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002264PySet_Clear(PyObject *set)
2265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 if (!PySet_Check(set)) {
2267 PyErr_BadInternalCall();
2268 return -1;
2269 }
2270 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002271}
2272
2273int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002274PySet_Contains(PyObject *anyset, PyObject *key)
2275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 if (!PyAnySet_Check(anyset)) {
2277 PyErr_BadInternalCall();
2278 return -1;
2279 }
2280 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002281}
2282
2283int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002284PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 if (!PySet_Check(set)) {
2287 PyErr_BadInternalCall();
2288 return -1;
2289 }
2290 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002291}
2292
2293int
Christian Heimesfd66e512008-01-29 12:18:50 +00002294PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 if (!PySet_Check(anyset) &&
2297 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2298 PyErr_BadInternalCall();
2299 return -1;
2300 }
2301 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002302}
2303
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002304int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002305_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 if (!PyAnySet_Check(set)) {
2310 PyErr_BadInternalCall();
2311 return -1;
2312 }
2313 if (set_next((PySetObject *)set, pos, &entry) == 0)
2314 return 0;
2315 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002316 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002318}
2319
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002320PyObject *
2321PySet_Pop(PyObject *set)
2322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 if (!PySet_Check(set)) {
2324 PyErr_BadInternalCall();
2325 return NULL;
2326 }
2327 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002328}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002329
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002330int
2331_PySet_Update(PyObject *set, PyObject *iterable)
2332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 if (!PySet_Check(set)) {
2334 PyErr_BadInternalCall();
2335 return -1;
2336 }
2337 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002338}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002339
Raymond Hettingere259f132013-12-15 11:56:14 -08002340/* Exported for the gdb plugin's benefit. */
2341PyObject *_PySet_Dummy = dummy;
2342
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002343#ifdef Py_DEBUG
2344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002346 Returns True and original set is restored. */
2347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348#define assertRaises(call_return_value, exception) \
2349 do { \
2350 assert(call_return_value); \
2351 assert(PyErr_ExceptionMatches(exception)); \
2352 PyErr_Clear(); \
2353 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002354
2355static PyObject *
2356test_c_api(PySetObject *so)
2357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 Py_ssize_t count;
2359 char *s;
2360 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002361 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002363 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 /* Verify preconditions */
2367 assert(PyAnySet_Check(ob));
2368 assert(PyAnySet_CheckExact(ob));
2369 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 /* so.clear(); so |= set("abc"); */
2372 str = PyUnicode_FromString("abc");
2373 if (str == NULL)
2374 return NULL;
2375 set_clear_internal(so);
2376 if (set_update_internal(so, str) == -1) {
2377 Py_DECREF(str);
2378 return NULL;
2379 }
2380 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 /* Exercise type/size checks */
2383 assert(PySet_Size(ob) == 3);
2384 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 /* Raise TypeError for non-iterable constructor arguments */
2387 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2388 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 /* Raise TypeError for unhashable key */
2391 dup = PySet_New(ob);
2392 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2393 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2394 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 /* Exercise successful pop, contains, add, and discard */
2397 elem = PySet_Pop(ob);
2398 assert(PySet_Contains(ob, elem) == 0);
2399 assert(PySet_GET_SIZE(ob) == 2);
2400 assert(PySet_Add(ob, elem) == 0);
2401 assert(PySet_Contains(ob, elem) == 1);
2402 assert(PySet_GET_SIZE(ob) == 3);
2403 assert(PySet_Discard(ob, elem) == 1);
2404 assert(PySet_GET_SIZE(ob) == 2);
2405 assert(PySet_Discard(ob, elem) == 0);
2406 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 /* Exercise clear */
2409 dup2 = PySet_New(dup);
2410 assert(PySet_Clear(dup2) == 0);
2411 assert(PySet_Size(dup2) == 0);
2412 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 /* Raise SystemError on clear or update of frozen set */
2415 f = PyFrozenSet_New(dup);
2416 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2417 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2418 assert(PySet_Add(f, elem) == 0);
2419 Py_INCREF(f);
2420 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2421 Py_DECREF(f);
2422 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* Exercise direct iteration */
2425 i = 0, count = 0;
2426 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2427 s = _PyUnicode_AsString(x);
2428 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2429 count++;
2430 }
2431 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 /* Exercise updates */
2434 dup2 = PySet_New(NULL);
2435 assert(_PySet_Update(dup2, dup) == 0);
2436 assert(PySet_Size(dup2) == 3);
2437 assert(_PySet_Update(dup2, dup) == 0);
2438 assert(PySet_Size(dup2) == 3);
2439 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 /* Raise SystemError when self argument is not a set or frozenset. */
2442 t = PyTuple_New(0);
2443 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2444 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2445 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 /* Raise SystemError when self argument is not a set. */
2448 f = PyFrozenSet_New(dup);
2449 assert(PySet_Size(f) == 3);
2450 assert(PyFrozenSet_CheckExact(f));
2451 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2452 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2453 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 /* Raise KeyError when popping from an empty set */
2456 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2457 Py_DECREF(ob);
2458 assert(PySet_GET_SIZE(ob) == 0);
2459 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 /* Restore the set from the copy using the PyNumber API */
2462 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2463 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 /* Verify constructors accept NULL arguments */
2466 f = PySet_New(NULL);
2467 assert(f != NULL);
2468 assert(PySet_GET_SIZE(f) == 0);
2469 Py_DECREF(f);
2470 f = PyFrozenSet_New(NULL);
2471 assert(f != NULL);
2472 assert(PyFrozenSet_CheckExact(f));
2473 assert(PySet_GET_SIZE(f) == 0);
2474 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 Py_DECREF(elem);
2477 Py_DECREF(dup);
2478 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002479}
2480
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002481#undef assertRaises
2482
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002483#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002484
2485/***** Dummy Struct *************************************************/
2486
2487static PyObject *
2488dummy_repr(PyObject *op)
2489{
2490 return PyUnicode_FromString("<dummy key>");
2491}
2492
2493static void
2494dummy_dealloc(PyObject* ignore)
2495{
2496 Py_FatalError("deallocating <dummy key>");
2497}
2498
2499static PyTypeObject _PySetDummy_Type = {
2500 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2501 "<dummy key> type",
2502 0,
2503 0,
2504 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2505 0, /*tp_print*/
2506 0, /*tp_getattr*/
2507 0, /*tp_setattr*/
2508 0, /*tp_reserved*/
2509 dummy_repr, /*tp_repr*/
2510 0, /*tp_as_number*/
2511 0, /*tp_as_sequence*/
2512 0, /*tp_as_mapping*/
2513 0, /*tp_hash */
2514 0, /*tp_call */
2515 0, /*tp_str */
2516 0, /*tp_getattro */
2517 0, /*tp_setattro */
2518 0, /*tp_as_buffer */
2519 Py_TPFLAGS_DEFAULT, /*tp_flags */
2520};
2521
2522static PyObject _dummy_struct = {
2523 _PyObject_EXTRA_INIT
2524 2, &_PySetDummy_Type
2525};
2526