blob: 5b430b380e530fc498d32d0882ebb1efdbada1bc [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
128/*
129Internal routine to insert a new key into the table.
130Used by the public insert routine.
131Eats a reference to key.
132*/
133static int
134set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
135{
136 setentry *table = so->table;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700137 setentry *freeslot;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700138 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700139 size_t perturb;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700140 size_t mask = so->mask;
141 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
142 size_t j;
143 int cmp;
144
145 entry = &table[i];
146 if (entry->key == NULL)
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700147 goto found_null_first;
148
149 freeslot = NULL;
150 perturb = hash;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700151
152 while (1) {
153 if (entry->hash == hash) {
154 PyObject *startkey = entry->key;
155 /* startkey cannot be a dummy because the dummy hash field is -1 */
156 assert(startkey != dummy);
157 if (startkey == key)
158 goto found_active;
159 if (PyUnicode_CheckExact(startkey)
160 && PyUnicode_CheckExact(key)
161 && unicode_eq(startkey, key))
162 goto found_active;
163 Py_INCREF(startkey);
164 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
165 Py_DECREF(startkey);
166 if (cmp < 0) /* unlikely */
167 return -1;
168 if (table != so->table || entry->key != startkey) /* unlikely */
169 return set_insert_key(so, key, hash);
170 if (cmp > 0) /* likely */
171 goto found_active;
172 mask = so->mask; /* help avoid a register spill */
173 }
174 if (entry->hash == -1 && freeslot == NULL)
175 freeslot = entry;
176
177 if (i + LINEAR_PROBES <= mask) {
178 for (j = 0 ; j < LINEAR_PROBES ; j++) {
179 entry++;
180 if (entry->hash == 0 && entry->key == NULL)
181 goto found_null;
182 if (entry->hash == hash) {
183 PyObject *startkey = entry->key;
184 assert(startkey != dummy);
185 if (startkey == key)
186 goto found_active;
187 if (PyUnicode_CheckExact(startkey)
188 && PyUnicode_CheckExact(key)
189 && unicode_eq(startkey, key))
190 goto found_active;
191 Py_INCREF(startkey);
192 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
193 Py_DECREF(startkey);
194 if (cmp < 0)
195 return -1;
196 if (table != so->table || entry->key != startkey)
197 return set_insert_key(so, key, hash);
198 if (cmp > 0)
199 goto found_active;
200 mask = so->mask;
201 }
Raymond Hettinger438f9132015-02-09 06:48:29 -0600202 if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800203 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700204 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700206
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700207 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800208 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700209
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800210 entry = &table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700211 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700212 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700214
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700215 found_null_first:
216 so->fill++;
217 so->used++;
218 entry->key = key;
219 entry->hash = hash;
220 return 0;
221
Raymond Hettingera35adf52013-09-02 03:23:21 -0700222 found_null:
Raymond Hettinger8651a502015-05-27 10:37:20 -0700223 if (freeslot == NULL) {
224 /* UNUSED */
225 so->fill++;
226 } else {
227 /* DUMMY */
228 entry = freeslot;
229 }
230 so->used++;
231 entry->key = key;
232 entry->hash = hash;
233 return 0;
234
235 found_active:
236 Py_DECREF(key);
237 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000238}
239
240/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000241Internal routine used by set_table_resize() to insert an item which is
242known to be absent from the set. This routine also assumes that
243the set contains no deleted entries. Besides the performance benefit,
244using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
245Note that no refcounts are changed by this routine; if needed, the caller
246is responsible for incref'ing `key`.
247*/
248static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200249set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200252 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700253 size_t perturb = hash;
254 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800255 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700256 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000257
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700258 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800259 entry = &table[i];
260 if (entry->key == NULL)
261 goto found_null;
262 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800263 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800264 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800265 if (entry->key == NULL)
266 goto found_null;
267 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700268 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700269 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800270 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700272 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 entry->key = key;
274 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700275 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000277}
278
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700279/* ======== End logic for probing the hash table ========================== */
280/* ======================================================================== */
281
Thomas Wouters89f507f2006-12-13 04:49:30 +0000282/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000283Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000284keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000285actually be smaller than the old one.
286*/
287static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 Py_ssize_t newsize;
291 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800292 Py_ssize_t oldfill = so->fill;
293 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 int is_oldtable_malloced;
295 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100300 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 for (newsize = PySet_MINSIZE;
302 newsize <= minused && newsize > 0;
303 newsize <<= 1)
304 ;
305 if (newsize <= 0) {
306 PyErr_NoMemory();
307 return -1;
308 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Get space for a new table. */
311 oldtable = so->table;
312 assert(oldtable != NULL);
313 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (newsize == PySet_MINSIZE) {
316 /* A large table is shrinking, or we can't get any smaller. */
317 newtable = so->smalltable;
318 if (newtable == oldtable) {
319 if (so->fill == so->used) {
320 /* No dummies, so no point doing anything. */
321 return 0;
322 }
323 /* We're not going to resize it, but rebuild the
324 table anyway to purge old dummy entries.
325 Subtle: This is *necessary* if fill==size,
326 as set_lookkey needs at least one virgin slot to
327 terminate failing searches. If fill < size, it's
328 merely desirable, as dummies slow searches. */
329 assert(so->fill > so->used);
330 memcpy(small_copy, oldtable, sizeof(small_copy));
331 oldtable = small_copy;
332 }
333 }
334 else {
335 newtable = PyMem_NEW(setentry, newsize);
336 if (newtable == NULL) {
337 PyErr_NoMemory();
338 return -1;
339 }
340 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 /* Make the set empty, using the new table. */
343 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800346 so->used = 0;
347 so->mask = newsize - 1;
348 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 /* Copy the data over; this is refcount-neutral for active entries;
351 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800352 if (oldfill == oldused) {
353 for (entry = oldtable; oldused > 0; entry++) {
354 if (entry->key != NULL) {
355 oldused--;
356 set_insert_clean(so, entry->key, entry->hash);
357 }
358 }
359 } else {
360 for (entry = oldtable; oldused > 0; entry++) {
361 if (entry->key != NULL && entry->key != dummy) {
362 oldused--;
363 set_insert_clean(so, entry->key, entry->hash);
364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 }
366 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (is_oldtable_malloced)
369 PyMem_DEL(oldtable);
370 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000371}
372
Raymond Hettingerc991db22005-08-11 07:58:45 +0000373/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
374
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000375static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200376set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000377{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200378 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000379 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100380 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 assert(so->fill <= so->mask); /* at least one empty slot */
383 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000384 Py_INCREF(key);
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700385 if (set_insert_key(so, key, hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000386 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 return -1;
388 }
389 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
390 return 0;
391 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000392}
393
394static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200395set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000396{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800397 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200398 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200401 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 hash = PyObject_Hash(key);
403 if (hash == -1)
404 return -1;
405 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800406 entry.key = key;
407 entry.hash = hash;
408 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000409}
410
411#define DISCARD_NOTFOUND 0
412#define DISCARD_FOUND 1
413
414static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000415set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800416{
417 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000419
Raymond Hettinger93035c42015-01-25 16:12:49 -0800420 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (entry == NULL)
422 return -1;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700423 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 return DISCARD_NOTFOUND;
425 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800427 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 so->used--;
429 Py_DECREF(old_key);
430 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000431}
432
433static int
434set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000435{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800436 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200437 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200442 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 hash = PyObject_Hash(key);
444 if (hash == -1)
445 return -1;
446 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800447 entry.key = key;
448 entry.hash = hash;
449 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450}
451
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700452static void
453set_empty_to_minsize(PySetObject *so)
454{
455 memset(so->smalltable, 0, sizeof(so->smalltable));
456 so->fill = 0;
457 so->used = 0;
458 so->mask = PySet_MINSIZE - 1;
459 so->table = so->smalltable;
460 so->hash = -1;
461}
462
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000463static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464set_clear_internal(PySetObject *so)
465{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800466 setentry *entry;
467 setentry *table = so->table;
468 Py_ssize_t fill = so->fill;
469 Py_ssize_t used = so->used;
470 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000472
Raymond Hettinger583cd032013-09-07 22:06:35 -0700473 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 /* This is delicate. During the process of clearing the set,
477 * decrefs can cause the set to mutate. To avoid fatal confusion
478 * (voice of experience), we have to make the set empty before
479 * clearing the slots, and never refer to anything via so->ref while
480 * clearing.
481 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700483 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 else if (fill > 0) {
486 /* It's a small table with something that needs to be cleared.
487 * Afraid the only safe way is to copy the set entries into
488 * another small table first.
489 */
490 memcpy(small_copy, table, sizeof(small_copy));
491 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700492 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
494 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 /* Now we can finally clear things. If C had refcounts, we could
497 * assert that the refcount on table is 1 now, i.e. that this function
498 * has unique access to it, so decref side-effects can't alter it.
499 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800500 for (entry = table; used > 0; entry++) {
501 if (entry->key && entry->key != dummy) {
502 used--;
503 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (table_is_malloced)
508 PyMem_DEL(table);
509 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000510}
511
512/*
513 * Iterate over a set table. Use like so:
514 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000515 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000516 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000517 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000518 * while (set_next(yourset, &pos, &entry)) {
519 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000520 * }
521 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000522 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000524 */
525static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000526set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 Py_ssize_t i;
529 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200530 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 assert (PyAnySet_Check(so));
533 i = *pos_ptr;
534 assert(i >= 0);
535 table = so->table;
536 mask = so->mask;
537 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
538 i++;
539 *pos_ptr = i+1;
540 if (i > mask)
541 return 0;
542 assert(table[i].key != NULL);
543 *entry_ptr = &table[i];
544 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545}
546
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000547static void
548set_dealloc(PySetObject *so)
549{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200550 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800551 Py_ssize_t used = so->used;
552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 PyObject_GC_UnTrack(so);
554 Py_TRASHCAN_SAFE_BEGIN(so)
555 if (so->weakreflist != NULL)
556 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000557
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800558 for (entry = so->table; used > 0; entry++) {
559 if (entry->key && entry->key != dummy) {
560 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700561 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 }
563 }
564 if (so->table != so->smalltable)
565 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700566 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568}
569
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000570static PyObject *
571set_repr(PySetObject *so)
572{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200573 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (status != 0) {
577 if (status < 0)
578 return NULL;
579 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
580 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 /* shortcut for the empty set */
583 if (!so->used) {
584 Py_ReprLeave((PyObject*)so);
585 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
586 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 keys = PySequence_List((PyObject *)so);
589 if (keys == NULL)
590 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 listrepr = PyObject_Repr(keys);
594 Py_DECREF(keys);
595 if (listrepr == NULL)
596 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200597 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200599 if (tmp == NULL)
600 goto done;
601 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200602
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200603 if (Py_TYPE(so) != &PySet_Type)
604 result = PyUnicode_FromFormat("%s({%U})",
605 Py_TYPE(so)->tp_name,
606 listrepr);
607 else
608 result = PyUnicode_FromFormat("{%U}", listrepr);
609 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000610done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 Py_ReprLeave((PyObject*)so);
612 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000613}
614
Martin v. Löwis18e16552006-02-15 17:27:45 +0000615static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000616set_len(PyObject *so)
617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000619}
620
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000622set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000625 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200626 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700627 setentry *so_entry;
628 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 assert (PyAnySet_Check(so));
631 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 other = (PySetObject*)otherset;
634 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700635 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 return 0;
637 /* Do one big resize at the start, rather than
638 * incrementally resizing as we insert new keys. Expect
639 * that there will be no (or few) overlapping keys.
640 */
641 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
642 if (set_table_resize(so, (so->used + other->used)*2) != 0)
643 return -1;
644 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700645 so_entry = so->table;
646 other_entry = other->table;
647
648 /* If our table is empty, and both tables have the same size, and
649 there are no dummies to eliminate, then just copy the pointers. */
650 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
651 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
652 key = other_entry->key;
653 if (key != NULL) {
654 assert(so_entry->key == NULL);
655 Py_INCREF(key);
656 so_entry->key = key;
657 so_entry->hash = other_entry->hash;
658 }
659 }
660 so->fill = other->fill;
661 so->used = other->used;
662 return 0;
663 }
664
665 /* If our table is empty, we can use set_insert_clean() */
666 if (so->fill == 0) {
667 for (i = 0; i <= other->mask; i++, other_entry++) {
668 key = other_entry->key;
669 if (key != NULL && key != dummy) {
670 Py_INCREF(key);
671 set_insert_clean(so, key, other_entry->hash);
672 }
673 }
674 return 0;
675 }
676
677 /* We can't assure there are no duplicates, so do normal insertions */
678 for (i = 0; i <= other->mask; i++, other_entry++) {
679 key = other_entry->key;
680 if (key != NULL && key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000681 Py_INCREF(key);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700682 if (set_insert_key(so, key, other_entry->hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000683 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 return -1;
685 }
686 }
687 }
688 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000689}
690
691static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000692set_contains_entry(PySetObject *so, setentry *entry)
693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 PyObject *key;
695 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000696
Raymond Hettinger93035c42015-01-25 16:12:49 -0800697 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (lu_entry == NULL)
699 return -1;
700 key = lu_entry->key;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700701 return key != NULL;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000702}
703
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800704static int
705set_contains_key(PySetObject *so, PyObject *key)
706{
707 setentry entry;
708 Py_hash_t hash;
709
710 if (!PyUnicode_CheckExact(key) ||
711 (hash = ((PyASCIIObject *) key)->hash) == -1) {
712 hash = PyObject_Hash(key);
713 if (hash == -1)
714 return -1;
715 }
716 entry.key = key;
717 entry.hash = hash;
718 return set_contains_entry(so, &entry);
719}
720
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000721static PyObject *
722set_pop(PySetObject *so)
723{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800724 /* Make sure the search finger is in bounds */
725 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200726 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 assert (PyAnySet_Check(so));
730 if (so->used == 0) {
731 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
732 return NULL;
733 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000734
Raymond Hettinger1202a472015-01-18 13:12:42 -0800735 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
736 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800737 if (i > so->mask)
738 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 }
740 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800742 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800744 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000746}
747
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000748PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
749Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000750
751static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000752set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 Py_ssize_t pos = 0;
755 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 while (set_next(so, &pos, &entry))
758 Py_VISIT(entry->key);
759 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000760}
761
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000762static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000763frozenset_hash(PyObject *self)
764{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800765 /* Most of the constants in this hash algorithm are randomly choosen
766 large primes with "interesting bit patterns" and that passed
767 tests for good collision statistics on a variety of problematic
768 datasets such as:
769
770 ps = []
771 for r in range(21):
772 ps += itertools.combinations(range(20), r)
773 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
774
775 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800777 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 setentry *entry;
779 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 if (so->hash != -1)
782 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000783
Mark Dickinson57e683e2011-09-24 18:18:40 +0100784 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 while (set_next(so, &pos, &entry)) {
786 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800787 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 combinations of a small number of elements with nearby
789 hashes so that many distinct combinations collapse to only
790 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000791 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800792 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800794 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500795 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800796 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200797 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800798 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 so->hash = hash;
800 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000801}
802
Raymond Hettingera9d99362005-08-05 00:01:15 +0000803/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject_HEAD
807 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
808 Py_ssize_t si_used;
809 Py_ssize_t si_pos;
810 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000811} setiterobject;
812
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813static void
814setiter_dealloc(setiterobject *si)
815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 Py_XDECREF(si->si_set);
817 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000818}
819
820static int
821setiter_traverse(setiterobject *si, visitproc visit, void *arg)
822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 Py_VISIT(si->si_set);
824 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825}
826
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000827static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828setiter_len(setiterobject *si)
829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 Py_ssize_t len = 0;
831 if (si->si_set != NULL && si->si_used == si->si_set->used)
832 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000833 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000834}
835
Armin Rigof5b3e362006-02-11 21:32:43 +0000836PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000837
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000838static PyObject *setiter_iternext(setiterobject *si);
839
840static PyObject *
841setiter_reduce(setiterobject *si)
842{
843 PyObject *list;
844 setiterobject tmp;
845
846 list = PyList_New(0);
847 if (!list)
848 return NULL;
849
Ezio Melotti0e1af282012-09-28 16:43:40 +0300850 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000851 tmp = *si;
852 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300853
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000854 /* iterate the temporary into a list */
855 for(;;) {
856 PyObject *element = setiter_iternext(&tmp);
857 if (element) {
858 if (PyList_Append(list, element)) {
859 Py_DECREF(element);
860 Py_DECREF(list);
861 Py_XDECREF(tmp.si_set);
862 return NULL;
863 }
864 Py_DECREF(element);
865 } else
866 break;
867 }
868 Py_XDECREF(tmp.si_set);
869 /* check for error */
870 if (tmp.si_set != NULL) {
871 /* we have an error */
872 Py_DECREF(list);
873 return NULL;
874 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200875 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000876}
877
878PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
879
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000880static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000882 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000884};
885
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000886static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200889 Py_ssize_t i, mask;
890 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (so == NULL)
894 return NULL;
895 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (si->si_used != so->used) {
898 PyErr_SetString(PyExc_RuntimeError,
899 "Set changed size during iteration");
900 si->si_used = -1; /* Make this state sticky */
901 return NULL;
902 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 i = si->si_pos;
905 assert(i>=0);
906 entry = so->table;
907 mask = so->mask;
908 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
909 i++;
910 si->si_pos = i+1;
911 if (i > mask)
912 goto fail;
913 si->len--;
914 key = entry[i].key;
915 Py_INCREF(key);
916 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000917
918fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 Py_DECREF(so);
920 si->si_set = NULL;
921 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000922}
923
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000924PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 PyVarObject_HEAD_INIT(&PyType_Type, 0)
926 "set_iterator", /* tp_name */
927 sizeof(setiterobject), /* tp_basicsize */
928 0, /* tp_itemsize */
929 /* methods */
930 (destructor)setiter_dealloc, /* tp_dealloc */
931 0, /* tp_print */
932 0, /* tp_getattr */
933 0, /* tp_setattr */
934 0, /* tp_reserved */
935 0, /* tp_repr */
936 0, /* tp_as_number */
937 0, /* tp_as_sequence */
938 0, /* tp_as_mapping */
939 0, /* tp_hash */
940 0, /* tp_call */
941 0, /* tp_str */
942 PyObject_GenericGetAttr, /* tp_getattro */
943 0, /* tp_setattro */
944 0, /* tp_as_buffer */
945 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
946 0, /* tp_doc */
947 (traverseproc)setiter_traverse, /* tp_traverse */
948 0, /* tp_clear */
949 0, /* tp_richcompare */
950 0, /* tp_weaklistoffset */
951 PyObject_SelfIter, /* tp_iter */
952 (iternextfunc)setiter_iternext, /* tp_iternext */
953 setiter_methods, /* tp_methods */
954 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000955};
956
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000957static PyObject *
958set_iter(PySetObject *so)
959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
961 if (si == NULL)
962 return NULL;
963 Py_INCREF(so);
964 si->si_set = so;
965 si->si_used = so->used;
966 si->si_pos = 0;
967 si->len = so->used;
968 _PyObject_GC_TRACK(si);
969 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000970}
971
Raymond Hettingerd7946662005-08-01 21:39:29 +0000972static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000973set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 if (PyAnySet_Check(other))
978 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 if (PyDict_CheckExact(other)) {
981 PyObject *value;
982 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000983 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 /* Do one big resize at the start, rather than
987 * incrementally resizing as we insert new keys. Expect
988 * that there will be no (or few) overlapping keys.
989 */
990 if (dictsize == -1)
991 return -1;
992 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
993 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
994 return -1;
995 }
996 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
997 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 an_entry.hash = hash;
1000 an_entry.key = key;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001001 if (set_add_entry(so, &an_entry))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 return -1;
1003 }
1004 return 0;
1005 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 it = PyObject_GetIter(other);
1008 if (it == NULL)
1009 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001012 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 Py_DECREF(it);
1014 Py_DECREF(key);
1015 return -1;
1016 }
1017 Py_DECREF(key);
1018 }
1019 Py_DECREF(it);
1020 if (PyErr_Occurred())
1021 return -1;
1022 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001023}
1024
1025static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001026set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1031 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001032 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 return NULL;
1034 }
1035 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001036}
1037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001039"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001040
Raymond Hettinger426d9952014-05-18 21:40:20 +01001041/* XXX Todo:
1042 If aligned memory allocations become available, make the
1043 set object 64 byte aligned so that most of the fields
1044 can be retrieved or updated in a single cache line.
1045*/
1046
Raymond Hettingera38123e2003-11-24 22:18:49 +00001047static PyObject *
1048make_new_set(PyTypeObject *type, PyObject *iterable)
1049{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001050 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001053 so = (PySetObject *)type->tp_alloc(type, 0);
1054 if (so == NULL)
1055 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001056
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001057 so->fill = 0;
1058 so->used = 0;
1059 so->mask = PySet_MINSIZE - 1;
1060 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001061 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -08001062 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001066 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 Py_DECREF(so);
1068 return NULL;
1069 }
1070 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001073}
1074
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001075static PyObject *
1076make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1079 if (PyType_IsSubtype(type, &PySet_Type))
1080 type = &PySet_Type;
1081 else
1082 type = &PyFrozenSet_Type;
1083 }
1084 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001085}
1086
Raymond Hettingerd7946662005-08-01 21:39:29 +00001087/* The empty frozenset is a singleton */
1088static PyObject *emptyfrozenset = NULL;
1089
Raymond Hettingera690a992003-11-16 16:17:49 +00001090static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001091frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1096 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1099 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 if (type != &PyFrozenSet_Type)
1102 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 if (iterable != NULL) {
1105 /* frozenset(f) is idempotent */
1106 if (PyFrozenSet_CheckExact(iterable)) {
1107 Py_INCREF(iterable);
1108 return iterable;
1109 }
1110 result = make_new_set(type, iterable);
1111 if (result == NULL || PySet_GET_SIZE(result))
1112 return result;
1113 Py_DECREF(result);
1114 }
1115 /* The empty frozenset is a singleton */
1116 if (emptyfrozenset == NULL)
1117 emptyfrozenset = make_new_set(type, NULL);
1118 Py_XINCREF(emptyfrozenset);
1119 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001120}
1121
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001122int
1123PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001124{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001125 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001126}
1127
1128void
1129PySet_Fini(void)
1130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001132}
1133
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001134static PyObject *
1135set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1138 return NULL;
1139
1140 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001141}
1142
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001143/* set_swap_bodies() switches the contents of any two sets by moving their
1144 internal data pointers and, if needed, copying the internal smalltables.
1145 Semantically equivalent to:
1146
1147 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1148
1149 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001150 Useful for operations that update in-place (by allowing an intermediate
1151 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001152*/
1153
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001154static void
1155set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 Py_ssize_t t;
1158 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001160 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 t = a->fill; a->fill = b->fill; b->fill = t;
1163 t = a->used; a->used = b->used; b->used = t;
1164 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 u = a->table;
1167 if (a->table == a->smalltable)
1168 u = b->smalltable;
1169 a->table = b->table;
1170 if (b->table == b->smalltable)
1171 a->table = a->smalltable;
1172 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 if (a->table == a->smalltable || b->table == b->smalltable) {
1175 memcpy(tab, a->smalltable, sizeof(tab));
1176 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1177 memcpy(b->smalltable, tab, sizeof(tab));
1178 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1181 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1182 h = a->hash; a->hash = b->hash; b->hash = h;
1183 } else {
1184 a->hash = -1;
1185 b->hash = -1;
1186 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001187}
1188
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001189static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001190set_copy(PySetObject *so)
1191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001193}
1194
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001195static PyObject *
1196frozenset_copy(PySetObject *so)
1197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 if (PyFrozenSet_CheckExact(so)) {
1199 Py_INCREF(so);
1200 return (PyObject *)so;
1201 }
1202 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001203}
1204
Raymond Hettingera690a992003-11-16 16:17:49 +00001205PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1206
1207static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001208set_clear(PySetObject *so)
1209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 set_clear_internal(so);
1211 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001212}
1213
1214PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1215
1216static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001217set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 PySetObject *result;
1220 PyObject *other;
1221 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 result = (PySetObject *)set_copy(so);
1224 if (result == NULL)
1225 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1228 other = PyTuple_GET_ITEM(args, i);
1229 if ((PyObject *)so == other)
1230 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001231 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 Py_DECREF(result);
1233 return NULL;
1234 }
1235 }
1236 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001237}
1238
1239PyDoc_STRVAR(union_doc,
1240 "Return the union of sets as a new set.\n\
1241\n\
1242(i.e. all elements that are in either set.)");
1243
1244static PyObject *
1245set_or(PySetObject *so, PyObject *other)
1246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001248
Brian Curtindfc80e32011-08-10 20:28:54 -05001249 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1250 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 result = (PySetObject *)set_copy(so);
1253 if (result == NULL)
1254 return NULL;
1255 if ((PyObject *)so == other)
1256 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001257 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 Py_DECREF(result);
1259 return NULL;
1260 }
1261 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001262}
1263
Raymond Hettingera690a992003-11-16 16:17:49 +00001264static PyObject *
1265set_ior(PySetObject *so, PyObject *other)
1266{
Brian Curtindfc80e32011-08-10 20:28:54 -05001267 if (!PyAnySet_Check(other))
1268 Py_RETURN_NOTIMPLEMENTED;
1269
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001270 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 return NULL;
1272 Py_INCREF(so);
1273 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001274}
1275
1276static PyObject *
1277set_intersection(PySetObject *so, PyObject *other)
1278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 PySetObject *result;
1280 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if ((PyObject *)so == other)
1283 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1286 if (result == NULL)
1287 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if (PyAnySet_Check(other)) {
1290 Py_ssize_t pos = 0;
1291 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1294 tmp = (PyObject *)so;
1295 so = (PySetObject *)other;
1296 other = tmp;
1297 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 while (set_next((PySetObject *)other, &pos, &entry)) {
1300 int rv = set_contains_entry(so, entry);
1301 if (rv == -1) {
1302 Py_DECREF(result);
1303 return NULL;
1304 }
1305 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001306 if (set_add_entry(result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 Py_DECREF(result);
1308 return NULL;
1309 }
1310 }
1311 }
1312 return (PyObject *)result;
1313 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 it = PyObject_GetIter(other);
1316 if (it == NULL) {
1317 Py_DECREF(result);
1318 return NULL;
1319 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 while ((key = PyIter_Next(it)) != NULL) {
1322 int rv;
1323 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001324 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 if (hash == -1) {
1327 Py_DECREF(it);
1328 Py_DECREF(result);
1329 Py_DECREF(key);
1330 return NULL;
1331 }
1332 entry.hash = hash;
1333 entry.key = key;
1334 rv = set_contains_entry(so, &entry);
1335 if (rv == -1) {
1336 Py_DECREF(it);
1337 Py_DECREF(result);
1338 Py_DECREF(key);
1339 return NULL;
1340 }
1341 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001342 if (set_add_entry(result, &entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 Py_DECREF(it);
1344 Py_DECREF(result);
1345 Py_DECREF(key);
1346 return NULL;
1347 }
1348 }
1349 Py_DECREF(key);
1350 }
1351 Py_DECREF(it);
1352 if (PyErr_Occurred()) {
1353 Py_DECREF(result);
1354 return NULL;
1355 }
1356 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001357}
1358
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001359static PyObject *
1360set_intersection_multi(PySetObject *so, PyObject *args)
1361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 Py_ssize_t i;
1363 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 if (PyTuple_GET_SIZE(args) == 0)
1366 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 Py_INCREF(so);
1369 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1370 PyObject *other = PyTuple_GET_ITEM(args, i);
1371 PyObject *newresult = set_intersection((PySetObject *)result, other);
1372 if (newresult == NULL) {
1373 Py_DECREF(result);
1374 return NULL;
1375 }
1376 Py_DECREF(result);
1377 result = newresult;
1378 }
1379 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001380}
1381
Raymond Hettingera690a992003-11-16 16:17:49 +00001382PyDoc_STRVAR(intersection_doc,
1383"Return the intersection of two sets as a new set.\n\
1384\n\
1385(i.e. all elements that are in both sets.)");
1386
1387static PyObject *
1388set_intersection_update(PySetObject *so, PyObject *other)
1389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 tmp = set_intersection(so, other);
1393 if (tmp == NULL)
1394 return NULL;
1395 set_swap_bodies(so, (PySetObject *)tmp);
1396 Py_DECREF(tmp);
1397 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001398}
1399
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001400static PyObject *
1401set_intersection_update_multi(PySetObject *so, PyObject *args)
1402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 tmp = set_intersection_multi(so, args);
1406 if (tmp == NULL)
1407 return NULL;
1408 set_swap_bodies(so, (PySetObject *)tmp);
1409 Py_DECREF(tmp);
1410 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001411}
1412
Raymond Hettingera690a992003-11-16 16:17:49 +00001413PyDoc_STRVAR(intersection_update_doc,
1414"Update a set with the intersection of itself and another.");
1415
1416static PyObject *
1417set_and(PySetObject *so, PyObject *other)
1418{
Brian Curtindfc80e32011-08-10 20:28:54 -05001419 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1420 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001422}
1423
1424static PyObject *
1425set_iand(PySetObject *so, PyObject *other)
1426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001428
Brian Curtindfc80e32011-08-10 20:28:54 -05001429 if (!PyAnySet_Check(other))
1430 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 result = set_intersection_update(so, other);
1432 if (result == NULL)
1433 return NULL;
1434 Py_DECREF(result);
1435 Py_INCREF(so);
1436 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001437}
1438
Guido van Rossum58da9312007-11-10 23:39:45 +00001439static PyObject *
1440set_isdisjoint(PySetObject *so, PyObject *other)
1441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 if ((PyObject *)so == other) {
1445 if (PySet_GET_SIZE(so) == 0)
1446 Py_RETURN_TRUE;
1447 else
1448 Py_RETURN_FALSE;
1449 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 if (PyAnySet_CheckExact(other)) {
1452 Py_ssize_t pos = 0;
1453 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1456 tmp = (PyObject *)so;
1457 so = (PySetObject *)other;
1458 other = tmp;
1459 }
1460 while (set_next((PySetObject *)other, &pos, &entry)) {
1461 int rv = set_contains_entry(so, entry);
1462 if (rv == -1)
1463 return NULL;
1464 if (rv)
1465 Py_RETURN_FALSE;
1466 }
1467 Py_RETURN_TRUE;
1468 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 it = PyObject_GetIter(other);
1471 if (it == NULL)
1472 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 while ((key = PyIter_Next(it)) != NULL) {
1475 int rv;
1476 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001477 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (hash == -1) {
1480 Py_DECREF(key);
1481 Py_DECREF(it);
1482 return NULL;
1483 }
1484 entry.hash = hash;
1485 entry.key = key;
1486 rv = set_contains_entry(so, &entry);
1487 Py_DECREF(key);
1488 if (rv == -1) {
1489 Py_DECREF(it);
1490 return NULL;
1491 }
1492 if (rv) {
1493 Py_DECREF(it);
1494 Py_RETURN_FALSE;
1495 }
1496 }
1497 Py_DECREF(it);
1498 if (PyErr_Occurred())
1499 return NULL;
1500 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001501}
1502
1503PyDoc_STRVAR(isdisjoint_doc,
1504"Return True if two sets have a null intersection.");
1505
Neal Norwitz6576bd82005-11-13 18:41:28 +00001506static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001507set_difference_update_internal(PySetObject *so, PyObject *other)
1508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 if ((PyObject *)so == other)
1510 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 if (PyAnySet_Check(other)) {
1513 setentry *entry;
1514 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 while (set_next((PySetObject *)other, &pos, &entry))
1517 if (set_discard_entry(so, entry) == -1)
1518 return -1;
1519 } else {
1520 PyObject *key, *it;
1521 it = PyObject_GetIter(other);
1522 if (it == NULL)
1523 return -1;
1524
1525 while ((key = PyIter_Next(it)) != NULL) {
1526 if (set_discard_key(so, key) == -1) {
1527 Py_DECREF(it);
1528 Py_DECREF(key);
1529 return -1;
1530 }
1531 Py_DECREF(key);
1532 }
1533 Py_DECREF(it);
1534 if (PyErr_Occurred())
1535 return -1;
1536 }
1537 /* If more than 1/5 are dummies, then resize them away. */
1538 if ((so->fill - so->used) * 5 < so->mask)
1539 return 0;
1540 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001541}
1542
Raymond Hettingera690a992003-11-16 16:17:49 +00001543static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001544set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1549 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001550 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return NULL;
1552 }
1553 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001554}
1555
1556PyDoc_STRVAR(difference_update_doc,
1557"Remove all elements of another set from this set.");
1558
1559static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001560set_copy_and_difference(PySetObject *so, PyObject *other)
1561{
1562 PyObject *result;
1563
1564 result = set_copy(so);
1565 if (result == NULL)
1566 return NULL;
1567 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1568 return result;
1569 Py_DECREF(result);
1570 return NULL;
1571}
1572
1573static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001574set_difference(PySetObject *so, PyObject *other)
1575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 PyObject *result;
1577 setentry *entry;
1578 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001581 return set_copy_and_difference(so, other);
1582 }
1583
1584 /* If len(so) much more than len(other), it's more efficient to simply copy
1585 * so and then iterate other looking for common elements. */
1586 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1587 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 result = make_new_set_basetype(Py_TYPE(so), NULL);
1591 if (result == NULL)
1592 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (PyDict_CheckExact(other)) {
1595 while (set_next(so, &pos, &entry)) {
1596 setentry entrycopy;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001597 int rv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 entrycopy.hash = entry->hash;
1599 entrycopy.key = entry->key;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001600 rv = _PyDict_Contains(other, entry->key, entry->hash);
1601 if (rv < 0) {
1602 Py_DECREF(result);
1603 return NULL;
1604 }
1605 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001606 if (set_add_entry((PySetObject *)result, &entrycopy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 Py_DECREF(result);
1608 return NULL;
1609 }
1610 }
1611 }
1612 return result;
1613 }
1614
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001615 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 while (set_next(so, &pos, &entry)) {
1617 int rv = set_contains_entry((PySetObject *)other, entry);
1618 if (rv == -1) {
1619 Py_DECREF(result);
1620 return NULL;
1621 }
1622 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001623 if (set_add_entry((PySetObject *)result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 Py_DECREF(result);
1625 return NULL;
1626 }
1627 }
1628 }
1629 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001630}
1631
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001632static PyObject *
1633set_difference_multi(PySetObject *so, PyObject *args)
1634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 Py_ssize_t i;
1636 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if (PyTuple_GET_SIZE(args) == 0)
1639 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 other = PyTuple_GET_ITEM(args, 0);
1642 result = set_difference(so, other);
1643 if (result == NULL)
1644 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1647 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001648 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 Py_DECREF(result);
1650 return NULL;
1651 }
1652 }
1653 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001654}
1655
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001656PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001657"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001658\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001659(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001660static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001661set_sub(PySetObject *so, PyObject *other)
1662{
Brian Curtindfc80e32011-08-10 20:28:54 -05001663 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1664 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001666}
1667
1668static PyObject *
1669set_isub(PySetObject *so, PyObject *other)
1670{
Brian Curtindfc80e32011-08-10 20:28:54 -05001671 if (!PyAnySet_Check(other))
1672 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001673 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 return NULL;
1675 Py_INCREF(so);
1676 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001677}
1678
1679static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001680set_symmetric_difference_update(PySetObject *so, PyObject *other)
1681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 PySetObject *otherset;
1683 PyObject *key;
1684 Py_ssize_t pos = 0;
1685 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 if ((PyObject *)so == other)
1688 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 if (PyDict_CheckExact(other)) {
1691 PyObject *value;
1692 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001693 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1695 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001696
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001697 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 an_entry.hash = hash;
1699 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001702 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001703 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001706 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001707 if (set_add_entry(so, &an_entry)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001708 Py_DECREF(key);
1709 return NULL;
1710 }
1711 }
Georg Brandl2d444492010-09-03 10:52:55 +00001712 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 }
1714 Py_RETURN_NONE;
1715 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 if (PyAnySet_Check(other)) {
1718 Py_INCREF(other);
1719 otherset = (PySetObject *)other;
1720 } else {
1721 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1722 if (otherset == NULL)
1723 return NULL;
1724 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 while (set_next(otherset, &pos, &entry)) {
1727 int rv = set_discard_entry(so, entry);
1728 if (rv == -1) {
1729 Py_DECREF(otherset);
1730 return NULL;
1731 }
1732 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001733 if (set_add_entry(so, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 Py_DECREF(otherset);
1735 return NULL;
1736 }
1737 }
1738 }
1739 Py_DECREF(otherset);
1740 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001741}
1742
1743PyDoc_STRVAR(symmetric_difference_update_doc,
1744"Update a set with the symmetric difference of itself and another.");
1745
1746static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001747set_symmetric_difference(PySetObject *so, PyObject *other)
1748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 PyObject *rv;
1750 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1753 if (otherset == NULL)
1754 return NULL;
1755 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1756 if (rv == NULL)
1757 return NULL;
1758 Py_DECREF(rv);
1759 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001760}
1761
1762PyDoc_STRVAR(symmetric_difference_doc,
1763"Return the symmetric difference of two sets as a new set.\n\
1764\n\
1765(i.e. all elements that are in exactly one of the sets.)");
1766
1767static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001768set_xor(PySetObject *so, PyObject *other)
1769{
Brian Curtindfc80e32011-08-10 20:28:54 -05001770 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1771 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001773}
1774
1775static PyObject *
1776set_ixor(PySetObject *so, PyObject *other)
1777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001779
Brian Curtindfc80e32011-08-10 20:28:54 -05001780 if (!PyAnySet_Check(other))
1781 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 result = set_symmetric_difference_update(so, other);
1783 if (result == NULL)
1784 return NULL;
1785 Py_DECREF(result);
1786 Py_INCREF(so);
1787 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001788}
1789
1790static PyObject *
1791set_issubset(PySetObject *so, PyObject *other)
1792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 setentry *entry;
1794 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 if (!PyAnySet_Check(other)) {
1797 PyObject *tmp, *result;
1798 tmp = make_new_set(&PySet_Type, other);
1799 if (tmp == NULL)
1800 return NULL;
1801 result = set_issubset(so, tmp);
1802 Py_DECREF(tmp);
1803 return result;
1804 }
1805 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1806 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 while (set_next(so, &pos, &entry)) {
1809 int rv = set_contains_entry((PySetObject *)other, entry);
1810 if (rv == -1)
1811 return NULL;
1812 if (!rv)
1813 Py_RETURN_FALSE;
1814 }
1815 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001816}
1817
1818PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1819
1820static PyObject *
1821set_issuperset(PySetObject *so, PyObject *other)
1822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 if (!PyAnySet_Check(other)) {
1826 tmp = make_new_set(&PySet_Type, other);
1827 if (tmp == NULL)
1828 return NULL;
1829 result = set_issuperset(so, tmp);
1830 Py_DECREF(tmp);
1831 return result;
1832 }
1833 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001834}
1835
1836PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1837
Raymond Hettingera690a992003-11-16 16:17:49 +00001838static PyObject *
1839set_richcompare(PySetObject *v, PyObject *w, int op)
1840{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001841 PyObject *r1;
1842 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001843
Brian Curtindfc80e32011-08-10 20:28:54 -05001844 if(!PyAnySet_Check(w))
1845 Py_RETURN_NOTIMPLEMENTED;
1846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 switch (op) {
1848 case Py_EQ:
1849 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1850 Py_RETURN_FALSE;
1851 if (v->hash != -1 &&
1852 ((PySetObject *)w)->hash != -1 &&
1853 v->hash != ((PySetObject *)w)->hash)
1854 Py_RETURN_FALSE;
1855 return set_issubset(v, w);
1856 case Py_NE:
1857 r1 = set_richcompare(v, w, Py_EQ);
1858 if (r1 == NULL)
1859 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001860 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001862 if (r2 < 0)
1863 return NULL;
1864 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 case Py_LE:
1866 return set_issubset(v, w);
1867 case Py_GE:
1868 return set_issuperset(v, w);
1869 case Py_LT:
1870 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1871 Py_RETURN_FALSE;
1872 return set_issubset(v, w);
1873 case Py_GT:
1874 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1875 Py_RETURN_FALSE;
1876 return set_issuperset(v, w);
1877 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001878 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001879}
1880
Raymond Hettingera690a992003-11-16 16:17:49 +00001881static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001882set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001883{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001884 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 return NULL;
1886 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001887}
1888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001890"Add an element to a set.\n\
1891\n\
1892This has no effect if the element is already present.");
1893
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001894static int
1895set_contains(PySetObject *so, PyObject *key)
1896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 PyObject *tmpkey;
1898 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 rv = set_contains_key(so, key);
1901 if (rv == -1) {
1902 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1903 return -1;
1904 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001905 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (tmpkey == NULL)
1907 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001908 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 Py_DECREF(tmpkey);
1910 }
1911 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001912}
1913
1914static PyObject *
1915set_direct_contains(PySetObject *so, PyObject *key)
1916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 result = set_contains(so, key);
1920 if (result == -1)
1921 return NULL;
1922 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001923}
1924
1925PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1926
Raymond Hettingera690a992003-11-16 16:17:49 +00001927static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001928set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 PyObject *tmpkey;
1931 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 rv = set_discard_key(so, key);
1934 if (rv == -1) {
1935 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1936 return NULL;
1937 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001938 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 if (tmpkey == NULL)
1940 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 Py_DECREF(tmpkey);
1943 if (rv == -1)
1944 return NULL;
1945 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001948 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 return NULL;
1950 }
1951 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001952}
1953
1954PyDoc_STRVAR(remove_doc,
1955"Remove an element from a set; it must be a member.\n\
1956\n\
1957If the element is not a member, raise a KeyError.");
1958
1959static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001960set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001961{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001962 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 rv = set_discard_key(so, key);
1966 if (rv == -1) {
1967 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1968 return NULL;
1969 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001970 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 if (tmpkey == NULL)
1972 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001973 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001975 if (rv == -1)
1976 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 }
1978 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001979}
1980
1981PyDoc_STRVAR(discard_doc,
1982"Remove an element from a set if it is a member.\n\
1983\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001985
1986static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001987set_reduce(PySetObject *so)
1988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001990 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 keys = PySequence_List((PyObject *)so);
1993 if (keys == NULL)
1994 goto done;
1995 args = PyTuple_Pack(1, keys);
1996 if (args == NULL)
1997 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001998 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 if (dict == NULL) {
2000 PyErr_Clear();
2001 dict = Py_None;
2002 Py_INCREF(dict);
2003 }
2004 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002005done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 Py_XDECREF(args);
2007 Py_XDECREF(keys);
2008 Py_XDECREF(dict);
2009 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002010}
2011
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002012static PyObject *
2013set_sizeof(PySetObject *so)
2014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 res = sizeof(PySetObject);
2018 if (so->table != so->smalltable)
2019 res = res + (so->mask + 1) * sizeof(setentry);
2020 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002021}
2022
2023PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002024static int
2025set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 if (!PyAnySet_Check(self))
2030 return -1;
2031 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2032 return -1;
2033 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2034 return -1;
2035 set_clear_internal(self);
2036 self->hash = -1;
2037 if (iterable == NULL)
2038 return 0;
2039 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002040}
2041
Raymond Hettingera690a992003-11-16 16:17:49 +00002042static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 set_len, /* sq_length */
2044 0, /* sq_concat */
2045 0, /* sq_repeat */
2046 0, /* sq_item */
2047 0, /* sq_slice */
2048 0, /* sq_ass_item */
2049 0, /* sq_ass_slice */
2050 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002051};
2052
2053/* set object ********************************************************/
2054
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002055#ifdef Py_DEBUG
2056static PyObject *test_c_api(PySetObject *so);
2057
2058PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2059All is well if assertions don't fail.");
2060#endif
2061
Raymond Hettingera690a992003-11-16 16:17:49 +00002062static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 {"add", (PyCFunction)set_add, METH_O,
2064 add_doc},
2065 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2066 clear_doc},
2067 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2068 contains_doc},
2069 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2070 copy_doc},
2071 {"discard", (PyCFunction)set_discard, METH_O,
2072 discard_doc},
2073 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2074 difference_doc},
2075 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2076 difference_update_doc},
2077 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2078 intersection_doc},
2079 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2080 intersection_update_doc},
2081 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2082 isdisjoint_doc},
2083 {"issubset", (PyCFunction)set_issubset, METH_O,
2084 issubset_doc},
2085 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2086 issuperset_doc},
2087 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2088 pop_doc},
2089 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2090 reduce_doc},
2091 {"remove", (PyCFunction)set_remove, METH_O,
2092 remove_doc},
2093 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2094 sizeof_doc},
2095 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2096 symmetric_difference_doc},
2097 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2098 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002099#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2101 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002102#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 {"union", (PyCFunction)set_union, METH_VARARGS,
2104 union_doc},
2105 {"update", (PyCFunction)set_update, METH_VARARGS,
2106 update_doc},
2107 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002108};
2109
2110static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 0, /*nb_add*/
2112 (binaryfunc)set_sub, /*nb_subtract*/
2113 0, /*nb_multiply*/
2114 0, /*nb_remainder*/
2115 0, /*nb_divmod*/
2116 0, /*nb_power*/
2117 0, /*nb_negative*/
2118 0, /*nb_positive*/
2119 0, /*nb_absolute*/
2120 0, /*nb_bool*/
2121 0, /*nb_invert*/
2122 0, /*nb_lshift*/
2123 0, /*nb_rshift*/
2124 (binaryfunc)set_and, /*nb_and*/
2125 (binaryfunc)set_xor, /*nb_xor*/
2126 (binaryfunc)set_or, /*nb_or*/
2127 0, /*nb_int*/
2128 0, /*nb_reserved*/
2129 0, /*nb_float*/
2130 0, /*nb_inplace_add*/
2131 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2132 0, /*nb_inplace_multiply*/
2133 0, /*nb_inplace_remainder*/
2134 0, /*nb_inplace_power*/
2135 0, /*nb_inplace_lshift*/
2136 0, /*nb_inplace_rshift*/
2137 (binaryfunc)set_iand, /*nb_inplace_and*/
2138 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2139 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002140};
2141
2142PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002143"set() -> new empty set object\n\
2144set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002145\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002146Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002147
2148PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2150 "set", /* tp_name */
2151 sizeof(PySetObject), /* tp_basicsize */
2152 0, /* tp_itemsize */
2153 /* methods */
2154 (destructor)set_dealloc, /* tp_dealloc */
2155 0, /* tp_print */
2156 0, /* tp_getattr */
2157 0, /* tp_setattr */
2158 0, /* tp_reserved */
2159 (reprfunc)set_repr, /* tp_repr */
2160 &set_as_number, /* tp_as_number */
2161 &set_as_sequence, /* tp_as_sequence */
2162 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002163 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 0, /* tp_call */
2165 0, /* tp_str */
2166 PyObject_GenericGetAttr, /* tp_getattro */
2167 0, /* tp_setattro */
2168 0, /* tp_as_buffer */
2169 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2170 Py_TPFLAGS_BASETYPE, /* tp_flags */
2171 set_doc, /* tp_doc */
2172 (traverseproc)set_traverse, /* tp_traverse */
2173 (inquiry)set_clear_internal, /* tp_clear */
2174 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002175 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2176 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 0, /* tp_iternext */
2178 set_methods, /* tp_methods */
2179 0, /* tp_members */
2180 0, /* tp_getset */
2181 0, /* tp_base */
2182 0, /* tp_dict */
2183 0, /* tp_descr_get */
2184 0, /* tp_descr_set */
2185 0, /* tp_dictoffset */
2186 (initproc)set_init, /* tp_init */
2187 PyType_GenericAlloc, /* tp_alloc */
2188 set_new, /* tp_new */
2189 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002190};
2191
2192/* frozenset object ********************************************************/
2193
2194
2195static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2197 contains_doc},
2198 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2199 copy_doc},
2200 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2201 difference_doc},
2202 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2203 intersection_doc},
2204 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2205 isdisjoint_doc},
2206 {"issubset", (PyCFunction)set_issubset, METH_O,
2207 issubset_doc},
2208 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2209 issuperset_doc},
2210 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2211 reduce_doc},
2212 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2213 sizeof_doc},
2214 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2215 symmetric_difference_doc},
2216 {"union", (PyCFunction)set_union, METH_VARARGS,
2217 union_doc},
2218 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002219};
2220
2221static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 0, /*nb_add*/
2223 (binaryfunc)set_sub, /*nb_subtract*/
2224 0, /*nb_multiply*/
2225 0, /*nb_remainder*/
2226 0, /*nb_divmod*/
2227 0, /*nb_power*/
2228 0, /*nb_negative*/
2229 0, /*nb_positive*/
2230 0, /*nb_absolute*/
2231 0, /*nb_bool*/
2232 0, /*nb_invert*/
2233 0, /*nb_lshift*/
2234 0, /*nb_rshift*/
2235 (binaryfunc)set_and, /*nb_and*/
2236 (binaryfunc)set_xor, /*nb_xor*/
2237 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002238};
2239
2240PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002241"frozenset() -> empty frozenset object\n\
2242frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002243\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002244Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002245
2246PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2248 "frozenset", /* tp_name */
2249 sizeof(PySetObject), /* tp_basicsize */
2250 0, /* tp_itemsize */
2251 /* methods */
2252 (destructor)set_dealloc, /* tp_dealloc */
2253 0, /* tp_print */
2254 0, /* tp_getattr */
2255 0, /* tp_setattr */
2256 0, /* tp_reserved */
2257 (reprfunc)set_repr, /* tp_repr */
2258 &frozenset_as_number, /* tp_as_number */
2259 &set_as_sequence, /* tp_as_sequence */
2260 0, /* tp_as_mapping */
2261 frozenset_hash, /* tp_hash */
2262 0, /* tp_call */
2263 0, /* tp_str */
2264 PyObject_GenericGetAttr, /* tp_getattro */
2265 0, /* tp_setattro */
2266 0, /* tp_as_buffer */
2267 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2268 Py_TPFLAGS_BASETYPE, /* tp_flags */
2269 frozenset_doc, /* tp_doc */
2270 (traverseproc)set_traverse, /* tp_traverse */
2271 (inquiry)set_clear_internal, /* tp_clear */
2272 (richcmpfunc)set_richcompare, /* tp_richcompare */
2273 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2274 (getiterfunc)set_iter, /* tp_iter */
2275 0, /* tp_iternext */
2276 frozenset_methods, /* tp_methods */
2277 0, /* tp_members */
2278 0, /* tp_getset */
2279 0, /* tp_base */
2280 0, /* tp_dict */
2281 0, /* tp_descr_get */
2282 0, /* tp_descr_set */
2283 0, /* tp_dictoffset */
2284 0, /* tp_init */
2285 PyType_GenericAlloc, /* tp_alloc */
2286 frozenset_new, /* tp_new */
2287 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002288};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002289
2290
2291/***** C API functions *************************************************/
2292
2293PyObject *
2294PySet_New(PyObject *iterable)
2295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002297}
2298
2299PyObject *
2300PyFrozenSet_New(PyObject *iterable)
2301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002303}
2304
Neal Norwitz8c49c822006-03-04 18:41:19 +00002305Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002306PySet_Size(PyObject *anyset)
2307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 if (!PyAnySet_Check(anyset)) {
2309 PyErr_BadInternalCall();
2310 return -1;
2311 }
2312 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002313}
2314
2315int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002316PySet_Clear(PyObject *set)
2317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 if (!PySet_Check(set)) {
2319 PyErr_BadInternalCall();
2320 return -1;
2321 }
2322 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002323}
2324
2325int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002326PySet_Contains(PyObject *anyset, PyObject *key)
2327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 if (!PyAnySet_Check(anyset)) {
2329 PyErr_BadInternalCall();
2330 return -1;
2331 }
2332 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002333}
2334
2335int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002336PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 if (!PySet_Check(set)) {
2339 PyErr_BadInternalCall();
2340 return -1;
2341 }
2342 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002343}
2344
2345int
Christian Heimesfd66e512008-01-29 12:18:50 +00002346PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 if (!PySet_Check(anyset) &&
2349 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2350 PyErr_BadInternalCall();
2351 return -1;
2352 }
2353 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002354}
2355
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002356int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002357_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 if (!PyAnySet_Check(set)) {
2362 PyErr_BadInternalCall();
2363 return -1;
2364 }
2365 if (set_next((PySetObject *)set, pos, &entry) == 0)
2366 return 0;
2367 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002368 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002370}
2371
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002372PyObject *
2373PySet_Pop(PyObject *set)
2374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 if (!PySet_Check(set)) {
2376 PyErr_BadInternalCall();
2377 return NULL;
2378 }
2379 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002380}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002381
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002382int
2383_PySet_Update(PyObject *set, PyObject *iterable)
2384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 if (!PySet_Check(set)) {
2386 PyErr_BadInternalCall();
2387 return -1;
2388 }
2389 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002390}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002391
Raymond Hettingere259f132013-12-15 11:56:14 -08002392/* Exported for the gdb plugin's benefit. */
2393PyObject *_PySet_Dummy = dummy;
2394
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002395#ifdef Py_DEBUG
2396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002398 Returns True and original set is restored. */
2399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400#define assertRaises(call_return_value, exception) \
2401 do { \
2402 assert(call_return_value); \
2403 assert(PyErr_ExceptionMatches(exception)); \
2404 PyErr_Clear(); \
2405 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002406
2407static PyObject *
2408test_c_api(PySetObject *so)
2409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 Py_ssize_t count;
2411 char *s;
2412 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002413 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002415 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 /* Verify preconditions */
2419 assert(PyAnySet_Check(ob));
2420 assert(PyAnySet_CheckExact(ob));
2421 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 /* so.clear(); so |= set("abc"); */
2424 str = PyUnicode_FromString("abc");
2425 if (str == NULL)
2426 return NULL;
2427 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002428 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 Py_DECREF(str);
2430 return NULL;
2431 }
2432 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 /* Exercise type/size checks */
2435 assert(PySet_Size(ob) == 3);
2436 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 /* Raise TypeError for non-iterable constructor arguments */
2439 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2440 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 /* Raise TypeError for unhashable key */
2443 dup = PySet_New(ob);
2444 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2445 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2446 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 /* Exercise successful pop, contains, add, and discard */
2449 elem = PySet_Pop(ob);
2450 assert(PySet_Contains(ob, elem) == 0);
2451 assert(PySet_GET_SIZE(ob) == 2);
2452 assert(PySet_Add(ob, elem) == 0);
2453 assert(PySet_Contains(ob, elem) == 1);
2454 assert(PySet_GET_SIZE(ob) == 3);
2455 assert(PySet_Discard(ob, elem) == 1);
2456 assert(PySet_GET_SIZE(ob) == 2);
2457 assert(PySet_Discard(ob, elem) == 0);
2458 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* Exercise clear */
2461 dup2 = PySet_New(dup);
2462 assert(PySet_Clear(dup2) == 0);
2463 assert(PySet_Size(dup2) == 0);
2464 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 /* Raise SystemError on clear or update of frozen set */
2467 f = PyFrozenSet_New(dup);
2468 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2469 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2470 assert(PySet_Add(f, elem) == 0);
2471 Py_INCREF(f);
2472 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2473 Py_DECREF(f);
2474 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 /* Exercise direct iteration */
2477 i = 0, count = 0;
2478 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2479 s = _PyUnicode_AsString(x);
2480 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2481 count++;
2482 }
2483 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 /* Exercise updates */
2486 dup2 = PySet_New(NULL);
2487 assert(_PySet_Update(dup2, dup) == 0);
2488 assert(PySet_Size(dup2) == 3);
2489 assert(_PySet_Update(dup2, dup) == 0);
2490 assert(PySet_Size(dup2) == 3);
2491 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 /* Raise SystemError when self argument is not a set or frozenset. */
2494 t = PyTuple_New(0);
2495 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2496 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2497 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 /* Raise SystemError when self argument is not a set. */
2500 f = PyFrozenSet_New(dup);
2501 assert(PySet_Size(f) == 3);
2502 assert(PyFrozenSet_CheckExact(f));
2503 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2504 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2505 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 /* Raise KeyError when popping from an empty set */
2508 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2509 Py_DECREF(ob);
2510 assert(PySet_GET_SIZE(ob) == 0);
2511 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 /* Restore the set from the copy using the PyNumber API */
2514 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2515 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 /* Verify constructors accept NULL arguments */
2518 f = PySet_New(NULL);
2519 assert(f != NULL);
2520 assert(PySet_GET_SIZE(f) == 0);
2521 Py_DECREF(f);
2522 f = PyFrozenSet_New(NULL);
2523 assert(f != NULL);
2524 assert(PyFrozenSet_CheckExact(f));
2525 assert(PySet_GET_SIZE(f) == 0);
2526 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 Py_DECREF(elem);
2529 Py_DECREF(dup);
2530 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002531}
2532
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002533#undef assertRaises
2534
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002535#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002536
2537/***** Dummy Struct *************************************************/
2538
2539static PyObject *
2540dummy_repr(PyObject *op)
2541{
2542 return PyUnicode_FromString("<dummy key>");
2543}
2544
2545static void
2546dummy_dealloc(PyObject* ignore)
2547{
2548 Py_FatalError("deallocating <dummy key>");
2549}
2550
2551static PyTypeObject _PySetDummy_Type = {
2552 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2553 "<dummy key> type",
2554 0,
2555 0,
2556 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2557 0, /*tp_print*/
2558 0, /*tp_getattr*/
2559 0, /*tp_setattr*/
2560 0, /*tp_reserved*/
2561 dummy_repr, /*tp_repr*/
2562 0, /*tp_as_number*/
2563 0, /*tp_as_sequence*/
2564 0, /*tp_as_mapping*/
2565 0, /*tp_hash */
2566 0, /*tp_call */
2567 0, /*tp_str */
2568 0, /*tp_getattro */
2569 0, /*tp_setattro */
2570 0, /*tp_as_buffer */
2571 Py_TPFLAGS_DEFAULT, /*tp_flags */
2572};
2573
2574static PyObject _dummy_struct = {
2575 _PyObject_EXTRA_INIT
2576 2, &_PySetDummy_Type
2577};
2578