blob: 219e81d0baf5fdc9cf652818a1372b89ad181189 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07007 The basic lookup function used by all operations.
8 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
9
10 The initial probe index is computed as hash mod the table size.
11 Subsequent probe indices are computed as explained in Objects/dictobject.c.
12
13 To improve cache locality, each probe inspects a series of consecutive
14 nearby entries before moving on to probes elsewhere in memory. This leaves
Raymond Hettinger64263df2017-09-04 18:54:16 -070015 us with a hybrid of linear probing and randomized probing. The linear probing
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070016 reduces the cost of hash collisions because consecutive memory accesses
17 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
Raymond Hettinger64263df2017-09-04 18:54:16 -070018 we then use more of the upper bits from the hash value and apply a simple
19 linear congruential random number genearator. This helps break-up long
20 chains of collisions.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070021
22 All arithmetic on hash should ignore overflow.
23
Raymond Hettinger4d45c102015-01-25 16:27:40 -080024 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070025 NULL if the rich comparison returns an error.
Raymond Hettinger64263df2017-09-04 18:54:16 -070026
27 Use cases for sets differ considerably from dictionaries where looked-up
28 keys are more likely to be present. In contrast, sets are primarily
29 about membership testing where the presence of an element is not known in
30 advance. Accordingly, the set implementation needs to optimize for both
31 the found and not-found case.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032*/
33
Raymond Hettingera690a992003-11-16 16:17:49 +000034#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000035#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000036
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000037/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050038static PyObject _dummy_struct;
39
40#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000041
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000042
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070043/* ======================================================================== */
44/* ======= Begin logic for probing the hash table ========================= */
45
Raymond Hettinger710a67e2013-09-21 20:17:31 -070046/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070047#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070048#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070049#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070050
51/* This must be >= 1 */
52#define PERTURB_SHIFT 5
53
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000054static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020055set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000056{
Raymond Hettinger70559b52015-07-23 07:42:23 -040057 setentry *table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020058 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070059 size_t perturb;
Raymond Hettingerafe89092013-08-28 20:59:31 -070060 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080061 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070062 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070063 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000064
Raymond Hettinger70559b52015-07-23 07:42:23 -040065 entry = &so->table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070066 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070068
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070069 perturb = hash;
70
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070071 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080072 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070073 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080074 /* startkey cannot be a dummy because the dummy hash field is -1 */
75 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080076 if (startkey == key)
77 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080078 if (PyUnicode_CheckExact(startkey)
79 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -070080 && _PyUnicode_EQ(startkey, key))
Raymond Hettinger93035c42015-01-25 16:12:49 -080081 return entry;
Raymond Hettinger70559b52015-07-23 07:42:23 -040082 table = so->table;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 Py_INCREF(startkey);
84 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
85 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010086 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010088 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010090 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070091 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060092 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070093 }
Raymond Hettingerafe89092013-08-28 20:59:31 -070094
Raymond Hettingerc658d852015-02-02 08:35:00 -080095 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080096 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080097 entry++;
Raymond Hettinger8651a502015-05-27 10:37:20 -070098 if (entry->hash == 0 && entry->key == NULL)
99 return entry;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800100 if (entry->hash == hash) {
101 PyObject *startkey = entry->key;
102 assert(startkey != dummy);
103 if (startkey == key)
104 return entry;
105 if (PyUnicode_CheckExact(startkey)
106 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -0700107 && _PyUnicode_EQ(startkey, key))
Raymond Hettingerc658d852015-02-02 08:35:00 -0800108 return entry;
Raymond Hettinger70559b52015-07-23 07:42:23 -0400109 table = so->table;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800110 Py_INCREF(startkey);
111 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
112 Py_DECREF(startkey);
113 if (cmp < 0)
114 return NULL;
115 if (table != so->table || entry->key != startkey)
116 return set_lookkey(so, key, hash);
117 if (cmp > 0)
118 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600119 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800120 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700121 }
122 }
123
124 perturb >>= PERTURB_SHIFT;
125 i = (i * 5 + 1 + perturb) & mask;
126
Raymond Hettinger70559b52015-07-23 07:42:23 -0400127 entry = &so->table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700128 if (entry->key == NULL)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700129 return entry;
130 }
131}
132
Raymond Hettinger15f08692015-07-03 17:21:17 -0700133static int set_table_resize(PySetObject *, Py_ssize_t);
134
Raymond Hettinger8651a502015-05-27 10:37:20 -0700135static int
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400136set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700137{
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400138 setentry *table;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700139 setentry *freeslot;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700140 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700141 size_t perturb;
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400142 size_t mask;
143 size_t i; /* Unsigned for defined overflow behavior */
Raymond Hettinger8651a502015-05-27 10:37:20 -0700144 size_t j;
145 int cmp;
146
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400147 /* Pre-increment is necessary to prevent arbitrary code in the rich
148 comparison from deallocating the key just before the insertion. */
149 Py_INCREF(key);
150
151 restart:
152
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400153 mask = so->mask;
154 i = (size_t)hash & mask;
155
Raymond Hettinger70559b52015-07-23 07:42:23 -0400156 entry = &so->table[i];
Raymond Hettinger8651a502015-05-27 10:37:20 -0700157 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700158 goto found_unused;
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700159
160 freeslot = NULL;
161 perturb = hash;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700162
163 while (1) {
164 if (entry->hash == hash) {
165 PyObject *startkey = entry->key;
166 /* startkey cannot be a dummy because the dummy hash field is -1 */
167 assert(startkey != dummy);
168 if (startkey == key)
169 goto found_active;
170 if (PyUnicode_CheckExact(startkey)
171 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -0700172 && _PyUnicode_EQ(startkey, key))
Raymond Hettinger8651a502015-05-27 10:37:20 -0700173 goto found_active;
Raymond Hettinger70559b52015-07-23 07:42:23 -0400174 table = so->table;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700175 Py_INCREF(startkey);
176 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
177 Py_DECREF(startkey);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700178 if (cmp > 0) /* likely */
179 goto found_active;
Raymond Hettingerdaffc912015-07-31 07:58:56 -0700180 if (cmp < 0)
181 goto comparison_error;
182 /* Continuing the search from the current entry only makes
183 sense if the table and entry are unchanged; otherwise,
184 we have to restart from the beginning */
185 if (table != so->table || entry->key != startkey)
186 goto restart;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700187 mask = so->mask; /* help avoid a register spill */
188 }
Raymond Hettinger70559b52015-07-23 07:42:23 -0400189 else if (entry->hash == -1 && freeslot == NULL)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700190 freeslot = entry;
191
192 if (i + LINEAR_PROBES <= mask) {
193 for (j = 0 ; j < LINEAR_PROBES ; j++) {
194 entry++;
195 if (entry->hash == 0 && entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700196 goto found_unused_or_dummy;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700197 if (entry->hash == hash) {
198 PyObject *startkey = entry->key;
199 assert(startkey != dummy);
200 if (startkey == key)
201 goto found_active;
202 if (PyUnicode_CheckExact(startkey)
203 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -0700204 && _PyUnicode_EQ(startkey, key))
Raymond Hettinger8651a502015-05-27 10:37:20 -0700205 goto found_active;
Raymond Hettinger70559b52015-07-23 07:42:23 -0400206 table = so->table;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700207 Py_INCREF(startkey);
208 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
209 Py_DECREF(startkey);
Raymond Hettingerdaffc912015-07-31 07:58:56 -0700210 if (cmp > 0)
211 goto found_active;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700212 if (cmp < 0)
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400213 goto comparison_error;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700214 if (table != so->table || entry->key != startkey)
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400215 goto restart;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700216 mask = so->mask;
217 }
Raymond Hettinger91672612015-06-26 02:50:21 -0700218 else if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800219 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700220 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700222
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700223 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800224 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700225
Raymond Hettinger70559b52015-07-23 07:42:23 -0400226 entry = &so->table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700227 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700228 goto found_unused_or_dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700230
Raymond Hettinger15f08692015-07-03 17:21:17 -0700231 found_unused_or_dummy:
232 if (freeslot == NULL)
233 goto found_unused;
Raymond Hettinger15f08692015-07-03 17:21:17 -0700234 so->used++;
235 freeslot->key = key;
236 freeslot->hash = hash;
237 return 0;
238
239 found_unused:
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700240 so->fill++;
241 so->used++;
242 entry->key = key;
243 entry->hash = hash;
Raymond Hettinger5cd87a82017-02-04 02:43:42 -0800244 if ((size_t)so->fill*5 < mask*3)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700245 return 0;
INADA Naokie82cf862017-04-01 17:20:25 +0900246 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700247
248 found_active:
Raymond Hettinger482c05c2015-07-20 01:23:32 -0400249 Py_DECREF(key);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700250 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000251
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400252 comparison_error:
Raymond Hettinger061091a2015-07-15 23:54:02 -0700253 Py_DECREF(key);
254 return -1;
255}
256
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000257/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000258Internal routine used by set_table_resize() to insert an item which is
259known to be absent from the set. This routine also assumes that
260the set contains no deleted entries. Besides the performance benefit,
Raymond Hettinger6019c8c2015-11-17 08:28:07 -0800261there is also safety benefit since using set_add_entry() risks making
262a callback in the middle of a set_table_resize(), see issue 1456209.
263The caller is responsible for updating the key's reference count and
264the setobject's fill and used fields.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000265*/
266static void
Raymond Hettinger5088f602015-12-13 18:45:01 -0800267set_insert_clean(setentry *table, size_t mask, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000268{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200269 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700270 size_t perturb = hash;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800271 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700272 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000273
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700274 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800275 entry = &table[i];
276 if (entry->key == NULL)
277 goto found_null;
278 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800279 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800280 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800281 if (entry->key == NULL)
282 goto found_null;
283 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700284 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700285 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800286 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700288 found_null:
Raymond Hettinger5088f602015-12-13 18:45:01 -0800289 entry->key = key;
Raymond Hettinger86d322f2015-12-13 19:27:17 -0800290 entry->hash = hash;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000291}
292
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700293/* ======== End logic for probing the hash table ========================== */
294/* ======================================================================== */
295
Thomas Wouters89f507f2006-12-13 04:49:30 +0000296/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000297Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000298keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000299actually be smaller than the old one.
300*/
301static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 Py_ssize_t newsize;
305 setentry *oldtable, *newtable, *entry;
Raymond Hettinger5088f602015-12-13 18:45:01 -0800306 Py_ssize_t oldmask = so->mask;
307 size_t newmask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 int is_oldtable_malloced;
309 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100314 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 for (newsize = PySet_MINSIZE;
316 newsize <= minused && newsize > 0;
317 newsize <<= 1)
318 ;
319 if (newsize <= 0) {
320 PyErr_NoMemory();
321 return -1;
322 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 /* Get space for a new table. */
325 oldtable = so->table;
326 assert(oldtable != NULL);
327 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 if (newsize == PySet_MINSIZE) {
330 /* A large table is shrinking, or we can't get any smaller. */
331 newtable = so->smalltable;
332 if (newtable == oldtable) {
333 if (so->fill == so->used) {
334 /* No dummies, so no point doing anything. */
335 return 0;
336 }
337 /* We're not going to resize it, but rebuild the
338 table anyway to purge old dummy entries.
339 Subtle: This is *necessary* if fill==size,
340 as set_lookkey needs at least one virgin slot to
341 terminate failing searches. If fill < size, it's
342 merely desirable, as dummies slow searches. */
343 assert(so->fill > so->used);
344 memcpy(small_copy, oldtable, sizeof(small_copy));
345 oldtable = small_copy;
346 }
347 }
348 else {
349 newtable = PyMem_NEW(setentry, newsize);
350 if (newtable == NULL) {
351 PyErr_NoMemory();
352 return -1;
353 }
354 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 /* Make the set empty, using the new table. */
357 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800359 so->mask = newsize - 1;
360 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 /* Copy the data over; this is refcount-neutral for active entries;
363 dummy entries aren't copied over, of course */
Raymond Hettinger5088f602015-12-13 18:45:01 -0800364 newmask = (size_t)so->mask;
Raymond Hettingere1af6962017-02-02 08:24:48 -0800365 if (so->fill == so->used) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800366 for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800367 if (entry->key != NULL) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800368 set_insert_clean(newtable, newmask, entry->key, entry->hash);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800369 }
370 }
371 } else {
Raymond Hettingere1af6962017-02-02 08:24:48 -0800372 so->fill = so->used;
Raymond Hettinger5088f602015-12-13 18:45:01 -0800373 for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800374 if (entry->key != NULL && entry->key != dummy) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800375 set_insert_clean(newtable, newmask, entry->key, entry->hash);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800376 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 }
378 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (is_oldtable_malloced)
381 PyMem_DEL(oldtable);
382 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000383}
384
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000385static int
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700386set_contains_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000387{
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700388 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000389
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700390 entry = set_lookkey(so, key, hash);
391 if (entry != NULL)
392 return entry->key != NULL;
393 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394}
395
396#define DISCARD_NOTFOUND 0
397#define DISCARD_FOUND 1
398
399static int
Raymond Hettinger73799b12015-07-05 16:06:10 -0700400set_discard_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800401{
402 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000404
Raymond Hettingerdc28d5a2015-07-05 10:03:20 -0700405 entry = set_lookkey(so, key, hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 if (entry == NULL)
407 return -1;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700408 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 return DISCARD_NOTFOUND;
410 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800412 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 so->used--;
414 Py_DECREF(old_key);
415 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000416}
417
418static int
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700419set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000420{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200421 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000422
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700423 if (!PyUnicode_CheckExact(key) ||
424 (hash = ((PyASCIIObject *) key)->hash) == -1) {
425 hash = PyObject_Hash(key);
426 if (hash == -1)
427 return -1;
428 }
429 return set_add_entry(so, key, hash);
430}
431
432static int
433set_contains_key(PySetObject *so, PyObject *key)
434{
435 Py_hash_t hash;
436
437 if (!PyUnicode_CheckExact(key) ||
438 (hash = ((PyASCIIObject *) key)->hash) == -1) {
439 hash = PyObject_Hash(key);
440 if (hash == -1)
441 return -1;
442 }
443 return set_contains_entry(so, key, hash);
444}
445
446static int
447set_discard_key(PySetObject *so, PyObject *key)
448{
449 Py_hash_t hash;
Christian Heimes0ded5b52007-12-10 15:50:56 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200452 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 hash = PyObject_Hash(key);
454 if (hash == -1)
455 return -1;
456 }
Raymond Hettinger73799b12015-07-05 16:06:10 -0700457 return set_discard_entry(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000458}
459
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700460static void
461set_empty_to_minsize(PySetObject *so)
462{
463 memset(so->smalltable, 0, sizeof(so->smalltable));
464 so->fill = 0;
465 so->used = 0;
466 so->mask = PySet_MINSIZE - 1;
467 so->table = so->smalltable;
468 so->hash = -1;
469}
470
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000471static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472set_clear_internal(PySetObject *so)
473{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800474 setentry *entry;
475 setentry *table = so->table;
476 Py_ssize_t fill = so->fill;
477 Py_ssize_t used = so->used;
478 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000480
Raymond Hettinger583cd032013-09-07 22:06:35 -0700481 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 /* This is delicate. During the process of clearing the set,
485 * decrefs can cause the set to mutate. To avoid fatal confusion
486 * (voice of experience), we have to make the set empty before
487 * clearing the slots, and never refer to anything via so->ref while
488 * clearing.
489 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700491 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 else if (fill > 0) {
494 /* It's a small table with something that needs to be cleared.
495 * Afraid the only safe way is to copy the set entries into
496 * another small table first.
497 */
498 memcpy(small_copy, table, sizeof(small_copy));
499 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700500 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 }
502 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 /* Now we can finally clear things. If C had refcounts, we could
505 * assert that the refcount on table is 1 now, i.e. that this function
506 * has unique access to it, so decref side-effects can't alter it.
507 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800508 for (entry = table; used > 0; entry++) {
509 if (entry->key && entry->key != dummy) {
510 used--;
511 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if (table_is_malloced)
516 PyMem_DEL(table);
517 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000518}
519
520/*
521 * Iterate over a set table. Use like so:
522 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000523 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000524 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000525 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000526 * while (set_next(yourset, &pos, &entry)) {
527 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000528 * }
529 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000530 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532 */
533static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000534set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_ssize_t i;
537 Py_ssize_t mask;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700538 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 assert (PyAnySet_Check(so));
541 i = *pos_ptr;
542 assert(i >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 mask = so->mask;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700544 entry = &so->table[i];
545 while (i <= mask && (entry->key == NULL || entry->key == dummy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 i++;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700547 entry++;
548 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 *pos_ptr = i+1;
550 if (i > mask)
551 return 0;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700552 assert(entry != NULL);
553 *entry_ptr = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000555}
556
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000557static void
558set_dealloc(PySetObject *so)
559{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200560 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800561 Py_ssize_t used = so->used;
562
INADA Naokia6296d32017-08-24 14:55:17 +0900563 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 PyObject_GC_UnTrack(so);
565 Py_TRASHCAN_SAFE_BEGIN(so)
566 if (so->weakreflist != NULL)
567 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800569 for (entry = so->table; used > 0; entry++) {
570 if (entry->key && entry->key != dummy) {
571 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700572 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 }
574 }
575 if (so->table != so->smalltable)
576 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700577 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000579}
580
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000581static PyObject *
582set_repr(PySetObject *so)
583{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200584 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if (status != 0) {
588 if (status < 0)
589 return NULL;
590 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
591 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 /* shortcut for the empty set */
594 if (!so->used) {
595 Py_ReprLeave((PyObject*)so);
596 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
597 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 keys = PySequence_List((PyObject *)so);
600 if (keys == NULL)
601 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000602
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200603 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 listrepr = PyObject_Repr(keys);
605 Py_DECREF(keys);
606 if (listrepr == NULL)
607 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200608 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200610 if (tmp == NULL)
611 goto done;
612 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200613
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200614 if (Py_TYPE(so) != &PySet_Type)
615 result = PyUnicode_FromFormat("%s({%U})",
616 Py_TYPE(so)->tp_name,
617 listrepr);
618 else
619 result = PyUnicode_FromFormat("{%U}", listrepr);
620 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000621done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 Py_ReprLeave((PyObject*)so);
623 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000624}
625
Martin v. Löwis18e16552006-02-15 17:27:45 +0000626static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000627set_len(PyObject *so)
628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000630}
631
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000632static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000633set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000636 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200637 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700638 setentry *so_entry;
639 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 assert (PyAnySet_Check(so));
642 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 other = (PySetObject*)otherset;
645 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700646 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 return 0;
648 /* Do one big resize at the start, rather than
649 * incrementally resizing as we insert new keys. Expect
650 * that there will be no (or few) overlapping keys.
651 */
Raymond Hettinger5cd87a82017-02-04 02:43:42 -0800652 if ((so->fill + other->used)*5 >= so->mask*3) {
INADA Naokie82cf862017-04-01 17:20:25 +0900653 if (set_table_resize(so, (so->used + other->used)*2) != 0)
654 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700656 so_entry = so->table;
657 other_entry = other->table;
658
659 /* If our table is empty, and both tables have the same size, and
660 there are no dummies to eliminate, then just copy the pointers. */
661 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
662 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
663 key = other_entry->key;
664 if (key != NULL) {
665 assert(so_entry->key == NULL);
666 Py_INCREF(key);
667 so_entry->key = key;
668 so_entry->hash = other_entry->hash;
669 }
670 }
671 so->fill = other->fill;
672 so->used = other->used;
673 return 0;
674 }
675
676 /* If our table is empty, we can use set_insert_clean() */
677 if (so->fill == 0) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800678 setentry *newtable = so->table;
679 size_t newmask = (size_t)so->mask;
Raymond Hettinger6019c8c2015-11-17 08:28:07 -0800680 so->fill = other->used;
681 so->used = other->used;
Raymond Hettingere4495872015-12-15 00:42:30 -0800682 for (i = other->mask + 1; i > 0 ; i--, other_entry++) {
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700683 key = other_entry->key;
684 if (key != NULL && key != dummy) {
685 Py_INCREF(key);
Raymond Hettinger5088f602015-12-13 18:45:01 -0800686 set_insert_clean(newtable, newmask, key, other_entry->hash);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700687 }
688 }
689 return 0;
690 }
691
692 /* We can't assure there are no duplicates, so do normal insertions */
Raymond Hettingera3626bc2015-07-15 23:50:14 -0700693 for (i = 0; i <= other->mask; i++) {
694 other_entry = &other->table[i];
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700695 key = other_entry->key;
696 if (key != NULL && key != dummy) {
Raymond Hettinger73799b12015-07-05 16:06:10 -0700697 if (set_add_entry(so, key, other_entry->hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 }
700 }
701 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000702}
703
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000704static PyObject *
705set_pop(PySetObject *so)
706{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800707 /* Make sure the search finger is in bounds */
708 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200709 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 assert (PyAnySet_Check(so));
713 if (so->used == 0) {
714 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
715 return NULL;
716 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000717
Raymond Hettinger1202a472015-01-18 13:12:42 -0800718 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
719 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800720 if (i > so->mask)
721 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 }
723 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800725 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800727 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000729}
730
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000731PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
732Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000733
734static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000735set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 Py_ssize_t pos = 0;
738 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 while (set_next(so, &pos, &entry))
741 Py_VISIT(entry->key);
742 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000743}
744
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700745/* Work to increase the bit dispersion for closely spaced hash values.
746 This is important because some use cases have many combinations of a
747 small number of elements with nearby hashes so that many distinct
748 combinations collapse to only a handful of distinct hash values. */
749
750static Py_uhash_t
751_shuffle_bits(Py_uhash_t h)
752{
753 return ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
754}
755
756/* Most of the constants in this hash algorithm are randomly chosen
757 large primes with "interesting bit patterns" and that passed tests
758 for good collision statistics on a variety of problematic datasets
759 including powersets and graph structures (such as David Eppstein's
760 graph recipes in Lib/test/test_set.py) */
761
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000762static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000763frozenset_hash(PyObject *self)
764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 PySetObject *so = (PySetObject *)self;
Raymond Hettinger41481952015-08-07 00:43:39 -0700766 Py_uhash_t hash = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000768
Raymond Hettingerb501a272015-08-06 22:15:22 -0700769 if (so->hash != -1)
770 return so->hash;
771
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700772 /* Xor-in shuffled bits from every entry's hash field because xor is
773 commutative and a frozenset hash should be independent of order.
774
775 For speed, include null entries and dummy entries and then
776 subtract out their effect afterwards so that the final hash
777 depends only on active entries. This allows the code to be
778 vectorized by the compiler and it saves the unpredictable
779 branches that would arise when trying to exclude null and dummy
780 entries on every iteration. */
781
782 for (entry = so->table; entry <= &so->table[so->mask]; entry++)
783 hash ^= _shuffle_bits(entry->hash);
784
Raymond Hettingera286a512015-08-01 11:07:11 -0700785 /* Remove the effect of an odd number of NULL entries */
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700786 if ((so->mask + 1 - so->fill) & 1)
787 hash ^= _shuffle_bits(0);
788
789 /* Remove the effect of an odd number of dummy entries */
790 if ((so->fill - so->used) & 1)
791 hash ^= _shuffle_bits(-1);
792
Raymond Hettinger41481952015-08-07 00:43:39 -0700793 /* Factor in the number of active entries */
794 hash ^= ((Py_uhash_t)PySet_GET_SIZE(self) + 1) * 1927868237UL;
795
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700796 /* Disperse patterns arising in nested frozensets */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800797 hash = hash * 69069U + 907133923UL;
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700798
Raymond Hettinger36c05002015-08-01 10:57:42 -0700799 /* -1 is reserved as an error code */
Victor Stinner12174a52014-08-15 23:17:38 +0200800 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800801 hash = 590923713UL;
Raymond Hettinger36c05002015-08-01 10:57:42 -0700802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 so->hash = hash;
804 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000805}
806
Raymond Hettingera9d99362005-08-05 00:01:15 +0000807/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000808
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyObject_HEAD
811 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
812 Py_ssize_t si_used;
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700813 Py_ssize_t si_pos;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815} setiterobject;
816
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000817static void
818setiter_dealloc(setiterobject *si)
819{
INADA Naokia6296d32017-08-24 14:55:17 +0900820 /* bpo-31095: UnTrack is needed before calling any callbacks */
821 _PyObject_GC_UNTRACK(si);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 Py_XDECREF(si->si_set);
823 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000824}
825
826static int
827setiter_traverse(setiterobject *si, visitproc visit, void *arg)
828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 Py_VISIT(si->si_set);
830 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831}
832
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000833static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000834setiter_len(setiterobject *si)
835{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 Py_ssize_t len = 0;
837 if (si->si_set != NULL && si->si_used == si->si_set->used)
838 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000839 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000840}
841
Armin Rigof5b3e362006-02-11 21:32:43 +0000842PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000843
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000844static PyObject *setiter_iternext(setiterobject *si);
845
846static PyObject *
847setiter_reduce(setiterobject *si)
848{
849 PyObject *list;
850 setiterobject tmp;
851
852 list = PyList_New(0);
853 if (!list)
854 return NULL;
855
Ezio Melotti0e1af282012-09-28 16:43:40 +0300856 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000857 tmp = *si;
858 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300859
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000860 /* iterate the temporary into a list */
861 for(;;) {
862 PyObject *element = setiter_iternext(&tmp);
863 if (element) {
864 if (PyList_Append(list, element)) {
865 Py_DECREF(element);
866 Py_DECREF(list);
867 Py_XDECREF(tmp.si_set);
868 return NULL;
869 }
870 Py_DECREF(element);
871 } else
872 break;
873 }
874 Py_XDECREF(tmp.si_set);
875 /* check for error */
876 if (tmp.si_set != NULL) {
877 /* we have an error */
878 Py_DECREF(list);
879 return NULL;
880 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200881 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000882}
883
884PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
885
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000886static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000888 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000890};
891
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000892static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000893{
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700894 PyObject *key;
895 Py_ssize_t i, mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200896 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 if (so == NULL)
900 return NULL;
901 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (si->si_used != so->used) {
904 PyErr_SetString(PyExc_RuntimeError,
905 "Set changed size during iteration");
906 si->si_used = -1; /* Make this state sticky */
907 return NULL;
908 }
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700909
910 i = si->si_pos;
911 assert(i>=0);
912 entry = so->table;
913 mask = so->mask;
914 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
915 i++;
916 si->si_pos = i+1;
917 if (i > mask)
918 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 si->len--;
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700920 key = entry[i].key;
921 Py_INCREF(key);
922 return key;
923
924fail:
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700925 si->si_set = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300926 Py_DECREF(so);
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700927 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000928}
929
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000930PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 PyVarObject_HEAD_INIT(&PyType_Type, 0)
932 "set_iterator", /* tp_name */
933 sizeof(setiterobject), /* tp_basicsize */
934 0, /* tp_itemsize */
935 /* methods */
936 (destructor)setiter_dealloc, /* tp_dealloc */
937 0, /* tp_print */
938 0, /* tp_getattr */
939 0, /* tp_setattr */
940 0, /* tp_reserved */
941 0, /* tp_repr */
942 0, /* tp_as_number */
943 0, /* tp_as_sequence */
944 0, /* tp_as_mapping */
945 0, /* tp_hash */
946 0, /* tp_call */
947 0, /* tp_str */
948 PyObject_GenericGetAttr, /* tp_getattro */
949 0, /* tp_setattro */
950 0, /* tp_as_buffer */
Raymond Hettinger5d2385f2015-07-08 11:52:27 -0700951 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 0, /* tp_doc */
953 (traverseproc)setiter_traverse, /* tp_traverse */
954 0, /* tp_clear */
955 0, /* tp_richcompare */
956 0, /* tp_weaklistoffset */
957 PyObject_SelfIter, /* tp_iter */
958 (iternextfunc)setiter_iternext, /* tp_iternext */
959 setiter_methods, /* tp_methods */
960 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000961};
962
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000963static PyObject *
964set_iter(PySetObject *so)
965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
967 if (si == NULL)
968 return NULL;
969 Py_INCREF(so);
970 si->si_set = so;
971 si->si_used = so->used;
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700972 si->si_pos = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 si->len = so->used;
974 _PyObject_GC_TRACK(si);
975 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000976}
977
Raymond Hettingerd7946662005-08-01 21:39:29 +0000978static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000979set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 if (PyAnySet_Check(other))
984 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 if (PyDict_CheckExact(other)) {
987 PyObject *value;
988 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000989 Py_hash_t hash;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200990 Py_ssize_t dictsize = PyDict_GET_SIZE(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 /* Do one big resize at the start, rather than
993 * incrementally resizing as we insert new keys. Expect
994 * that there will be no (or few) overlapping keys.
995 */
Raymond Hettingerc2480dc2015-07-04 08:46:31 -0700996 if (dictsize < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 return -1;
Raymond Hettinger5cd87a82017-02-04 02:43:42 -0800998 if ((so->fill + dictsize)*5 >= so->mask*3) {
INADA Naokie82cf862017-04-01 17:20:25 +0900999 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 return -1;
1001 }
1002 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001003 if (set_add_entry(so, key, hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 return -1;
1005 }
1006 return 0;
1007 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 it = PyObject_GetIter(other);
1010 if (it == NULL)
1011 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001014 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 Py_DECREF(it);
1016 Py_DECREF(key);
1017 return -1;
1018 }
1019 Py_DECREF(key);
1020 }
1021 Py_DECREF(it);
1022 if (PyErr_Occurred())
1023 return -1;
1024 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001025}
1026
1027static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001028set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1033 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001034 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 return NULL;
1036 }
1037 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001038}
1039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001041"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001042
Raymond Hettinger426d9952014-05-18 21:40:20 +01001043/* XXX Todo:
1044 If aligned memory allocations become available, make the
1045 set object 64 byte aligned so that most of the fields
1046 can be retrieved or updated in a single cache line.
1047*/
1048
Raymond Hettingera38123e2003-11-24 22:18:49 +00001049static PyObject *
1050make_new_set(PyTypeObject *type, PyObject *iterable)
1051{
Raymond Hettinger8421d712016-04-29 01:37:05 -07001052 PySetObject *so;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001053
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001054 so = (PySetObject *)type->tp_alloc(type, 0);
1055 if (so == NULL)
1056 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001057
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001058 so->fill = 0;
1059 so->used = 0;
1060 so->mask = PySet_MINSIZE - 1;
1061 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001062 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -08001063 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001067 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 Py_DECREF(so);
1069 return NULL;
1070 }
1071 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001074}
1075
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001076static PyObject *
1077make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1080 if (PyType_IsSubtype(type, &PySet_Type))
1081 type = &PySet_Type;
1082 else
1083 type = &PyFrozenSet_Type;
1084 }
1085 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001086}
1087
Raymond Hettingerd7946662005-08-01 21:39:29 +00001088/* The empty frozenset is a singleton */
1089static PyObject *emptyfrozenset = NULL;
1090
Raymond Hettingera690a992003-11-16 16:17:49 +00001091static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001092frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001095
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001096 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1100 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 if (type != &PyFrozenSet_Type)
1103 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (iterable != NULL) {
1106 /* frozenset(f) is idempotent */
1107 if (PyFrozenSet_CheckExact(iterable)) {
1108 Py_INCREF(iterable);
1109 return iterable;
1110 }
1111 result = make_new_set(type, iterable);
1112 if (result == NULL || PySet_GET_SIZE(result))
1113 return result;
1114 Py_DECREF(result);
1115 }
1116 /* The empty frozenset is a singleton */
1117 if (emptyfrozenset == NULL)
1118 emptyfrozenset = make_new_set(type, NULL);
1119 Py_XINCREF(emptyfrozenset);
1120 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001121}
1122
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001123static PyObject *
1124set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001127}
1128
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001129/* set_swap_bodies() switches the contents of any two sets by moving their
1130 internal data pointers and, if needed, copying the internal smalltables.
1131 Semantically equivalent to:
1132
1133 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1134
1135 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001136 Useful for operations that update in-place (by allowing an intermediate
1137 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001138*/
1139
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001140static void
1141set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 Py_ssize_t t;
1144 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001146 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 t = a->fill; a->fill = b->fill; b->fill = t;
1149 t = a->used; a->used = b->used; b->used = t;
1150 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 u = a->table;
1153 if (a->table == a->smalltable)
1154 u = b->smalltable;
1155 a->table = b->table;
1156 if (b->table == b->smalltable)
1157 a->table = a->smalltable;
1158 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 if (a->table == a->smalltable || b->table == b->smalltable) {
1161 memcpy(tab, a->smalltable, sizeof(tab));
1162 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1163 memcpy(b->smalltable, tab, sizeof(tab));
1164 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1167 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1168 h = a->hash; a->hash = b->hash; b->hash = h;
1169 } else {
1170 a->hash = -1;
1171 b->hash = -1;
1172 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001173}
1174
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001175static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001176set_copy(PySetObject *so)
1177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001179}
1180
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001181static PyObject *
1182frozenset_copy(PySetObject *so)
1183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 if (PyFrozenSet_CheckExact(so)) {
1185 Py_INCREF(so);
1186 return (PyObject *)so;
1187 }
1188 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001189}
1190
Raymond Hettingera690a992003-11-16 16:17:49 +00001191PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1192
1193static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001194set_clear(PySetObject *so)
1195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 set_clear_internal(so);
1197 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001198}
1199
1200PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1201
1202static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001203set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 PySetObject *result;
1206 PyObject *other;
1207 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 result = (PySetObject *)set_copy(so);
1210 if (result == NULL)
1211 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1214 other = PyTuple_GET_ITEM(args, i);
1215 if ((PyObject *)so == other)
1216 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001217 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 Py_DECREF(result);
1219 return NULL;
1220 }
1221 }
1222 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001223}
1224
1225PyDoc_STRVAR(union_doc,
1226 "Return the union of sets as a new set.\n\
1227\n\
1228(i.e. all elements that are in either set.)");
1229
1230static PyObject *
1231set_or(PySetObject *so, PyObject *other)
1232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001234
Brian Curtindfc80e32011-08-10 20:28:54 -05001235 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1236 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 result = (PySetObject *)set_copy(so);
1239 if (result == NULL)
1240 return NULL;
1241 if ((PyObject *)so == other)
1242 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001243 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 Py_DECREF(result);
1245 return NULL;
1246 }
1247 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001248}
1249
Raymond Hettingera690a992003-11-16 16:17:49 +00001250static PyObject *
1251set_ior(PySetObject *so, PyObject *other)
1252{
Brian Curtindfc80e32011-08-10 20:28:54 -05001253 if (!PyAnySet_Check(other))
1254 Py_RETURN_NOTIMPLEMENTED;
1255
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001256 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 return NULL;
1258 Py_INCREF(so);
1259 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001260}
1261
1262static PyObject *
1263set_intersection(PySetObject *so, PyObject *other)
1264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 PySetObject *result;
1266 PyObject *key, *it, *tmp;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001267 Py_hash_t hash;
1268 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if ((PyObject *)so == other)
1271 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1274 if (result == NULL)
1275 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (PyAnySet_Check(other)) {
1278 Py_ssize_t pos = 0;
1279 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1282 tmp = (PyObject *)so;
1283 so = (PySetObject *)other;
1284 other = tmp;
1285 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001288 key = entry->key;
1289 hash = entry->hash;
1290 rv = set_contains_entry(so, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001291 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 Py_DECREF(result);
1293 return NULL;
1294 }
1295 if (rv) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001296 if (set_add_entry(result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 Py_DECREF(result);
1298 return NULL;
1299 }
1300 }
1301 }
1302 return (PyObject *)result;
1303 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 it = PyObject_GetIter(other);
1306 if (it == NULL) {
1307 Py_DECREF(result);
1308 return NULL;
1309 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001312 hash = PyObject_Hash(key);
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001313 if (hash == -1)
1314 goto error;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001315 rv = set_contains_entry(so, key, hash);
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001316 if (rv < 0)
1317 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if (rv) {
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001319 if (set_add_entry(result, key, hash))
1320 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 }
1322 Py_DECREF(key);
1323 }
1324 Py_DECREF(it);
1325 if (PyErr_Occurred()) {
1326 Py_DECREF(result);
1327 return NULL;
1328 }
1329 return (PyObject *)result;
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001330 error:
1331 Py_DECREF(it);
1332 Py_DECREF(result);
1333 Py_DECREF(key);
1334 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001335}
1336
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001337static PyObject *
1338set_intersection_multi(PySetObject *so, PyObject *args)
1339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 Py_ssize_t i;
1341 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (PyTuple_GET_SIZE(args) == 0)
1344 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 Py_INCREF(so);
1347 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1348 PyObject *other = PyTuple_GET_ITEM(args, i);
1349 PyObject *newresult = set_intersection((PySetObject *)result, other);
1350 if (newresult == NULL) {
1351 Py_DECREF(result);
1352 return NULL;
1353 }
1354 Py_DECREF(result);
1355 result = newresult;
1356 }
1357 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001358}
1359
Raymond Hettingera690a992003-11-16 16:17:49 +00001360PyDoc_STRVAR(intersection_doc,
1361"Return the intersection of two sets as a new set.\n\
1362\n\
1363(i.e. all elements that are in both sets.)");
1364
1365static PyObject *
1366set_intersection_update(PySetObject *so, PyObject *other)
1367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 tmp = set_intersection(so, other);
1371 if (tmp == NULL)
1372 return NULL;
1373 set_swap_bodies(so, (PySetObject *)tmp);
1374 Py_DECREF(tmp);
1375 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001376}
1377
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001378static PyObject *
1379set_intersection_update_multi(PySetObject *so, PyObject *args)
1380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 tmp = set_intersection_multi(so, args);
1384 if (tmp == NULL)
1385 return NULL;
1386 set_swap_bodies(so, (PySetObject *)tmp);
1387 Py_DECREF(tmp);
1388 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001389}
1390
Raymond Hettingera690a992003-11-16 16:17:49 +00001391PyDoc_STRVAR(intersection_update_doc,
1392"Update a set with the intersection of itself and another.");
1393
1394static PyObject *
1395set_and(PySetObject *so, PyObject *other)
1396{
Brian Curtindfc80e32011-08-10 20:28:54 -05001397 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1398 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001400}
1401
1402static PyObject *
1403set_iand(PySetObject *so, PyObject *other)
1404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001406
Brian Curtindfc80e32011-08-10 20:28:54 -05001407 if (!PyAnySet_Check(other))
1408 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 result = set_intersection_update(so, other);
1410 if (result == NULL)
1411 return NULL;
1412 Py_DECREF(result);
1413 Py_INCREF(so);
1414 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001415}
1416
Guido van Rossum58da9312007-11-10 23:39:45 +00001417static PyObject *
1418set_isdisjoint(PySetObject *so, PyObject *other)
1419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 PyObject *key, *it, *tmp;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001421 int rv;
Guido van Rossum58da9312007-11-10 23:39:45 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if ((PyObject *)so == other) {
1424 if (PySet_GET_SIZE(so) == 0)
1425 Py_RETURN_TRUE;
1426 else
1427 Py_RETURN_FALSE;
1428 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 if (PyAnySet_CheckExact(other)) {
1431 Py_ssize_t pos = 0;
1432 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1435 tmp = (PyObject *)so;
1436 so = (PySetObject *)other;
1437 other = tmp;
1438 }
1439 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001440 rv = set_contains_entry(so, entry->key, entry->hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001441 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 return NULL;
1443 if (rv)
1444 Py_RETURN_FALSE;
1445 }
1446 Py_RETURN_TRUE;
1447 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 it = PyObject_GetIter(other);
1450 if (it == NULL)
1451 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 while ((key = PyIter_Next(it)) != NULL) {
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001454 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 if (hash == -1) {
1457 Py_DECREF(key);
1458 Py_DECREF(it);
1459 return NULL;
1460 }
Raymond Hettinger73799b12015-07-05 16:06:10 -07001461 rv = set_contains_entry(so, key, hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 Py_DECREF(key);
Raymond Hettingerc2480dc2015-07-04 08:46:31 -07001463 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 Py_DECREF(it);
1465 return NULL;
1466 }
1467 if (rv) {
1468 Py_DECREF(it);
1469 Py_RETURN_FALSE;
1470 }
1471 }
1472 Py_DECREF(it);
1473 if (PyErr_Occurred())
1474 return NULL;
1475 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001476}
1477
1478PyDoc_STRVAR(isdisjoint_doc,
1479"Return True if two sets have a null intersection.");
1480
Neal Norwitz6576bd82005-11-13 18:41:28 +00001481static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001482set_difference_update_internal(PySetObject *so, PyObject *other)
1483{
Raymond Hettinger4103e4d2016-09-11 22:02:28 -07001484 if (PySet_GET_SIZE(so) == 0) {
1485 return 0;
1486 }
1487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 if ((PyObject *)so == other)
1489 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 if (PyAnySet_Check(other)) {
1492 setentry *entry;
1493 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettinger73799b12015-07-05 16:06:10 -07001496 if (set_discard_entry(so, entry->key, entry->hash) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 return -1;
1498 } else {
1499 PyObject *key, *it;
1500 it = PyObject_GetIter(other);
1501 if (it == NULL)
1502 return -1;
1503
1504 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerb3223262015-07-03 23:37:16 -07001505 if (set_discard_key(so, key) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 Py_DECREF(it);
1507 Py_DECREF(key);
1508 return -1;
1509 }
1510 Py_DECREF(key);
1511 }
1512 Py_DECREF(it);
1513 if (PyErr_Occurred())
1514 return -1;
1515 }
Raymond Hettingere186c762015-07-04 11:28:35 -07001516 /* If more than 1/4th are dummies, then resize them away. */
1517 if ((size_t)(so->fill - so->used) <= (size_t)so->mask / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 return 0;
INADA Naokie82cf862017-04-01 17:20:25 +09001519 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001520}
1521
Raymond Hettingera690a992003-11-16 16:17:49 +00001522static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001523set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1528 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001529 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 return NULL;
1531 }
1532 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001533}
1534
1535PyDoc_STRVAR(difference_update_doc,
1536"Remove all elements of another set from this set.");
1537
1538static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001539set_copy_and_difference(PySetObject *so, PyObject *other)
1540{
1541 PyObject *result;
1542
1543 result = set_copy(so);
1544 if (result == NULL)
1545 return NULL;
Raymond Hettingerb3223262015-07-03 23:37:16 -07001546 if (set_difference_update_internal((PySetObject *) result, other) == 0)
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001547 return result;
1548 Py_DECREF(result);
1549 return NULL;
1550}
1551
1552static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001553set_difference(PySetObject *so, PyObject *other)
1554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 PyObject *result;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001556 PyObject *key;
1557 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 setentry *entry;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03001559 Py_ssize_t pos = 0, other_size;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001560 int rv;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001561
Raymond Hettinger4103e4d2016-09-11 22:02:28 -07001562 if (PySet_GET_SIZE(so) == 0) {
1563 return set_copy(so);
1564 }
1565
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03001566 if (PyAnySet_Check(other)) {
1567 other_size = PySet_GET_SIZE(other);
1568 }
1569 else if (PyDict_CheckExact(other)) {
1570 other_size = PyDict_GET_SIZE(other);
1571 }
1572 else {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001573 return set_copy_and_difference(so, other);
1574 }
1575
1576 /* If len(so) much more than len(other), it's more efficient to simply copy
1577 * so and then iterate other looking for common elements. */
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03001578 if ((PySet_GET_SIZE(so) >> 2) > other_size) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001579 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 result = make_new_set_basetype(Py_TYPE(so), NULL);
1583 if (result == NULL)
1584 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 if (PyDict_CheckExact(other)) {
1587 while (set_next(so, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001588 key = entry->key;
1589 hash = entry->hash;
Raymond Hettinger15f08692015-07-03 17:21:17 -07001590 rv = _PyDict_Contains(other, key, hash);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001591 if (rv < 0) {
1592 Py_DECREF(result);
1593 return NULL;
1594 }
1595 if (!rv) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001596 if (set_add_entry((PySetObject *)result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 Py_DECREF(result);
1598 return NULL;
1599 }
1600 }
1601 }
1602 return result;
1603 }
1604
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001605 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 while (set_next(so, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001607 key = entry->key;
1608 hash = entry->hash;
1609 rv = set_contains_entry((PySetObject *)other, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001610 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 Py_DECREF(result);
1612 return NULL;
1613 }
1614 if (!rv) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001615 if (set_add_entry((PySetObject *)result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 Py_DECREF(result);
1617 return NULL;
1618 }
1619 }
1620 }
1621 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001622}
1623
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001624static PyObject *
1625set_difference_multi(PySetObject *so, PyObject *args)
1626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 Py_ssize_t i;
1628 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (PyTuple_GET_SIZE(args) == 0)
1631 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 other = PyTuple_GET_ITEM(args, 0);
1634 result = set_difference(so, other);
1635 if (result == NULL)
1636 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1639 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001640 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 Py_DECREF(result);
1642 return NULL;
1643 }
1644 }
1645 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001646}
1647
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001648PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001649"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001650\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001651(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001652static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001653set_sub(PySetObject *so, PyObject *other)
1654{
Brian Curtindfc80e32011-08-10 20:28:54 -05001655 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1656 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001658}
1659
1660static PyObject *
1661set_isub(PySetObject *so, PyObject *other)
1662{
Brian Curtindfc80e32011-08-10 20:28:54 -05001663 if (!PyAnySet_Check(other))
1664 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001665 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 return NULL;
1667 Py_INCREF(so);
1668 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001669}
1670
1671static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001672set_symmetric_difference_update(PySetObject *so, PyObject *other)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 PySetObject *otherset;
1675 PyObject *key;
1676 Py_ssize_t pos = 0;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001677 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 setentry *entry;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001679 int rv;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 if ((PyObject *)so == other)
1682 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (PyDict_CheckExact(other)) {
1685 PyObject *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001687 Py_INCREF(key);
Raymond Hettinger73799b12015-07-05 16:06:10 -07001688 rv = set_discard_entry(so, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001689 if (rv < 0) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001690 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001693 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001694 if (set_add_entry(so, key, hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001695 Py_DECREF(key);
1696 return NULL;
1697 }
1698 }
Georg Brandl2d444492010-09-03 10:52:55 +00001699 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 }
1701 Py_RETURN_NONE;
1702 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 if (PyAnySet_Check(other)) {
1705 Py_INCREF(other);
1706 otherset = (PySetObject *)other;
1707 } else {
1708 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1709 if (otherset == NULL)
1710 return NULL;
1711 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 while (set_next(otherset, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001714 key = entry->key;
1715 hash = entry->hash;
1716 rv = set_discard_entry(so, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001717 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 Py_DECREF(otherset);
1719 return NULL;
1720 }
1721 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001722 if (set_add_entry(so, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 Py_DECREF(otherset);
1724 return NULL;
1725 }
1726 }
1727 }
1728 Py_DECREF(otherset);
1729 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001730}
1731
1732PyDoc_STRVAR(symmetric_difference_update_doc,
1733"Update a set with the symmetric difference of itself and another.");
1734
1735static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001736set_symmetric_difference(PySetObject *so, PyObject *other)
1737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 PyObject *rv;
1739 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1742 if (otherset == NULL)
1743 return NULL;
1744 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1745 if (rv == NULL)
1746 return NULL;
1747 Py_DECREF(rv);
1748 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001749}
1750
1751PyDoc_STRVAR(symmetric_difference_doc,
1752"Return the symmetric difference of two sets as a new set.\n\
1753\n\
1754(i.e. all elements that are in exactly one of the sets.)");
1755
1756static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001757set_xor(PySetObject *so, PyObject *other)
1758{
Brian Curtindfc80e32011-08-10 20:28:54 -05001759 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1760 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001762}
1763
1764static PyObject *
1765set_ixor(PySetObject *so, PyObject *other)
1766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001768
Brian Curtindfc80e32011-08-10 20:28:54 -05001769 if (!PyAnySet_Check(other))
1770 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 result = set_symmetric_difference_update(so, other);
1772 if (result == NULL)
1773 return NULL;
1774 Py_DECREF(result);
1775 Py_INCREF(so);
1776 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001777}
1778
1779static PyObject *
1780set_issubset(PySetObject *so, PyObject *other)
1781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 setentry *entry;
1783 Py_ssize_t pos = 0;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001784 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 if (!PyAnySet_Check(other)) {
1787 PyObject *tmp, *result;
1788 tmp = make_new_set(&PySet_Type, other);
1789 if (tmp == NULL)
1790 return NULL;
1791 result = set_issubset(so, tmp);
1792 Py_DECREF(tmp);
1793 return result;
1794 }
1795 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1796 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 while (set_next(so, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001799 rv = set_contains_entry((PySetObject *)other, entry->key, entry->hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001800 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 return NULL;
1802 if (!rv)
1803 Py_RETURN_FALSE;
1804 }
1805 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001806}
1807
1808PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1809
1810static PyObject *
1811set_issuperset(PySetObject *so, PyObject *other)
1812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 if (!PyAnySet_Check(other)) {
1816 tmp = make_new_set(&PySet_Type, other);
1817 if (tmp == NULL)
1818 return NULL;
1819 result = set_issuperset(so, tmp);
1820 Py_DECREF(tmp);
1821 return result;
1822 }
1823 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001824}
1825
1826PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1827
Raymond Hettingera690a992003-11-16 16:17:49 +00001828static PyObject *
1829set_richcompare(PySetObject *v, PyObject *w, int op)
1830{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001831 PyObject *r1;
1832 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001833
Brian Curtindfc80e32011-08-10 20:28:54 -05001834 if(!PyAnySet_Check(w))
1835 Py_RETURN_NOTIMPLEMENTED;
1836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 switch (op) {
1838 case Py_EQ:
1839 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1840 Py_RETURN_FALSE;
1841 if (v->hash != -1 &&
1842 ((PySetObject *)w)->hash != -1 &&
1843 v->hash != ((PySetObject *)w)->hash)
1844 Py_RETURN_FALSE;
1845 return set_issubset(v, w);
1846 case Py_NE:
1847 r1 = set_richcompare(v, w, Py_EQ);
1848 if (r1 == NULL)
1849 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001850 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001852 if (r2 < 0)
1853 return NULL;
1854 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 case Py_LE:
1856 return set_issubset(v, w);
1857 case Py_GE:
1858 return set_issuperset(v, w);
1859 case Py_LT:
1860 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1861 Py_RETURN_FALSE;
1862 return set_issubset(v, w);
1863 case Py_GT:
1864 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1865 Py_RETURN_FALSE;
1866 return set_issuperset(v, w);
1867 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001868 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001869}
1870
Raymond Hettingera690a992003-11-16 16:17:49 +00001871static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001872set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001873{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001874 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 return NULL;
1876 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001877}
1878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001880"Add an element to a set.\n\
1881\n\
1882This has no effect if the element is already present.");
1883
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001884static int
1885set_contains(PySetObject *so, PyObject *key)
1886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 PyObject *tmpkey;
1888 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 rv = set_contains_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001891 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1893 return -1;
1894 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001895 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 if (tmpkey == NULL)
1897 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001898 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 Py_DECREF(tmpkey);
1900 }
1901 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001902}
1903
1904static PyObject *
1905set_direct_contains(PySetObject *so, PyObject *key)
1906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 result = set_contains(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001910 if (result < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 return NULL;
1912 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001913}
1914
1915PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1916
Raymond Hettingera690a992003-11-16 16:17:49 +00001917static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001918set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 PyObject *tmpkey;
1921 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 rv = set_discard_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001924 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1926 return NULL;
1927 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001928 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 if (tmpkey == NULL)
1930 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 Py_DECREF(tmpkey);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001933 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 return NULL;
1935 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001938 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 return NULL;
1940 }
1941 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001942}
1943
1944PyDoc_STRVAR(remove_doc,
1945"Remove an element from a set; it must be a member.\n\
1946\n\
1947If the element is not a member, raise a KeyError.");
1948
1949static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001950set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001951{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001952 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 rv = set_discard_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001956 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1958 return NULL;
1959 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001960 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (tmpkey == NULL)
1962 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001963 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 Py_DECREF(tmpkey);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001965 if (rv < 0)
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001966 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 }
1968 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001969}
1970
1971PyDoc_STRVAR(discard_doc,
1972"Remove an element from a set if it is a member.\n\
1973\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001975
1976static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001977set_reduce(PySetObject *so)
1978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001980 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 keys = PySequence_List((PyObject *)so);
1983 if (keys == NULL)
1984 goto done;
1985 args = PyTuple_Pack(1, keys);
1986 if (args == NULL)
1987 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001988 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 if (dict == NULL) {
1990 PyErr_Clear();
1991 dict = Py_None;
1992 Py_INCREF(dict);
1993 }
1994 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001995done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 Py_XDECREF(args);
1997 Py_XDECREF(keys);
1998 Py_XDECREF(dict);
1999 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002000}
2001
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002002static PyObject *
2003set_sizeof(PySetObject *so)
2004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002006
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002007 res = _PyObject_SIZE(Py_TYPE(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (so->table != so->smalltable)
2009 res = res + (so->mask + 1) * sizeof(setentry);
2010 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002011}
2012
2013PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002014static int
2015set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002018
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002019 if (!_PyArg_NoKeywords("set", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 return -1;
2021 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2022 return -1;
Raymond Hettingerb72e21b2016-03-25 02:29:59 -07002023 if (self->fill)
2024 set_clear_internal(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 self->hash = -1;
2026 if (iterable == NULL)
2027 return 0;
2028 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002029}
2030
Raymond Hettingera690a992003-11-16 16:17:49 +00002031static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 set_len, /* sq_length */
2033 0, /* sq_concat */
2034 0, /* sq_repeat */
2035 0, /* sq_item */
2036 0, /* sq_slice */
2037 0, /* sq_ass_item */
2038 0, /* sq_ass_slice */
2039 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002040};
2041
2042/* set object ********************************************************/
2043
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002044#ifdef Py_DEBUG
2045static PyObject *test_c_api(PySetObject *so);
2046
2047PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2048All is well if assertions don't fail.");
2049#endif
2050
Raymond Hettingera690a992003-11-16 16:17:49 +00002051static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 {"add", (PyCFunction)set_add, METH_O,
2053 add_doc},
2054 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2055 clear_doc},
2056 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2057 contains_doc},
2058 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2059 copy_doc},
2060 {"discard", (PyCFunction)set_discard, METH_O,
2061 discard_doc},
2062 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2063 difference_doc},
2064 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2065 difference_update_doc},
2066 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2067 intersection_doc},
2068 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2069 intersection_update_doc},
2070 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2071 isdisjoint_doc},
2072 {"issubset", (PyCFunction)set_issubset, METH_O,
2073 issubset_doc},
2074 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2075 issuperset_doc},
2076 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2077 pop_doc},
2078 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2079 reduce_doc},
2080 {"remove", (PyCFunction)set_remove, METH_O,
2081 remove_doc},
2082 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2083 sizeof_doc},
2084 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2085 symmetric_difference_doc},
2086 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2087 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002088#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2090 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002091#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 {"union", (PyCFunction)set_union, METH_VARARGS,
2093 union_doc},
2094 {"update", (PyCFunction)set_update, METH_VARARGS,
2095 update_doc},
2096 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002097};
2098
2099static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 0, /*nb_add*/
2101 (binaryfunc)set_sub, /*nb_subtract*/
2102 0, /*nb_multiply*/
2103 0, /*nb_remainder*/
2104 0, /*nb_divmod*/
2105 0, /*nb_power*/
2106 0, /*nb_negative*/
2107 0, /*nb_positive*/
2108 0, /*nb_absolute*/
2109 0, /*nb_bool*/
2110 0, /*nb_invert*/
2111 0, /*nb_lshift*/
2112 0, /*nb_rshift*/
2113 (binaryfunc)set_and, /*nb_and*/
2114 (binaryfunc)set_xor, /*nb_xor*/
2115 (binaryfunc)set_or, /*nb_or*/
2116 0, /*nb_int*/
2117 0, /*nb_reserved*/
2118 0, /*nb_float*/
2119 0, /*nb_inplace_add*/
2120 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2121 0, /*nb_inplace_multiply*/
2122 0, /*nb_inplace_remainder*/
2123 0, /*nb_inplace_power*/
2124 0, /*nb_inplace_lshift*/
2125 0, /*nb_inplace_rshift*/
2126 (binaryfunc)set_iand, /*nb_inplace_and*/
2127 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2128 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002129};
2130
2131PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002132"set() -> new empty set object\n\
2133set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002134\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002135Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002136
2137PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2139 "set", /* tp_name */
2140 sizeof(PySetObject), /* tp_basicsize */
2141 0, /* tp_itemsize */
2142 /* methods */
2143 (destructor)set_dealloc, /* tp_dealloc */
2144 0, /* tp_print */
2145 0, /* tp_getattr */
2146 0, /* tp_setattr */
2147 0, /* tp_reserved */
2148 (reprfunc)set_repr, /* tp_repr */
2149 &set_as_number, /* tp_as_number */
2150 &set_as_sequence, /* tp_as_sequence */
2151 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002152 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 0, /* tp_call */
2154 0, /* tp_str */
2155 PyObject_GenericGetAttr, /* tp_getattro */
2156 0, /* tp_setattro */
2157 0, /* tp_as_buffer */
2158 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2159 Py_TPFLAGS_BASETYPE, /* tp_flags */
2160 set_doc, /* tp_doc */
2161 (traverseproc)set_traverse, /* tp_traverse */
2162 (inquiry)set_clear_internal, /* tp_clear */
2163 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002164 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2165 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 0, /* tp_iternext */
2167 set_methods, /* tp_methods */
2168 0, /* tp_members */
2169 0, /* tp_getset */
2170 0, /* tp_base */
2171 0, /* tp_dict */
2172 0, /* tp_descr_get */
2173 0, /* tp_descr_set */
2174 0, /* tp_dictoffset */
2175 (initproc)set_init, /* tp_init */
2176 PyType_GenericAlloc, /* tp_alloc */
2177 set_new, /* tp_new */
2178 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002179};
2180
2181/* frozenset object ********************************************************/
2182
2183
2184static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2186 contains_doc},
2187 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2188 copy_doc},
2189 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2190 difference_doc},
Raymond Hettinger5d2385f2015-07-08 11:52:27 -07002191 {"intersection", (PyCFunction)set_intersection_multi, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 intersection_doc},
2193 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2194 isdisjoint_doc},
2195 {"issubset", (PyCFunction)set_issubset, METH_O,
2196 issubset_doc},
2197 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2198 issuperset_doc},
2199 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2200 reduce_doc},
2201 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2202 sizeof_doc},
2203 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2204 symmetric_difference_doc},
2205 {"union", (PyCFunction)set_union, METH_VARARGS,
2206 union_doc},
2207 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002208};
2209
2210static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 0, /*nb_add*/
2212 (binaryfunc)set_sub, /*nb_subtract*/
2213 0, /*nb_multiply*/
2214 0, /*nb_remainder*/
2215 0, /*nb_divmod*/
2216 0, /*nb_power*/
2217 0, /*nb_negative*/
2218 0, /*nb_positive*/
2219 0, /*nb_absolute*/
2220 0, /*nb_bool*/
2221 0, /*nb_invert*/
2222 0, /*nb_lshift*/
2223 0, /*nb_rshift*/
2224 (binaryfunc)set_and, /*nb_and*/
2225 (binaryfunc)set_xor, /*nb_xor*/
2226 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002227};
2228
2229PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002230"frozenset() -> empty frozenset object\n\
2231frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002232\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002233Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002234
2235PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2237 "frozenset", /* tp_name */
2238 sizeof(PySetObject), /* tp_basicsize */
2239 0, /* tp_itemsize */
2240 /* methods */
2241 (destructor)set_dealloc, /* tp_dealloc */
2242 0, /* tp_print */
2243 0, /* tp_getattr */
2244 0, /* tp_setattr */
2245 0, /* tp_reserved */
2246 (reprfunc)set_repr, /* tp_repr */
2247 &frozenset_as_number, /* tp_as_number */
2248 &set_as_sequence, /* tp_as_sequence */
2249 0, /* tp_as_mapping */
2250 frozenset_hash, /* tp_hash */
2251 0, /* tp_call */
2252 0, /* tp_str */
2253 PyObject_GenericGetAttr, /* tp_getattro */
2254 0, /* tp_setattro */
2255 0, /* tp_as_buffer */
2256 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2257 Py_TPFLAGS_BASETYPE, /* tp_flags */
2258 frozenset_doc, /* tp_doc */
2259 (traverseproc)set_traverse, /* tp_traverse */
2260 (inquiry)set_clear_internal, /* tp_clear */
2261 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger5d2385f2015-07-08 11:52:27 -07002262 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 (getiterfunc)set_iter, /* tp_iter */
2264 0, /* tp_iternext */
2265 frozenset_methods, /* tp_methods */
2266 0, /* tp_members */
2267 0, /* tp_getset */
2268 0, /* tp_base */
2269 0, /* tp_dict */
2270 0, /* tp_descr_get */
2271 0, /* tp_descr_set */
2272 0, /* tp_dictoffset */
2273 0, /* tp_init */
2274 PyType_GenericAlloc, /* tp_alloc */
2275 frozenset_new, /* tp_new */
2276 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002277};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002278
2279
2280/***** C API functions *************************************************/
2281
2282PyObject *
2283PySet_New(PyObject *iterable)
2284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002286}
2287
2288PyObject *
2289PyFrozenSet_New(PyObject *iterable)
2290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002292}
2293
Neal Norwitz8c49c822006-03-04 18:41:19 +00002294Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002295PySet_Size(PyObject *anyset)
2296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 if (!PyAnySet_Check(anyset)) {
2298 PyErr_BadInternalCall();
2299 return -1;
2300 }
2301 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002302}
2303
2304int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002305PySet_Clear(PyObject *set)
2306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (!PySet_Check(set)) {
2308 PyErr_BadInternalCall();
2309 return -1;
2310 }
2311 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002312}
2313
2314int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002315PySet_Contains(PyObject *anyset, PyObject *key)
2316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!PyAnySet_Check(anyset)) {
2318 PyErr_BadInternalCall();
2319 return -1;
2320 }
2321 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002322}
2323
2324int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002325PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 if (!PySet_Check(set)) {
2328 PyErr_BadInternalCall();
2329 return -1;
2330 }
2331 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002332}
2333
2334int
Christian Heimesfd66e512008-01-29 12:18:50 +00002335PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 if (!PySet_Check(anyset) &&
2338 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2339 PyErr_BadInternalCall();
2340 return -1;
2341 }
2342 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002343}
2344
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002345int
Raymond Hettinger3625af52016-03-27 01:15:07 -07002346PySet_ClearFreeList(void)
2347{
2348 return 0;
2349}
2350
2351void
2352PySet_Fini(void)
2353{
2354 Py_CLEAR(emptyfrozenset);
2355}
2356
2357int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002358_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 if (!PyAnySet_Check(set)) {
2363 PyErr_BadInternalCall();
2364 return -1;
2365 }
2366 if (set_next((PySetObject *)set, pos, &entry) == 0)
2367 return 0;
2368 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002369 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002371}
2372
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002373PyObject *
2374PySet_Pop(PyObject *set)
2375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 if (!PySet_Check(set)) {
2377 PyErr_BadInternalCall();
2378 return NULL;
2379 }
2380 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002381}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002382
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002383int
2384_PySet_Update(PyObject *set, PyObject *iterable)
2385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 if (!PySet_Check(set)) {
2387 PyErr_BadInternalCall();
2388 return -1;
2389 }
2390 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002391}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002392
Raymond Hettingere259f132013-12-15 11:56:14 -08002393/* Exported for the gdb plugin's benefit. */
2394PyObject *_PySet_Dummy = dummy;
2395
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002396#ifdef Py_DEBUG
2397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002399 Returns True and original set is restored. */
2400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401#define assertRaises(call_return_value, exception) \
2402 do { \
2403 assert(call_return_value); \
2404 assert(PyErr_ExceptionMatches(exception)); \
2405 PyErr_Clear(); \
2406 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002407
2408static PyObject *
2409test_c_api(PySetObject *so)
2410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 Py_ssize_t count;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002412 const char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002414 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002416 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* Verify preconditions */
2420 assert(PyAnySet_Check(ob));
2421 assert(PyAnySet_CheckExact(ob));
2422 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* so.clear(); so |= set("abc"); */
2425 str = PyUnicode_FromString("abc");
2426 if (str == NULL)
2427 return NULL;
2428 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002429 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 Py_DECREF(str);
2431 return NULL;
2432 }
2433 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* Exercise type/size checks */
2436 assert(PySet_Size(ob) == 3);
2437 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* Raise TypeError for non-iterable constructor arguments */
2440 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2441 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 /* Raise TypeError for unhashable key */
2444 dup = PySet_New(ob);
2445 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2446 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2447 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 /* Exercise successful pop, contains, add, and discard */
2450 elem = PySet_Pop(ob);
2451 assert(PySet_Contains(ob, elem) == 0);
2452 assert(PySet_GET_SIZE(ob) == 2);
2453 assert(PySet_Add(ob, elem) == 0);
2454 assert(PySet_Contains(ob, elem) == 1);
2455 assert(PySet_GET_SIZE(ob) == 3);
2456 assert(PySet_Discard(ob, elem) == 1);
2457 assert(PySet_GET_SIZE(ob) == 2);
2458 assert(PySet_Discard(ob, elem) == 0);
2459 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 /* Exercise clear */
2462 dup2 = PySet_New(dup);
2463 assert(PySet_Clear(dup2) == 0);
2464 assert(PySet_Size(dup2) == 0);
2465 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 /* Raise SystemError on clear or update of frozen set */
2468 f = PyFrozenSet_New(dup);
2469 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2470 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2471 assert(PySet_Add(f, elem) == 0);
2472 Py_INCREF(f);
2473 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2474 Py_DECREF(f);
2475 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 /* Exercise direct iteration */
2478 i = 0, count = 0;
2479 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +02002480 s = PyUnicode_AsUTF8(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2482 count++;
2483 }
2484 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* Exercise updates */
2487 dup2 = PySet_New(NULL);
2488 assert(_PySet_Update(dup2, dup) == 0);
2489 assert(PySet_Size(dup2) == 3);
2490 assert(_PySet_Update(dup2, dup) == 0);
2491 assert(PySet_Size(dup2) == 3);
2492 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 /* Raise SystemError when self argument is not a set or frozenset. */
2495 t = PyTuple_New(0);
2496 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2497 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2498 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 /* Raise SystemError when self argument is not a set. */
2501 f = PyFrozenSet_New(dup);
2502 assert(PySet_Size(f) == 3);
2503 assert(PyFrozenSet_CheckExact(f));
2504 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2505 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2506 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 /* Raise KeyError when popping from an empty set */
2509 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2510 Py_DECREF(ob);
2511 assert(PySet_GET_SIZE(ob) == 0);
2512 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 /* Restore the set from the copy using the PyNumber API */
2515 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2516 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 /* Verify constructors accept NULL arguments */
2519 f = PySet_New(NULL);
2520 assert(f != NULL);
2521 assert(PySet_GET_SIZE(f) == 0);
2522 Py_DECREF(f);
2523 f = PyFrozenSet_New(NULL);
2524 assert(f != NULL);
2525 assert(PyFrozenSet_CheckExact(f));
2526 assert(PySet_GET_SIZE(f) == 0);
2527 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 Py_DECREF(elem);
2530 Py_DECREF(dup);
2531 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002532}
2533
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002534#undef assertRaises
2535
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002536#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002537
2538/***** Dummy Struct *************************************************/
2539
2540static PyObject *
2541dummy_repr(PyObject *op)
2542{
2543 return PyUnicode_FromString("<dummy key>");
2544}
2545
2546static void
2547dummy_dealloc(PyObject* ignore)
2548{
2549 Py_FatalError("deallocating <dummy key>");
2550}
2551
2552static PyTypeObject _PySetDummy_Type = {
2553 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2554 "<dummy key> type",
2555 0,
2556 0,
2557 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2558 0, /*tp_print*/
2559 0, /*tp_getattr*/
2560 0, /*tp_setattr*/
2561 0, /*tp_reserved*/
2562 dummy_repr, /*tp_repr*/
2563 0, /*tp_as_number*/
2564 0, /*tp_as_sequence*/
2565 0, /*tp_as_mapping*/
2566 0, /*tp_hash */
2567 0, /*tp_call */
2568 0, /*tp_str */
2569 0, /*tp_getattro */
2570 0, /*tp_setattro */
2571 0, /*tp_as_buffer */
2572 Py_TPFLAGS_DEFAULT, /*tp_flags */
2573};
2574
2575static PyObject _dummy_struct = {
2576 _PyObject_EXTRA_INIT
2577 2, &_PySetDummy_Type
2578};
2579