blob: cd5d2dd83c03098a9b68ed097732a8f345d55a86 [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.
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +030026
Raymond Hettinger64263df2017-09-04 18:54:16 -070027 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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060035#include "internal/pystate.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000036#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000037
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050039static PyObject _dummy_struct;
40
41#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000042
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000043
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070044/* ======================================================================== */
45/* ======= Begin logic for probing the hash table ========================= */
46
Raymond Hettinger710a67e2013-09-21 20:17:31 -070047/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070048#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070049#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070050#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070051
52/* This must be >= 1 */
53#define PERTURB_SHIFT 5
54
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000055static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057{
Raymond Hettinger70559b52015-07-23 07:42:23 -040058 setentry *table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020059 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070060 size_t perturb;
Raymond Hettingerafe89092013-08-28 20:59:31 -070061 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080062 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070063 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070064 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000065
Raymond Hettinger70559b52015-07-23 07:42:23 -040066 entry = &so->table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070067 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070069
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070070 perturb = hash;
71
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070072 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080073 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070074 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080075 /* startkey cannot be a dummy because the dummy hash field is -1 */
76 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080077 if (startkey == key)
78 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080079 if (PyUnicode_CheckExact(startkey)
80 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -070081 && _PyUnicode_EQ(startkey, key))
Raymond Hettinger93035c42015-01-25 16:12:49 -080082 return entry;
Raymond Hettinger70559b52015-07-23 07:42:23 -040083 table = so->table;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 Py_INCREF(startkey);
85 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
86 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010087 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010089 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010091 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070092 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060093 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070094 }
Raymond Hettingerafe89092013-08-28 20:59:31 -070095
Raymond Hettingerc658d852015-02-02 08:35:00 -080096 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080097 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080098 entry++;
Raymond Hettinger8651a502015-05-27 10:37:20 -070099 if (entry->hash == 0 && entry->key == NULL)
100 return entry;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800101 if (entry->hash == hash) {
102 PyObject *startkey = entry->key;
103 assert(startkey != dummy);
104 if (startkey == key)
105 return entry;
106 if (PyUnicode_CheckExact(startkey)
107 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -0700108 && _PyUnicode_EQ(startkey, key))
Raymond Hettingerc658d852015-02-02 08:35:00 -0800109 return entry;
Raymond Hettinger70559b52015-07-23 07:42:23 -0400110 table = so->table;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800111 Py_INCREF(startkey);
112 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
113 Py_DECREF(startkey);
114 if (cmp < 0)
115 return NULL;
116 if (table != so->table || entry->key != startkey)
117 return set_lookkey(so, key, hash);
118 if (cmp > 0)
119 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600120 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800121 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700122 }
123 }
124
125 perturb >>= PERTURB_SHIFT;
126 i = (i * 5 + 1 + perturb) & mask;
127
Raymond Hettinger70559b52015-07-23 07:42:23 -0400128 entry = &so->table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700129 if (entry->key == NULL)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700130 return entry;
131 }
132}
133
Raymond Hettinger15f08692015-07-03 17:21:17 -0700134static int set_table_resize(PySetObject *, Py_ssize_t);
135
Raymond Hettinger8651a502015-05-27 10:37:20 -0700136static int
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400137set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700138{
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400139 setentry *table;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700140 setentry *freeslot;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700141 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700142 size_t perturb;
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400143 size_t mask;
144 size_t i; /* Unsigned for defined overflow behavior */
Raymond Hettinger8651a502015-05-27 10:37:20 -0700145 size_t j;
146 int cmp;
147
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400148 /* Pre-increment is necessary to prevent arbitrary code in the rich
149 comparison from deallocating the key just before the insertion. */
150 Py_INCREF(key);
151
152 restart:
153
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400154 mask = so->mask;
155 i = (size_t)hash & mask;
156
Raymond Hettinger70559b52015-07-23 07:42:23 -0400157 entry = &so->table[i];
Raymond Hettinger8651a502015-05-27 10:37:20 -0700158 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700159 goto found_unused;
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700160
161 freeslot = NULL;
162 perturb = hash;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700163
164 while (1) {
165 if (entry->hash == hash) {
166 PyObject *startkey = entry->key;
167 /* startkey cannot be a dummy because the dummy hash field is -1 */
168 assert(startkey != dummy);
169 if (startkey == key)
170 goto found_active;
171 if (PyUnicode_CheckExact(startkey)
172 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -0700173 && _PyUnicode_EQ(startkey, key))
Raymond Hettinger8651a502015-05-27 10:37:20 -0700174 goto found_active;
Raymond Hettinger70559b52015-07-23 07:42:23 -0400175 table = so->table;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700176 Py_INCREF(startkey);
177 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
178 Py_DECREF(startkey);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700179 if (cmp > 0) /* likely */
180 goto found_active;
Raymond Hettingerdaffc912015-07-31 07:58:56 -0700181 if (cmp < 0)
182 goto comparison_error;
183 /* Continuing the search from the current entry only makes
184 sense if the table and entry are unchanged; otherwise,
185 we have to restart from the beginning */
186 if (table != so->table || entry->key != startkey)
187 goto restart;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700188 mask = so->mask; /* help avoid a register spill */
189 }
Raymond Hettinger70559b52015-07-23 07:42:23 -0400190 else if (entry->hash == -1 && freeslot == NULL)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700191 freeslot = entry;
192
193 if (i + LINEAR_PROBES <= mask) {
194 for (j = 0 ; j < LINEAR_PROBES ; j++) {
195 entry++;
196 if (entry->hash == 0 && entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700197 goto found_unused_or_dummy;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700198 if (entry->hash == hash) {
199 PyObject *startkey = entry->key;
200 assert(startkey != dummy);
201 if (startkey == key)
202 goto found_active;
203 if (PyUnicode_CheckExact(startkey)
204 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -0700205 && _PyUnicode_EQ(startkey, key))
Raymond Hettinger8651a502015-05-27 10:37:20 -0700206 goto found_active;
Raymond Hettinger70559b52015-07-23 07:42:23 -0400207 table = so->table;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700208 Py_INCREF(startkey);
209 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
210 Py_DECREF(startkey);
Raymond Hettingerdaffc912015-07-31 07:58:56 -0700211 if (cmp > 0)
212 goto found_active;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700213 if (cmp < 0)
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400214 goto comparison_error;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700215 if (table != so->table || entry->key != startkey)
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400216 goto restart;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700217 mask = so->mask;
218 }
Raymond Hettinger91672612015-06-26 02:50:21 -0700219 else if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800220 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700221 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700223
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700224 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800225 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700226
Raymond Hettinger70559b52015-07-23 07:42:23 -0400227 entry = &so->table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700228 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700229 goto found_unused_or_dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700231
Raymond Hettinger15f08692015-07-03 17:21:17 -0700232 found_unused_or_dummy:
233 if (freeslot == NULL)
234 goto found_unused;
Raymond Hettinger15f08692015-07-03 17:21:17 -0700235 so->used++;
236 freeslot->key = key;
237 freeslot->hash = hash;
238 return 0;
239
240 found_unused:
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700241 so->fill++;
242 so->used++;
243 entry->key = key;
244 entry->hash = hash;
Raymond Hettinger5cd87a82017-02-04 02:43:42 -0800245 if ((size_t)so->fill*5 < mask*3)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700246 return 0;
INADA Naokie82cf862017-04-01 17:20:25 +0900247 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700248
249 found_active:
Raymond Hettinger482c05c2015-07-20 01:23:32 -0400250 Py_DECREF(key);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700251 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000252
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400253 comparison_error:
Raymond Hettinger061091a2015-07-15 23:54:02 -0700254 Py_DECREF(key);
255 return -1;
256}
257
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000258/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000259Internal routine used by set_table_resize() to insert an item which is
260known to be absent from the set. This routine also assumes that
261the set contains no deleted entries. Besides the performance benefit,
Raymond Hettinger6019c8c2015-11-17 08:28:07 -0800262there is also safety benefit since using set_add_entry() risks making
263a callback in the middle of a set_table_resize(), see issue 1456209.
264The caller is responsible for updating the key's reference count and
265the setobject's fill and used fields.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000266*/
267static void
Raymond Hettinger5088f602015-12-13 18:45:01 -0800268set_insert_clean(setentry *table, size_t mask, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000269{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200270 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700271 size_t perturb = hash;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800272 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700273 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000274
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700275 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800276 entry = &table[i];
277 if (entry->key == NULL)
278 goto found_null;
279 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800280 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800281 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800282 if (entry->key == NULL)
283 goto found_null;
284 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700285 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700286 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800287 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700289 found_null:
Raymond Hettinger5088f602015-12-13 18:45:01 -0800290 entry->key = key;
Raymond Hettinger86d322f2015-12-13 19:27:17 -0800291 entry->hash = hash;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000292}
293
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700294/* ======== End logic for probing the hash table ========================== */
295/* ======================================================================== */
296
Thomas Wouters89f507f2006-12-13 04:49:30 +0000297/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000298Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000299keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000300actually be smaller than the old one.
301*/
302static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000303set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 Py_ssize_t newsize;
306 setentry *oldtable, *newtable, *entry;
Raymond Hettinger5088f602015-12-13 18:45:01 -0800307 Py_ssize_t oldmask = so->mask;
308 size_t newmask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 int is_oldtable_malloced;
310 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100315 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 for (newsize = PySet_MINSIZE;
317 newsize <= minused && newsize > 0;
318 newsize <<= 1)
319 ;
320 if (newsize <= 0) {
321 PyErr_NoMemory();
322 return -1;
323 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 /* Get space for a new table. */
326 oldtable = so->table;
327 assert(oldtable != NULL);
328 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 if (newsize == PySet_MINSIZE) {
331 /* A large table is shrinking, or we can't get any smaller. */
332 newtable = so->smalltable;
333 if (newtable == oldtable) {
334 if (so->fill == so->used) {
335 /* No dummies, so no point doing anything. */
336 return 0;
337 }
338 /* We're not going to resize it, but rebuild the
339 table anyway to purge old dummy entries.
340 Subtle: This is *necessary* if fill==size,
341 as set_lookkey needs at least one virgin slot to
342 terminate failing searches. If fill < size, it's
343 merely desirable, as dummies slow searches. */
344 assert(so->fill > so->used);
345 memcpy(small_copy, oldtable, sizeof(small_copy));
346 oldtable = small_copy;
347 }
348 }
349 else {
350 newtable = PyMem_NEW(setentry, newsize);
351 if (newtable == NULL) {
352 PyErr_NoMemory();
353 return -1;
354 }
355 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 /* Make the set empty, using the new table. */
358 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800360 so->mask = newsize - 1;
361 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 /* Copy the data over; this is refcount-neutral for active entries;
364 dummy entries aren't copied over, of course */
Raymond Hettinger5088f602015-12-13 18:45:01 -0800365 newmask = (size_t)so->mask;
Raymond Hettingere1af6962017-02-02 08:24:48 -0800366 if (so->fill == so->used) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800367 for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800368 if (entry->key != NULL) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800369 set_insert_clean(newtable, newmask, entry->key, entry->hash);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800370 }
371 }
372 } else {
Raymond Hettingere1af6962017-02-02 08:24:48 -0800373 so->fill = so->used;
Raymond Hettinger5088f602015-12-13 18:45:01 -0800374 for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800375 if (entry->key != NULL && entry->key != dummy) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800376 set_insert_clean(newtable, newmask, entry->key, entry->hash);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 }
379 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 if (is_oldtable_malloced)
382 PyMem_DEL(oldtable);
383 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000384}
385
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386static int
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700387set_contains_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000388{
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700389 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000390
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700391 entry = set_lookkey(so, key, hash);
392 if (entry != NULL)
393 return entry->key != NULL;
394 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000395}
396
397#define DISCARD_NOTFOUND 0
398#define DISCARD_FOUND 1
399
400static int
Raymond Hettinger73799b12015-07-05 16:06:10 -0700401set_discard_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800402{
403 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000405
Raymond Hettingerdc28d5a2015-07-05 10:03:20 -0700406 entry = set_lookkey(so, key, hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (entry == NULL)
408 return -1;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700409 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return DISCARD_NOTFOUND;
411 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800413 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 so->used--;
415 Py_DECREF(old_key);
416 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000417}
418
419static int
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700420set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200422 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000423
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700424 if (!PyUnicode_CheckExact(key) ||
425 (hash = ((PyASCIIObject *) key)->hash) == -1) {
426 hash = PyObject_Hash(key);
427 if (hash == -1)
428 return -1;
429 }
430 return set_add_entry(so, key, hash);
431}
432
433static int
434set_contains_key(PySetObject *so, PyObject *key)
435{
436 Py_hash_t hash;
437
438 if (!PyUnicode_CheckExact(key) ||
439 (hash = ((PyASCIIObject *) key)->hash) == -1) {
440 hash = PyObject_Hash(key);
441 if (hash == -1)
442 return -1;
443 }
444 return set_contains_entry(so, key, hash);
445}
446
447static int
448set_discard_key(PySetObject *so, PyObject *key)
449{
450 Py_hash_t hash;
Christian Heimes0ded5b52007-12-10 15:50:56 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200453 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 hash = PyObject_Hash(key);
455 if (hash == -1)
456 return -1;
457 }
Raymond Hettinger73799b12015-07-05 16:06:10 -0700458 return set_discard_entry(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000459}
460
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700461static void
462set_empty_to_minsize(PySetObject *so)
463{
464 memset(so->smalltable, 0, sizeof(so->smalltable));
465 so->fill = 0;
466 so->used = 0;
467 so->mask = PySet_MINSIZE - 1;
468 so->table = so->smalltable;
469 so->hash = -1;
470}
471
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000472static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473set_clear_internal(PySetObject *so)
474{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800475 setentry *entry;
476 setentry *table = so->table;
477 Py_ssize_t fill = so->fill;
478 Py_ssize_t used = so->used;
479 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000481
Raymond Hettinger583cd032013-09-07 22:06:35 -0700482 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 /* This is delicate. During the process of clearing the set,
486 * decrefs can cause the set to mutate. To avoid fatal confusion
487 * (voice of experience), we have to make the set empty before
488 * clearing the slots, and never refer to anything via so->ref while
489 * clearing.
490 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700492 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 else if (fill > 0) {
495 /* It's a small table with something that needs to be cleared.
496 * Afraid the only safe way is to copy the set entries into
497 * another small table first.
498 */
499 memcpy(small_copy, table, sizeof(small_copy));
500 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700501 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 }
503 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* Now we can finally clear things. If C had refcounts, we could
506 * assert that the refcount on table is 1 now, i.e. that this function
507 * has unique access to it, so decref side-effects can't alter it.
508 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800509 for (entry = table; used > 0; entry++) {
510 if (entry->key && entry->key != dummy) {
511 used--;
512 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 if (table_is_malloced)
517 PyMem_DEL(table);
518 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519}
520
521/*
522 * Iterate over a set table. Use like so:
523 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000524 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000525 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000526 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000527 * while (set_next(yourset, &pos, &entry)) {
528 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529 * }
530 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000531 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000533 */
534static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000535set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 Py_ssize_t i;
538 Py_ssize_t mask;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700539 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 assert (PyAnySet_Check(so));
542 i = *pos_ptr;
543 assert(i >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 mask = so->mask;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700545 entry = &so->table[i];
546 while (i <= mask && (entry->key == NULL || entry->key == dummy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 i++;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700548 entry++;
549 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 *pos_ptr = i+1;
551 if (i > mask)
552 return 0;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700553 assert(entry != NULL);
554 *entry_ptr = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000556}
557
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000558static void
559set_dealloc(PySetObject *so)
560{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200561 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800562 Py_ssize_t used = so->used;
563
INADA Naokia6296d32017-08-24 14:55:17 +0900564 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 PyObject_GC_UnTrack(so);
566 Py_TRASHCAN_SAFE_BEGIN(so)
567 if (so->weakreflist != NULL)
568 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000569
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800570 for (entry = so->table; used > 0; entry++) {
571 if (entry->key && entry->key != dummy) {
572 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700573 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 }
575 }
576 if (so->table != so->smalltable)
577 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700578 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000580}
581
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000582static PyObject *
583set_repr(PySetObject *so)
584{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200585 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 if (status != 0) {
589 if (status < 0)
590 return NULL;
591 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
592 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 /* shortcut for the empty set */
595 if (!so->used) {
596 Py_ReprLeave((PyObject*)so);
597 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
598 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 keys = PySequence_List((PyObject *)so);
601 if (keys == NULL)
602 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000603
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200604 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 listrepr = PyObject_Repr(keys);
606 Py_DECREF(keys);
607 if (listrepr == NULL)
608 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200609 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200611 if (tmp == NULL)
612 goto done;
613 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200614
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200615 if (Py_TYPE(so) != &PySet_Type)
616 result = PyUnicode_FromFormat("%s({%U})",
617 Py_TYPE(so)->tp_name,
618 listrepr);
619 else
620 result = PyUnicode_FromFormat("{%U}", listrepr);
621 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000622done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 Py_ReprLeave((PyObject*)so);
624 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000625}
626
Martin v. Löwis18e16552006-02-15 17:27:45 +0000627static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000628set_len(PyObject *so)
629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000631}
632
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000633static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000634set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000637 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200638 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700639 setentry *so_entry;
640 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 assert (PyAnySet_Check(so));
643 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 other = (PySetObject*)otherset;
646 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700647 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 return 0;
649 /* Do one big resize at the start, rather than
650 * incrementally resizing as we insert new keys. Expect
651 * that there will be no (or few) overlapping keys.
652 */
Raymond Hettinger5cd87a82017-02-04 02:43:42 -0800653 if ((so->fill + other->used)*5 >= so->mask*3) {
INADA Naokie82cf862017-04-01 17:20:25 +0900654 if (set_table_resize(so, (so->used + other->used)*2) != 0)
655 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700657 so_entry = so->table;
658 other_entry = other->table;
659
660 /* If our table is empty, and both tables have the same size, and
661 there are no dummies to eliminate, then just copy the pointers. */
662 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
663 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
664 key = other_entry->key;
665 if (key != NULL) {
666 assert(so_entry->key == NULL);
667 Py_INCREF(key);
668 so_entry->key = key;
669 so_entry->hash = other_entry->hash;
670 }
671 }
672 so->fill = other->fill;
673 so->used = other->used;
674 return 0;
675 }
676
677 /* If our table is empty, we can use set_insert_clean() */
678 if (so->fill == 0) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800679 setentry *newtable = so->table;
680 size_t newmask = (size_t)so->mask;
Raymond Hettinger6019c8c2015-11-17 08:28:07 -0800681 so->fill = other->used;
682 so->used = other->used;
Raymond Hettingere4495872015-12-15 00:42:30 -0800683 for (i = other->mask + 1; i > 0 ; i--, other_entry++) {
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700684 key = other_entry->key;
685 if (key != NULL && key != dummy) {
686 Py_INCREF(key);
Raymond Hettinger5088f602015-12-13 18:45:01 -0800687 set_insert_clean(newtable, newmask, key, other_entry->hash);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700688 }
689 }
690 return 0;
691 }
692
693 /* We can't assure there are no duplicates, so do normal insertions */
Raymond Hettingera3626bc2015-07-15 23:50:14 -0700694 for (i = 0; i <= other->mask; i++) {
695 other_entry = &other->table[i];
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700696 key = other_entry->key;
697 if (key != NULL && key != dummy) {
Raymond Hettinger73799b12015-07-05 16:06:10 -0700698 if (set_add_entry(so, key, other_entry->hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 }
701 }
702 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000703}
704
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000705static PyObject *
706set_pop(PySetObject *so)
707{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800708 /* Make sure the search finger is in bounds */
709 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200710 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 assert (PyAnySet_Check(so));
714 if (so->used == 0) {
715 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
716 return NULL;
717 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000718
Raymond Hettinger1202a472015-01-18 13:12:42 -0800719 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
720 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800721 if (i > so->mask)
722 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 }
724 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800726 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800728 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000730}
731
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000732PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
733Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000734
735static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000736set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 Py_ssize_t pos = 0;
739 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 while (set_next(so, &pos, &entry))
742 Py_VISIT(entry->key);
743 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000744}
745
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700746/* Work to increase the bit dispersion for closely spaced hash values.
747 This is important because some use cases have many combinations of a
748 small number of elements with nearby hashes so that many distinct
749 combinations collapse to only a handful of distinct hash values. */
750
751static Py_uhash_t
752_shuffle_bits(Py_uhash_t h)
753{
754 return ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
755}
756
757/* Most of the constants in this hash algorithm are randomly chosen
758 large primes with "interesting bit patterns" and that passed tests
759 for good collision statistics on a variety of problematic datasets
760 including powersets and graph structures (such as David Eppstein's
761 graph recipes in Lib/test/test_set.py) */
762
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000763static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000764frozenset_hash(PyObject *self)
765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 PySetObject *so = (PySetObject *)self;
Raymond Hettinger41481952015-08-07 00:43:39 -0700767 Py_uhash_t hash = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000769
Raymond Hettingerb501a272015-08-06 22:15:22 -0700770 if (so->hash != -1)
771 return so->hash;
772
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700773 /* Xor-in shuffled bits from every entry's hash field because xor is
774 commutative and a frozenset hash should be independent of order.
775
776 For speed, include null entries and dummy entries and then
777 subtract out their effect afterwards so that the final hash
778 depends only on active entries. This allows the code to be
779 vectorized by the compiler and it saves the unpredictable
780 branches that would arise when trying to exclude null and dummy
781 entries on every iteration. */
782
783 for (entry = so->table; entry <= &so->table[so->mask]; entry++)
784 hash ^= _shuffle_bits(entry->hash);
785
Raymond Hettingera286a512015-08-01 11:07:11 -0700786 /* Remove the effect of an odd number of NULL entries */
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700787 if ((so->mask + 1 - so->fill) & 1)
788 hash ^= _shuffle_bits(0);
789
790 /* Remove the effect of an odd number of dummy entries */
791 if ((so->fill - so->used) & 1)
792 hash ^= _shuffle_bits(-1);
793
Raymond Hettinger41481952015-08-07 00:43:39 -0700794 /* Factor in the number of active entries */
795 hash ^= ((Py_uhash_t)PySet_GET_SIZE(self) + 1) * 1927868237UL;
796
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700797 /* Disperse patterns arising in nested frozensets */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800798 hash = hash * 69069U + 907133923UL;
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700799
Raymond Hettinger36c05002015-08-01 10:57:42 -0700800 /* -1 is reserved as an error code */
Victor Stinner12174a52014-08-15 23:17:38 +0200801 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800802 hash = 590923713UL;
Raymond Hettinger36c05002015-08-01 10:57:42 -0700803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 so->hash = hash;
805 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000806}
807
Raymond Hettingera9d99362005-08-05 00:01:15 +0000808/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 PyObject_HEAD
812 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
813 Py_ssize_t si_used;
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700814 Py_ssize_t si_pos;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000816} setiterobject;
817
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818static void
819setiter_dealloc(setiterobject *si)
820{
INADA Naokia6296d32017-08-24 14:55:17 +0900821 /* bpo-31095: UnTrack is needed before calling any callbacks */
822 _PyObject_GC_UNTRACK(si);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 Py_XDECREF(si->si_set);
824 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000825}
826
827static int
828setiter_traverse(setiterobject *si, visitproc visit, void *arg)
829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 Py_VISIT(si->si_set);
831 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832}
833
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000834static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835setiter_len(setiterobject *si)
836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 Py_ssize_t len = 0;
838 if (si->si_set != NULL && si->si_used == si->si_set->used)
839 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000840 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000841}
842
Armin Rigof5b3e362006-02-11 21:32:43 +0000843PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000844
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000845static PyObject *setiter_iternext(setiterobject *si);
846
847static PyObject *
848setiter_reduce(setiterobject *si)
849{
850 PyObject *list;
851 setiterobject tmp;
852
853 list = PyList_New(0);
854 if (!list)
855 return NULL;
856
Ezio Melotti0e1af282012-09-28 16:43:40 +0300857 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000858 tmp = *si;
859 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300860
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000861 /* iterate the temporary into a list */
862 for(;;) {
863 PyObject *element = setiter_iternext(&tmp);
864 if (element) {
865 if (PyList_Append(list, element)) {
866 Py_DECREF(element);
867 Py_DECREF(list);
868 Py_XDECREF(tmp.si_set);
869 return NULL;
870 }
871 Py_DECREF(element);
872 } else
873 break;
874 }
875 Py_XDECREF(tmp.si_set);
876 /* check for error */
877 if (tmp.si_set != NULL) {
878 /* we have an error */
879 Py_DECREF(list);
880 return NULL;
881 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200882 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000883}
884
885PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
886
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000887static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000889 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891};
892
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000893static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000894{
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700895 PyObject *key;
896 Py_ssize_t i, mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200897 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 if (so == NULL)
901 return NULL;
902 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 if (si->si_used != so->used) {
905 PyErr_SetString(PyExc_RuntimeError,
906 "Set changed size during iteration");
907 si->si_used = -1; /* Make this state sticky */
908 return NULL;
909 }
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700910
911 i = si->si_pos;
912 assert(i>=0);
913 entry = so->table;
914 mask = so->mask;
915 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
916 i++;
917 si->si_pos = i+1;
918 if (i > mask)
919 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 si->len--;
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700921 key = entry[i].key;
922 Py_INCREF(key);
923 return key;
924
925fail:
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700926 si->si_set = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +0300927 Py_DECREF(so);
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700928 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000929}
930
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000931PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 PyVarObject_HEAD_INIT(&PyType_Type, 0)
933 "set_iterator", /* tp_name */
934 sizeof(setiterobject), /* tp_basicsize */
935 0, /* tp_itemsize */
936 /* methods */
937 (destructor)setiter_dealloc, /* tp_dealloc */
938 0, /* tp_print */
939 0, /* tp_getattr */
940 0, /* tp_setattr */
941 0, /* tp_reserved */
942 0, /* tp_repr */
943 0, /* tp_as_number */
944 0, /* tp_as_sequence */
945 0, /* tp_as_mapping */
946 0, /* tp_hash */
947 0, /* tp_call */
948 0, /* tp_str */
949 PyObject_GenericGetAttr, /* tp_getattro */
950 0, /* tp_setattro */
951 0, /* tp_as_buffer */
Raymond Hettinger5d2385f2015-07-08 11:52:27 -0700952 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 0, /* tp_doc */
954 (traverseproc)setiter_traverse, /* tp_traverse */
955 0, /* tp_clear */
956 0, /* tp_richcompare */
957 0, /* tp_weaklistoffset */
958 PyObject_SelfIter, /* tp_iter */
959 (iternextfunc)setiter_iternext, /* tp_iternext */
960 setiter_methods, /* tp_methods */
961 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000962};
963
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000964static PyObject *
965set_iter(PySetObject *so)
966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
968 if (si == NULL)
969 return NULL;
970 Py_INCREF(so);
971 si->si_set = so;
972 si->si_used = so->used;
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700973 si->si_pos = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 si->len = so->used;
975 _PyObject_GC_TRACK(si);
976 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000977}
978
Raymond Hettingerd7946662005-08-01 21:39:29 +0000979static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000980set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 if (PyAnySet_Check(other))
985 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 if (PyDict_CheckExact(other)) {
988 PyObject *value;
989 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000990 Py_hash_t hash;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200991 Py_ssize_t dictsize = PyDict_GET_SIZE(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 /* Do one big resize at the start, rather than
994 * incrementally resizing as we insert new keys. Expect
995 * that there will be no (or few) overlapping keys.
996 */
Raymond Hettingerc2480dc2015-07-04 08:46:31 -0700997 if (dictsize < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 return -1;
Raymond Hettinger5cd87a82017-02-04 02:43:42 -0800999 if ((so->fill + dictsize)*5 >= so->mask*3) {
INADA Naokie82cf862017-04-01 17:20:25 +09001000 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 return -1;
1002 }
1003 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001004 if (set_add_entry(so, key, hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 return -1;
1006 }
1007 return 0;
1008 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 it = PyObject_GetIter(other);
1011 if (it == NULL)
1012 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001015 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 Py_DECREF(it);
1017 Py_DECREF(key);
1018 return -1;
1019 }
1020 Py_DECREF(key);
1021 }
1022 Py_DECREF(it);
1023 if (PyErr_Occurred())
1024 return -1;
1025 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001026}
1027
1028static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001029set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1034 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001035 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 return NULL;
1037 }
1038 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001039}
1040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001042"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001043
Raymond Hettinger426d9952014-05-18 21:40:20 +01001044/* XXX Todo:
1045 If aligned memory allocations become available, make the
1046 set object 64 byte aligned so that most of the fields
1047 can be retrieved or updated in a single cache line.
1048*/
1049
Raymond Hettingera38123e2003-11-24 22:18:49 +00001050static PyObject *
1051make_new_set(PyTypeObject *type, PyObject *iterable)
1052{
Raymond Hettinger8421d712016-04-29 01:37:05 -07001053 PySetObject *so;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001054
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001055 so = (PySetObject *)type->tp_alloc(type, 0);
1056 if (so == NULL)
1057 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001058
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001059 so->fill = 0;
1060 so->used = 0;
1061 so->mask = PySet_MINSIZE - 1;
1062 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001063 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -08001064 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001068 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 Py_DECREF(so);
1070 return NULL;
1071 }
1072 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001075}
1076
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001077static PyObject *
1078make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1081 if (PyType_IsSubtype(type, &PySet_Type))
1082 type = &PySet_Type;
1083 else
1084 type = &PyFrozenSet_Type;
1085 }
1086 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001087}
1088
Raymond Hettingerd7946662005-08-01 21:39:29 +00001089/* The empty frozenset is a singleton */
1090static PyObject *emptyfrozenset = NULL;
1091
Raymond Hettingera690a992003-11-16 16:17:49 +00001092static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001093frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001096
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001097 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1101 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 if (type != &PyFrozenSet_Type)
1104 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 if (iterable != NULL) {
1107 /* frozenset(f) is idempotent */
1108 if (PyFrozenSet_CheckExact(iterable)) {
1109 Py_INCREF(iterable);
1110 return iterable;
1111 }
1112 result = make_new_set(type, iterable);
1113 if (result == NULL || PySet_GET_SIZE(result))
1114 return result;
1115 Py_DECREF(result);
1116 }
1117 /* The empty frozenset is a singleton */
1118 if (emptyfrozenset == NULL)
1119 emptyfrozenset = make_new_set(type, NULL);
1120 Py_XINCREF(emptyfrozenset);
1121 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001122}
1123
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001124static PyObject *
1125set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001128}
1129
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001130/* set_swap_bodies() switches the contents of any two sets by moving their
1131 internal data pointers and, if needed, copying the internal smalltables.
1132 Semantically equivalent to:
1133
1134 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1135
1136 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001137 Useful for operations that update in-place (by allowing an intermediate
1138 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001139*/
1140
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001141static void
1142set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 Py_ssize_t t;
1145 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001147 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 t = a->fill; a->fill = b->fill; b->fill = t;
1150 t = a->used; a->used = b->used; b->used = t;
1151 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 u = a->table;
1154 if (a->table == a->smalltable)
1155 u = b->smalltable;
1156 a->table = b->table;
1157 if (b->table == b->smalltable)
1158 a->table = a->smalltable;
1159 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (a->table == a->smalltable || b->table == b->smalltable) {
1162 memcpy(tab, a->smalltable, sizeof(tab));
1163 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1164 memcpy(b->smalltable, tab, sizeof(tab));
1165 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1168 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1169 h = a->hash; a->hash = b->hash; b->hash = h;
1170 } else {
1171 a->hash = -1;
1172 b->hash = -1;
1173 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001174}
1175
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001176static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001177set_copy(PySetObject *so)
1178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001180}
1181
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001182static PyObject *
1183frozenset_copy(PySetObject *so)
1184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 if (PyFrozenSet_CheckExact(so)) {
1186 Py_INCREF(so);
1187 return (PyObject *)so;
1188 }
1189 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001190}
1191
Raymond Hettingera690a992003-11-16 16:17:49 +00001192PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1193
1194static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001195set_clear(PySetObject *so)
1196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 set_clear_internal(so);
1198 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001199}
1200
1201PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1202
1203static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001204set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 PySetObject *result;
1207 PyObject *other;
1208 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 result = (PySetObject *)set_copy(so);
1211 if (result == NULL)
1212 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1215 other = PyTuple_GET_ITEM(args, i);
1216 if ((PyObject *)so == other)
1217 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001218 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 Py_DECREF(result);
1220 return NULL;
1221 }
1222 }
1223 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001224}
1225
1226PyDoc_STRVAR(union_doc,
1227 "Return the union of sets as a new set.\n\
1228\n\
1229(i.e. all elements that are in either set.)");
1230
1231static PyObject *
1232set_or(PySetObject *so, PyObject *other)
1233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001235
Brian Curtindfc80e32011-08-10 20:28:54 -05001236 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1237 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 result = (PySetObject *)set_copy(so);
1240 if (result == NULL)
1241 return NULL;
1242 if ((PyObject *)so == other)
1243 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001244 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 Py_DECREF(result);
1246 return NULL;
1247 }
1248 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001249}
1250
Raymond Hettingera690a992003-11-16 16:17:49 +00001251static PyObject *
1252set_ior(PySetObject *so, PyObject *other)
1253{
Brian Curtindfc80e32011-08-10 20:28:54 -05001254 if (!PyAnySet_Check(other))
1255 Py_RETURN_NOTIMPLEMENTED;
1256
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001257 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return NULL;
1259 Py_INCREF(so);
1260 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001261}
1262
1263static PyObject *
1264set_intersection(PySetObject *so, PyObject *other)
1265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 PySetObject *result;
1267 PyObject *key, *it, *tmp;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001268 Py_hash_t hash;
1269 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 if ((PyObject *)so == other)
1272 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1275 if (result == NULL)
1276 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 if (PyAnySet_Check(other)) {
1279 Py_ssize_t pos = 0;
1280 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1283 tmp = (PyObject *)so;
1284 so = (PySetObject *)other;
1285 other = tmp;
1286 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001289 key = entry->key;
1290 hash = entry->hash;
1291 rv = set_contains_entry(so, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001292 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 Py_DECREF(result);
1294 return NULL;
1295 }
1296 if (rv) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001297 if (set_add_entry(result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 Py_DECREF(result);
1299 return NULL;
1300 }
1301 }
1302 }
1303 return (PyObject *)result;
1304 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 it = PyObject_GetIter(other);
1307 if (it == NULL) {
1308 Py_DECREF(result);
1309 return NULL;
1310 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001313 hash = PyObject_Hash(key);
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001314 if (hash == -1)
1315 goto error;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001316 rv = set_contains_entry(so, key, hash);
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001317 if (rv < 0)
1318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 if (rv) {
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001320 if (set_add_entry(result, key, hash))
1321 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 }
1323 Py_DECREF(key);
1324 }
1325 Py_DECREF(it);
1326 if (PyErr_Occurred()) {
1327 Py_DECREF(result);
1328 return NULL;
1329 }
1330 return (PyObject *)result;
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001331 error:
1332 Py_DECREF(it);
1333 Py_DECREF(result);
1334 Py_DECREF(key);
1335 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001336}
1337
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001338static PyObject *
1339set_intersection_multi(PySetObject *so, PyObject *args)
1340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 Py_ssize_t i;
1342 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (PyTuple_GET_SIZE(args) == 0)
1345 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 Py_INCREF(so);
1348 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1349 PyObject *other = PyTuple_GET_ITEM(args, i);
1350 PyObject *newresult = set_intersection((PySetObject *)result, other);
1351 if (newresult == NULL) {
1352 Py_DECREF(result);
1353 return NULL;
1354 }
1355 Py_DECREF(result);
1356 result = newresult;
1357 }
1358 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001359}
1360
Raymond Hettingera690a992003-11-16 16:17:49 +00001361PyDoc_STRVAR(intersection_doc,
1362"Return the intersection of two sets as a new set.\n\
1363\n\
1364(i.e. all elements that are in both sets.)");
1365
1366static PyObject *
1367set_intersection_update(PySetObject *so, PyObject *other)
1368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 tmp = set_intersection(so, other);
1372 if (tmp == NULL)
1373 return NULL;
1374 set_swap_bodies(so, (PySetObject *)tmp);
1375 Py_DECREF(tmp);
1376 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001377}
1378
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001379static PyObject *
1380set_intersection_update_multi(PySetObject *so, PyObject *args)
1381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 tmp = set_intersection_multi(so, args);
1385 if (tmp == NULL)
1386 return NULL;
1387 set_swap_bodies(so, (PySetObject *)tmp);
1388 Py_DECREF(tmp);
1389 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001390}
1391
Raymond Hettingera690a992003-11-16 16:17:49 +00001392PyDoc_STRVAR(intersection_update_doc,
1393"Update a set with the intersection of itself and another.");
1394
1395static PyObject *
1396set_and(PySetObject *so, PyObject *other)
1397{
Brian Curtindfc80e32011-08-10 20:28:54 -05001398 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1399 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001401}
1402
1403static PyObject *
1404set_iand(PySetObject *so, PyObject *other)
1405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001407
Brian Curtindfc80e32011-08-10 20:28:54 -05001408 if (!PyAnySet_Check(other))
1409 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 result = set_intersection_update(so, other);
1411 if (result == NULL)
1412 return NULL;
1413 Py_DECREF(result);
1414 Py_INCREF(so);
1415 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001416}
1417
Guido van Rossum58da9312007-11-10 23:39:45 +00001418static PyObject *
1419set_isdisjoint(PySetObject *so, PyObject *other)
1420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 PyObject *key, *it, *tmp;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001422 int rv;
Guido van Rossum58da9312007-11-10 23:39:45 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 if ((PyObject *)so == other) {
1425 if (PySet_GET_SIZE(so) == 0)
1426 Py_RETURN_TRUE;
1427 else
1428 Py_RETURN_FALSE;
1429 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 if (PyAnySet_CheckExact(other)) {
1432 Py_ssize_t pos = 0;
1433 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1436 tmp = (PyObject *)so;
1437 so = (PySetObject *)other;
1438 other = tmp;
1439 }
1440 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001441 rv = set_contains_entry(so, entry->key, entry->hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001442 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 return NULL;
1444 if (rv)
1445 Py_RETURN_FALSE;
1446 }
1447 Py_RETURN_TRUE;
1448 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 it = PyObject_GetIter(other);
1451 if (it == NULL)
1452 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 while ((key = PyIter_Next(it)) != NULL) {
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001455 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if (hash == -1) {
1458 Py_DECREF(key);
1459 Py_DECREF(it);
1460 return NULL;
1461 }
Raymond Hettinger73799b12015-07-05 16:06:10 -07001462 rv = set_contains_entry(so, key, hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 Py_DECREF(key);
Raymond Hettingerc2480dc2015-07-04 08:46:31 -07001464 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 Py_DECREF(it);
1466 return NULL;
1467 }
1468 if (rv) {
1469 Py_DECREF(it);
1470 Py_RETURN_FALSE;
1471 }
1472 }
1473 Py_DECREF(it);
1474 if (PyErr_Occurred())
1475 return NULL;
1476 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001477}
1478
1479PyDoc_STRVAR(isdisjoint_doc,
1480"Return True if two sets have a null intersection.");
1481
Neal Norwitz6576bd82005-11-13 18:41:28 +00001482static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001483set_difference_update_internal(PySetObject *so, PyObject *other)
1484{
Raymond Hettinger4103e4d2016-09-11 22:02:28 -07001485 if (PySet_GET_SIZE(so) == 0) {
1486 return 0;
1487 }
1488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 if ((PyObject *)so == other)
1490 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (PyAnySet_Check(other)) {
1493 setentry *entry;
1494 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettinger73799b12015-07-05 16:06:10 -07001497 if (set_discard_entry(so, entry->key, entry->hash) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 return -1;
1499 } else {
1500 PyObject *key, *it;
1501 it = PyObject_GetIter(other);
1502 if (it == NULL)
1503 return -1;
1504
1505 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerb3223262015-07-03 23:37:16 -07001506 if (set_discard_key(so, key) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 Py_DECREF(it);
1508 Py_DECREF(key);
1509 return -1;
1510 }
1511 Py_DECREF(key);
1512 }
1513 Py_DECREF(it);
1514 if (PyErr_Occurred())
1515 return -1;
1516 }
Raymond Hettingere186c762015-07-04 11:28:35 -07001517 /* If more than 1/4th are dummies, then resize them away. */
1518 if ((size_t)(so->fill - so->used) <= (size_t)so->mask / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 return 0;
INADA Naokie82cf862017-04-01 17:20:25 +09001520 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001521}
1522
Raymond Hettingera690a992003-11-16 16:17:49 +00001523static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001524set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1529 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001530 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 return NULL;
1532 }
1533 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001534}
1535
1536PyDoc_STRVAR(difference_update_doc,
1537"Remove all elements of another set from this set.");
1538
1539static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001540set_copy_and_difference(PySetObject *so, PyObject *other)
1541{
1542 PyObject *result;
1543
1544 result = set_copy(so);
1545 if (result == NULL)
1546 return NULL;
Raymond Hettingerb3223262015-07-03 23:37:16 -07001547 if (set_difference_update_internal((PySetObject *) result, other) == 0)
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001548 return result;
1549 Py_DECREF(result);
1550 return NULL;
1551}
1552
1553static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001554set_difference(PySetObject *so, PyObject *other)
1555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 PyObject *result;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001557 PyObject *key;
1558 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 setentry *entry;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03001560 Py_ssize_t pos = 0, other_size;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001561 int rv;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001562
Raymond Hettinger4103e4d2016-09-11 22:02:28 -07001563 if (PySet_GET_SIZE(so) == 0) {
1564 return set_copy(so);
1565 }
1566
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03001567 if (PyAnySet_Check(other)) {
1568 other_size = PySet_GET_SIZE(other);
1569 }
1570 else if (PyDict_CheckExact(other)) {
1571 other_size = PyDict_GET_SIZE(other);
1572 }
1573 else {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001574 return set_copy_and_difference(so, other);
1575 }
1576
1577 /* If len(so) much more than len(other), it's more efficient to simply copy
1578 * so and then iterate other looking for common elements. */
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03001579 if ((PySet_GET_SIZE(so) >> 2) > other_size) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001580 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 result = make_new_set_basetype(Py_TYPE(so), NULL);
1584 if (result == NULL)
1585 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 if (PyDict_CheckExact(other)) {
1588 while (set_next(so, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001589 key = entry->key;
1590 hash = entry->hash;
Raymond Hettinger15f08692015-07-03 17:21:17 -07001591 rv = _PyDict_Contains(other, key, hash);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001592 if (rv < 0) {
1593 Py_DECREF(result);
1594 return NULL;
1595 }
1596 if (!rv) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001597 if (set_add_entry((PySetObject *)result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 Py_DECREF(result);
1599 return NULL;
1600 }
1601 }
1602 }
1603 return result;
1604 }
1605
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001606 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 while (set_next(so, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001608 key = entry->key;
1609 hash = entry->hash;
1610 rv = set_contains_entry((PySetObject *)other, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001611 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 Py_DECREF(result);
1613 return NULL;
1614 }
1615 if (!rv) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001616 if (set_add_entry((PySetObject *)result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 Py_DECREF(result);
1618 return NULL;
1619 }
1620 }
1621 }
1622 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001623}
1624
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001625static PyObject *
1626set_difference_multi(PySetObject *so, PyObject *args)
1627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 Py_ssize_t i;
1629 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (PyTuple_GET_SIZE(args) == 0)
1632 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 other = PyTuple_GET_ITEM(args, 0);
1635 result = set_difference(so, other);
1636 if (result == NULL)
1637 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1640 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001641 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 Py_DECREF(result);
1643 return NULL;
1644 }
1645 }
1646 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001647}
1648
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001649PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001650"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001651\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001652(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001653static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001654set_sub(PySetObject *so, PyObject *other)
1655{
Brian Curtindfc80e32011-08-10 20:28:54 -05001656 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1657 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001659}
1660
1661static PyObject *
1662set_isub(PySetObject *so, PyObject *other)
1663{
Brian Curtindfc80e32011-08-10 20:28:54 -05001664 if (!PyAnySet_Check(other))
1665 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001666 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 return NULL;
1668 Py_INCREF(so);
1669 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001670}
1671
1672static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001673set_symmetric_difference_update(PySetObject *so, PyObject *other)
1674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 PySetObject *otherset;
1676 PyObject *key;
1677 Py_ssize_t pos = 0;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001678 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 setentry *entry;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001680 int rv;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 if ((PyObject *)so == other)
1683 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 if (PyDict_CheckExact(other)) {
1686 PyObject *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001688 Py_INCREF(key);
Raymond Hettinger73799b12015-07-05 16:06:10 -07001689 rv = set_discard_entry(so, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001690 if (rv < 0) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001691 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001694 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001695 if (set_add_entry(so, key, hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001696 Py_DECREF(key);
1697 return NULL;
1698 }
1699 }
Georg Brandl2d444492010-09-03 10:52:55 +00001700 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 }
1702 Py_RETURN_NONE;
1703 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 if (PyAnySet_Check(other)) {
1706 Py_INCREF(other);
1707 otherset = (PySetObject *)other;
1708 } else {
1709 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1710 if (otherset == NULL)
1711 return NULL;
1712 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 while (set_next(otherset, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001715 key = entry->key;
1716 hash = entry->hash;
1717 rv = set_discard_entry(so, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001718 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 Py_DECREF(otherset);
1720 return NULL;
1721 }
1722 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001723 if (set_add_entry(so, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 Py_DECREF(otherset);
1725 return NULL;
1726 }
1727 }
1728 }
1729 Py_DECREF(otherset);
1730 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001731}
1732
1733PyDoc_STRVAR(symmetric_difference_update_doc,
1734"Update a set with the symmetric difference of itself and another.");
1735
1736static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001737set_symmetric_difference(PySetObject *so, PyObject *other)
1738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 PyObject *rv;
1740 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1743 if (otherset == NULL)
1744 return NULL;
1745 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1746 if (rv == NULL)
1747 return NULL;
1748 Py_DECREF(rv);
1749 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001750}
1751
1752PyDoc_STRVAR(symmetric_difference_doc,
1753"Return the symmetric difference of two sets as a new set.\n\
1754\n\
1755(i.e. all elements that are in exactly one of the sets.)");
1756
1757static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001758set_xor(PySetObject *so, PyObject *other)
1759{
Brian Curtindfc80e32011-08-10 20:28:54 -05001760 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1761 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001763}
1764
1765static PyObject *
1766set_ixor(PySetObject *so, PyObject *other)
1767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001769
Brian Curtindfc80e32011-08-10 20:28:54 -05001770 if (!PyAnySet_Check(other))
1771 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 result = set_symmetric_difference_update(so, other);
1773 if (result == NULL)
1774 return NULL;
1775 Py_DECREF(result);
1776 Py_INCREF(so);
1777 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001778}
1779
1780static PyObject *
1781set_issubset(PySetObject *so, PyObject *other)
1782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 setentry *entry;
1784 Py_ssize_t pos = 0;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001785 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (!PyAnySet_Check(other)) {
1788 PyObject *tmp, *result;
1789 tmp = make_new_set(&PySet_Type, other);
1790 if (tmp == NULL)
1791 return NULL;
1792 result = set_issubset(so, tmp);
1793 Py_DECREF(tmp);
1794 return result;
1795 }
1796 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1797 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 while (set_next(so, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001800 rv = set_contains_entry((PySetObject *)other, entry->key, entry->hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001801 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 return NULL;
1803 if (!rv)
1804 Py_RETURN_FALSE;
1805 }
1806 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001807}
1808
1809PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1810
1811static PyObject *
1812set_issuperset(PySetObject *so, PyObject *other)
1813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 if (!PyAnySet_Check(other)) {
1817 tmp = make_new_set(&PySet_Type, other);
1818 if (tmp == NULL)
1819 return NULL;
1820 result = set_issuperset(so, tmp);
1821 Py_DECREF(tmp);
1822 return result;
1823 }
1824 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001825}
1826
1827PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1828
Raymond Hettingera690a992003-11-16 16:17:49 +00001829static PyObject *
1830set_richcompare(PySetObject *v, PyObject *w, int op)
1831{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001832 PyObject *r1;
1833 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001834
Brian Curtindfc80e32011-08-10 20:28:54 -05001835 if(!PyAnySet_Check(w))
1836 Py_RETURN_NOTIMPLEMENTED;
1837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 switch (op) {
1839 case Py_EQ:
1840 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1841 Py_RETURN_FALSE;
1842 if (v->hash != -1 &&
1843 ((PySetObject *)w)->hash != -1 &&
1844 v->hash != ((PySetObject *)w)->hash)
1845 Py_RETURN_FALSE;
1846 return set_issubset(v, w);
1847 case Py_NE:
1848 r1 = set_richcompare(v, w, Py_EQ);
1849 if (r1 == NULL)
1850 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001851 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001853 if (r2 < 0)
1854 return NULL;
1855 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 case Py_LE:
1857 return set_issubset(v, w);
1858 case Py_GE:
1859 return set_issuperset(v, w);
1860 case Py_LT:
1861 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1862 Py_RETURN_FALSE;
1863 return set_issubset(v, w);
1864 case Py_GT:
1865 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1866 Py_RETURN_FALSE;
1867 return set_issuperset(v, w);
1868 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001869 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001870}
1871
Raymond Hettingera690a992003-11-16 16:17:49 +00001872static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001873set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001874{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001875 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 return NULL;
1877 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001878}
1879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001881"Add an element to a set.\n\
1882\n\
1883This has no effect if the element is already present.");
1884
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001885static int
1886set_contains(PySetObject *so, PyObject *key)
1887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 PyObject *tmpkey;
1889 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 rv = set_contains_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001892 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1894 return -1;
1895 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001896 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 if (tmpkey == NULL)
1898 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001899 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 Py_DECREF(tmpkey);
1901 }
1902 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001903}
1904
1905static PyObject *
1906set_direct_contains(PySetObject *so, PyObject *key)
1907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 result = set_contains(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001911 if (result < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 return NULL;
1913 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001914}
1915
1916PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1917
Raymond Hettingera690a992003-11-16 16:17:49 +00001918static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001919set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 PyObject *tmpkey;
1922 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 rv = set_discard_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001925 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1927 return NULL;
1928 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001929 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 if (tmpkey == NULL)
1931 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 Py_DECREF(tmpkey);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001934 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 return NULL;
1936 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001939 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 return NULL;
1941 }
1942 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001943}
1944
1945PyDoc_STRVAR(remove_doc,
1946"Remove an element from a set; it must be a member.\n\
1947\n\
1948If the element is not a member, raise a KeyError.");
1949
1950static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001951set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001952{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001953 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 rv = set_discard_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001957 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1959 return NULL;
1960 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001961 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 if (tmpkey == NULL)
1963 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001964 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 Py_DECREF(tmpkey);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001966 if (rv < 0)
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001967 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 }
1969 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001970}
1971
1972PyDoc_STRVAR(discard_doc,
1973"Remove an element from a set if it is a member.\n\
1974\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001976
1977static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001978set_reduce(PySetObject *so)
1979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001981 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 keys = PySequence_List((PyObject *)so);
1984 if (keys == NULL)
1985 goto done;
1986 args = PyTuple_Pack(1, keys);
1987 if (args == NULL)
1988 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001989 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 if (dict == NULL) {
1991 PyErr_Clear();
1992 dict = Py_None;
1993 Py_INCREF(dict);
1994 }
1995 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001996done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 Py_XDECREF(args);
1998 Py_XDECREF(keys);
1999 Py_XDECREF(dict);
2000 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002001}
2002
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002003static PyObject *
2004set_sizeof(PySetObject *so)
2005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002007
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002008 res = _PyObject_SIZE(Py_TYPE(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 if (so->table != so->smalltable)
2010 res = res + (so->mask + 1) * sizeof(setentry);
2011 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002012}
2013
2014PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002015static int
2016set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002019
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002020 if (!_PyArg_NoKeywords("set", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 return -1;
2022 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2023 return -1;
Raymond Hettingerb72e21b2016-03-25 02:29:59 -07002024 if (self->fill)
2025 set_clear_internal(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 self->hash = -1;
2027 if (iterable == NULL)
2028 return 0;
2029 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002030}
2031
Raymond Hettingera690a992003-11-16 16:17:49 +00002032static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 set_len, /* sq_length */
2034 0, /* sq_concat */
2035 0, /* sq_repeat */
2036 0, /* sq_item */
2037 0, /* sq_slice */
2038 0, /* sq_ass_item */
2039 0, /* sq_ass_slice */
2040 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002041};
2042
2043/* set object ********************************************************/
2044
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002045#ifdef Py_DEBUG
2046static PyObject *test_c_api(PySetObject *so);
2047
2048PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2049All is well if assertions don't fail.");
2050#endif
2051
Raymond Hettingera690a992003-11-16 16:17:49 +00002052static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 {"add", (PyCFunction)set_add, METH_O,
2054 add_doc},
2055 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2056 clear_doc},
2057 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2058 contains_doc},
2059 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2060 copy_doc},
2061 {"discard", (PyCFunction)set_discard, METH_O,
2062 discard_doc},
2063 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2064 difference_doc},
2065 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2066 difference_update_doc},
2067 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2068 intersection_doc},
2069 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2070 intersection_update_doc},
2071 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2072 isdisjoint_doc},
2073 {"issubset", (PyCFunction)set_issubset, METH_O,
2074 issubset_doc},
2075 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2076 issuperset_doc},
2077 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2078 pop_doc},
2079 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2080 reduce_doc},
2081 {"remove", (PyCFunction)set_remove, METH_O,
2082 remove_doc},
2083 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2084 sizeof_doc},
2085 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2086 symmetric_difference_doc},
2087 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2088 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002089#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2091 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002092#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 {"union", (PyCFunction)set_union, METH_VARARGS,
2094 union_doc},
2095 {"update", (PyCFunction)set_update, METH_VARARGS,
2096 update_doc},
2097 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002098};
2099
2100static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 0, /*nb_add*/
2102 (binaryfunc)set_sub, /*nb_subtract*/
2103 0, /*nb_multiply*/
2104 0, /*nb_remainder*/
2105 0, /*nb_divmod*/
2106 0, /*nb_power*/
2107 0, /*nb_negative*/
2108 0, /*nb_positive*/
2109 0, /*nb_absolute*/
2110 0, /*nb_bool*/
2111 0, /*nb_invert*/
2112 0, /*nb_lshift*/
2113 0, /*nb_rshift*/
2114 (binaryfunc)set_and, /*nb_and*/
2115 (binaryfunc)set_xor, /*nb_xor*/
2116 (binaryfunc)set_or, /*nb_or*/
2117 0, /*nb_int*/
2118 0, /*nb_reserved*/
2119 0, /*nb_float*/
2120 0, /*nb_inplace_add*/
2121 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2122 0, /*nb_inplace_multiply*/
2123 0, /*nb_inplace_remainder*/
2124 0, /*nb_inplace_power*/
2125 0, /*nb_inplace_lshift*/
2126 0, /*nb_inplace_rshift*/
2127 (binaryfunc)set_iand, /*nb_inplace_and*/
2128 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2129 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002130};
2131
2132PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002133"set() -> new empty set object\n\
2134set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002135\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002136Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002137
2138PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2140 "set", /* tp_name */
2141 sizeof(PySetObject), /* tp_basicsize */
2142 0, /* tp_itemsize */
2143 /* methods */
2144 (destructor)set_dealloc, /* tp_dealloc */
2145 0, /* tp_print */
2146 0, /* tp_getattr */
2147 0, /* tp_setattr */
2148 0, /* tp_reserved */
2149 (reprfunc)set_repr, /* tp_repr */
2150 &set_as_number, /* tp_as_number */
2151 &set_as_sequence, /* tp_as_sequence */
2152 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002153 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 0, /* tp_call */
2155 0, /* tp_str */
2156 PyObject_GenericGetAttr, /* tp_getattro */
2157 0, /* tp_setattro */
2158 0, /* tp_as_buffer */
2159 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2160 Py_TPFLAGS_BASETYPE, /* tp_flags */
2161 set_doc, /* tp_doc */
2162 (traverseproc)set_traverse, /* tp_traverse */
2163 (inquiry)set_clear_internal, /* tp_clear */
2164 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002165 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2166 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 0, /* tp_iternext */
2168 set_methods, /* tp_methods */
2169 0, /* tp_members */
2170 0, /* tp_getset */
2171 0, /* tp_base */
2172 0, /* tp_dict */
2173 0, /* tp_descr_get */
2174 0, /* tp_descr_set */
2175 0, /* tp_dictoffset */
2176 (initproc)set_init, /* tp_init */
2177 PyType_GenericAlloc, /* tp_alloc */
2178 set_new, /* tp_new */
2179 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002180};
2181
2182/* frozenset object ********************************************************/
2183
2184
2185static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2187 contains_doc},
2188 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2189 copy_doc},
2190 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2191 difference_doc},
Raymond Hettinger5d2385f2015-07-08 11:52:27 -07002192 {"intersection", (PyCFunction)set_intersection_multi, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 intersection_doc},
2194 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2195 isdisjoint_doc},
2196 {"issubset", (PyCFunction)set_issubset, METH_O,
2197 issubset_doc},
2198 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2199 issuperset_doc},
2200 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2201 reduce_doc},
2202 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2203 sizeof_doc},
2204 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2205 symmetric_difference_doc},
2206 {"union", (PyCFunction)set_union, METH_VARARGS,
2207 union_doc},
2208 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002209};
2210
2211static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 0, /*nb_add*/
2213 (binaryfunc)set_sub, /*nb_subtract*/
2214 0, /*nb_multiply*/
2215 0, /*nb_remainder*/
2216 0, /*nb_divmod*/
2217 0, /*nb_power*/
2218 0, /*nb_negative*/
2219 0, /*nb_positive*/
2220 0, /*nb_absolute*/
2221 0, /*nb_bool*/
2222 0, /*nb_invert*/
2223 0, /*nb_lshift*/
2224 0, /*nb_rshift*/
2225 (binaryfunc)set_and, /*nb_and*/
2226 (binaryfunc)set_xor, /*nb_xor*/
2227 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002228};
2229
2230PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002231"frozenset() -> empty frozenset object\n\
2232frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002233\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002234Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002235
2236PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2238 "frozenset", /* tp_name */
2239 sizeof(PySetObject), /* tp_basicsize */
2240 0, /* tp_itemsize */
2241 /* methods */
2242 (destructor)set_dealloc, /* tp_dealloc */
2243 0, /* tp_print */
2244 0, /* tp_getattr */
2245 0, /* tp_setattr */
2246 0, /* tp_reserved */
2247 (reprfunc)set_repr, /* tp_repr */
2248 &frozenset_as_number, /* tp_as_number */
2249 &set_as_sequence, /* tp_as_sequence */
2250 0, /* tp_as_mapping */
2251 frozenset_hash, /* tp_hash */
2252 0, /* tp_call */
2253 0, /* tp_str */
2254 PyObject_GenericGetAttr, /* tp_getattro */
2255 0, /* tp_setattro */
2256 0, /* tp_as_buffer */
2257 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2258 Py_TPFLAGS_BASETYPE, /* tp_flags */
2259 frozenset_doc, /* tp_doc */
2260 (traverseproc)set_traverse, /* tp_traverse */
2261 (inquiry)set_clear_internal, /* tp_clear */
2262 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger5d2385f2015-07-08 11:52:27 -07002263 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 (getiterfunc)set_iter, /* tp_iter */
2265 0, /* tp_iternext */
2266 frozenset_methods, /* tp_methods */
2267 0, /* tp_members */
2268 0, /* tp_getset */
2269 0, /* tp_base */
2270 0, /* tp_dict */
2271 0, /* tp_descr_get */
2272 0, /* tp_descr_set */
2273 0, /* tp_dictoffset */
2274 0, /* tp_init */
2275 PyType_GenericAlloc, /* tp_alloc */
2276 frozenset_new, /* tp_new */
2277 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002278};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002279
2280
2281/***** C API functions *************************************************/
2282
2283PyObject *
2284PySet_New(PyObject *iterable)
2285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002287}
2288
2289PyObject *
2290PyFrozenSet_New(PyObject *iterable)
2291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002293}
2294
Neal Norwitz8c49c822006-03-04 18:41:19 +00002295Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002296PySet_Size(PyObject *anyset)
2297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 if (!PyAnySet_Check(anyset)) {
2299 PyErr_BadInternalCall();
2300 return -1;
2301 }
2302 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002303}
2304
2305int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002306PySet_Clear(PyObject *set)
2307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 if (!PySet_Check(set)) {
2309 PyErr_BadInternalCall();
2310 return -1;
2311 }
2312 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002313}
2314
2315int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002316PySet_Contains(PyObject *anyset, PyObject *key)
2317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 if (!PyAnySet_Check(anyset)) {
2319 PyErr_BadInternalCall();
2320 return -1;
2321 }
2322 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002323}
2324
2325int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002326PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 if (!PySet_Check(set)) {
2329 PyErr_BadInternalCall();
2330 return -1;
2331 }
2332 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002333}
2334
2335int
Christian Heimesfd66e512008-01-29 12:18:50 +00002336PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 if (!PySet_Check(anyset) &&
2339 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2340 PyErr_BadInternalCall();
2341 return -1;
2342 }
2343 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002344}
2345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002346int
Raymond Hettinger3625af52016-03-27 01:15:07 -07002347PySet_ClearFreeList(void)
2348{
2349 return 0;
2350}
2351
2352void
2353PySet_Fini(void)
2354{
2355 Py_CLEAR(emptyfrozenset);
2356}
2357
2358int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002359_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 if (!PyAnySet_Check(set)) {
2364 PyErr_BadInternalCall();
2365 return -1;
2366 }
2367 if (set_next((PySetObject *)set, pos, &entry) == 0)
2368 return 0;
2369 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002370 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002372}
2373
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002374PyObject *
2375PySet_Pop(PyObject *set)
2376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 if (!PySet_Check(set)) {
2378 PyErr_BadInternalCall();
2379 return NULL;
2380 }
2381 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002382}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002383
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002384int
2385_PySet_Update(PyObject *set, PyObject *iterable)
2386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 if (!PySet_Check(set)) {
2388 PyErr_BadInternalCall();
2389 return -1;
2390 }
2391 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002392}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002393
Raymond Hettingere259f132013-12-15 11:56:14 -08002394/* Exported for the gdb plugin's benefit. */
2395PyObject *_PySet_Dummy = dummy;
2396
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002397#ifdef Py_DEBUG
2398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002400 Returns True and original set is restored. */
2401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402#define assertRaises(call_return_value, exception) \
2403 do { \
2404 assert(call_return_value); \
2405 assert(PyErr_ExceptionMatches(exception)); \
2406 PyErr_Clear(); \
2407 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002408
2409static PyObject *
2410test_c_api(PySetObject *so)
2411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 Py_ssize_t count;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002413 const char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002415 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002417 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 /* Verify preconditions */
2421 assert(PyAnySet_Check(ob));
2422 assert(PyAnySet_CheckExact(ob));
2423 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 /* so.clear(); so |= set("abc"); */
2426 str = PyUnicode_FromString("abc");
2427 if (str == NULL)
2428 return NULL;
2429 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002430 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 Py_DECREF(str);
2432 return NULL;
2433 }
2434 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 /* Exercise type/size checks */
2437 assert(PySet_Size(ob) == 3);
2438 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 /* Raise TypeError for non-iterable constructor arguments */
2441 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2442 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 /* Raise TypeError for unhashable key */
2445 dup = PySet_New(ob);
2446 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2447 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2448 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 /* Exercise successful pop, contains, add, and discard */
2451 elem = PySet_Pop(ob);
2452 assert(PySet_Contains(ob, elem) == 0);
2453 assert(PySet_GET_SIZE(ob) == 2);
2454 assert(PySet_Add(ob, elem) == 0);
2455 assert(PySet_Contains(ob, elem) == 1);
2456 assert(PySet_GET_SIZE(ob) == 3);
2457 assert(PySet_Discard(ob, elem) == 1);
2458 assert(PySet_GET_SIZE(ob) == 2);
2459 assert(PySet_Discard(ob, elem) == 0);
2460 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 /* Exercise clear */
2463 dup2 = PySet_New(dup);
2464 assert(PySet_Clear(dup2) == 0);
2465 assert(PySet_Size(dup2) == 0);
2466 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 /* Raise SystemError on clear or update of frozen set */
2469 f = PyFrozenSet_New(dup);
2470 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2471 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2472 assert(PySet_Add(f, elem) == 0);
2473 Py_INCREF(f);
2474 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2475 Py_DECREF(f);
2476 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 /* Exercise direct iteration */
2479 i = 0, count = 0;
2480 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +02002481 s = PyUnicode_AsUTF8(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2483 count++;
2484 }
2485 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 /* Exercise updates */
2488 dup2 = PySet_New(NULL);
2489 assert(_PySet_Update(dup2, dup) == 0);
2490 assert(PySet_Size(dup2) == 3);
2491 assert(_PySet_Update(dup2, dup) == 0);
2492 assert(PySet_Size(dup2) == 3);
2493 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 /* Raise SystemError when self argument is not a set or frozenset. */
2496 t = PyTuple_New(0);
2497 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2498 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2499 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 /* Raise SystemError when self argument is not a set. */
2502 f = PyFrozenSet_New(dup);
2503 assert(PySet_Size(f) == 3);
2504 assert(PyFrozenSet_CheckExact(f));
2505 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2506 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2507 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 /* Raise KeyError when popping from an empty set */
2510 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2511 Py_DECREF(ob);
2512 assert(PySet_GET_SIZE(ob) == 0);
2513 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 /* Restore the set from the copy using the PyNumber API */
2516 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2517 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 /* Verify constructors accept NULL arguments */
2520 f = PySet_New(NULL);
2521 assert(f != NULL);
2522 assert(PySet_GET_SIZE(f) == 0);
2523 Py_DECREF(f);
2524 f = PyFrozenSet_New(NULL);
2525 assert(f != NULL);
2526 assert(PyFrozenSet_CheckExact(f));
2527 assert(PySet_GET_SIZE(f) == 0);
2528 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 Py_DECREF(elem);
2531 Py_DECREF(dup);
2532 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002533}
2534
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002535#undef assertRaises
2536
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002537#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002538
2539/***** Dummy Struct *************************************************/
2540
2541static PyObject *
2542dummy_repr(PyObject *op)
2543{
2544 return PyUnicode_FromString("<dummy key>");
2545}
2546
2547static void
2548dummy_dealloc(PyObject* ignore)
2549{
2550 Py_FatalError("deallocating <dummy key>");
2551}
2552
2553static PyTypeObject _PySetDummy_Type = {
2554 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2555 "<dummy key> type",
2556 0,
2557 0,
2558 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2559 0, /*tp_print*/
2560 0, /*tp_getattr*/
2561 0, /*tp_setattr*/
2562 0, /*tp_reserved*/
2563 dummy_repr, /*tp_repr*/
2564 0, /*tp_as_number*/
2565 0, /*tp_as_sequence*/
2566 0, /*tp_as_mapping*/
2567 0, /*tp_hash */
2568 0, /*tp_call */
2569 0, /*tp_str */
2570 0, /*tp_getattro */
2571 0, /*tp_setattro */
2572 0, /*tp_as_buffer */
2573 Py_TPFLAGS_DEFAULT, /*tp_flags */
2574};
2575
2576static PyObject _dummy_struct = {
2577 _PyObject_EXTRA_INIT
2578 2, &_PySetDummy_Type
2579};
2580