blob: 176570e46b1f0b6d9bc15d8e4115745483f9a46b [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07007 The basic lookup function used by all operations.
8 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
9
10 The initial probe index is computed as hash mod the table size.
11 Subsequent probe indices are computed as explained in Objects/dictobject.c.
12
13 To improve cache locality, each probe inspects a series of consecutive
14 nearby entries before moving on to probes elsewhere in memory. This leaves
15 us with a hybrid of linear probing and open addressing. The linear probing
16 reduces the cost of hash collisions because consecutive memory accesses
17 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
18 we then use open addressing with the upper bits from the hash value. This
19 helps break-up long chains of collisions.
20
21 All arithmetic on hash should ignore overflow.
22
Raymond Hettinger4d45c102015-01-25 16:27:40 -080023 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070024 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000025*/
26
Raymond Hettingera690a992003-11-16 16:17:49 +000027#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000028#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000029
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000030/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050031static PyObject _dummy_struct;
32
33#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070036/* ======================================================================== */
37/* ======= Begin logic for probing the hash table ========================= */
38
Raymond Hettinger710a67e2013-09-21 20:17:31 -070039/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070040#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070041#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070042#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070043
44/* This must be >= 1 */
45#define PERTURB_SHIFT 5
46
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020048set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000049{
Raymond Hettinger70559b52015-07-23 07:42:23 -040050 setentry *table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020051 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070052 size_t perturb;
Raymond Hettingerafe89092013-08-28 20:59:31 -070053 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080054 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070055 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070056 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057
Raymond Hettinger70559b52015-07-23 07:42:23 -040058 entry = &so->table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070059 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070061
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070062 perturb = hash;
63
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070064 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080065 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070066 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080067 /* startkey cannot be a dummy because the dummy hash field is -1 */
68 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080069 if (startkey == key)
70 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080071 if (PyUnicode_CheckExact(startkey)
72 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -070073 && _PyUnicode_EQ(startkey, key))
Raymond Hettinger93035c42015-01-25 16:12:49 -080074 return entry;
Raymond Hettinger70559b52015-07-23 07:42:23 -040075 table = so->table;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 Py_INCREF(startkey);
77 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
78 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010079 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010081 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010083 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070084 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060085 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070086 }
Raymond Hettingerafe89092013-08-28 20:59:31 -070087
Raymond Hettingerc658d852015-02-02 08:35:00 -080088 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080089 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080090 entry++;
Raymond Hettinger8651a502015-05-27 10:37:20 -070091 if (entry->hash == 0 && entry->key == NULL)
92 return entry;
Raymond Hettingerc658d852015-02-02 08:35:00 -080093 if (entry->hash == hash) {
94 PyObject *startkey = entry->key;
95 assert(startkey != dummy);
96 if (startkey == key)
97 return entry;
98 if (PyUnicode_CheckExact(startkey)
99 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -0700100 && _PyUnicode_EQ(startkey, key))
Raymond Hettingerc658d852015-02-02 08:35:00 -0800101 return entry;
Raymond Hettinger70559b52015-07-23 07:42:23 -0400102 table = so->table;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800103 Py_INCREF(startkey);
104 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
105 Py_DECREF(startkey);
106 if (cmp < 0)
107 return NULL;
108 if (table != so->table || entry->key != startkey)
109 return set_lookkey(so, key, hash);
110 if (cmp > 0)
111 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600112 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800113 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700114 }
115 }
116
117 perturb >>= PERTURB_SHIFT;
118 i = (i * 5 + 1 + perturb) & mask;
119
Raymond Hettinger70559b52015-07-23 07:42:23 -0400120 entry = &so->table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700121 if (entry->key == NULL)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700122 return entry;
123 }
124}
125
Raymond Hettinger15f08692015-07-03 17:21:17 -0700126static int set_table_resize(PySetObject *, Py_ssize_t);
127
Raymond Hettinger8651a502015-05-27 10:37:20 -0700128static int
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400129set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700130{
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400131 setentry *table;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700132 setentry *freeslot;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700133 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700134 size_t perturb;
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400135 size_t mask;
136 size_t i; /* Unsigned for defined overflow behavior */
Raymond Hettinger8651a502015-05-27 10:37:20 -0700137 size_t j;
138 int cmp;
139
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400140 /* Pre-increment is necessary to prevent arbitrary code in the rich
141 comparison from deallocating the key just before the insertion. */
142 Py_INCREF(key);
143
144 restart:
145
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400146 mask = so->mask;
147 i = (size_t)hash & mask;
148
Raymond Hettinger70559b52015-07-23 07:42:23 -0400149 entry = &so->table[i];
Raymond Hettinger8651a502015-05-27 10:37:20 -0700150 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700151 goto found_unused;
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700152
153 freeslot = NULL;
154 perturb = hash;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700155
156 while (1) {
157 if (entry->hash == hash) {
158 PyObject *startkey = entry->key;
159 /* startkey cannot be a dummy because the dummy hash field is -1 */
160 assert(startkey != dummy);
161 if (startkey == key)
162 goto found_active;
163 if (PyUnicode_CheckExact(startkey)
164 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -0700165 && _PyUnicode_EQ(startkey, key))
Raymond Hettinger8651a502015-05-27 10:37:20 -0700166 goto found_active;
Raymond Hettinger70559b52015-07-23 07:42:23 -0400167 table = so->table;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700168 Py_INCREF(startkey);
169 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
170 Py_DECREF(startkey);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700171 if (cmp > 0) /* likely */
172 goto found_active;
Raymond Hettingerdaffc912015-07-31 07:58:56 -0700173 if (cmp < 0)
174 goto comparison_error;
175 /* Continuing the search from the current entry only makes
176 sense if the table and entry are unchanged; otherwise,
177 we have to restart from the beginning */
178 if (table != so->table || entry->key != startkey)
179 goto restart;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700180 mask = so->mask; /* help avoid a register spill */
181 }
Raymond Hettinger70559b52015-07-23 07:42:23 -0400182 else if (entry->hash == -1 && freeslot == NULL)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700183 freeslot = entry;
184
185 if (i + LINEAR_PROBES <= mask) {
186 for (j = 0 ; j < LINEAR_PROBES ; j++) {
187 entry++;
188 if (entry->hash == 0 && entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700189 goto found_unused_or_dummy;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700190 if (entry->hash == hash) {
191 PyObject *startkey = entry->key;
192 assert(startkey != dummy);
193 if (startkey == key)
194 goto found_active;
195 if (PyUnicode_CheckExact(startkey)
196 && PyUnicode_CheckExact(key)
Raymond Hettingerac2ef652015-07-04 16:04:44 -0700197 && _PyUnicode_EQ(startkey, key))
Raymond Hettinger8651a502015-05-27 10:37:20 -0700198 goto found_active;
Raymond Hettinger70559b52015-07-23 07:42:23 -0400199 table = so->table;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700200 Py_INCREF(startkey);
201 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
202 Py_DECREF(startkey);
Raymond Hettingerdaffc912015-07-31 07:58:56 -0700203 if (cmp > 0)
204 goto found_active;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700205 if (cmp < 0)
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400206 goto comparison_error;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700207 if (table != so->table || entry->key != startkey)
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400208 goto restart;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700209 mask = so->mask;
210 }
Raymond Hettinger91672612015-06-26 02:50:21 -0700211 else if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800212 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700213 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700215
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700216 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800217 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700218
Raymond Hettinger70559b52015-07-23 07:42:23 -0400219 entry = &so->table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700220 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700221 goto found_unused_or_dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700223
Raymond Hettinger15f08692015-07-03 17:21:17 -0700224 found_unused_or_dummy:
225 if (freeslot == NULL)
226 goto found_unused;
Raymond Hettinger15f08692015-07-03 17:21:17 -0700227 so->used++;
228 freeslot->key = key;
229 freeslot->hash = hash;
230 return 0;
231
232 found_unused:
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700233 so->fill++;
234 so->used++;
235 entry->key = key;
236 entry->hash = hash;
Raymond Hettinger15f08692015-07-03 17:21:17 -0700237 if ((size_t)so->fill*3 < mask*2)
238 return 0;
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400239 return set_table_resize(so, so->used);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700240
241 found_active:
Raymond Hettinger482c05c2015-07-20 01:23:32 -0400242 Py_DECREF(key);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700243 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000244
Raymond Hettingerff9e18a2015-07-20 07:34:05 -0400245 comparison_error:
Raymond Hettinger061091a2015-07-15 23:54:02 -0700246 Py_DECREF(key);
247 return -1;
248}
249
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000250/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000251Internal routine used by set_table_resize() to insert an item which is
252known to be absent from the set. This routine also assumes that
253the set contains no deleted entries. Besides the performance benefit,
Raymond Hettinger6019c8c2015-11-17 08:28:07 -0800254there is also safety benefit since using set_add_entry() risks making
255a callback in the middle of a set_table_resize(), see issue 1456209.
256The caller is responsible for updating the key's reference count and
257the setobject's fill and used fields.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000258*/
259static void
Raymond Hettinger5088f602015-12-13 18:45:01 -0800260set_insert_clean(setentry *table, size_t mask, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000261{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200262 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700263 size_t perturb = hash;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800264 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700265 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000266
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700267 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800268 entry = &table[i];
269 if (entry->key == NULL)
270 goto found_null;
271 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800272 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800273 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800274 if (entry->key == NULL)
275 goto found_null;
276 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700277 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700278 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800279 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700281 found_null:
Raymond Hettinger5088f602015-12-13 18:45:01 -0800282 entry->key = key;
Raymond Hettinger86d322f2015-12-13 19:27:17 -0800283 entry->hash = hash;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000284}
285
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700286/* ======== End logic for probing the hash table ========================== */
287/* ======================================================================== */
288
Thomas Wouters89f507f2006-12-13 04:49:30 +0000289/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000290Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000291keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000292actually be smaller than the old one.
293*/
294static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000295set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_ssize_t newsize;
298 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800299 Py_ssize_t oldfill = so->fill;
300 Py_ssize_t oldused = so->used;
Raymond Hettinger5088f602015-12-13 18:45:01 -0800301 Py_ssize_t oldmask = so->mask;
302 size_t newmask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 int is_oldtable_malloced;
304 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 assert(minused >= 0);
Raymond Hettinger48973002015-07-03 20:00:03 -0700307 minused = (minused > 50000) ? minused * 2 : minused * 4;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100310 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 for (newsize = PySet_MINSIZE;
312 newsize <= minused && newsize > 0;
313 newsize <<= 1)
314 ;
315 if (newsize <= 0) {
316 PyErr_NoMemory();
317 return -1;
318 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* Get space for a new table. */
321 oldtable = so->table;
322 assert(oldtable != NULL);
323 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (newsize == PySet_MINSIZE) {
326 /* A large table is shrinking, or we can't get any smaller. */
327 newtable = so->smalltable;
328 if (newtable == oldtable) {
329 if (so->fill == so->used) {
330 /* No dummies, so no point doing anything. */
331 return 0;
332 }
333 /* We're not going to resize it, but rebuild the
334 table anyway to purge old dummy entries.
335 Subtle: This is *necessary* if fill==size,
336 as set_lookkey needs at least one virgin slot to
337 terminate failing searches. If fill < size, it's
338 merely desirable, as dummies slow searches. */
339 assert(so->fill > so->used);
340 memcpy(small_copy, oldtable, sizeof(small_copy));
341 oldtable = small_copy;
342 }
343 }
344 else {
345 newtable = PyMem_NEW(setentry, newsize);
346 if (newtable == NULL) {
347 PyErr_NoMemory();
348 return -1;
349 }
350 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 /* Make the set empty, using the new table. */
353 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettinger6019c8c2015-11-17 08:28:07 -0800355 so->fill = oldused;
356 so->used = oldused;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800357 so->mask = newsize - 1;
358 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 /* Copy the data over; this is refcount-neutral for active entries;
361 dummy entries aren't copied over, of course */
Raymond Hettinger5088f602015-12-13 18:45:01 -0800362 newmask = (size_t)so->mask;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800363 if (oldfill == oldused) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800364 for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800365 if (entry->key != NULL) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800366 set_insert_clean(newtable, newmask, entry->key, entry->hash);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800367 }
368 }
369 } else {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800370 for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800371 if (entry->key != NULL && entry->key != dummy) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800372 set_insert_clean(newtable, newmask, entry->key, entry->hash);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800373 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 }
375 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (is_oldtable_malloced)
378 PyMem_DEL(oldtable);
379 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380}
381
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382static int
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700383set_contains_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000384{
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700385 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700387 entry = set_lookkey(so, key, hash);
388 if (entry != NULL)
389 return entry->key != NULL;
390 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000391}
392
393#define DISCARD_NOTFOUND 0
394#define DISCARD_FOUND 1
395
396static int
Raymond Hettinger73799b12015-07-05 16:06:10 -0700397set_discard_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800398{
399 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000401
Raymond Hettingerdc28d5a2015-07-05 10:03:20 -0700402 entry = set_lookkey(so, key, hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (entry == NULL)
404 return -1;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700405 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return DISCARD_NOTFOUND;
407 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800409 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 so->used--;
411 Py_DECREF(old_key);
412 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000413}
414
415static int
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700416set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000417{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200418 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000419
Raymond Hettingerb48d6a62015-07-05 16:27:44 -0700420 if (!PyUnicode_CheckExact(key) ||
421 (hash = ((PyASCIIObject *) key)->hash) == -1) {
422 hash = PyObject_Hash(key);
423 if (hash == -1)
424 return -1;
425 }
426 return set_add_entry(so, key, hash);
427}
428
429static int
430set_contains_key(PySetObject *so, PyObject *key)
431{
432 Py_hash_t hash;
433
434 if (!PyUnicode_CheckExact(key) ||
435 (hash = ((PyASCIIObject *) key)->hash) == -1) {
436 hash = PyObject_Hash(key);
437 if (hash == -1)
438 return -1;
439 }
440 return set_contains_entry(so, key, hash);
441}
442
443static int
444set_discard_key(PySetObject *so, PyObject *key)
445{
446 Py_hash_t hash;
Christian Heimes0ded5b52007-12-10 15:50:56 +0000447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200449 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 hash = PyObject_Hash(key);
451 if (hash == -1)
452 return -1;
453 }
Raymond Hettinger73799b12015-07-05 16:06:10 -0700454 return set_discard_entry(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455}
456
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700457static void
458set_empty_to_minsize(PySetObject *so)
459{
460 memset(so->smalltable, 0, sizeof(so->smalltable));
461 so->fill = 0;
462 so->used = 0;
463 so->mask = PySet_MINSIZE - 1;
464 so->table = so->smalltable;
465 so->hash = -1;
466}
467
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000468static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000469set_clear_internal(PySetObject *so)
470{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800471 setentry *entry;
472 setentry *table = so->table;
473 Py_ssize_t fill = so->fill;
474 Py_ssize_t used = so->used;
475 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000477
Raymond Hettinger583cd032013-09-07 22:06:35 -0700478 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 /* This is delicate. During the process of clearing the set,
482 * decrefs can cause the set to mutate. To avoid fatal confusion
483 * (voice of experience), we have to make the set empty before
484 * clearing the slots, and never refer to anything via so->ref while
485 * clearing.
486 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700488 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 else if (fill > 0) {
491 /* It's a small table with something that needs to be cleared.
492 * Afraid the only safe way is to copy the set entries into
493 * another small table first.
494 */
495 memcpy(small_copy, table, sizeof(small_copy));
496 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700497 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 }
499 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 /* Now we can finally clear things. If C had refcounts, we could
502 * assert that the refcount on table is 1 now, i.e. that this function
503 * has unique access to it, so decref side-effects can't alter it.
504 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800505 for (entry = table; used > 0; entry++) {
506 if (entry->key && entry->key != dummy) {
507 used--;
508 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (table_is_malloced)
513 PyMem_DEL(table);
514 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515}
516
517/*
518 * Iterate over a set table. Use like so:
519 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000520 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000521 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000522 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000523 * while (set_next(yourset, &pos, &entry)) {
524 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525 * }
526 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000527 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529 */
530static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000531set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 Py_ssize_t i;
534 Py_ssize_t mask;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700535 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 assert (PyAnySet_Check(so));
538 i = *pos_ptr;
539 assert(i >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 mask = so->mask;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700541 entry = &so->table[i];
542 while (i <= mask && (entry->key == NULL || entry->key == dummy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 i++;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700544 entry++;
545 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 *pos_ptr = i+1;
547 if (i > mask)
548 return 0;
Raymond Hettingeref6bd7d2015-07-06 08:43:37 -0700549 assert(entry != NULL);
550 *entry_ptr = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000552}
553
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000554static void
555set_dealloc(PySetObject *so)
556{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200557 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800558 Py_ssize_t used = so->used;
559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 PyObject_GC_UnTrack(so);
561 Py_TRASHCAN_SAFE_BEGIN(so)
562 if (so->weakreflist != NULL)
563 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000564
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800565 for (entry = so->table; used > 0; entry++) {
566 if (entry->key && entry->key != dummy) {
567 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700568 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 }
570 }
571 if (so->table != so->smalltable)
572 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700573 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000575}
576
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000577static PyObject *
578set_repr(PySetObject *so)
579{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200580 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (status != 0) {
584 if (status < 0)
585 return NULL;
586 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
587 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 /* shortcut for the empty set */
590 if (!so->used) {
591 Py_ReprLeave((PyObject*)so);
592 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
593 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 keys = PySequence_List((PyObject *)so);
596 if (keys == NULL)
597 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000598
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200599 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 listrepr = PyObject_Repr(keys);
601 Py_DECREF(keys);
602 if (listrepr == NULL)
603 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200604 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200606 if (tmp == NULL)
607 goto done;
608 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200609
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200610 if (Py_TYPE(so) != &PySet_Type)
611 result = PyUnicode_FromFormat("%s({%U})",
612 Py_TYPE(so)->tp_name,
613 listrepr);
614 else
615 result = PyUnicode_FromFormat("{%U}", listrepr);
616 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000617done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 Py_ReprLeave((PyObject*)so);
619 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000620}
621
Martin v. Löwis18e16552006-02-15 17:27:45 +0000622static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000623set_len(PyObject *so)
624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000626}
627
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000628static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000629set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000632 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200633 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700634 setentry *so_entry;
635 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 assert (PyAnySet_Check(so));
638 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 other = (PySetObject*)otherset;
641 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700642 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 return 0;
644 /* Do one big resize at the start, rather than
645 * incrementally resizing as we insert new keys. Expect
646 * that there will be no (or few) overlapping keys.
647 */
Raymond Hettinger15f08692015-07-03 17:21:17 -0700648 if ((so->fill + other->used)*3 >= so->mask*2) {
Raymond Hettinger48973002015-07-03 20:00:03 -0700649 if (set_table_resize(so, so->used + other->used) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return -1;
651 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700652 so_entry = so->table;
653 other_entry = other->table;
654
655 /* If our table is empty, and both tables have the same size, and
656 there are no dummies to eliminate, then just copy the pointers. */
657 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
658 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
659 key = other_entry->key;
660 if (key != NULL) {
661 assert(so_entry->key == NULL);
662 Py_INCREF(key);
663 so_entry->key = key;
664 so_entry->hash = other_entry->hash;
665 }
666 }
667 so->fill = other->fill;
668 so->used = other->used;
669 return 0;
670 }
671
672 /* If our table is empty, we can use set_insert_clean() */
673 if (so->fill == 0) {
Raymond Hettinger5088f602015-12-13 18:45:01 -0800674 setentry *newtable = so->table;
675 size_t newmask = (size_t)so->mask;
Raymond Hettinger6019c8c2015-11-17 08:28:07 -0800676 so->fill = other->used;
677 so->used = other->used;
Raymond Hettingere4495872015-12-15 00:42:30 -0800678 for (i = other->mask + 1; i > 0 ; i--, other_entry++) {
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700679 key = other_entry->key;
680 if (key != NULL && key != dummy) {
681 Py_INCREF(key);
Raymond Hettinger5088f602015-12-13 18:45:01 -0800682 set_insert_clean(newtable, newmask, key, other_entry->hash);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700683 }
684 }
685 return 0;
686 }
687
688 /* We can't assure there are no duplicates, so do normal insertions */
Raymond Hettingera3626bc2015-07-15 23:50:14 -0700689 for (i = 0; i <= other->mask; i++) {
690 other_entry = &other->table[i];
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700691 key = other_entry->key;
692 if (key != NULL && key != dummy) {
Raymond Hettinger73799b12015-07-05 16:06:10 -0700693 if (set_add_entry(so, key, other_entry->hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 }
696 }
697 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000698}
699
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000700static PyObject *
701set_pop(PySetObject *so)
702{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800703 /* Make sure the search finger is in bounds */
704 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200705 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 assert (PyAnySet_Check(so));
709 if (so->used == 0) {
710 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
711 return NULL;
712 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000713
Raymond Hettinger1202a472015-01-18 13:12:42 -0800714 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
715 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800716 if (i > so->mask)
717 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 }
719 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800721 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800723 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000725}
726
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000727PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
728Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000729
730static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000731set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 Py_ssize_t pos = 0;
734 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 while (set_next(so, &pos, &entry))
737 Py_VISIT(entry->key);
738 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000739}
740
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700741/* Work to increase the bit dispersion for closely spaced hash values.
742 This is important because some use cases have many combinations of a
743 small number of elements with nearby hashes so that many distinct
744 combinations collapse to only a handful of distinct hash values. */
745
746static Py_uhash_t
747_shuffle_bits(Py_uhash_t h)
748{
749 return ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
750}
751
752/* Most of the constants in this hash algorithm are randomly chosen
753 large primes with "interesting bit patterns" and that passed tests
754 for good collision statistics on a variety of problematic datasets
755 including powersets and graph structures (such as David Eppstein's
756 graph recipes in Lib/test/test_set.py) */
757
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000758static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000759frozenset_hash(PyObject *self)
760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 PySetObject *so = (PySetObject *)self;
Raymond Hettinger41481952015-08-07 00:43:39 -0700762 Py_uhash_t hash = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000764
Raymond Hettingerb501a272015-08-06 22:15:22 -0700765 if (so->hash != -1)
766 return so->hash;
767
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700768 /* Xor-in shuffled bits from every entry's hash field because xor is
769 commutative and a frozenset hash should be independent of order.
770
771 For speed, include null entries and dummy entries and then
772 subtract out their effect afterwards so that the final hash
773 depends only on active entries. This allows the code to be
774 vectorized by the compiler and it saves the unpredictable
775 branches that would arise when trying to exclude null and dummy
776 entries on every iteration. */
777
778 for (entry = so->table; entry <= &so->table[so->mask]; entry++)
779 hash ^= _shuffle_bits(entry->hash);
780
Raymond Hettingera286a512015-08-01 11:07:11 -0700781 /* Remove the effect of an odd number of NULL entries */
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700782 if ((so->mask + 1 - so->fill) & 1)
783 hash ^= _shuffle_bits(0);
784
785 /* Remove the effect of an odd number of dummy entries */
786 if ((so->fill - so->used) & 1)
787 hash ^= _shuffle_bits(-1);
788
Raymond Hettinger41481952015-08-07 00:43:39 -0700789 /* Factor in the number of active entries */
790 hash ^= ((Py_uhash_t)PySet_GET_SIZE(self) + 1) * 1927868237UL;
791
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700792 /* Disperse patterns arising in nested frozensets */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800793 hash = hash * 69069U + 907133923UL;
Raymond Hettingerfbffdef2015-08-01 09:53:00 -0700794
Raymond Hettinger36c05002015-08-01 10:57:42 -0700795 /* -1 is reserved as an error code */
Victor Stinner12174a52014-08-15 23:17:38 +0200796 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800797 hash = 590923713UL;
Raymond Hettinger36c05002015-08-01 10:57:42 -0700798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 so->hash = hash;
800 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000801}
802
Raymond Hettingera9d99362005-08-05 00:01:15 +0000803/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject_HEAD
807 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
808 Py_ssize_t si_used;
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700809 Py_ssize_t si_pos;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811} setiterobject;
812
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813static void
814setiter_dealloc(setiterobject *si)
815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 Py_XDECREF(si->si_set);
817 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000818}
819
820static int
821setiter_traverse(setiterobject *si, visitproc visit, void *arg)
822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 Py_VISIT(si->si_set);
824 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825}
826
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000827static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828setiter_len(setiterobject *si)
829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 Py_ssize_t len = 0;
831 if (si->si_set != NULL && si->si_used == si->si_set->used)
832 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000833 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000834}
835
Armin Rigof5b3e362006-02-11 21:32:43 +0000836PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000837
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000838static PyObject *setiter_iternext(setiterobject *si);
839
840static PyObject *
841setiter_reduce(setiterobject *si)
842{
843 PyObject *list;
844 setiterobject tmp;
845
846 list = PyList_New(0);
847 if (!list)
848 return NULL;
849
Ezio Melotti0e1af282012-09-28 16:43:40 +0300850 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000851 tmp = *si;
852 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300853
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000854 /* iterate the temporary into a list */
855 for(;;) {
856 PyObject *element = setiter_iternext(&tmp);
857 if (element) {
858 if (PyList_Append(list, element)) {
859 Py_DECREF(element);
860 Py_DECREF(list);
861 Py_XDECREF(tmp.si_set);
862 return NULL;
863 }
864 Py_DECREF(element);
865 } else
866 break;
867 }
868 Py_XDECREF(tmp.si_set);
869 /* check for error */
870 if (tmp.si_set != NULL) {
871 /* we have an error */
872 Py_DECREF(list);
873 return NULL;
874 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200875 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000876}
877
878PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
879
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000880static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000882 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000884};
885
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000886static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000887{
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700888 PyObject *key;
889 Py_ssize_t i, mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200890 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (so == NULL)
894 return NULL;
895 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (si->si_used != so->used) {
898 PyErr_SetString(PyExc_RuntimeError,
899 "Set changed size during iteration");
900 si->si_used = -1; /* Make this state sticky */
901 return NULL;
902 }
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700903
904 i = si->si_pos;
905 assert(i>=0);
906 entry = so->table;
907 mask = so->mask;
908 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
909 i++;
910 si->si_pos = i+1;
911 if (i > mask)
912 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 si->len--;
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700914 key = entry[i].key;
915 Py_INCREF(key);
916 return key;
917
918fail:
919 Py_DECREF(so);
920 si->si_set = NULL;
921 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000922}
923
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000924PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 PyVarObject_HEAD_INIT(&PyType_Type, 0)
926 "set_iterator", /* tp_name */
927 sizeof(setiterobject), /* tp_basicsize */
928 0, /* tp_itemsize */
929 /* methods */
930 (destructor)setiter_dealloc, /* tp_dealloc */
931 0, /* tp_print */
932 0, /* tp_getattr */
933 0, /* tp_setattr */
934 0, /* tp_reserved */
935 0, /* tp_repr */
936 0, /* tp_as_number */
937 0, /* tp_as_sequence */
938 0, /* tp_as_mapping */
939 0, /* tp_hash */
940 0, /* tp_call */
941 0, /* tp_str */
942 PyObject_GenericGetAttr, /* tp_getattro */
943 0, /* tp_setattro */
944 0, /* tp_as_buffer */
Raymond Hettinger5d2385f2015-07-08 11:52:27 -0700945 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 0, /* tp_doc */
947 (traverseproc)setiter_traverse, /* tp_traverse */
948 0, /* tp_clear */
949 0, /* tp_richcompare */
950 0, /* tp_weaklistoffset */
951 PyObject_SelfIter, /* tp_iter */
952 (iternextfunc)setiter_iternext, /* tp_iternext */
953 setiter_methods, /* tp_methods */
954 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000955};
956
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000957static PyObject *
958set_iter(PySetObject *so)
959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
961 if (si == NULL)
962 return NULL;
963 Py_INCREF(so);
964 si->si_set = so;
965 si->si_used = so->used;
Raymond Hettinger9632a7d2015-07-07 15:29:24 -0700966 si->si_pos = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 si->len = so->used;
968 _PyObject_GC_TRACK(si);
969 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000970}
971
Raymond Hettingerd7946662005-08-01 21:39:29 +0000972static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000973set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 if (PyAnySet_Check(other))
978 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 if (PyDict_CheckExact(other)) {
981 PyObject *value;
982 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000983 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 /* Do one big resize at the start, rather than
987 * incrementally resizing as we insert new keys. Expect
988 * that there will be no (or few) overlapping keys.
989 */
Raymond Hettingerc2480dc2015-07-04 08:46:31 -0700990 if (dictsize < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 return -1;
Raymond Hettinger15f08692015-07-03 17:21:17 -0700992 if ((so->fill + dictsize)*3 >= so->mask*2) {
Raymond Hettinger48973002015-07-03 20:00:03 -0700993 if (set_table_resize(so, so->used + dictsize) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 return -1;
995 }
996 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -0700997 if (set_add_entry(so, key, hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 return -1;
999 }
1000 return 0;
1001 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 it = PyObject_GetIter(other);
1004 if (it == NULL)
1005 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001008 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_DECREF(it);
1010 Py_DECREF(key);
1011 return -1;
1012 }
1013 Py_DECREF(key);
1014 }
1015 Py_DECREF(it);
1016 if (PyErr_Occurred())
1017 return -1;
1018 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001019}
1020
1021static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001022set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1027 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001028 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 return NULL;
1030 }
1031 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001032}
1033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001035"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001036
Raymond Hettinger426d9952014-05-18 21:40:20 +01001037/* XXX Todo:
1038 If aligned memory allocations become available, make the
1039 set object 64 byte aligned so that most of the fields
1040 can be retrieved or updated in a single cache line.
1041*/
1042
Raymond Hettingera38123e2003-11-24 22:18:49 +00001043static PyObject *
1044make_new_set(PyTypeObject *type, PyObject *iterable)
1045{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001046 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001049 so = (PySetObject *)type->tp_alloc(type, 0);
1050 if (so == NULL)
1051 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001052
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001053 so->fill = 0;
1054 so->used = 0;
1055 so->mask = PySet_MINSIZE - 1;
1056 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001057 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -08001058 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001062 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 Py_DECREF(so);
1064 return NULL;
1065 }
1066 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001069}
1070
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001071static PyObject *
1072make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1075 if (PyType_IsSubtype(type, &PySet_Type))
1076 type = &PySet_Type;
1077 else
1078 type = &PyFrozenSet_Type;
1079 }
1080 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001081}
1082
Raymond Hettingerd7946662005-08-01 21:39:29 +00001083/* The empty frozenset is a singleton */
1084static PyObject *emptyfrozenset = NULL;
1085
Raymond Hettingera690a992003-11-16 16:17:49 +00001086static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001087frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001090
Raymond Hettingerf5021542016-02-04 02:46:16 -08001091 if (kwds != NULL && type == &PyFrozenSet_Type
1092 && !_PyArg_NoKeywords("frozenset()", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1096 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (type != &PyFrozenSet_Type)
1099 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 if (iterable != NULL) {
1102 /* frozenset(f) is idempotent */
1103 if (PyFrozenSet_CheckExact(iterable)) {
1104 Py_INCREF(iterable);
1105 return iterable;
1106 }
1107 result = make_new_set(type, iterable);
1108 if (result == NULL || PySet_GET_SIZE(result))
1109 return result;
1110 Py_DECREF(result);
1111 }
1112 /* The empty frozenset is a singleton */
1113 if (emptyfrozenset == NULL)
1114 emptyfrozenset = make_new_set(type, NULL);
1115 Py_XINCREF(emptyfrozenset);
1116 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001117}
1118
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001119static PyObject *
1120set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001123}
1124
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001125/* set_swap_bodies() switches the contents of any two sets by moving their
1126 internal data pointers and, if needed, copying the internal smalltables.
1127 Semantically equivalent to:
1128
1129 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1130
1131 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001132 Useful for operations that update in-place (by allowing an intermediate
1133 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001134*/
1135
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001136static void
1137set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 Py_ssize_t t;
1140 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001142 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 t = a->fill; a->fill = b->fill; b->fill = t;
1145 t = a->used; a->used = b->used; b->used = t;
1146 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 u = a->table;
1149 if (a->table == a->smalltable)
1150 u = b->smalltable;
1151 a->table = b->table;
1152 if (b->table == b->smalltable)
1153 a->table = a->smalltable;
1154 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (a->table == a->smalltable || b->table == b->smalltable) {
1157 memcpy(tab, a->smalltable, sizeof(tab));
1158 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1159 memcpy(b->smalltable, tab, sizeof(tab));
1160 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1163 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1164 h = a->hash; a->hash = b->hash; b->hash = h;
1165 } else {
1166 a->hash = -1;
1167 b->hash = -1;
1168 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001169}
1170
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001171static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001172set_copy(PySetObject *so)
1173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001175}
1176
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001177static PyObject *
1178frozenset_copy(PySetObject *so)
1179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (PyFrozenSet_CheckExact(so)) {
1181 Py_INCREF(so);
1182 return (PyObject *)so;
1183 }
1184 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001185}
1186
Raymond Hettingera690a992003-11-16 16:17:49 +00001187PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1188
1189static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001190set_clear(PySetObject *so)
1191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 set_clear_internal(so);
1193 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001194}
1195
1196PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1197
1198static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001199set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 PySetObject *result;
1202 PyObject *other;
1203 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 result = (PySetObject *)set_copy(so);
1206 if (result == NULL)
1207 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1210 other = PyTuple_GET_ITEM(args, i);
1211 if ((PyObject *)so == other)
1212 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001213 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 Py_DECREF(result);
1215 return NULL;
1216 }
1217 }
1218 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001219}
1220
1221PyDoc_STRVAR(union_doc,
1222 "Return the union of sets as a new set.\n\
1223\n\
1224(i.e. all elements that are in either set.)");
1225
1226static PyObject *
1227set_or(PySetObject *so, PyObject *other)
1228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001230
Brian Curtindfc80e32011-08-10 20:28:54 -05001231 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1232 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 result = (PySetObject *)set_copy(so);
1235 if (result == NULL)
1236 return NULL;
1237 if ((PyObject *)so == other)
1238 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001239 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 Py_DECREF(result);
1241 return NULL;
1242 }
1243 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001244}
1245
Raymond Hettingera690a992003-11-16 16:17:49 +00001246static PyObject *
1247set_ior(PySetObject *so, PyObject *other)
1248{
Brian Curtindfc80e32011-08-10 20:28:54 -05001249 if (!PyAnySet_Check(other))
1250 Py_RETURN_NOTIMPLEMENTED;
1251
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001252 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 return NULL;
1254 Py_INCREF(so);
1255 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001256}
1257
1258static PyObject *
1259set_intersection(PySetObject *so, PyObject *other)
1260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 PySetObject *result;
1262 PyObject *key, *it, *tmp;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001263 Py_hash_t hash;
1264 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 if ((PyObject *)so == other)
1267 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1270 if (result == NULL)
1271 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 if (PyAnySet_Check(other)) {
1274 Py_ssize_t pos = 0;
1275 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1278 tmp = (PyObject *)so;
1279 so = (PySetObject *)other;
1280 other = tmp;
1281 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001284 key = entry->key;
1285 hash = entry->hash;
1286 rv = set_contains_entry(so, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001287 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 Py_DECREF(result);
1289 return NULL;
1290 }
1291 if (rv) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001292 if (set_add_entry(result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 Py_DECREF(result);
1294 return NULL;
1295 }
1296 }
1297 }
1298 return (PyObject *)result;
1299 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 it = PyObject_GetIter(other);
1302 if (it == NULL) {
1303 Py_DECREF(result);
1304 return NULL;
1305 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001308 hash = PyObject_Hash(key);
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001309 if (hash == -1)
1310 goto error;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001311 rv = set_contains_entry(so, key, hash);
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001312 if (rv < 0)
1313 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (rv) {
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001315 if (set_add_entry(result, key, hash))
1316 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 }
1318 Py_DECREF(key);
1319 }
1320 Py_DECREF(it);
1321 if (PyErr_Occurred()) {
1322 Py_DECREF(result);
1323 return NULL;
1324 }
1325 return (PyObject *)result;
Raymond Hettinger11ce8e62015-07-06 19:08:49 -07001326 error:
1327 Py_DECREF(it);
1328 Py_DECREF(result);
1329 Py_DECREF(key);
1330 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001331}
1332
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001333static PyObject *
1334set_intersection_multi(PySetObject *so, PyObject *args)
1335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 Py_ssize_t i;
1337 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (PyTuple_GET_SIZE(args) == 0)
1340 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 Py_INCREF(so);
1343 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1344 PyObject *other = PyTuple_GET_ITEM(args, i);
1345 PyObject *newresult = set_intersection((PySetObject *)result, other);
1346 if (newresult == NULL) {
1347 Py_DECREF(result);
1348 return NULL;
1349 }
1350 Py_DECREF(result);
1351 result = newresult;
1352 }
1353 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001354}
1355
Raymond Hettingera690a992003-11-16 16:17:49 +00001356PyDoc_STRVAR(intersection_doc,
1357"Return the intersection of two sets as a new set.\n\
1358\n\
1359(i.e. all elements that are in both sets.)");
1360
1361static PyObject *
1362set_intersection_update(PySetObject *so, PyObject *other)
1363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 tmp = set_intersection(so, other);
1367 if (tmp == NULL)
1368 return NULL;
1369 set_swap_bodies(so, (PySetObject *)tmp);
1370 Py_DECREF(tmp);
1371 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001372}
1373
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001374static PyObject *
1375set_intersection_update_multi(PySetObject *so, PyObject *args)
1376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 tmp = set_intersection_multi(so, args);
1380 if (tmp == NULL)
1381 return NULL;
1382 set_swap_bodies(so, (PySetObject *)tmp);
1383 Py_DECREF(tmp);
1384 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001385}
1386
Raymond Hettingera690a992003-11-16 16:17:49 +00001387PyDoc_STRVAR(intersection_update_doc,
1388"Update a set with the intersection of itself and another.");
1389
1390static PyObject *
1391set_and(PySetObject *so, PyObject *other)
1392{
Brian Curtindfc80e32011-08-10 20:28:54 -05001393 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1394 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001396}
1397
1398static PyObject *
1399set_iand(PySetObject *so, PyObject *other)
1400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001402
Brian Curtindfc80e32011-08-10 20:28:54 -05001403 if (!PyAnySet_Check(other))
1404 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 result = set_intersection_update(so, other);
1406 if (result == NULL)
1407 return NULL;
1408 Py_DECREF(result);
1409 Py_INCREF(so);
1410 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001411}
1412
Guido van Rossum58da9312007-11-10 23:39:45 +00001413static PyObject *
1414set_isdisjoint(PySetObject *so, PyObject *other)
1415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 PyObject *key, *it, *tmp;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001417 int rv;
Guido van Rossum58da9312007-11-10 23:39:45 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if ((PyObject *)so == other) {
1420 if (PySet_GET_SIZE(so) == 0)
1421 Py_RETURN_TRUE;
1422 else
1423 Py_RETURN_FALSE;
1424 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if (PyAnySet_CheckExact(other)) {
1427 Py_ssize_t pos = 0;
1428 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1431 tmp = (PyObject *)so;
1432 so = (PySetObject *)other;
1433 other = tmp;
1434 }
1435 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001436 rv = set_contains_entry(so, entry->key, entry->hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001437 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 return NULL;
1439 if (rv)
1440 Py_RETURN_FALSE;
1441 }
1442 Py_RETURN_TRUE;
1443 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 it = PyObject_GetIter(other);
1446 if (it == NULL)
1447 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 while ((key = PyIter_Next(it)) != NULL) {
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001450 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (hash == -1) {
1453 Py_DECREF(key);
1454 Py_DECREF(it);
1455 return NULL;
1456 }
Raymond Hettinger73799b12015-07-05 16:06:10 -07001457 rv = set_contains_entry(so, key, hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 Py_DECREF(key);
Raymond Hettingerc2480dc2015-07-04 08:46:31 -07001459 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 Py_DECREF(it);
1461 return NULL;
1462 }
1463 if (rv) {
1464 Py_DECREF(it);
1465 Py_RETURN_FALSE;
1466 }
1467 }
1468 Py_DECREF(it);
1469 if (PyErr_Occurred())
1470 return NULL;
1471 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001472}
1473
1474PyDoc_STRVAR(isdisjoint_doc,
1475"Return True if two sets have a null intersection.");
1476
Neal Norwitz6576bd82005-11-13 18:41:28 +00001477static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001478set_difference_update_internal(PySetObject *so, PyObject *other)
1479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 if ((PyObject *)so == other)
1481 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (PyAnySet_Check(other)) {
1484 setentry *entry;
1485 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettinger73799b12015-07-05 16:06:10 -07001488 if (set_discard_entry(so, entry->key, entry->hash) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 return -1;
1490 } else {
1491 PyObject *key, *it;
1492 it = PyObject_GetIter(other);
1493 if (it == NULL)
1494 return -1;
1495
1496 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerb3223262015-07-03 23:37:16 -07001497 if (set_discard_key(so, key) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 Py_DECREF(it);
1499 Py_DECREF(key);
1500 return -1;
1501 }
1502 Py_DECREF(key);
1503 }
1504 Py_DECREF(it);
1505 if (PyErr_Occurred())
1506 return -1;
1507 }
Raymond Hettingere186c762015-07-04 11:28:35 -07001508 /* If more than 1/4th are dummies, then resize them away. */
1509 if ((size_t)(so->fill - so->used) <= (size_t)so->mask / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 return 0;
Raymond Hettinger48973002015-07-03 20:00:03 -07001511 return set_table_resize(so, so->used);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001512}
1513
Raymond Hettingera690a992003-11-16 16:17:49 +00001514static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001515set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1520 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001521 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 return NULL;
1523 }
1524 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001525}
1526
1527PyDoc_STRVAR(difference_update_doc,
1528"Remove all elements of another set from this set.");
1529
1530static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001531set_copy_and_difference(PySetObject *so, PyObject *other)
1532{
1533 PyObject *result;
1534
1535 result = set_copy(so);
1536 if (result == NULL)
1537 return NULL;
Raymond Hettingerb3223262015-07-03 23:37:16 -07001538 if (set_difference_update_internal((PySetObject *) result, other) == 0)
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001539 return result;
1540 Py_DECREF(result);
1541 return NULL;
1542}
1543
1544static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001545set_difference(PySetObject *so, PyObject *other)
1546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 PyObject *result;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001548 PyObject *key;
1549 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 setentry *entry;
1551 Py_ssize_t pos = 0;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001552 int rv;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001555 return set_copy_and_difference(so, other);
1556 }
1557
1558 /* If len(so) much more than len(other), it's more efficient to simply copy
1559 * so and then iterate other looking for common elements. */
1560 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1561 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 result = make_new_set_basetype(Py_TYPE(so), NULL);
1565 if (result == NULL)
1566 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (PyDict_CheckExact(other)) {
1569 while (set_next(so, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001570 key = entry->key;
1571 hash = entry->hash;
Raymond Hettinger15f08692015-07-03 17:21:17 -07001572 rv = _PyDict_Contains(other, key, hash);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001573 if (rv < 0) {
1574 Py_DECREF(result);
1575 return NULL;
1576 }
1577 if (!rv) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001578 if (set_add_entry((PySetObject *)result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 Py_DECREF(result);
1580 return NULL;
1581 }
1582 }
1583 }
1584 return result;
1585 }
1586
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001587 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 while (set_next(so, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001589 key = entry->key;
1590 hash = entry->hash;
1591 rv = set_contains_entry((PySetObject *)other, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001592 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 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;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001604}
1605
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001606static PyObject *
1607set_difference_multi(PySetObject *so, PyObject *args)
1608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 Py_ssize_t i;
1610 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (PyTuple_GET_SIZE(args) == 0)
1613 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 other = PyTuple_GET_ITEM(args, 0);
1616 result = set_difference(so, other);
1617 if (result == NULL)
1618 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1621 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001622 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 Py_DECREF(result);
1624 return NULL;
1625 }
1626 }
1627 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001628}
1629
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001630PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001631"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001632\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001633(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001634static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001635set_sub(PySetObject *so, PyObject *other)
1636{
Brian Curtindfc80e32011-08-10 20:28:54 -05001637 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1638 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001640}
1641
1642static PyObject *
1643set_isub(PySetObject *so, PyObject *other)
1644{
Brian Curtindfc80e32011-08-10 20:28:54 -05001645 if (!PyAnySet_Check(other))
1646 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001647 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 return NULL;
1649 Py_INCREF(so);
1650 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001651}
1652
1653static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001654set_symmetric_difference_update(PySetObject *so, PyObject *other)
1655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 PySetObject *otherset;
1657 PyObject *key;
1658 Py_ssize_t pos = 0;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001659 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 setentry *entry;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001661 int rv;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 if ((PyObject *)so == other)
1664 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 if (PyDict_CheckExact(other)) {
1667 PyObject *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001669 Py_INCREF(key);
Raymond Hettinger73799b12015-07-05 16:06:10 -07001670 rv = set_discard_entry(so, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001671 if (rv < 0) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001672 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001675 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001676 if (set_add_entry(so, key, hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001677 Py_DECREF(key);
1678 return NULL;
1679 }
1680 }
Georg Brandl2d444492010-09-03 10:52:55 +00001681 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 }
1683 Py_RETURN_NONE;
1684 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 if (PyAnySet_Check(other)) {
1687 Py_INCREF(other);
1688 otherset = (PySetObject *)other;
1689 } else {
1690 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1691 if (otherset == NULL)
1692 return NULL;
1693 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 while (set_next(otherset, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001696 key = entry->key;
1697 hash = entry->hash;
1698 rv = set_discard_entry(so, key, hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001699 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 Py_DECREF(otherset);
1701 return NULL;
1702 }
1703 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001704 if (set_add_entry(so, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 Py_DECREF(otherset);
1706 return NULL;
1707 }
1708 }
1709 }
1710 Py_DECREF(otherset);
1711 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001712}
1713
1714PyDoc_STRVAR(symmetric_difference_update_doc,
1715"Update a set with the symmetric difference of itself and another.");
1716
1717static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001718set_symmetric_difference(PySetObject *so, PyObject *other)
1719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 PyObject *rv;
1721 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1724 if (otherset == NULL)
1725 return NULL;
1726 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1727 if (rv == NULL)
1728 return NULL;
1729 Py_DECREF(rv);
1730 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001731}
1732
1733PyDoc_STRVAR(symmetric_difference_doc,
1734"Return the symmetric difference of two sets as a new set.\n\
1735\n\
1736(i.e. all elements that are in exactly one of the sets.)");
1737
1738static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001739set_xor(PySetObject *so, PyObject *other)
1740{
Brian Curtindfc80e32011-08-10 20:28:54 -05001741 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1742 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001744}
1745
1746static PyObject *
1747set_ixor(PySetObject *so, PyObject *other)
1748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001750
Brian Curtindfc80e32011-08-10 20:28:54 -05001751 if (!PyAnySet_Check(other))
1752 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 result = set_symmetric_difference_update(so, other);
1754 if (result == NULL)
1755 return NULL;
1756 Py_DECREF(result);
1757 Py_INCREF(so);
1758 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001759}
1760
1761static PyObject *
1762set_issubset(PySetObject *so, PyObject *other)
1763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 setentry *entry;
1765 Py_ssize_t pos = 0;
Raymond Hettinger73799b12015-07-05 16:06:10 -07001766 int rv;
Raymond Hettingera690a992003-11-16 16:17:49 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 if (!PyAnySet_Check(other)) {
1769 PyObject *tmp, *result;
1770 tmp = make_new_set(&PySet_Type, other);
1771 if (tmp == NULL)
1772 return NULL;
1773 result = set_issubset(so, tmp);
1774 Py_DECREF(tmp);
1775 return result;
1776 }
1777 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1778 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 while (set_next(so, &pos, &entry)) {
Raymond Hettinger73799b12015-07-05 16:06:10 -07001781 rv = set_contains_entry((PySetObject *)other, entry->key, entry->hash);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001782 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 return NULL;
1784 if (!rv)
1785 Py_RETURN_FALSE;
1786 }
1787 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001788}
1789
1790PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1791
1792static PyObject *
1793set_issuperset(PySetObject *so, PyObject *other)
1794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 if (!PyAnySet_Check(other)) {
1798 tmp = make_new_set(&PySet_Type, other);
1799 if (tmp == NULL)
1800 return NULL;
1801 result = set_issuperset(so, tmp);
1802 Py_DECREF(tmp);
1803 return result;
1804 }
1805 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001806}
1807
1808PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1809
Raymond Hettingera690a992003-11-16 16:17:49 +00001810static PyObject *
1811set_richcompare(PySetObject *v, PyObject *w, int op)
1812{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001813 PyObject *r1;
1814 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001815
Brian Curtindfc80e32011-08-10 20:28:54 -05001816 if(!PyAnySet_Check(w))
1817 Py_RETURN_NOTIMPLEMENTED;
1818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 switch (op) {
1820 case Py_EQ:
1821 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1822 Py_RETURN_FALSE;
1823 if (v->hash != -1 &&
1824 ((PySetObject *)w)->hash != -1 &&
1825 v->hash != ((PySetObject *)w)->hash)
1826 Py_RETURN_FALSE;
1827 return set_issubset(v, w);
1828 case Py_NE:
1829 r1 = set_richcompare(v, w, Py_EQ);
1830 if (r1 == NULL)
1831 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001832 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001834 if (r2 < 0)
1835 return NULL;
1836 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 case Py_LE:
1838 return set_issubset(v, w);
1839 case Py_GE:
1840 return set_issuperset(v, w);
1841 case Py_LT:
1842 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1843 Py_RETURN_FALSE;
1844 return set_issubset(v, w);
1845 case Py_GT:
1846 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1847 Py_RETURN_FALSE;
1848 return set_issuperset(v, w);
1849 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001850 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001851}
1852
Raymond Hettingera690a992003-11-16 16:17:49 +00001853static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001854set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001855{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001856 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 return NULL;
1858 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001859}
1860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001862"Add an element to a set.\n\
1863\n\
1864This has no effect if the element is already present.");
1865
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001866static int
1867set_contains(PySetObject *so, PyObject *key)
1868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 PyObject *tmpkey;
1870 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 rv = set_contains_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001873 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1875 return -1;
1876 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001877 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 if (tmpkey == NULL)
1879 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001880 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 Py_DECREF(tmpkey);
1882 }
1883 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001884}
1885
1886static PyObject *
1887set_direct_contains(PySetObject *so, PyObject *key)
1888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 result = set_contains(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001892 if (result < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 return NULL;
1894 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001895}
1896
1897PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1898
Raymond Hettingera690a992003-11-16 16:17:49 +00001899static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001900set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 PyObject *tmpkey;
1903 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 rv = set_discard_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001906 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1908 return NULL;
1909 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001910 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 if (tmpkey == NULL)
1912 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 Py_DECREF(tmpkey);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001915 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 return NULL;
1917 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001920 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 return NULL;
1922 }
1923 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001924}
1925
1926PyDoc_STRVAR(remove_doc,
1927"Remove an element from a set; it must be a member.\n\
1928\n\
1929If the element is not a member, raise a KeyError.");
1930
1931static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001932set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001933{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001934 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 rv = set_discard_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001938 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1940 return NULL;
1941 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001942 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 if (tmpkey == NULL)
1944 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001945 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 Py_DECREF(tmpkey);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001947 if (rv < 0)
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001948 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 }
1950 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001951}
1952
1953PyDoc_STRVAR(discard_doc,
1954"Remove an element from a set if it is a member.\n\
1955\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001957
1958static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001959set_reduce(PySetObject *so)
1960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001962 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 keys = PySequence_List((PyObject *)so);
1965 if (keys == NULL)
1966 goto done;
1967 args = PyTuple_Pack(1, keys);
1968 if (args == NULL)
1969 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001970 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 if (dict == NULL) {
1972 PyErr_Clear();
1973 dict = Py_None;
1974 Py_INCREF(dict);
1975 }
1976 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001977done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 Py_XDECREF(args);
1979 Py_XDECREF(keys);
1980 Py_XDECREF(dict);
1981 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001982}
1983
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001984static PyObject *
1985set_sizeof(PySetObject *so)
1986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001988
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001989 res = _PyObject_SIZE(Py_TYPE(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 if (so->table != so->smalltable)
1991 res = res + (so->mask + 1) * sizeof(setentry);
1992 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001993}
1994
1995PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001996static int
1997set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 if (!PyAnySet_Check(self))
2002 return -1;
Raymond Hettingerb72e21b2016-03-25 02:29:59 -07002003 if (kwds != NULL && PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 return -1;
2005 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2006 return -1;
Raymond Hettingerb72e21b2016-03-25 02:29:59 -07002007 if (self->fill)
2008 set_clear_internal(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 self->hash = -1;
2010 if (iterable == NULL)
2011 return 0;
2012 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002013}
2014
Raymond Hettingera690a992003-11-16 16:17:49 +00002015static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 set_len, /* sq_length */
2017 0, /* sq_concat */
2018 0, /* sq_repeat */
2019 0, /* sq_item */
2020 0, /* sq_slice */
2021 0, /* sq_ass_item */
2022 0, /* sq_ass_slice */
2023 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002024};
2025
2026/* set object ********************************************************/
2027
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002028#ifdef Py_DEBUG
2029static PyObject *test_c_api(PySetObject *so);
2030
2031PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2032All is well if assertions don't fail.");
2033#endif
2034
Raymond Hettingera690a992003-11-16 16:17:49 +00002035static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 {"add", (PyCFunction)set_add, METH_O,
2037 add_doc},
2038 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2039 clear_doc},
2040 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2041 contains_doc},
2042 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2043 copy_doc},
2044 {"discard", (PyCFunction)set_discard, METH_O,
2045 discard_doc},
2046 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2047 difference_doc},
2048 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2049 difference_update_doc},
2050 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2051 intersection_doc},
2052 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2053 intersection_update_doc},
2054 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2055 isdisjoint_doc},
2056 {"issubset", (PyCFunction)set_issubset, METH_O,
2057 issubset_doc},
2058 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2059 issuperset_doc},
2060 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2061 pop_doc},
2062 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2063 reduce_doc},
2064 {"remove", (PyCFunction)set_remove, METH_O,
2065 remove_doc},
2066 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2067 sizeof_doc},
2068 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2069 symmetric_difference_doc},
2070 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2071 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002072#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2074 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002075#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 {"union", (PyCFunction)set_union, METH_VARARGS,
2077 union_doc},
2078 {"update", (PyCFunction)set_update, METH_VARARGS,
2079 update_doc},
2080 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002081};
2082
2083static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 0, /*nb_add*/
2085 (binaryfunc)set_sub, /*nb_subtract*/
2086 0, /*nb_multiply*/
2087 0, /*nb_remainder*/
2088 0, /*nb_divmod*/
2089 0, /*nb_power*/
2090 0, /*nb_negative*/
2091 0, /*nb_positive*/
2092 0, /*nb_absolute*/
2093 0, /*nb_bool*/
2094 0, /*nb_invert*/
2095 0, /*nb_lshift*/
2096 0, /*nb_rshift*/
2097 (binaryfunc)set_and, /*nb_and*/
2098 (binaryfunc)set_xor, /*nb_xor*/
2099 (binaryfunc)set_or, /*nb_or*/
2100 0, /*nb_int*/
2101 0, /*nb_reserved*/
2102 0, /*nb_float*/
2103 0, /*nb_inplace_add*/
2104 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2105 0, /*nb_inplace_multiply*/
2106 0, /*nb_inplace_remainder*/
2107 0, /*nb_inplace_power*/
2108 0, /*nb_inplace_lshift*/
2109 0, /*nb_inplace_rshift*/
2110 (binaryfunc)set_iand, /*nb_inplace_and*/
2111 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2112 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002113};
2114
2115PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002116"set() -> new empty set object\n\
2117set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002118\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002119Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002120
2121PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2123 "set", /* tp_name */
2124 sizeof(PySetObject), /* tp_basicsize */
2125 0, /* tp_itemsize */
2126 /* methods */
2127 (destructor)set_dealloc, /* tp_dealloc */
2128 0, /* tp_print */
2129 0, /* tp_getattr */
2130 0, /* tp_setattr */
2131 0, /* tp_reserved */
2132 (reprfunc)set_repr, /* tp_repr */
2133 &set_as_number, /* tp_as_number */
2134 &set_as_sequence, /* tp_as_sequence */
2135 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002136 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 0, /* tp_call */
2138 0, /* tp_str */
2139 PyObject_GenericGetAttr, /* tp_getattro */
2140 0, /* tp_setattro */
2141 0, /* tp_as_buffer */
2142 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2143 Py_TPFLAGS_BASETYPE, /* tp_flags */
2144 set_doc, /* tp_doc */
2145 (traverseproc)set_traverse, /* tp_traverse */
2146 (inquiry)set_clear_internal, /* tp_clear */
2147 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002148 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2149 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 0, /* tp_iternext */
2151 set_methods, /* tp_methods */
2152 0, /* tp_members */
2153 0, /* tp_getset */
2154 0, /* tp_base */
2155 0, /* tp_dict */
2156 0, /* tp_descr_get */
2157 0, /* tp_descr_set */
2158 0, /* tp_dictoffset */
2159 (initproc)set_init, /* tp_init */
2160 PyType_GenericAlloc, /* tp_alloc */
2161 set_new, /* tp_new */
2162 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002163};
2164
2165/* frozenset object ********************************************************/
2166
2167
2168static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2170 contains_doc},
2171 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2172 copy_doc},
2173 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2174 difference_doc},
Raymond Hettinger5d2385f2015-07-08 11:52:27 -07002175 {"intersection", (PyCFunction)set_intersection_multi, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 intersection_doc},
2177 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2178 isdisjoint_doc},
2179 {"issubset", (PyCFunction)set_issubset, METH_O,
2180 issubset_doc},
2181 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2182 issuperset_doc},
2183 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2184 reduce_doc},
2185 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2186 sizeof_doc},
2187 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2188 symmetric_difference_doc},
2189 {"union", (PyCFunction)set_union, METH_VARARGS,
2190 union_doc},
2191 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002192};
2193
2194static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 0, /*nb_add*/
2196 (binaryfunc)set_sub, /*nb_subtract*/
2197 0, /*nb_multiply*/
2198 0, /*nb_remainder*/
2199 0, /*nb_divmod*/
2200 0, /*nb_power*/
2201 0, /*nb_negative*/
2202 0, /*nb_positive*/
2203 0, /*nb_absolute*/
2204 0, /*nb_bool*/
2205 0, /*nb_invert*/
2206 0, /*nb_lshift*/
2207 0, /*nb_rshift*/
2208 (binaryfunc)set_and, /*nb_and*/
2209 (binaryfunc)set_xor, /*nb_xor*/
2210 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002211};
2212
2213PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002214"frozenset() -> empty frozenset object\n\
2215frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002216\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002217Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002218
2219PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2221 "frozenset", /* tp_name */
2222 sizeof(PySetObject), /* tp_basicsize */
2223 0, /* tp_itemsize */
2224 /* methods */
2225 (destructor)set_dealloc, /* tp_dealloc */
2226 0, /* tp_print */
2227 0, /* tp_getattr */
2228 0, /* tp_setattr */
2229 0, /* tp_reserved */
2230 (reprfunc)set_repr, /* tp_repr */
2231 &frozenset_as_number, /* tp_as_number */
2232 &set_as_sequence, /* tp_as_sequence */
2233 0, /* tp_as_mapping */
2234 frozenset_hash, /* tp_hash */
2235 0, /* tp_call */
2236 0, /* tp_str */
2237 PyObject_GenericGetAttr, /* tp_getattro */
2238 0, /* tp_setattro */
2239 0, /* tp_as_buffer */
2240 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2241 Py_TPFLAGS_BASETYPE, /* tp_flags */
2242 frozenset_doc, /* tp_doc */
2243 (traverseproc)set_traverse, /* tp_traverse */
2244 (inquiry)set_clear_internal, /* tp_clear */
2245 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger5d2385f2015-07-08 11:52:27 -07002246 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 (getiterfunc)set_iter, /* tp_iter */
2248 0, /* tp_iternext */
2249 frozenset_methods, /* tp_methods */
2250 0, /* tp_members */
2251 0, /* tp_getset */
2252 0, /* tp_base */
2253 0, /* tp_dict */
2254 0, /* tp_descr_get */
2255 0, /* tp_descr_set */
2256 0, /* tp_dictoffset */
2257 0, /* tp_init */
2258 PyType_GenericAlloc, /* tp_alloc */
2259 frozenset_new, /* tp_new */
2260 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002261};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002262
2263
2264/***** C API functions *************************************************/
2265
2266PyObject *
2267PySet_New(PyObject *iterable)
2268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002270}
2271
2272PyObject *
2273PyFrozenSet_New(PyObject *iterable)
2274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002276}
2277
Neal Norwitz8c49c822006-03-04 18:41:19 +00002278Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002279PySet_Size(PyObject *anyset)
2280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 if (!PyAnySet_Check(anyset)) {
2282 PyErr_BadInternalCall();
2283 return -1;
2284 }
2285 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002286}
2287
2288int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002289PySet_Clear(PyObject *set)
2290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 if (!PySet_Check(set)) {
2292 PyErr_BadInternalCall();
2293 return -1;
2294 }
2295 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002296}
2297
2298int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002299PySet_Contains(PyObject *anyset, PyObject *key)
2300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (!PyAnySet_Check(anyset)) {
2302 PyErr_BadInternalCall();
2303 return -1;
2304 }
2305 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002306}
2307
2308int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002309PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 if (!PySet_Check(set)) {
2312 PyErr_BadInternalCall();
2313 return -1;
2314 }
2315 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002316}
2317
2318int
Christian Heimesfd66e512008-01-29 12:18:50 +00002319PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 if (!PySet_Check(anyset) &&
2322 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2323 PyErr_BadInternalCall();
2324 return -1;
2325 }
2326 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002327}
2328
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002329int
Raymond Hettinger3625af52016-03-27 01:15:07 -07002330PySet_ClearFreeList(void)
2331{
2332 return 0;
2333}
2334
2335void
2336PySet_Fini(void)
2337{
2338 Py_CLEAR(emptyfrozenset);
2339}
2340
2341int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002342_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 if (!PyAnySet_Check(set)) {
2347 PyErr_BadInternalCall();
2348 return -1;
2349 }
2350 if (set_next((PySetObject *)set, pos, &entry) == 0)
2351 return 0;
2352 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002353 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002355}
2356
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002357PyObject *
2358PySet_Pop(PyObject *set)
2359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 if (!PySet_Check(set)) {
2361 PyErr_BadInternalCall();
2362 return NULL;
2363 }
2364 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002365}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002366
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002367int
2368_PySet_Update(PyObject *set, PyObject *iterable)
2369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 if (!PySet_Check(set)) {
2371 PyErr_BadInternalCall();
2372 return -1;
2373 }
2374 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002375}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002376
Raymond Hettingere259f132013-12-15 11:56:14 -08002377/* Exported for the gdb plugin's benefit. */
2378PyObject *_PySet_Dummy = dummy;
2379
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002380#ifdef Py_DEBUG
2381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002383 Returns True and original set is restored. */
2384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385#define assertRaises(call_return_value, exception) \
2386 do { \
2387 assert(call_return_value); \
2388 assert(PyErr_ExceptionMatches(exception)); \
2389 PyErr_Clear(); \
2390 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002391
2392static PyObject *
2393test_c_api(PySetObject *so)
2394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 Py_ssize_t count;
2396 char *s;
2397 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002398 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002400 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 /* Verify preconditions */
2404 assert(PyAnySet_Check(ob));
2405 assert(PyAnySet_CheckExact(ob));
2406 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 /* so.clear(); so |= set("abc"); */
2409 str = PyUnicode_FromString("abc");
2410 if (str == NULL)
2411 return NULL;
2412 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002413 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 Py_DECREF(str);
2415 return NULL;
2416 }
2417 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* Exercise type/size checks */
2420 assert(PySet_Size(ob) == 3);
2421 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 /* Raise TypeError for non-iterable constructor arguments */
2424 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2425 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 /* Raise TypeError for unhashable key */
2428 dup = PySet_New(ob);
2429 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2430 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2431 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 /* Exercise successful pop, contains, add, and discard */
2434 elem = PySet_Pop(ob);
2435 assert(PySet_Contains(ob, elem) == 0);
2436 assert(PySet_GET_SIZE(ob) == 2);
2437 assert(PySet_Add(ob, elem) == 0);
2438 assert(PySet_Contains(ob, elem) == 1);
2439 assert(PySet_GET_SIZE(ob) == 3);
2440 assert(PySet_Discard(ob, elem) == 1);
2441 assert(PySet_GET_SIZE(ob) == 2);
2442 assert(PySet_Discard(ob, elem) == 0);
2443 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 /* Exercise clear */
2446 dup2 = PySet_New(dup);
2447 assert(PySet_Clear(dup2) == 0);
2448 assert(PySet_Size(dup2) == 0);
2449 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 /* Raise SystemError on clear or update of frozen set */
2452 f = PyFrozenSet_New(dup);
2453 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2454 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2455 assert(PySet_Add(f, elem) == 0);
2456 Py_INCREF(f);
2457 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2458 Py_DECREF(f);
2459 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 /* Exercise direct iteration */
2462 i = 0, count = 0;
2463 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2464 s = _PyUnicode_AsString(x);
2465 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2466 count++;
2467 }
2468 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 /* Exercise updates */
2471 dup2 = PySet_New(NULL);
2472 assert(_PySet_Update(dup2, dup) == 0);
2473 assert(PySet_Size(dup2) == 3);
2474 assert(_PySet_Update(dup2, dup) == 0);
2475 assert(PySet_Size(dup2) == 3);
2476 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 /* Raise SystemError when self argument is not a set or frozenset. */
2479 t = PyTuple_New(0);
2480 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2481 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2482 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 /* Raise SystemError when self argument is not a set. */
2485 f = PyFrozenSet_New(dup);
2486 assert(PySet_Size(f) == 3);
2487 assert(PyFrozenSet_CheckExact(f));
2488 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2489 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2490 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 /* Raise KeyError when popping from an empty set */
2493 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2494 Py_DECREF(ob);
2495 assert(PySet_GET_SIZE(ob) == 0);
2496 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 /* Restore the set from the copy using the PyNumber API */
2499 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2500 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 /* Verify constructors accept NULL arguments */
2503 f = PySet_New(NULL);
2504 assert(f != NULL);
2505 assert(PySet_GET_SIZE(f) == 0);
2506 Py_DECREF(f);
2507 f = PyFrozenSet_New(NULL);
2508 assert(f != NULL);
2509 assert(PyFrozenSet_CheckExact(f));
2510 assert(PySet_GET_SIZE(f) == 0);
2511 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 Py_DECREF(elem);
2514 Py_DECREF(dup);
2515 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002516}
2517
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002518#undef assertRaises
2519
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002520#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002521
2522/***** Dummy Struct *************************************************/
2523
2524static PyObject *
2525dummy_repr(PyObject *op)
2526{
2527 return PyUnicode_FromString("<dummy key>");
2528}
2529
2530static void
2531dummy_dealloc(PyObject* ignore)
2532{
2533 Py_FatalError("deallocating <dummy key>");
2534}
2535
2536static PyTypeObject _PySetDummy_Type = {
2537 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2538 "<dummy key> type",
2539 0,
2540 0,
2541 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2542 0, /*tp_print*/
2543 0, /*tp_getattr*/
2544 0, /*tp_setattr*/
2545 0, /*tp_reserved*/
2546 dummy_repr, /*tp_repr*/
2547 0, /*tp_as_number*/
2548 0, /*tp_as_sequence*/
2549 0, /*tp_as_mapping*/
2550 0, /*tp_hash */
2551 0, /*tp_call */
2552 0, /*tp_str */
2553 0, /*tp_getattro */
2554 0, /*tp_setattro */
2555 0, /*tp_as_buffer */
2556 Py_TPFLAGS_DEFAULT, /*tp_flags */
2557};
2558
2559static PyObject _dummy_struct = {
2560 _PyObject_EXTRA_INIT
2561 2, &_PySetDummy_Type
2562};
2563