blob: cc87f284f60d9111fcf1bb986fc2b6ad6b9a804f [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettingerbd9b2002015-01-18 16:10:30 -08007 Copyright (c) 2003-2015 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
Raymond Hettinger4d45c102015-01-25 16:27:40 -080026 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070027 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020055 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070056 size_t perturb;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080058 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070059 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070060 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080062 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070063 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070065
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070066 perturb = hash;
67
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070068 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080069 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070070 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080071 /* startkey cannot be a dummy because the dummy hash field is -1 */
72 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080073 if (startkey == key)
74 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080075 if (PyUnicode_CheckExact(startkey)
76 && PyUnicode_CheckExact(key)
77 && unicode_eq(startkey, key))
78 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 Py_INCREF(startkey);
80 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
81 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010082 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010084 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010086 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070087 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060088 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070089 }
Raymond Hettingerafe89092013-08-28 20:59:31 -070090
Raymond Hettingerc658d852015-02-02 08:35:00 -080091 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080092 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080093 entry++;
Raymond Hettinger8651a502015-05-27 10:37:20 -070094 if (entry->hash == 0 && entry->key == NULL)
95 return entry;
Raymond Hettingerc658d852015-02-02 08:35:00 -080096 if (entry->hash == hash) {
97 PyObject *startkey = entry->key;
98 assert(startkey != dummy);
99 if (startkey == key)
100 return entry;
101 if (PyUnicode_CheckExact(startkey)
102 && PyUnicode_CheckExact(key)
103 && unicode_eq(startkey, key))
104 return entry;
105 Py_INCREF(startkey);
106 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
107 Py_DECREF(startkey);
108 if (cmp < 0)
109 return NULL;
110 if (table != so->table || entry->key != startkey)
111 return set_lookkey(so, key, hash);
112 if (cmp > 0)
113 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600114 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800115 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700116 }
117 }
118
119 perturb >>= PERTURB_SHIFT;
120 i = (i * 5 + 1 + perturb) & mask;
121
122 entry = &table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700123 if (entry->key == NULL)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700124 return entry;
125 }
126}
127
Raymond Hettinger15f08692015-07-03 17:21:17 -0700128static int set_table_resize(PySetObject *, Py_ssize_t);
129
Raymond Hettinger8651a502015-05-27 10:37:20 -0700130static int
131set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
132{
133 setentry *table = so->table;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700134 setentry *freeslot;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700135 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700136 size_t perturb;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700137 size_t mask = so->mask;
138 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
139 size_t j;
140 int cmp;
141
142 entry = &table[i];
143 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700144 goto found_unused;
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700145
146 freeslot = NULL;
147 perturb = hash;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700148
149 while (1) {
150 if (entry->hash == hash) {
151 PyObject *startkey = entry->key;
152 /* startkey cannot be a dummy because the dummy hash field is -1 */
153 assert(startkey != dummy);
154 if (startkey == key)
155 goto found_active;
156 if (PyUnicode_CheckExact(startkey)
157 && PyUnicode_CheckExact(key)
158 && unicode_eq(startkey, key))
159 goto found_active;
160 Py_INCREF(startkey);
161 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
162 Py_DECREF(startkey);
163 if (cmp < 0) /* unlikely */
164 return -1;
165 if (table != so->table || entry->key != startkey) /* unlikely */
166 return set_insert_key(so, key, hash);
167 if (cmp > 0) /* likely */
168 goto found_active;
169 mask = so->mask; /* help avoid a register spill */
170 }
171 if (entry->hash == -1 && freeslot == NULL)
172 freeslot = entry;
173
174 if (i + LINEAR_PROBES <= mask) {
175 for (j = 0 ; j < LINEAR_PROBES ; j++) {
176 entry++;
177 if (entry->hash == 0 && entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700178 goto found_unused_or_dummy;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700179 if (entry->hash == hash) {
180 PyObject *startkey = entry->key;
181 assert(startkey != dummy);
182 if (startkey == key)
183 goto found_active;
184 if (PyUnicode_CheckExact(startkey)
185 && PyUnicode_CheckExact(key)
186 && unicode_eq(startkey, key))
187 goto found_active;
188 Py_INCREF(startkey);
189 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
190 Py_DECREF(startkey);
191 if (cmp < 0)
192 return -1;
193 if (table != so->table || entry->key != startkey)
194 return set_insert_key(so, key, hash);
195 if (cmp > 0)
196 goto found_active;
197 mask = so->mask;
198 }
Raymond Hettinger91672612015-06-26 02:50:21 -0700199 else if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800200 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700203
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700204 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800205 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700206
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800207 entry = &table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700208 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700209 goto found_unused_or_dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700211
Raymond Hettinger15f08692015-07-03 17:21:17 -0700212 found_unused_or_dummy:
213 if (freeslot == NULL)
214 goto found_unused;
215 Py_INCREF(key);
216 so->used++;
217 freeslot->key = key;
218 freeslot->hash = hash;
219 return 0;
220
221 found_unused:
Raymond Hettinger2eff9e92015-06-27 22:03:35 -0700222 Py_INCREF(key);
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700223 so->fill++;
224 so->used++;
225 entry->key = key;
226 entry->hash = hash;
Raymond Hettinger15f08692015-07-03 17:21:17 -0700227 if ((size_t)so->fill*3 < mask*2)
228 return 0;
Raymond Hettinger48973002015-07-03 20:00:03 -0700229 return set_table_resize(so, so->used);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700230
231 found_active:
Raymond Hettinger8651a502015-05-27 10:37:20 -0700232 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000233}
234
235/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000236Internal routine used by set_table_resize() to insert an item which is
237known to be absent from the set. This routine also assumes that
238the set contains no deleted entries. Besides the performance benefit,
239using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
240Note that no refcounts are changed by this routine; if needed, the caller
241is responsible for incref'ing `key`.
242*/
243static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200244set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200247 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700248 size_t perturb = hash;
249 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800250 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700251 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700253 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800254 entry = &table[i];
255 if (entry->key == NULL)
256 goto found_null;
257 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800258 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800259 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800260 if (entry->key == NULL)
261 goto found_null;
262 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700263 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700264 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800265 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700267 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 entry->key = key;
269 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700270 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000272}
273
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700274/* ======== End logic for probing the hash table ========================== */
275/* ======================================================================== */
276
Thomas Wouters89f507f2006-12-13 04:49:30 +0000277/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000279keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000280actually be smaller than the old one.
281*/
282static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 Py_ssize_t newsize;
286 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800287 Py_ssize_t oldfill = so->fill;
288 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 int is_oldtable_malloced;
290 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 assert(minused >= 0);
Raymond Hettinger48973002015-07-03 20:00:03 -0700293 minused = (minused > 50000) ? minused * 2 : minused * 4;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100296 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 for (newsize = PySet_MINSIZE;
298 newsize <= minused && newsize > 0;
299 newsize <<= 1)
300 ;
301 if (newsize <= 0) {
302 PyErr_NoMemory();
303 return -1;
304 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 /* Get space for a new table. */
307 oldtable = so->table;
308 assert(oldtable != NULL);
309 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (newsize == PySet_MINSIZE) {
312 /* A large table is shrinking, or we can't get any smaller. */
313 newtable = so->smalltable;
314 if (newtable == oldtable) {
315 if (so->fill == so->used) {
316 /* No dummies, so no point doing anything. */
317 return 0;
318 }
319 /* We're not going to resize it, but rebuild the
320 table anyway to purge old dummy entries.
321 Subtle: This is *necessary* if fill==size,
322 as set_lookkey needs at least one virgin slot to
323 terminate failing searches. If fill < size, it's
324 merely desirable, as dummies slow searches. */
325 assert(so->fill > so->used);
326 memcpy(small_copy, oldtable, sizeof(small_copy));
327 oldtable = small_copy;
328 }
329 }
330 else {
331 newtable = PyMem_NEW(setentry, newsize);
332 if (newtable == NULL) {
333 PyErr_NoMemory();
334 return -1;
335 }
336 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Make the set empty, using the new table. */
339 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800342 so->used = 0;
343 so->mask = newsize - 1;
344 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 /* Copy the data over; this is refcount-neutral for active entries;
347 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800348 if (oldfill == oldused) {
349 for (entry = oldtable; oldused > 0; entry++) {
350 if (entry->key != NULL) {
351 oldused--;
352 set_insert_clean(so, entry->key, entry->hash);
353 }
354 }
355 } else {
356 for (entry = oldtable; oldused > 0; entry++) {
357 if (entry->key != NULL && entry->key != dummy) {
358 oldused--;
359 set_insert_clean(so, entry->key, entry->hash);
360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 }
362 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (is_oldtable_malloced)
365 PyMem_DEL(oldtable);
366 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000367}
368
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000369static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200370set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000371{
Raymond Hettinger15f08692015-07-03 17:21:17 -0700372 return set_insert_key(so, entry->key, entry->hash);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000373}
374
375static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200376set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200378 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200381 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 hash = PyObject_Hash(key);
383 if (hash == -1)
384 return -1;
385 }
Raymond Hettinger15f08692015-07-03 17:21:17 -0700386 return set_insert_key(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000387}
388
389#define DISCARD_NOTFOUND 0
390#define DISCARD_FOUND 1
391
392static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000393set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800394{
395 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000397
Raymond Hettinger93035c42015-01-25 16:12:49 -0800398 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (entry == NULL)
400 return -1;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700401 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 return DISCARD_NOTFOUND;
403 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800405 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 so->used--;
407 Py_DECREF(old_key);
408 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000409}
410
411static int
412set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000413{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800414 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200415 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200420 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 hash = PyObject_Hash(key);
422 if (hash == -1)
423 return -1;
424 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800425 entry.key = key;
426 entry.hash = hash;
427 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000428}
429
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700430static void
431set_empty_to_minsize(PySetObject *so)
432{
433 memset(so->smalltable, 0, sizeof(so->smalltable));
434 so->fill = 0;
435 so->used = 0;
436 so->mask = PySet_MINSIZE - 1;
437 so->table = so->smalltable;
438 so->hash = -1;
439}
440
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000441static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000442set_clear_internal(PySetObject *so)
443{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800444 setentry *entry;
445 setentry *table = so->table;
446 Py_ssize_t fill = so->fill;
447 Py_ssize_t used = so->used;
448 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000450
Raymond Hettinger583cd032013-09-07 22:06:35 -0700451 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 /* This is delicate. During the process of clearing the set,
455 * decrefs can cause the set to mutate. To avoid fatal confusion
456 * (voice of experience), we have to make the set empty before
457 * clearing the slots, and never refer to anything via so->ref while
458 * clearing.
459 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700461 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 else if (fill > 0) {
464 /* It's a small table with something that needs to be cleared.
465 * Afraid the only safe way is to copy the set entries into
466 * another small table first.
467 */
468 memcpy(small_copy, table, sizeof(small_copy));
469 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700470 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 }
472 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 /* Now we can finally clear things. If C had refcounts, we could
475 * assert that the refcount on table is 1 now, i.e. that this function
476 * has unique access to it, so decref side-effects can't alter it.
477 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800478 for (entry = table; used > 0; entry++) {
479 if (entry->key && entry->key != dummy) {
480 used--;
481 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (table_is_malloced)
486 PyMem_DEL(table);
487 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000488}
489
490/*
491 * Iterate over a set table. Use like so:
492 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000493 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000494 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000495 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000496 * while (set_next(yourset, &pos, &entry)) {
497 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498 * }
499 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000500 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502 */
503static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000504set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 Py_ssize_t i;
507 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200508 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 assert (PyAnySet_Check(so));
511 i = *pos_ptr;
512 assert(i >= 0);
513 table = so->table;
514 mask = so->mask;
515 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
516 i++;
517 *pos_ptr = i+1;
518 if (i > mask)
519 return 0;
520 assert(table[i].key != NULL);
521 *entry_ptr = &table[i];
522 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523}
524
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000525static void
526set_dealloc(PySetObject *so)
527{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200528 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800529 Py_ssize_t used = so->used;
530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyObject_GC_UnTrack(so);
532 Py_TRASHCAN_SAFE_BEGIN(so)
533 if (so->weakreflist != NULL)
534 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000535
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800536 for (entry = so->table; used > 0; entry++) {
537 if (entry->key && entry->key != dummy) {
538 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700539 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 }
541 }
542 if (so->table != so->smalltable)
543 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700544 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000546}
547
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000548static PyObject *
549set_repr(PySetObject *so)
550{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200551 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (status != 0) {
555 if (status < 0)
556 return NULL;
557 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
558 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* shortcut for the empty set */
561 if (!so->used) {
562 Py_ReprLeave((PyObject*)so);
563 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
564 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 keys = PySequence_List((PyObject *)so);
567 if (keys == NULL)
568 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000569
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200570 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 listrepr = PyObject_Repr(keys);
572 Py_DECREF(keys);
573 if (listrepr == NULL)
574 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200575 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200577 if (tmp == NULL)
578 goto done;
579 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200580
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200581 if (Py_TYPE(so) != &PySet_Type)
582 result = PyUnicode_FromFormat("%s({%U})",
583 Py_TYPE(so)->tp_name,
584 listrepr);
585 else
586 result = PyUnicode_FromFormat("{%U}", listrepr);
587 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000588done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 Py_ReprLeave((PyObject*)so);
590 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591}
592
Martin v. Löwis18e16552006-02-15 17:27:45 +0000593static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000594set_len(PyObject *so)
595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000597}
598
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000599static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000600set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000603 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200604 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700605 setentry *so_entry;
606 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 assert (PyAnySet_Check(so));
609 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 other = (PySetObject*)otherset;
612 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700613 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 return 0;
615 /* Do one big resize at the start, rather than
616 * incrementally resizing as we insert new keys. Expect
617 * that there will be no (or few) overlapping keys.
618 */
Raymond Hettinger15f08692015-07-03 17:21:17 -0700619 if ((so->fill + other->used)*3 >= so->mask*2) {
Raymond Hettinger48973002015-07-03 20:00:03 -0700620 if (set_table_resize(so, so->used + other->used) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 return -1;
622 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700623 so_entry = so->table;
624 other_entry = other->table;
625
626 /* If our table is empty, and both tables have the same size, and
627 there are no dummies to eliminate, then just copy the pointers. */
628 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
629 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
630 key = other_entry->key;
631 if (key != NULL) {
632 assert(so_entry->key == NULL);
633 Py_INCREF(key);
634 so_entry->key = key;
635 so_entry->hash = other_entry->hash;
636 }
637 }
638 so->fill = other->fill;
639 so->used = other->used;
640 return 0;
641 }
642
643 /* If our table is empty, we can use set_insert_clean() */
644 if (so->fill == 0) {
645 for (i = 0; i <= other->mask; i++, other_entry++) {
646 key = other_entry->key;
647 if (key != NULL && key != dummy) {
648 Py_INCREF(key);
649 set_insert_clean(so, key, other_entry->hash);
650 }
651 }
652 return 0;
653 }
654
655 /* We can't assure there are no duplicates, so do normal insertions */
656 for (i = 0; i <= other->mask; i++, other_entry++) {
657 key = other_entry->key;
658 if (key != NULL && key != dummy) {
Raymond Hettinger2eff9e92015-06-27 22:03:35 -0700659 if (set_insert_key(so, key, other_entry->hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 }
662 }
663 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000664}
665
666static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000667set_contains_entry(PySetObject *so, setentry *entry)
668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PyObject *key;
670 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000671
Raymond Hettinger93035c42015-01-25 16:12:49 -0800672 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (lu_entry == NULL)
674 return -1;
675 key = lu_entry->key;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700676 return key != NULL;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000677}
678
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800679static int
680set_contains_key(PySetObject *so, PyObject *key)
681{
Raymond Hettinger3c1f52e2015-07-03 18:31:09 -0700682 setentry *entry;
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800683 Py_hash_t hash;
684
685 if (!PyUnicode_CheckExact(key) ||
686 (hash = ((PyASCIIObject *) key)->hash) == -1) {
687 hash = PyObject_Hash(key);
688 if (hash == -1)
689 return -1;
690 }
Raymond Hettinger3c1f52e2015-07-03 18:31:09 -0700691 entry = set_lookkey(so, key, hash);
692 if (entry == NULL)
693 return -1;
694 return entry->key != NULL;
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800695}
696
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000697static PyObject *
698set_pop(PySetObject *so)
699{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800700 /* Make sure the search finger is in bounds */
701 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200702 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 assert (PyAnySet_Check(so));
706 if (so->used == 0) {
707 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
708 return NULL;
709 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000710
Raymond Hettinger1202a472015-01-18 13:12:42 -0800711 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
712 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800713 if (i > so->mask)
714 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 }
716 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800718 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800720 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000722}
723
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000724PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
725Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000726
727static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000728set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 Py_ssize_t pos = 0;
731 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 while (set_next(so, &pos, &entry))
734 Py_VISIT(entry->key);
735 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000736}
737
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000738static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000739frozenset_hash(PyObject *self)
740{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800741 /* Most of the constants in this hash algorithm are randomly choosen
742 large primes with "interesting bit patterns" and that passed
743 tests for good collision statistics on a variety of problematic
744 datasets such as:
745
746 ps = []
747 for r in range(21):
748 ps += itertools.combinations(range(20), r)
749 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
750
751 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800753 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 setentry *entry;
755 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (so->hash != -1)
758 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000759
Mark Dickinson57e683e2011-09-24 18:18:40 +0100760 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 while (set_next(so, &pos, &entry)) {
762 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800763 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 combinations of a small number of elements with nearby
765 hashes so that many distinct combinations collapse to only
766 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000767 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800768 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800770 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500771 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800772 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200773 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800774 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 so->hash = hash;
776 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000777}
778
Raymond Hettingera9d99362005-08-05 00:01:15 +0000779/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000780
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000781typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 PyObject_HEAD
783 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
784 Py_ssize_t si_used;
785 Py_ssize_t si_pos;
786 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787} setiterobject;
788
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789static void
790setiter_dealloc(setiterobject *si)
791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 Py_XDECREF(si->si_set);
793 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000794}
795
796static int
797setiter_traverse(setiterobject *si, visitproc visit, void *arg)
798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 Py_VISIT(si->si_set);
800 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801}
802
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000803static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804setiter_len(setiterobject *si)
805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 Py_ssize_t len = 0;
807 if (si->si_set != NULL && si->si_used == si->si_set->used)
808 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000809 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810}
811
Armin Rigof5b3e362006-02-11 21:32:43 +0000812PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000813
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000814static PyObject *setiter_iternext(setiterobject *si);
815
816static PyObject *
817setiter_reduce(setiterobject *si)
818{
819 PyObject *list;
820 setiterobject tmp;
821
822 list = PyList_New(0);
823 if (!list)
824 return NULL;
825
Ezio Melotti0e1af282012-09-28 16:43:40 +0300826 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000827 tmp = *si;
828 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300829
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000830 /* iterate the temporary into a list */
831 for(;;) {
832 PyObject *element = setiter_iternext(&tmp);
833 if (element) {
834 if (PyList_Append(list, element)) {
835 Py_DECREF(element);
836 Py_DECREF(list);
837 Py_XDECREF(tmp.si_set);
838 return NULL;
839 }
840 Py_DECREF(element);
841 } else
842 break;
843 }
844 Py_XDECREF(tmp.si_set);
845 /* check for error */
846 if (tmp.si_set != NULL) {
847 /* we have an error */
848 Py_DECREF(list);
849 return NULL;
850 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200851 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000852}
853
854PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
855
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000856static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000858 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000860};
861
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000862static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200865 Py_ssize_t i, mask;
866 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (so == NULL)
870 return NULL;
871 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (si->si_used != so->used) {
874 PyErr_SetString(PyExc_RuntimeError,
875 "Set changed size during iteration");
876 si->si_used = -1; /* Make this state sticky */
877 return NULL;
878 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 i = si->si_pos;
881 assert(i>=0);
882 entry = so->table;
883 mask = so->mask;
884 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
885 i++;
886 si->si_pos = i+1;
887 if (i > mask)
888 goto fail;
889 si->len--;
890 key = entry[i].key;
891 Py_INCREF(key);
892 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000893
894fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 Py_DECREF(so);
896 si->si_set = NULL;
897 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000898}
899
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000900PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyVarObject_HEAD_INIT(&PyType_Type, 0)
902 "set_iterator", /* tp_name */
903 sizeof(setiterobject), /* tp_basicsize */
904 0, /* tp_itemsize */
905 /* methods */
906 (destructor)setiter_dealloc, /* tp_dealloc */
907 0, /* tp_print */
908 0, /* tp_getattr */
909 0, /* tp_setattr */
910 0, /* tp_reserved */
911 0, /* tp_repr */
912 0, /* tp_as_number */
913 0, /* tp_as_sequence */
914 0, /* tp_as_mapping */
915 0, /* tp_hash */
916 0, /* tp_call */
917 0, /* tp_str */
918 PyObject_GenericGetAttr, /* tp_getattro */
919 0, /* tp_setattro */
920 0, /* tp_as_buffer */
921 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
922 0, /* tp_doc */
923 (traverseproc)setiter_traverse, /* tp_traverse */
924 0, /* tp_clear */
925 0, /* tp_richcompare */
926 0, /* tp_weaklistoffset */
927 PyObject_SelfIter, /* tp_iter */
928 (iternextfunc)setiter_iternext, /* tp_iternext */
929 setiter_methods, /* tp_methods */
930 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000931};
932
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000933static PyObject *
934set_iter(PySetObject *so)
935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
937 if (si == NULL)
938 return NULL;
939 Py_INCREF(so);
940 si->si_set = so;
941 si->si_used = so->used;
942 si->si_pos = 0;
943 si->len = so->used;
944 _PyObject_GC_TRACK(si);
945 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000946}
947
Raymond Hettingerd7946662005-08-01 21:39:29 +0000948static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 if (PyAnySet_Check(other))
954 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (PyDict_CheckExact(other)) {
957 PyObject *value;
958 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000959 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 /* Do one big resize at the start, rather than
963 * incrementally resizing as we insert new keys. Expect
964 * that there will be no (or few) overlapping keys.
965 */
Raymond Hettingerc2480dc2015-07-04 08:46:31 -0700966 if (dictsize < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 return -1;
Raymond Hettinger15f08692015-07-03 17:21:17 -0700968 if ((so->fill + dictsize)*3 >= so->mask*2) {
Raymond Hettinger48973002015-07-03 20:00:03 -0700969 if (set_table_resize(so, so->used + dictsize) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 return -1;
971 }
972 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettinger15f08692015-07-03 17:21:17 -0700973 if (set_insert_key(so, key, hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 return -1;
975 }
976 return 0;
977 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 it = PyObject_GetIter(other);
980 if (it == NULL)
981 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700984 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 Py_DECREF(it);
986 Py_DECREF(key);
987 return -1;
988 }
989 Py_DECREF(key);
990 }
991 Py_DECREF(it);
992 if (PyErr_Occurred())
993 return -1;
994 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000995}
996
997static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000998set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1003 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001004 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 return NULL;
1006 }
1007 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001008}
1009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001011"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001012
Raymond Hettinger426d9952014-05-18 21:40:20 +01001013/* XXX Todo:
1014 If aligned memory allocations become available, make the
1015 set object 64 byte aligned so that most of the fields
1016 can be retrieved or updated in a single cache line.
1017*/
1018
Raymond Hettingera38123e2003-11-24 22:18:49 +00001019static PyObject *
1020make_new_set(PyTypeObject *type, PyObject *iterable)
1021{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001022 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001025 so = (PySetObject *)type->tp_alloc(type, 0);
1026 if (so == NULL)
1027 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001028
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001029 so->fill = 0;
1030 so->used = 0;
1031 so->mask = PySet_MINSIZE - 1;
1032 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001033 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -08001034 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001038 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 Py_DECREF(so);
1040 return NULL;
1041 }
1042 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001045}
1046
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001047static PyObject *
1048make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1051 if (PyType_IsSubtype(type, &PySet_Type))
1052 type = &PySet_Type;
1053 else
1054 type = &PyFrozenSet_Type;
1055 }
1056 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001057}
1058
Raymond Hettingerd7946662005-08-01 21:39:29 +00001059/* The empty frozenset is a singleton */
1060static PyObject *emptyfrozenset = NULL;
1061
Raymond Hettingera690a992003-11-16 16:17:49 +00001062static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001063frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1068 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1071 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (type != &PyFrozenSet_Type)
1074 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 if (iterable != NULL) {
1077 /* frozenset(f) is idempotent */
1078 if (PyFrozenSet_CheckExact(iterable)) {
1079 Py_INCREF(iterable);
1080 return iterable;
1081 }
1082 result = make_new_set(type, iterable);
1083 if (result == NULL || PySet_GET_SIZE(result))
1084 return result;
1085 Py_DECREF(result);
1086 }
1087 /* The empty frozenset is a singleton */
1088 if (emptyfrozenset == NULL)
1089 emptyfrozenset = make_new_set(type, NULL);
1090 Py_XINCREF(emptyfrozenset);
1091 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001092}
1093
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001094int
1095PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001096{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001097 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001098}
1099
1100void
1101PySet_Fini(void)
1102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001104}
1105
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001106static PyObject *
1107set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1110 return NULL;
1111
1112 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001113}
1114
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001115/* set_swap_bodies() switches the contents of any two sets by moving their
1116 internal data pointers and, if needed, copying the internal smalltables.
1117 Semantically equivalent to:
1118
1119 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1120
1121 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001122 Useful for operations that update in-place (by allowing an intermediate
1123 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001124*/
1125
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001126static void
1127set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 Py_ssize_t t;
1130 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001132 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 t = a->fill; a->fill = b->fill; b->fill = t;
1135 t = a->used; a->used = b->used; b->used = t;
1136 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 u = a->table;
1139 if (a->table == a->smalltable)
1140 u = b->smalltable;
1141 a->table = b->table;
1142 if (b->table == b->smalltable)
1143 a->table = a->smalltable;
1144 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (a->table == a->smalltable || b->table == b->smalltable) {
1147 memcpy(tab, a->smalltable, sizeof(tab));
1148 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1149 memcpy(b->smalltable, tab, sizeof(tab));
1150 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1153 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1154 h = a->hash; a->hash = b->hash; b->hash = h;
1155 } else {
1156 a->hash = -1;
1157 b->hash = -1;
1158 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001159}
1160
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001161static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001162set_copy(PySetObject *so)
1163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001165}
1166
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001167static PyObject *
1168frozenset_copy(PySetObject *so)
1169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (PyFrozenSet_CheckExact(so)) {
1171 Py_INCREF(so);
1172 return (PyObject *)so;
1173 }
1174 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001175}
1176
Raymond Hettingera690a992003-11-16 16:17:49 +00001177PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1178
1179static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001180set_clear(PySetObject *so)
1181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 set_clear_internal(so);
1183 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001184}
1185
1186PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1187
1188static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001189set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 PySetObject *result;
1192 PyObject *other;
1193 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 result = (PySetObject *)set_copy(so);
1196 if (result == NULL)
1197 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1200 other = PyTuple_GET_ITEM(args, i);
1201 if ((PyObject *)so == other)
1202 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001203 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 Py_DECREF(result);
1205 return NULL;
1206 }
1207 }
1208 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001209}
1210
1211PyDoc_STRVAR(union_doc,
1212 "Return the union of sets as a new set.\n\
1213\n\
1214(i.e. all elements that are in either set.)");
1215
1216static PyObject *
1217set_or(PySetObject *so, PyObject *other)
1218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001220
Brian Curtindfc80e32011-08-10 20:28:54 -05001221 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1222 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 result = (PySetObject *)set_copy(so);
1225 if (result == NULL)
1226 return NULL;
1227 if ((PyObject *)so == other)
1228 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001229 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 Py_DECREF(result);
1231 return NULL;
1232 }
1233 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001234}
1235
Raymond Hettingera690a992003-11-16 16:17:49 +00001236static PyObject *
1237set_ior(PySetObject *so, PyObject *other)
1238{
Brian Curtindfc80e32011-08-10 20:28:54 -05001239 if (!PyAnySet_Check(other))
1240 Py_RETURN_NOTIMPLEMENTED;
1241
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001242 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 return NULL;
1244 Py_INCREF(so);
1245 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001246}
1247
1248static PyObject *
1249set_intersection(PySetObject *so, PyObject *other)
1250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 PySetObject *result;
1252 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 if ((PyObject *)so == other)
1255 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1258 if (result == NULL)
1259 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (PyAnySet_Check(other)) {
1262 Py_ssize_t pos = 0;
1263 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1266 tmp = (PyObject *)so;
1267 so = (PySetObject *)other;
1268 other = tmp;
1269 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 while (set_next((PySetObject *)other, &pos, &entry)) {
1272 int rv = set_contains_entry(so, entry);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001273 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 Py_DECREF(result);
1275 return NULL;
1276 }
1277 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001278 if (set_add_entry(result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 Py_DECREF(result);
1280 return NULL;
1281 }
1282 }
1283 }
1284 return (PyObject *)result;
1285 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 it = PyObject_GetIter(other);
1288 if (it == NULL) {
1289 Py_DECREF(result);
1290 return NULL;
1291 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 while ((key = PyIter_Next(it)) != NULL) {
1294 int rv;
1295 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001296 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 if (hash == -1) {
1299 Py_DECREF(it);
1300 Py_DECREF(result);
1301 Py_DECREF(key);
1302 return NULL;
1303 }
1304 entry.hash = hash;
1305 entry.key = key;
1306 rv = set_contains_entry(so, &entry);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001307 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 Py_DECREF(it);
1309 Py_DECREF(result);
1310 Py_DECREF(key);
1311 return NULL;
1312 }
1313 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001314 if (set_add_entry(result, &entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 Py_DECREF(it);
1316 Py_DECREF(result);
1317 Py_DECREF(key);
1318 return NULL;
1319 }
1320 }
1321 Py_DECREF(key);
1322 }
1323 Py_DECREF(it);
1324 if (PyErr_Occurred()) {
1325 Py_DECREF(result);
1326 return NULL;
1327 }
1328 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001329}
1330
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001331static PyObject *
1332set_intersection_multi(PySetObject *so, PyObject *args)
1333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 Py_ssize_t i;
1335 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 if (PyTuple_GET_SIZE(args) == 0)
1338 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 Py_INCREF(so);
1341 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1342 PyObject *other = PyTuple_GET_ITEM(args, i);
1343 PyObject *newresult = set_intersection((PySetObject *)result, other);
1344 if (newresult == NULL) {
1345 Py_DECREF(result);
1346 return NULL;
1347 }
1348 Py_DECREF(result);
1349 result = newresult;
1350 }
1351 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001352}
1353
Raymond Hettingera690a992003-11-16 16:17:49 +00001354PyDoc_STRVAR(intersection_doc,
1355"Return the intersection of two sets as a new set.\n\
1356\n\
1357(i.e. all elements that are in both sets.)");
1358
1359static PyObject *
1360set_intersection_update(PySetObject *so, PyObject *other)
1361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 tmp = set_intersection(so, other);
1365 if (tmp == NULL)
1366 return NULL;
1367 set_swap_bodies(so, (PySetObject *)tmp);
1368 Py_DECREF(tmp);
1369 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001370}
1371
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001372static PyObject *
1373set_intersection_update_multi(PySetObject *so, PyObject *args)
1374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 tmp = set_intersection_multi(so, args);
1378 if (tmp == NULL)
1379 return NULL;
1380 set_swap_bodies(so, (PySetObject *)tmp);
1381 Py_DECREF(tmp);
1382 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001383}
1384
Raymond Hettingera690a992003-11-16 16:17:49 +00001385PyDoc_STRVAR(intersection_update_doc,
1386"Update a set with the intersection of itself and another.");
1387
1388static PyObject *
1389set_and(PySetObject *so, PyObject *other)
1390{
Brian Curtindfc80e32011-08-10 20:28:54 -05001391 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1392 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001394}
1395
1396static PyObject *
1397set_iand(PySetObject *so, PyObject *other)
1398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001400
Brian Curtindfc80e32011-08-10 20:28:54 -05001401 if (!PyAnySet_Check(other))
1402 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 result = set_intersection_update(so, other);
1404 if (result == NULL)
1405 return NULL;
1406 Py_DECREF(result);
1407 Py_INCREF(so);
1408 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001409}
1410
Guido van Rossum58da9312007-11-10 23:39:45 +00001411static PyObject *
1412set_isdisjoint(PySetObject *so, PyObject *other)
1413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if ((PyObject *)so == other) {
1417 if (PySet_GET_SIZE(so) == 0)
1418 Py_RETURN_TRUE;
1419 else
1420 Py_RETURN_FALSE;
1421 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (PyAnySet_CheckExact(other)) {
1424 Py_ssize_t pos = 0;
1425 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1428 tmp = (PyObject *)so;
1429 so = (PySetObject *)other;
1430 other = tmp;
1431 }
1432 while (set_next((PySetObject *)other, &pos, &entry)) {
1433 int rv = set_contains_entry(so, entry);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001434 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 return NULL;
1436 if (rv)
1437 Py_RETURN_FALSE;
1438 }
1439 Py_RETURN_TRUE;
1440 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 it = PyObject_GetIter(other);
1443 if (it == NULL)
1444 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 while ((key = PyIter_Next(it)) != NULL) {
1447 int rv;
1448 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001449 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 if (hash == -1) {
1452 Py_DECREF(key);
1453 Py_DECREF(it);
1454 return NULL;
1455 }
1456 entry.hash = hash;
1457 entry.key = key;
1458 rv = set_contains_entry(so, &entry);
1459 Py_DECREF(key);
Raymond Hettingerc2480dc2015-07-04 08:46:31 -07001460 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 Py_DECREF(it);
1462 return NULL;
1463 }
1464 if (rv) {
1465 Py_DECREF(it);
1466 Py_RETURN_FALSE;
1467 }
1468 }
1469 Py_DECREF(it);
1470 if (PyErr_Occurred())
1471 return NULL;
1472 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001473}
1474
1475PyDoc_STRVAR(isdisjoint_doc,
1476"Return True if two sets have a null intersection.");
1477
Neal Norwitz6576bd82005-11-13 18:41:28 +00001478static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001479set_difference_update_internal(PySetObject *so, PyObject *other)
1480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 if ((PyObject *)so == other)
1482 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 if (PyAnySet_Check(other)) {
1485 setentry *entry;
1486 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettingerb3223262015-07-03 23:37:16 -07001489 if (set_discard_entry(so, entry) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 return -1;
1491 } else {
1492 PyObject *key, *it;
1493 it = PyObject_GetIter(other);
1494 if (it == NULL)
1495 return -1;
1496
1497 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerb3223262015-07-03 23:37:16 -07001498 if (set_discard_key(so, key) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 Py_DECREF(it);
1500 Py_DECREF(key);
1501 return -1;
1502 }
1503 Py_DECREF(key);
1504 }
1505 Py_DECREF(it);
1506 if (PyErr_Occurred())
1507 return -1;
1508 }
Raymond Hettingere186c762015-07-04 11:28:35 -07001509 /* If more than 1/4th are dummies, then resize them away. */
1510 if ((size_t)(so->fill - so->used) <= (size_t)so->mask / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 return 0;
Raymond Hettinger48973002015-07-03 20:00:03 -07001512 return set_table_resize(so, so->used);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001513}
1514
Raymond Hettingera690a992003-11-16 16:17:49 +00001515static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001516set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1521 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001522 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 return NULL;
1524 }
1525 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001526}
1527
1528PyDoc_STRVAR(difference_update_doc,
1529"Remove all elements of another set from this set.");
1530
1531static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001532set_copy_and_difference(PySetObject *so, PyObject *other)
1533{
1534 PyObject *result;
1535
1536 result = set_copy(so);
1537 if (result == NULL)
1538 return NULL;
Raymond Hettingerb3223262015-07-03 23:37:16 -07001539 if (set_difference_update_internal((PySetObject *) result, other) == 0)
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001540 return result;
1541 Py_DECREF(result);
1542 return NULL;
1543}
1544
1545static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001546set_difference(PySetObject *so, PyObject *other)
1547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 PyObject *result;
1549 setentry *entry;
1550 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001553 return set_copy_and_difference(so, other);
1554 }
1555
1556 /* If len(so) much more than len(other), it's more efficient to simply copy
1557 * so and then iterate other looking for common elements. */
1558 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1559 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 result = make_new_set_basetype(Py_TYPE(so), NULL);
1563 if (result == NULL)
1564 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 if (PyDict_CheckExact(other)) {
1567 while (set_next(so, &pos, &entry)) {
Raymond Hettinger15f08692015-07-03 17:21:17 -07001568 PyObject *key = entry->key;
1569 Py_hash_t hash = entry->hash;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001570 int rv;
Raymond Hettinger15f08692015-07-03 17:21:17 -07001571 rv = _PyDict_Contains(other, key, hash);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001572 if (rv < 0) {
1573 Py_DECREF(result);
1574 return NULL;
1575 }
1576 if (!rv) {
Raymond Hettinger15f08692015-07-03 17:21:17 -07001577 if (set_insert_key((PySetObject *)result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 Py_DECREF(result);
1579 return NULL;
1580 }
1581 }
1582 }
1583 return result;
1584 }
1585
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001586 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 while (set_next(so, &pos, &entry)) {
1588 int rv = set_contains_entry((PySetObject *)other, entry);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001589 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 Py_DECREF(result);
1591 return NULL;
1592 }
1593 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001594 if (set_add_entry((PySetObject *)result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 Py_DECREF(result);
1596 return NULL;
1597 }
1598 }
1599 }
1600 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001601}
1602
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001603static PyObject *
1604set_difference_multi(PySetObject *so, PyObject *args)
1605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 Py_ssize_t i;
1607 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 if (PyTuple_GET_SIZE(args) == 0)
1610 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 other = PyTuple_GET_ITEM(args, 0);
1613 result = set_difference(so, other);
1614 if (result == NULL)
1615 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1618 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001619 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 Py_DECREF(result);
1621 return NULL;
1622 }
1623 }
1624 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001625}
1626
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001627PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001628"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001629\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001630(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001631static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001632set_sub(PySetObject *so, PyObject *other)
1633{
Brian Curtindfc80e32011-08-10 20:28:54 -05001634 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1635 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001637}
1638
1639static PyObject *
1640set_isub(PySetObject *so, PyObject *other)
1641{
Brian Curtindfc80e32011-08-10 20:28:54 -05001642 if (!PyAnySet_Check(other))
1643 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001644 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 return NULL;
1646 Py_INCREF(so);
1647 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001648}
1649
1650static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001651set_symmetric_difference_update(PySetObject *so, PyObject *other)
1652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 PySetObject *otherset;
1654 PyObject *key;
1655 Py_ssize_t pos = 0;
1656 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if ((PyObject *)so == other)
1659 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (PyDict_CheckExact(other)) {
1662 PyObject *value;
1663 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001664 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1666 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001667
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001668 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 an_entry.hash = hash;
1670 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 rv = set_discard_entry(so, &an_entry);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001673 if (rv < 0) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001674 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001677 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001678 if (set_add_entry(so, &an_entry)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001679 Py_DECREF(key);
1680 return NULL;
1681 }
1682 }
Georg Brandl2d444492010-09-03 10:52:55 +00001683 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 }
1685 Py_RETURN_NONE;
1686 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if (PyAnySet_Check(other)) {
1689 Py_INCREF(other);
1690 otherset = (PySetObject *)other;
1691 } else {
1692 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1693 if (otherset == NULL)
1694 return NULL;
1695 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 while (set_next(otherset, &pos, &entry)) {
1698 int rv = set_discard_entry(so, entry);
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 Hettinger5af9e132015-05-13 01:44:36 -07001704 if (set_add_entry(so, entry)) {
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 Hettingera690a992003-11-16 16:17:49 +00001766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 if (!PyAnySet_Check(other)) {
1768 PyObject *tmp, *result;
1769 tmp = make_new_set(&PySet_Type, other);
1770 if (tmp == NULL)
1771 return NULL;
1772 result = set_issubset(so, tmp);
1773 Py_DECREF(tmp);
1774 return result;
1775 }
1776 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1777 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 while (set_next(so, &pos, &entry)) {
1780 int rv = set_contains_entry((PySetObject *)other, entry);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001781 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 return NULL;
1783 if (!rv)
1784 Py_RETURN_FALSE;
1785 }
1786 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001787}
1788
1789PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1790
1791static PyObject *
1792set_issuperset(PySetObject *so, PyObject *other)
1793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 if (!PyAnySet_Check(other)) {
1797 tmp = make_new_set(&PySet_Type, other);
1798 if (tmp == NULL)
1799 return NULL;
1800 result = set_issuperset(so, tmp);
1801 Py_DECREF(tmp);
1802 return result;
1803 }
1804 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001805}
1806
1807PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1808
Raymond Hettingera690a992003-11-16 16:17:49 +00001809static PyObject *
1810set_richcompare(PySetObject *v, PyObject *w, int op)
1811{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001812 PyObject *r1;
1813 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001814
Brian Curtindfc80e32011-08-10 20:28:54 -05001815 if(!PyAnySet_Check(w))
1816 Py_RETURN_NOTIMPLEMENTED;
1817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 switch (op) {
1819 case Py_EQ:
1820 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1821 Py_RETURN_FALSE;
1822 if (v->hash != -1 &&
1823 ((PySetObject *)w)->hash != -1 &&
1824 v->hash != ((PySetObject *)w)->hash)
1825 Py_RETURN_FALSE;
1826 return set_issubset(v, w);
1827 case Py_NE:
1828 r1 = set_richcompare(v, w, Py_EQ);
1829 if (r1 == NULL)
1830 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001831 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001833 if (r2 < 0)
1834 return NULL;
1835 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 case Py_LE:
1837 return set_issubset(v, w);
1838 case Py_GE:
1839 return set_issuperset(v, w);
1840 case Py_LT:
1841 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1842 Py_RETURN_FALSE;
1843 return set_issubset(v, w);
1844 case Py_GT:
1845 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1846 Py_RETURN_FALSE;
1847 return set_issuperset(v, w);
1848 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001849 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001850}
1851
Raymond Hettingera690a992003-11-16 16:17:49 +00001852static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001853set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001854{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001855 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 return NULL;
1857 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001858}
1859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001861"Add an element to a set.\n\
1862\n\
1863This has no effect if the element is already present.");
1864
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001865static int
1866set_contains(PySetObject *so, PyObject *key)
1867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 PyObject *tmpkey;
1869 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 rv = set_contains_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001872 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1874 return -1;
1875 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001876 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 if (tmpkey == NULL)
1878 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001879 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 Py_DECREF(tmpkey);
1881 }
1882 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001883}
1884
1885static PyObject *
1886set_direct_contains(PySetObject *so, PyObject *key)
1887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 result = set_contains(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001891 if (result < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 return NULL;
1893 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001894}
1895
1896PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1897
Raymond Hettingera690a992003-11-16 16:17:49 +00001898static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001899set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 PyObject *tmpkey;
1902 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 rv = set_discard_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001905 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1907 return NULL;
1908 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001909 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 if (tmpkey == NULL)
1911 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 Py_DECREF(tmpkey);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001914 if (rv < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 return NULL;
1916 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001919 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 return NULL;
1921 }
1922 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001923}
1924
1925PyDoc_STRVAR(remove_doc,
1926"Remove an element from a set; it must be a member.\n\
1927\n\
1928If the element is not a member, raise a KeyError.");
1929
1930static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001931set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001932{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001933 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 rv = set_discard_key(so, key);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001937 if (rv < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1939 return NULL;
1940 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001941 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 if (tmpkey == NULL)
1943 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001944 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 Py_DECREF(tmpkey);
Raymond Hettingerb3223262015-07-03 23:37:16 -07001946 if (rv < 0)
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001947 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 }
1949 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001950}
1951
1952PyDoc_STRVAR(discard_doc,
1953"Remove an element from a set if it is a member.\n\
1954\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001956
1957static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001958set_reduce(PySetObject *so)
1959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001961 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 keys = PySequence_List((PyObject *)so);
1964 if (keys == NULL)
1965 goto done;
1966 args = PyTuple_Pack(1, keys);
1967 if (args == NULL)
1968 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001969 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 if (dict == NULL) {
1971 PyErr_Clear();
1972 dict = Py_None;
1973 Py_INCREF(dict);
1974 }
1975 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001976done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 Py_XDECREF(args);
1978 Py_XDECREF(keys);
1979 Py_XDECREF(dict);
1980 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001981}
1982
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001983static PyObject *
1984set_sizeof(PySetObject *so)
1985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 res = sizeof(PySetObject);
1989 if (so->table != so->smalltable)
1990 res = res + (so->mask + 1) * sizeof(setentry);
1991 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001992}
1993
1994PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001995static int
1996set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 if (!PyAnySet_Check(self))
2001 return -1;
2002 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2003 return -1;
2004 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2005 return -1;
2006 set_clear_internal(self);
2007 self->hash = -1;
2008 if (iterable == NULL)
2009 return 0;
2010 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002011}
2012
Raymond Hettingera690a992003-11-16 16:17:49 +00002013static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 set_len, /* sq_length */
2015 0, /* sq_concat */
2016 0, /* sq_repeat */
2017 0, /* sq_item */
2018 0, /* sq_slice */
2019 0, /* sq_ass_item */
2020 0, /* sq_ass_slice */
2021 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002022};
2023
2024/* set object ********************************************************/
2025
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002026#ifdef Py_DEBUG
2027static PyObject *test_c_api(PySetObject *so);
2028
2029PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2030All is well if assertions don't fail.");
2031#endif
2032
Raymond Hettingera690a992003-11-16 16:17:49 +00002033static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 {"add", (PyCFunction)set_add, METH_O,
2035 add_doc},
2036 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2037 clear_doc},
2038 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2039 contains_doc},
2040 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2041 copy_doc},
2042 {"discard", (PyCFunction)set_discard, METH_O,
2043 discard_doc},
2044 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2045 difference_doc},
2046 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2047 difference_update_doc},
2048 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2049 intersection_doc},
2050 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2051 intersection_update_doc},
2052 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2053 isdisjoint_doc},
2054 {"issubset", (PyCFunction)set_issubset, METH_O,
2055 issubset_doc},
2056 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2057 issuperset_doc},
2058 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2059 pop_doc},
2060 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2061 reduce_doc},
2062 {"remove", (PyCFunction)set_remove, METH_O,
2063 remove_doc},
2064 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2065 sizeof_doc},
2066 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2067 symmetric_difference_doc},
2068 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2069 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002070#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2072 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002073#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 {"union", (PyCFunction)set_union, METH_VARARGS,
2075 union_doc},
2076 {"update", (PyCFunction)set_update, METH_VARARGS,
2077 update_doc},
2078 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002079};
2080
2081static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 0, /*nb_add*/
2083 (binaryfunc)set_sub, /*nb_subtract*/
2084 0, /*nb_multiply*/
2085 0, /*nb_remainder*/
2086 0, /*nb_divmod*/
2087 0, /*nb_power*/
2088 0, /*nb_negative*/
2089 0, /*nb_positive*/
2090 0, /*nb_absolute*/
2091 0, /*nb_bool*/
2092 0, /*nb_invert*/
2093 0, /*nb_lshift*/
2094 0, /*nb_rshift*/
2095 (binaryfunc)set_and, /*nb_and*/
2096 (binaryfunc)set_xor, /*nb_xor*/
2097 (binaryfunc)set_or, /*nb_or*/
2098 0, /*nb_int*/
2099 0, /*nb_reserved*/
2100 0, /*nb_float*/
2101 0, /*nb_inplace_add*/
2102 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2103 0, /*nb_inplace_multiply*/
2104 0, /*nb_inplace_remainder*/
2105 0, /*nb_inplace_power*/
2106 0, /*nb_inplace_lshift*/
2107 0, /*nb_inplace_rshift*/
2108 (binaryfunc)set_iand, /*nb_inplace_and*/
2109 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2110 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002111};
2112
2113PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002114"set() -> new empty set object\n\
2115set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002116\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002117Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002118
2119PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2121 "set", /* tp_name */
2122 sizeof(PySetObject), /* tp_basicsize */
2123 0, /* tp_itemsize */
2124 /* methods */
2125 (destructor)set_dealloc, /* tp_dealloc */
2126 0, /* tp_print */
2127 0, /* tp_getattr */
2128 0, /* tp_setattr */
2129 0, /* tp_reserved */
2130 (reprfunc)set_repr, /* tp_repr */
2131 &set_as_number, /* tp_as_number */
2132 &set_as_sequence, /* tp_as_sequence */
2133 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002134 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 0, /* tp_call */
2136 0, /* tp_str */
2137 PyObject_GenericGetAttr, /* tp_getattro */
2138 0, /* tp_setattro */
2139 0, /* tp_as_buffer */
2140 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2141 Py_TPFLAGS_BASETYPE, /* tp_flags */
2142 set_doc, /* tp_doc */
2143 (traverseproc)set_traverse, /* tp_traverse */
2144 (inquiry)set_clear_internal, /* tp_clear */
2145 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002146 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2147 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 0, /* tp_iternext */
2149 set_methods, /* tp_methods */
2150 0, /* tp_members */
2151 0, /* tp_getset */
2152 0, /* tp_base */
2153 0, /* tp_dict */
2154 0, /* tp_descr_get */
2155 0, /* tp_descr_set */
2156 0, /* tp_dictoffset */
2157 (initproc)set_init, /* tp_init */
2158 PyType_GenericAlloc, /* tp_alloc */
2159 set_new, /* tp_new */
2160 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002161};
2162
2163/* frozenset object ********************************************************/
2164
2165
2166static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2168 contains_doc},
2169 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2170 copy_doc},
2171 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2172 difference_doc},
2173 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2174 intersection_doc},
2175 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2176 isdisjoint_doc},
2177 {"issubset", (PyCFunction)set_issubset, METH_O,
2178 issubset_doc},
2179 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2180 issuperset_doc},
2181 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2182 reduce_doc},
2183 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2184 sizeof_doc},
2185 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2186 symmetric_difference_doc},
2187 {"union", (PyCFunction)set_union, METH_VARARGS,
2188 union_doc},
2189 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002190};
2191
2192static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 0, /*nb_add*/
2194 (binaryfunc)set_sub, /*nb_subtract*/
2195 0, /*nb_multiply*/
2196 0, /*nb_remainder*/
2197 0, /*nb_divmod*/
2198 0, /*nb_power*/
2199 0, /*nb_negative*/
2200 0, /*nb_positive*/
2201 0, /*nb_absolute*/
2202 0, /*nb_bool*/
2203 0, /*nb_invert*/
2204 0, /*nb_lshift*/
2205 0, /*nb_rshift*/
2206 (binaryfunc)set_and, /*nb_and*/
2207 (binaryfunc)set_xor, /*nb_xor*/
2208 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002209};
2210
2211PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002212"frozenset() -> empty frozenset object\n\
2213frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002214\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002215Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002216
2217PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2219 "frozenset", /* tp_name */
2220 sizeof(PySetObject), /* tp_basicsize */
2221 0, /* tp_itemsize */
2222 /* methods */
2223 (destructor)set_dealloc, /* tp_dealloc */
2224 0, /* tp_print */
2225 0, /* tp_getattr */
2226 0, /* tp_setattr */
2227 0, /* tp_reserved */
2228 (reprfunc)set_repr, /* tp_repr */
2229 &frozenset_as_number, /* tp_as_number */
2230 &set_as_sequence, /* tp_as_sequence */
2231 0, /* tp_as_mapping */
2232 frozenset_hash, /* tp_hash */
2233 0, /* tp_call */
2234 0, /* tp_str */
2235 PyObject_GenericGetAttr, /* tp_getattro */
2236 0, /* tp_setattro */
2237 0, /* tp_as_buffer */
2238 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2239 Py_TPFLAGS_BASETYPE, /* tp_flags */
2240 frozenset_doc, /* tp_doc */
2241 (traverseproc)set_traverse, /* tp_traverse */
2242 (inquiry)set_clear_internal, /* tp_clear */
2243 (richcmpfunc)set_richcompare, /* tp_richcompare */
2244 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2245 (getiterfunc)set_iter, /* tp_iter */
2246 0, /* tp_iternext */
2247 frozenset_methods, /* tp_methods */
2248 0, /* tp_members */
2249 0, /* tp_getset */
2250 0, /* tp_base */
2251 0, /* tp_dict */
2252 0, /* tp_descr_get */
2253 0, /* tp_descr_set */
2254 0, /* tp_dictoffset */
2255 0, /* tp_init */
2256 PyType_GenericAlloc, /* tp_alloc */
2257 frozenset_new, /* tp_new */
2258 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002259};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002260
2261
2262/***** C API functions *************************************************/
2263
2264PyObject *
2265PySet_New(PyObject *iterable)
2266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002268}
2269
2270PyObject *
2271PyFrozenSet_New(PyObject *iterable)
2272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002274}
2275
Neal Norwitz8c49c822006-03-04 18:41:19 +00002276Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002277PySet_Size(PyObject *anyset)
2278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 if (!PyAnySet_Check(anyset)) {
2280 PyErr_BadInternalCall();
2281 return -1;
2282 }
2283 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002284}
2285
2286int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002287PySet_Clear(PyObject *set)
2288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 if (!PySet_Check(set)) {
2290 PyErr_BadInternalCall();
2291 return -1;
2292 }
2293 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002294}
2295
2296int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002297PySet_Contains(PyObject *anyset, PyObject *key)
2298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 if (!PyAnySet_Check(anyset)) {
2300 PyErr_BadInternalCall();
2301 return -1;
2302 }
2303 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002304}
2305
2306int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002307PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 if (!PySet_Check(set)) {
2310 PyErr_BadInternalCall();
2311 return -1;
2312 }
2313 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002314}
2315
2316int
Christian Heimesfd66e512008-01-29 12:18:50 +00002317PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 if (!PySet_Check(anyset) &&
2320 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2321 PyErr_BadInternalCall();
2322 return -1;
2323 }
2324 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002325}
2326
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002327int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002328_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 if (!PyAnySet_Check(set)) {
2333 PyErr_BadInternalCall();
2334 return -1;
2335 }
2336 if (set_next((PySetObject *)set, pos, &entry) == 0)
2337 return 0;
2338 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002339 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002341}
2342
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002343PyObject *
2344PySet_Pop(PyObject *set)
2345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 if (!PySet_Check(set)) {
2347 PyErr_BadInternalCall();
2348 return NULL;
2349 }
2350 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002351}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002352
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002353int
2354_PySet_Update(PyObject *set, PyObject *iterable)
2355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if (!PySet_Check(set)) {
2357 PyErr_BadInternalCall();
2358 return -1;
2359 }
2360 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002361}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002362
Raymond Hettingere259f132013-12-15 11:56:14 -08002363/* Exported for the gdb plugin's benefit. */
2364PyObject *_PySet_Dummy = dummy;
2365
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002366#ifdef Py_DEBUG
2367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002369 Returns True and original set is restored. */
2370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371#define assertRaises(call_return_value, exception) \
2372 do { \
2373 assert(call_return_value); \
2374 assert(PyErr_ExceptionMatches(exception)); \
2375 PyErr_Clear(); \
2376 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002377
2378static PyObject *
2379test_c_api(PySetObject *so)
2380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 Py_ssize_t count;
2382 char *s;
2383 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002384 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002386 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 /* Verify preconditions */
2390 assert(PyAnySet_Check(ob));
2391 assert(PyAnySet_CheckExact(ob));
2392 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 /* so.clear(); so |= set("abc"); */
2395 str = PyUnicode_FromString("abc");
2396 if (str == NULL)
2397 return NULL;
2398 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002399 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 Py_DECREF(str);
2401 return NULL;
2402 }
2403 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* Exercise type/size checks */
2406 assert(PySet_Size(ob) == 3);
2407 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* Raise TypeError for non-iterable constructor arguments */
2410 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2411 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Raise TypeError for unhashable key */
2414 dup = PySet_New(ob);
2415 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2416 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2417 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* Exercise successful pop, contains, add, and discard */
2420 elem = PySet_Pop(ob);
2421 assert(PySet_Contains(ob, elem) == 0);
2422 assert(PySet_GET_SIZE(ob) == 2);
2423 assert(PySet_Add(ob, elem) == 0);
2424 assert(PySet_Contains(ob, elem) == 1);
2425 assert(PySet_GET_SIZE(ob) == 3);
2426 assert(PySet_Discard(ob, elem) == 1);
2427 assert(PySet_GET_SIZE(ob) == 2);
2428 assert(PySet_Discard(ob, elem) == 0);
2429 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 /* Exercise clear */
2432 dup2 = PySet_New(dup);
2433 assert(PySet_Clear(dup2) == 0);
2434 assert(PySet_Size(dup2) == 0);
2435 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 /* Raise SystemError on clear or update of frozen set */
2438 f = PyFrozenSet_New(dup);
2439 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2440 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2441 assert(PySet_Add(f, elem) == 0);
2442 Py_INCREF(f);
2443 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2444 Py_DECREF(f);
2445 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 /* Exercise direct iteration */
2448 i = 0, count = 0;
2449 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2450 s = _PyUnicode_AsString(x);
2451 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2452 count++;
2453 }
2454 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 /* Exercise updates */
2457 dup2 = PySet_New(NULL);
2458 assert(_PySet_Update(dup2, dup) == 0);
2459 assert(PySet_Size(dup2) == 3);
2460 assert(_PySet_Update(dup2, dup) == 0);
2461 assert(PySet_Size(dup2) == 3);
2462 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 /* Raise SystemError when self argument is not a set or frozenset. */
2465 t = PyTuple_New(0);
2466 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2467 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2468 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 /* Raise SystemError when self argument is not a set. */
2471 f = PyFrozenSet_New(dup);
2472 assert(PySet_Size(f) == 3);
2473 assert(PyFrozenSet_CheckExact(f));
2474 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2475 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2476 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 /* Raise KeyError when popping from an empty set */
2479 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2480 Py_DECREF(ob);
2481 assert(PySet_GET_SIZE(ob) == 0);
2482 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 /* Restore the set from the copy using the PyNumber API */
2485 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2486 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 /* Verify constructors accept NULL arguments */
2489 f = PySet_New(NULL);
2490 assert(f != NULL);
2491 assert(PySet_GET_SIZE(f) == 0);
2492 Py_DECREF(f);
2493 f = PyFrozenSet_New(NULL);
2494 assert(f != NULL);
2495 assert(PyFrozenSet_CheckExact(f));
2496 assert(PySet_GET_SIZE(f) == 0);
2497 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 Py_DECREF(elem);
2500 Py_DECREF(dup);
2501 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002502}
2503
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002504#undef assertRaises
2505
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002506#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002507
2508/***** Dummy Struct *************************************************/
2509
2510static PyObject *
2511dummy_repr(PyObject *op)
2512{
2513 return PyUnicode_FromString("<dummy key>");
2514}
2515
2516static void
2517dummy_dealloc(PyObject* ignore)
2518{
2519 Py_FatalError("deallocating <dummy key>");
2520}
2521
2522static PyTypeObject _PySetDummy_Type = {
2523 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2524 "<dummy key> type",
2525 0,
2526 0,
2527 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2528 0, /*tp_print*/
2529 0, /*tp_getattr*/
2530 0, /*tp_setattr*/
2531 0, /*tp_reserved*/
2532 dummy_repr, /*tp_repr*/
2533 0, /*tp_as_number*/
2534 0, /*tp_as_sequence*/
2535 0, /*tp_as_mapping*/
2536 0, /*tp_hash */
2537 0, /*tp_call */
2538 0, /*tp_str */
2539 0, /*tp_getattro */
2540 0, /*tp_setattro */
2541 0, /*tp_as_buffer */
2542 Py_TPFLAGS_DEFAULT, /*tp_flags */
2543};
2544
2545static PyObject _dummy_struct = {
2546 _PyObject_EXTRA_INIT
2547 2, &_PySetDummy_Type
2548};
2549