blob: 09d1129c8c524f0b34771fffd837636f3ed182e4 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettingerbd9b2002015-01-18 16:10:30 -08007 Copyright (c) 2003-2015 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
Raymond Hettinger4d45c102015-01-25 16:27:40 -080026 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070027 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020055 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070056 size_t perturb;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080058 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070059 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070060 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080062 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070063 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070065
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070066 perturb = hash;
67
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070068 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080069 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070070 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080071 /* startkey cannot be a dummy because the dummy hash field is -1 */
72 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080073 if (startkey == key)
74 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080075 if (PyUnicode_CheckExact(startkey)
76 && PyUnicode_CheckExact(key)
77 && unicode_eq(startkey, key))
78 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 Py_INCREF(startkey);
80 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
81 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010082 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010084 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010086 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070087 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060088 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070089 }
Raymond Hettingerafe89092013-08-28 20:59:31 -070090
Raymond Hettingerc658d852015-02-02 08:35:00 -080091 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080092 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080093 entry++;
Raymond Hettinger8651a502015-05-27 10:37:20 -070094 if (entry->hash == 0 && entry->key == NULL)
95 return entry;
Raymond Hettingerc658d852015-02-02 08:35:00 -080096 if (entry->hash == hash) {
97 PyObject *startkey = entry->key;
98 assert(startkey != dummy);
99 if (startkey == key)
100 return entry;
101 if (PyUnicode_CheckExact(startkey)
102 && PyUnicode_CheckExact(key)
103 && unicode_eq(startkey, key))
104 return entry;
105 Py_INCREF(startkey);
106 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
107 Py_DECREF(startkey);
108 if (cmp < 0)
109 return NULL;
110 if (table != so->table || entry->key != startkey)
111 return set_lookkey(so, key, hash);
112 if (cmp > 0)
113 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600114 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800115 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700116 }
117 }
118
119 perturb >>= PERTURB_SHIFT;
120 i = (i * 5 + 1 + perturb) & mask;
121
122 entry = &table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700123 if (entry->key == NULL)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700124 return entry;
125 }
126}
127
Raymond Hettinger8651a502015-05-27 10:37:20 -0700128static int
129set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
130{
131 setentry *table = so->table;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700132 setentry *freeslot;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700133 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700134 size_t perturb;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700135 size_t mask = so->mask;
136 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
137 size_t j;
138 int cmp;
139
140 entry = &table[i];
141 if (entry->key == NULL)
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700142 goto found_null_first;
143
144 freeslot = NULL;
145 perturb = hash;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700146
147 while (1) {
148 if (entry->hash == hash) {
149 PyObject *startkey = entry->key;
150 /* startkey cannot be a dummy because the dummy hash field is -1 */
151 assert(startkey != dummy);
152 if (startkey == key)
153 goto found_active;
154 if (PyUnicode_CheckExact(startkey)
155 && PyUnicode_CheckExact(key)
156 && unicode_eq(startkey, key))
157 goto found_active;
158 Py_INCREF(startkey);
159 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
160 Py_DECREF(startkey);
161 if (cmp < 0) /* unlikely */
162 return -1;
163 if (table != so->table || entry->key != startkey) /* unlikely */
164 return set_insert_key(so, key, hash);
165 if (cmp > 0) /* likely */
166 goto found_active;
167 mask = so->mask; /* help avoid a register spill */
168 }
169 if (entry->hash == -1 && freeslot == NULL)
170 freeslot = entry;
171
172 if (i + LINEAR_PROBES <= mask) {
173 for (j = 0 ; j < LINEAR_PROBES ; j++) {
174 entry++;
175 if (entry->hash == 0 && entry->key == NULL)
176 goto found_null;
177 if (entry->hash == hash) {
178 PyObject *startkey = entry->key;
179 assert(startkey != dummy);
180 if (startkey == key)
181 goto found_active;
182 if (PyUnicode_CheckExact(startkey)
183 && PyUnicode_CheckExact(key)
184 && unicode_eq(startkey, key))
185 goto found_active;
186 Py_INCREF(startkey);
187 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
188 Py_DECREF(startkey);
189 if (cmp < 0)
190 return -1;
191 if (table != so->table || entry->key != startkey)
192 return set_insert_key(so, key, hash);
193 if (cmp > 0)
194 goto found_active;
195 mask = so->mask;
196 }
Raymond Hettinger91672612015-06-26 02:50:21 -0700197 else if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800198 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700201
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700202 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800203 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700204
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800205 entry = &table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700206 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700207 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700209
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700210 found_null_first:
Raymond Hettinger2eff9e92015-06-27 22:03:35 -0700211 Py_INCREF(key);
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700212 so->fill++;
213 so->used++;
214 entry->key = key;
215 entry->hash = hash;
216 return 0;
217
Raymond Hettingera35adf52013-09-02 03:23:21 -0700218 found_null:
Raymond Hettinger2eff9e92015-06-27 22:03:35 -0700219 Py_INCREF(key);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700220 if (freeslot == NULL) {
221 /* UNUSED */
222 so->fill++;
223 } else {
224 /* DUMMY */
225 entry = freeslot;
226 }
227 so->used++;
228 entry->key = key;
229 entry->hash = hash;
230 return 0;
231
232 found_active:
Raymond Hettinger8651a502015-05-27 10:37:20 -0700233 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234}
235
236/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000237Internal routine used by set_table_resize() to insert an item which is
238known to be absent from the set. This routine also assumes that
239the set contains no deleted entries. Besides the performance benefit,
240using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
241Note that no refcounts are changed by this routine; if needed, the caller
242is responsible for incref'ing `key`.
243*/
244static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200245set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200248 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700249 size_t perturb = hash;
250 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800251 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700252 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000253
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700254 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800255 entry = &table[i];
256 if (entry->key == NULL)
257 goto found_null;
258 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800259 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800260 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800261 if (entry->key == NULL)
262 goto found_null;
263 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700264 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700265 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800266 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700268 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 entry->key = key;
270 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700271 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000273}
274
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700275/* ======== End logic for probing the hash table ========================== */
276/* ======================================================================== */
277
Thomas Wouters89f507f2006-12-13 04:49:30 +0000278/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000279Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000280keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000281actually be smaller than the old one.
282*/
283static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_ssize_t newsize;
287 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800288 Py_ssize_t oldfill = so->fill;
289 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 int is_oldtable_malloced;
291 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100296 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 for (newsize = PySet_MINSIZE;
298 newsize <= minused && newsize > 0;
299 newsize <<= 1)
300 ;
301 if (newsize <= 0) {
302 PyErr_NoMemory();
303 return -1;
304 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 /* Get space for a new table. */
307 oldtable = so->table;
308 assert(oldtable != NULL);
309 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (newsize == PySet_MINSIZE) {
312 /* A large table is shrinking, or we can't get any smaller. */
313 newtable = so->smalltable;
314 if (newtable == oldtable) {
315 if (so->fill == so->used) {
316 /* No dummies, so no point doing anything. */
317 return 0;
318 }
319 /* We're not going to resize it, but rebuild the
320 table anyway to purge old dummy entries.
321 Subtle: This is *necessary* if fill==size,
322 as set_lookkey needs at least one virgin slot to
323 terminate failing searches. If fill < size, it's
324 merely desirable, as dummies slow searches. */
325 assert(so->fill > so->used);
326 memcpy(small_copy, oldtable, sizeof(small_copy));
327 oldtable = small_copy;
328 }
329 }
330 else {
331 newtable = PyMem_NEW(setentry, newsize);
332 if (newtable == NULL) {
333 PyErr_NoMemory();
334 return -1;
335 }
336 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Make the set empty, using the new table. */
339 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800342 so->used = 0;
343 so->mask = newsize - 1;
344 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 /* Copy the data over; this is refcount-neutral for active entries;
347 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800348 if (oldfill == oldused) {
349 for (entry = oldtable; oldused > 0; entry++) {
350 if (entry->key != NULL) {
351 oldused--;
352 set_insert_clean(so, entry->key, entry->hash);
353 }
354 }
355 } else {
356 for (entry = oldtable; oldused > 0; entry++) {
357 if (entry->key != NULL && entry->key != dummy) {
358 oldused--;
359 set_insert_clean(so, entry->key, entry->hash);
360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 }
362 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (is_oldtable_malloced)
365 PyMem_DEL(oldtable);
366 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000367}
368
Raymond Hettingerc991db22005-08-11 07:58:45 +0000369/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
370
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000371static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200372set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000373{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200374 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000375 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100376 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 assert(so->fill <= so->mask); /* at least one empty slot */
379 n_used = so->used;
Raymond Hettinger2eff9e92015-06-27 22:03:35 -0700380 if (set_insert_key(so, key, hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
383 return 0;
384 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000385}
386
387static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200388set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000389{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800390 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200391 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200394 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 hash = PyObject_Hash(key);
396 if (hash == -1)
397 return -1;
398 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800399 entry.key = key;
400 entry.hash = hash;
401 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402}
403
404#define DISCARD_NOTFOUND 0
405#define DISCARD_FOUND 1
406
407static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000408set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800409{
410 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000412
Raymond Hettinger93035c42015-01-25 16:12:49 -0800413 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (entry == NULL)
415 return -1;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700416 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return DISCARD_NOTFOUND;
418 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800420 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 so->used--;
422 Py_DECREF(old_key);
423 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000424}
425
426static int
427set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000428{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800429 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200430 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200435 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 hash = PyObject_Hash(key);
437 if (hash == -1)
438 return -1;
439 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800440 entry.key = key;
441 entry.hash = hash;
442 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443}
444
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700445static void
446set_empty_to_minsize(PySetObject *so)
447{
448 memset(so->smalltable, 0, sizeof(so->smalltable));
449 so->fill = 0;
450 so->used = 0;
451 so->mask = PySet_MINSIZE - 1;
452 so->table = so->smalltable;
453 so->hash = -1;
454}
455
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000456static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457set_clear_internal(PySetObject *so)
458{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800459 setentry *entry;
460 setentry *table = so->table;
461 Py_ssize_t fill = so->fill;
462 Py_ssize_t used = so->used;
463 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000465
Raymond Hettinger583cd032013-09-07 22:06:35 -0700466 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 /* This is delicate. During the process of clearing the set,
470 * decrefs can cause the set to mutate. To avoid fatal confusion
471 * (voice of experience), we have to make the set empty before
472 * clearing the slots, and never refer to anything via so->ref while
473 * clearing.
474 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700476 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 else if (fill > 0) {
479 /* It's a small table with something that needs to be cleared.
480 * Afraid the only safe way is to copy the set entries into
481 * another small table first.
482 */
483 memcpy(small_copy, table, sizeof(small_copy));
484 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700485 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 }
487 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* Now we can finally clear things. If C had refcounts, we could
490 * assert that the refcount on table is 1 now, i.e. that this function
491 * has unique access to it, so decref side-effects can't alter it.
492 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800493 for (entry = table; used > 0; entry++) {
494 if (entry->key && entry->key != dummy) {
495 used--;
496 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (table_is_malloced)
501 PyMem_DEL(table);
502 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503}
504
505/*
506 * Iterate over a set table. Use like so:
507 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000508 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000509 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000510 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000511 * while (set_next(yourset, &pos, &entry)) {
512 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513 * }
514 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000515 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000517 */
518static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000519set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_ssize_t i;
522 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200523 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 assert (PyAnySet_Check(so));
526 i = *pos_ptr;
527 assert(i >= 0);
528 table = so->table;
529 mask = so->mask;
530 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
531 i++;
532 *pos_ptr = i+1;
533 if (i > mask)
534 return 0;
535 assert(table[i].key != NULL);
536 *entry_ptr = &table[i];
537 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000538}
539
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000540static void
541set_dealloc(PySetObject *so)
542{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200543 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800544 Py_ssize_t used = so->used;
545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 PyObject_GC_UnTrack(so);
547 Py_TRASHCAN_SAFE_BEGIN(so)
548 if (so->weakreflist != NULL)
549 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000550
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800551 for (entry = so->table; used > 0; entry++) {
552 if (entry->key && entry->key != dummy) {
553 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700554 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 }
556 }
557 if (so->table != so->smalltable)
558 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700559 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000561}
562
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000563static PyObject *
564set_repr(PySetObject *so)
565{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200566 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 if (status != 0) {
570 if (status < 0)
571 return NULL;
572 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
573 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 /* shortcut for the empty set */
576 if (!so->used) {
577 Py_ReprLeave((PyObject*)so);
578 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
579 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 keys = PySequence_List((PyObject *)so);
582 if (keys == NULL)
583 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000584
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200585 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 listrepr = PyObject_Repr(keys);
587 Py_DECREF(keys);
588 if (listrepr == NULL)
589 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200590 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 if (tmp == NULL)
593 goto done;
594 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200595
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200596 if (Py_TYPE(so) != &PySet_Type)
597 result = PyUnicode_FromFormat("%s({%U})",
598 Py_TYPE(so)->tp_name,
599 listrepr);
600 else
601 result = PyUnicode_FromFormat("{%U}", listrepr);
602 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000603done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 Py_ReprLeave((PyObject*)so);
605 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000606}
607
Martin v. Löwis18e16552006-02-15 17:27:45 +0000608static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000609set_len(PyObject *so)
610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000612}
613
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000614static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000615set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000618 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200619 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700620 setentry *so_entry;
621 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 assert (PyAnySet_Check(so));
624 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 other = (PySetObject*)otherset;
627 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700628 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 return 0;
630 /* Do one big resize at the start, rather than
631 * incrementally resizing as we insert new keys. Expect
632 * that there will be no (or few) overlapping keys.
633 */
634 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
635 if (set_table_resize(so, (so->used + other->used)*2) != 0)
636 return -1;
637 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700638 so_entry = so->table;
639 other_entry = other->table;
640
641 /* If our table is empty, and both tables have the same size, and
642 there are no dummies to eliminate, then just copy the pointers. */
643 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
644 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
645 key = other_entry->key;
646 if (key != NULL) {
647 assert(so_entry->key == NULL);
648 Py_INCREF(key);
649 so_entry->key = key;
650 so_entry->hash = other_entry->hash;
651 }
652 }
653 so->fill = other->fill;
654 so->used = other->used;
655 return 0;
656 }
657
658 /* If our table is empty, we can use set_insert_clean() */
659 if (so->fill == 0) {
660 for (i = 0; i <= other->mask; i++, other_entry++) {
661 key = other_entry->key;
662 if (key != NULL && key != dummy) {
663 Py_INCREF(key);
664 set_insert_clean(so, key, other_entry->hash);
665 }
666 }
667 return 0;
668 }
669
670 /* We can't assure there are no duplicates, so do normal insertions */
671 for (i = 0; i <= other->mask; i++, other_entry++) {
672 key = other_entry->key;
673 if (key != NULL && key != dummy) {
Raymond Hettinger2eff9e92015-06-27 22:03:35 -0700674 if (set_insert_key(so, key, other_entry->hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 }
677 }
678 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000679}
680
681static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000682set_contains_entry(PySetObject *so, setentry *entry)
683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 PyObject *key;
685 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000686
Raymond Hettinger93035c42015-01-25 16:12:49 -0800687 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if (lu_entry == NULL)
689 return -1;
690 key = lu_entry->key;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700691 return key != NULL;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000692}
693
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800694static int
695set_contains_key(PySetObject *so, PyObject *key)
696{
697 setentry entry;
698 Py_hash_t hash;
699
700 if (!PyUnicode_CheckExact(key) ||
701 (hash = ((PyASCIIObject *) key)->hash) == -1) {
702 hash = PyObject_Hash(key);
703 if (hash == -1)
704 return -1;
705 }
706 entry.key = key;
707 entry.hash = hash;
708 return set_contains_entry(so, &entry);
709}
710
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000711static PyObject *
712set_pop(PySetObject *so)
713{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800714 /* Make sure the search finger is in bounds */
715 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200716 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 assert (PyAnySet_Check(so));
720 if (so->used == 0) {
721 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
722 return NULL;
723 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000724
Raymond Hettinger1202a472015-01-18 13:12:42 -0800725 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
726 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800727 if (i > so->mask)
728 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 }
730 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800732 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800734 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000736}
737
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000738PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
739Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000740
741static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000742set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 Py_ssize_t pos = 0;
745 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 while (set_next(so, &pos, &entry))
748 Py_VISIT(entry->key);
749 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000750}
751
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000752static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000753frozenset_hash(PyObject *self)
754{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800755 /* Most of the constants in this hash algorithm are randomly choosen
756 large primes with "interesting bit patterns" and that passed
757 tests for good collision statistics on a variety of problematic
758 datasets such as:
759
760 ps = []
761 for r in range(21):
762 ps += itertools.combinations(range(20), r)
763 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
764
765 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800767 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 setentry *entry;
769 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (so->hash != -1)
772 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000773
Mark Dickinson57e683e2011-09-24 18:18:40 +0100774 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 while (set_next(so, &pos, &entry)) {
776 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800777 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 combinations of a small number of elements with nearby
779 hashes so that many distinct combinations collapse to only
780 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000781 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800782 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800784 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500785 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800786 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200787 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800788 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 so->hash = hash;
790 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000791}
792
Raymond Hettingera9d99362005-08-05 00:01:15 +0000793/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000795typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 PyObject_HEAD
797 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
798 Py_ssize_t si_used;
799 Py_ssize_t si_pos;
800 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801} setiterobject;
802
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000803static void
804setiter_dealloc(setiterobject *si)
805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 Py_XDECREF(si->si_set);
807 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000808}
809
810static int
811setiter_traverse(setiterobject *si, visitproc visit, void *arg)
812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 Py_VISIT(si->si_set);
814 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815}
816
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000817static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818setiter_len(setiterobject *si)
819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 Py_ssize_t len = 0;
821 if (si->si_set != NULL && si->si_used == si->si_set->used)
822 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000823 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824}
825
Armin Rigof5b3e362006-02-11 21:32:43 +0000826PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000827
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000828static PyObject *setiter_iternext(setiterobject *si);
829
830static PyObject *
831setiter_reduce(setiterobject *si)
832{
833 PyObject *list;
834 setiterobject tmp;
835
836 list = PyList_New(0);
837 if (!list)
838 return NULL;
839
Ezio Melotti0e1af282012-09-28 16:43:40 +0300840 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000841 tmp = *si;
842 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300843
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000844 /* iterate the temporary into a list */
845 for(;;) {
846 PyObject *element = setiter_iternext(&tmp);
847 if (element) {
848 if (PyList_Append(list, element)) {
849 Py_DECREF(element);
850 Py_DECREF(list);
851 Py_XDECREF(tmp.si_set);
852 return NULL;
853 }
854 Py_DECREF(element);
855 } else
856 break;
857 }
858 Py_XDECREF(tmp.si_set);
859 /* check for error */
860 if (tmp.si_set != NULL) {
861 /* we have an error */
862 Py_DECREF(list);
863 return NULL;
864 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200865 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000866}
867
868PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
869
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000870static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000872 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000874};
875
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000876static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200879 Py_ssize_t i, mask;
880 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 if (so == NULL)
884 return NULL;
885 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 if (si->si_used != so->used) {
888 PyErr_SetString(PyExc_RuntimeError,
889 "Set changed size during iteration");
890 si->si_used = -1; /* Make this state sticky */
891 return NULL;
892 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 i = si->si_pos;
895 assert(i>=0);
896 entry = so->table;
897 mask = so->mask;
898 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
899 i++;
900 si->si_pos = i+1;
901 if (i > mask)
902 goto fail;
903 si->len--;
904 key = entry[i].key;
905 Py_INCREF(key);
906 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000907
908fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 Py_DECREF(so);
910 si->si_set = NULL;
911 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000912}
913
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000914PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 PyVarObject_HEAD_INIT(&PyType_Type, 0)
916 "set_iterator", /* tp_name */
917 sizeof(setiterobject), /* tp_basicsize */
918 0, /* tp_itemsize */
919 /* methods */
920 (destructor)setiter_dealloc, /* tp_dealloc */
921 0, /* tp_print */
922 0, /* tp_getattr */
923 0, /* tp_setattr */
924 0, /* tp_reserved */
925 0, /* tp_repr */
926 0, /* tp_as_number */
927 0, /* tp_as_sequence */
928 0, /* tp_as_mapping */
929 0, /* tp_hash */
930 0, /* tp_call */
931 0, /* tp_str */
932 PyObject_GenericGetAttr, /* tp_getattro */
933 0, /* tp_setattro */
934 0, /* tp_as_buffer */
935 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
936 0, /* tp_doc */
937 (traverseproc)setiter_traverse, /* tp_traverse */
938 0, /* tp_clear */
939 0, /* tp_richcompare */
940 0, /* tp_weaklistoffset */
941 PyObject_SelfIter, /* tp_iter */
942 (iternextfunc)setiter_iternext, /* tp_iternext */
943 setiter_methods, /* tp_methods */
944 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000945};
946
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000947static PyObject *
948set_iter(PySetObject *so)
949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
951 if (si == NULL)
952 return NULL;
953 Py_INCREF(so);
954 si->si_set = so;
955 si->si_used = so->used;
956 si->si_pos = 0;
957 si->len = so->used;
958 _PyObject_GC_TRACK(si);
959 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000960}
961
Raymond Hettingerd7946662005-08-01 21:39:29 +0000962static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000963set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 if (PyAnySet_Check(other))
968 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 if (PyDict_CheckExact(other)) {
971 PyObject *value;
972 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000973 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 /* Do one big resize at the start, rather than
977 * incrementally resizing as we insert new keys. Expect
978 * that there will be no (or few) overlapping keys.
979 */
980 if (dictsize == -1)
981 return -1;
982 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
983 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
984 return -1;
985 }
986 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
987 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 an_entry.hash = hash;
990 an_entry.key = key;
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700991 if (set_add_entry(so, &an_entry))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 return -1;
993 }
994 return 0;
995 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 it = PyObject_GetIter(other);
998 if (it == NULL)
999 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001002 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Py_DECREF(it);
1004 Py_DECREF(key);
1005 return -1;
1006 }
1007 Py_DECREF(key);
1008 }
1009 Py_DECREF(it);
1010 if (PyErr_Occurred())
1011 return -1;
1012 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001013}
1014
1015static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001016set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1021 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001022 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 return NULL;
1024 }
1025 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001026}
1027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001029"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001030
Raymond Hettinger426d9952014-05-18 21:40:20 +01001031/* XXX Todo:
1032 If aligned memory allocations become available, make the
1033 set object 64 byte aligned so that most of the fields
1034 can be retrieved or updated in a single cache line.
1035*/
1036
Raymond Hettingera38123e2003-11-24 22:18:49 +00001037static PyObject *
1038make_new_set(PyTypeObject *type, PyObject *iterable)
1039{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001040 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001043 so = (PySetObject *)type->tp_alloc(type, 0);
1044 if (so == NULL)
1045 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001046
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001047 so->fill = 0;
1048 so->used = 0;
1049 so->mask = PySet_MINSIZE - 1;
1050 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001051 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -08001052 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001056 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 Py_DECREF(so);
1058 return NULL;
1059 }
1060 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001063}
1064
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001065static PyObject *
1066make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1069 if (PyType_IsSubtype(type, &PySet_Type))
1070 type = &PySet_Type;
1071 else
1072 type = &PyFrozenSet_Type;
1073 }
1074 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001075}
1076
Raymond Hettingerd7946662005-08-01 21:39:29 +00001077/* The empty frozenset is a singleton */
1078static PyObject *emptyfrozenset = NULL;
1079
Raymond Hettingera690a992003-11-16 16:17:49 +00001080static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001081frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1086 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1089 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (type != &PyFrozenSet_Type)
1092 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (iterable != NULL) {
1095 /* frozenset(f) is idempotent */
1096 if (PyFrozenSet_CheckExact(iterable)) {
1097 Py_INCREF(iterable);
1098 return iterable;
1099 }
1100 result = make_new_set(type, iterable);
1101 if (result == NULL || PySet_GET_SIZE(result))
1102 return result;
1103 Py_DECREF(result);
1104 }
1105 /* The empty frozenset is a singleton */
1106 if (emptyfrozenset == NULL)
1107 emptyfrozenset = make_new_set(type, NULL);
1108 Py_XINCREF(emptyfrozenset);
1109 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001110}
1111
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001112int
1113PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001114{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001115 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001116}
1117
1118void
1119PySet_Fini(void)
1120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001122}
1123
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001124static PyObject *
1125set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1128 return NULL;
1129
1130 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001131}
1132
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001133/* set_swap_bodies() switches the contents of any two sets by moving their
1134 internal data pointers and, if needed, copying the internal smalltables.
1135 Semantically equivalent to:
1136
1137 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1138
1139 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001140 Useful for operations that update in-place (by allowing an intermediate
1141 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001142*/
1143
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001144static void
1145set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 Py_ssize_t t;
1148 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001150 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 t = a->fill; a->fill = b->fill; b->fill = t;
1153 t = a->used; a->used = b->used; b->used = t;
1154 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 u = a->table;
1157 if (a->table == a->smalltable)
1158 u = b->smalltable;
1159 a->table = b->table;
1160 if (b->table == b->smalltable)
1161 a->table = a->smalltable;
1162 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 if (a->table == a->smalltable || b->table == b->smalltable) {
1165 memcpy(tab, a->smalltable, sizeof(tab));
1166 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1167 memcpy(b->smalltable, tab, sizeof(tab));
1168 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1171 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1172 h = a->hash; a->hash = b->hash; b->hash = h;
1173 } else {
1174 a->hash = -1;
1175 b->hash = -1;
1176 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001177}
1178
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001179static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001180set_copy(PySetObject *so)
1181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001183}
1184
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001185static PyObject *
1186frozenset_copy(PySetObject *so)
1187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 if (PyFrozenSet_CheckExact(so)) {
1189 Py_INCREF(so);
1190 return (PyObject *)so;
1191 }
1192 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001193}
1194
Raymond Hettingera690a992003-11-16 16:17:49 +00001195PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1196
1197static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001198set_clear(PySetObject *so)
1199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 set_clear_internal(so);
1201 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001202}
1203
1204PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1205
1206static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001207set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 PySetObject *result;
1210 PyObject *other;
1211 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 result = (PySetObject *)set_copy(so);
1214 if (result == NULL)
1215 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1218 other = PyTuple_GET_ITEM(args, i);
1219 if ((PyObject *)so == other)
1220 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001221 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 Py_DECREF(result);
1223 return NULL;
1224 }
1225 }
1226 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001227}
1228
1229PyDoc_STRVAR(union_doc,
1230 "Return the union of sets as a new set.\n\
1231\n\
1232(i.e. all elements that are in either set.)");
1233
1234static PyObject *
1235set_or(PySetObject *so, PyObject *other)
1236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001238
Brian Curtindfc80e32011-08-10 20:28:54 -05001239 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1240 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 result = (PySetObject *)set_copy(so);
1243 if (result == NULL)
1244 return NULL;
1245 if ((PyObject *)so == other)
1246 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001247 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 Py_DECREF(result);
1249 return NULL;
1250 }
1251 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001252}
1253
Raymond Hettingera690a992003-11-16 16:17:49 +00001254static PyObject *
1255set_ior(PySetObject *so, PyObject *other)
1256{
Brian Curtindfc80e32011-08-10 20:28:54 -05001257 if (!PyAnySet_Check(other))
1258 Py_RETURN_NOTIMPLEMENTED;
1259
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001260 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 return NULL;
1262 Py_INCREF(so);
1263 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001264}
1265
1266static PyObject *
1267set_intersection(PySetObject *so, PyObject *other)
1268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 PySetObject *result;
1270 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 if ((PyObject *)so == other)
1273 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1276 if (result == NULL)
1277 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (PyAnySet_Check(other)) {
1280 Py_ssize_t pos = 0;
1281 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1284 tmp = (PyObject *)so;
1285 so = (PySetObject *)other;
1286 other = tmp;
1287 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 while (set_next((PySetObject *)other, &pos, &entry)) {
1290 int rv = set_contains_entry(so, entry);
1291 if (rv == -1) {
1292 Py_DECREF(result);
1293 return NULL;
1294 }
1295 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001296 if (set_add_entry(result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 Py_DECREF(result);
1298 return NULL;
1299 }
1300 }
1301 }
1302 return (PyObject *)result;
1303 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 it = PyObject_GetIter(other);
1306 if (it == NULL) {
1307 Py_DECREF(result);
1308 return NULL;
1309 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 while ((key = PyIter_Next(it)) != NULL) {
1312 int rv;
1313 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001314 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 if (hash == -1) {
1317 Py_DECREF(it);
1318 Py_DECREF(result);
1319 Py_DECREF(key);
1320 return NULL;
1321 }
1322 entry.hash = hash;
1323 entry.key = key;
1324 rv = set_contains_entry(so, &entry);
1325 if (rv == -1) {
1326 Py_DECREF(it);
1327 Py_DECREF(result);
1328 Py_DECREF(key);
1329 return NULL;
1330 }
1331 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001332 if (set_add_entry(result, &entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 Py_DECREF(it);
1334 Py_DECREF(result);
1335 Py_DECREF(key);
1336 return NULL;
1337 }
1338 }
1339 Py_DECREF(key);
1340 }
1341 Py_DECREF(it);
1342 if (PyErr_Occurred()) {
1343 Py_DECREF(result);
1344 return NULL;
1345 }
1346 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001347}
1348
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001349static PyObject *
1350set_intersection_multi(PySetObject *so, PyObject *args)
1351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 Py_ssize_t i;
1353 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 if (PyTuple_GET_SIZE(args) == 0)
1356 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 Py_INCREF(so);
1359 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1360 PyObject *other = PyTuple_GET_ITEM(args, i);
1361 PyObject *newresult = set_intersection((PySetObject *)result, other);
1362 if (newresult == NULL) {
1363 Py_DECREF(result);
1364 return NULL;
1365 }
1366 Py_DECREF(result);
1367 result = newresult;
1368 }
1369 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001370}
1371
Raymond Hettingera690a992003-11-16 16:17:49 +00001372PyDoc_STRVAR(intersection_doc,
1373"Return the intersection of two sets as a new set.\n\
1374\n\
1375(i.e. all elements that are in both sets.)");
1376
1377static PyObject *
1378set_intersection_update(PySetObject *so, PyObject *other)
1379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 tmp = set_intersection(so, other);
1383 if (tmp == NULL)
1384 return NULL;
1385 set_swap_bodies(so, (PySetObject *)tmp);
1386 Py_DECREF(tmp);
1387 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001388}
1389
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001390static PyObject *
1391set_intersection_update_multi(PySetObject *so, PyObject *args)
1392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 tmp = set_intersection_multi(so, args);
1396 if (tmp == NULL)
1397 return NULL;
1398 set_swap_bodies(so, (PySetObject *)tmp);
1399 Py_DECREF(tmp);
1400 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001401}
1402
Raymond Hettingera690a992003-11-16 16:17:49 +00001403PyDoc_STRVAR(intersection_update_doc,
1404"Update a set with the intersection of itself and another.");
1405
1406static PyObject *
1407set_and(PySetObject *so, PyObject *other)
1408{
Brian Curtindfc80e32011-08-10 20:28:54 -05001409 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1410 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001412}
1413
1414static PyObject *
1415set_iand(PySetObject *so, PyObject *other)
1416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001418
Brian Curtindfc80e32011-08-10 20:28:54 -05001419 if (!PyAnySet_Check(other))
1420 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 result = set_intersection_update(so, other);
1422 if (result == NULL)
1423 return NULL;
1424 Py_DECREF(result);
1425 Py_INCREF(so);
1426 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001427}
1428
Guido van Rossum58da9312007-11-10 23:39:45 +00001429static PyObject *
1430set_isdisjoint(PySetObject *so, PyObject *other)
1431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if ((PyObject *)so == other) {
1435 if (PySet_GET_SIZE(so) == 0)
1436 Py_RETURN_TRUE;
1437 else
1438 Py_RETURN_FALSE;
1439 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 if (PyAnySet_CheckExact(other)) {
1442 Py_ssize_t pos = 0;
1443 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1446 tmp = (PyObject *)so;
1447 so = (PySetObject *)other;
1448 other = tmp;
1449 }
1450 while (set_next((PySetObject *)other, &pos, &entry)) {
1451 int rv = set_contains_entry(so, entry);
1452 if (rv == -1)
1453 return NULL;
1454 if (rv)
1455 Py_RETURN_FALSE;
1456 }
1457 Py_RETURN_TRUE;
1458 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 it = PyObject_GetIter(other);
1461 if (it == NULL)
1462 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 while ((key = PyIter_Next(it)) != NULL) {
1465 int rv;
1466 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001467 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 if (hash == -1) {
1470 Py_DECREF(key);
1471 Py_DECREF(it);
1472 return NULL;
1473 }
1474 entry.hash = hash;
1475 entry.key = key;
1476 rv = set_contains_entry(so, &entry);
1477 Py_DECREF(key);
1478 if (rv == -1) {
1479 Py_DECREF(it);
1480 return NULL;
1481 }
1482 if (rv) {
1483 Py_DECREF(it);
1484 Py_RETURN_FALSE;
1485 }
1486 }
1487 Py_DECREF(it);
1488 if (PyErr_Occurred())
1489 return NULL;
1490 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001491}
1492
1493PyDoc_STRVAR(isdisjoint_doc,
1494"Return True if two sets have a null intersection.");
1495
Neal Norwitz6576bd82005-11-13 18:41:28 +00001496static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001497set_difference_update_internal(PySetObject *so, PyObject *other)
1498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if ((PyObject *)so == other)
1500 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 if (PyAnySet_Check(other)) {
1503 setentry *entry;
1504 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 while (set_next((PySetObject *)other, &pos, &entry))
1507 if (set_discard_entry(so, entry) == -1)
1508 return -1;
1509 } else {
1510 PyObject *key, *it;
1511 it = PyObject_GetIter(other);
1512 if (it == NULL)
1513 return -1;
1514
1515 while ((key = PyIter_Next(it)) != NULL) {
1516 if (set_discard_key(so, key) == -1) {
1517 Py_DECREF(it);
1518 Py_DECREF(key);
1519 return -1;
1520 }
1521 Py_DECREF(key);
1522 }
1523 Py_DECREF(it);
1524 if (PyErr_Occurred())
1525 return -1;
1526 }
1527 /* If more than 1/5 are dummies, then resize them away. */
1528 if ((so->fill - so->used) * 5 < so->mask)
1529 return 0;
1530 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001531}
1532
Raymond Hettingera690a992003-11-16 16:17:49 +00001533static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001534set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1539 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001540 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 return NULL;
1542 }
1543 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001544}
1545
1546PyDoc_STRVAR(difference_update_doc,
1547"Remove all elements of another set from this set.");
1548
1549static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001550set_copy_and_difference(PySetObject *so, PyObject *other)
1551{
1552 PyObject *result;
1553
1554 result = set_copy(so);
1555 if (result == NULL)
1556 return NULL;
1557 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1558 return result;
1559 Py_DECREF(result);
1560 return NULL;
1561}
1562
1563static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001564set_difference(PySetObject *so, PyObject *other)
1565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 PyObject *result;
1567 setentry *entry;
1568 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001571 return set_copy_and_difference(so, other);
1572 }
1573
1574 /* If len(so) much more than len(other), it's more efficient to simply copy
1575 * so and then iterate other looking for common elements. */
1576 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1577 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 result = make_new_set_basetype(Py_TYPE(so), NULL);
1581 if (result == NULL)
1582 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 if (PyDict_CheckExact(other)) {
1585 while (set_next(so, &pos, &entry)) {
1586 setentry entrycopy;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001587 int rv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 entrycopy.hash = entry->hash;
1589 entrycopy.key = entry->key;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001590 rv = _PyDict_Contains(other, entry->key, entry->hash);
1591 if (rv < 0) {
1592 Py_DECREF(result);
1593 return NULL;
1594 }
1595 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001596 if (set_add_entry((PySetObject *)result, &entrycopy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 Py_DECREF(result);
1598 return NULL;
1599 }
1600 }
1601 }
1602 return result;
1603 }
1604
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001605 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 while (set_next(so, &pos, &entry)) {
1607 int rv = set_contains_entry((PySetObject *)other, entry);
1608 if (rv == -1) {
1609 Py_DECREF(result);
1610 return NULL;
1611 }
1612 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001613 if (set_add_entry((PySetObject *)result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 Py_DECREF(result);
1615 return NULL;
1616 }
1617 }
1618 }
1619 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001620}
1621
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001622static PyObject *
1623set_difference_multi(PySetObject *so, PyObject *args)
1624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 Py_ssize_t i;
1626 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 if (PyTuple_GET_SIZE(args) == 0)
1629 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 other = PyTuple_GET_ITEM(args, 0);
1632 result = set_difference(so, other);
1633 if (result == NULL)
1634 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1637 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001638 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 Py_DECREF(result);
1640 return NULL;
1641 }
1642 }
1643 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001644}
1645
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001646PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001647"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001648\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001649(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001650static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001651set_sub(PySetObject *so, PyObject *other)
1652{
Brian Curtindfc80e32011-08-10 20:28:54 -05001653 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1654 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001656}
1657
1658static PyObject *
1659set_isub(PySetObject *so, PyObject *other)
1660{
Brian Curtindfc80e32011-08-10 20:28:54 -05001661 if (!PyAnySet_Check(other))
1662 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001663 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 return NULL;
1665 Py_INCREF(so);
1666 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001667}
1668
1669static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001670set_symmetric_difference_update(PySetObject *so, PyObject *other)
1671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 PySetObject *otherset;
1673 PyObject *key;
1674 Py_ssize_t pos = 0;
1675 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 if ((PyObject *)so == other)
1678 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 if (PyDict_CheckExact(other)) {
1681 PyObject *value;
1682 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001683 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1685 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001686
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001687 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 an_entry.hash = hash;
1689 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001692 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001693 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001696 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001697 if (set_add_entry(so, &an_entry)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001698 Py_DECREF(key);
1699 return NULL;
1700 }
1701 }
Georg Brandl2d444492010-09-03 10:52:55 +00001702 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 }
1704 Py_RETURN_NONE;
1705 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 if (PyAnySet_Check(other)) {
1708 Py_INCREF(other);
1709 otherset = (PySetObject *)other;
1710 } else {
1711 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1712 if (otherset == NULL)
1713 return NULL;
1714 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 while (set_next(otherset, &pos, &entry)) {
1717 int rv = set_discard_entry(so, entry);
1718 if (rv == -1) {
1719 Py_DECREF(otherset);
1720 return NULL;
1721 }
1722 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001723 if (set_add_entry(so, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 Py_DECREF(otherset);
1725 return NULL;
1726 }
1727 }
1728 }
1729 Py_DECREF(otherset);
1730 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001731}
1732
1733PyDoc_STRVAR(symmetric_difference_update_doc,
1734"Update a set with the symmetric difference of itself and another.");
1735
1736static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001737set_symmetric_difference(PySetObject *so, PyObject *other)
1738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 PyObject *rv;
1740 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1743 if (otherset == NULL)
1744 return NULL;
1745 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1746 if (rv == NULL)
1747 return NULL;
1748 Py_DECREF(rv);
1749 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001750}
1751
1752PyDoc_STRVAR(symmetric_difference_doc,
1753"Return the symmetric difference of two sets as a new set.\n\
1754\n\
1755(i.e. all elements that are in exactly one of the sets.)");
1756
1757static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001758set_xor(PySetObject *so, PyObject *other)
1759{
Brian Curtindfc80e32011-08-10 20:28:54 -05001760 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1761 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001763}
1764
1765static PyObject *
1766set_ixor(PySetObject *so, PyObject *other)
1767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001769
Brian Curtindfc80e32011-08-10 20:28:54 -05001770 if (!PyAnySet_Check(other))
1771 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 result = set_symmetric_difference_update(so, other);
1773 if (result == NULL)
1774 return NULL;
1775 Py_DECREF(result);
1776 Py_INCREF(so);
1777 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001778}
1779
1780static PyObject *
1781set_issubset(PySetObject *so, PyObject *other)
1782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 setentry *entry;
1784 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 if (!PyAnySet_Check(other)) {
1787 PyObject *tmp, *result;
1788 tmp = make_new_set(&PySet_Type, other);
1789 if (tmp == NULL)
1790 return NULL;
1791 result = set_issubset(so, tmp);
1792 Py_DECREF(tmp);
1793 return result;
1794 }
1795 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1796 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 while (set_next(so, &pos, &entry)) {
1799 int rv = set_contains_entry((PySetObject *)other, entry);
1800 if (rv == -1)
1801 return NULL;
1802 if (!rv)
1803 Py_RETURN_FALSE;
1804 }
1805 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001806}
1807
1808PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1809
1810static PyObject *
1811set_issuperset(PySetObject *so, PyObject *other)
1812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 if (!PyAnySet_Check(other)) {
1816 tmp = make_new_set(&PySet_Type, other);
1817 if (tmp == NULL)
1818 return NULL;
1819 result = set_issuperset(so, tmp);
1820 Py_DECREF(tmp);
1821 return result;
1822 }
1823 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001824}
1825
1826PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1827
Raymond Hettingera690a992003-11-16 16:17:49 +00001828static PyObject *
1829set_richcompare(PySetObject *v, PyObject *w, int op)
1830{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001831 PyObject *r1;
1832 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001833
Brian Curtindfc80e32011-08-10 20:28:54 -05001834 if(!PyAnySet_Check(w))
1835 Py_RETURN_NOTIMPLEMENTED;
1836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 switch (op) {
1838 case Py_EQ:
1839 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1840 Py_RETURN_FALSE;
1841 if (v->hash != -1 &&
1842 ((PySetObject *)w)->hash != -1 &&
1843 v->hash != ((PySetObject *)w)->hash)
1844 Py_RETURN_FALSE;
1845 return set_issubset(v, w);
1846 case Py_NE:
1847 r1 = set_richcompare(v, w, Py_EQ);
1848 if (r1 == NULL)
1849 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001850 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001852 if (r2 < 0)
1853 return NULL;
1854 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 case Py_LE:
1856 return set_issubset(v, w);
1857 case Py_GE:
1858 return set_issuperset(v, w);
1859 case Py_LT:
1860 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1861 Py_RETURN_FALSE;
1862 return set_issubset(v, w);
1863 case Py_GT:
1864 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1865 Py_RETURN_FALSE;
1866 return set_issuperset(v, w);
1867 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001868 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001869}
1870
Raymond Hettingera690a992003-11-16 16:17:49 +00001871static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001872set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001873{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001874 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 return NULL;
1876 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001877}
1878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001880"Add an element to a set.\n\
1881\n\
1882This has no effect if the element is already present.");
1883
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001884static int
1885set_contains(PySetObject *so, PyObject *key)
1886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 PyObject *tmpkey;
1888 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 rv = set_contains_key(so, key);
1891 if (rv == -1) {
1892 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1893 return -1;
1894 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001895 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 if (tmpkey == NULL)
1897 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001898 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 Py_DECREF(tmpkey);
1900 }
1901 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001902}
1903
1904static PyObject *
1905set_direct_contains(PySetObject *so, PyObject *key)
1906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 result = set_contains(so, key);
1910 if (result == -1)
1911 return NULL;
1912 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001913}
1914
1915PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1916
Raymond Hettingera690a992003-11-16 16:17:49 +00001917static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001918set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 PyObject *tmpkey;
1921 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 rv = set_discard_key(so, key);
1924 if (rv == -1) {
1925 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1926 return NULL;
1927 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001928 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 if (tmpkey == NULL)
1930 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 Py_DECREF(tmpkey);
1933 if (rv == -1)
1934 return NULL;
1935 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001938 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 return NULL;
1940 }
1941 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001942}
1943
1944PyDoc_STRVAR(remove_doc,
1945"Remove an element from a set; it must be a member.\n\
1946\n\
1947If the element is not a member, raise a KeyError.");
1948
1949static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001950set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001951{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001952 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 rv = set_discard_key(so, key);
1956 if (rv == -1) {
1957 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1958 return NULL;
1959 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001960 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (tmpkey == NULL)
1962 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001963 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001965 if (rv == -1)
1966 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 }
1968 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001969}
1970
1971PyDoc_STRVAR(discard_doc,
1972"Remove an element from a set if it is a member.\n\
1973\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001975
1976static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001977set_reduce(PySetObject *so)
1978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001980 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 keys = PySequence_List((PyObject *)so);
1983 if (keys == NULL)
1984 goto done;
1985 args = PyTuple_Pack(1, keys);
1986 if (args == NULL)
1987 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001988 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 if (dict == NULL) {
1990 PyErr_Clear();
1991 dict = Py_None;
1992 Py_INCREF(dict);
1993 }
1994 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001995done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 Py_XDECREF(args);
1997 Py_XDECREF(keys);
1998 Py_XDECREF(dict);
1999 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002000}
2001
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002002static PyObject *
2003set_sizeof(PySetObject *so)
2004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 res = sizeof(PySetObject);
2008 if (so->table != so->smalltable)
2009 res = res + (so->mask + 1) * sizeof(setentry);
2010 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002011}
2012
2013PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002014static int
2015set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 if (!PyAnySet_Check(self))
2020 return -1;
2021 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2022 return -1;
2023 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2024 return -1;
2025 set_clear_internal(self);
2026 self->hash = -1;
2027 if (iterable == NULL)
2028 return 0;
2029 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002030}
2031
Raymond Hettingera690a992003-11-16 16:17:49 +00002032static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 set_len, /* sq_length */
2034 0, /* sq_concat */
2035 0, /* sq_repeat */
2036 0, /* sq_item */
2037 0, /* sq_slice */
2038 0, /* sq_ass_item */
2039 0, /* sq_ass_slice */
2040 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002041};
2042
2043/* set object ********************************************************/
2044
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002045#ifdef Py_DEBUG
2046static PyObject *test_c_api(PySetObject *so);
2047
2048PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2049All is well if assertions don't fail.");
2050#endif
2051
Raymond Hettingera690a992003-11-16 16:17:49 +00002052static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 {"add", (PyCFunction)set_add, METH_O,
2054 add_doc},
2055 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2056 clear_doc},
2057 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2058 contains_doc},
2059 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2060 copy_doc},
2061 {"discard", (PyCFunction)set_discard, METH_O,
2062 discard_doc},
2063 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2064 difference_doc},
2065 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2066 difference_update_doc},
2067 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2068 intersection_doc},
2069 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2070 intersection_update_doc},
2071 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2072 isdisjoint_doc},
2073 {"issubset", (PyCFunction)set_issubset, METH_O,
2074 issubset_doc},
2075 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2076 issuperset_doc},
2077 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2078 pop_doc},
2079 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2080 reduce_doc},
2081 {"remove", (PyCFunction)set_remove, METH_O,
2082 remove_doc},
2083 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2084 sizeof_doc},
2085 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2086 symmetric_difference_doc},
2087 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2088 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002089#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2091 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002092#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 {"union", (PyCFunction)set_union, METH_VARARGS,
2094 union_doc},
2095 {"update", (PyCFunction)set_update, METH_VARARGS,
2096 update_doc},
2097 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002098};
2099
2100static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 0, /*nb_add*/
2102 (binaryfunc)set_sub, /*nb_subtract*/
2103 0, /*nb_multiply*/
2104 0, /*nb_remainder*/
2105 0, /*nb_divmod*/
2106 0, /*nb_power*/
2107 0, /*nb_negative*/
2108 0, /*nb_positive*/
2109 0, /*nb_absolute*/
2110 0, /*nb_bool*/
2111 0, /*nb_invert*/
2112 0, /*nb_lshift*/
2113 0, /*nb_rshift*/
2114 (binaryfunc)set_and, /*nb_and*/
2115 (binaryfunc)set_xor, /*nb_xor*/
2116 (binaryfunc)set_or, /*nb_or*/
2117 0, /*nb_int*/
2118 0, /*nb_reserved*/
2119 0, /*nb_float*/
2120 0, /*nb_inplace_add*/
2121 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2122 0, /*nb_inplace_multiply*/
2123 0, /*nb_inplace_remainder*/
2124 0, /*nb_inplace_power*/
2125 0, /*nb_inplace_lshift*/
2126 0, /*nb_inplace_rshift*/
2127 (binaryfunc)set_iand, /*nb_inplace_and*/
2128 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2129 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002130};
2131
2132PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002133"set() -> new empty set object\n\
2134set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002135\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002136Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002137
2138PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2140 "set", /* tp_name */
2141 sizeof(PySetObject), /* tp_basicsize */
2142 0, /* tp_itemsize */
2143 /* methods */
2144 (destructor)set_dealloc, /* tp_dealloc */
2145 0, /* tp_print */
2146 0, /* tp_getattr */
2147 0, /* tp_setattr */
2148 0, /* tp_reserved */
2149 (reprfunc)set_repr, /* tp_repr */
2150 &set_as_number, /* tp_as_number */
2151 &set_as_sequence, /* tp_as_sequence */
2152 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002153 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 0, /* tp_call */
2155 0, /* tp_str */
2156 PyObject_GenericGetAttr, /* tp_getattro */
2157 0, /* tp_setattro */
2158 0, /* tp_as_buffer */
2159 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2160 Py_TPFLAGS_BASETYPE, /* tp_flags */
2161 set_doc, /* tp_doc */
2162 (traverseproc)set_traverse, /* tp_traverse */
2163 (inquiry)set_clear_internal, /* tp_clear */
2164 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002165 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2166 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 0, /* tp_iternext */
2168 set_methods, /* tp_methods */
2169 0, /* tp_members */
2170 0, /* tp_getset */
2171 0, /* tp_base */
2172 0, /* tp_dict */
2173 0, /* tp_descr_get */
2174 0, /* tp_descr_set */
2175 0, /* tp_dictoffset */
2176 (initproc)set_init, /* tp_init */
2177 PyType_GenericAlloc, /* tp_alloc */
2178 set_new, /* tp_new */
2179 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002180};
2181
2182/* frozenset object ********************************************************/
2183
2184
2185static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2187 contains_doc},
2188 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2189 copy_doc},
2190 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2191 difference_doc},
2192 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2193 intersection_doc},
2194 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2195 isdisjoint_doc},
2196 {"issubset", (PyCFunction)set_issubset, METH_O,
2197 issubset_doc},
2198 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2199 issuperset_doc},
2200 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2201 reduce_doc},
2202 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2203 sizeof_doc},
2204 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2205 symmetric_difference_doc},
2206 {"union", (PyCFunction)set_union, METH_VARARGS,
2207 union_doc},
2208 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002209};
2210
2211static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 0, /*nb_add*/
2213 (binaryfunc)set_sub, /*nb_subtract*/
2214 0, /*nb_multiply*/
2215 0, /*nb_remainder*/
2216 0, /*nb_divmod*/
2217 0, /*nb_power*/
2218 0, /*nb_negative*/
2219 0, /*nb_positive*/
2220 0, /*nb_absolute*/
2221 0, /*nb_bool*/
2222 0, /*nb_invert*/
2223 0, /*nb_lshift*/
2224 0, /*nb_rshift*/
2225 (binaryfunc)set_and, /*nb_and*/
2226 (binaryfunc)set_xor, /*nb_xor*/
2227 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002228};
2229
2230PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002231"frozenset() -> empty frozenset object\n\
2232frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002233\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002234Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002235
2236PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2238 "frozenset", /* tp_name */
2239 sizeof(PySetObject), /* tp_basicsize */
2240 0, /* tp_itemsize */
2241 /* methods */
2242 (destructor)set_dealloc, /* tp_dealloc */
2243 0, /* tp_print */
2244 0, /* tp_getattr */
2245 0, /* tp_setattr */
2246 0, /* tp_reserved */
2247 (reprfunc)set_repr, /* tp_repr */
2248 &frozenset_as_number, /* tp_as_number */
2249 &set_as_sequence, /* tp_as_sequence */
2250 0, /* tp_as_mapping */
2251 frozenset_hash, /* tp_hash */
2252 0, /* tp_call */
2253 0, /* tp_str */
2254 PyObject_GenericGetAttr, /* tp_getattro */
2255 0, /* tp_setattro */
2256 0, /* tp_as_buffer */
2257 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2258 Py_TPFLAGS_BASETYPE, /* tp_flags */
2259 frozenset_doc, /* tp_doc */
2260 (traverseproc)set_traverse, /* tp_traverse */
2261 (inquiry)set_clear_internal, /* tp_clear */
2262 (richcmpfunc)set_richcompare, /* tp_richcompare */
2263 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2264 (getiterfunc)set_iter, /* tp_iter */
2265 0, /* tp_iternext */
2266 frozenset_methods, /* tp_methods */
2267 0, /* tp_members */
2268 0, /* tp_getset */
2269 0, /* tp_base */
2270 0, /* tp_dict */
2271 0, /* tp_descr_get */
2272 0, /* tp_descr_set */
2273 0, /* tp_dictoffset */
2274 0, /* tp_init */
2275 PyType_GenericAlloc, /* tp_alloc */
2276 frozenset_new, /* tp_new */
2277 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002278};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002279
2280
2281/***** C API functions *************************************************/
2282
2283PyObject *
2284PySet_New(PyObject *iterable)
2285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002287}
2288
2289PyObject *
2290PyFrozenSet_New(PyObject *iterable)
2291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002293}
2294
Neal Norwitz8c49c822006-03-04 18:41:19 +00002295Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002296PySet_Size(PyObject *anyset)
2297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 if (!PyAnySet_Check(anyset)) {
2299 PyErr_BadInternalCall();
2300 return -1;
2301 }
2302 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002303}
2304
2305int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002306PySet_Clear(PyObject *set)
2307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 if (!PySet_Check(set)) {
2309 PyErr_BadInternalCall();
2310 return -1;
2311 }
2312 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002313}
2314
2315int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002316PySet_Contains(PyObject *anyset, PyObject *key)
2317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 if (!PyAnySet_Check(anyset)) {
2319 PyErr_BadInternalCall();
2320 return -1;
2321 }
2322 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002323}
2324
2325int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002326PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 if (!PySet_Check(set)) {
2329 PyErr_BadInternalCall();
2330 return -1;
2331 }
2332 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002333}
2334
2335int
Christian Heimesfd66e512008-01-29 12:18:50 +00002336PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 if (!PySet_Check(anyset) &&
2339 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2340 PyErr_BadInternalCall();
2341 return -1;
2342 }
2343 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002344}
2345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002346int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002347_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 if (!PyAnySet_Check(set)) {
2352 PyErr_BadInternalCall();
2353 return -1;
2354 }
2355 if (set_next((PySetObject *)set, pos, &entry) == 0)
2356 return 0;
2357 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002358 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002360}
2361
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002362PyObject *
2363PySet_Pop(PyObject *set)
2364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 if (!PySet_Check(set)) {
2366 PyErr_BadInternalCall();
2367 return NULL;
2368 }
2369 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002370}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002371
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002372int
2373_PySet_Update(PyObject *set, PyObject *iterable)
2374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 if (!PySet_Check(set)) {
2376 PyErr_BadInternalCall();
2377 return -1;
2378 }
2379 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002380}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002381
Raymond Hettingere259f132013-12-15 11:56:14 -08002382/* Exported for the gdb plugin's benefit. */
2383PyObject *_PySet_Dummy = dummy;
2384
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002385#ifdef Py_DEBUG
2386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002388 Returns True and original set is restored. */
2389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390#define assertRaises(call_return_value, exception) \
2391 do { \
2392 assert(call_return_value); \
2393 assert(PyErr_ExceptionMatches(exception)); \
2394 PyErr_Clear(); \
2395 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002396
2397static PyObject *
2398test_c_api(PySetObject *so)
2399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 Py_ssize_t count;
2401 char *s;
2402 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002403 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002405 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 /* Verify preconditions */
2409 assert(PyAnySet_Check(ob));
2410 assert(PyAnySet_CheckExact(ob));
2411 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* so.clear(); so |= set("abc"); */
2414 str = PyUnicode_FromString("abc");
2415 if (str == NULL)
2416 return NULL;
2417 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002418 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 Py_DECREF(str);
2420 return NULL;
2421 }
2422 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* Exercise type/size checks */
2425 assert(PySet_Size(ob) == 3);
2426 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 /* Raise TypeError for non-iterable constructor arguments */
2429 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2430 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 /* Raise TypeError for unhashable key */
2433 dup = PySet_New(ob);
2434 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2435 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2436 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 /* Exercise successful pop, contains, add, and discard */
2439 elem = PySet_Pop(ob);
2440 assert(PySet_Contains(ob, elem) == 0);
2441 assert(PySet_GET_SIZE(ob) == 2);
2442 assert(PySet_Add(ob, elem) == 0);
2443 assert(PySet_Contains(ob, elem) == 1);
2444 assert(PySet_GET_SIZE(ob) == 3);
2445 assert(PySet_Discard(ob, elem) == 1);
2446 assert(PySet_GET_SIZE(ob) == 2);
2447 assert(PySet_Discard(ob, elem) == 0);
2448 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 /* Exercise clear */
2451 dup2 = PySet_New(dup);
2452 assert(PySet_Clear(dup2) == 0);
2453 assert(PySet_Size(dup2) == 0);
2454 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 /* Raise SystemError on clear or update of frozen set */
2457 f = PyFrozenSet_New(dup);
2458 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2459 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2460 assert(PySet_Add(f, elem) == 0);
2461 Py_INCREF(f);
2462 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2463 Py_DECREF(f);
2464 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 /* Exercise direct iteration */
2467 i = 0, count = 0;
2468 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2469 s = _PyUnicode_AsString(x);
2470 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2471 count++;
2472 }
2473 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 /* Exercise updates */
2476 dup2 = PySet_New(NULL);
2477 assert(_PySet_Update(dup2, dup) == 0);
2478 assert(PySet_Size(dup2) == 3);
2479 assert(_PySet_Update(dup2, dup) == 0);
2480 assert(PySet_Size(dup2) == 3);
2481 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 /* Raise SystemError when self argument is not a set or frozenset. */
2484 t = PyTuple_New(0);
2485 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2486 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2487 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 /* Raise SystemError when self argument is not a set. */
2490 f = PyFrozenSet_New(dup);
2491 assert(PySet_Size(f) == 3);
2492 assert(PyFrozenSet_CheckExact(f));
2493 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2494 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2495 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 /* Raise KeyError when popping from an empty set */
2498 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2499 Py_DECREF(ob);
2500 assert(PySet_GET_SIZE(ob) == 0);
2501 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 /* Restore the set from the copy using the PyNumber API */
2504 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2505 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 /* Verify constructors accept NULL arguments */
2508 f = PySet_New(NULL);
2509 assert(f != NULL);
2510 assert(PySet_GET_SIZE(f) == 0);
2511 Py_DECREF(f);
2512 f = PyFrozenSet_New(NULL);
2513 assert(f != NULL);
2514 assert(PyFrozenSet_CheckExact(f));
2515 assert(PySet_GET_SIZE(f) == 0);
2516 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 Py_DECREF(elem);
2519 Py_DECREF(dup);
2520 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002521}
2522
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002523#undef assertRaises
2524
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002525#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002526
2527/***** Dummy Struct *************************************************/
2528
2529static PyObject *
2530dummy_repr(PyObject *op)
2531{
2532 return PyUnicode_FromString("<dummy key>");
2533}
2534
2535static void
2536dummy_dealloc(PyObject* ignore)
2537{
2538 Py_FatalError("deallocating <dummy key>");
2539}
2540
2541static PyTypeObject _PySetDummy_Type = {
2542 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2543 "<dummy key> type",
2544 0,
2545 0,
2546 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2547 0, /*tp_print*/
2548 0, /*tp_getattr*/
2549 0, /*tp_setattr*/
2550 0, /*tp_reserved*/
2551 dummy_repr, /*tp_repr*/
2552 0, /*tp_as_number*/
2553 0, /*tp_as_sequence*/
2554 0, /*tp_as_mapping*/
2555 0, /*tp_hash */
2556 0, /*tp_call */
2557 0, /*tp_str */
2558 0, /*tp_getattro */
2559 0, /*tp_setattro */
2560 0, /*tp_as_buffer */
2561 Py_TPFLAGS_DEFAULT, /*tp_flags */
2562};
2563
2564static PyObject _dummy_struct = {
2565 _PyObject_EXTRA_INIT
2566 2, &_PySetDummy_Type
2567};
2568