blob: 021b83eb7429f35e58de67ee61772346c47b4ace [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;
Raymond Hettingerafe89092013-08-28 20:59:31 -070055 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t perturb = hash;
58 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080059 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070060 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070061 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080063 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070064 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070066
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070067 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080068 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070069 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080070 /* startkey cannot be a dummy because the dummy hash field is -1 */
71 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080072 if (startkey == key)
73 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080074 if (PyUnicode_CheckExact(startkey)
75 && PyUnicode_CheckExact(key)
76 && unicode_eq(startkey, key))
77 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 Py_INCREF(startkey);
79 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
80 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010081 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010083 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010085 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070086 return entry;
87 }
Raymond Hettingerf8d1a312015-01-26 22:06:43 -080088 if (entry->key == dummy && freeslot == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -070089 freeslot = entry;
90
Raymond Hettingerc70a2b72013-09-21 14:02:55 -070091 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
92 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -070093 if (entry->key == NULL)
94 goto found_null;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080095 if (entry->hash == hash) {
Raymond Hettingera35adf52013-09-02 03:23:21 -070096 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080097 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080098 if (startkey == key)
99 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -0800100 if (PyUnicode_CheckExact(startkey)
101 && PyUnicode_CheckExact(key)
102 && unicode_eq(startkey, key))
103 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700104 Py_INCREF(startkey);
105 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
106 Py_DECREF(startkey);
107 if (cmp < 0)
108 return NULL;
109 if (table != so->table || entry->key != startkey)
110 return set_lookkey(so, key, hash);
111 if (cmp > 0)
112 return entry;
113 }
Raymond Hettingerf8d1a312015-01-26 22:06:43 -0800114 if (entry->key == dummy && freeslot == NULL)
Raymond Hettingera35adf52013-09-02 03:23:21 -0700115 freeslot = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700117
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700118 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800119 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700120
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800121 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700122 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700123 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700125 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700126 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000127}
128
129/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000130Internal routine used by set_table_resize() to insert an item which is
131known to be absent from the set. This routine also assumes that
132the set contains no deleted entries. Besides the performance benefit,
133using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
134Note that no refcounts are changed by this routine; if needed, the caller
135is responsible for incref'ing `key`.
136*/
137static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200138set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200141 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700142 size_t perturb = hash;
143 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800144 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700145 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000146
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700147 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800148 entry = &table[i];
149 if (entry->key == NULL)
150 goto found_null;
151 if (i + LINEAR_PROBES <= mask) {
152 for (j = 1; j <= LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800153 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800154 if (entry->key == NULL)
155 goto found_null;
156 }
157 } else {
158 for (j = 1; j <= LINEAR_PROBES; j++) {
159 entry = &table[(i + j) & mask];
160 if (entry->key == NULL)
161 goto found_null;
162 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700163 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700164 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800165 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700167 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 entry->key = key;
169 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700170 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000172}
173
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700174/* ======== End logic for probing the hash table ========================== */
175/* ======================================================================== */
176
177
178/*
179Internal routine to insert a new key into the table.
180Used by the public insert routine.
181Eats a reference to key.
182*/
183static int
184set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
185{
186 setentry *entry;
187
Raymond Hettinger93035c42015-01-25 16:12:49 -0800188 entry = set_lookkey(so, key, hash);
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700189 if (entry == NULL)
190 return -1;
191 if (entry->key == NULL) {
192 /* UNUSED */
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700193 entry->key = key;
194 entry->hash = hash;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800195 so->fill++;
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700196 so->used++;
197 } else if (entry->key == dummy) {
198 /* DUMMY */
199 entry->key = key;
200 entry->hash = hash;
201 so->used++;
202 } else {
203 /* ACTIVE */
204 Py_DECREF(key);
205 }
206 return 0;
207}
208
Thomas Wouters89f507f2006-12-13 04:49:30 +0000209/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000210Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000211keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000212actually be smaller than the old one.
213*/
214static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000215set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 Py_ssize_t newsize;
218 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800219 Py_ssize_t oldfill = so->fill;
220 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 int is_oldtable_malloced;
222 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100227 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 for (newsize = PySet_MINSIZE;
229 newsize <= minused && newsize > 0;
230 newsize <<= 1)
231 ;
232 if (newsize <= 0) {
233 PyErr_NoMemory();
234 return -1;
235 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 /* Get space for a new table. */
238 oldtable = so->table;
239 assert(oldtable != NULL);
240 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (newsize == PySet_MINSIZE) {
243 /* A large table is shrinking, or we can't get any smaller. */
244 newtable = so->smalltable;
245 if (newtable == oldtable) {
246 if (so->fill == so->used) {
247 /* No dummies, so no point doing anything. */
248 return 0;
249 }
250 /* We're not going to resize it, but rebuild the
251 table anyway to purge old dummy entries.
252 Subtle: This is *necessary* if fill==size,
253 as set_lookkey needs at least one virgin slot to
254 terminate failing searches. If fill < size, it's
255 merely desirable, as dummies slow searches. */
256 assert(so->fill > so->used);
257 memcpy(small_copy, oldtable, sizeof(small_copy));
258 oldtable = small_copy;
259 }
260 }
261 else {
262 newtable = PyMem_NEW(setentry, newsize);
263 if (newtable == NULL) {
264 PyErr_NoMemory();
265 return -1;
266 }
267 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 /* Make the set empty, using the new table. */
270 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800273 so->used = 0;
274 so->mask = newsize - 1;
275 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 /* Copy the data over; this is refcount-neutral for active entries;
278 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800279 if (oldfill == oldused) {
280 for (entry = oldtable; oldused > 0; entry++) {
281 if (entry->key != NULL) {
282 oldused--;
283 set_insert_clean(so, entry->key, entry->hash);
284 }
285 }
286 } else {
287 for (entry = oldtable; oldused > 0; entry++) {
288 if (entry->key != NULL && entry->key != dummy) {
289 oldused--;
290 set_insert_clean(so, entry->key, entry->hash);
291 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 }
293 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 if (is_oldtable_malloced)
296 PyMem_DEL(oldtable);
297 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000298}
299
Raymond Hettingerc991db22005-08-11 07:58:45 +0000300/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
301
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000302static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200303set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000304{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200305 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000306 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100307 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 assert(so->fill <= so->mask); /* at least one empty slot */
310 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000311 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100312 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000313 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 return -1;
315 }
316 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
317 return 0;
318 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000319}
320
321static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200322set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800324 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200325 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200328 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 hash = PyObject_Hash(key);
330 if (hash == -1)
331 return -1;
332 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800333 entry.key = key;
334 entry.hash = hash;
335 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000336}
337
338#define DISCARD_NOTFOUND 0
339#define DISCARD_FOUND 1
340
341static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000342set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800343{
344 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000346
Raymond Hettinger93035c42015-01-25 16:12:49 -0800347 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 if (entry == NULL)
349 return -1;
350 if (entry->key == NULL || entry->key == dummy)
351 return DISCARD_NOTFOUND;
352 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800354 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 so->used--;
356 Py_DECREF(old_key);
357 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000358}
359
360static int
361set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000362{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800363 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200364 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200369 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 hash = PyObject_Hash(key);
371 if (hash == -1)
372 return -1;
373 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800374 entry.key = key;
375 entry.hash = hash;
376 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377}
378
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700379static void
380set_empty_to_minsize(PySetObject *so)
381{
382 memset(so->smalltable, 0, sizeof(so->smalltable));
383 so->fill = 0;
384 so->used = 0;
385 so->mask = PySet_MINSIZE - 1;
386 so->table = so->smalltable;
387 so->hash = -1;
388}
389
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000390static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000391set_clear_internal(PySetObject *so)
392{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800393 setentry *entry;
394 setentry *table = so->table;
395 Py_ssize_t fill = so->fill;
396 Py_ssize_t used = so->used;
397 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000399
Raymond Hettinger583cd032013-09-07 22:06:35 -0700400 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 /* This is delicate. During the process of clearing the set,
404 * decrefs can cause the set to mutate. To avoid fatal confusion
405 * (voice of experience), we have to make the set empty before
406 * clearing the slots, and never refer to anything via so->ref while
407 * clearing.
408 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700410 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 else if (fill > 0) {
413 /* It's a small table with something that needs to be cleared.
414 * Afraid the only safe way is to copy the set entries into
415 * another small table first.
416 */
417 memcpy(small_copy, table, sizeof(small_copy));
418 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700419 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 }
421 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 /* Now we can finally clear things. If C had refcounts, we could
424 * assert that the refcount on table is 1 now, i.e. that this function
425 * has unique access to it, so decref side-effects can't alter it.
426 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800427 for (entry = table; used > 0; entry++) {
428 if (entry->key && entry->key != dummy) {
429 used--;
430 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (table_is_malloced)
435 PyMem_DEL(table);
436 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437}
438
439/*
440 * Iterate over a set table. Use like so:
441 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000442 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000443 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000444 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000445 * while (set_next(yourset, &pos, &entry)) {
446 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000447 * }
448 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000449 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451 */
452static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000453set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 Py_ssize_t i;
456 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200457 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 assert (PyAnySet_Check(so));
460 i = *pos_ptr;
461 assert(i >= 0);
462 table = so->table;
463 mask = so->mask;
464 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
465 i++;
466 *pos_ptr = i+1;
467 if (i > mask)
468 return 0;
469 assert(table[i].key != NULL);
470 *entry_ptr = &table[i];
471 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472}
473
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000474static void
475set_dealloc(PySetObject *so)
476{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200477 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800478 Py_ssize_t used = so->used;
479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 PyObject_GC_UnTrack(so);
481 Py_TRASHCAN_SAFE_BEGIN(so)
482 if (so->weakreflist != NULL)
483 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000484
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800485 for (entry = so->table; used > 0; entry++) {
486 if (entry->key && entry->key != dummy) {
487 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700488 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 }
490 }
491 if (so->table != so->smalltable)
492 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700493 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000495}
496
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000497static PyObject *
498set_repr(PySetObject *so)
499{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200500 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (status != 0) {
504 if (status < 0)
505 return NULL;
506 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
507 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 /* shortcut for the empty set */
510 if (!so->used) {
511 Py_ReprLeave((PyObject*)so);
512 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
513 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 keys = PySequence_List((PyObject *)so);
516 if (keys == NULL)
517 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000518
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200519 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 listrepr = PyObject_Repr(keys);
521 Py_DECREF(keys);
522 if (listrepr == NULL)
523 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200524 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200526 if (tmp == NULL)
527 goto done;
528 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200529
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200530 if (Py_TYPE(so) != &PySet_Type)
531 result = PyUnicode_FromFormat("%s({%U})",
532 Py_TYPE(so)->tp_name,
533 listrepr);
534 else
535 result = PyUnicode_FromFormat("{%U}", listrepr);
536 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000537done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 Py_ReprLeave((PyObject*)so);
539 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000540}
541
Martin v. Löwis18e16552006-02-15 17:27:45 +0000542static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000543set_len(PyObject *so)
544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000546}
547
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000548static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000549set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000552 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100553 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200554 Py_ssize_t i;
555 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 assert (PyAnySet_Check(so));
558 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 other = (PySetObject*)otherset;
561 if (other == so || other->used == 0)
562 /* a.update(a) or a.update({}); nothing to do */
563 return 0;
564 /* Do one big resize at the start, rather than
565 * incrementally resizing as we insert new keys. Expect
566 * that there will be no (or few) overlapping keys.
567 */
568 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
569 if (set_table_resize(so, (so->used + other->used)*2) != 0)
570 return -1;
571 }
572 for (i = 0; i <= other->mask; i++) {
573 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000574 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100575 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000576 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500577 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000578 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100579 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000580 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 return -1;
582 }
583 }
584 }
585 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000586}
587
588static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000589set_contains_entry(PySetObject *so, setentry *entry)
590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PyObject *key;
592 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000593
Raymond Hettinger93035c42015-01-25 16:12:49 -0800594 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 if (lu_entry == NULL)
596 return -1;
597 key = lu_entry->key;
598 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000599}
600
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800601static int
602set_contains_key(PySetObject *so, PyObject *key)
603{
604 setentry entry;
605 Py_hash_t hash;
606
607 if (!PyUnicode_CheckExact(key) ||
608 (hash = ((PyASCIIObject *) key)->hash) == -1) {
609 hash = PyObject_Hash(key);
610 if (hash == -1)
611 return -1;
612 }
613 entry.key = key;
614 entry.hash = hash;
615 return set_contains_entry(so, &entry);
616}
617
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000618static PyObject *
619set_pop(PySetObject *so)
620{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800621 /* Make sure the search finger is in bounds */
622 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200623 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 assert (PyAnySet_Check(so));
627 if (so->used == 0) {
628 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
629 return NULL;
630 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000631
Raymond Hettinger1202a472015-01-18 13:12:42 -0800632 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
633 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800634 if (i > so->mask)
635 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 }
637 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800639 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800641 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000643}
644
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000645PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
646Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000647
648static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000649set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 Py_ssize_t pos = 0;
652 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 while (set_next(so, &pos, &entry))
655 Py_VISIT(entry->key);
656 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000657}
658
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000659static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000660frozenset_hash(PyObject *self)
661{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800662 /* Most of the constants in this hash algorithm are randomly choosen
663 large primes with "interesting bit patterns" and that passed
664 tests for good collision statistics on a variety of problematic
665 datasets such as:
666
667 ps = []
668 for r in range(21):
669 ps += itertools.combinations(range(20), r)
670 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
671
672 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800674 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 setentry *entry;
676 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (so->hash != -1)
679 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000680
Mark Dickinson57e683e2011-09-24 18:18:40 +0100681 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 while (set_next(so, &pos, &entry)) {
683 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800684 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 combinations of a small number of elements with nearby
686 hashes so that many distinct combinations collapse to only
687 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000688 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800689 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800691 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500692 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800693 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200694 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800695 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 so->hash = hash;
697 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000698}
699
Raymond Hettingera9d99362005-08-05 00:01:15 +0000700/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000701
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000702typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject_HEAD
704 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
705 Py_ssize_t si_used;
706 Py_ssize_t si_pos;
707 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000708} setiterobject;
709
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000710static void
711setiter_dealloc(setiterobject *si)
712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 Py_XDECREF(si->si_set);
714 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000715}
716
717static int
718setiter_traverse(setiterobject *si, visitproc visit, void *arg)
719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 Py_VISIT(si->si_set);
721 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000722}
723
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000724static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000725setiter_len(setiterobject *si)
726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 Py_ssize_t len = 0;
728 if (si->si_set != NULL && si->si_used == si->si_set->used)
729 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000730 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000731}
732
Armin Rigof5b3e362006-02-11 21:32:43 +0000733PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000734
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000735static PyObject *setiter_iternext(setiterobject *si);
736
737static PyObject *
738setiter_reduce(setiterobject *si)
739{
740 PyObject *list;
741 setiterobject tmp;
742
743 list = PyList_New(0);
744 if (!list)
745 return NULL;
746
Ezio Melotti0e1af282012-09-28 16:43:40 +0300747 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000748 tmp = *si;
749 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300750
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000751 /* iterate the temporary into a list */
752 for(;;) {
753 PyObject *element = setiter_iternext(&tmp);
754 if (element) {
755 if (PyList_Append(list, element)) {
756 Py_DECREF(element);
757 Py_DECREF(list);
758 Py_XDECREF(tmp.si_set);
759 return NULL;
760 }
761 Py_DECREF(element);
762 } else
763 break;
764 }
765 Py_XDECREF(tmp.si_set);
766 /* check for error */
767 if (tmp.si_set != NULL) {
768 /* we have an error */
769 Py_DECREF(list);
770 return NULL;
771 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200772 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000773}
774
775PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
776
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000777static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000779 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000781};
782
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000783static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200786 Py_ssize_t i, mask;
787 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 if (so == NULL)
791 return NULL;
792 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (si->si_used != so->used) {
795 PyErr_SetString(PyExc_RuntimeError,
796 "Set changed size during iteration");
797 si->si_used = -1; /* Make this state sticky */
798 return NULL;
799 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 i = si->si_pos;
802 assert(i>=0);
803 entry = so->table;
804 mask = so->mask;
805 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
806 i++;
807 si->si_pos = i+1;
808 if (i > mask)
809 goto fail;
810 si->len--;
811 key = entry[i].key;
812 Py_INCREF(key);
813 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000814
815fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 Py_DECREF(so);
817 si->si_set = NULL;
818 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000819}
820
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000821PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 PyVarObject_HEAD_INIT(&PyType_Type, 0)
823 "set_iterator", /* tp_name */
824 sizeof(setiterobject), /* tp_basicsize */
825 0, /* tp_itemsize */
826 /* methods */
827 (destructor)setiter_dealloc, /* tp_dealloc */
828 0, /* tp_print */
829 0, /* tp_getattr */
830 0, /* tp_setattr */
831 0, /* tp_reserved */
832 0, /* tp_repr */
833 0, /* tp_as_number */
834 0, /* tp_as_sequence */
835 0, /* tp_as_mapping */
836 0, /* tp_hash */
837 0, /* tp_call */
838 0, /* tp_str */
839 PyObject_GenericGetAttr, /* tp_getattro */
840 0, /* tp_setattro */
841 0, /* tp_as_buffer */
842 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
843 0, /* tp_doc */
844 (traverseproc)setiter_traverse, /* tp_traverse */
845 0, /* tp_clear */
846 0, /* tp_richcompare */
847 0, /* tp_weaklistoffset */
848 PyObject_SelfIter, /* tp_iter */
849 (iternextfunc)setiter_iternext, /* tp_iternext */
850 setiter_methods, /* tp_methods */
851 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000852};
853
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000854static PyObject *
855set_iter(PySetObject *so)
856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
858 if (si == NULL)
859 return NULL;
860 Py_INCREF(so);
861 si->si_set = so;
862 si->si_used = so->used;
863 si->si_pos = 0;
864 si->len = so->used;
865 _PyObject_GC_TRACK(si);
866 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867}
868
Raymond Hettingerd7946662005-08-01 21:39:29 +0000869static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000870set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (PyAnySet_Check(other))
875 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (PyDict_CheckExact(other)) {
878 PyObject *value;
879 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000880 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 /* Do one big resize at the start, rather than
884 * incrementally resizing as we insert new keys. Expect
885 * that there will be no (or few) overlapping keys.
886 */
887 if (dictsize == -1)
888 return -1;
889 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
890 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
891 return -1;
892 }
893 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
894 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 an_entry.hash = hash;
897 an_entry.key = key;
898 if (set_add_entry(so, &an_entry) == -1)
899 return -1;
900 }
901 return 0;
902 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 it = PyObject_GetIter(other);
905 if (it == NULL)
906 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 while ((key = PyIter_Next(it)) != NULL) {
909 if (set_add_key(so, key) == -1) {
910 Py_DECREF(it);
911 Py_DECREF(key);
912 return -1;
913 }
914 Py_DECREF(key);
915 }
916 Py_DECREF(it);
917 if (PyErr_Occurred())
918 return -1;
919 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000920}
921
922static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000923set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
928 PyObject *other = PyTuple_GET_ITEM(args, i);
929 if (set_update_internal(so, other) == -1)
930 return NULL;
931 }
932 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000933}
934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000936"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000937
Raymond Hettinger426d9952014-05-18 21:40:20 +0100938/* XXX Todo:
939 If aligned memory allocations become available, make the
940 set object 64 byte aligned so that most of the fields
941 can be retrieved or updated in a single cache line.
942*/
943
Raymond Hettingera38123e2003-11-24 22:18:49 +0000944static PyObject *
945make_new_set(PyTypeObject *type, PyObject *iterable)
946{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200947 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700950 so = (PySetObject *)type->tp_alloc(type, 0);
951 if (so == NULL)
952 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000953
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700954 so->fill = 0;
955 so->used = 0;
956 so->mask = PySet_MINSIZE - 1;
957 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700958 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800959 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (iterable != NULL) {
963 if (set_update_internal(so, iterable) == -1) {
964 Py_DECREF(so);
965 return NULL;
966 }
967 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +0000970}
971
Raymond Hettinger7d99f092008-11-16 11:44:54 +0000972static PyObject *
973make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
976 if (PyType_IsSubtype(type, &PySet_Type))
977 type = &PySet_Type;
978 else
979 type = &PyFrozenSet_Type;
980 }
981 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +0000982}
983
Raymond Hettingerd7946662005-08-01 21:39:29 +0000984/* The empty frozenset is a singleton */
985static PyObject *emptyfrozenset = NULL;
986
Raymond Hettingera690a992003-11-16 16:17:49 +0000987static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000988frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
993 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
996 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 if (type != &PyFrozenSet_Type)
999 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 if (iterable != NULL) {
1002 /* frozenset(f) is idempotent */
1003 if (PyFrozenSet_CheckExact(iterable)) {
1004 Py_INCREF(iterable);
1005 return iterable;
1006 }
1007 result = make_new_set(type, iterable);
1008 if (result == NULL || PySet_GET_SIZE(result))
1009 return result;
1010 Py_DECREF(result);
1011 }
1012 /* The empty frozenset is a singleton */
1013 if (emptyfrozenset == NULL)
1014 emptyfrozenset = make_new_set(type, NULL);
1015 Py_XINCREF(emptyfrozenset);
1016 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001017}
1018
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001019int
1020PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001021{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001022 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001023}
1024
1025void
1026PySet_Fini(void)
1027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001029}
1030
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001031static PyObject *
1032set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1035 return NULL;
1036
1037 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001038}
1039
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001040/* set_swap_bodies() switches the contents of any two sets by moving their
1041 internal data pointers and, if needed, copying the internal smalltables.
1042 Semantically equivalent to:
1043
1044 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1045
1046 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001047 Useful for operations that update in-place (by allowing an intermediate
1048 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001049*/
1050
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001051static void
1052set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 Py_ssize_t t;
1055 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001057 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 t = a->fill; a->fill = b->fill; b->fill = t;
1060 t = a->used; a->used = b->used; b->used = t;
1061 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 u = a->table;
1064 if (a->table == a->smalltable)
1065 u = b->smalltable;
1066 a->table = b->table;
1067 if (b->table == b->smalltable)
1068 a->table = a->smalltable;
1069 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 if (a->table == a->smalltable || b->table == b->smalltable) {
1072 memcpy(tab, a->smalltable, sizeof(tab));
1073 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1074 memcpy(b->smalltable, tab, sizeof(tab));
1075 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1078 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1079 h = a->hash; a->hash = b->hash; b->hash = h;
1080 } else {
1081 a->hash = -1;
1082 b->hash = -1;
1083 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001084}
1085
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001086static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001087set_copy(PySetObject *so)
1088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001090}
1091
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001092static PyObject *
1093frozenset_copy(PySetObject *so)
1094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 if (PyFrozenSet_CheckExact(so)) {
1096 Py_INCREF(so);
1097 return (PyObject *)so;
1098 }
1099 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001100}
1101
Raymond Hettingera690a992003-11-16 16:17:49 +00001102PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1103
1104static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001105set_clear(PySetObject *so)
1106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 set_clear_internal(so);
1108 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001109}
1110
1111PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1112
1113static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001114set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 PySetObject *result;
1117 PyObject *other;
1118 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 result = (PySetObject *)set_copy(so);
1121 if (result == NULL)
1122 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1125 other = PyTuple_GET_ITEM(args, i);
1126 if ((PyObject *)so == other)
1127 continue;
1128 if (set_update_internal(result, other) == -1) {
1129 Py_DECREF(result);
1130 return NULL;
1131 }
1132 }
1133 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001134}
1135
1136PyDoc_STRVAR(union_doc,
1137 "Return the union of sets as a new set.\n\
1138\n\
1139(i.e. all elements that are in either set.)");
1140
1141static PyObject *
1142set_or(PySetObject *so, PyObject *other)
1143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001145
Brian Curtindfc80e32011-08-10 20:28:54 -05001146 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1147 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 result = (PySetObject *)set_copy(so);
1150 if (result == NULL)
1151 return NULL;
1152 if ((PyObject *)so == other)
1153 return (PyObject *)result;
1154 if (set_update_internal(result, other) == -1) {
1155 Py_DECREF(result);
1156 return NULL;
1157 }
1158 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001159}
1160
Raymond Hettingera690a992003-11-16 16:17:49 +00001161static PyObject *
1162set_ior(PySetObject *so, PyObject *other)
1163{
Brian Curtindfc80e32011-08-10 20:28:54 -05001164 if (!PyAnySet_Check(other))
1165 Py_RETURN_NOTIMPLEMENTED;
1166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (set_update_internal(so, other) == -1)
1168 return NULL;
1169 Py_INCREF(so);
1170 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001171}
1172
1173static PyObject *
1174set_intersection(PySetObject *so, PyObject *other)
1175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 PySetObject *result;
1177 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if ((PyObject *)so == other)
1180 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1183 if (result == NULL)
1184 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (PyAnySet_Check(other)) {
1187 Py_ssize_t pos = 0;
1188 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1191 tmp = (PyObject *)so;
1192 so = (PySetObject *)other;
1193 other = tmp;
1194 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 while (set_next((PySetObject *)other, &pos, &entry)) {
1197 int rv = set_contains_entry(so, entry);
1198 if (rv == -1) {
1199 Py_DECREF(result);
1200 return NULL;
1201 }
1202 if (rv) {
1203 if (set_add_entry(result, entry) == -1) {
1204 Py_DECREF(result);
1205 return NULL;
1206 }
1207 }
1208 }
1209 return (PyObject *)result;
1210 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 it = PyObject_GetIter(other);
1213 if (it == NULL) {
1214 Py_DECREF(result);
1215 return NULL;
1216 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 while ((key = PyIter_Next(it)) != NULL) {
1219 int rv;
1220 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001221 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 if (hash == -1) {
1224 Py_DECREF(it);
1225 Py_DECREF(result);
1226 Py_DECREF(key);
1227 return NULL;
1228 }
1229 entry.hash = hash;
1230 entry.key = key;
1231 rv = set_contains_entry(so, &entry);
1232 if (rv == -1) {
1233 Py_DECREF(it);
1234 Py_DECREF(result);
1235 Py_DECREF(key);
1236 return NULL;
1237 }
1238 if (rv) {
1239 if (set_add_entry(result, &entry) == -1) {
1240 Py_DECREF(it);
1241 Py_DECREF(result);
1242 Py_DECREF(key);
1243 return NULL;
1244 }
1245 }
1246 Py_DECREF(key);
1247 }
1248 Py_DECREF(it);
1249 if (PyErr_Occurred()) {
1250 Py_DECREF(result);
1251 return NULL;
1252 }
1253 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001254}
1255
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001256static PyObject *
1257set_intersection_multi(PySetObject *so, PyObject *args)
1258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 Py_ssize_t i;
1260 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (PyTuple_GET_SIZE(args) == 0)
1263 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 Py_INCREF(so);
1266 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1267 PyObject *other = PyTuple_GET_ITEM(args, i);
1268 PyObject *newresult = set_intersection((PySetObject *)result, other);
1269 if (newresult == NULL) {
1270 Py_DECREF(result);
1271 return NULL;
1272 }
1273 Py_DECREF(result);
1274 result = newresult;
1275 }
1276 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001277}
1278
Raymond Hettingera690a992003-11-16 16:17:49 +00001279PyDoc_STRVAR(intersection_doc,
1280"Return the intersection of two sets as a new set.\n\
1281\n\
1282(i.e. all elements that are in both sets.)");
1283
1284static PyObject *
1285set_intersection_update(PySetObject *so, PyObject *other)
1286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 tmp = set_intersection(so, other);
1290 if (tmp == NULL)
1291 return NULL;
1292 set_swap_bodies(so, (PySetObject *)tmp);
1293 Py_DECREF(tmp);
1294 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001295}
1296
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001297static PyObject *
1298set_intersection_update_multi(PySetObject *so, PyObject *args)
1299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 tmp = set_intersection_multi(so, args);
1303 if (tmp == NULL)
1304 return NULL;
1305 set_swap_bodies(so, (PySetObject *)tmp);
1306 Py_DECREF(tmp);
1307 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001308}
1309
Raymond Hettingera690a992003-11-16 16:17:49 +00001310PyDoc_STRVAR(intersection_update_doc,
1311"Update a set with the intersection of itself and another.");
1312
1313static PyObject *
1314set_and(PySetObject *so, PyObject *other)
1315{
Brian Curtindfc80e32011-08-10 20:28:54 -05001316 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1317 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001319}
1320
1321static PyObject *
1322set_iand(PySetObject *so, PyObject *other)
1323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001325
Brian Curtindfc80e32011-08-10 20:28:54 -05001326 if (!PyAnySet_Check(other))
1327 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 result = set_intersection_update(so, other);
1329 if (result == NULL)
1330 return NULL;
1331 Py_DECREF(result);
1332 Py_INCREF(so);
1333 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001334}
1335
Guido van Rossum58da9312007-11-10 23:39:45 +00001336static PyObject *
1337set_isdisjoint(PySetObject *so, PyObject *other)
1338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if ((PyObject *)so == other) {
1342 if (PySet_GET_SIZE(so) == 0)
1343 Py_RETURN_TRUE;
1344 else
1345 Py_RETURN_FALSE;
1346 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 if (PyAnySet_CheckExact(other)) {
1349 Py_ssize_t pos = 0;
1350 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1353 tmp = (PyObject *)so;
1354 so = (PySetObject *)other;
1355 other = tmp;
1356 }
1357 while (set_next((PySetObject *)other, &pos, &entry)) {
1358 int rv = set_contains_entry(so, entry);
1359 if (rv == -1)
1360 return NULL;
1361 if (rv)
1362 Py_RETURN_FALSE;
1363 }
1364 Py_RETURN_TRUE;
1365 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 it = PyObject_GetIter(other);
1368 if (it == NULL)
1369 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 while ((key = PyIter_Next(it)) != NULL) {
1372 int rv;
1373 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001374 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if (hash == -1) {
1377 Py_DECREF(key);
1378 Py_DECREF(it);
1379 return NULL;
1380 }
1381 entry.hash = hash;
1382 entry.key = key;
1383 rv = set_contains_entry(so, &entry);
1384 Py_DECREF(key);
1385 if (rv == -1) {
1386 Py_DECREF(it);
1387 return NULL;
1388 }
1389 if (rv) {
1390 Py_DECREF(it);
1391 Py_RETURN_FALSE;
1392 }
1393 }
1394 Py_DECREF(it);
1395 if (PyErr_Occurred())
1396 return NULL;
1397 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001398}
1399
1400PyDoc_STRVAR(isdisjoint_doc,
1401"Return True if two sets have a null intersection.");
1402
Neal Norwitz6576bd82005-11-13 18:41:28 +00001403static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001404set_difference_update_internal(PySetObject *so, PyObject *other)
1405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if ((PyObject *)so == other)
1407 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 if (PyAnySet_Check(other)) {
1410 setentry *entry;
1411 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 while (set_next((PySetObject *)other, &pos, &entry))
1414 if (set_discard_entry(so, entry) == -1)
1415 return -1;
1416 } else {
1417 PyObject *key, *it;
1418 it = PyObject_GetIter(other);
1419 if (it == NULL)
1420 return -1;
1421
1422 while ((key = PyIter_Next(it)) != NULL) {
1423 if (set_discard_key(so, key) == -1) {
1424 Py_DECREF(it);
1425 Py_DECREF(key);
1426 return -1;
1427 }
1428 Py_DECREF(key);
1429 }
1430 Py_DECREF(it);
1431 if (PyErr_Occurred())
1432 return -1;
1433 }
1434 /* If more than 1/5 are dummies, then resize them away. */
1435 if ((so->fill - so->used) * 5 < so->mask)
1436 return 0;
1437 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001438}
1439
Raymond Hettingera690a992003-11-16 16:17:49 +00001440static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001441set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1446 PyObject *other = PyTuple_GET_ITEM(args, i);
1447 if (set_difference_update_internal(so, other) == -1)
1448 return NULL;
1449 }
1450 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001451}
1452
1453PyDoc_STRVAR(difference_update_doc,
1454"Remove all elements of another set from this set.");
1455
1456static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001457set_copy_and_difference(PySetObject *so, PyObject *other)
1458{
1459 PyObject *result;
1460
1461 result = set_copy(so);
1462 if (result == NULL)
1463 return NULL;
1464 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1465 return result;
1466 Py_DECREF(result);
1467 return NULL;
1468}
1469
1470static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001471set_difference(PySetObject *so, PyObject *other)
1472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 PyObject *result;
1474 setentry *entry;
1475 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001478 return set_copy_and_difference(so, other);
1479 }
1480
1481 /* If len(so) much more than len(other), it's more efficient to simply copy
1482 * so and then iterate other looking for common elements. */
1483 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1484 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 result = make_new_set_basetype(Py_TYPE(so), NULL);
1488 if (result == NULL)
1489 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 if (PyDict_CheckExact(other)) {
1492 while (set_next(so, &pos, &entry)) {
1493 setentry entrycopy;
1494 entrycopy.hash = entry->hash;
1495 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001496 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1498 Py_DECREF(result);
1499 return NULL;
1500 }
1501 }
1502 }
1503 return result;
1504 }
1505
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001506 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 while (set_next(so, &pos, &entry)) {
1508 int rv = set_contains_entry((PySetObject *)other, entry);
1509 if (rv == -1) {
1510 Py_DECREF(result);
1511 return NULL;
1512 }
1513 if (!rv) {
1514 if (set_add_entry((PySetObject *)result, entry) == -1) {
1515 Py_DECREF(result);
1516 return NULL;
1517 }
1518 }
1519 }
1520 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001521}
1522
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001523static PyObject *
1524set_difference_multi(PySetObject *so, PyObject *args)
1525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 Py_ssize_t i;
1527 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 if (PyTuple_GET_SIZE(args) == 0)
1530 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 other = PyTuple_GET_ITEM(args, 0);
1533 result = set_difference(so, other);
1534 if (result == NULL)
1535 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1538 other = PyTuple_GET_ITEM(args, i);
1539 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1540 Py_DECREF(result);
1541 return NULL;
1542 }
1543 }
1544 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001545}
1546
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001547PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001548"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001549\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001550(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001551static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001552set_sub(PySetObject *so, PyObject *other)
1553{
Brian Curtindfc80e32011-08-10 20:28:54 -05001554 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1555 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001557}
1558
1559static PyObject *
1560set_isub(PySetObject *so, PyObject *other)
1561{
Brian Curtindfc80e32011-08-10 20:28:54 -05001562 if (!PyAnySet_Check(other))
1563 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (set_difference_update_internal(so, other) == -1)
1565 return NULL;
1566 Py_INCREF(so);
1567 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001568}
1569
1570static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001571set_symmetric_difference_update(PySetObject *so, PyObject *other)
1572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 PySetObject *otherset;
1574 PyObject *key;
1575 Py_ssize_t pos = 0;
1576 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 if ((PyObject *)so == other)
1579 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 if (PyDict_CheckExact(other)) {
1582 PyObject *value;
1583 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001584 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1586 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001587
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001588 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 an_entry.hash = hash;
1590 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001593 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001594 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001597 if (rv == DISCARD_NOTFOUND) {
1598 if (set_add_entry(so, &an_entry) == -1) {
1599 Py_DECREF(key);
1600 return NULL;
1601 }
1602 }
Georg Brandl2d444492010-09-03 10:52:55 +00001603 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 }
1605 Py_RETURN_NONE;
1606 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 if (PyAnySet_Check(other)) {
1609 Py_INCREF(other);
1610 otherset = (PySetObject *)other;
1611 } else {
1612 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1613 if (otherset == NULL)
1614 return NULL;
1615 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 while (set_next(otherset, &pos, &entry)) {
1618 int rv = set_discard_entry(so, entry);
1619 if (rv == -1) {
1620 Py_DECREF(otherset);
1621 return NULL;
1622 }
1623 if (rv == DISCARD_NOTFOUND) {
1624 if (set_add_entry(so, entry) == -1) {
1625 Py_DECREF(otherset);
1626 return NULL;
1627 }
1628 }
1629 }
1630 Py_DECREF(otherset);
1631 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001632}
1633
1634PyDoc_STRVAR(symmetric_difference_update_doc,
1635"Update a set with the symmetric difference of itself and another.");
1636
1637static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001638set_symmetric_difference(PySetObject *so, PyObject *other)
1639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 PyObject *rv;
1641 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1644 if (otherset == NULL)
1645 return NULL;
1646 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1647 if (rv == NULL)
1648 return NULL;
1649 Py_DECREF(rv);
1650 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001651}
1652
1653PyDoc_STRVAR(symmetric_difference_doc,
1654"Return the symmetric difference of two sets as a new set.\n\
1655\n\
1656(i.e. all elements that are in exactly one of the sets.)");
1657
1658static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001659set_xor(PySetObject *so, PyObject *other)
1660{
Brian Curtindfc80e32011-08-10 20:28:54 -05001661 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1662 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001664}
1665
1666static PyObject *
1667set_ixor(PySetObject *so, PyObject *other)
1668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001670
Brian Curtindfc80e32011-08-10 20:28:54 -05001671 if (!PyAnySet_Check(other))
1672 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 result = set_symmetric_difference_update(so, other);
1674 if (result == NULL)
1675 return NULL;
1676 Py_DECREF(result);
1677 Py_INCREF(so);
1678 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001679}
1680
1681static PyObject *
1682set_issubset(PySetObject *so, PyObject *other)
1683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 setentry *entry;
1685 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 if (!PyAnySet_Check(other)) {
1688 PyObject *tmp, *result;
1689 tmp = make_new_set(&PySet_Type, other);
1690 if (tmp == NULL)
1691 return NULL;
1692 result = set_issubset(so, tmp);
1693 Py_DECREF(tmp);
1694 return result;
1695 }
1696 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1697 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 while (set_next(so, &pos, &entry)) {
1700 int rv = set_contains_entry((PySetObject *)other, entry);
1701 if (rv == -1)
1702 return NULL;
1703 if (!rv)
1704 Py_RETURN_FALSE;
1705 }
1706 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001707}
1708
1709PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1710
1711static PyObject *
1712set_issuperset(PySetObject *so, PyObject *other)
1713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 if (!PyAnySet_Check(other)) {
1717 tmp = make_new_set(&PySet_Type, other);
1718 if (tmp == NULL)
1719 return NULL;
1720 result = set_issuperset(so, tmp);
1721 Py_DECREF(tmp);
1722 return result;
1723 }
1724 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001725}
1726
1727PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1728
Raymond Hettingera690a992003-11-16 16:17:49 +00001729static PyObject *
1730set_richcompare(PySetObject *v, PyObject *w, int op)
1731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001733
Brian Curtindfc80e32011-08-10 20:28:54 -05001734 if(!PyAnySet_Check(w))
1735 Py_RETURN_NOTIMPLEMENTED;
1736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 switch (op) {
1738 case Py_EQ:
1739 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1740 Py_RETURN_FALSE;
1741 if (v->hash != -1 &&
1742 ((PySetObject *)w)->hash != -1 &&
1743 v->hash != ((PySetObject *)w)->hash)
1744 Py_RETURN_FALSE;
1745 return set_issubset(v, w);
1746 case Py_NE:
1747 r1 = set_richcompare(v, w, Py_EQ);
1748 if (r1 == NULL)
1749 return NULL;
1750 r2 = PyBool_FromLong(PyObject_Not(r1));
1751 Py_DECREF(r1);
1752 return r2;
1753 case Py_LE:
1754 return set_issubset(v, w);
1755 case Py_GE:
1756 return set_issuperset(v, w);
1757 case Py_LT:
1758 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1759 Py_RETURN_FALSE;
1760 return set_issubset(v, w);
1761 case Py_GT:
1762 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1763 Py_RETURN_FALSE;
1764 return set_issuperset(v, w);
1765 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001766 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001767}
1768
Raymond Hettingera690a992003-11-16 16:17:49 +00001769static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001770set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 if (set_add_key(so, key) == -1)
1773 return NULL;
1774 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001775}
1776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001778"Add an element to a set.\n\
1779\n\
1780This has no effect if the element is already present.");
1781
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001782static int
1783set_contains(PySetObject *so, PyObject *key)
1784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 PyObject *tmpkey;
1786 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 rv = set_contains_key(so, key);
1789 if (rv == -1) {
1790 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1791 return -1;
1792 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001793 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 if (tmpkey == NULL)
1795 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001796 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 Py_DECREF(tmpkey);
1798 }
1799 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001800}
1801
1802static PyObject *
1803set_direct_contains(PySetObject *so, PyObject *key)
1804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 result = set_contains(so, key);
1808 if (result == -1)
1809 return NULL;
1810 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001811}
1812
1813PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1814
Raymond Hettingera690a992003-11-16 16:17:49 +00001815static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001816set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 PyObject *tmpkey;
1819 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 rv = set_discard_key(so, key);
1822 if (rv == -1) {
1823 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1824 return NULL;
1825 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001826 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 if (tmpkey == NULL)
1828 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 Py_DECREF(tmpkey);
1831 if (rv == -1)
1832 return NULL;
1833 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001836 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 return NULL;
1838 }
1839 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001840}
1841
1842PyDoc_STRVAR(remove_doc,
1843"Remove an element from a set; it must be a member.\n\
1844\n\
1845If the element is not a member, raise a KeyError.");
1846
1847static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001848set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001849{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001850 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 rv = set_discard_key(so, key);
1854 if (rv == -1) {
1855 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1856 return NULL;
1857 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001858 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 if (tmpkey == NULL)
1860 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001861 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001863 if (rv == -1)
1864 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 }
1866 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001867}
1868
1869PyDoc_STRVAR(discard_doc,
1870"Remove an element from a set if it is a member.\n\
1871\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001873
1874static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001875set_reduce(PySetObject *so)
1876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001878 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 keys = PySequence_List((PyObject *)so);
1881 if (keys == NULL)
1882 goto done;
1883 args = PyTuple_Pack(1, keys);
1884 if (args == NULL)
1885 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001886 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 if (dict == NULL) {
1888 PyErr_Clear();
1889 dict = Py_None;
1890 Py_INCREF(dict);
1891 }
1892 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001893done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 Py_XDECREF(args);
1895 Py_XDECREF(keys);
1896 Py_XDECREF(dict);
1897 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001898}
1899
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001900static PyObject *
1901set_sizeof(PySetObject *so)
1902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 res = sizeof(PySetObject);
1906 if (so->table != so->smalltable)
1907 res = res + (so->mask + 1) * sizeof(setentry);
1908 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001909}
1910
1911PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001912static int
1913set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 if (!PyAnySet_Check(self))
1918 return -1;
1919 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1920 return -1;
1921 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1922 return -1;
1923 set_clear_internal(self);
1924 self->hash = -1;
1925 if (iterable == NULL)
1926 return 0;
1927 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001928}
1929
Raymond Hettingera690a992003-11-16 16:17:49 +00001930static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 set_len, /* sq_length */
1932 0, /* sq_concat */
1933 0, /* sq_repeat */
1934 0, /* sq_item */
1935 0, /* sq_slice */
1936 0, /* sq_ass_item */
1937 0, /* sq_ass_slice */
1938 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001939};
1940
1941/* set object ********************************************************/
1942
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001943#ifdef Py_DEBUG
1944static PyObject *test_c_api(PySetObject *so);
1945
1946PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1947All is well if assertions don't fail.");
1948#endif
1949
Raymond Hettingera690a992003-11-16 16:17:49 +00001950static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 {"add", (PyCFunction)set_add, METH_O,
1952 add_doc},
1953 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1954 clear_doc},
1955 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1956 contains_doc},
1957 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1958 copy_doc},
1959 {"discard", (PyCFunction)set_discard, METH_O,
1960 discard_doc},
1961 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
1962 difference_doc},
1963 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
1964 difference_update_doc},
1965 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
1966 intersection_doc},
1967 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
1968 intersection_update_doc},
1969 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1970 isdisjoint_doc},
1971 {"issubset", (PyCFunction)set_issubset, METH_O,
1972 issubset_doc},
1973 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1974 issuperset_doc},
1975 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1976 pop_doc},
1977 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1978 reduce_doc},
1979 {"remove", (PyCFunction)set_remove, METH_O,
1980 remove_doc},
1981 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
1982 sizeof_doc},
1983 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1984 symmetric_difference_doc},
1985 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1986 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001987#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1989 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001990#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 {"union", (PyCFunction)set_union, METH_VARARGS,
1992 union_doc},
1993 {"update", (PyCFunction)set_update, METH_VARARGS,
1994 update_doc},
1995 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00001996};
1997
1998static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 0, /*nb_add*/
2000 (binaryfunc)set_sub, /*nb_subtract*/
2001 0, /*nb_multiply*/
2002 0, /*nb_remainder*/
2003 0, /*nb_divmod*/
2004 0, /*nb_power*/
2005 0, /*nb_negative*/
2006 0, /*nb_positive*/
2007 0, /*nb_absolute*/
2008 0, /*nb_bool*/
2009 0, /*nb_invert*/
2010 0, /*nb_lshift*/
2011 0, /*nb_rshift*/
2012 (binaryfunc)set_and, /*nb_and*/
2013 (binaryfunc)set_xor, /*nb_xor*/
2014 (binaryfunc)set_or, /*nb_or*/
2015 0, /*nb_int*/
2016 0, /*nb_reserved*/
2017 0, /*nb_float*/
2018 0, /*nb_inplace_add*/
2019 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2020 0, /*nb_inplace_multiply*/
2021 0, /*nb_inplace_remainder*/
2022 0, /*nb_inplace_power*/
2023 0, /*nb_inplace_lshift*/
2024 0, /*nb_inplace_rshift*/
2025 (binaryfunc)set_iand, /*nb_inplace_and*/
2026 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2027 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002028};
2029
2030PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002031"set() -> new empty set object\n\
2032set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002033\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002034Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002035
2036PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2038 "set", /* tp_name */
2039 sizeof(PySetObject), /* tp_basicsize */
2040 0, /* tp_itemsize */
2041 /* methods */
2042 (destructor)set_dealloc, /* tp_dealloc */
2043 0, /* tp_print */
2044 0, /* tp_getattr */
2045 0, /* tp_setattr */
2046 0, /* tp_reserved */
2047 (reprfunc)set_repr, /* tp_repr */
2048 &set_as_number, /* tp_as_number */
2049 &set_as_sequence, /* tp_as_sequence */
2050 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002051 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 0, /* tp_call */
2053 0, /* tp_str */
2054 PyObject_GenericGetAttr, /* tp_getattro */
2055 0, /* tp_setattro */
2056 0, /* tp_as_buffer */
2057 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2058 Py_TPFLAGS_BASETYPE, /* tp_flags */
2059 set_doc, /* tp_doc */
2060 (traverseproc)set_traverse, /* tp_traverse */
2061 (inquiry)set_clear_internal, /* tp_clear */
2062 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002063 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2064 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 0, /* tp_iternext */
2066 set_methods, /* tp_methods */
2067 0, /* tp_members */
2068 0, /* tp_getset */
2069 0, /* tp_base */
2070 0, /* tp_dict */
2071 0, /* tp_descr_get */
2072 0, /* tp_descr_set */
2073 0, /* tp_dictoffset */
2074 (initproc)set_init, /* tp_init */
2075 PyType_GenericAlloc, /* tp_alloc */
2076 set_new, /* tp_new */
2077 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002078};
2079
2080/* frozenset object ********************************************************/
2081
2082
2083static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2085 contains_doc},
2086 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2087 copy_doc},
2088 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2089 difference_doc},
2090 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2091 intersection_doc},
2092 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2093 isdisjoint_doc},
2094 {"issubset", (PyCFunction)set_issubset, METH_O,
2095 issubset_doc},
2096 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2097 issuperset_doc},
2098 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2099 reduce_doc},
2100 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2101 sizeof_doc},
2102 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2103 symmetric_difference_doc},
2104 {"union", (PyCFunction)set_union, METH_VARARGS,
2105 union_doc},
2106 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002107};
2108
2109static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 0, /*nb_add*/
2111 (binaryfunc)set_sub, /*nb_subtract*/
2112 0, /*nb_multiply*/
2113 0, /*nb_remainder*/
2114 0, /*nb_divmod*/
2115 0, /*nb_power*/
2116 0, /*nb_negative*/
2117 0, /*nb_positive*/
2118 0, /*nb_absolute*/
2119 0, /*nb_bool*/
2120 0, /*nb_invert*/
2121 0, /*nb_lshift*/
2122 0, /*nb_rshift*/
2123 (binaryfunc)set_and, /*nb_and*/
2124 (binaryfunc)set_xor, /*nb_xor*/
2125 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002126};
2127
2128PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002129"frozenset() -> empty frozenset object\n\
2130frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002131\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002132Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002133
2134PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2136 "frozenset", /* tp_name */
2137 sizeof(PySetObject), /* tp_basicsize */
2138 0, /* tp_itemsize */
2139 /* methods */
2140 (destructor)set_dealloc, /* tp_dealloc */
2141 0, /* tp_print */
2142 0, /* tp_getattr */
2143 0, /* tp_setattr */
2144 0, /* tp_reserved */
2145 (reprfunc)set_repr, /* tp_repr */
2146 &frozenset_as_number, /* tp_as_number */
2147 &set_as_sequence, /* tp_as_sequence */
2148 0, /* tp_as_mapping */
2149 frozenset_hash, /* tp_hash */
2150 0, /* tp_call */
2151 0, /* tp_str */
2152 PyObject_GenericGetAttr, /* tp_getattro */
2153 0, /* tp_setattro */
2154 0, /* tp_as_buffer */
2155 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2156 Py_TPFLAGS_BASETYPE, /* tp_flags */
2157 frozenset_doc, /* tp_doc */
2158 (traverseproc)set_traverse, /* tp_traverse */
2159 (inquiry)set_clear_internal, /* tp_clear */
2160 (richcmpfunc)set_richcompare, /* tp_richcompare */
2161 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2162 (getiterfunc)set_iter, /* tp_iter */
2163 0, /* tp_iternext */
2164 frozenset_methods, /* tp_methods */
2165 0, /* tp_members */
2166 0, /* tp_getset */
2167 0, /* tp_base */
2168 0, /* tp_dict */
2169 0, /* tp_descr_get */
2170 0, /* tp_descr_set */
2171 0, /* tp_dictoffset */
2172 0, /* tp_init */
2173 PyType_GenericAlloc, /* tp_alloc */
2174 frozenset_new, /* tp_new */
2175 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002176};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002177
2178
2179/***** C API functions *************************************************/
2180
2181PyObject *
2182PySet_New(PyObject *iterable)
2183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002185}
2186
2187PyObject *
2188PyFrozenSet_New(PyObject *iterable)
2189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002191}
2192
Neal Norwitz8c49c822006-03-04 18:41:19 +00002193Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002194PySet_Size(PyObject *anyset)
2195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 if (!PyAnySet_Check(anyset)) {
2197 PyErr_BadInternalCall();
2198 return -1;
2199 }
2200 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002201}
2202
2203int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002204PySet_Clear(PyObject *set)
2205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 if (!PySet_Check(set)) {
2207 PyErr_BadInternalCall();
2208 return -1;
2209 }
2210 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002211}
2212
2213int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002214PySet_Contains(PyObject *anyset, PyObject *key)
2215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (!PyAnySet_Check(anyset)) {
2217 PyErr_BadInternalCall();
2218 return -1;
2219 }
2220 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002221}
2222
2223int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002224PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 if (!PySet_Check(set)) {
2227 PyErr_BadInternalCall();
2228 return -1;
2229 }
2230 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002231}
2232
2233int
Christian Heimesfd66e512008-01-29 12:18:50 +00002234PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 if (!PySet_Check(anyset) &&
2237 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2238 PyErr_BadInternalCall();
2239 return -1;
2240 }
2241 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002242}
2243
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002244int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002245_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 if (!PyAnySet_Check(set)) {
2250 PyErr_BadInternalCall();
2251 return -1;
2252 }
2253 if (set_next((PySetObject *)set, pos, &entry) == 0)
2254 return 0;
2255 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002256 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002258}
2259
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002260PyObject *
2261PySet_Pop(PyObject *set)
2262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 if (!PySet_Check(set)) {
2264 PyErr_BadInternalCall();
2265 return NULL;
2266 }
2267 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002268}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002269
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002270int
2271_PySet_Update(PyObject *set, PyObject *iterable)
2272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 if (!PySet_Check(set)) {
2274 PyErr_BadInternalCall();
2275 return -1;
2276 }
2277 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002278}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002279
Raymond Hettingere259f132013-12-15 11:56:14 -08002280/* Exported for the gdb plugin's benefit. */
2281PyObject *_PySet_Dummy = dummy;
2282
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002283#ifdef Py_DEBUG
2284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002286 Returns True and original set is restored. */
2287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288#define assertRaises(call_return_value, exception) \
2289 do { \
2290 assert(call_return_value); \
2291 assert(PyErr_ExceptionMatches(exception)); \
2292 PyErr_Clear(); \
2293 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002294
2295static PyObject *
2296test_c_api(PySetObject *so)
2297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 Py_ssize_t count;
2299 char *s;
2300 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002301 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002303 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 /* Verify preconditions */
2307 assert(PyAnySet_Check(ob));
2308 assert(PyAnySet_CheckExact(ob));
2309 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 /* so.clear(); so |= set("abc"); */
2312 str = PyUnicode_FromString("abc");
2313 if (str == NULL)
2314 return NULL;
2315 set_clear_internal(so);
2316 if (set_update_internal(so, str) == -1) {
2317 Py_DECREF(str);
2318 return NULL;
2319 }
2320 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 /* Exercise type/size checks */
2323 assert(PySet_Size(ob) == 3);
2324 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 /* Raise TypeError for non-iterable constructor arguments */
2327 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2328 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 /* Raise TypeError for unhashable key */
2331 dup = PySet_New(ob);
2332 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2333 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2334 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 /* Exercise successful pop, contains, add, and discard */
2337 elem = PySet_Pop(ob);
2338 assert(PySet_Contains(ob, elem) == 0);
2339 assert(PySet_GET_SIZE(ob) == 2);
2340 assert(PySet_Add(ob, elem) == 0);
2341 assert(PySet_Contains(ob, elem) == 1);
2342 assert(PySet_GET_SIZE(ob) == 3);
2343 assert(PySet_Discard(ob, elem) == 1);
2344 assert(PySet_GET_SIZE(ob) == 2);
2345 assert(PySet_Discard(ob, elem) == 0);
2346 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 /* Exercise clear */
2349 dup2 = PySet_New(dup);
2350 assert(PySet_Clear(dup2) == 0);
2351 assert(PySet_Size(dup2) == 0);
2352 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 /* Raise SystemError on clear or update of frozen set */
2355 f = PyFrozenSet_New(dup);
2356 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2357 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2358 assert(PySet_Add(f, elem) == 0);
2359 Py_INCREF(f);
2360 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2361 Py_DECREF(f);
2362 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* Exercise direct iteration */
2365 i = 0, count = 0;
2366 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2367 s = _PyUnicode_AsString(x);
2368 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2369 count++;
2370 }
2371 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 /* Exercise updates */
2374 dup2 = PySet_New(NULL);
2375 assert(_PySet_Update(dup2, dup) == 0);
2376 assert(PySet_Size(dup2) == 3);
2377 assert(_PySet_Update(dup2, dup) == 0);
2378 assert(PySet_Size(dup2) == 3);
2379 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 /* Raise SystemError when self argument is not a set or frozenset. */
2382 t = PyTuple_New(0);
2383 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2384 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2385 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 /* Raise SystemError when self argument is not a set. */
2388 f = PyFrozenSet_New(dup);
2389 assert(PySet_Size(f) == 3);
2390 assert(PyFrozenSet_CheckExact(f));
2391 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2392 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2393 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 /* Raise KeyError when popping from an empty set */
2396 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2397 Py_DECREF(ob);
2398 assert(PySet_GET_SIZE(ob) == 0);
2399 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* Restore the set from the copy using the PyNumber API */
2402 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2403 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* Verify constructors accept NULL arguments */
2406 f = PySet_New(NULL);
2407 assert(f != NULL);
2408 assert(PySet_GET_SIZE(f) == 0);
2409 Py_DECREF(f);
2410 f = PyFrozenSet_New(NULL);
2411 assert(f != NULL);
2412 assert(PyFrozenSet_CheckExact(f));
2413 assert(PySet_GET_SIZE(f) == 0);
2414 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 Py_DECREF(elem);
2417 Py_DECREF(dup);
2418 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002419}
2420
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002421#undef assertRaises
2422
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002423#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002424
2425/***** Dummy Struct *************************************************/
2426
2427static PyObject *
2428dummy_repr(PyObject *op)
2429{
2430 return PyUnicode_FromString("<dummy key>");
2431}
2432
2433static void
2434dummy_dealloc(PyObject* ignore)
2435{
2436 Py_FatalError("deallocating <dummy key>");
2437}
2438
2439static PyTypeObject _PySetDummy_Type = {
2440 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2441 "<dummy key> type",
2442 0,
2443 0,
2444 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2445 0, /*tp_print*/
2446 0, /*tp_getattr*/
2447 0, /*tp_setattr*/
2448 0, /*tp_reserved*/
2449 dummy_repr, /*tp_repr*/
2450 0, /*tp_as_number*/
2451 0, /*tp_as_sequence*/
2452 0, /*tp_as_mapping*/
2453 0, /*tp_hash */
2454 0, /*tp_call */
2455 0, /*tp_str */
2456 0, /*tp_getattro */
2457 0, /*tp_setattro */
2458 0, /*tp_as_buffer */
2459 Py_TPFLAGS_DEFAULT, /*tp_flags */
2460};
2461
2462static PyObject _dummy_struct = {
2463 _PyObject_EXTRA_INIT
2464 2, &_PySetDummy_Type
2465};
2466