blob: 56084c1d0bc3670e5573933ecbed2728ca9a8716 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettingerbd9b2002015-01-18 16:10:30 -08007 Copyright (c) 2003-2015 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
Raymond Hettinger4d45c102015-01-25 16:27:40 -080026 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070027 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020055 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070056 size_t perturb;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080058 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070059 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070060 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080062 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070063 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070065
Raymond Hettinger38bb95e2015-06-24 01:22:19 -070066 perturb = hash;
67
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070068 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080069 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070070 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080071 /* startkey cannot be a dummy because the dummy hash field is -1 */
72 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080073 if (startkey == key)
74 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080075 if (PyUnicode_CheckExact(startkey)
76 && PyUnicode_CheckExact(key)
77 && unicode_eq(startkey, key))
78 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 Py_INCREF(startkey);
80 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
81 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010082 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010084 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010086 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070087 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060088 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070089 }
Raymond Hettingerafe89092013-08-28 20:59:31 -070090
Raymond Hettingerc658d852015-02-02 08:35:00 -080091 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080092 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080093 entry++;
Raymond Hettinger8651a502015-05-27 10:37:20 -070094 if (entry->hash == 0 && entry->key == NULL)
95 return entry;
Raymond Hettingerc658d852015-02-02 08:35:00 -080096 if (entry->hash == hash) {
97 PyObject *startkey = entry->key;
98 assert(startkey != dummy);
99 if (startkey == key)
100 return entry;
101 if (PyUnicode_CheckExact(startkey)
102 && PyUnicode_CheckExact(key)
103 && unicode_eq(startkey, key))
104 return entry;
105 Py_INCREF(startkey);
106 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
107 Py_DECREF(startkey);
108 if (cmp < 0)
109 return NULL;
110 if (table != so->table || entry->key != startkey)
111 return set_lookkey(so, key, hash);
112 if (cmp > 0)
113 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600114 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800115 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700116 }
117 }
118
119 perturb >>= PERTURB_SHIFT;
120 i = (i * 5 + 1 + perturb) & mask;
121
122 entry = &table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700123 if (entry->key == NULL)
Raymond Hettinger8651a502015-05-27 10:37:20 -0700124 return entry;
125 }
126}
127
Raymond Hettinger15f08692015-07-03 17:21:17 -0700128static int set_table_resize(PySetObject *, Py_ssize_t);
129
Raymond Hettinger8651a502015-05-27 10:37:20 -0700130static int
131set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
132{
133 setentry *table = so->table;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700134 setentry *freeslot;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700135 setentry *entry;
Raymond Hettinger38bb95e2015-06-24 01:22:19 -0700136 size_t perturb;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700137 size_t mask = so->mask;
138 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
139 size_t j;
140 int cmp;
141
142 entry = &table[i];
143 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700144 goto found_unused;
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700145
146 freeslot = NULL;
147 perturb = hash;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700148
149 while (1) {
150 if (entry->hash == hash) {
151 PyObject *startkey = entry->key;
152 /* startkey cannot be a dummy because the dummy hash field is -1 */
153 assert(startkey != dummy);
154 if (startkey == key)
155 goto found_active;
156 if (PyUnicode_CheckExact(startkey)
157 && PyUnicode_CheckExact(key)
158 && unicode_eq(startkey, key))
159 goto found_active;
160 Py_INCREF(startkey);
161 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
162 Py_DECREF(startkey);
163 if (cmp < 0) /* unlikely */
164 return -1;
165 if (table != so->table || entry->key != startkey) /* unlikely */
166 return set_insert_key(so, key, hash);
167 if (cmp > 0) /* likely */
168 goto found_active;
169 mask = so->mask; /* help avoid a register spill */
170 }
171 if (entry->hash == -1 && freeslot == NULL)
172 freeslot = entry;
173
174 if (i + LINEAR_PROBES <= mask) {
175 for (j = 0 ; j < LINEAR_PROBES ; j++) {
176 entry++;
177 if (entry->hash == 0 && entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700178 goto found_unused_or_dummy;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700179 if (entry->hash == hash) {
180 PyObject *startkey = entry->key;
181 assert(startkey != dummy);
182 if (startkey == key)
183 goto found_active;
184 if (PyUnicode_CheckExact(startkey)
185 && PyUnicode_CheckExact(key)
186 && unicode_eq(startkey, key))
187 goto found_active;
188 Py_INCREF(startkey);
189 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
190 Py_DECREF(startkey);
191 if (cmp < 0)
192 return -1;
193 if (table != so->table || entry->key != startkey)
194 return set_insert_key(so, key, hash);
195 if (cmp > 0)
196 goto found_active;
197 mask = so->mask;
198 }
Raymond Hettinger91672612015-06-26 02:50:21 -0700199 else if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800200 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700203
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700204 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800205 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700206
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800207 entry = &table[i];
Raymond Hettinger7e3592d2015-06-21 10:47:20 -0700208 if (entry->key == NULL)
Raymond Hettinger15f08692015-07-03 17:21:17 -0700209 goto found_unused_or_dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700211
Raymond Hettinger15f08692015-07-03 17:21:17 -0700212 found_unused_or_dummy:
213 if (freeslot == NULL)
214 goto found_unused;
215 Py_INCREF(key);
216 so->used++;
217 freeslot->key = key;
218 freeslot->hash = hash;
219 return 0;
220
221 found_unused:
Raymond Hettinger2eff9e92015-06-27 22:03:35 -0700222 Py_INCREF(key);
Raymond Hettinger6ee588f2015-06-20 21:39:51 -0700223 so->fill++;
224 so->used++;
225 entry->key = key;
226 entry->hash = hash;
Raymond Hettinger15f08692015-07-03 17:21:17 -0700227 if ((size_t)so->fill*3 < mask*2)
228 return 0;
229 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger8651a502015-05-27 10:37:20 -0700230
231 found_active:
Raymond Hettinger8651a502015-05-27 10:37:20 -0700232 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000233}
234
235/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000236Internal routine used by set_table_resize() to insert an item which is
237known to be absent from the set. This routine also assumes that
238the set contains no deleted entries. Besides the performance benefit,
239using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
240Note that no refcounts are changed by this routine; if needed, the caller
241is responsible for incref'ing `key`.
242*/
243static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200244set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200247 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700248 size_t perturb = hash;
249 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800250 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700251 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700253 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800254 entry = &table[i];
255 if (entry->key == NULL)
256 goto found_null;
257 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800258 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800259 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800260 if (entry->key == NULL)
261 goto found_null;
262 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700263 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700264 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800265 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700267 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 entry->key = key;
269 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700270 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000272}
273
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700274/* ======== End logic for probing the hash table ========================== */
275/* ======================================================================== */
276
Thomas Wouters89f507f2006-12-13 04:49:30 +0000277/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000279keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000280actually be smaller than the old one.
281*/
282static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 Py_ssize_t newsize;
286 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800287 Py_ssize_t oldfill = so->fill;
288 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 int is_oldtable_malloced;
290 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100295 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 for (newsize = PySet_MINSIZE;
297 newsize <= minused && newsize > 0;
298 newsize <<= 1)
299 ;
300 if (newsize <= 0) {
301 PyErr_NoMemory();
302 return -1;
303 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 /* Get space for a new table. */
306 oldtable = so->table;
307 assert(oldtable != NULL);
308 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (newsize == PySet_MINSIZE) {
311 /* A large table is shrinking, or we can't get any smaller. */
312 newtable = so->smalltable;
313 if (newtable == oldtable) {
314 if (so->fill == so->used) {
315 /* No dummies, so no point doing anything. */
316 return 0;
317 }
318 /* We're not going to resize it, but rebuild the
319 table anyway to purge old dummy entries.
320 Subtle: This is *necessary* if fill==size,
321 as set_lookkey needs at least one virgin slot to
322 terminate failing searches. If fill < size, it's
323 merely desirable, as dummies slow searches. */
324 assert(so->fill > so->used);
325 memcpy(small_copy, oldtable, sizeof(small_copy));
326 oldtable = small_copy;
327 }
328 }
329 else {
330 newtable = PyMem_NEW(setentry, newsize);
331 if (newtable == NULL) {
332 PyErr_NoMemory();
333 return -1;
334 }
335 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 /* Make the set empty, using the new table. */
338 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800341 so->used = 0;
342 so->mask = newsize - 1;
343 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 /* Copy the data over; this is refcount-neutral for active entries;
346 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800347 if (oldfill == oldused) {
348 for (entry = oldtable; oldused > 0; entry++) {
349 if (entry->key != NULL) {
350 oldused--;
351 set_insert_clean(so, entry->key, entry->hash);
352 }
353 }
354 } else {
355 for (entry = oldtable; oldused > 0; entry++) {
356 if (entry->key != NULL && entry->key != dummy) {
357 oldused--;
358 set_insert_clean(so, entry->key, entry->hash);
359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 }
361 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (is_oldtable_malloced)
364 PyMem_DEL(oldtable);
365 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000366}
367
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000368static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200369set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000370{
Raymond Hettinger15f08692015-07-03 17:21:17 -0700371 return set_insert_key(so, entry->key, entry->hash);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000372}
373
374static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200375set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200377 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200380 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 hash = PyObject_Hash(key);
382 if (hash == -1)
383 return -1;
384 }
Raymond Hettinger15f08692015-07-03 17:21:17 -0700385 return set_insert_key(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386}
387
388#define DISCARD_NOTFOUND 0
389#define DISCARD_FOUND 1
390
391static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000392set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800393{
394 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000396
Raymond Hettinger93035c42015-01-25 16:12:49 -0800397 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (entry == NULL)
399 return -1;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700400 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 return DISCARD_NOTFOUND;
402 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800404 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 so->used--;
406 Py_DECREF(old_key);
407 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000408}
409
410static int
411set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000412{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800413 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200414 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200419 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 hash = PyObject_Hash(key);
421 if (hash == -1)
422 return -1;
423 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800424 entry.key = key;
425 entry.hash = hash;
426 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000427}
428
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700429static void
430set_empty_to_minsize(PySetObject *so)
431{
432 memset(so->smalltable, 0, sizeof(so->smalltable));
433 so->fill = 0;
434 so->used = 0;
435 so->mask = PySet_MINSIZE - 1;
436 so->table = so->smalltable;
437 so->hash = -1;
438}
439
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000440static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000441set_clear_internal(PySetObject *so)
442{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800443 setentry *entry;
444 setentry *table = so->table;
445 Py_ssize_t fill = so->fill;
446 Py_ssize_t used = so->used;
447 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000449
Raymond Hettinger583cd032013-09-07 22:06:35 -0700450 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 /* This is delicate. During the process of clearing the set,
454 * decrefs can cause the set to mutate. To avoid fatal confusion
455 * (voice of experience), we have to make the set empty before
456 * clearing the slots, and never refer to anything via so->ref while
457 * clearing.
458 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700460 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 else if (fill > 0) {
463 /* It's a small table with something that needs to be cleared.
464 * Afraid the only safe way is to copy the set entries into
465 * another small table first.
466 */
467 memcpy(small_copy, table, sizeof(small_copy));
468 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700469 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 }
471 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 /* Now we can finally clear things. If C had refcounts, we could
474 * assert that the refcount on table is 1 now, i.e. that this function
475 * has unique access to it, so decref side-effects can't alter it.
476 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800477 for (entry = table; used > 0; entry++) {
478 if (entry->key && entry->key != dummy) {
479 used--;
480 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 if (table_is_malloced)
485 PyMem_DEL(table);
486 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487}
488
489/*
490 * Iterate over a set table. Use like so:
491 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000492 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000493 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000494 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000495 * while (set_next(yourset, &pos, &entry)) {
496 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000497 * }
498 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000499 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501 */
502static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000503set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 Py_ssize_t i;
506 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200507 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 assert (PyAnySet_Check(so));
510 i = *pos_ptr;
511 assert(i >= 0);
512 table = so->table;
513 mask = so->mask;
514 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
515 i++;
516 *pos_ptr = i+1;
517 if (i > mask)
518 return 0;
519 assert(table[i].key != NULL);
520 *entry_ptr = &table[i];
521 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522}
523
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000524static void
525set_dealloc(PySetObject *so)
526{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200527 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800528 Py_ssize_t used = so->used;
529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 PyObject_GC_UnTrack(so);
531 Py_TRASHCAN_SAFE_BEGIN(so)
532 if (so->weakreflist != NULL)
533 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000534
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800535 for (entry = so->table; used > 0; entry++) {
536 if (entry->key && entry->key != dummy) {
537 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700538 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 }
540 }
541 if (so->table != so->smalltable)
542 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700543 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000545}
546
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000547static PyObject *
548set_repr(PySetObject *so)
549{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200550 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (status != 0) {
554 if (status < 0)
555 return NULL;
556 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
557 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* shortcut for the empty set */
560 if (!so->used) {
561 Py_ReprLeave((PyObject*)so);
562 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
563 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 keys = PySequence_List((PyObject *)so);
566 if (keys == NULL)
567 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200569 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 listrepr = PyObject_Repr(keys);
571 Py_DECREF(keys);
572 if (listrepr == NULL)
573 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200574 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200576 if (tmp == NULL)
577 goto done;
578 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200579
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200580 if (Py_TYPE(so) != &PySet_Type)
581 result = PyUnicode_FromFormat("%s({%U})",
582 Py_TYPE(so)->tp_name,
583 listrepr);
584 else
585 result = PyUnicode_FromFormat("{%U}", listrepr);
586 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000587done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 Py_ReprLeave((PyObject*)so);
589 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000590}
591
Martin v. Löwis18e16552006-02-15 17:27:45 +0000592static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000593set_len(PyObject *so)
594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000596}
597
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000598static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000599set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000602 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200603 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700604 setentry *so_entry;
605 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 assert (PyAnySet_Check(so));
608 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 other = (PySetObject*)otherset;
611 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700612 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return 0;
614 /* Do one big resize at the start, rather than
615 * incrementally resizing as we insert new keys. Expect
616 * that there will be no (or few) overlapping keys.
617 */
Raymond Hettinger15f08692015-07-03 17:21:17 -0700618 if ((so->fill + other->used)*3 >= so->mask*2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (set_table_resize(so, (so->used + other->used)*2) != 0)
620 return -1;
621 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700622 so_entry = so->table;
623 other_entry = other->table;
624
625 /* If our table is empty, and both tables have the same size, and
626 there are no dummies to eliminate, then just copy the pointers. */
627 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
628 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
629 key = other_entry->key;
630 if (key != NULL) {
631 assert(so_entry->key == NULL);
632 Py_INCREF(key);
633 so_entry->key = key;
634 so_entry->hash = other_entry->hash;
635 }
636 }
637 so->fill = other->fill;
638 so->used = other->used;
639 return 0;
640 }
641
642 /* If our table is empty, we can use set_insert_clean() */
643 if (so->fill == 0) {
644 for (i = 0; i <= other->mask; i++, other_entry++) {
645 key = other_entry->key;
646 if (key != NULL && key != dummy) {
647 Py_INCREF(key);
648 set_insert_clean(so, key, other_entry->hash);
649 }
650 }
651 return 0;
652 }
653
654 /* We can't assure there are no duplicates, so do normal insertions */
655 for (i = 0; i <= other->mask; i++, other_entry++) {
656 key = other_entry->key;
657 if (key != NULL && key != dummy) {
Raymond Hettinger2eff9e92015-06-27 22:03:35 -0700658 if (set_insert_key(so, key, other_entry->hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 }
661 }
662 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000663}
664
665static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000666set_contains_entry(PySetObject *so, setentry *entry)
667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 PyObject *key;
669 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000670
Raymond Hettinger93035c42015-01-25 16:12:49 -0800671 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (lu_entry == NULL)
673 return -1;
674 key = lu_entry->key;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700675 return key != NULL;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000676}
677
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800678static int
679set_contains_key(PySetObject *so, PyObject *key)
680{
681 setentry entry;
682 Py_hash_t hash;
683
684 if (!PyUnicode_CheckExact(key) ||
685 (hash = ((PyASCIIObject *) key)->hash) == -1) {
686 hash = PyObject_Hash(key);
687 if (hash == -1)
688 return -1;
689 }
690 entry.key = key;
691 entry.hash = hash;
692 return set_contains_entry(so, &entry);
693}
694
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000695static PyObject *
696set_pop(PySetObject *so)
697{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800698 /* Make sure the search finger is in bounds */
699 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200700 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 assert (PyAnySet_Check(so));
704 if (so->used == 0) {
705 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
706 return NULL;
707 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000708
Raymond Hettinger1202a472015-01-18 13:12:42 -0800709 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
710 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800711 if (i > so->mask)
712 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 }
714 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800716 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800718 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000720}
721
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000722PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
723Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000724
725static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000726set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 Py_ssize_t pos = 0;
729 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 while (set_next(so, &pos, &entry))
732 Py_VISIT(entry->key);
733 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000734}
735
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000736static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000737frozenset_hash(PyObject *self)
738{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800739 /* Most of the constants in this hash algorithm are randomly choosen
740 large primes with "interesting bit patterns" and that passed
741 tests for good collision statistics on a variety of problematic
742 datasets such as:
743
744 ps = []
745 for r in range(21):
746 ps += itertools.combinations(range(20), r)
747 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
748
749 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800751 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 setentry *entry;
753 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 if (so->hash != -1)
756 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000757
Mark Dickinson57e683e2011-09-24 18:18:40 +0100758 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 while (set_next(so, &pos, &entry)) {
760 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800761 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 combinations of a small number of elements with nearby
763 hashes so that many distinct combinations collapse to only
764 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000765 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800766 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800768 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500769 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800770 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200771 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800772 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 so->hash = hash;
774 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000775}
776
Raymond Hettingera9d99362005-08-05 00:01:15 +0000777/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000778
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000779typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 PyObject_HEAD
781 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
782 Py_ssize_t si_used;
783 Py_ssize_t si_pos;
784 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000785} setiterobject;
786
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787static void
788setiter_dealloc(setiterobject *si)
789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 Py_XDECREF(si->si_set);
791 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000792}
793
794static int
795setiter_traverse(setiterobject *si, visitproc visit, void *arg)
796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 Py_VISIT(si->si_set);
798 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799}
800
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000801static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802setiter_len(setiterobject *si)
803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 Py_ssize_t len = 0;
805 if (si->si_set != NULL && si->si_used == si->si_set->used)
806 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000807 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000808}
809
Armin Rigof5b3e362006-02-11 21:32:43 +0000810PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000811
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000812static PyObject *setiter_iternext(setiterobject *si);
813
814static PyObject *
815setiter_reduce(setiterobject *si)
816{
817 PyObject *list;
818 setiterobject tmp;
819
820 list = PyList_New(0);
821 if (!list)
822 return NULL;
823
Ezio Melotti0e1af282012-09-28 16:43:40 +0300824 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000825 tmp = *si;
826 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300827
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000828 /* iterate the temporary into a list */
829 for(;;) {
830 PyObject *element = setiter_iternext(&tmp);
831 if (element) {
832 if (PyList_Append(list, element)) {
833 Py_DECREF(element);
834 Py_DECREF(list);
835 Py_XDECREF(tmp.si_set);
836 return NULL;
837 }
838 Py_DECREF(element);
839 } else
840 break;
841 }
842 Py_XDECREF(tmp.si_set);
843 /* check for error */
844 if (tmp.si_set != NULL) {
845 /* we have an error */
846 Py_DECREF(list);
847 return NULL;
848 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200849 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000850}
851
852PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
853
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000854static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000856 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000858};
859
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000860static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200863 Py_ssize_t i, mask;
864 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (so == NULL)
868 return NULL;
869 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 if (si->si_used != so->used) {
872 PyErr_SetString(PyExc_RuntimeError,
873 "Set changed size during iteration");
874 si->si_used = -1; /* Make this state sticky */
875 return NULL;
876 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 i = si->si_pos;
879 assert(i>=0);
880 entry = so->table;
881 mask = so->mask;
882 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
883 i++;
884 si->si_pos = i+1;
885 if (i > mask)
886 goto fail;
887 si->len--;
888 key = entry[i].key;
889 Py_INCREF(key);
890 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891
892fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 Py_DECREF(so);
894 si->si_set = NULL;
895 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000896}
897
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000898PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 PyVarObject_HEAD_INIT(&PyType_Type, 0)
900 "set_iterator", /* tp_name */
901 sizeof(setiterobject), /* tp_basicsize */
902 0, /* tp_itemsize */
903 /* methods */
904 (destructor)setiter_dealloc, /* tp_dealloc */
905 0, /* tp_print */
906 0, /* tp_getattr */
907 0, /* tp_setattr */
908 0, /* tp_reserved */
909 0, /* tp_repr */
910 0, /* tp_as_number */
911 0, /* tp_as_sequence */
912 0, /* tp_as_mapping */
913 0, /* tp_hash */
914 0, /* tp_call */
915 0, /* tp_str */
916 PyObject_GenericGetAttr, /* tp_getattro */
917 0, /* tp_setattro */
918 0, /* tp_as_buffer */
919 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
920 0, /* tp_doc */
921 (traverseproc)setiter_traverse, /* tp_traverse */
922 0, /* tp_clear */
923 0, /* tp_richcompare */
924 0, /* tp_weaklistoffset */
925 PyObject_SelfIter, /* tp_iter */
926 (iternextfunc)setiter_iternext, /* tp_iternext */
927 setiter_methods, /* tp_methods */
928 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000929};
930
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000931static PyObject *
932set_iter(PySetObject *so)
933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
935 if (si == NULL)
936 return NULL;
937 Py_INCREF(so);
938 si->si_set = so;
939 si->si_used = so->used;
940 si->si_pos = 0;
941 si->len = so->used;
942 _PyObject_GC_TRACK(si);
943 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000944}
945
Raymond Hettingerd7946662005-08-01 21:39:29 +0000946static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000947set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 if (PyAnySet_Check(other))
952 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (PyDict_CheckExact(other)) {
955 PyObject *value;
956 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000957 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 /* Do one big resize at the start, rather than
961 * incrementally resizing as we insert new keys. Expect
962 * that there will be no (or few) overlapping keys.
963 */
964 if (dictsize == -1)
965 return -1;
Raymond Hettinger15f08692015-07-03 17:21:17 -0700966 if ((so->fill + dictsize)*3 >= so->mask*2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
968 return -1;
969 }
970 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettinger15f08692015-07-03 17:21:17 -0700971 if (set_insert_key(so, key, hash))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 return -1;
973 }
974 return 0;
975 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 it = PyObject_GetIter(other);
978 if (it == NULL)
979 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700982 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 Py_DECREF(it);
984 Py_DECREF(key);
985 return -1;
986 }
987 Py_DECREF(key);
988 }
989 Py_DECREF(it);
990 if (PyErr_Occurred())
991 return -1;
992 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000993}
994
995static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000996set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1001 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001002 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 return NULL;
1004 }
1005 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001006}
1007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001009"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001010
Raymond Hettinger426d9952014-05-18 21:40:20 +01001011/* XXX Todo:
1012 If aligned memory allocations become available, make the
1013 set object 64 byte aligned so that most of the fields
1014 can be retrieved or updated in a single cache line.
1015*/
1016
Raymond Hettingera38123e2003-11-24 22:18:49 +00001017static PyObject *
1018make_new_set(PyTypeObject *type, PyObject *iterable)
1019{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001020 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001023 so = (PySetObject *)type->tp_alloc(type, 0);
1024 if (so == NULL)
1025 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001026
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001027 so->fill = 0;
1028 so->used = 0;
1029 so->mask = PySet_MINSIZE - 1;
1030 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001031 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -08001032 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001036 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 Py_DECREF(so);
1038 return NULL;
1039 }
1040 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001043}
1044
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001045static PyObject *
1046make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1049 if (PyType_IsSubtype(type, &PySet_Type))
1050 type = &PySet_Type;
1051 else
1052 type = &PyFrozenSet_Type;
1053 }
1054 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001055}
1056
Raymond Hettingerd7946662005-08-01 21:39:29 +00001057/* The empty frozenset is a singleton */
1058static PyObject *emptyfrozenset = NULL;
1059
Raymond Hettingera690a992003-11-16 16:17:49 +00001060static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001061frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1066 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1069 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 if (type != &PyFrozenSet_Type)
1072 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (iterable != NULL) {
1075 /* frozenset(f) is idempotent */
1076 if (PyFrozenSet_CheckExact(iterable)) {
1077 Py_INCREF(iterable);
1078 return iterable;
1079 }
1080 result = make_new_set(type, iterable);
1081 if (result == NULL || PySet_GET_SIZE(result))
1082 return result;
1083 Py_DECREF(result);
1084 }
1085 /* The empty frozenset is a singleton */
1086 if (emptyfrozenset == NULL)
1087 emptyfrozenset = make_new_set(type, NULL);
1088 Py_XINCREF(emptyfrozenset);
1089 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001090}
1091
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001092int
1093PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001094{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001095 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001096}
1097
1098void
1099PySet_Fini(void)
1100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001102}
1103
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001104static PyObject *
1105set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1108 return NULL;
1109
1110 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001111}
1112
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001113/* set_swap_bodies() switches the contents of any two sets by moving their
1114 internal data pointers and, if needed, copying the internal smalltables.
1115 Semantically equivalent to:
1116
1117 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1118
1119 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001120 Useful for operations that update in-place (by allowing an intermediate
1121 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001122*/
1123
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001124static void
1125set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 Py_ssize_t t;
1128 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001130 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 t = a->fill; a->fill = b->fill; b->fill = t;
1133 t = a->used; a->used = b->used; b->used = t;
1134 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 u = a->table;
1137 if (a->table == a->smalltable)
1138 u = b->smalltable;
1139 a->table = b->table;
1140 if (b->table == b->smalltable)
1141 a->table = a->smalltable;
1142 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (a->table == a->smalltable || b->table == b->smalltable) {
1145 memcpy(tab, a->smalltable, sizeof(tab));
1146 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1147 memcpy(b->smalltable, tab, sizeof(tab));
1148 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1151 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1152 h = a->hash; a->hash = b->hash; b->hash = h;
1153 } else {
1154 a->hash = -1;
1155 b->hash = -1;
1156 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001157}
1158
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001159static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001160set_copy(PySetObject *so)
1161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001163}
1164
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001165static PyObject *
1166frozenset_copy(PySetObject *so)
1167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 if (PyFrozenSet_CheckExact(so)) {
1169 Py_INCREF(so);
1170 return (PyObject *)so;
1171 }
1172 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001173}
1174
Raymond Hettingera690a992003-11-16 16:17:49 +00001175PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1176
1177static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001178set_clear(PySetObject *so)
1179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 set_clear_internal(so);
1181 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001182}
1183
1184PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1185
1186static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001187set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 PySetObject *result;
1190 PyObject *other;
1191 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 result = (PySetObject *)set_copy(so);
1194 if (result == NULL)
1195 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1198 other = PyTuple_GET_ITEM(args, i);
1199 if ((PyObject *)so == other)
1200 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001201 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 Py_DECREF(result);
1203 return NULL;
1204 }
1205 }
1206 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001207}
1208
1209PyDoc_STRVAR(union_doc,
1210 "Return the union of sets as a new set.\n\
1211\n\
1212(i.e. all elements that are in either set.)");
1213
1214static PyObject *
1215set_or(PySetObject *so, PyObject *other)
1216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001218
Brian Curtindfc80e32011-08-10 20:28:54 -05001219 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1220 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 result = (PySetObject *)set_copy(so);
1223 if (result == NULL)
1224 return NULL;
1225 if ((PyObject *)so == other)
1226 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001227 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 Py_DECREF(result);
1229 return NULL;
1230 }
1231 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001232}
1233
Raymond Hettingera690a992003-11-16 16:17:49 +00001234static PyObject *
1235set_ior(PySetObject *so, PyObject *other)
1236{
Brian Curtindfc80e32011-08-10 20:28:54 -05001237 if (!PyAnySet_Check(other))
1238 Py_RETURN_NOTIMPLEMENTED;
1239
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001240 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 return NULL;
1242 Py_INCREF(so);
1243 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001244}
1245
1246static PyObject *
1247set_intersection(PySetObject *so, PyObject *other)
1248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 PySetObject *result;
1250 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if ((PyObject *)so == other)
1253 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1256 if (result == NULL)
1257 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (PyAnySet_Check(other)) {
1260 Py_ssize_t pos = 0;
1261 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1264 tmp = (PyObject *)so;
1265 so = (PySetObject *)other;
1266 other = tmp;
1267 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 while (set_next((PySetObject *)other, &pos, &entry)) {
1270 int rv = set_contains_entry(so, entry);
1271 if (rv == -1) {
1272 Py_DECREF(result);
1273 return NULL;
1274 }
1275 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001276 if (set_add_entry(result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 Py_DECREF(result);
1278 return NULL;
1279 }
1280 }
1281 }
1282 return (PyObject *)result;
1283 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 it = PyObject_GetIter(other);
1286 if (it == NULL) {
1287 Py_DECREF(result);
1288 return NULL;
1289 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 while ((key = PyIter_Next(it)) != NULL) {
1292 int rv;
1293 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001294 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 if (hash == -1) {
1297 Py_DECREF(it);
1298 Py_DECREF(result);
1299 Py_DECREF(key);
1300 return NULL;
1301 }
1302 entry.hash = hash;
1303 entry.key = key;
1304 rv = set_contains_entry(so, &entry);
1305 if (rv == -1) {
1306 Py_DECREF(it);
1307 Py_DECREF(result);
1308 Py_DECREF(key);
1309 return NULL;
1310 }
1311 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001312 if (set_add_entry(result, &entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 Py_DECREF(it);
1314 Py_DECREF(result);
1315 Py_DECREF(key);
1316 return NULL;
1317 }
1318 }
1319 Py_DECREF(key);
1320 }
1321 Py_DECREF(it);
1322 if (PyErr_Occurred()) {
1323 Py_DECREF(result);
1324 return NULL;
1325 }
1326 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001327}
1328
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001329static PyObject *
1330set_intersection_multi(PySetObject *so, PyObject *args)
1331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 Py_ssize_t i;
1333 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (PyTuple_GET_SIZE(args) == 0)
1336 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 Py_INCREF(so);
1339 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1340 PyObject *other = PyTuple_GET_ITEM(args, i);
1341 PyObject *newresult = set_intersection((PySetObject *)result, other);
1342 if (newresult == NULL) {
1343 Py_DECREF(result);
1344 return NULL;
1345 }
1346 Py_DECREF(result);
1347 result = newresult;
1348 }
1349 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001350}
1351
Raymond Hettingera690a992003-11-16 16:17:49 +00001352PyDoc_STRVAR(intersection_doc,
1353"Return the intersection of two sets as a new set.\n\
1354\n\
1355(i.e. all elements that are in both sets.)");
1356
1357static PyObject *
1358set_intersection_update(PySetObject *so, PyObject *other)
1359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 tmp = set_intersection(so, other);
1363 if (tmp == NULL)
1364 return NULL;
1365 set_swap_bodies(so, (PySetObject *)tmp);
1366 Py_DECREF(tmp);
1367 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001368}
1369
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001370static PyObject *
1371set_intersection_update_multi(PySetObject *so, PyObject *args)
1372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 tmp = set_intersection_multi(so, args);
1376 if (tmp == NULL)
1377 return NULL;
1378 set_swap_bodies(so, (PySetObject *)tmp);
1379 Py_DECREF(tmp);
1380 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001381}
1382
Raymond Hettingera690a992003-11-16 16:17:49 +00001383PyDoc_STRVAR(intersection_update_doc,
1384"Update a set with the intersection of itself and another.");
1385
1386static PyObject *
1387set_and(PySetObject *so, PyObject *other)
1388{
Brian Curtindfc80e32011-08-10 20:28:54 -05001389 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1390 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001392}
1393
1394static PyObject *
1395set_iand(PySetObject *so, PyObject *other)
1396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001398
Brian Curtindfc80e32011-08-10 20:28:54 -05001399 if (!PyAnySet_Check(other))
1400 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 result = set_intersection_update(so, other);
1402 if (result == NULL)
1403 return NULL;
1404 Py_DECREF(result);
1405 Py_INCREF(so);
1406 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001407}
1408
Guido van Rossum58da9312007-11-10 23:39:45 +00001409static PyObject *
1410set_isdisjoint(PySetObject *so, PyObject *other)
1411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 if ((PyObject *)so == other) {
1415 if (PySet_GET_SIZE(so) == 0)
1416 Py_RETURN_TRUE;
1417 else
1418 Py_RETURN_FALSE;
1419 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 if (PyAnySet_CheckExact(other)) {
1422 Py_ssize_t pos = 0;
1423 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1426 tmp = (PyObject *)so;
1427 so = (PySetObject *)other;
1428 other = tmp;
1429 }
1430 while (set_next((PySetObject *)other, &pos, &entry)) {
1431 int rv = set_contains_entry(so, entry);
1432 if (rv == -1)
1433 return NULL;
1434 if (rv)
1435 Py_RETURN_FALSE;
1436 }
1437 Py_RETURN_TRUE;
1438 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 it = PyObject_GetIter(other);
1441 if (it == NULL)
1442 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 while ((key = PyIter_Next(it)) != NULL) {
1445 int rv;
1446 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001447 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 if (hash == -1) {
1450 Py_DECREF(key);
1451 Py_DECREF(it);
1452 return NULL;
1453 }
1454 entry.hash = hash;
1455 entry.key = key;
1456 rv = set_contains_entry(so, &entry);
1457 Py_DECREF(key);
1458 if (rv == -1) {
1459 Py_DECREF(it);
1460 return NULL;
1461 }
1462 if (rv) {
1463 Py_DECREF(it);
1464 Py_RETURN_FALSE;
1465 }
1466 }
1467 Py_DECREF(it);
1468 if (PyErr_Occurred())
1469 return NULL;
1470 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001471}
1472
1473PyDoc_STRVAR(isdisjoint_doc,
1474"Return True if two sets have a null intersection.");
1475
Neal Norwitz6576bd82005-11-13 18:41:28 +00001476static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001477set_difference_update_internal(PySetObject *so, PyObject *other)
1478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if ((PyObject *)so == other)
1480 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 if (PyAnySet_Check(other)) {
1483 setentry *entry;
1484 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 while (set_next((PySetObject *)other, &pos, &entry))
1487 if (set_discard_entry(so, entry) == -1)
1488 return -1;
1489 } else {
1490 PyObject *key, *it;
1491 it = PyObject_GetIter(other);
1492 if (it == NULL)
1493 return -1;
1494
1495 while ((key = PyIter_Next(it)) != NULL) {
1496 if (set_discard_key(so, key) == -1) {
1497 Py_DECREF(it);
1498 Py_DECREF(key);
1499 return -1;
1500 }
1501 Py_DECREF(key);
1502 }
1503 Py_DECREF(it);
1504 if (PyErr_Occurred())
1505 return -1;
1506 }
1507 /* If more than 1/5 are dummies, then resize them away. */
1508 if ((so->fill - so->used) * 5 < so->mask)
1509 return 0;
1510 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001511}
1512
Raymond Hettingera690a992003-11-16 16:17:49 +00001513static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001514set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1519 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001520 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 return NULL;
1522 }
1523 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001524}
1525
1526PyDoc_STRVAR(difference_update_doc,
1527"Remove all elements of another set from this set.");
1528
1529static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001530set_copy_and_difference(PySetObject *so, PyObject *other)
1531{
1532 PyObject *result;
1533
1534 result = set_copy(so);
1535 if (result == NULL)
1536 return NULL;
1537 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1538 return result;
1539 Py_DECREF(result);
1540 return NULL;
1541}
1542
1543static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001544set_difference(PySetObject *so, PyObject *other)
1545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 PyObject *result;
1547 setentry *entry;
1548 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001551 return set_copy_and_difference(so, other);
1552 }
1553
1554 /* If len(so) much more than len(other), it's more efficient to simply copy
1555 * so and then iterate other looking for common elements. */
1556 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1557 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 result = make_new_set_basetype(Py_TYPE(so), NULL);
1561 if (result == NULL)
1562 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (PyDict_CheckExact(other)) {
1565 while (set_next(so, &pos, &entry)) {
Raymond Hettinger15f08692015-07-03 17:21:17 -07001566 PyObject *key = entry->key;
1567 Py_hash_t hash = entry->hash;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001568 int rv;
Raymond Hettinger15f08692015-07-03 17:21:17 -07001569 rv = _PyDict_Contains(other, key, hash);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001570 if (rv < 0) {
1571 Py_DECREF(result);
1572 return NULL;
1573 }
1574 if (!rv) {
Raymond Hettinger15f08692015-07-03 17:21:17 -07001575 if (set_insert_key((PySetObject *)result, key, hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 Py_DECREF(result);
1577 return NULL;
1578 }
1579 }
1580 }
1581 return result;
1582 }
1583
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001584 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 while (set_next(so, &pos, &entry)) {
1586 int rv = set_contains_entry((PySetObject *)other, entry);
1587 if (rv == -1) {
1588 Py_DECREF(result);
1589 return NULL;
1590 }
1591 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001592 if (set_add_entry((PySetObject *)result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 Py_DECREF(result);
1594 return NULL;
1595 }
1596 }
1597 }
1598 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001599}
1600
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001601static PyObject *
1602set_difference_multi(PySetObject *so, PyObject *args)
1603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 Py_ssize_t i;
1605 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 if (PyTuple_GET_SIZE(args) == 0)
1608 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 other = PyTuple_GET_ITEM(args, 0);
1611 result = set_difference(so, other);
1612 if (result == NULL)
1613 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1616 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001617 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 Py_DECREF(result);
1619 return NULL;
1620 }
1621 }
1622 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001623}
1624
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001625PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001626"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001627\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001628(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001629static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001630set_sub(PySetObject *so, PyObject *other)
1631{
Brian Curtindfc80e32011-08-10 20:28:54 -05001632 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1633 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001635}
1636
1637static PyObject *
1638set_isub(PySetObject *so, PyObject *other)
1639{
Brian Curtindfc80e32011-08-10 20:28:54 -05001640 if (!PyAnySet_Check(other))
1641 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001642 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 return NULL;
1644 Py_INCREF(so);
1645 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001646}
1647
1648static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001649set_symmetric_difference_update(PySetObject *so, PyObject *other)
1650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 PySetObject *otherset;
1652 PyObject *key;
1653 Py_ssize_t pos = 0;
1654 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 if ((PyObject *)so == other)
1657 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (PyDict_CheckExact(other)) {
1660 PyObject *value;
1661 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001662 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1664 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001665
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001666 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 an_entry.hash = hash;
1668 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001671 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001672 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001675 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001676 if (set_add_entry(so, &an_entry)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001677 Py_DECREF(key);
1678 return NULL;
1679 }
1680 }
Georg Brandl2d444492010-09-03 10:52:55 +00001681 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 }
1683 Py_RETURN_NONE;
1684 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 if (PyAnySet_Check(other)) {
1687 Py_INCREF(other);
1688 otherset = (PySetObject *)other;
1689 } else {
1690 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1691 if (otherset == NULL)
1692 return NULL;
1693 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 while (set_next(otherset, &pos, &entry)) {
1696 int rv = set_discard_entry(so, entry);
1697 if (rv == -1) {
1698 Py_DECREF(otherset);
1699 return NULL;
1700 }
1701 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001702 if (set_add_entry(so, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 Py_DECREF(otherset);
1704 return NULL;
1705 }
1706 }
1707 }
1708 Py_DECREF(otherset);
1709 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001710}
1711
1712PyDoc_STRVAR(symmetric_difference_update_doc,
1713"Update a set with the symmetric difference of itself and another.");
1714
1715static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001716set_symmetric_difference(PySetObject *so, PyObject *other)
1717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 PyObject *rv;
1719 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1722 if (otherset == NULL)
1723 return NULL;
1724 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1725 if (rv == NULL)
1726 return NULL;
1727 Py_DECREF(rv);
1728 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001729}
1730
1731PyDoc_STRVAR(symmetric_difference_doc,
1732"Return the symmetric difference of two sets as a new set.\n\
1733\n\
1734(i.e. all elements that are in exactly one of the sets.)");
1735
1736static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001737set_xor(PySetObject *so, PyObject *other)
1738{
Brian Curtindfc80e32011-08-10 20:28:54 -05001739 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1740 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001742}
1743
1744static PyObject *
1745set_ixor(PySetObject *so, PyObject *other)
1746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001748
Brian Curtindfc80e32011-08-10 20:28:54 -05001749 if (!PyAnySet_Check(other))
1750 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 result = set_symmetric_difference_update(so, other);
1752 if (result == NULL)
1753 return NULL;
1754 Py_DECREF(result);
1755 Py_INCREF(so);
1756 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001757}
1758
1759static PyObject *
1760set_issubset(PySetObject *so, PyObject *other)
1761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 setentry *entry;
1763 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 if (!PyAnySet_Check(other)) {
1766 PyObject *tmp, *result;
1767 tmp = make_new_set(&PySet_Type, other);
1768 if (tmp == NULL)
1769 return NULL;
1770 result = set_issubset(so, tmp);
1771 Py_DECREF(tmp);
1772 return result;
1773 }
1774 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1775 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 while (set_next(so, &pos, &entry)) {
1778 int rv = set_contains_entry((PySetObject *)other, entry);
1779 if (rv == -1)
1780 return NULL;
1781 if (!rv)
1782 Py_RETURN_FALSE;
1783 }
1784 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001785}
1786
1787PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1788
1789static PyObject *
1790set_issuperset(PySetObject *so, PyObject *other)
1791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 if (!PyAnySet_Check(other)) {
1795 tmp = make_new_set(&PySet_Type, other);
1796 if (tmp == NULL)
1797 return NULL;
1798 result = set_issuperset(so, tmp);
1799 Py_DECREF(tmp);
1800 return result;
1801 }
1802 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001803}
1804
1805PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1806
Raymond Hettingera690a992003-11-16 16:17:49 +00001807static PyObject *
1808set_richcompare(PySetObject *v, PyObject *w, int op)
1809{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001810 PyObject *r1;
1811 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001812
Brian Curtindfc80e32011-08-10 20:28:54 -05001813 if(!PyAnySet_Check(w))
1814 Py_RETURN_NOTIMPLEMENTED;
1815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 switch (op) {
1817 case Py_EQ:
1818 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1819 Py_RETURN_FALSE;
1820 if (v->hash != -1 &&
1821 ((PySetObject *)w)->hash != -1 &&
1822 v->hash != ((PySetObject *)w)->hash)
1823 Py_RETURN_FALSE;
1824 return set_issubset(v, w);
1825 case Py_NE:
1826 r1 = set_richcompare(v, w, Py_EQ);
1827 if (r1 == NULL)
1828 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001829 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001831 if (r2 < 0)
1832 return NULL;
1833 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 case Py_LE:
1835 return set_issubset(v, w);
1836 case Py_GE:
1837 return set_issuperset(v, w);
1838 case Py_LT:
1839 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1840 Py_RETURN_FALSE;
1841 return set_issubset(v, w);
1842 case Py_GT:
1843 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1844 Py_RETURN_FALSE;
1845 return set_issuperset(v, w);
1846 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001847 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001848}
1849
Raymond Hettingera690a992003-11-16 16:17:49 +00001850static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001851set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001852{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001853 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 return NULL;
1855 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001856}
1857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001859"Add an element to a set.\n\
1860\n\
1861This has no effect if the element is already present.");
1862
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001863static int
1864set_contains(PySetObject *so, PyObject *key)
1865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 PyObject *tmpkey;
1867 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 rv = set_contains_key(so, key);
1870 if (rv == -1) {
1871 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1872 return -1;
1873 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001874 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 if (tmpkey == NULL)
1876 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001877 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 Py_DECREF(tmpkey);
1879 }
1880 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001881}
1882
1883static PyObject *
1884set_direct_contains(PySetObject *so, PyObject *key)
1885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 result = set_contains(so, key);
1889 if (result == -1)
1890 return NULL;
1891 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001892}
1893
1894PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1895
Raymond Hettingera690a992003-11-16 16:17:49 +00001896static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001897set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 PyObject *tmpkey;
1900 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 rv = set_discard_key(so, key);
1903 if (rv == -1) {
1904 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1905 return NULL;
1906 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001907 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 if (tmpkey == NULL)
1909 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 Py_DECREF(tmpkey);
1912 if (rv == -1)
1913 return NULL;
1914 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001917 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 return NULL;
1919 }
1920 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001921}
1922
1923PyDoc_STRVAR(remove_doc,
1924"Remove an element from a set; it must be a member.\n\
1925\n\
1926If the element is not a member, raise a KeyError.");
1927
1928static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001929set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001930{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001931 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 rv = set_discard_key(so, key);
1935 if (rv == -1) {
1936 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1937 return NULL;
1938 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001939 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 if (tmpkey == NULL)
1941 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001942 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001944 if (rv == -1)
1945 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 }
1947 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001948}
1949
1950PyDoc_STRVAR(discard_doc,
1951"Remove an element from a set if it is a member.\n\
1952\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001954
1955static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001956set_reduce(PySetObject *so)
1957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001959 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 keys = PySequence_List((PyObject *)so);
1962 if (keys == NULL)
1963 goto done;
1964 args = PyTuple_Pack(1, keys);
1965 if (args == NULL)
1966 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001967 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 if (dict == NULL) {
1969 PyErr_Clear();
1970 dict = Py_None;
1971 Py_INCREF(dict);
1972 }
1973 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001974done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 Py_XDECREF(args);
1976 Py_XDECREF(keys);
1977 Py_XDECREF(dict);
1978 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001979}
1980
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001981static PyObject *
1982set_sizeof(PySetObject *so)
1983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 res = sizeof(PySetObject);
1987 if (so->table != so->smalltable)
1988 res = res + (so->mask + 1) * sizeof(setentry);
1989 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001990}
1991
1992PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001993static int
1994set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 if (!PyAnySet_Check(self))
1999 return -1;
2000 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2001 return -1;
2002 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2003 return -1;
2004 set_clear_internal(self);
2005 self->hash = -1;
2006 if (iterable == NULL)
2007 return 0;
2008 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002009}
2010
Raymond Hettingera690a992003-11-16 16:17:49 +00002011static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 set_len, /* sq_length */
2013 0, /* sq_concat */
2014 0, /* sq_repeat */
2015 0, /* sq_item */
2016 0, /* sq_slice */
2017 0, /* sq_ass_item */
2018 0, /* sq_ass_slice */
2019 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002020};
2021
2022/* set object ********************************************************/
2023
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002024#ifdef Py_DEBUG
2025static PyObject *test_c_api(PySetObject *so);
2026
2027PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2028All is well if assertions don't fail.");
2029#endif
2030
Raymond Hettingera690a992003-11-16 16:17:49 +00002031static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 {"add", (PyCFunction)set_add, METH_O,
2033 add_doc},
2034 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2035 clear_doc},
2036 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2037 contains_doc},
2038 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2039 copy_doc},
2040 {"discard", (PyCFunction)set_discard, METH_O,
2041 discard_doc},
2042 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2043 difference_doc},
2044 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2045 difference_update_doc},
2046 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2047 intersection_doc},
2048 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2049 intersection_update_doc},
2050 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2051 isdisjoint_doc},
2052 {"issubset", (PyCFunction)set_issubset, METH_O,
2053 issubset_doc},
2054 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2055 issuperset_doc},
2056 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2057 pop_doc},
2058 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2059 reduce_doc},
2060 {"remove", (PyCFunction)set_remove, METH_O,
2061 remove_doc},
2062 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2063 sizeof_doc},
2064 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2065 symmetric_difference_doc},
2066 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2067 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002068#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2070 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002071#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 {"union", (PyCFunction)set_union, METH_VARARGS,
2073 union_doc},
2074 {"update", (PyCFunction)set_update, METH_VARARGS,
2075 update_doc},
2076 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002077};
2078
2079static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 0, /*nb_add*/
2081 (binaryfunc)set_sub, /*nb_subtract*/
2082 0, /*nb_multiply*/
2083 0, /*nb_remainder*/
2084 0, /*nb_divmod*/
2085 0, /*nb_power*/
2086 0, /*nb_negative*/
2087 0, /*nb_positive*/
2088 0, /*nb_absolute*/
2089 0, /*nb_bool*/
2090 0, /*nb_invert*/
2091 0, /*nb_lshift*/
2092 0, /*nb_rshift*/
2093 (binaryfunc)set_and, /*nb_and*/
2094 (binaryfunc)set_xor, /*nb_xor*/
2095 (binaryfunc)set_or, /*nb_or*/
2096 0, /*nb_int*/
2097 0, /*nb_reserved*/
2098 0, /*nb_float*/
2099 0, /*nb_inplace_add*/
2100 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2101 0, /*nb_inplace_multiply*/
2102 0, /*nb_inplace_remainder*/
2103 0, /*nb_inplace_power*/
2104 0, /*nb_inplace_lshift*/
2105 0, /*nb_inplace_rshift*/
2106 (binaryfunc)set_iand, /*nb_inplace_and*/
2107 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2108 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002109};
2110
2111PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002112"set() -> new empty set object\n\
2113set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002114\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002115Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002116
2117PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2119 "set", /* tp_name */
2120 sizeof(PySetObject), /* tp_basicsize */
2121 0, /* tp_itemsize */
2122 /* methods */
2123 (destructor)set_dealloc, /* tp_dealloc */
2124 0, /* tp_print */
2125 0, /* tp_getattr */
2126 0, /* tp_setattr */
2127 0, /* tp_reserved */
2128 (reprfunc)set_repr, /* tp_repr */
2129 &set_as_number, /* tp_as_number */
2130 &set_as_sequence, /* tp_as_sequence */
2131 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002132 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 0, /* tp_call */
2134 0, /* tp_str */
2135 PyObject_GenericGetAttr, /* tp_getattro */
2136 0, /* tp_setattro */
2137 0, /* tp_as_buffer */
2138 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2139 Py_TPFLAGS_BASETYPE, /* tp_flags */
2140 set_doc, /* tp_doc */
2141 (traverseproc)set_traverse, /* tp_traverse */
2142 (inquiry)set_clear_internal, /* tp_clear */
2143 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002144 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2145 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 0, /* tp_iternext */
2147 set_methods, /* tp_methods */
2148 0, /* tp_members */
2149 0, /* tp_getset */
2150 0, /* tp_base */
2151 0, /* tp_dict */
2152 0, /* tp_descr_get */
2153 0, /* tp_descr_set */
2154 0, /* tp_dictoffset */
2155 (initproc)set_init, /* tp_init */
2156 PyType_GenericAlloc, /* tp_alloc */
2157 set_new, /* tp_new */
2158 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002159};
2160
2161/* frozenset object ********************************************************/
2162
2163
2164static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2166 contains_doc},
2167 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2168 copy_doc},
2169 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2170 difference_doc},
2171 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2172 intersection_doc},
2173 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2174 isdisjoint_doc},
2175 {"issubset", (PyCFunction)set_issubset, METH_O,
2176 issubset_doc},
2177 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2178 issuperset_doc},
2179 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2180 reduce_doc},
2181 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2182 sizeof_doc},
2183 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2184 symmetric_difference_doc},
2185 {"union", (PyCFunction)set_union, METH_VARARGS,
2186 union_doc},
2187 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002188};
2189
2190static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 0, /*nb_add*/
2192 (binaryfunc)set_sub, /*nb_subtract*/
2193 0, /*nb_multiply*/
2194 0, /*nb_remainder*/
2195 0, /*nb_divmod*/
2196 0, /*nb_power*/
2197 0, /*nb_negative*/
2198 0, /*nb_positive*/
2199 0, /*nb_absolute*/
2200 0, /*nb_bool*/
2201 0, /*nb_invert*/
2202 0, /*nb_lshift*/
2203 0, /*nb_rshift*/
2204 (binaryfunc)set_and, /*nb_and*/
2205 (binaryfunc)set_xor, /*nb_xor*/
2206 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002207};
2208
2209PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002210"frozenset() -> empty frozenset object\n\
2211frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002212\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002213Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002214
2215PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2217 "frozenset", /* tp_name */
2218 sizeof(PySetObject), /* tp_basicsize */
2219 0, /* tp_itemsize */
2220 /* methods */
2221 (destructor)set_dealloc, /* tp_dealloc */
2222 0, /* tp_print */
2223 0, /* tp_getattr */
2224 0, /* tp_setattr */
2225 0, /* tp_reserved */
2226 (reprfunc)set_repr, /* tp_repr */
2227 &frozenset_as_number, /* tp_as_number */
2228 &set_as_sequence, /* tp_as_sequence */
2229 0, /* tp_as_mapping */
2230 frozenset_hash, /* tp_hash */
2231 0, /* tp_call */
2232 0, /* tp_str */
2233 PyObject_GenericGetAttr, /* tp_getattro */
2234 0, /* tp_setattro */
2235 0, /* tp_as_buffer */
2236 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2237 Py_TPFLAGS_BASETYPE, /* tp_flags */
2238 frozenset_doc, /* tp_doc */
2239 (traverseproc)set_traverse, /* tp_traverse */
2240 (inquiry)set_clear_internal, /* tp_clear */
2241 (richcmpfunc)set_richcompare, /* tp_richcompare */
2242 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2243 (getiterfunc)set_iter, /* tp_iter */
2244 0, /* tp_iternext */
2245 frozenset_methods, /* tp_methods */
2246 0, /* tp_members */
2247 0, /* tp_getset */
2248 0, /* tp_base */
2249 0, /* tp_dict */
2250 0, /* tp_descr_get */
2251 0, /* tp_descr_set */
2252 0, /* tp_dictoffset */
2253 0, /* tp_init */
2254 PyType_GenericAlloc, /* tp_alloc */
2255 frozenset_new, /* tp_new */
2256 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002257};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002258
2259
2260/***** C API functions *************************************************/
2261
2262PyObject *
2263PySet_New(PyObject *iterable)
2264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002266}
2267
2268PyObject *
2269PyFrozenSet_New(PyObject *iterable)
2270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002272}
2273
Neal Norwitz8c49c822006-03-04 18:41:19 +00002274Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002275PySet_Size(PyObject *anyset)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 if (!PyAnySet_Check(anyset)) {
2278 PyErr_BadInternalCall();
2279 return -1;
2280 }
2281 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002282}
2283
2284int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002285PySet_Clear(PyObject *set)
2286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 if (!PySet_Check(set)) {
2288 PyErr_BadInternalCall();
2289 return -1;
2290 }
2291 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002292}
2293
2294int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002295PySet_Contains(PyObject *anyset, PyObject *key)
2296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 if (!PyAnySet_Check(anyset)) {
2298 PyErr_BadInternalCall();
2299 return -1;
2300 }
2301 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002302}
2303
2304int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002305PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (!PySet_Check(set)) {
2308 PyErr_BadInternalCall();
2309 return -1;
2310 }
2311 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002312}
2313
2314int
Christian Heimesfd66e512008-01-29 12:18:50 +00002315PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!PySet_Check(anyset) &&
2318 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2319 PyErr_BadInternalCall();
2320 return -1;
2321 }
2322 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002323}
2324
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002325int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002326_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 if (!PyAnySet_Check(set)) {
2331 PyErr_BadInternalCall();
2332 return -1;
2333 }
2334 if (set_next((PySetObject *)set, pos, &entry) == 0)
2335 return 0;
2336 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002337 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002339}
2340
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002341PyObject *
2342PySet_Pop(PyObject *set)
2343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 if (!PySet_Check(set)) {
2345 PyErr_BadInternalCall();
2346 return NULL;
2347 }
2348 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002349}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002350
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002351int
2352_PySet_Update(PyObject *set, PyObject *iterable)
2353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (!PySet_Check(set)) {
2355 PyErr_BadInternalCall();
2356 return -1;
2357 }
2358 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002359}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002360
Raymond Hettingere259f132013-12-15 11:56:14 -08002361/* Exported for the gdb plugin's benefit. */
2362PyObject *_PySet_Dummy = dummy;
2363
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002364#ifdef Py_DEBUG
2365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002367 Returns True and original set is restored. */
2368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369#define assertRaises(call_return_value, exception) \
2370 do { \
2371 assert(call_return_value); \
2372 assert(PyErr_ExceptionMatches(exception)); \
2373 PyErr_Clear(); \
2374 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002375
2376static PyObject *
2377test_c_api(PySetObject *so)
2378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 Py_ssize_t count;
2380 char *s;
2381 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002382 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002384 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 /* Verify preconditions */
2388 assert(PyAnySet_Check(ob));
2389 assert(PyAnySet_CheckExact(ob));
2390 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* so.clear(); so |= set("abc"); */
2393 str = PyUnicode_FromString("abc");
2394 if (str == NULL)
2395 return NULL;
2396 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002397 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 Py_DECREF(str);
2399 return NULL;
2400 }
2401 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 /* Exercise type/size checks */
2404 assert(PySet_Size(ob) == 3);
2405 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 /* Raise TypeError for non-iterable constructor arguments */
2408 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2409 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 /* Raise TypeError for unhashable key */
2412 dup = PySet_New(ob);
2413 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2414 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2415 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 /* Exercise successful pop, contains, add, and discard */
2418 elem = PySet_Pop(ob);
2419 assert(PySet_Contains(ob, elem) == 0);
2420 assert(PySet_GET_SIZE(ob) == 2);
2421 assert(PySet_Add(ob, elem) == 0);
2422 assert(PySet_Contains(ob, elem) == 1);
2423 assert(PySet_GET_SIZE(ob) == 3);
2424 assert(PySet_Discard(ob, elem) == 1);
2425 assert(PySet_GET_SIZE(ob) == 2);
2426 assert(PySet_Discard(ob, elem) == 0);
2427 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* Exercise clear */
2430 dup2 = PySet_New(dup);
2431 assert(PySet_Clear(dup2) == 0);
2432 assert(PySet_Size(dup2) == 0);
2433 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* Raise SystemError on clear or update of frozen set */
2436 f = PyFrozenSet_New(dup);
2437 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2438 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2439 assert(PySet_Add(f, elem) == 0);
2440 Py_INCREF(f);
2441 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2442 Py_DECREF(f);
2443 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 /* Exercise direct iteration */
2446 i = 0, count = 0;
2447 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2448 s = _PyUnicode_AsString(x);
2449 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2450 count++;
2451 }
2452 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 /* Exercise updates */
2455 dup2 = PySet_New(NULL);
2456 assert(_PySet_Update(dup2, dup) == 0);
2457 assert(PySet_Size(dup2) == 3);
2458 assert(_PySet_Update(dup2, dup) == 0);
2459 assert(PySet_Size(dup2) == 3);
2460 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 /* Raise SystemError when self argument is not a set or frozenset. */
2463 t = PyTuple_New(0);
2464 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2465 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2466 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 /* Raise SystemError when self argument is not a set. */
2469 f = PyFrozenSet_New(dup);
2470 assert(PySet_Size(f) == 3);
2471 assert(PyFrozenSet_CheckExact(f));
2472 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2473 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2474 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 /* Raise KeyError when popping from an empty set */
2477 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2478 Py_DECREF(ob);
2479 assert(PySet_GET_SIZE(ob) == 0);
2480 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* Restore the set from the copy using the PyNumber API */
2483 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2484 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* Verify constructors accept NULL arguments */
2487 f = PySet_New(NULL);
2488 assert(f != NULL);
2489 assert(PySet_GET_SIZE(f) == 0);
2490 Py_DECREF(f);
2491 f = PyFrozenSet_New(NULL);
2492 assert(f != NULL);
2493 assert(PyFrozenSet_CheckExact(f));
2494 assert(PySet_GET_SIZE(f) == 0);
2495 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 Py_DECREF(elem);
2498 Py_DECREF(dup);
2499 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002500}
2501
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002502#undef assertRaises
2503
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002504#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002505
2506/***** Dummy Struct *************************************************/
2507
2508static PyObject *
2509dummy_repr(PyObject *op)
2510{
2511 return PyUnicode_FromString("<dummy key>");
2512}
2513
2514static void
2515dummy_dealloc(PyObject* ignore)
2516{
2517 Py_FatalError("deallocating <dummy key>");
2518}
2519
2520static PyTypeObject _PySetDummy_Type = {
2521 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2522 "<dummy key> type",
2523 0,
2524 0,
2525 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2526 0, /*tp_print*/
2527 0, /*tp_getattr*/
2528 0, /*tp_setattr*/
2529 0, /*tp_reserved*/
2530 dummy_repr, /*tp_repr*/
2531 0, /*tp_as_number*/
2532 0, /*tp_as_sequence*/
2533 0, /*tp_as_mapping*/
2534 0, /*tp_hash */
2535 0, /*tp_call */
2536 0, /*tp_str */
2537 0, /*tp_getattro */
2538 0, /*tp_setattro */
2539 0, /*tp_as_buffer */
2540 Py_TPFLAGS_DEFAULT, /*tp_flags */
2541};
2542
2543static PyObject _dummy_struct = {
2544 _PyObject_EXTRA_INIT
2545 2, &_PySetDummy_Type
2546};
2547