blob: 4ef692db33203129a69a324ed285c89fc533cac8 [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 Hettingerae7b00e2013-09-07 15:05:00 -07007 The basic lookup function used by all operations.
8 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
9
10 The initial probe index is computed as hash mod the table size.
11 Subsequent probe indices are computed as explained in Objects/dictobject.c.
12
13 To improve cache locality, each probe inspects a series of consecutive
14 nearby entries before moving on to probes elsewhere in memory. This leaves
15 us with a hybrid of linear probing and open addressing. The linear probing
16 reduces the cost of hash collisions because consecutive memory accesses
17 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
18 we then use open addressing with the upper bits from the hash value. This
19 helps break-up long chains of collisions.
20
21 All arithmetic on hash should ignore overflow.
22
Raymond Hettinger4d45c102015-01-25 16:27:40 -080023 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070024 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000025*/
26
Raymond Hettingera690a992003-11-16 16:17:49 +000027#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000028#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000029#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000030
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000031/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050032static PyObject _dummy_struct;
33
34#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000036
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070037/* ======================================================================== */
38/* ======= Begin logic for probing the hash table ========================= */
39
Raymond Hettinger710a67e2013-09-21 20:17:31 -070040/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070041#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070042#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070043#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070044
45/* This must be >= 1 */
46#define PERTURB_SHIFT 5
47
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000048static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020049set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 setentry *table = so->table;
Yury Selivanov7aa53412015-05-30 10:57:56 -040052 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020053 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070054 size_t perturb = hash;
55 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080056 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070057 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070058 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000059
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080060 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070061 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070063
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070064 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080065 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070066 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080067 /* startkey cannot be a dummy because the dummy hash field is -1 */
68 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080069 if (startkey == key)
70 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080071 if (PyUnicode_CheckExact(startkey)
72 && PyUnicode_CheckExact(key)
73 && unicode_eq(startkey, key))
74 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 Py_INCREF(startkey);
76 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
77 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010078 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010080 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010082 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070083 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060084 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070085 }
Yury Selivanov7aa53412015-05-30 10:57:56 -040086 if (entry->hash == -1 && freeslot == NULL)
87 freeslot = entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070088
Raymond Hettingerc658d852015-02-02 08:35:00 -080089 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080090 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080091 entry++;
Yury Selivanov7aa53412015-05-30 10:57:56 -040092 if (entry->key == NULL)
93 goto found_null;
Raymond Hettingerc658d852015-02-02 08:35:00 -080094 if (entry->hash == hash) {
95 PyObject *startkey = entry->key;
96 assert(startkey != dummy);
97 if (startkey == key)
98 return entry;
99 if (PyUnicode_CheckExact(startkey)
100 && PyUnicode_CheckExact(key)
101 && unicode_eq(startkey, key))
102 return entry;
103 Py_INCREF(startkey);
104 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
105 Py_DECREF(startkey);
106 if (cmp < 0)
107 return NULL;
108 if (table != so->table || entry->key != startkey)
109 return set_lookkey(so, key, hash);
110 if (cmp > 0)
111 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600112 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800113 }
Raymond Hettinger438f9132015-02-09 06:48:29 -0600114 if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800115 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700118
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700119 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800120 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700121
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800122 entry = &table[i];
Yury Selivanov7aa53412015-05-30 10:57:56 -0400123 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700124 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700126 found_null:
Yury Selivanov7aa53412015-05-30 10:57:56 -0400127 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000128}
129
130/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000131Internal routine used by set_table_resize() to insert an item which is
132known to be absent from the set. This routine also assumes that
133the set contains no deleted entries. Besides the performance benefit,
134using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
135Note that no refcounts are changed by this routine; if needed, the caller
136is responsible for incref'ing `key`.
137*/
138static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200139set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200142 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700143 size_t perturb = hash;
144 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800145 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700146 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000147
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700148 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800149 entry = &table[i];
150 if (entry->key == NULL)
151 goto found_null;
152 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800153 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800154 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800155 if (entry->key == NULL)
156 goto found_null;
157 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700158 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700159 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800160 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700162 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 entry->key = key;
164 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700165 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000167}
168
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700169/* ======== End logic for probing the hash table ========================== */
170/* ======================================================================== */
171
Yury Selivanov7aa53412015-05-30 10:57:56 -0400172
173/*
174Internal routine to insert a new key into the table.
175Used by the public insert routine.
176Eats a reference to key.
177*/
178static int
179set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
180{
181 setentry *entry;
182
183 entry = set_lookkey(so, key, hash);
184 if (entry == NULL)
185 return -1;
186 if (entry->key == NULL) {
187 /* UNUSED */
188 entry->key = key;
189 entry->hash = hash;
190 so->fill++;
191 so->used++;
192 } else if (entry->key == dummy) {
193 /* DUMMY */
194 entry->key = key;
195 entry->hash = hash;
196 so->used++;
197 } else {
198 /* ACTIVE */
199 Py_DECREF(key);
200 }
201 return 0;
202}
203
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000205Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000206keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000207actually be smaller than the old one.
208*/
209static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000210set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 Py_ssize_t newsize;
213 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800214 Py_ssize_t oldfill = so->fill;
215 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 int is_oldtable_malloced;
217 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100222 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 for (newsize = PySet_MINSIZE;
224 newsize <= minused && newsize > 0;
225 newsize <<= 1)
226 ;
227 if (newsize <= 0) {
228 PyErr_NoMemory();
229 return -1;
230 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 /* Get space for a new table. */
233 oldtable = so->table;
234 assert(oldtable != NULL);
235 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 if (newsize == PySet_MINSIZE) {
238 /* A large table is shrinking, or we can't get any smaller. */
239 newtable = so->smalltable;
240 if (newtable == oldtable) {
241 if (so->fill == so->used) {
242 /* No dummies, so no point doing anything. */
243 return 0;
244 }
245 /* We're not going to resize it, but rebuild the
246 table anyway to purge old dummy entries.
247 Subtle: This is *necessary* if fill==size,
248 as set_lookkey needs at least one virgin slot to
249 terminate failing searches. If fill < size, it's
250 merely desirable, as dummies slow searches. */
251 assert(so->fill > so->used);
252 memcpy(small_copy, oldtable, sizeof(small_copy));
253 oldtable = small_copy;
254 }
255 }
256 else {
257 newtable = PyMem_NEW(setentry, newsize);
258 if (newtable == NULL) {
259 PyErr_NoMemory();
260 return -1;
261 }
262 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* Make the set empty, using the new table. */
265 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800268 so->used = 0;
269 so->mask = newsize - 1;
270 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* Copy the data over; this is refcount-neutral for active entries;
273 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800274 if (oldfill == oldused) {
275 for (entry = oldtable; oldused > 0; entry++) {
276 if (entry->key != NULL) {
277 oldused--;
278 set_insert_clean(so, entry->key, entry->hash);
279 }
280 }
281 } else {
282 for (entry = oldtable; oldused > 0; entry++) {
283 if (entry->key != NULL && entry->key != dummy) {
284 oldused--;
285 set_insert_clean(so, entry->key, entry->hash);
286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
288 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if (is_oldtable_malloced)
291 PyMem_DEL(oldtable);
292 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000293}
294
Raymond Hettingerc991db22005-08-11 07:58:45 +0000295/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
296
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000297static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200298set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000299{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200300 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000301 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100302 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 assert(so->fill <= so->mask); /* at least one empty slot */
305 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000306 Py_INCREF(key);
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700307 if (set_insert_key(so, key, hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000308 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 return -1;
310 }
311 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
312 return 0;
313 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000314}
315
316static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200317set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000318{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800319 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200320 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200323 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 hash = PyObject_Hash(key);
325 if (hash == -1)
326 return -1;
327 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800328 entry.key = key;
329 entry.hash = hash;
330 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331}
332
333#define DISCARD_NOTFOUND 0
334#define DISCARD_FOUND 1
335
336static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000337set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800338{
339 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000341
Raymond Hettinger93035c42015-01-25 16:12:49 -0800342 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 if (entry == NULL)
344 return -1;
Yury Selivanov7aa53412015-05-30 10:57:56 -0400345 if (entry->key == NULL || entry->key == dummy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 return DISCARD_NOTFOUND;
347 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800349 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 so->used--;
351 Py_DECREF(old_key);
352 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000353}
354
355static int
356set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000357{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800358 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200359 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200364 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 hash = PyObject_Hash(key);
366 if (hash == -1)
367 return -1;
368 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800369 entry.key = key;
370 entry.hash = hash;
371 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000372}
373
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700374static void
375set_empty_to_minsize(PySetObject *so)
376{
377 memset(so->smalltable, 0, sizeof(so->smalltable));
378 so->fill = 0;
379 so->used = 0;
380 so->mask = PySet_MINSIZE - 1;
381 so->table = so->smalltable;
382 so->hash = -1;
383}
384
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000385static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386set_clear_internal(PySetObject *so)
387{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800388 setentry *entry;
389 setentry *table = so->table;
390 Py_ssize_t fill = so->fill;
391 Py_ssize_t used = so->used;
392 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000394
Raymond Hettinger583cd032013-09-07 22:06:35 -0700395 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 /* This is delicate. During the process of clearing the set,
399 * decrefs can cause the set to mutate. To avoid fatal confusion
400 * (voice of experience), we have to make the set empty before
401 * clearing the slots, and never refer to anything via so->ref while
402 * clearing.
403 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700405 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 else if (fill > 0) {
408 /* It's a small table with something that needs to be cleared.
409 * Afraid the only safe way is to copy the set entries into
410 * another small table first.
411 */
412 memcpy(small_copy, table, sizeof(small_copy));
413 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700414 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 }
416 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 /* Now we can finally clear things. If C had refcounts, we could
419 * assert that the refcount on table is 1 now, i.e. that this function
420 * has unique access to it, so decref side-effects can't alter it.
421 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800422 for (entry = table; used > 0; entry++) {
423 if (entry->key && entry->key != dummy) {
424 used--;
425 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (table_is_malloced)
430 PyMem_DEL(table);
431 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000432}
433
434/*
435 * Iterate over a set table. Use like so:
436 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000437 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000438 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000439 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000440 * while (set_next(yourset, &pos, &entry)) {
441 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000442 * }
443 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000444 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000446 */
447static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 Py_ssize_t i;
451 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200452 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 assert (PyAnySet_Check(so));
455 i = *pos_ptr;
456 assert(i >= 0);
457 table = so->table;
458 mask = so->mask;
459 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
460 i++;
461 *pos_ptr = i+1;
462 if (i > mask)
463 return 0;
464 assert(table[i].key != NULL);
465 *entry_ptr = &table[i];
466 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467}
468
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000469static void
470set_dealloc(PySetObject *so)
471{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200472 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800473 Py_ssize_t used = so->used;
474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 PyObject_GC_UnTrack(so);
476 Py_TRASHCAN_SAFE_BEGIN(so)
477 if (so->weakreflist != NULL)
478 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000479
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800480 for (entry = so->table; used > 0; entry++) {
481 if (entry->key && entry->key != dummy) {
482 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700483 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 }
485 }
486 if (so->table != so->smalltable)
487 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700488 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000490}
491
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000492static PyObject *
493set_repr(PySetObject *so)
494{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200495 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (status != 0) {
499 if (status < 0)
500 return NULL;
501 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
502 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 /* shortcut for the empty set */
505 if (!so->used) {
506 Py_ReprLeave((PyObject*)so);
507 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
508 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 keys = PySequence_List((PyObject *)so);
511 if (keys == NULL)
512 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000513
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200514 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 listrepr = PyObject_Repr(keys);
516 Py_DECREF(keys);
517 if (listrepr == NULL)
518 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200519 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200521 if (tmp == NULL)
522 goto done;
523 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200524
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200525 if (Py_TYPE(so) != &PySet_Type)
526 result = PyUnicode_FromFormat("%s({%U})",
527 Py_TYPE(so)->tp_name,
528 listrepr);
529 else
530 result = PyUnicode_FromFormat("{%U}", listrepr);
531 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000532done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 Py_ReprLeave((PyObject*)so);
534 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000535}
536
Martin v. Löwis18e16552006-02-15 17:27:45 +0000537static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000538set_len(PyObject *so)
539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000541}
542
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000544set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000547 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200548 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700549 setentry *so_entry;
550 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 assert (PyAnySet_Check(so));
553 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 other = (PySetObject*)otherset;
556 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700557 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 return 0;
559 /* Do one big resize at the start, rather than
560 * incrementally resizing as we insert new keys. Expect
561 * that there will be no (or few) overlapping keys.
562 */
563 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
564 if (set_table_resize(so, (so->used + other->used)*2) != 0)
565 return -1;
566 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700567 so_entry = so->table;
568 other_entry = other->table;
569
570 /* If our table is empty, and both tables have the same size, and
571 there are no dummies to eliminate, then just copy the pointers. */
572 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
573 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
574 key = other_entry->key;
575 if (key != NULL) {
576 assert(so_entry->key == NULL);
577 Py_INCREF(key);
578 so_entry->key = key;
579 so_entry->hash = other_entry->hash;
580 }
581 }
582 so->fill = other->fill;
583 so->used = other->used;
584 return 0;
585 }
586
587 /* If our table is empty, we can use set_insert_clean() */
588 if (so->fill == 0) {
589 for (i = 0; i <= other->mask; i++, other_entry++) {
590 key = other_entry->key;
591 if (key != NULL && key != dummy) {
592 Py_INCREF(key);
593 set_insert_clean(so, key, other_entry->hash);
594 }
595 }
596 return 0;
597 }
598
599 /* We can't assure there are no duplicates, so do normal insertions */
Raymond Hettingera3626bc2015-07-15 23:50:14 -0700600 for (i = 0; i <= other->mask; i++) {
601 other_entry = &other->table[i];
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700602 key = other_entry->key;
603 if (key != NULL && key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000604 Py_INCREF(key);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700605 if (set_insert_key(so, key, other_entry->hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000606 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 return -1;
608 }
609 }
610 }
611 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000612}
613
614static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000615set_contains_entry(PySetObject *so, setentry *entry)
616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 PyObject *key;
618 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000619
Raymond Hettinger93035c42015-01-25 16:12:49 -0800620 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (lu_entry == NULL)
622 return -1;
623 key = lu_entry->key;
Yury Selivanov7aa53412015-05-30 10:57:56 -0400624 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000625}
626
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800627static int
628set_contains_key(PySetObject *so, PyObject *key)
629{
630 setentry entry;
631 Py_hash_t hash;
632
633 if (!PyUnicode_CheckExact(key) ||
634 (hash = ((PyASCIIObject *) key)->hash) == -1) {
635 hash = PyObject_Hash(key);
636 if (hash == -1)
637 return -1;
638 }
639 entry.key = key;
640 entry.hash = hash;
641 return set_contains_entry(so, &entry);
642}
643
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000644static PyObject *
645set_pop(PySetObject *so)
646{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800647 /* Make sure the search finger is in bounds */
648 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200649 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 assert (PyAnySet_Check(so));
653 if (so->used == 0) {
654 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
655 return NULL;
656 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000657
Raymond Hettinger1202a472015-01-18 13:12:42 -0800658 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
659 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800660 if (i > so->mask)
661 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 }
663 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800665 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800667 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000669}
670
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000671PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
672Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000673
674static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000675set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 Py_ssize_t pos = 0;
678 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 while (set_next(so, &pos, &entry))
681 Py_VISIT(entry->key);
682 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000683}
684
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000685static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000686frozenset_hash(PyObject *self)
687{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800688 /* Most of the constants in this hash algorithm are randomly choosen
689 large primes with "interesting bit patterns" and that passed
690 tests for good collision statistics on a variety of problematic
691 datasets such as:
692
693 ps = []
694 for r in range(21):
695 ps += itertools.combinations(range(20), r)
696 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
697
698 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800700 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 setentry *entry;
702 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 if (so->hash != -1)
705 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000706
Mark Dickinson57e683e2011-09-24 18:18:40 +0100707 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 while (set_next(so, &pos, &entry)) {
709 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800710 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 combinations of a small number of elements with nearby
712 hashes so that many distinct combinations collapse to only
713 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000714 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800715 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800717 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500718 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800719 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200720 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800721 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 so->hash = hash;
723 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000724}
725
Raymond Hettingera9d99362005-08-05 00:01:15 +0000726/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000727
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000728typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 PyObject_HEAD
730 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
731 Py_ssize_t si_used;
732 Py_ssize_t si_pos;
733 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000734} setiterobject;
735
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000736static void
737setiter_dealloc(setiterobject *si)
738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 Py_XDECREF(si->si_set);
740 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000741}
742
743static int
744setiter_traverse(setiterobject *si, visitproc visit, void *arg)
745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 Py_VISIT(si->si_set);
747 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000748}
749
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000750static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000751setiter_len(setiterobject *si)
752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 Py_ssize_t len = 0;
754 if (si->si_set != NULL && si->si_used == si->si_set->used)
755 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000756 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000757}
758
Armin Rigof5b3e362006-02-11 21:32:43 +0000759PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000760
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000761static PyObject *setiter_iternext(setiterobject *si);
762
763static PyObject *
764setiter_reduce(setiterobject *si)
765{
766 PyObject *list;
767 setiterobject tmp;
768
769 list = PyList_New(0);
770 if (!list)
771 return NULL;
772
Ezio Melotti0e1af282012-09-28 16:43:40 +0300773 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000774 tmp = *si;
775 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300776
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000777 /* iterate the temporary into a list */
778 for(;;) {
779 PyObject *element = setiter_iternext(&tmp);
780 if (element) {
781 if (PyList_Append(list, element)) {
782 Py_DECREF(element);
783 Py_DECREF(list);
784 Py_XDECREF(tmp.si_set);
785 return NULL;
786 }
787 Py_DECREF(element);
788 } else
789 break;
790 }
791 Py_XDECREF(tmp.si_set);
792 /* check for error */
793 if (tmp.si_set != NULL) {
794 /* we have an error */
795 Py_DECREF(list);
796 return NULL;
797 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200798 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000799}
800
801PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
802
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000803static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000805 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807};
808
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000809static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200812 Py_ssize_t i, mask;
813 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 if (so == NULL)
817 return NULL;
818 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 if (si->si_used != so->used) {
821 PyErr_SetString(PyExc_RuntimeError,
822 "Set changed size during iteration");
823 si->si_used = -1; /* Make this state sticky */
824 return NULL;
825 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 i = si->si_pos;
828 assert(i>=0);
829 entry = so->table;
830 mask = so->mask;
831 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
832 i++;
833 si->si_pos = i+1;
834 if (i > mask)
835 goto fail;
836 si->len--;
837 key = entry[i].key;
838 Py_INCREF(key);
839 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000840
841fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 si->si_set = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300843 Py_DECREF(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000845}
846
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000847PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 PyVarObject_HEAD_INIT(&PyType_Type, 0)
849 "set_iterator", /* tp_name */
850 sizeof(setiterobject), /* tp_basicsize */
851 0, /* tp_itemsize */
852 /* methods */
853 (destructor)setiter_dealloc, /* tp_dealloc */
854 0, /* tp_print */
855 0, /* tp_getattr */
856 0, /* tp_setattr */
857 0, /* tp_reserved */
858 0, /* tp_repr */
859 0, /* tp_as_number */
860 0, /* tp_as_sequence */
861 0, /* tp_as_mapping */
862 0, /* tp_hash */
863 0, /* tp_call */
864 0, /* tp_str */
865 PyObject_GenericGetAttr, /* tp_getattro */
866 0, /* tp_setattro */
867 0, /* tp_as_buffer */
868 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
869 0, /* tp_doc */
870 (traverseproc)setiter_traverse, /* tp_traverse */
871 0, /* tp_clear */
872 0, /* tp_richcompare */
873 0, /* tp_weaklistoffset */
874 PyObject_SelfIter, /* tp_iter */
875 (iternextfunc)setiter_iternext, /* tp_iternext */
876 setiter_methods, /* tp_methods */
877 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000878};
879
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880static PyObject *
881set_iter(PySetObject *so)
882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
884 if (si == NULL)
885 return NULL;
886 Py_INCREF(so);
887 si->si_set = so;
888 si->si_used = so->used;
889 si->si_pos = 0;
890 si->len = so->used;
891 _PyObject_GC_TRACK(si);
892 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000893}
894
Raymond Hettingerd7946662005-08-01 21:39:29 +0000895static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000896set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 if (PyAnySet_Check(other))
901 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (PyDict_CheckExact(other)) {
904 PyObject *value;
905 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000906 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 /* Do one big resize at the start, rather than
910 * incrementally resizing as we insert new keys. Expect
911 * that there will be no (or few) overlapping keys.
912 */
913 if (dictsize == -1)
914 return -1;
915 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
916 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
917 return -1;
918 }
919 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
920 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 an_entry.hash = hash;
923 an_entry.key = key;
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700924 if (set_add_entry(so, &an_entry))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 return -1;
926 }
927 return 0;
928 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 it = PyObject_GetIter(other);
931 if (it == NULL)
932 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700935 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 Py_DECREF(it);
937 Py_DECREF(key);
938 return -1;
939 }
940 Py_DECREF(key);
941 }
942 Py_DECREF(it);
943 if (PyErr_Occurred())
944 return -1;
945 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000946}
947
948static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000949set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
954 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700955 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 return NULL;
957 }
958 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000959}
960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000962"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000963
Raymond Hettinger426d9952014-05-18 21:40:20 +0100964/* XXX Todo:
965 If aligned memory allocations become available, make the
966 set object 64 byte aligned so that most of the fields
967 can be retrieved or updated in a single cache line.
968*/
969
Raymond Hettingera38123e2003-11-24 22:18:49 +0000970static PyObject *
971make_new_set(PyTypeObject *type, PyObject *iterable)
972{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200973 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700976 so = (PySetObject *)type->tp_alloc(type, 0);
977 if (so == NULL)
978 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000979
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700980 so->fill = 0;
981 so->used = 0;
982 so->mask = PySet_MINSIZE - 1;
983 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700984 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800985 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700989 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 Py_DECREF(so);
991 return NULL;
992 }
993 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +0000996}
997
Raymond Hettinger7d99f092008-11-16 11:44:54 +0000998static PyObject *
999make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1002 if (PyType_IsSubtype(type, &PySet_Type))
1003 type = &PySet_Type;
1004 else
1005 type = &PyFrozenSet_Type;
1006 }
1007 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001008}
1009
Raymond Hettingerd7946662005-08-01 21:39:29 +00001010/* The empty frozenset is a singleton */
1011static PyObject *emptyfrozenset = NULL;
1012
Raymond Hettingera690a992003-11-16 16:17:49 +00001013static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001014frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1019 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1022 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (type != &PyFrozenSet_Type)
1025 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 if (iterable != NULL) {
1028 /* frozenset(f) is idempotent */
1029 if (PyFrozenSet_CheckExact(iterable)) {
1030 Py_INCREF(iterable);
1031 return iterable;
1032 }
1033 result = make_new_set(type, iterable);
1034 if (result == NULL || PySet_GET_SIZE(result))
1035 return result;
1036 Py_DECREF(result);
1037 }
1038 /* The empty frozenset is a singleton */
1039 if (emptyfrozenset == NULL)
1040 emptyfrozenset = make_new_set(type, NULL);
1041 Py_XINCREF(emptyfrozenset);
1042 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001043}
1044
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001045int
1046PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001047{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001048 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001049}
1050
1051void
1052PySet_Fini(void)
1053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001055}
1056
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001057static PyObject *
1058set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1061 return NULL;
1062
1063 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001064}
1065
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001066/* set_swap_bodies() switches the contents of any two sets by moving their
1067 internal data pointers and, if needed, copying the internal smalltables.
1068 Semantically equivalent to:
1069
1070 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1071
1072 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001073 Useful for operations that update in-place (by allowing an intermediate
1074 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001075*/
1076
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001077static void
1078set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 Py_ssize_t t;
1081 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001083 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 t = a->fill; a->fill = b->fill; b->fill = t;
1086 t = a->used; a->used = b->used; b->used = t;
1087 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 u = a->table;
1090 if (a->table == a->smalltable)
1091 u = b->smalltable;
1092 a->table = b->table;
1093 if (b->table == b->smalltable)
1094 a->table = a->smalltable;
1095 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (a->table == a->smalltable || b->table == b->smalltable) {
1098 memcpy(tab, a->smalltable, sizeof(tab));
1099 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1100 memcpy(b->smalltable, tab, sizeof(tab));
1101 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1104 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1105 h = a->hash; a->hash = b->hash; b->hash = h;
1106 } else {
1107 a->hash = -1;
1108 b->hash = -1;
1109 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001110}
1111
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001112static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001113set_copy(PySetObject *so)
1114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001116}
1117
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001118static PyObject *
1119frozenset_copy(PySetObject *so)
1120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (PyFrozenSet_CheckExact(so)) {
1122 Py_INCREF(so);
1123 return (PyObject *)so;
1124 }
1125 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001126}
1127
Raymond Hettingera690a992003-11-16 16:17:49 +00001128PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1129
1130static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001131set_clear(PySetObject *so)
1132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 set_clear_internal(so);
1134 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001135}
1136
1137PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1138
1139static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001140set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 PySetObject *result;
1143 PyObject *other;
1144 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 result = (PySetObject *)set_copy(so);
1147 if (result == NULL)
1148 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1151 other = PyTuple_GET_ITEM(args, i);
1152 if ((PyObject *)so == other)
1153 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001154 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 Py_DECREF(result);
1156 return NULL;
1157 }
1158 }
1159 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001160}
1161
1162PyDoc_STRVAR(union_doc,
1163 "Return the union of sets as a new set.\n\
1164\n\
1165(i.e. all elements that are in either set.)");
1166
1167static PyObject *
1168set_or(PySetObject *so, PyObject *other)
1169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001171
Brian Curtindfc80e32011-08-10 20:28:54 -05001172 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1173 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 result = (PySetObject *)set_copy(so);
1176 if (result == NULL)
1177 return NULL;
1178 if ((PyObject *)so == other)
1179 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001180 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 Py_DECREF(result);
1182 return NULL;
1183 }
1184 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001185}
1186
Raymond Hettingera690a992003-11-16 16:17:49 +00001187static PyObject *
1188set_ior(PySetObject *so, PyObject *other)
1189{
Brian Curtindfc80e32011-08-10 20:28:54 -05001190 if (!PyAnySet_Check(other))
1191 Py_RETURN_NOTIMPLEMENTED;
1192
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001193 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 return NULL;
1195 Py_INCREF(so);
1196 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001197}
1198
1199static PyObject *
1200set_intersection(PySetObject *so, PyObject *other)
1201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 PySetObject *result;
1203 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 if ((PyObject *)so == other)
1206 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1209 if (result == NULL)
1210 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 if (PyAnySet_Check(other)) {
1213 Py_ssize_t pos = 0;
1214 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1217 tmp = (PyObject *)so;
1218 so = (PySetObject *)other;
1219 other = tmp;
1220 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 while (set_next((PySetObject *)other, &pos, &entry)) {
1223 int rv = set_contains_entry(so, entry);
1224 if (rv == -1) {
1225 Py_DECREF(result);
1226 return NULL;
1227 }
1228 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001229 if (set_add_entry(result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 Py_DECREF(result);
1231 return NULL;
1232 }
1233 }
1234 }
1235 return (PyObject *)result;
1236 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 it = PyObject_GetIter(other);
1239 if (it == NULL) {
1240 Py_DECREF(result);
1241 return NULL;
1242 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 while ((key = PyIter_Next(it)) != NULL) {
1245 int rv;
1246 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001247 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 if (hash == -1) {
1250 Py_DECREF(it);
1251 Py_DECREF(result);
1252 Py_DECREF(key);
1253 return NULL;
1254 }
1255 entry.hash = hash;
1256 entry.key = key;
1257 rv = set_contains_entry(so, &entry);
1258 if (rv == -1) {
1259 Py_DECREF(it);
1260 Py_DECREF(result);
1261 Py_DECREF(key);
1262 return NULL;
1263 }
1264 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001265 if (set_add_entry(result, &entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 Py_DECREF(it);
1267 Py_DECREF(result);
1268 Py_DECREF(key);
1269 return NULL;
1270 }
1271 }
1272 Py_DECREF(key);
1273 }
1274 Py_DECREF(it);
1275 if (PyErr_Occurred()) {
1276 Py_DECREF(result);
1277 return NULL;
1278 }
1279 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001280}
1281
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001282static PyObject *
1283set_intersection_multi(PySetObject *so, PyObject *args)
1284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 Py_ssize_t i;
1286 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (PyTuple_GET_SIZE(args) == 0)
1289 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 Py_INCREF(so);
1292 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1293 PyObject *other = PyTuple_GET_ITEM(args, i);
1294 PyObject *newresult = set_intersection((PySetObject *)result, other);
1295 if (newresult == NULL) {
1296 Py_DECREF(result);
1297 return NULL;
1298 }
1299 Py_DECREF(result);
1300 result = newresult;
1301 }
1302 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001303}
1304
Raymond Hettingera690a992003-11-16 16:17:49 +00001305PyDoc_STRVAR(intersection_doc,
1306"Return the intersection of two sets as a new set.\n\
1307\n\
1308(i.e. all elements that are in both sets.)");
1309
1310static PyObject *
1311set_intersection_update(PySetObject *so, PyObject *other)
1312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 tmp = set_intersection(so, other);
1316 if (tmp == NULL)
1317 return NULL;
1318 set_swap_bodies(so, (PySetObject *)tmp);
1319 Py_DECREF(tmp);
1320 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001321}
1322
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001323static PyObject *
1324set_intersection_update_multi(PySetObject *so, PyObject *args)
1325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 tmp = set_intersection_multi(so, args);
1329 if (tmp == NULL)
1330 return NULL;
1331 set_swap_bodies(so, (PySetObject *)tmp);
1332 Py_DECREF(tmp);
1333 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001334}
1335
Raymond Hettingera690a992003-11-16 16:17:49 +00001336PyDoc_STRVAR(intersection_update_doc,
1337"Update a set with the intersection of itself and another.");
1338
1339static PyObject *
1340set_and(PySetObject *so, PyObject *other)
1341{
Brian Curtindfc80e32011-08-10 20:28:54 -05001342 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1343 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001345}
1346
1347static PyObject *
1348set_iand(PySetObject *so, PyObject *other)
1349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001351
Brian Curtindfc80e32011-08-10 20:28:54 -05001352 if (!PyAnySet_Check(other))
1353 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 result = set_intersection_update(so, other);
1355 if (result == NULL)
1356 return NULL;
1357 Py_DECREF(result);
1358 Py_INCREF(so);
1359 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001360}
1361
Guido van Rossum58da9312007-11-10 23:39:45 +00001362static PyObject *
1363set_isdisjoint(PySetObject *so, PyObject *other)
1364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 if ((PyObject *)so == other) {
1368 if (PySet_GET_SIZE(so) == 0)
1369 Py_RETURN_TRUE;
1370 else
1371 Py_RETURN_FALSE;
1372 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (PyAnySet_CheckExact(other)) {
1375 Py_ssize_t pos = 0;
1376 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1379 tmp = (PyObject *)so;
1380 so = (PySetObject *)other;
1381 other = tmp;
1382 }
1383 while (set_next((PySetObject *)other, &pos, &entry)) {
1384 int rv = set_contains_entry(so, entry);
1385 if (rv == -1)
1386 return NULL;
1387 if (rv)
1388 Py_RETURN_FALSE;
1389 }
1390 Py_RETURN_TRUE;
1391 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 it = PyObject_GetIter(other);
1394 if (it == NULL)
1395 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 while ((key = PyIter_Next(it)) != NULL) {
1398 int rv;
1399 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001400 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if (hash == -1) {
1403 Py_DECREF(key);
1404 Py_DECREF(it);
1405 return NULL;
1406 }
1407 entry.hash = hash;
1408 entry.key = key;
1409 rv = set_contains_entry(so, &entry);
1410 Py_DECREF(key);
1411 if (rv == -1) {
1412 Py_DECREF(it);
1413 return NULL;
1414 }
1415 if (rv) {
1416 Py_DECREF(it);
1417 Py_RETURN_FALSE;
1418 }
1419 }
1420 Py_DECREF(it);
1421 if (PyErr_Occurred())
1422 return NULL;
1423 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001424}
1425
1426PyDoc_STRVAR(isdisjoint_doc,
1427"Return True if two sets have a null intersection.");
1428
Neal Norwitz6576bd82005-11-13 18:41:28 +00001429static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001430set_difference_update_internal(PySetObject *so, PyObject *other)
1431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if ((PyObject *)so == other)
1433 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (PyAnySet_Check(other)) {
1436 setentry *entry;
1437 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 while (set_next((PySetObject *)other, &pos, &entry))
1440 if (set_discard_entry(so, entry) == -1)
1441 return -1;
1442 } else {
1443 PyObject *key, *it;
1444 it = PyObject_GetIter(other);
1445 if (it == NULL)
1446 return -1;
1447
1448 while ((key = PyIter_Next(it)) != NULL) {
1449 if (set_discard_key(so, key) == -1) {
1450 Py_DECREF(it);
1451 Py_DECREF(key);
1452 return -1;
1453 }
1454 Py_DECREF(key);
1455 }
1456 Py_DECREF(it);
1457 if (PyErr_Occurred())
1458 return -1;
1459 }
1460 /* If more than 1/5 are dummies, then resize them away. */
1461 if ((so->fill - so->used) * 5 < so->mask)
1462 return 0;
1463 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001464}
1465
Raymond Hettingera690a992003-11-16 16:17:49 +00001466static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001467set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1472 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001473 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 return NULL;
1475 }
1476 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001477}
1478
1479PyDoc_STRVAR(difference_update_doc,
1480"Remove all elements of another set from this set.");
1481
1482static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001483set_copy_and_difference(PySetObject *so, PyObject *other)
1484{
1485 PyObject *result;
1486
1487 result = set_copy(so);
1488 if (result == NULL)
1489 return NULL;
1490 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1491 return result;
1492 Py_DECREF(result);
1493 return NULL;
1494}
1495
1496static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001497set_difference(PySetObject *so, PyObject *other)
1498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 PyObject *result;
1500 setentry *entry;
1501 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001504 return set_copy_and_difference(so, other);
1505 }
1506
1507 /* If len(so) much more than len(other), it's more efficient to simply copy
1508 * so and then iterate other looking for common elements. */
1509 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1510 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 result = make_new_set_basetype(Py_TYPE(so), NULL);
1514 if (result == NULL)
1515 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 if (PyDict_CheckExact(other)) {
1518 while (set_next(so, &pos, &entry)) {
1519 setentry entrycopy;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001520 int rv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 entrycopy.hash = entry->hash;
1522 entrycopy.key = entry->key;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001523 rv = _PyDict_Contains(other, entry->key, entry->hash);
1524 if (rv < 0) {
1525 Py_DECREF(result);
1526 return NULL;
1527 }
1528 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001529 if (set_add_entry((PySetObject *)result, &entrycopy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 Py_DECREF(result);
1531 return NULL;
1532 }
1533 }
1534 }
1535 return result;
1536 }
1537
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001538 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 while (set_next(so, &pos, &entry)) {
1540 int rv = set_contains_entry((PySetObject *)other, entry);
1541 if (rv == -1) {
1542 Py_DECREF(result);
1543 return NULL;
1544 }
1545 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001546 if (set_add_entry((PySetObject *)result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 Py_DECREF(result);
1548 return NULL;
1549 }
1550 }
1551 }
1552 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001553}
1554
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001555static PyObject *
1556set_difference_multi(PySetObject *so, PyObject *args)
1557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 Py_ssize_t i;
1559 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 if (PyTuple_GET_SIZE(args) == 0)
1562 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 other = PyTuple_GET_ITEM(args, 0);
1565 result = set_difference(so, other);
1566 if (result == NULL)
1567 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1570 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001571 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 Py_DECREF(result);
1573 return NULL;
1574 }
1575 }
1576 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001577}
1578
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001579PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001580"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001581\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001582(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001583static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001584set_sub(PySetObject *so, PyObject *other)
1585{
Brian Curtindfc80e32011-08-10 20:28:54 -05001586 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1587 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001589}
1590
1591static PyObject *
1592set_isub(PySetObject *so, PyObject *other)
1593{
Brian Curtindfc80e32011-08-10 20:28:54 -05001594 if (!PyAnySet_Check(other))
1595 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001596 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 return NULL;
1598 Py_INCREF(so);
1599 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001600}
1601
1602static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001603set_symmetric_difference_update(PySetObject *so, PyObject *other)
1604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 PySetObject *otherset;
1606 PyObject *key;
1607 Py_ssize_t pos = 0;
1608 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if ((PyObject *)so == other)
1611 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if (PyDict_CheckExact(other)) {
1614 PyObject *value;
1615 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001616 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1618 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001619
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001620 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 an_entry.hash = hash;
1622 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001625 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001626 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001629 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001630 if (set_add_entry(so, &an_entry)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001631 Py_DECREF(key);
1632 return NULL;
1633 }
1634 }
Georg Brandl2d444492010-09-03 10:52:55 +00001635 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 }
1637 Py_RETURN_NONE;
1638 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (PyAnySet_Check(other)) {
1641 Py_INCREF(other);
1642 otherset = (PySetObject *)other;
1643 } else {
1644 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1645 if (otherset == NULL)
1646 return NULL;
1647 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 while (set_next(otherset, &pos, &entry)) {
1650 int rv = set_discard_entry(so, entry);
1651 if (rv == -1) {
1652 Py_DECREF(otherset);
1653 return NULL;
1654 }
1655 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001656 if (set_add_entry(so, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 Py_DECREF(otherset);
1658 return NULL;
1659 }
1660 }
1661 }
1662 Py_DECREF(otherset);
1663 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001664}
1665
1666PyDoc_STRVAR(symmetric_difference_update_doc,
1667"Update a set with the symmetric difference of itself and another.");
1668
1669static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001670set_symmetric_difference(PySetObject *so, PyObject *other)
1671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 PyObject *rv;
1673 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1676 if (otherset == NULL)
1677 return NULL;
1678 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1679 if (rv == NULL)
1680 return NULL;
1681 Py_DECREF(rv);
1682 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001683}
1684
1685PyDoc_STRVAR(symmetric_difference_doc,
1686"Return the symmetric difference of two sets as a new set.\n\
1687\n\
1688(i.e. all elements that are in exactly one of the sets.)");
1689
1690static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001691set_xor(PySetObject *so, PyObject *other)
1692{
Brian Curtindfc80e32011-08-10 20:28:54 -05001693 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1694 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001696}
1697
1698static PyObject *
1699set_ixor(PySetObject *so, PyObject *other)
1700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001702
Brian Curtindfc80e32011-08-10 20:28:54 -05001703 if (!PyAnySet_Check(other))
1704 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 result = set_symmetric_difference_update(so, other);
1706 if (result == NULL)
1707 return NULL;
1708 Py_DECREF(result);
1709 Py_INCREF(so);
1710 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001711}
1712
1713static PyObject *
1714set_issubset(PySetObject *so, PyObject *other)
1715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 setentry *entry;
1717 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 if (!PyAnySet_Check(other)) {
1720 PyObject *tmp, *result;
1721 tmp = make_new_set(&PySet_Type, other);
1722 if (tmp == NULL)
1723 return NULL;
1724 result = set_issubset(so, tmp);
1725 Py_DECREF(tmp);
1726 return result;
1727 }
1728 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1729 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 while (set_next(so, &pos, &entry)) {
1732 int rv = set_contains_entry((PySetObject *)other, entry);
1733 if (rv == -1)
1734 return NULL;
1735 if (!rv)
1736 Py_RETURN_FALSE;
1737 }
1738 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001739}
1740
1741PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1742
1743static PyObject *
1744set_issuperset(PySetObject *so, PyObject *other)
1745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 if (!PyAnySet_Check(other)) {
1749 tmp = make_new_set(&PySet_Type, other);
1750 if (tmp == NULL)
1751 return NULL;
1752 result = set_issuperset(so, tmp);
1753 Py_DECREF(tmp);
1754 return result;
1755 }
1756 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001757}
1758
1759PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1760
Raymond Hettingera690a992003-11-16 16:17:49 +00001761static PyObject *
1762set_richcompare(PySetObject *v, PyObject *w, int op)
1763{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001764 PyObject *r1;
1765 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001766
Brian Curtindfc80e32011-08-10 20:28:54 -05001767 if(!PyAnySet_Check(w))
1768 Py_RETURN_NOTIMPLEMENTED;
1769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 switch (op) {
1771 case Py_EQ:
1772 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1773 Py_RETURN_FALSE;
1774 if (v->hash != -1 &&
1775 ((PySetObject *)w)->hash != -1 &&
1776 v->hash != ((PySetObject *)w)->hash)
1777 Py_RETURN_FALSE;
1778 return set_issubset(v, w);
1779 case Py_NE:
1780 r1 = set_richcompare(v, w, Py_EQ);
1781 if (r1 == NULL)
1782 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001783 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001785 if (r2 < 0)
1786 return NULL;
1787 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 case Py_LE:
1789 return set_issubset(v, w);
1790 case Py_GE:
1791 return set_issuperset(v, w);
1792 case Py_LT:
1793 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1794 Py_RETURN_FALSE;
1795 return set_issubset(v, w);
1796 case Py_GT:
1797 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1798 Py_RETURN_FALSE;
1799 return set_issuperset(v, w);
1800 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001801 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001802}
1803
Raymond Hettingera690a992003-11-16 16:17:49 +00001804static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001805set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001806{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001807 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 return NULL;
1809 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001810}
1811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001813"Add an element to a set.\n\
1814\n\
1815This has no effect if the element is already present.");
1816
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001817static int
1818set_contains(PySetObject *so, PyObject *key)
1819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 PyObject *tmpkey;
1821 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 rv = set_contains_key(so, key);
1824 if (rv == -1) {
1825 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1826 return -1;
1827 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001828 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 if (tmpkey == NULL)
1830 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001831 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 Py_DECREF(tmpkey);
1833 }
1834 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001835}
1836
1837static PyObject *
1838set_direct_contains(PySetObject *so, PyObject *key)
1839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 result = set_contains(so, key);
1843 if (result == -1)
1844 return NULL;
1845 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001846}
1847
1848PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1849
Raymond Hettingera690a992003-11-16 16:17:49 +00001850static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001851set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 PyObject *tmpkey;
1854 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 rv = set_discard_key(so, key);
1857 if (rv == -1) {
1858 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1859 return NULL;
1860 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001861 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 if (tmpkey == NULL)
1863 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 Py_DECREF(tmpkey);
1866 if (rv == -1)
1867 return NULL;
1868 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001871 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 return NULL;
1873 }
1874 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001875}
1876
1877PyDoc_STRVAR(remove_doc,
1878"Remove an element from a set; it must be a member.\n\
1879\n\
1880If the element is not a member, raise a KeyError.");
1881
1882static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001883set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001884{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001885 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 rv = set_discard_key(so, key);
1889 if (rv == -1) {
1890 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1891 return NULL;
1892 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001893 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 if (tmpkey == NULL)
1895 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001896 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001898 if (rv == -1)
1899 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 }
1901 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001902}
1903
1904PyDoc_STRVAR(discard_doc,
1905"Remove an element from a set if it is a member.\n\
1906\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001908
1909static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001910set_reduce(PySetObject *so)
1911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001913 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 keys = PySequence_List((PyObject *)so);
1916 if (keys == NULL)
1917 goto done;
1918 args = PyTuple_Pack(1, keys);
1919 if (args == NULL)
1920 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001921 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 if (dict == NULL) {
1923 PyErr_Clear();
1924 dict = Py_None;
1925 Py_INCREF(dict);
1926 }
1927 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001928done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 Py_XDECREF(args);
1930 Py_XDECREF(keys);
1931 Py_XDECREF(dict);
1932 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001933}
1934
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001935static PyObject *
1936set_sizeof(PySetObject *so)
1937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001939
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001940 res = _PyObject_SIZE(Py_TYPE(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 if (so->table != so->smalltable)
1942 res = res + (so->mask + 1) * sizeof(setentry);
1943 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001944}
1945
1946PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001947static int
1948set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (!PyAnySet_Check(self))
1953 return -1;
1954 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1955 return -1;
1956 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1957 return -1;
1958 set_clear_internal(self);
1959 self->hash = -1;
1960 if (iterable == NULL)
1961 return 0;
1962 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001963}
1964
Raymond Hettingera690a992003-11-16 16:17:49 +00001965static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 set_len, /* sq_length */
1967 0, /* sq_concat */
1968 0, /* sq_repeat */
1969 0, /* sq_item */
1970 0, /* sq_slice */
1971 0, /* sq_ass_item */
1972 0, /* sq_ass_slice */
1973 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001974};
1975
1976/* set object ********************************************************/
1977
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001978#ifdef Py_DEBUG
1979static PyObject *test_c_api(PySetObject *so);
1980
1981PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1982All is well if assertions don't fail.");
1983#endif
1984
Raymond Hettingera690a992003-11-16 16:17:49 +00001985static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 {"add", (PyCFunction)set_add, METH_O,
1987 add_doc},
1988 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1989 clear_doc},
1990 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1991 contains_doc},
1992 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1993 copy_doc},
1994 {"discard", (PyCFunction)set_discard, METH_O,
1995 discard_doc},
1996 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
1997 difference_doc},
1998 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
1999 difference_update_doc},
2000 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2001 intersection_doc},
2002 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2003 intersection_update_doc},
2004 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2005 isdisjoint_doc},
2006 {"issubset", (PyCFunction)set_issubset, METH_O,
2007 issubset_doc},
2008 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2009 issuperset_doc},
2010 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2011 pop_doc},
2012 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2013 reduce_doc},
2014 {"remove", (PyCFunction)set_remove, METH_O,
2015 remove_doc},
2016 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2017 sizeof_doc},
2018 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2019 symmetric_difference_doc},
2020 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2021 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002022#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2024 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002025#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 {"union", (PyCFunction)set_union, METH_VARARGS,
2027 union_doc},
2028 {"update", (PyCFunction)set_update, METH_VARARGS,
2029 update_doc},
2030 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002031};
2032
2033static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 0, /*nb_add*/
2035 (binaryfunc)set_sub, /*nb_subtract*/
2036 0, /*nb_multiply*/
2037 0, /*nb_remainder*/
2038 0, /*nb_divmod*/
2039 0, /*nb_power*/
2040 0, /*nb_negative*/
2041 0, /*nb_positive*/
2042 0, /*nb_absolute*/
2043 0, /*nb_bool*/
2044 0, /*nb_invert*/
2045 0, /*nb_lshift*/
2046 0, /*nb_rshift*/
2047 (binaryfunc)set_and, /*nb_and*/
2048 (binaryfunc)set_xor, /*nb_xor*/
2049 (binaryfunc)set_or, /*nb_or*/
2050 0, /*nb_int*/
2051 0, /*nb_reserved*/
2052 0, /*nb_float*/
2053 0, /*nb_inplace_add*/
2054 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2055 0, /*nb_inplace_multiply*/
2056 0, /*nb_inplace_remainder*/
2057 0, /*nb_inplace_power*/
2058 0, /*nb_inplace_lshift*/
2059 0, /*nb_inplace_rshift*/
2060 (binaryfunc)set_iand, /*nb_inplace_and*/
2061 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2062 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002063};
2064
2065PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002066"set() -> new empty set object\n\
2067set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002068\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002069Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002070
2071PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2073 "set", /* tp_name */
2074 sizeof(PySetObject), /* tp_basicsize */
2075 0, /* tp_itemsize */
2076 /* methods */
2077 (destructor)set_dealloc, /* tp_dealloc */
2078 0, /* tp_print */
2079 0, /* tp_getattr */
2080 0, /* tp_setattr */
2081 0, /* tp_reserved */
2082 (reprfunc)set_repr, /* tp_repr */
2083 &set_as_number, /* tp_as_number */
2084 &set_as_sequence, /* tp_as_sequence */
2085 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002086 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 0, /* tp_call */
2088 0, /* tp_str */
2089 PyObject_GenericGetAttr, /* tp_getattro */
2090 0, /* tp_setattro */
2091 0, /* tp_as_buffer */
2092 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2093 Py_TPFLAGS_BASETYPE, /* tp_flags */
2094 set_doc, /* tp_doc */
2095 (traverseproc)set_traverse, /* tp_traverse */
2096 (inquiry)set_clear_internal, /* tp_clear */
2097 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002098 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2099 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 0, /* tp_iternext */
2101 set_methods, /* tp_methods */
2102 0, /* tp_members */
2103 0, /* tp_getset */
2104 0, /* tp_base */
2105 0, /* tp_dict */
2106 0, /* tp_descr_get */
2107 0, /* tp_descr_set */
2108 0, /* tp_dictoffset */
2109 (initproc)set_init, /* tp_init */
2110 PyType_GenericAlloc, /* tp_alloc */
2111 set_new, /* tp_new */
2112 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002113};
2114
2115/* frozenset object ********************************************************/
2116
2117
2118static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2120 contains_doc},
2121 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2122 copy_doc},
2123 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2124 difference_doc},
2125 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2126 intersection_doc},
2127 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2128 isdisjoint_doc},
2129 {"issubset", (PyCFunction)set_issubset, METH_O,
2130 issubset_doc},
2131 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2132 issuperset_doc},
2133 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2134 reduce_doc},
2135 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2136 sizeof_doc},
2137 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2138 symmetric_difference_doc},
2139 {"union", (PyCFunction)set_union, METH_VARARGS,
2140 union_doc},
2141 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002142};
2143
2144static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 0, /*nb_add*/
2146 (binaryfunc)set_sub, /*nb_subtract*/
2147 0, /*nb_multiply*/
2148 0, /*nb_remainder*/
2149 0, /*nb_divmod*/
2150 0, /*nb_power*/
2151 0, /*nb_negative*/
2152 0, /*nb_positive*/
2153 0, /*nb_absolute*/
2154 0, /*nb_bool*/
2155 0, /*nb_invert*/
2156 0, /*nb_lshift*/
2157 0, /*nb_rshift*/
2158 (binaryfunc)set_and, /*nb_and*/
2159 (binaryfunc)set_xor, /*nb_xor*/
2160 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002161};
2162
2163PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002164"frozenset() -> empty frozenset object\n\
2165frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002166\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002167Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002168
2169PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2171 "frozenset", /* tp_name */
2172 sizeof(PySetObject), /* tp_basicsize */
2173 0, /* tp_itemsize */
2174 /* methods */
2175 (destructor)set_dealloc, /* tp_dealloc */
2176 0, /* tp_print */
2177 0, /* tp_getattr */
2178 0, /* tp_setattr */
2179 0, /* tp_reserved */
2180 (reprfunc)set_repr, /* tp_repr */
2181 &frozenset_as_number, /* tp_as_number */
2182 &set_as_sequence, /* tp_as_sequence */
2183 0, /* tp_as_mapping */
2184 frozenset_hash, /* tp_hash */
2185 0, /* tp_call */
2186 0, /* tp_str */
2187 PyObject_GenericGetAttr, /* tp_getattro */
2188 0, /* tp_setattro */
2189 0, /* tp_as_buffer */
2190 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2191 Py_TPFLAGS_BASETYPE, /* tp_flags */
2192 frozenset_doc, /* tp_doc */
2193 (traverseproc)set_traverse, /* tp_traverse */
2194 (inquiry)set_clear_internal, /* tp_clear */
2195 (richcmpfunc)set_richcompare, /* tp_richcompare */
2196 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2197 (getiterfunc)set_iter, /* tp_iter */
2198 0, /* tp_iternext */
2199 frozenset_methods, /* tp_methods */
2200 0, /* tp_members */
2201 0, /* tp_getset */
2202 0, /* tp_base */
2203 0, /* tp_dict */
2204 0, /* tp_descr_get */
2205 0, /* tp_descr_set */
2206 0, /* tp_dictoffset */
2207 0, /* tp_init */
2208 PyType_GenericAlloc, /* tp_alloc */
2209 frozenset_new, /* tp_new */
2210 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002211};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002212
2213
2214/***** C API functions *************************************************/
2215
2216PyObject *
2217PySet_New(PyObject *iterable)
2218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002220}
2221
2222PyObject *
2223PyFrozenSet_New(PyObject *iterable)
2224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002226}
2227
Neal Norwitz8c49c822006-03-04 18:41:19 +00002228Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002229PySet_Size(PyObject *anyset)
2230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 if (!PyAnySet_Check(anyset)) {
2232 PyErr_BadInternalCall();
2233 return -1;
2234 }
2235 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002236}
2237
2238int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002239PySet_Clear(PyObject *set)
2240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 if (!PySet_Check(set)) {
2242 PyErr_BadInternalCall();
2243 return -1;
2244 }
2245 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002246}
2247
2248int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002249PySet_Contains(PyObject *anyset, PyObject *key)
2250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 if (!PyAnySet_Check(anyset)) {
2252 PyErr_BadInternalCall();
2253 return -1;
2254 }
2255 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002256}
2257
2258int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002259PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 if (!PySet_Check(set)) {
2262 PyErr_BadInternalCall();
2263 return -1;
2264 }
2265 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002266}
2267
2268int
Christian Heimesfd66e512008-01-29 12:18:50 +00002269PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 if (!PySet_Check(anyset) &&
2272 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2273 PyErr_BadInternalCall();
2274 return -1;
2275 }
2276 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002277}
2278
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002279int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002280_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 if (!PyAnySet_Check(set)) {
2285 PyErr_BadInternalCall();
2286 return -1;
2287 }
2288 if (set_next((PySetObject *)set, pos, &entry) == 0)
2289 return 0;
2290 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002291 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002293}
2294
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002295PyObject *
2296PySet_Pop(PyObject *set)
2297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 if (!PySet_Check(set)) {
2299 PyErr_BadInternalCall();
2300 return NULL;
2301 }
2302 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002303}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002304
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002305int
2306_PySet_Update(PyObject *set, PyObject *iterable)
2307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 if (!PySet_Check(set)) {
2309 PyErr_BadInternalCall();
2310 return -1;
2311 }
2312 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002313}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002314
Raymond Hettingere259f132013-12-15 11:56:14 -08002315/* Exported for the gdb plugin's benefit. */
2316PyObject *_PySet_Dummy = dummy;
2317
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002318#ifdef Py_DEBUG
2319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002321 Returns True and original set is restored. */
2322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323#define assertRaises(call_return_value, exception) \
2324 do { \
2325 assert(call_return_value); \
2326 assert(PyErr_ExceptionMatches(exception)); \
2327 PyErr_Clear(); \
2328 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002329
2330static PyObject *
2331test_c_api(PySetObject *so)
2332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 Py_ssize_t count;
2334 char *s;
2335 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002336 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002338 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 /* Verify preconditions */
2342 assert(PyAnySet_Check(ob));
2343 assert(PyAnySet_CheckExact(ob));
2344 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 /* so.clear(); so |= set("abc"); */
2347 str = PyUnicode_FromString("abc");
2348 if (str == NULL)
2349 return NULL;
2350 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002351 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 Py_DECREF(str);
2353 return NULL;
2354 }
2355 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 /* Exercise type/size checks */
2358 assert(PySet_Size(ob) == 3);
2359 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 /* Raise TypeError for non-iterable constructor arguments */
2362 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2363 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* Raise TypeError for unhashable key */
2366 dup = PySet_New(ob);
2367 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2368 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2369 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 /* Exercise successful pop, contains, add, and discard */
2372 elem = PySet_Pop(ob);
2373 assert(PySet_Contains(ob, elem) == 0);
2374 assert(PySet_GET_SIZE(ob) == 2);
2375 assert(PySet_Add(ob, elem) == 0);
2376 assert(PySet_Contains(ob, elem) == 1);
2377 assert(PySet_GET_SIZE(ob) == 3);
2378 assert(PySet_Discard(ob, elem) == 1);
2379 assert(PySet_GET_SIZE(ob) == 2);
2380 assert(PySet_Discard(ob, elem) == 0);
2381 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 /* Exercise clear */
2384 dup2 = PySet_New(dup);
2385 assert(PySet_Clear(dup2) == 0);
2386 assert(PySet_Size(dup2) == 0);
2387 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 /* Raise SystemError on clear or update of frozen set */
2390 f = PyFrozenSet_New(dup);
2391 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2392 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2393 assert(PySet_Add(f, elem) == 0);
2394 Py_INCREF(f);
2395 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2396 Py_DECREF(f);
2397 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 /* Exercise direct iteration */
2400 i = 0, count = 0;
2401 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2402 s = _PyUnicode_AsString(x);
2403 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2404 count++;
2405 }
2406 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 /* Exercise updates */
2409 dup2 = PySet_New(NULL);
2410 assert(_PySet_Update(dup2, dup) == 0);
2411 assert(PySet_Size(dup2) == 3);
2412 assert(_PySet_Update(dup2, dup) == 0);
2413 assert(PySet_Size(dup2) == 3);
2414 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* Raise SystemError when self argument is not a set or frozenset. */
2417 t = PyTuple_New(0);
2418 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2419 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2420 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 /* Raise SystemError when self argument is not a set. */
2423 f = PyFrozenSet_New(dup);
2424 assert(PySet_Size(f) == 3);
2425 assert(PyFrozenSet_CheckExact(f));
2426 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2427 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2428 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 /* Raise KeyError when popping from an empty set */
2431 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2432 Py_DECREF(ob);
2433 assert(PySet_GET_SIZE(ob) == 0);
2434 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 /* Restore the set from the copy using the PyNumber API */
2437 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2438 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 /* Verify constructors accept NULL arguments */
2441 f = PySet_New(NULL);
2442 assert(f != NULL);
2443 assert(PySet_GET_SIZE(f) == 0);
2444 Py_DECREF(f);
2445 f = PyFrozenSet_New(NULL);
2446 assert(f != NULL);
2447 assert(PyFrozenSet_CheckExact(f));
2448 assert(PySet_GET_SIZE(f) == 0);
2449 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 Py_DECREF(elem);
2452 Py_DECREF(dup);
2453 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002454}
2455
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002456#undef assertRaises
2457
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002458#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002459
2460/***** Dummy Struct *************************************************/
2461
2462static PyObject *
2463dummy_repr(PyObject *op)
2464{
2465 return PyUnicode_FromString("<dummy key>");
2466}
2467
2468static void
2469dummy_dealloc(PyObject* ignore)
2470{
2471 Py_FatalError("deallocating <dummy key>");
2472}
2473
2474static PyTypeObject _PySetDummy_Type = {
2475 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2476 "<dummy key> type",
2477 0,
2478 0,
2479 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2480 0, /*tp_print*/
2481 0, /*tp_getattr*/
2482 0, /*tp_setattr*/
2483 0, /*tp_reserved*/
2484 dummy_repr, /*tp_repr*/
2485 0, /*tp_as_number*/
2486 0, /*tp_as_sequence*/
2487 0, /*tp_as_mapping*/
2488 0, /*tp_hash */
2489 0, /*tp_call */
2490 0, /*tp_str */
2491 0, /*tp_getattro */
2492 0, /*tp_setattro */
2493 0, /*tp_as_buffer */
2494 Py_TPFLAGS_DEFAULT, /*tp_flags */
2495};
2496
2497static PyObject _dummy_struct = {
2498 _PyObject_EXTRA_INIT
2499 2, &_PySetDummy_Type
2500};
2501