blob: d62164773bc2acd7b76eb6d7bef4fca01c372677 [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 Hettingerbd9b2002015-01-18 16:10:30 -08007 Copyright (c) 2003-2015 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
Raymond Hettinger4d45c102015-01-25 16:27:40 -080026 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070027 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020055 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070056 size_t perturb = hash;
57 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080058 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070059 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070060 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080062 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070063 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070065
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070066 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080067 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070068 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080069 /* startkey cannot be a dummy because the dummy hash field is -1 */
70 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080071 if (startkey == key)
72 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080073 if (PyUnicode_CheckExact(startkey)
74 && PyUnicode_CheckExact(key)
75 && unicode_eq(startkey, key))
76 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 Py_INCREF(startkey);
78 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
79 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010080 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010082 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010084 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070085 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060086 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070087 }
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++;
Raymond Hettinger8651a502015-05-27 10:37:20 -070092 if (entry->hash == 0 && entry->key == NULL)
93 return entry;
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 Hettinger8651a502015-05-27 10:37:20 -0700114 }
115 }
116
117 perturb >>= PERTURB_SHIFT;
118 i = (i * 5 + 1 + perturb) & mask;
119
120 entry = &table[i];
121 if (entry->hash == 0 && entry->key == NULL)
122 return entry;
123 }
124}
125
126/*
127Internal routine to insert a new key into the table.
128Used by the public insert routine.
129Eats a reference to key.
130*/
131static int
132set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
133{
134 setentry *table = so->table;
135 setentry *freeslot = NULL;
136 setentry *entry;
137 size_t perturb = hash;
138 size_t mask = so->mask;
139 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
140 size_t j;
141 int cmp;
142
143 entry = &table[i];
144 if (entry->key == NULL)
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700145 goto found_null_first;
146
147 freeslot = NULL;
148 perturb = hash;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700149
150 while (1) {
151 if (entry->hash == hash) {
152 PyObject *startkey = entry->key;
153 /* startkey cannot be a dummy because the dummy hash field is -1 */
154 assert(startkey != dummy);
155 if (startkey == key)
156 goto found_active;
157 if (PyUnicode_CheckExact(startkey)
158 && PyUnicode_CheckExact(key)
159 && unicode_eq(startkey, key))
160 goto found_active;
161 Py_INCREF(startkey);
162 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
163 Py_DECREF(startkey);
164 if (cmp < 0) /* unlikely */
165 return -1;
166 if (table != so->table || entry->key != startkey) /* unlikely */
167 return set_insert_key(so, key, hash);
168 if (cmp > 0) /* likely */
169 goto found_active;
170 mask = so->mask; /* help avoid a register spill */
171 }
172 if (entry->hash == -1 && freeslot == NULL)
173 freeslot = entry;
174
175 if (i + LINEAR_PROBES <= mask) {
176 for (j = 0 ; j < LINEAR_PROBES ; j++) {
177 entry++;
178 if (entry->hash == 0 && entry->key == NULL)
179 goto found_null;
180 if (entry->hash == hash) {
181 PyObject *startkey = entry->key;
182 assert(startkey != dummy);
183 if (startkey == key)
184 goto found_active;
185 if (PyUnicode_CheckExact(startkey)
186 && PyUnicode_CheckExact(key)
187 && unicode_eq(startkey, key))
188 goto found_active;
189 Py_INCREF(startkey);
190 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
191 Py_DECREF(startkey);
192 if (cmp < 0)
193 return -1;
194 if (table != so->table || entry->key != startkey)
195 return set_insert_key(so, key, hash);
196 if (cmp > 0)
197 goto found_active;
198 mask = so->mask;
199 }
Raymond Hettinger438f9132015-02-09 06:48:29 -0600200 if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800201 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700202 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700204
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700205 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800206 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700207
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800208 entry = &table[i];
Raymond Hettinger8651a502015-05-27 10:37:20 -0700209 if (entry->hash == 0 && entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700210 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700212
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700213 found_null_first:
214 so->fill++;
215 so->used++;
216 entry->key = key;
217 entry->hash = hash;
218 return 0;
219
Raymond Hettingera35adf52013-09-02 03:23:21 -0700220 found_null:
Raymond Hettinger8651a502015-05-27 10:37:20 -0700221 if (freeslot == NULL) {
222 /* UNUSED */
223 so->fill++;
224 } else {
225 /* DUMMY */
226 entry = freeslot;
227 }
228 so->used++;
229 entry->key = key;
230 entry->hash = hash;
231 return 0;
232
233 found_active:
234 Py_DECREF(key);
235 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000236}
237
238/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000239Internal routine used by set_table_resize() to insert an item which is
240known to be absent from the set. This routine also assumes that
241the set contains no deleted entries. Besides the performance benefit,
242using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
243Note that no refcounts are changed by this routine; if needed, the caller
244is responsible for incref'ing `key`.
245*/
246static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200247set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200250 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700251 size_t perturb = hash;
252 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800253 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700254 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000255
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700256 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800257 entry = &table[i];
258 if (entry->key == NULL)
259 goto found_null;
260 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800261 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800262 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800263 if (entry->key == NULL)
264 goto found_null;
265 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700266 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700267 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800268 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700270 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 entry->key = key;
272 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700273 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000275}
276
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700277/* ======== End logic for probing the hash table ========================== */
278/* ======================================================================== */
279
Thomas Wouters89f507f2006-12-13 04:49:30 +0000280/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000281Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000282keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000283actually be smaller than the old one.
284*/
285static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_ssize_t newsize;
289 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800290 Py_ssize_t oldfill = so->fill;
291 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 int is_oldtable_malloced;
293 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100298 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 for (newsize = PySet_MINSIZE;
300 newsize <= minused && newsize > 0;
301 newsize <<= 1)
302 ;
303 if (newsize <= 0) {
304 PyErr_NoMemory();
305 return -1;
306 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 /* Get space for a new table. */
309 oldtable = so->table;
310 assert(oldtable != NULL);
311 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 if (newsize == PySet_MINSIZE) {
314 /* A large table is shrinking, or we can't get any smaller. */
315 newtable = so->smalltable;
316 if (newtable == oldtable) {
317 if (so->fill == so->used) {
318 /* No dummies, so no point doing anything. */
319 return 0;
320 }
321 /* We're not going to resize it, but rebuild the
322 table anyway to purge old dummy entries.
323 Subtle: This is *necessary* if fill==size,
324 as set_lookkey needs at least one virgin slot to
325 terminate failing searches. If fill < size, it's
326 merely desirable, as dummies slow searches. */
327 assert(so->fill > so->used);
328 memcpy(small_copy, oldtable, sizeof(small_copy));
329 oldtable = small_copy;
330 }
331 }
332 else {
333 newtable = PyMem_NEW(setentry, newsize);
334 if (newtable == NULL) {
335 PyErr_NoMemory();
336 return -1;
337 }
338 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 /* Make the set empty, using the new table. */
341 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800344 so->used = 0;
345 so->mask = newsize - 1;
346 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* Copy the data over; this is refcount-neutral for active entries;
349 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800350 if (oldfill == oldused) {
351 for (entry = oldtable; oldused > 0; entry++) {
352 if (entry->key != NULL) {
353 oldused--;
354 set_insert_clean(so, entry->key, entry->hash);
355 }
356 }
357 } else {
358 for (entry = oldtable; oldused > 0; entry++) {
359 if (entry->key != NULL && entry->key != dummy) {
360 oldused--;
361 set_insert_clean(so, entry->key, entry->hash);
362 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 }
364 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (is_oldtable_malloced)
367 PyMem_DEL(oldtable);
368 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000369}
370
Raymond Hettingerc991db22005-08-11 07:58:45 +0000371/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
372
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000373static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200374set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000375{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200376 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000377 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100378 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 assert(so->fill <= so->mask); /* at least one empty slot */
381 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000382 Py_INCREF(key);
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700383 if (set_insert_key(so, key, hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000384 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 return -1;
386 }
387 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
388 return 0;
389 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000390}
391
392static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200393set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800395 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200396 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200399 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 hash = PyObject_Hash(key);
401 if (hash == -1)
402 return -1;
403 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800404 entry.key = key;
405 entry.hash = hash;
406 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000407}
408
409#define DISCARD_NOTFOUND 0
410#define DISCARD_FOUND 1
411
412static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000413set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800414{
415 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000417
Raymond Hettinger93035c42015-01-25 16:12:49 -0800418 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (entry == NULL)
420 return -1;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700421 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 return DISCARD_NOTFOUND;
423 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800425 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 so->used--;
427 Py_DECREF(old_key);
428 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000429}
430
431static int
432set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000433{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800434 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200435 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200440 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 hash = PyObject_Hash(key);
442 if (hash == -1)
443 return -1;
444 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800445 entry.key = key;
446 entry.hash = hash;
447 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448}
449
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700450static void
451set_empty_to_minsize(PySetObject *so)
452{
453 memset(so->smalltable, 0, sizeof(so->smalltable));
454 so->fill = 0;
455 so->used = 0;
456 so->mask = PySet_MINSIZE - 1;
457 so->table = so->smalltable;
458 so->hash = -1;
459}
460
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000461static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462set_clear_internal(PySetObject *so)
463{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800464 setentry *entry;
465 setentry *table = so->table;
466 Py_ssize_t fill = so->fill;
467 Py_ssize_t used = so->used;
468 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000470
Raymond Hettinger583cd032013-09-07 22:06:35 -0700471 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 /* This is delicate. During the process of clearing the set,
475 * decrefs can cause the set to mutate. To avoid fatal confusion
476 * (voice of experience), we have to make the set empty before
477 * clearing the slots, and never refer to anything via so->ref while
478 * clearing.
479 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700481 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 else if (fill > 0) {
484 /* It's a small table with something that needs to be cleared.
485 * Afraid the only safe way is to copy the set entries into
486 * another small table first.
487 */
488 memcpy(small_copy, table, sizeof(small_copy));
489 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700490 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 }
492 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 /* Now we can finally clear things. If C had refcounts, we could
495 * assert that the refcount on table is 1 now, i.e. that this function
496 * has unique access to it, so decref side-effects can't alter it.
497 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800498 for (entry = table; used > 0; entry++) {
499 if (entry->key && entry->key != dummy) {
500 used--;
501 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (table_is_malloced)
506 PyMem_DEL(table);
507 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508}
509
510/*
511 * Iterate over a set table. Use like so:
512 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000513 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000514 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000515 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000516 * while (set_next(yourset, &pos, &entry)) {
517 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000518 * }
519 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000520 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522 */
523static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 Py_ssize_t i;
527 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200528 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 assert (PyAnySet_Check(so));
531 i = *pos_ptr;
532 assert(i >= 0);
533 table = so->table;
534 mask = so->mask;
535 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
536 i++;
537 *pos_ptr = i+1;
538 if (i > mask)
539 return 0;
540 assert(table[i].key != NULL);
541 *entry_ptr = &table[i];
542 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543}
544
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000545static void
546set_dealloc(PySetObject *so)
547{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200548 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800549 Py_ssize_t used = so->used;
550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PyObject_GC_UnTrack(so);
552 Py_TRASHCAN_SAFE_BEGIN(so)
553 if (so->weakreflist != NULL)
554 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000555
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800556 for (entry = so->table; used > 0; entry++) {
557 if (entry->key && entry->key != dummy) {
558 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700559 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 }
561 }
562 if (so->table != so->smalltable)
563 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700564 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000566}
567
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568static PyObject *
569set_repr(PySetObject *so)
570{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200571 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (status != 0) {
575 if (status < 0)
576 return NULL;
577 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
578 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 /* shortcut for the empty set */
581 if (!so->used) {
582 Py_ReprLeave((PyObject*)so);
583 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
584 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 keys = PySequence_List((PyObject *)so);
587 if (keys == NULL)
588 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000589
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200590 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 listrepr = PyObject_Repr(keys);
592 Py_DECREF(keys);
593 if (listrepr == NULL)
594 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200595 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200597 if (tmp == NULL)
598 goto done;
599 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200600
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200601 if (Py_TYPE(so) != &PySet_Type)
602 result = PyUnicode_FromFormat("%s({%U})",
603 Py_TYPE(so)->tp_name,
604 listrepr);
605 else
606 result = PyUnicode_FromFormat("{%U}", listrepr);
607 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000608done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 Py_ReprLeave((PyObject*)so);
610 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000611}
612
Martin v. Löwis18e16552006-02-15 17:27:45 +0000613static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000614set_len(PyObject *so)
615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000617}
618
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000619static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000620set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000623 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200624 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700625 setentry *so_entry;
626 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 assert (PyAnySet_Check(so));
629 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 other = (PySetObject*)otherset;
632 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700633 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 return 0;
635 /* Do one big resize at the start, rather than
636 * incrementally resizing as we insert new keys. Expect
637 * that there will be no (or few) overlapping keys.
638 */
639 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
640 if (set_table_resize(so, (so->used + other->used)*2) != 0)
641 return -1;
642 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700643 so_entry = so->table;
644 other_entry = other->table;
645
646 /* If our table is empty, and both tables have the same size, and
647 there are no dummies to eliminate, then just copy the pointers. */
648 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
649 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
650 key = other_entry->key;
651 if (key != NULL) {
652 assert(so_entry->key == NULL);
653 Py_INCREF(key);
654 so_entry->key = key;
655 so_entry->hash = other_entry->hash;
656 }
657 }
658 so->fill = other->fill;
659 so->used = other->used;
660 return 0;
661 }
662
663 /* If our table is empty, we can use set_insert_clean() */
664 if (so->fill == 0) {
665 for (i = 0; i <= other->mask; i++, other_entry++) {
666 key = other_entry->key;
667 if (key != NULL && key != dummy) {
668 Py_INCREF(key);
669 set_insert_clean(so, key, other_entry->hash);
670 }
671 }
672 return 0;
673 }
674
675 /* We can't assure there are no duplicates, so do normal insertions */
676 for (i = 0; i <= other->mask; i++, other_entry++) {
677 key = other_entry->key;
678 if (key != NULL && key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000679 Py_INCREF(key);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700680 if (set_insert_key(so, key, other_entry->hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000681 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 return -1;
683 }
684 }
685 }
686 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000687}
688
689static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000690set_contains_entry(PySetObject *so, setentry *entry)
691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 PyObject *key;
693 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000694
Raymond Hettinger93035c42015-01-25 16:12:49 -0800695 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 if (lu_entry == NULL)
697 return -1;
698 key = lu_entry->key;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700699 return key != NULL;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000700}
701
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800702static int
703set_contains_key(PySetObject *so, PyObject *key)
704{
705 setentry entry;
706 Py_hash_t hash;
707
708 if (!PyUnicode_CheckExact(key) ||
709 (hash = ((PyASCIIObject *) key)->hash) == -1) {
710 hash = PyObject_Hash(key);
711 if (hash == -1)
712 return -1;
713 }
714 entry.key = key;
715 entry.hash = hash;
716 return set_contains_entry(so, &entry);
717}
718
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000719static PyObject *
720set_pop(PySetObject *so)
721{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800722 /* Make sure the search finger is in bounds */
723 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200724 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 assert (PyAnySet_Check(so));
728 if (so->used == 0) {
729 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
730 return NULL;
731 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000732
Raymond Hettinger1202a472015-01-18 13:12:42 -0800733 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
734 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800735 if (i > so->mask)
736 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 }
738 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800740 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800742 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000744}
745
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000746PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
747Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000748
749static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000750set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 Py_ssize_t pos = 0;
753 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 while (set_next(so, &pos, &entry))
756 Py_VISIT(entry->key);
757 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000758}
759
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000760static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000761frozenset_hash(PyObject *self)
762{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800763 /* Most of the constants in this hash algorithm are randomly choosen
764 large primes with "interesting bit patterns" and that passed
765 tests for good collision statistics on a variety of problematic
766 datasets such as:
767
768 ps = []
769 for r in range(21):
770 ps += itertools.combinations(range(20), r)
771 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
772
773 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800775 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 setentry *entry;
777 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 if (so->hash != -1)
780 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000781
Mark Dickinson57e683e2011-09-24 18:18:40 +0100782 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 while (set_next(so, &pos, &entry)) {
784 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800785 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 combinations of a small number of elements with nearby
787 hashes so that many distinct combinations collapse to only
788 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000789 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800790 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800792 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500793 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800794 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200795 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800796 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 so->hash = hash;
798 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000799}
800
Raymond Hettingera9d99362005-08-05 00:01:15 +0000801/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000803typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 PyObject_HEAD
805 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
806 Py_ssize_t si_used;
807 Py_ssize_t si_pos;
808 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809} setiterobject;
810
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811static void
812setiter_dealloc(setiterobject *si)
813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_XDECREF(si->si_set);
815 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000816}
817
818static int
819setiter_traverse(setiterobject *si, visitproc visit, void *arg)
820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 Py_VISIT(si->si_set);
822 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000823}
824
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000825static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000826setiter_len(setiterobject *si)
827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 Py_ssize_t len = 0;
829 if (si->si_set != NULL && si->si_used == si->si_set->used)
830 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000831 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832}
833
Armin Rigof5b3e362006-02-11 21:32:43 +0000834PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000835
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000836static PyObject *setiter_iternext(setiterobject *si);
837
838static PyObject *
839setiter_reduce(setiterobject *si)
840{
841 PyObject *list;
842 setiterobject tmp;
843
844 list = PyList_New(0);
845 if (!list)
846 return NULL;
847
Ezio Melotti0e1af282012-09-28 16:43:40 +0300848 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000849 tmp = *si;
850 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300851
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000852 /* iterate the temporary into a list */
853 for(;;) {
854 PyObject *element = setiter_iternext(&tmp);
855 if (element) {
856 if (PyList_Append(list, element)) {
857 Py_DECREF(element);
858 Py_DECREF(list);
859 Py_XDECREF(tmp.si_set);
860 return NULL;
861 }
862 Py_DECREF(element);
863 } else
864 break;
865 }
866 Py_XDECREF(tmp.si_set);
867 /* check for error */
868 if (tmp.si_set != NULL) {
869 /* we have an error */
870 Py_DECREF(list);
871 return NULL;
872 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200873 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000874}
875
876PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
877
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000878static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000880 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000882};
883
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000884static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200887 Py_ssize_t i, mask;
888 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 if (so == NULL)
892 return NULL;
893 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 if (si->si_used != so->used) {
896 PyErr_SetString(PyExc_RuntimeError,
897 "Set changed size during iteration");
898 si->si_used = -1; /* Make this state sticky */
899 return NULL;
900 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 i = si->si_pos;
903 assert(i>=0);
904 entry = so->table;
905 mask = so->mask;
906 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
907 i++;
908 si->si_pos = i+1;
909 if (i > mask)
910 goto fail;
911 si->len--;
912 key = entry[i].key;
913 Py_INCREF(key);
914 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000915
916fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 Py_DECREF(so);
918 si->si_set = NULL;
919 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000920}
921
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000922PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 PyVarObject_HEAD_INIT(&PyType_Type, 0)
924 "set_iterator", /* tp_name */
925 sizeof(setiterobject), /* tp_basicsize */
926 0, /* tp_itemsize */
927 /* methods */
928 (destructor)setiter_dealloc, /* tp_dealloc */
929 0, /* tp_print */
930 0, /* tp_getattr */
931 0, /* tp_setattr */
932 0, /* tp_reserved */
933 0, /* tp_repr */
934 0, /* tp_as_number */
935 0, /* tp_as_sequence */
936 0, /* tp_as_mapping */
937 0, /* tp_hash */
938 0, /* tp_call */
939 0, /* tp_str */
940 PyObject_GenericGetAttr, /* tp_getattro */
941 0, /* tp_setattro */
942 0, /* tp_as_buffer */
943 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
944 0, /* tp_doc */
945 (traverseproc)setiter_traverse, /* tp_traverse */
946 0, /* tp_clear */
947 0, /* tp_richcompare */
948 0, /* tp_weaklistoffset */
949 PyObject_SelfIter, /* tp_iter */
950 (iternextfunc)setiter_iternext, /* tp_iternext */
951 setiter_methods, /* tp_methods */
952 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000953};
954
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000955static PyObject *
956set_iter(PySetObject *so)
957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
959 if (si == NULL)
960 return NULL;
961 Py_INCREF(so);
962 si->si_set = so;
963 si->si_used = so->used;
964 si->si_pos = 0;
965 si->len = so->used;
966 _PyObject_GC_TRACK(si);
967 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000968}
969
Raymond Hettingerd7946662005-08-01 21:39:29 +0000970static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000971set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 if (PyAnySet_Check(other))
976 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 if (PyDict_CheckExact(other)) {
979 PyObject *value;
980 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000981 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* Do one big resize at the start, rather than
985 * incrementally resizing as we insert new keys. Expect
986 * that there will be no (or few) overlapping keys.
987 */
988 if (dictsize == -1)
989 return -1;
990 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
991 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
992 return -1;
993 }
994 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
995 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 an_entry.hash = hash;
998 an_entry.key = key;
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700999 if (set_add_entry(so, &an_entry))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 return -1;
1001 }
1002 return 0;
1003 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 it = PyObject_GetIter(other);
1006 if (it == NULL)
1007 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001010 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 Py_DECREF(it);
1012 Py_DECREF(key);
1013 return -1;
1014 }
1015 Py_DECREF(key);
1016 }
1017 Py_DECREF(it);
1018 if (PyErr_Occurred())
1019 return -1;
1020 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001021}
1022
1023static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001024set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1029 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001030 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 return NULL;
1032 }
1033 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001034}
1035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001037"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001038
Raymond Hettinger426d9952014-05-18 21:40:20 +01001039/* XXX Todo:
1040 If aligned memory allocations become available, make the
1041 set object 64 byte aligned so that most of the fields
1042 can be retrieved or updated in a single cache line.
1043*/
1044
Raymond Hettingera38123e2003-11-24 22:18:49 +00001045static PyObject *
1046make_new_set(PyTypeObject *type, PyObject *iterable)
1047{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001048 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001051 so = (PySetObject *)type->tp_alloc(type, 0);
1052 if (so == NULL)
1053 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001054
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001055 so->fill = 0;
1056 so->used = 0;
1057 so->mask = PySet_MINSIZE - 1;
1058 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001059 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -08001060 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001064 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 Py_DECREF(so);
1066 return NULL;
1067 }
1068 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001071}
1072
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001073static PyObject *
1074make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1077 if (PyType_IsSubtype(type, &PySet_Type))
1078 type = &PySet_Type;
1079 else
1080 type = &PyFrozenSet_Type;
1081 }
1082 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001083}
1084
Raymond Hettingerd7946662005-08-01 21:39:29 +00001085/* The empty frozenset is a singleton */
1086static PyObject *emptyfrozenset = NULL;
1087
Raymond Hettingera690a992003-11-16 16:17:49 +00001088static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001089frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1094 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1097 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (type != &PyFrozenSet_Type)
1100 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 if (iterable != NULL) {
1103 /* frozenset(f) is idempotent */
1104 if (PyFrozenSet_CheckExact(iterable)) {
1105 Py_INCREF(iterable);
1106 return iterable;
1107 }
1108 result = make_new_set(type, iterable);
1109 if (result == NULL || PySet_GET_SIZE(result))
1110 return result;
1111 Py_DECREF(result);
1112 }
1113 /* The empty frozenset is a singleton */
1114 if (emptyfrozenset == NULL)
1115 emptyfrozenset = make_new_set(type, NULL);
1116 Py_XINCREF(emptyfrozenset);
1117 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001118}
1119
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001120int
1121PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001122{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001123 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001124}
1125
1126void
1127PySet_Fini(void)
1128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001130}
1131
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001132static PyObject *
1133set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1136 return NULL;
1137
1138 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001139}
1140
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001141/* set_swap_bodies() switches the contents of any two sets by moving their
1142 internal data pointers and, if needed, copying the internal smalltables.
1143 Semantically equivalent to:
1144
1145 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1146
1147 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001148 Useful for operations that update in-place (by allowing an intermediate
1149 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001150*/
1151
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001152static void
1153set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 Py_ssize_t t;
1156 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001158 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 t = a->fill; a->fill = b->fill; b->fill = t;
1161 t = a->used; a->used = b->used; b->used = t;
1162 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 u = a->table;
1165 if (a->table == a->smalltable)
1166 u = b->smalltable;
1167 a->table = b->table;
1168 if (b->table == b->smalltable)
1169 a->table = a->smalltable;
1170 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 if (a->table == a->smalltable || b->table == b->smalltable) {
1173 memcpy(tab, a->smalltable, sizeof(tab));
1174 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1175 memcpy(b->smalltable, tab, sizeof(tab));
1176 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1179 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1180 h = a->hash; a->hash = b->hash; b->hash = h;
1181 } else {
1182 a->hash = -1;
1183 b->hash = -1;
1184 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001185}
1186
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001187static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001188set_copy(PySetObject *so)
1189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001191}
1192
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001193static PyObject *
1194frozenset_copy(PySetObject *so)
1195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 if (PyFrozenSet_CheckExact(so)) {
1197 Py_INCREF(so);
1198 return (PyObject *)so;
1199 }
1200 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001201}
1202
Raymond Hettingera690a992003-11-16 16:17:49 +00001203PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1204
1205static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001206set_clear(PySetObject *so)
1207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 set_clear_internal(so);
1209 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001210}
1211
1212PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1213
1214static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001215set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 PySetObject *result;
1218 PyObject *other;
1219 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 result = (PySetObject *)set_copy(so);
1222 if (result == NULL)
1223 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1226 other = PyTuple_GET_ITEM(args, i);
1227 if ((PyObject *)so == other)
1228 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001229 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 Py_DECREF(result);
1231 return NULL;
1232 }
1233 }
1234 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001235}
1236
1237PyDoc_STRVAR(union_doc,
1238 "Return the union of sets as a new set.\n\
1239\n\
1240(i.e. all elements that are in either set.)");
1241
1242static PyObject *
1243set_or(PySetObject *so, PyObject *other)
1244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001246
Brian Curtindfc80e32011-08-10 20:28:54 -05001247 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1248 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 result = (PySetObject *)set_copy(so);
1251 if (result == NULL)
1252 return NULL;
1253 if ((PyObject *)so == other)
1254 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001255 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 Py_DECREF(result);
1257 return NULL;
1258 }
1259 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001260}
1261
Raymond Hettingera690a992003-11-16 16:17:49 +00001262static PyObject *
1263set_ior(PySetObject *so, PyObject *other)
1264{
Brian Curtindfc80e32011-08-10 20:28:54 -05001265 if (!PyAnySet_Check(other))
1266 Py_RETURN_NOTIMPLEMENTED;
1267
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001268 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 return NULL;
1270 Py_INCREF(so);
1271 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001272}
1273
1274static PyObject *
1275set_intersection(PySetObject *so, PyObject *other)
1276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 PySetObject *result;
1278 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 if ((PyObject *)so == other)
1281 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1284 if (result == NULL)
1285 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (PyAnySet_Check(other)) {
1288 Py_ssize_t pos = 0;
1289 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1292 tmp = (PyObject *)so;
1293 so = (PySetObject *)other;
1294 other = tmp;
1295 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 while (set_next((PySetObject *)other, &pos, &entry)) {
1298 int rv = set_contains_entry(so, entry);
1299 if (rv == -1) {
1300 Py_DECREF(result);
1301 return NULL;
1302 }
1303 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001304 if (set_add_entry(result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 Py_DECREF(result);
1306 return NULL;
1307 }
1308 }
1309 }
1310 return (PyObject *)result;
1311 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 it = PyObject_GetIter(other);
1314 if (it == NULL) {
1315 Py_DECREF(result);
1316 return NULL;
1317 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 while ((key = PyIter_Next(it)) != NULL) {
1320 int rv;
1321 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001322 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 if (hash == -1) {
1325 Py_DECREF(it);
1326 Py_DECREF(result);
1327 Py_DECREF(key);
1328 return NULL;
1329 }
1330 entry.hash = hash;
1331 entry.key = key;
1332 rv = set_contains_entry(so, &entry);
1333 if (rv == -1) {
1334 Py_DECREF(it);
1335 Py_DECREF(result);
1336 Py_DECREF(key);
1337 return NULL;
1338 }
1339 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001340 if (set_add_entry(result, &entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 Py_DECREF(it);
1342 Py_DECREF(result);
1343 Py_DECREF(key);
1344 return NULL;
1345 }
1346 }
1347 Py_DECREF(key);
1348 }
1349 Py_DECREF(it);
1350 if (PyErr_Occurred()) {
1351 Py_DECREF(result);
1352 return NULL;
1353 }
1354 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001355}
1356
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001357static PyObject *
1358set_intersection_multi(PySetObject *so, PyObject *args)
1359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 Py_ssize_t i;
1361 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 if (PyTuple_GET_SIZE(args) == 0)
1364 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 Py_INCREF(so);
1367 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1368 PyObject *other = PyTuple_GET_ITEM(args, i);
1369 PyObject *newresult = set_intersection((PySetObject *)result, other);
1370 if (newresult == NULL) {
1371 Py_DECREF(result);
1372 return NULL;
1373 }
1374 Py_DECREF(result);
1375 result = newresult;
1376 }
1377 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001378}
1379
Raymond Hettingera690a992003-11-16 16:17:49 +00001380PyDoc_STRVAR(intersection_doc,
1381"Return the intersection of two sets as a new set.\n\
1382\n\
1383(i.e. all elements that are in both sets.)");
1384
1385static PyObject *
1386set_intersection_update(PySetObject *so, PyObject *other)
1387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 tmp = set_intersection(so, other);
1391 if (tmp == NULL)
1392 return NULL;
1393 set_swap_bodies(so, (PySetObject *)tmp);
1394 Py_DECREF(tmp);
1395 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001396}
1397
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001398static PyObject *
1399set_intersection_update_multi(PySetObject *so, PyObject *args)
1400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 tmp = set_intersection_multi(so, args);
1404 if (tmp == NULL)
1405 return NULL;
1406 set_swap_bodies(so, (PySetObject *)tmp);
1407 Py_DECREF(tmp);
1408 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001409}
1410
Raymond Hettingera690a992003-11-16 16:17:49 +00001411PyDoc_STRVAR(intersection_update_doc,
1412"Update a set with the intersection of itself and another.");
1413
1414static PyObject *
1415set_and(PySetObject *so, PyObject *other)
1416{
Brian Curtindfc80e32011-08-10 20:28:54 -05001417 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1418 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001420}
1421
1422static PyObject *
1423set_iand(PySetObject *so, PyObject *other)
1424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001426
Brian Curtindfc80e32011-08-10 20:28:54 -05001427 if (!PyAnySet_Check(other))
1428 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 result = set_intersection_update(so, other);
1430 if (result == NULL)
1431 return NULL;
1432 Py_DECREF(result);
1433 Py_INCREF(so);
1434 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001435}
1436
Guido van Rossum58da9312007-11-10 23:39:45 +00001437static PyObject *
1438set_isdisjoint(PySetObject *so, PyObject *other)
1439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if ((PyObject *)so == other) {
1443 if (PySet_GET_SIZE(so) == 0)
1444 Py_RETURN_TRUE;
1445 else
1446 Py_RETURN_FALSE;
1447 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 if (PyAnySet_CheckExact(other)) {
1450 Py_ssize_t pos = 0;
1451 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1454 tmp = (PyObject *)so;
1455 so = (PySetObject *)other;
1456 other = tmp;
1457 }
1458 while (set_next((PySetObject *)other, &pos, &entry)) {
1459 int rv = set_contains_entry(so, entry);
1460 if (rv == -1)
1461 return NULL;
1462 if (rv)
1463 Py_RETURN_FALSE;
1464 }
1465 Py_RETURN_TRUE;
1466 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 it = PyObject_GetIter(other);
1469 if (it == NULL)
1470 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 while ((key = PyIter_Next(it)) != NULL) {
1473 int rv;
1474 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001475 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (hash == -1) {
1478 Py_DECREF(key);
1479 Py_DECREF(it);
1480 return NULL;
1481 }
1482 entry.hash = hash;
1483 entry.key = key;
1484 rv = set_contains_entry(so, &entry);
1485 Py_DECREF(key);
1486 if (rv == -1) {
1487 Py_DECREF(it);
1488 return NULL;
1489 }
1490 if (rv) {
1491 Py_DECREF(it);
1492 Py_RETURN_FALSE;
1493 }
1494 }
1495 Py_DECREF(it);
1496 if (PyErr_Occurred())
1497 return NULL;
1498 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001499}
1500
1501PyDoc_STRVAR(isdisjoint_doc,
1502"Return True if two sets have a null intersection.");
1503
Neal Norwitz6576bd82005-11-13 18:41:28 +00001504static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001505set_difference_update_internal(PySetObject *so, PyObject *other)
1506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 if ((PyObject *)so == other)
1508 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (PyAnySet_Check(other)) {
1511 setentry *entry;
1512 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 while (set_next((PySetObject *)other, &pos, &entry))
1515 if (set_discard_entry(so, entry) == -1)
1516 return -1;
1517 } else {
1518 PyObject *key, *it;
1519 it = PyObject_GetIter(other);
1520 if (it == NULL)
1521 return -1;
1522
1523 while ((key = PyIter_Next(it)) != NULL) {
1524 if (set_discard_key(so, key) == -1) {
1525 Py_DECREF(it);
1526 Py_DECREF(key);
1527 return -1;
1528 }
1529 Py_DECREF(key);
1530 }
1531 Py_DECREF(it);
1532 if (PyErr_Occurred())
1533 return -1;
1534 }
1535 /* If more than 1/5 are dummies, then resize them away. */
1536 if ((so->fill - so->used) * 5 < so->mask)
1537 return 0;
1538 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001539}
1540
Raymond Hettingera690a992003-11-16 16:17:49 +00001541static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001542set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1547 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001548 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 return NULL;
1550 }
1551 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001552}
1553
1554PyDoc_STRVAR(difference_update_doc,
1555"Remove all elements of another set from this set.");
1556
1557static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001558set_copy_and_difference(PySetObject *so, PyObject *other)
1559{
1560 PyObject *result;
1561
1562 result = set_copy(so);
1563 if (result == NULL)
1564 return NULL;
1565 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1566 return result;
1567 Py_DECREF(result);
1568 return NULL;
1569}
1570
1571static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001572set_difference(PySetObject *so, PyObject *other)
1573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 PyObject *result;
1575 setentry *entry;
1576 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001579 return set_copy_and_difference(so, other);
1580 }
1581
1582 /* If len(so) much more than len(other), it's more efficient to simply copy
1583 * so and then iterate other looking for common elements. */
1584 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1585 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 result = make_new_set_basetype(Py_TYPE(so), NULL);
1589 if (result == NULL)
1590 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 if (PyDict_CheckExact(other)) {
1593 while (set_next(so, &pos, &entry)) {
1594 setentry entrycopy;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001595 int rv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 entrycopy.hash = entry->hash;
1597 entrycopy.key = entry->key;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001598 rv = _PyDict_Contains(other, entry->key, entry->hash);
1599 if (rv < 0) {
1600 Py_DECREF(result);
1601 return NULL;
1602 }
1603 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001604 if (set_add_entry((PySetObject *)result, &entrycopy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 Py_DECREF(result);
1606 return NULL;
1607 }
1608 }
1609 }
1610 return result;
1611 }
1612
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001613 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 while (set_next(so, &pos, &entry)) {
1615 int rv = set_contains_entry((PySetObject *)other, entry);
1616 if (rv == -1) {
1617 Py_DECREF(result);
1618 return NULL;
1619 }
1620 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001621 if (set_add_entry((PySetObject *)result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 Py_DECREF(result);
1623 return NULL;
1624 }
1625 }
1626 }
1627 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001628}
1629
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001630static PyObject *
1631set_difference_multi(PySetObject *so, PyObject *args)
1632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 Py_ssize_t i;
1634 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 if (PyTuple_GET_SIZE(args) == 0)
1637 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 other = PyTuple_GET_ITEM(args, 0);
1640 result = set_difference(so, other);
1641 if (result == NULL)
1642 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1645 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001646 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 Py_DECREF(result);
1648 return NULL;
1649 }
1650 }
1651 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001652}
1653
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001654PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001655"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001656\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001657(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001658static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001659set_sub(PySetObject *so, PyObject *other)
1660{
Brian Curtindfc80e32011-08-10 20:28:54 -05001661 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1662 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001664}
1665
1666static PyObject *
1667set_isub(PySetObject *so, PyObject *other)
1668{
Brian Curtindfc80e32011-08-10 20:28:54 -05001669 if (!PyAnySet_Check(other))
1670 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001671 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 return NULL;
1673 Py_INCREF(so);
1674 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001675}
1676
1677static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001678set_symmetric_difference_update(PySetObject *so, PyObject *other)
1679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 PySetObject *otherset;
1681 PyObject *key;
1682 Py_ssize_t pos = 0;
1683 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 if ((PyObject *)so == other)
1686 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if (PyDict_CheckExact(other)) {
1689 PyObject *value;
1690 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001691 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1693 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001694
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001695 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 an_entry.hash = hash;
1697 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001700 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001701 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001704 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001705 if (set_add_entry(so, &an_entry)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001706 Py_DECREF(key);
1707 return NULL;
1708 }
1709 }
Georg Brandl2d444492010-09-03 10:52:55 +00001710 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 }
1712 Py_RETURN_NONE;
1713 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (PyAnySet_Check(other)) {
1716 Py_INCREF(other);
1717 otherset = (PySetObject *)other;
1718 } else {
1719 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1720 if (otherset == NULL)
1721 return NULL;
1722 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 while (set_next(otherset, &pos, &entry)) {
1725 int rv = set_discard_entry(so, entry);
1726 if (rv == -1) {
1727 Py_DECREF(otherset);
1728 return NULL;
1729 }
1730 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001731 if (set_add_entry(so, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 Py_DECREF(otherset);
1733 return NULL;
1734 }
1735 }
1736 }
1737 Py_DECREF(otherset);
1738 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001739}
1740
1741PyDoc_STRVAR(symmetric_difference_update_doc,
1742"Update a set with the symmetric difference of itself and another.");
1743
1744static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001745set_symmetric_difference(PySetObject *so, PyObject *other)
1746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 PyObject *rv;
1748 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1751 if (otherset == NULL)
1752 return NULL;
1753 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1754 if (rv == NULL)
1755 return NULL;
1756 Py_DECREF(rv);
1757 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001758}
1759
1760PyDoc_STRVAR(symmetric_difference_doc,
1761"Return the symmetric difference of two sets as a new set.\n\
1762\n\
1763(i.e. all elements that are in exactly one of the sets.)");
1764
1765static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001766set_xor(PySetObject *so, PyObject *other)
1767{
Brian Curtindfc80e32011-08-10 20:28:54 -05001768 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1769 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001771}
1772
1773static PyObject *
1774set_ixor(PySetObject *so, PyObject *other)
1775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001777
Brian Curtindfc80e32011-08-10 20:28:54 -05001778 if (!PyAnySet_Check(other))
1779 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 result = set_symmetric_difference_update(so, other);
1781 if (result == NULL)
1782 return NULL;
1783 Py_DECREF(result);
1784 Py_INCREF(so);
1785 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001786}
1787
1788static PyObject *
1789set_issubset(PySetObject *so, PyObject *other)
1790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 setentry *entry;
1792 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 if (!PyAnySet_Check(other)) {
1795 PyObject *tmp, *result;
1796 tmp = make_new_set(&PySet_Type, other);
1797 if (tmp == NULL)
1798 return NULL;
1799 result = set_issubset(so, tmp);
1800 Py_DECREF(tmp);
1801 return result;
1802 }
1803 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1804 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 while (set_next(so, &pos, &entry)) {
1807 int rv = set_contains_entry((PySetObject *)other, entry);
1808 if (rv == -1)
1809 return NULL;
1810 if (!rv)
1811 Py_RETURN_FALSE;
1812 }
1813 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001814}
1815
1816PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1817
1818static PyObject *
1819set_issuperset(PySetObject *so, PyObject *other)
1820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 if (!PyAnySet_Check(other)) {
1824 tmp = make_new_set(&PySet_Type, other);
1825 if (tmp == NULL)
1826 return NULL;
1827 result = set_issuperset(so, tmp);
1828 Py_DECREF(tmp);
1829 return result;
1830 }
1831 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001832}
1833
1834PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1835
Raymond Hettingera690a992003-11-16 16:17:49 +00001836static PyObject *
1837set_richcompare(PySetObject *v, PyObject *w, int op)
1838{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001839 PyObject *r1;
1840 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001841
Brian Curtindfc80e32011-08-10 20:28:54 -05001842 if(!PyAnySet_Check(w))
1843 Py_RETURN_NOTIMPLEMENTED;
1844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 switch (op) {
1846 case Py_EQ:
1847 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1848 Py_RETURN_FALSE;
1849 if (v->hash != -1 &&
1850 ((PySetObject *)w)->hash != -1 &&
1851 v->hash != ((PySetObject *)w)->hash)
1852 Py_RETURN_FALSE;
1853 return set_issubset(v, w);
1854 case Py_NE:
1855 r1 = set_richcompare(v, w, Py_EQ);
1856 if (r1 == NULL)
1857 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001858 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001860 if (r2 < 0)
1861 return NULL;
1862 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 case Py_LE:
1864 return set_issubset(v, w);
1865 case Py_GE:
1866 return set_issuperset(v, w);
1867 case Py_LT:
1868 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1869 Py_RETURN_FALSE;
1870 return set_issubset(v, w);
1871 case Py_GT:
1872 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1873 Py_RETURN_FALSE;
1874 return set_issuperset(v, w);
1875 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001876 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001877}
1878
Raymond Hettingera690a992003-11-16 16:17:49 +00001879static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001880set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001881{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001882 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 return NULL;
1884 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001885}
1886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001888"Add an element to a set.\n\
1889\n\
1890This has no effect if the element is already present.");
1891
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001892static int
1893set_contains(PySetObject *so, PyObject *key)
1894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 PyObject *tmpkey;
1896 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 rv = set_contains_key(so, key);
1899 if (rv == -1) {
1900 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1901 return -1;
1902 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001903 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 if (tmpkey == NULL)
1905 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001906 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 Py_DECREF(tmpkey);
1908 }
1909 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001910}
1911
1912static PyObject *
1913set_direct_contains(PySetObject *so, PyObject *key)
1914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 result = set_contains(so, key);
1918 if (result == -1)
1919 return NULL;
1920 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001921}
1922
1923PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1924
Raymond Hettingera690a992003-11-16 16:17:49 +00001925static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001926set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 PyObject *tmpkey;
1929 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 rv = set_discard_key(so, key);
1932 if (rv == -1) {
1933 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1934 return NULL;
1935 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001936 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 if (tmpkey == NULL)
1938 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 Py_DECREF(tmpkey);
1941 if (rv == -1)
1942 return NULL;
1943 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001946 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 return NULL;
1948 }
1949 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001950}
1951
1952PyDoc_STRVAR(remove_doc,
1953"Remove an element from a set; it must be a member.\n\
1954\n\
1955If the element is not a member, raise a KeyError.");
1956
1957static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001958set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001959{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001960 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 rv = set_discard_key(so, key);
1964 if (rv == -1) {
1965 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1966 return NULL;
1967 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001968 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 if (tmpkey == NULL)
1970 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001971 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001973 if (rv == -1)
1974 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 }
1976 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001977}
1978
1979PyDoc_STRVAR(discard_doc,
1980"Remove an element from a set if it is a member.\n\
1981\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001983
1984static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001985set_reduce(PySetObject *so)
1986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001988 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 keys = PySequence_List((PyObject *)so);
1991 if (keys == NULL)
1992 goto done;
1993 args = PyTuple_Pack(1, keys);
1994 if (args == NULL)
1995 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001996 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 if (dict == NULL) {
1998 PyErr_Clear();
1999 dict = Py_None;
2000 Py_INCREF(dict);
2001 }
2002 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002003done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 Py_XDECREF(args);
2005 Py_XDECREF(keys);
2006 Py_XDECREF(dict);
2007 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002008}
2009
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002010static PyObject *
2011set_sizeof(PySetObject *so)
2012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 res = sizeof(PySetObject);
2016 if (so->table != so->smalltable)
2017 res = res + (so->mask + 1) * sizeof(setentry);
2018 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002019}
2020
2021PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002022static int
2023set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 if (!PyAnySet_Check(self))
2028 return -1;
2029 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2030 return -1;
2031 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2032 return -1;
2033 set_clear_internal(self);
2034 self->hash = -1;
2035 if (iterable == NULL)
2036 return 0;
2037 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002038}
2039
Raymond Hettingera690a992003-11-16 16:17:49 +00002040static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 set_len, /* sq_length */
2042 0, /* sq_concat */
2043 0, /* sq_repeat */
2044 0, /* sq_item */
2045 0, /* sq_slice */
2046 0, /* sq_ass_item */
2047 0, /* sq_ass_slice */
2048 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002049};
2050
2051/* set object ********************************************************/
2052
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002053#ifdef Py_DEBUG
2054static PyObject *test_c_api(PySetObject *so);
2055
2056PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2057All is well if assertions don't fail.");
2058#endif
2059
Raymond Hettingera690a992003-11-16 16:17:49 +00002060static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 {"add", (PyCFunction)set_add, METH_O,
2062 add_doc},
2063 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2064 clear_doc},
2065 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2066 contains_doc},
2067 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2068 copy_doc},
2069 {"discard", (PyCFunction)set_discard, METH_O,
2070 discard_doc},
2071 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2072 difference_doc},
2073 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2074 difference_update_doc},
2075 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2076 intersection_doc},
2077 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2078 intersection_update_doc},
2079 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2080 isdisjoint_doc},
2081 {"issubset", (PyCFunction)set_issubset, METH_O,
2082 issubset_doc},
2083 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2084 issuperset_doc},
2085 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2086 pop_doc},
2087 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2088 reduce_doc},
2089 {"remove", (PyCFunction)set_remove, METH_O,
2090 remove_doc},
2091 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2092 sizeof_doc},
2093 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2094 symmetric_difference_doc},
2095 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2096 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002097#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2099 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002100#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 {"union", (PyCFunction)set_union, METH_VARARGS,
2102 union_doc},
2103 {"update", (PyCFunction)set_update, METH_VARARGS,
2104 update_doc},
2105 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002106};
2107
2108static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 0, /*nb_add*/
2110 (binaryfunc)set_sub, /*nb_subtract*/
2111 0, /*nb_multiply*/
2112 0, /*nb_remainder*/
2113 0, /*nb_divmod*/
2114 0, /*nb_power*/
2115 0, /*nb_negative*/
2116 0, /*nb_positive*/
2117 0, /*nb_absolute*/
2118 0, /*nb_bool*/
2119 0, /*nb_invert*/
2120 0, /*nb_lshift*/
2121 0, /*nb_rshift*/
2122 (binaryfunc)set_and, /*nb_and*/
2123 (binaryfunc)set_xor, /*nb_xor*/
2124 (binaryfunc)set_or, /*nb_or*/
2125 0, /*nb_int*/
2126 0, /*nb_reserved*/
2127 0, /*nb_float*/
2128 0, /*nb_inplace_add*/
2129 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2130 0, /*nb_inplace_multiply*/
2131 0, /*nb_inplace_remainder*/
2132 0, /*nb_inplace_power*/
2133 0, /*nb_inplace_lshift*/
2134 0, /*nb_inplace_rshift*/
2135 (binaryfunc)set_iand, /*nb_inplace_and*/
2136 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2137 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002138};
2139
2140PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002141"set() -> new empty set object\n\
2142set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002143\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002144Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002145
2146PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2148 "set", /* tp_name */
2149 sizeof(PySetObject), /* tp_basicsize */
2150 0, /* tp_itemsize */
2151 /* methods */
2152 (destructor)set_dealloc, /* tp_dealloc */
2153 0, /* tp_print */
2154 0, /* tp_getattr */
2155 0, /* tp_setattr */
2156 0, /* tp_reserved */
2157 (reprfunc)set_repr, /* tp_repr */
2158 &set_as_number, /* tp_as_number */
2159 &set_as_sequence, /* tp_as_sequence */
2160 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002161 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 0, /* tp_call */
2163 0, /* tp_str */
2164 PyObject_GenericGetAttr, /* tp_getattro */
2165 0, /* tp_setattro */
2166 0, /* tp_as_buffer */
2167 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2168 Py_TPFLAGS_BASETYPE, /* tp_flags */
2169 set_doc, /* tp_doc */
2170 (traverseproc)set_traverse, /* tp_traverse */
2171 (inquiry)set_clear_internal, /* tp_clear */
2172 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002173 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2174 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 0, /* tp_iternext */
2176 set_methods, /* tp_methods */
2177 0, /* tp_members */
2178 0, /* tp_getset */
2179 0, /* tp_base */
2180 0, /* tp_dict */
2181 0, /* tp_descr_get */
2182 0, /* tp_descr_set */
2183 0, /* tp_dictoffset */
2184 (initproc)set_init, /* tp_init */
2185 PyType_GenericAlloc, /* tp_alloc */
2186 set_new, /* tp_new */
2187 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002188};
2189
2190/* frozenset object ********************************************************/
2191
2192
2193static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2195 contains_doc},
2196 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2197 copy_doc},
2198 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2199 difference_doc},
2200 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2201 intersection_doc},
2202 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2203 isdisjoint_doc},
2204 {"issubset", (PyCFunction)set_issubset, METH_O,
2205 issubset_doc},
2206 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2207 issuperset_doc},
2208 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2209 reduce_doc},
2210 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2211 sizeof_doc},
2212 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2213 symmetric_difference_doc},
2214 {"union", (PyCFunction)set_union, METH_VARARGS,
2215 union_doc},
2216 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002217};
2218
2219static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 0, /*nb_add*/
2221 (binaryfunc)set_sub, /*nb_subtract*/
2222 0, /*nb_multiply*/
2223 0, /*nb_remainder*/
2224 0, /*nb_divmod*/
2225 0, /*nb_power*/
2226 0, /*nb_negative*/
2227 0, /*nb_positive*/
2228 0, /*nb_absolute*/
2229 0, /*nb_bool*/
2230 0, /*nb_invert*/
2231 0, /*nb_lshift*/
2232 0, /*nb_rshift*/
2233 (binaryfunc)set_and, /*nb_and*/
2234 (binaryfunc)set_xor, /*nb_xor*/
2235 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002236};
2237
2238PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002239"frozenset() -> empty frozenset object\n\
2240frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002241\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002242Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002243
2244PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2246 "frozenset", /* tp_name */
2247 sizeof(PySetObject), /* tp_basicsize */
2248 0, /* tp_itemsize */
2249 /* methods */
2250 (destructor)set_dealloc, /* tp_dealloc */
2251 0, /* tp_print */
2252 0, /* tp_getattr */
2253 0, /* tp_setattr */
2254 0, /* tp_reserved */
2255 (reprfunc)set_repr, /* tp_repr */
2256 &frozenset_as_number, /* tp_as_number */
2257 &set_as_sequence, /* tp_as_sequence */
2258 0, /* tp_as_mapping */
2259 frozenset_hash, /* tp_hash */
2260 0, /* tp_call */
2261 0, /* tp_str */
2262 PyObject_GenericGetAttr, /* tp_getattro */
2263 0, /* tp_setattro */
2264 0, /* tp_as_buffer */
2265 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2266 Py_TPFLAGS_BASETYPE, /* tp_flags */
2267 frozenset_doc, /* tp_doc */
2268 (traverseproc)set_traverse, /* tp_traverse */
2269 (inquiry)set_clear_internal, /* tp_clear */
2270 (richcmpfunc)set_richcompare, /* tp_richcompare */
2271 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2272 (getiterfunc)set_iter, /* tp_iter */
2273 0, /* tp_iternext */
2274 frozenset_methods, /* tp_methods */
2275 0, /* tp_members */
2276 0, /* tp_getset */
2277 0, /* tp_base */
2278 0, /* tp_dict */
2279 0, /* tp_descr_get */
2280 0, /* tp_descr_set */
2281 0, /* tp_dictoffset */
2282 0, /* tp_init */
2283 PyType_GenericAlloc, /* tp_alloc */
2284 frozenset_new, /* tp_new */
2285 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002286};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002287
2288
2289/***** C API functions *************************************************/
2290
2291PyObject *
2292PySet_New(PyObject *iterable)
2293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002295}
2296
2297PyObject *
2298PyFrozenSet_New(PyObject *iterable)
2299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002301}
2302
Neal Norwitz8c49c822006-03-04 18:41:19 +00002303Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002304PySet_Size(PyObject *anyset)
2305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 if (!PyAnySet_Check(anyset)) {
2307 PyErr_BadInternalCall();
2308 return -1;
2309 }
2310 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002311}
2312
2313int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002314PySet_Clear(PyObject *set)
2315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 if (!PySet_Check(set)) {
2317 PyErr_BadInternalCall();
2318 return -1;
2319 }
2320 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002321}
2322
2323int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002324PySet_Contains(PyObject *anyset, PyObject *key)
2325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 if (!PyAnySet_Check(anyset)) {
2327 PyErr_BadInternalCall();
2328 return -1;
2329 }
2330 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002331}
2332
2333int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002334PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 if (!PySet_Check(set)) {
2337 PyErr_BadInternalCall();
2338 return -1;
2339 }
2340 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002341}
2342
2343int
Christian Heimesfd66e512008-01-29 12:18:50 +00002344PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 if (!PySet_Check(anyset) &&
2347 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2348 PyErr_BadInternalCall();
2349 return -1;
2350 }
2351 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002352}
2353
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002354int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002355_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 if (!PyAnySet_Check(set)) {
2360 PyErr_BadInternalCall();
2361 return -1;
2362 }
2363 if (set_next((PySetObject *)set, pos, &entry) == 0)
2364 return 0;
2365 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002366 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002368}
2369
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002370PyObject *
2371PySet_Pop(PyObject *set)
2372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 if (!PySet_Check(set)) {
2374 PyErr_BadInternalCall();
2375 return NULL;
2376 }
2377 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002378}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002379
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002380int
2381_PySet_Update(PyObject *set, PyObject *iterable)
2382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 if (!PySet_Check(set)) {
2384 PyErr_BadInternalCall();
2385 return -1;
2386 }
2387 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002388}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002389
Raymond Hettingere259f132013-12-15 11:56:14 -08002390/* Exported for the gdb plugin's benefit. */
2391PyObject *_PySet_Dummy = dummy;
2392
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002393#ifdef Py_DEBUG
2394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002396 Returns True and original set is restored. */
2397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398#define assertRaises(call_return_value, exception) \
2399 do { \
2400 assert(call_return_value); \
2401 assert(PyErr_ExceptionMatches(exception)); \
2402 PyErr_Clear(); \
2403 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002404
2405static PyObject *
2406test_c_api(PySetObject *so)
2407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 Py_ssize_t count;
2409 char *s;
2410 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002411 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002413 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* Verify preconditions */
2417 assert(PyAnySet_Check(ob));
2418 assert(PyAnySet_CheckExact(ob));
2419 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 /* so.clear(); so |= set("abc"); */
2422 str = PyUnicode_FromString("abc");
2423 if (str == NULL)
2424 return NULL;
2425 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002426 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 Py_DECREF(str);
2428 return NULL;
2429 }
2430 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 /* Exercise type/size checks */
2433 assert(PySet_Size(ob) == 3);
2434 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 /* Raise TypeError for non-iterable constructor arguments */
2437 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2438 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 /* Raise TypeError for unhashable key */
2441 dup = PySet_New(ob);
2442 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2443 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2444 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 /* Exercise successful pop, contains, add, and discard */
2447 elem = PySet_Pop(ob);
2448 assert(PySet_Contains(ob, elem) == 0);
2449 assert(PySet_GET_SIZE(ob) == 2);
2450 assert(PySet_Add(ob, elem) == 0);
2451 assert(PySet_Contains(ob, elem) == 1);
2452 assert(PySet_GET_SIZE(ob) == 3);
2453 assert(PySet_Discard(ob, elem) == 1);
2454 assert(PySet_GET_SIZE(ob) == 2);
2455 assert(PySet_Discard(ob, elem) == 0);
2456 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 /* Exercise clear */
2459 dup2 = PySet_New(dup);
2460 assert(PySet_Clear(dup2) == 0);
2461 assert(PySet_Size(dup2) == 0);
2462 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 /* Raise SystemError on clear or update of frozen set */
2465 f = PyFrozenSet_New(dup);
2466 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2467 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2468 assert(PySet_Add(f, elem) == 0);
2469 Py_INCREF(f);
2470 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2471 Py_DECREF(f);
2472 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 /* Exercise direct iteration */
2475 i = 0, count = 0;
2476 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2477 s = _PyUnicode_AsString(x);
2478 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2479 count++;
2480 }
2481 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 /* Exercise updates */
2484 dup2 = PySet_New(NULL);
2485 assert(_PySet_Update(dup2, dup) == 0);
2486 assert(PySet_Size(dup2) == 3);
2487 assert(_PySet_Update(dup2, dup) == 0);
2488 assert(PySet_Size(dup2) == 3);
2489 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 /* Raise SystemError when self argument is not a set or frozenset. */
2492 t = PyTuple_New(0);
2493 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2494 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2495 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 /* Raise SystemError when self argument is not a set. */
2498 f = PyFrozenSet_New(dup);
2499 assert(PySet_Size(f) == 3);
2500 assert(PyFrozenSet_CheckExact(f));
2501 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2502 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2503 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 /* Raise KeyError when popping from an empty set */
2506 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2507 Py_DECREF(ob);
2508 assert(PySet_GET_SIZE(ob) == 0);
2509 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 /* Restore the set from the copy using the PyNumber API */
2512 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2513 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 /* Verify constructors accept NULL arguments */
2516 f = PySet_New(NULL);
2517 assert(f != NULL);
2518 assert(PySet_GET_SIZE(f) == 0);
2519 Py_DECREF(f);
2520 f = PyFrozenSet_New(NULL);
2521 assert(f != NULL);
2522 assert(PyFrozenSet_CheckExact(f));
2523 assert(PySet_GET_SIZE(f) == 0);
2524 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 Py_DECREF(elem);
2527 Py_DECREF(dup);
2528 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002529}
2530
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002531#undef assertRaises
2532
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002533#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002534
2535/***** Dummy Struct *************************************************/
2536
2537static PyObject *
2538dummy_repr(PyObject *op)
2539{
2540 return PyUnicode_FromString("<dummy key>");
2541}
2542
2543static void
2544dummy_dealloc(PyObject* ignore)
2545{
2546 Py_FatalError("deallocating <dummy key>");
2547}
2548
2549static PyTypeObject _PySetDummy_Type = {
2550 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2551 "<dummy key> type",
2552 0,
2553 0,
2554 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2555 0, /*tp_print*/
2556 0, /*tp_getattr*/
2557 0, /*tp_setattr*/
2558 0, /*tp_reserved*/
2559 dummy_repr, /*tp_repr*/
2560 0, /*tp_as_number*/
2561 0, /*tp_as_sequence*/
2562 0, /*tp_as_mapping*/
2563 0, /*tp_hash */
2564 0, /*tp_call */
2565 0, /*tp_str */
2566 0, /*tp_getattro */
2567 0, /*tp_setattro */
2568 0, /*tp_as_buffer */
2569 Py_TPFLAGS_DEFAULT, /*tp_flags */
2570};
2571
2572static PyObject _dummy_struct = {
2573 _PyObject_EXTRA_INIT
2574 2, &_PySetDummy_Type
2575};
2576