blob: e2cb666b67596849b22d053d5100d5279d5ce8ce [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 Hettinger6c3c1cc2013-08-31 21:34:24 -07007 Copyright (c) 2003-2013 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
26 Unlike the dictionary implementation, the lookkey functions can return
27 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 Hettingerc56e0e32013-09-02 16:32:27 -070034/* This must be >= 1 */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035#define PERTURB_SHIFT 5
Raymond Hettingerc56e0e32013-09-02 16:32:27 -070036
37/* This should be >= PySet_MINSIZE - 1 */
Raymond Hettingera35adf52013-09-02 03:23:21 -070038#define LINEAR_PROBES 9
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
40/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050041static PyObject _dummy_struct;
42
43#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000044
Antoine Pitrou9d952542013-08-24 21:07:07 +020045/* Exported for the gdb plugin's benefit. */
46PyObject *_PySet_Dummy = dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define INIT_NONZERO_SET_SLOTS(so) do { \
49 (so)->table = (so)->smalltable; \
50 (so)->mask = PySet_MINSIZE - 1; \
51 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000052 } while(0)
53
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054#define EMPTY_TO_MINSIZE(so) do { \
55 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
56 (so)->used = (so)->fill = 0; \
57 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000058 } while(0)
59
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070060/* ======================================================================== */
61/* ======= Begin logic for probing the hash table ========================= */
62
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000063static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020064set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070067 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020068 setentry *entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -070069 setentry *limit;
Raymond Hettingerafe89092013-08-28 20:59:31 -070070 size_t perturb = hash;
71 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -070072 size_t i = (size_t)hash; /* Unsigned for defined overflow behavior. */
73 size_t j;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020074 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075
Raymond Hettingera35adf52013-09-02 03:23:21 -070076 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -070077 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070079
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070080 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070082 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070083 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070084 PyObject *startkey = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 Py_INCREF(startkey);
86 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
87 Py_DECREF(startkey);
88 if (cmp < 0)
89 return NULL;
Raymond Hettingerafe89092013-08-28 20:59:31 -070090 if (table != so->table || entry->key != startkey)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 return set_lookkey(so, key, hash);
Raymond Hettingerafe89092013-08-28 20:59:31 -070092 if (cmp > 0)
93 return entry;
94 }
95 if (entry->key == dummy && freeslot == NULL)
96 freeslot = entry;
97
Raymond Hettingera35adf52013-09-02 03:23:21 -070098 limit = &table[mask];
99 for (j = 0 ; j < LINEAR_PROBES ; j++) {
100 entry = (entry == limit) ? &table[0] : entry + 1;
101 if (entry->key == NULL)
102 goto found_null;
103 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -0700104 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700105 if (entry->hash == hash && entry->key != dummy) {
106 PyObject *startkey = entry->key;
107 Py_INCREF(startkey);
108 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
109 Py_DECREF(startkey);
110 if (cmp < 0)
111 return NULL;
112 if (table != so->table || entry->key != startkey)
113 return set_lookkey(so, key, hash);
114 if (cmp > 0)
115 return entry;
116 }
117 if (entry->key == dummy && freeslot == NULL)
118 freeslot = entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700120
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700121 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700122 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700123
Raymond Hettingera35adf52013-09-02 03:23:21 -0700124 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700125 if (entry->key == NULL)
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700126 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700128 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700129 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130}
131
132/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000133 * Hacked up version of set_lookkey which can assume keys are always unicode;
134 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000135 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000136 */
137static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200138set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700141 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200142 setentry *entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700143 setentry *limit;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700144 size_t perturb = hash;
145 size_t mask = so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700146 size_t i = (size_t)hash;
147 size_t j;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 /* Make sure this function doesn't have to handle non-unicode keys,
150 including subclasses of str; e.g., one reason to subclass
151 strings is to override __eq__, and for speed we don't cater to
152 that here. */
153 if (!PyUnicode_CheckExact(key)) {
154 so->lookup = set_lookkey;
155 return set_lookkey(so, key, hash);
156 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700157
Raymond Hettingera35adf52013-09-02 03:23:21 -0700158 entry = &table[i & mask];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700159 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000161
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700162 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700163 if (entry->key == key
164 || (entry->hash == hash
165 && entry->key != dummy
166 && unicode_eq(entry->key, key)))
167 return entry;
168 if (entry->key == dummy && freeslot == NULL)
169 freeslot = entry;
170
Raymond Hettingera35adf52013-09-02 03:23:21 -0700171 limit = &table[mask];
172 for (j = 0 ; j < LINEAR_PROBES ; j++) {
173 entry = (entry == limit) ? &table[0] : entry + 1;
174 if (entry->key == NULL)
175 goto found_null;
176 if (entry->key == key
177 || (entry->hash == hash
178 && entry->key != dummy
179 && unicode_eq(entry->key, key)))
180 return entry;
181 if (entry->key == dummy && freeslot == NULL)
182 freeslot = entry;
183 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700184
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700185 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700186 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700187
Raymond Hettingera35adf52013-09-02 03:23:21 -0700188 entry = &table[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 if (entry->key == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -0700190 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700192 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700193 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000194}
195
196/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000197Internal routine used by set_table_resize() to insert an item which is
198known to be absent from the set. This routine also assumes that
199the set contains no deleted entries. Besides the performance benefit,
200using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
201Note that no refcounts are changed by this routine; if needed, the caller
202is responsible for incref'ing `key`.
203*/
204static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200205set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200208 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700209 size_t perturb = hash;
210 size_t mask = (size_t)so->mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700211 size_t i = (size_t)hash;
212 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000213
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700214 while (1) {
Raymond Hettingera35adf52013-09-02 03:23:21 -0700215 entry = &table[i & mask];
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700216 if (entry->key == NULL)
Raymond Hettingera35adf52013-09-02 03:23:21 -0700217 goto found_null;
218 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
219 entry = &table[(i + j) & mask];
220 if (entry->key == NULL)
221 goto found_null;
222 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700223 perturb >>= PERTURB_SHIFT;
Raymond Hettingerc56e0e32013-09-02 16:32:27 -0700224 i = i * 5 + 1 + perturb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700226 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 so->fill++;
228 entry->key = key;
229 entry->hash = hash;
230 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000231}
232
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700233/* ======== End logic for probing the hash table ========================== */
234/* ======================================================================== */
235
236
237/*
238Internal routine to insert a new key into the table.
239Used by the public insert routine.
240Eats a reference to key.
241*/
242static int
243set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
244{
245 setentry *entry;
246
247 assert(so->lookup != NULL);
248 entry = so->lookup(so, key, hash);
249 if (entry == NULL)
250 return -1;
251 if (entry->key == NULL) {
252 /* UNUSED */
253 so->fill++;
254 entry->key = key;
255 entry->hash = hash;
256 so->used++;
257 } else if (entry->key == dummy) {
258 /* DUMMY */
259 entry->key = key;
260 entry->hash = hash;
261 so->used++;
262 } else {
263 /* ACTIVE */
264 Py_DECREF(key);
265 }
266 return 0;
267}
268
Thomas Wouters89f507f2006-12-13 04:49:30 +0000269/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000271keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272actually be smaller than the old one.
273*/
274static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_ssize_t newsize;
278 setentry *oldtable, *newtable, *entry;
279 Py_ssize_t i;
280 int is_oldtable_malloced;
281 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 /* Find the smallest table size > minused. */
286 for (newsize = PySet_MINSIZE;
287 newsize <= minused && newsize > 0;
288 newsize <<= 1)
289 ;
290 if (newsize <= 0) {
291 PyErr_NoMemory();
292 return -1;
293 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* Get space for a new table. */
296 oldtable = so->table;
297 assert(oldtable != NULL);
298 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 if (newsize == PySet_MINSIZE) {
301 /* A large table is shrinking, or we can't get any smaller. */
302 newtable = so->smalltable;
303 if (newtable == oldtable) {
304 if (so->fill == so->used) {
305 /* No dummies, so no point doing anything. */
306 return 0;
307 }
308 /* We're not going to resize it, but rebuild the
309 table anyway to purge old dummy entries.
310 Subtle: This is *necessary* if fill==size,
311 as set_lookkey needs at least one virgin slot to
312 terminate failing searches. If fill < size, it's
313 merely desirable, as dummies slow searches. */
314 assert(so->fill > so->used);
315 memcpy(small_copy, oldtable, sizeof(small_copy));
316 oldtable = small_copy;
317 }
318 }
319 else {
320 newtable = PyMem_NEW(setentry, newsize);
321 if (newtable == NULL) {
322 PyErr_NoMemory();
323 return -1;
324 }
325 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 /* Make the set empty, using the new table. */
328 assert(newtable != oldtable);
329 so->table = newtable;
330 so->mask = newsize - 1;
331 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700332 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* Copy the data over; this is refcount-neutral for active entries;
337 dummy entries aren't copied over, of course */
338 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500339 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000341 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 }
343 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 if (is_oldtable_malloced)
346 PyMem_DEL(oldtable);
347 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000348}
349
Raymond Hettingerc991db22005-08-11 07:58:45 +0000350/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
351
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000352static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200353set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000354{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200355 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000356 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100357 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 assert(so->fill <= so->mask); /* at least one empty slot */
360 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000361 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100362 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000363 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 return -1;
365 }
366 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
367 return 0;
368 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000369}
370
371static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200372set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000373{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200374 Py_hash_t hash;
375 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200378 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 hash = PyObject_Hash(key);
380 if (hash == -1)
381 return -1;
382 }
383 assert(so->fill <= so->mask); /* at least one empty slot */
384 n_used = so->used;
385 Py_INCREF(key);
386 if (set_insert_key(so, key, hash) == -1) {
387 Py_DECREF(key);
388 return -1;
389 }
390 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
391 return 0;
392 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000393}
394
395#define DISCARD_NOTFOUND 0
396#define DISCARD_FOUND 1
397
398static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000399set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200400{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000402
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000403 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 if (entry == NULL)
405 return -1;
406 if (entry->key == NULL || entry->key == dummy)
407 return DISCARD_NOTFOUND;
408 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 entry->key = dummy;
410 so->used--;
411 Py_DECREF(old_key);
412 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000413}
414
415static int
416set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000417{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200418 Py_hash_t hash;
419 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200425 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 hash = PyObject_Hash(key);
427 if (hash == -1)
428 return -1;
429 }
430 entry = (so->lookup)(so, key, hash);
431 if (entry == NULL)
432 return -1;
433 if (entry->key == NULL || entry->key == dummy)
434 return DISCARD_NOTFOUND;
435 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 entry->key = dummy;
437 so->used--;
438 Py_DECREF(old_key);
439 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000440}
441
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000442static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443set_clear_internal(PySetObject *so)
444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 setentry *entry, *table;
446 int table_is_malloced;
447 Py_ssize_t fill;
448 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 Py_ssize_t i, n;
451 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 n = so->mask + 1;
454 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455#endif
456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 table = so->table;
458 assert(table != NULL);
459 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 /* This is delicate. During the process of clearing the set,
462 * decrefs can cause the set to mutate. To avoid fatal confusion
463 * (voice of experience), we have to make the set empty before
464 * clearing the slots, and never refer to anything via so->ref while
465 * clearing.
466 */
467 fill = so->fill;
468 if (table_is_malloced)
469 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 else if (fill > 0) {
472 /* It's a small table with something that needs to be cleared.
473 * Afraid the only safe way is to copy the set entries into
474 * another small table first.
475 */
476 memcpy(small_copy, table, sizeof(small_copy));
477 table = small_copy;
478 EMPTY_TO_MINSIZE(so);
479 }
480 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 /* Now we can finally clear things. If C had refcounts, we could
483 * assert that the refcount on table is 1 now, i.e. that this function
484 * has unique access to it, so decref side-effects can't alter it.
485 */
486 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 assert(i < n);
489 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000490#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (entry->key) {
492 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700493 if (entry->key != dummy)
494 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 else
498 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (table_is_malloced)
503 PyMem_DEL(table);
504 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505}
506
507/*
508 * Iterate over a set table. Use like so:
509 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000511 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000512 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000513 * while (set_next(yourset, &pos, &entry)) {
514 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515 * }
516 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000517 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519 */
520static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000521set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 Py_ssize_t i;
524 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200525 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 assert (PyAnySet_Check(so));
528 i = *pos_ptr;
529 assert(i >= 0);
530 table = so->table;
531 mask = so->mask;
532 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
533 i++;
534 *pos_ptr = i+1;
535 if (i > mask)
536 return 0;
537 assert(table[i].key != NULL);
538 *entry_ptr = &table[i];
539 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540}
541
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000542static void
543set_dealloc(PySetObject *so)
544{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200545 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 Py_ssize_t fill = so->fill;
547 PyObject_GC_UnTrack(so);
548 Py_TRASHCAN_SAFE_BEGIN(so)
549 if (so->weakreflist != NULL)
550 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 for (entry = so->table; fill > 0; entry++) {
553 if (entry->key) {
554 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700555 if (entry->key != dummy)
556 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 }
558 }
559 if (so->table != so->smalltable)
560 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700561 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000563}
564
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000565static PyObject *
566set_repr(PySetObject *so)
567{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200568 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (status != 0) {
572 if (status < 0)
573 return NULL;
574 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
575 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 /* shortcut for the empty set */
578 if (!so->used) {
579 Py_ReprLeave((PyObject*)so);
580 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
581 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 keys = PySequence_List((PyObject *)so);
584 if (keys == NULL)
585 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000586
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200587 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 listrepr = PyObject_Repr(keys);
589 Py_DECREF(keys);
590 if (listrepr == NULL)
591 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200594 if (tmp == NULL)
595 goto done;
596 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200597
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200598 if (Py_TYPE(so) != &PySet_Type)
599 result = PyUnicode_FromFormat("%s({%U})",
600 Py_TYPE(so)->tp_name,
601 listrepr);
602 else
603 result = PyUnicode_FromFormat("{%U}", listrepr);
604 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000605done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 Py_ReprLeave((PyObject*)so);
607 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000608}
609
Martin v. Löwis18e16552006-02-15 17:27:45 +0000610static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000611set_len(PyObject *so)
612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000614}
615
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000616static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000617set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000620 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100621 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200622 Py_ssize_t i;
623 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 assert (PyAnySet_Check(so));
626 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 other = (PySetObject*)otherset;
629 if (other == so || other->used == 0)
630 /* a.update(a) or a.update({}); nothing to do */
631 return 0;
632 /* Do one big resize at the start, rather than
633 * incrementally resizing as we insert new keys. Expect
634 * that there will be no (or few) overlapping keys.
635 */
636 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
637 if (set_table_resize(so, (so->used + other->used)*2) != 0)
638 return -1;
639 }
640 for (i = 0; i <= other->mask; i++) {
641 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000642 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100643 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000644 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500645 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000646 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100647 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000648 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 return -1;
650 }
651 }
652 }
653 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000654}
655
656static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000657set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000658{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000659 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200663 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 hash = PyObject_Hash(key);
665 if (hash == -1)
666 return -1;
667 }
668 entry = (so->lookup)(so, key, hash);
669 if (entry == NULL)
670 return -1;
671 key = entry->key;
672 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673}
674
Raymond Hettingerc991db22005-08-11 07:58:45 +0000675static int
676set_contains_entry(PySetObject *so, setentry *entry)
677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 PyObject *key;
679 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000680
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000681 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (lu_entry == NULL)
683 return -1;
684 key = lu_entry->key;
685 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000686}
687
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000688static PyObject *
689set_pop(PySetObject *so)
690{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200691 Py_ssize_t i = 0;
692 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 assert (PyAnySet_Check(so));
696 if (so->used == 0) {
697 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
698 return NULL;
699 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 /* Set entry to "the first" unused or dummy set entry. We abuse
702 * the hash field of slot 0 to hold a search finger:
703 * If slot 0 has a value, use slot 0.
704 * Else slot 0 is being used to hold a search finger,
705 * and we use its hash value as the first index to look.
706 */
707 entry = &so->table[0];
708 if (entry->key == NULL || entry->key == dummy) {
709 i = entry->hash;
710 /* The hash field may be a real hash value, or it may be a
711 * legit search finger, or it may be a once-legit search
712 * finger that's out of bounds now because it wrapped around
713 * or the table shrunk -- simply make sure it's in bounds now.
714 */
715 if (i > so->mask || i < 1)
716 i = 1; /* skip slot 0 */
717 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
718 i++;
719 if (i > so->mask)
720 i = 1;
721 }
722 }
723 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 entry->key = dummy;
725 so->used--;
726 so->table[0].hash = i + 1; /* next place to start */
727 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000728}
729
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000730PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
731Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000732
733static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000734set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 Py_ssize_t pos = 0;
737 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 while (set_next(so, &pos, &entry))
740 Py_VISIT(entry->key);
741 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000742}
743
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000744static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000745frozenset_hash(PyObject *self)
746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800748 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 setentry *entry;
750 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 if (so->hash != -1)
753 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000754
Mark Dickinson57e683e2011-09-24 18:18:40 +0100755 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 while (set_next(so, &pos, &entry)) {
757 /* Work to increase the bit dispersion for closely spaced hash
758 values. The is important because some use cases have many
759 combinations of a small number of elements with nearby
760 hashes so that many distinct combinations collapse to only
761 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000762 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800763 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800765 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800767 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 so->hash = hash;
769 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000770}
771
Raymond Hettingera9d99362005-08-05 00:01:15 +0000772/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000773
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000774typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 PyObject_HEAD
776 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
777 Py_ssize_t si_used;
778 Py_ssize_t si_pos;
779 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000780} setiterobject;
781
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000782static void
783setiter_dealloc(setiterobject *si)
784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 Py_XDECREF(si->si_set);
786 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000787}
788
789static int
790setiter_traverse(setiterobject *si, visitproc visit, void *arg)
791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 Py_VISIT(si->si_set);
793 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794}
795
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000796static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797setiter_len(setiterobject *si)
798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 Py_ssize_t len = 0;
800 if (si->si_set != NULL && si->si_used == si->si_set->used)
801 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000802 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000803}
804
Armin Rigof5b3e362006-02-11 21:32:43 +0000805PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000806
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000807static PyObject *setiter_iternext(setiterobject *si);
808
809static PyObject *
810setiter_reduce(setiterobject *si)
811{
812 PyObject *list;
813 setiterobject tmp;
814
815 list = PyList_New(0);
816 if (!list)
817 return NULL;
818
Ezio Melotti0e1af282012-09-28 16:43:40 +0300819 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000820 tmp = *si;
821 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300822
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000823 /* iterate the temporary into a list */
824 for(;;) {
825 PyObject *element = setiter_iternext(&tmp);
826 if (element) {
827 if (PyList_Append(list, element)) {
828 Py_DECREF(element);
829 Py_DECREF(list);
830 Py_XDECREF(tmp.si_set);
831 return NULL;
832 }
833 Py_DECREF(element);
834 } else
835 break;
836 }
837 Py_XDECREF(tmp.si_set);
838 /* check for error */
839 if (tmp.si_set != NULL) {
840 /* we have an error */
841 Py_DECREF(list);
842 return NULL;
843 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200844 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000845}
846
847PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
848
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000849static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000851 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853};
854
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000855static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200858 Py_ssize_t i, mask;
859 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 if (so == NULL)
863 return NULL;
864 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 if (si->si_used != so->used) {
867 PyErr_SetString(PyExc_RuntimeError,
868 "Set changed size during iteration");
869 si->si_used = -1; /* Make this state sticky */
870 return NULL;
871 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 i = si->si_pos;
874 assert(i>=0);
875 entry = so->table;
876 mask = so->mask;
877 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
878 i++;
879 si->si_pos = i+1;
880 if (i > mask)
881 goto fail;
882 si->len--;
883 key = entry[i].key;
884 Py_INCREF(key);
885 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000886
887fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_DECREF(so);
889 si->si_set = NULL;
890 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891}
892
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000893PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyVarObject_HEAD_INIT(&PyType_Type, 0)
895 "set_iterator", /* tp_name */
896 sizeof(setiterobject), /* tp_basicsize */
897 0, /* tp_itemsize */
898 /* methods */
899 (destructor)setiter_dealloc, /* tp_dealloc */
900 0, /* tp_print */
901 0, /* tp_getattr */
902 0, /* tp_setattr */
903 0, /* tp_reserved */
904 0, /* tp_repr */
905 0, /* tp_as_number */
906 0, /* tp_as_sequence */
907 0, /* tp_as_mapping */
908 0, /* tp_hash */
909 0, /* tp_call */
910 0, /* tp_str */
911 PyObject_GenericGetAttr, /* tp_getattro */
912 0, /* tp_setattro */
913 0, /* tp_as_buffer */
914 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
915 0, /* tp_doc */
916 (traverseproc)setiter_traverse, /* tp_traverse */
917 0, /* tp_clear */
918 0, /* tp_richcompare */
919 0, /* tp_weaklistoffset */
920 PyObject_SelfIter, /* tp_iter */
921 (iternextfunc)setiter_iternext, /* tp_iternext */
922 setiter_methods, /* tp_methods */
923 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000924};
925
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000926static PyObject *
927set_iter(PySetObject *so)
928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
930 if (si == NULL)
931 return NULL;
932 Py_INCREF(so);
933 si->si_set = so;
934 si->si_used = so->used;
935 si->si_pos = 0;
936 si->len = so->used;
937 _PyObject_GC_TRACK(si);
938 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939}
940
Raymond Hettingerd7946662005-08-01 21:39:29 +0000941static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 if (PyAnySet_Check(other))
947 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (PyDict_CheckExact(other)) {
950 PyObject *value;
951 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000952 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 /* Do one big resize at the start, rather than
956 * incrementally resizing as we insert new keys. Expect
957 * that there will be no (or few) overlapping keys.
958 */
959 if (dictsize == -1)
960 return -1;
961 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
962 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
963 return -1;
964 }
965 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
966 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 an_entry.hash = hash;
969 an_entry.key = key;
970 if (set_add_entry(so, &an_entry) == -1)
971 return -1;
972 }
973 return 0;
974 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 it = PyObject_GetIter(other);
977 if (it == NULL)
978 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 while ((key = PyIter_Next(it)) != NULL) {
981 if (set_add_key(so, key) == -1) {
982 Py_DECREF(it);
983 Py_DECREF(key);
984 return -1;
985 }
986 Py_DECREF(key);
987 }
988 Py_DECREF(it);
989 if (PyErr_Occurred())
990 return -1;
991 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000992}
993
994static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000995set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1000 PyObject *other = PyTuple_GET_ITEM(args, i);
1001 if (set_update_internal(so, other) == -1)
1002 return NULL;
1003 }
1004 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001005}
1006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001008"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001009
1010static PyObject *
1011make_new_set(PyTypeObject *type, PyObject *iterable)
1012{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001013 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001016 so = (PySetObject *)type->tp_alloc(type, 0);
1017 if (so == NULL)
1018 return NULL;
1019 /* tp_alloc has already zeroed the structure */
1020 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1021 INIT_NONZERO_SET_SLOTS(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 so->lookup = set_lookkey_unicode;
1024 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (iterable != NULL) {
1027 if (set_update_internal(so, iterable) == -1) {
1028 Py_DECREF(so);
1029 return NULL;
1030 }
1031 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001034}
1035
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001036static PyObject *
1037make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1040 if (PyType_IsSubtype(type, &PySet_Type))
1041 type = &PySet_Type;
1042 else
1043 type = &PyFrozenSet_Type;
1044 }
1045 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001046}
1047
Raymond Hettingerd7946662005-08-01 21:39:29 +00001048/* The empty frozenset is a singleton */
1049static PyObject *emptyfrozenset = NULL;
1050
Raymond Hettingera690a992003-11-16 16:17:49 +00001051static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001052frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1057 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1060 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 if (type != &PyFrozenSet_Type)
1063 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (iterable != NULL) {
1066 /* frozenset(f) is idempotent */
1067 if (PyFrozenSet_CheckExact(iterable)) {
1068 Py_INCREF(iterable);
1069 return iterable;
1070 }
1071 result = make_new_set(type, iterable);
1072 if (result == NULL || PySet_GET_SIZE(result))
1073 return result;
1074 Py_DECREF(result);
1075 }
1076 /* The empty frozenset is a singleton */
1077 if (emptyfrozenset == NULL)
1078 emptyfrozenset = make_new_set(type, NULL);
1079 Py_XINCREF(emptyfrozenset);
1080 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001081}
1082
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001083int
1084PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001085{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001086 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001087}
1088
1089void
1090PySet_Fini(void)
1091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001093}
1094
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001095static PyObject *
1096set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1099 return NULL;
1100
1101 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001102}
1103
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001104/* set_swap_bodies() switches the contents of any two sets by moving their
1105 internal data pointers and, if needed, copying the internal smalltables.
1106 Semantically equivalent to:
1107
1108 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1109
1110 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001112 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001114 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001115*/
1116
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001117static void
1118set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 Py_ssize_t t;
1121 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001122 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001124 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 t = a->fill; a->fill = b->fill; b->fill = t;
1127 t = a->used; a->used = b->used; b->used = t;
1128 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 u = a->table;
1131 if (a->table == a->smalltable)
1132 u = b->smalltable;
1133 a->table = b->table;
1134 if (b->table == b->smalltable)
1135 a->table = a->smalltable;
1136 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 if (a->table == a->smalltable || b->table == b->smalltable) {
1141 memcpy(tab, a->smalltable, sizeof(tab));
1142 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1143 memcpy(b->smalltable, tab, sizeof(tab));
1144 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1147 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1148 h = a->hash; a->hash = b->hash; b->hash = h;
1149 } else {
1150 a->hash = -1;
1151 b->hash = -1;
1152 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001153}
1154
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001155static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001156set_copy(PySetObject *so)
1157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001159}
1160
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001161static PyObject *
1162frozenset_copy(PySetObject *so)
1163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 if (PyFrozenSet_CheckExact(so)) {
1165 Py_INCREF(so);
1166 return (PyObject *)so;
1167 }
1168 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001169}
1170
Raymond Hettingera690a992003-11-16 16:17:49 +00001171PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1172
1173static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001174set_clear(PySetObject *so)
1175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 set_clear_internal(so);
1177 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001178}
1179
1180PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1181
1182static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001183set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 PySetObject *result;
1186 PyObject *other;
1187 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 result = (PySetObject *)set_copy(so);
1190 if (result == NULL)
1191 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1194 other = PyTuple_GET_ITEM(args, i);
1195 if ((PyObject *)so == other)
1196 continue;
1197 if (set_update_internal(result, other) == -1) {
1198 Py_DECREF(result);
1199 return NULL;
1200 }
1201 }
1202 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001203}
1204
1205PyDoc_STRVAR(union_doc,
1206 "Return the union of sets as a new set.\n\
1207\n\
1208(i.e. all elements that are in either set.)");
1209
1210static PyObject *
1211set_or(PySetObject *so, PyObject *other)
1212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001214
Brian Curtindfc80e32011-08-10 20:28:54 -05001215 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1216 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 result = (PySetObject *)set_copy(so);
1219 if (result == NULL)
1220 return NULL;
1221 if ((PyObject *)so == other)
1222 return (PyObject *)result;
1223 if (set_update_internal(result, other) == -1) {
1224 Py_DECREF(result);
1225 return NULL;
1226 }
1227 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001228}
1229
Raymond Hettingera690a992003-11-16 16:17:49 +00001230static PyObject *
1231set_ior(PySetObject *so, PyObject *other)
1232{
Brian Curtindfc80e32011-08-10 20:28:54 -05001233 if (!PyAnySet_Check(other))
1234 Py_RETURN_NOTIMPLEMENTED;
1235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 if (set_update_internal(so, other) == -1)
1237 return NULL;
1238 Py_INCREF(so);
1239 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001240}
1241
1242static PyObject *
1243set_intersection(PySetObject *so, PyObject *other)
1244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 PySetObject *result;
1246 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 if ((PyObject *)so == other)
1249 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1252 if (result == NULL)
1253 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (PyAnySet_Check(other)) {
1256 Py_ssize_t pos = 0;
1257 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1260 tmp = (PyObject *)so;
1261 so = (PySetObject *)other;
1262 other = tmp;
1263 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 while (set_next((PySetObject *)other, &pos, &entry)) {
1266 int rv = set_contains_entry(so, entry);
1267 if (rv == -1) {
1268 Py_DECREF(result);
1269 return NULL;
1270 }
1271 if (rv) {
1272 if (set_add_entry(result, entry) == -1) {
1273 Py_DECREF(result);
1274 return NULL;
1275 }
1276 }
1277 }
1278 return (PyObject *)result;
1279 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 it = PyObject_GetIter(other);
1282 if (it == NULL) {
1283 Py_DECREF(result);
1284 return NULL;
1285 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 while ((key = PyIter_Next(it)) != NULL) {
1288 int rv;
1289 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001290 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (hash == -1) {
1293 Py_DECREF(it);
1294 Py_DECREF(result);
1295 Py_DECREF(key);
1296 return NULL;
1297 }
1298 entry.hash = hash;
1299 entry.key = key;
1300 rv = set_contains_entry(so, &entry);
1301 if (rv == -1) {
1302 Py_DECREF(it);
1303 Py_DECREF(result);
1304 Py_DECREF(key);
1305 return NULL;
1306 }
1307 if (rv) {
1308 if (set_add_entry(result, &entry) == -1) {
1309 Py_DECREF(it);
1310 Py_DECREF(result);
1311 Py_DECREF(key);
1312 return NULL;
1313 }
1314 }
1315 Py_DECREF(key);
1316 }
1317 Py_DECREF(it);
1318 if (PyErr_Occurred()) {
1319 Py_DECREF(result);
1320 return NULL;
1321 }
1322 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001323}
1324
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001325static PyObject *
1326set_intersection_multi(PySetObject *so, PyObject *args)
1327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 Py_ssize_t i;
1329 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 if (PyTuple_GET_SIZE(args) == 0)
1332 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 Py_INCREF(so);
1335 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1336 PyObject *other = PyTuple_GET_ITEM(args, i);
1337 PyObject *newresult = set_intersection((PySetObject *)result, other);
1338 if (newresult == NULL) {
1339 Py_DECREF(result);
1340 return NULL;
1341 }
1342 Py_DECREF(result);
1343 result = newresult;
1344 }
1345 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001346}
1347
Raymond Hettingera690a992003-11-16 16:17:49 +00001348PyDoc_STRVAR(intersection_doc,
1349"Return the intersection of two sets as a new set.\n\
1350\n\
1351(i.e. all elements that are in both sets.)");
1352
1353static PyObject *
1354set_intersection_update(PySetObject *so, PyObject *other)
1355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 tmp = set_intersection(so, other);
1359 if (tmp == NULL)
1360 return NULL;
1361 set_swap_bodies(so, (PySetObject *)tmp);
1362 Py_DECREF(tmp);
1363 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001364}
1365
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001366static PyObject *
1367set_intersection_update_multi(PySetObject *so, PyObject *args)
1368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 tmp = set_intersection_multi(so, args);
1372 if (tmp == NULL)
1373 return NULL;
1374 set_swap_bodies(so, (PySetObject *)tmp);
1375 Py_DECREF(tmp);
1376 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001377}
1378
Raymond Hettingera690a992003-11-16 16:17:49 +00001379PyDoc_STRVAR(intersection_update_doc,
1380"Update a set with the intersection of itself and another.");
1381
1382static PyObject *
1383set_and(PySetObject *so, PyObject *other)
1384{
Brian Curtindfc80e32011-08-10 20:28:54 -05001385 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1386 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001388}
1389
1390static PyObject *
1391set_iand(PySetObject *so, PyObject *other)
1392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001394
Brian Curtindfc80e32011-08-10 20:28:54 -05001395 if (!PyAnySet_Check(other))
1396 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 result = set_intersection_update(so, other);
1398 if (result == NULL)
1399 return NULL;
1400 Py_DECREF(result);
1401 Py_INCREF(so);
1402 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001403}
1404
Guido van Rossum58da9312007-11-10 23:39:45 +00001405static PyObject *
1406set_isdisjoint(PySetObject *so, PyObject *other)
1407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if ((PyObject *)so == other) {
1411 if (PySet_GET_SIZE(so) == 0)
1412 Py_RETURN_TRUE;
1413 else
1414 Py_RETURN_FALSE;
1415 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 if (PyAnySet_CheckExact(other)) {
1418 Py_ssize_t pos = 0;
1419 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1422 tmp = (PyObject *)so;
1423 so = (PySetObject *)other;
1424 other = tmp;
1425 }
1426 while (set_next((PySetObject *)other, &pos, &entry)) {
1427 int rv = set_contains_entry(so, entry);
1428 if (rv == -1)
1429 return NULL;
1430 if (rv)
1431 Py_RETURN_FALSE;
1432 }
1433 Py_RETURN_TRUE;
1434 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 it = PyObject_GetIter(other);
1437 if (it == NULL)
1438 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 while ((key = PyIter_Next(it)) != NULL) {
1441 int rv;
1442 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001443 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 if (hash == -1) {
1446 Py_DECREF(key);
1447 Py_DECREF(it);
1448 return NULL;
1449 }
1450 entry.hash = hash;
1451 entry.key = key;
1452 rv = set_contains_entry(so, &entry);
1453 Py_DECREF(key);
1454 if (rv == -1) {
1455 Py_DECREF(it);
1456 return NULL;
1457 }
1458 if (rv) {
1459 Py_DECREF(it);
1460 Py_RETURN_FALSE;
1461 }
1462 }
1463 Py_DECREF(it);
1464 if (PyErr_Occurred())
1465 return NULL;
1466 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001467}
1468
1469PyDoc_STRVAR(isdisjoint_doc,
1470"Return True if two sets have a null intersection.");
1471
Neal Norwitz6576bd82005-11-13 18:41:28 +00001472static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001473set_difference_update_internal(PySetObject *so, PyObject *other)
1474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 if ((PyObject *)so == other)
1476 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if (PyAnySet_Check(other)) {
1479 setentry *entry;
1480 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 while (set_next((PySetObject *)other, &pos, &entry))
1483 if (set_discard_entry(so, entry) == -1)
1484 return -1;
1485 } else {
1486 PyObject *key, *it;
1487 it = PyObject_GetIter(other);
1488 if (it == NULL)
1489 return -1;
1490
1491 while ((key = PyIter_Next(it)) != NULL) {
1492 if (set_discard_key(so, key) == -1) {
1493 Py_DECREF(it);
1494 Py_DECREF(key);
1495 return -1;
1496 }
1497 Py_DECREF(key);
1498 }
1499 Py_DECREF(it);
1500 if (PyErr_Occurred())
1501 return -1;
1502 }
1503 /* If more than 1/5 are dummies, then resize them away. */
1504 if ((so->fill - so->used) * 5 < so->mask)
1505 return 0;
1506 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001507}
1508
Raymond Hettingera690a992003-11-16 16:17:49 +00001509static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001510set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1515 PyObject *other = PyTuple_GET_ITEM(args, i);
1516 if (set_difference_update_internal(so, other) == -1)
1517 return NULL;
1518 }
1519 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001520}
1521
1522PyDoc_STRVAR(difference_update_doc,
1523"Remove all elements of another set from this set.");
1524
1525static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001526set_copy_and_difference(PySetObject *so, PyObject *other)
1527{
1528 PyObject *result;
1529
1530 result = set_copy(so);
1531 if (result == NULL)
1532 return NULL;
1533 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1534 return result;
1535 Py_DECREF(result);
1536 return NULL;
1537}
1538
1539static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001540set_difference(PySetObject *so, PyObject *other)
1541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 PyObject *result;
1543 setentry *entry;
1544 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001547 return set_copy_and_difference(so, other);
1548 }
1549
1550 /* If len(so) much more than len(other), it's more efficient to simply copy
1551 * so and then iterate other looking for common elements. */
1552 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1553 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 result = make_new_set_basetype(Py_TYPE(so), NULL);
1557 if (result == NULL)
1558 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 if (PyDict_CheckExact(other)) {
1561 while (set_next(so, &pos, &entry)) {
1562 setentry entrycopy;
1563 entrycopy.hash = entry->hash;
1564 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001565 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1567 Py_DECREF(result);
1568 return NULL;
1569 }
1570 }
1571 }
1572 return result;
1573 }
1574
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001575 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 while (set_next(so, &pos, &entry)) {
1577 int rv = set_contains_entry((PySetObject *)other, entry);
1578 if (rv == -1) {
1579 Py_DECREF(result);
1580 return NULL;
1581 }
1582 if (!rv) {
1583 if (set_add_entry((PySetObject *)result, entry) == -1) {
1584 Py_DECREF(result);
1585 return NULL;
1586 }
1587 }
1588 }
1589 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001590}
1591
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001592static PyObject *
1593set_difference_multi(PySetObject *so, PyObject *args)
1594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 Py_ssize_t i;
1596 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 if (PyTuple_GET_SIZE(args) == 0)
1599 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 other = PyTuple_GET_ITEM(args, 0);
1602 result = set_difference(so, other);
1603 if (result == NULL)
1604 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1607 other = PyTuple_GET_ITEM(args, i);
1608 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1609 Py_DECREF(result);
1610 return NULL;
1611 }
1612 }
1613 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001614}
1615
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001616PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001617"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001618\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001619(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001620static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001621set_sub(PySetObject *so, PyObject *other)
1622{
Brian Curtindfc80e32011-08-10 20:28:54 -05001623 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1624 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001626}
1627
1628static PyObject *
1629set_isub(PySetObject *so, PyObject *other)
1630{
Brian Curtindfc80e32011-08-10 20:28:54 -05001631 if (!PyAnySet_Check(other))
1632 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (set_difference_update_internal(so, other) == -1)
1634 return NULL;
1635 Py_INCREF(so);
1636 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001637}
1638
1639static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001640set_symmetric_difference_update(PySetObject *so, PyObject *other)
1641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 PySetObject *otherset;
1643 PyObject *key;
1644 Py_ssize_t pos = 0;
1645 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 if ((PyObject *)so == other)
1648 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 if (PyDict_CheckExact(other)) {
1651 PyObject *value;
1652 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001653 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1655 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001656
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001657 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 an_entry.hash = hash;
1659 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001662 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001663 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001666 if (rv == DISCARD_NOTFOUND) {
1667 if (set_add_entry(so, &an_entry) == -1) {
1668 Py_DECREF(key);
1669 return NULL;
1670 }
1671 }
Georg Brandl2d444492010-09-03 10:52:55 +00001672 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 }
1674 Py_RETURN_NONE;
1675 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 if (PyAnySet_Check(other)) {
1678 Py_INCREF(other);
1679 otherset = (PySetObject *)other;
1680 } else {
1681 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1682 if (otherset == NULL)
1683 return NULL;
1684 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 while (set_next(otherset, &pos, &entry)) {
1687 int rv = set_discard_entry(so, entry);
1688 if (rv == -1) {
1689 Py_DECREF(otherset);
1690 return NULL;
1691 }
1692 if (rv == DISCARD_NOTFOUND) {
1693 if (set_add_entry(so, entry) == -1) {
1694 Py_DECREF(otherset);
1695 return NULL;
1696 }
1697 }
1698 }
1699 Py_DECREF(otherset);
1700 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001701}
1702
1703PyDoc_STRVAR(symmetric_difference_update_doc,
1704"Update a set with the symmetric difference of itself and another.");
1705
1706static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001707set_symmetric_difference(PySetObject *so, PyObject *other)
1708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 PyObject *rv;
1710 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1713 if (otherset == NULL)
1714 return NULL;
1715 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1716 if (rv == NULL)
1717 return NULL;
1718 Py_DECREF(rv);
1719 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001720}
1721
1722PyDoc_STRVAR(symmetric_difference_doc,
1723"Return the symmetric difference of two sets as a new set.\n\
1724\n\
1725(i.e. all elements that are in exactly one of the sets.)");
1726
1727static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001728set_xor(PySetObject *so, PyObject *other)
1729{
Brian Curtindfc80e32011-08-10 20:28:54 -05001730 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1731 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001733}
1734
1735static PyObject *
1736set_ixor(PySetObject *so, PyObject *other)
1737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001739
Brian Curtindfc80e32011-08-10 20:28:54 -05001740 if (!PyAnySet_Check(other))
1741 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 result = set_symmetric_difference_update(so, other);
1743 if (result == NULL)
1744 return NULL;
1745 Py_DECREF(result);
1746 Py_INCREF(so);
1747 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001748}
1749
1750static PyObject *
1751set_issubset(PySetObject *so, PyObject *other)
1752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 setentry *entry;
1754 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 if (!PyAnySet_Check(other)) {
1757 PyObject *tmp, *result;
1758 tmp = make_new_set(&PySet_Type, other);
1759 if (tmp == NULL)
1760 return NULL;
1761 result = set_issubset(so, tmp);
1762 Py_DECREF(tmp);
1763 return result;
1764 }
1765 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1766 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 while (set_next(so, &pos, &entry)) {
1769 int rv = set_contains_entry((PySetObject *)other, entry);
1770 if (rv == -1)
1771 return NULL;
1772 if (!rv)
1773 Py_RETURN_FALSE;
1774 }
1775 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001776}
1777
1778PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1779
1780static PyObject *
1781set_issuperset(PySetObject *so, PyObject *other)
1782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 if (!PyAnySet_Check(other)) {
1786 tmp = make_new_set(&PySet_Type, other);
1787 if (tmp == NULL)
1788 return NULL;
1789 result = set_issuperset(so, tmp);
1790 Py_DECREF(tmp);
1791 return result;
1792 }
1793 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001794}
1795
1796PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1797
Raymond Hettingera690a992003-11-16 16:17:49 +00001798static PyObject *
1799set_richcompare(PySetObject *v, PyObject *w, int op)
1800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001802
Brian Curtindfc80e32011-08-10 20:28:54 -05001803 if(!PyAnySet_Check(w))
1804 Py_RETURN_NOTIMPLEMENTED;
1805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 switch (op) {
1807 case Py_EQ:
1808 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1809 Py_RETURN_FALSE;
1810 if (v->hash != -1 &&
1811 ((PySetObject *)w)->hash != -1 &&
1812 v->hash != ((PySetObject *)w)->hash)
1813 Py_RETURN_FALSE;
1814 return set_issubset(v, w);
1815 case Py_NE:
1816 r1 = set_richcompare(v, w, Py_EQ);
1817 if (r1 == NULL)
1818 return NULL;
1819 r2 = PyBool_FromLong(PyObject_Not(r1));
1820 Py_DECREF(r1);
1821 return r2;
1822 case Py_LE:
1823 return set_issubset(v, w);
1824 case Py_GE:
1825 return set_issuperset(v, w);
1826 case Py_LT:
1827 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1828 Py_RETURN_FALSE;
1829 return set_issubset(v, w);
1830 case Py_GT:
1831 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1832 Py_RETURN_FALSE;
1833 return set_issuperset(v, w);
1834 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001835 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001836}
1837
Raymond Hettingera690a992003-11-16 16:17:49 +00001838static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001839set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 if (set_add_key(so, key) == -1)
1842 return NULL;
1843 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001844}
1845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001847"Add an element to a set.\n\
1848\n\
1849This has no effect if the element is already present.");
1850
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001851static int
1852set_contains(PySetObject *so, PyObject *key)
1853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 PyObject *tmpkey;
1855 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 rv = set_contains_key(so, key);
1858 if (rv == -1) {
1859 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1860 return -1;
1861 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001862 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (tmpkey == NULL)
1864 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001865 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 Py_DECREF(tmpkey);
1867 }
1868 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001869}
1870
1871static PyObject *
1872set_direct_contains(PySetObject *so, PyObject *key)
1873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 result = set_contains(so, key);
1877 if (result == -1)
1878 return NULL;
1879 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001880}
1881
1882PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1883
Raymond Hettingera690a992003-11-16 16:17:49 +00001884static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001885set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 PyObject *tmpkey;
1888 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 rv = set_discard_key(so, key);
1891 if (rv == -1) {
1892 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1893 return NULL;
1894 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001895 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 if (tmpkey == NULL)
1897 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 Py_DECREF(tmpkey);
1900 if (rv == -1)
1901 return NULL;
1902 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001905 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 return NULL;
1907 }
1908 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001909}
1910
1911PyDoc_STRVAR(remove_doc,
1912"Remove an element from a set; it must be a member.\n\
1913\n\
1914If the element is not a member, raise a KeyError.");
1915
1916static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001917set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001918{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001919 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 rv = set_discard_key(so, key);
1923 if (rv == -1) {
1924 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1925 return NULL;
1926 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001927 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 if (tmpkey == NULL)
1929 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001930 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001932 if (rv == -1)
1933 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 }
1935 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001936}
1937
1938PyDoc_STRVAR(discard_doc,
1939"Remove an element from a set if it is a member.\n\
1940\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001942
1943static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001944set_reduce(PySetObject *so)
1945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001947 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 keys = PySequence_List((PyObject *)so);
1950 if (keys == NULL)
1951 goto done;
1952 args = PyTuple_Pack(1, keys);
1953 if (args == NULL)
1954 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001955 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 if (dict == NULL) {
1957 PyErr_Clear();
1958 dict = Py_None;
1959 Py_INCREF(dict);
1960 }
1961 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001962done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 Py_XDECREF(args);
1964 Py_XDECREF(keys);
1965 Py_XDECREF(dict);
1966 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001967}
1968
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001969static PyObject *
1970set_sizeof(PySetObject *so)
1971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 res = sizeof(PySetObject);
1975 if (so->table != so->smalltable)
1976 res = res + (so->mask + 1) * sizeof(setentry);
1977 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001978}
1979
1980PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001981static int
1982set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 if (!PyAnySet_Check(self))
1987 return -1;
1988 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1989 return -1;
1990 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1991 return -1;
1992 set_clear_internal(self);
1993 self->hash = -1;
1994 if (iterable == NULL)
1995 return 0;
1996 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001997}
1998
Raymond Hettingera690a992003-11-16 16:17:49 +00001999static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 set_len, /* sq_length */
2001 0, /* sq_concat */
2002 0, /* sq_repeat */
2003 0, /* sq_item */
2004 0, /* sq_slice */
2005 0, /* sq_ass_item */
2006 0, /* sq_ass_slice */
2007 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002008};
2009
2010/* set object ********************************************************/
2011
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002012#ifdef Py_DEBUG
2013static PyObject *test_c_api(PySetObject *so);
2014
2015PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2016All is well if assertions don't fail.");
2017#endif
2018
Raymond Hettingera690a992003-11-16 16:17:49 +00002019static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 {"add", (PyCFunction)set_add, METH_O,
2021 add_doc},
2022 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2023 clear_doc},
2024 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2025 contains_doc},
2026 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2027 copy_doc},
2028 {"discard", (PyCFunction)set_discard, METH_O,
2029 discard_doc},
2030 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2031 difference_doc},
2032 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2033 difference_update_doc},
2034 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2035 intersection_doc},
2036 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2037 intersection_update_doc},
2038 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2039 isdisjoint_doc},
2040 {"issubset", (PyCFunction)set_issubset, METH_O,
2041 issubset_doc},
2042 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2043 issuperset_doc},
2044 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2045 pop_doc},
2046 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2047 reduce_doc},
2048 {"remove", (PyCFunction)set_remove, METH_O,
2049 remove_doc},
2050 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2051 sizeof_doc},
2052 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2053 symmetric_difference_doc},
2054 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2055 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002056#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2058 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002059#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 {"union", (PyCFunction)set_union, METH_VARARGS,
2061 union_doc},
2062 {"update", (PyCFunction)set_update, METH_VARARGS,
2063 update_doc},
2064 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002065};
2066
2067static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 0, /*nb_add*/
2069 (binaryfunc)set_sub, /*nb_subtract*/
2070 0, /*nb_multiply*/
2071 0, /*nb_remainder*/
2072 0, /*nb_divmod*/
2073 0, /*nb_power*/
2074 0, /*nb_negative*/
2075 0, /*nb_positive*/
2076 0, /*nb_absolute*/
2077 0, /*nb_bool*/
2078 0, /*nb_invert*/
2079 0, /*nb_lshift*/
2080 0, /*nb_rshift*/
2081 (binaryfunc)set_and, /*nb_and*/
2082 (binaryfunc)set_xor, /*nb_xor*/
2083 (binaryfunc)set_or, /*nb_or*/
2084 0, /*nb_int*/
2085 0, /*nb_reserved*/
2086 0, /*nb_float*/
2087 0, /*nb_inplace_add*/
2088 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2089 0, /*nb_inplace_multiply*/
2090 0, /*nb_inplace_remainder*/
2091 0, /*nb_inplace_power*/
2092 0, /*nb_inplace_lshift*/
2093 0, /*nb_inplace_rshift*/
2094 (binaryfunc)set_iand, /*nb_inplace_and*/
2095 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2096 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002097};
2098
2099PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002100"set() -> new empty set object\n\
2101set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002102\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002103Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002104
2105PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2107 "set", /* tp_name */
2108 sizeof(PySetObject), /* tp_basicsize */
2109 0, /* tp_itemsize */
2110 /* methods */
2111 (destructor)set_dealloc, /* tp_dealloc */
2112 0, /* tp_print */
2113 0, /* tp_getattr */
2114 0, /* tp_setattr */
2115 0, /* tp_reserved */
2116 (reprfunc)set_repr, /* tp_repr */
2117 &set_as_number, /* tp_as_number */
2118 &set_as_sequence, /* tp_as_sequence */
2119 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002120 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 0, /* tp_call */
2122 0, /* tp_str */
2123 PyObject_GenericGetAttr, /* tp_getattro */
2124 0, /* tp_setattro */
2125 0, /* tp_as_buffer */
2126 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2127 Py_TPFLAGS_BASETYPE, /* tp_flags */
2128 set_doc, /* tp_doc */
2129 (traverseproc)set_traverse, /* tp_traverse */
2130 (inquiry)set_clear_internal, /* tp_clear */
2131 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002132 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2133 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 0, /* tp_iternext */
2135 set_methods, /* tp_methods */
2136 0, /* tp_members */
2137 0, /* tp_getset */
2138 0, /* tp_base */
2139 0, /* tp_dict */
2140 0, /* tp_descr_get */
2141 0, /* tp_descr_set */
2142 0, /* tp_dictoffset */
2143 (initproc)set_init, /* tp_init */
2144 PyType_GenericAlloc, /* tp_alloc */
2145 set_new, /* tp_new */
2146 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002147};
2148
2149/* frozenset object ********************************************************/
2150
2151
2152static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2154 contains_doc},
2155 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2156 copy_doc},
2157 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2158 difference_doc},
2159 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2160 intersection_doc},
2161 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2162 isdisjoint_doc},
2163 {"issubset", (PyCFunction)set_issubset, METH_O,
2164 issubset_doc},
2165 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2166 issuperset_doc},
2167 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2168 reduce_doc},
2169 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2170 sizeof_doc},
2171 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2172 symmetric_difference_doc},
2173 {"union", (PyCFunction)set_union, METH_VARARGS,
2174 union_doc},
2175 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002176};
2177
2178static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 0, /*nb_add*/
2180 (binaryfunc)set_sub, /*nb_subtract*/
2181 0, /*nb_multiply*/
2182 0, /*nb_remainder*/
2183 0, /*nb_divmod*/
2184 0, /*nb_power*/
2185 0, /*nb_negative*/
2186 0, /*nb_positive*/
2187 0, /*nb_absolute*/
2188 0, /*nb_bool*/
2189 0, /*nb_invert*/
2190 0, /*nb_lshift*/
2191 0, /*nb_rshift*/
2192 (binaryfunc)set_and, /*nb_and*/
2193 (binaryfunc)set_xor, /*nb_xor*/
2194 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002195};
2196
2197PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002198"frozenset() -> empty frozenset object\n\
2199frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002200\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002201Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002202
2203PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2205 "frozenset", /* tp_name */
2206 sizeof(PySetObject), /* tp_basicsize */
2207 0, /* tp_itemsize */
2208 /* methods */
2209 (destructor)set_dealloc, /* tp_dealloc */
2210 0, /* tp_print */
2211 0, /* tp_getattr */
2212 0, /* tp_setattr */
2213 0, /* tp_reserved */
2214 (reprfunc)set_repr, /* tp_repr */
2215 &frozenset_as_number, /* tp_as_number */
2216 &set_as_sequence, /* tp_as_sequence */
2217 0, /* tp_as_mapping */
2218 frozenset_hash, /* tp_hash */
2219 0, /* tp_call */
2220 0, /* tp_str */
2221 PyObject_GenericGetAttr, /* tp_getattro */
2222 0, /* tp_setattro */
2223 0, /* tp_as_buffer */
2224 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2225 Py_TPFLAGS_BASETYPE, /* tp_flags */
2226 frozenset_doc, /* tp_doc */
2227 (traverseproc)set_traverse, /* tp_traverse */
2228 (inquiry)set_clear_internal, /* tp_clear */
2229 (richcmpfunc)set_richcompare, /* tp_richcompare */
2230 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2231 (getiterfunc)set_iter, /* tp_iter */
2232 0, /* tp_iternext */
2233 frozenset_methods, /* tp_methods */
2234 0, /* tp_members */
2235 0, /* tp_getset */
2236 0, /* tp_base */
2237 0, /* tp_dict */
2238 0, /* tp_descr_get */
2239 0, /* tp_descr_set */
2240 0, /* tp_dictoffset */
2241 0, /* tp_init */
2242 PyType_GenericAlloc, /* tp_alloc */
2243 frozenset_new, /* tp_new */
2244 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002245};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002246
2247
2248/***** C API functions *************************************************/
2249
2250PyObject *
2251PySet_New(PyObject *iterable)
2252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002254}
2255
2256PyObject *
2257PyFrozenSet_New(PyObject *iterable)
2258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002260}
2261
Neal Norwitz8c49c822006-03-04 18:41:19 +00002262Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002263PySet_Size(PyObject *anyset)
2264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 if (!PyAnySet_Check(anyset)) {
2266 PyErr_BadInternalCall();
2267 return -1;
2268 }
2269 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002270}
2271
2272int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002273PySet_Clear(PyObject *set)
2274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 if (!PySet_Check(set)) {
2276 PyErr_BadInternalCall();
2277 return -1;
2278 }
2279 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002280}
2281
2282int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002283PySet_Contains(PyObject *anyset, PyObject *key)
2284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 if (!PyAnySet_Check(anyset)) {
2286 PyErr_BadInternalCall();
2287 return -1;
2288 }
2289 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002290}
2291
2292int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002293PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 if (!PySet_Check(set)) {
2296 PyErr_BadInternalCall();
2297 return -1;
2298 }
2299 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002300}
2301
2302int
Christian Heimesfd66e512008-01-29 12:18:50 +00002303PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 if (!PySet_Check(anyset) &&
2306 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2307 PyErr_BadInternalCall();
2308 return -1;
2309 }
2310 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002311}
2312
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002313int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002314_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 if (!PyAnySet_Check(set)) {
2319 PyErr_BadInternalCall();
2320 return -1;
2321 }
2322 if (set_next((PySetObject *)set, pos, &entry) == 0)
2323 return 0;
2324 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002325 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002327}
2328
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002329PyObject *
2330PySet_Pop(PyObject *set)
2331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 if (!PySet_Check(set)) {
2333 PyErr_BadInternalCall();
2334 return NULL;
2335 }
2336 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002337}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002338
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002339int
2340_PySet_Update(PyObject *set, PyObject *iterable)
2341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 if (!PySet_Check(set)) {
2343 PyErr_BadInternalCall();
2344 return -1;
2345 }
2346 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002347}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002348
2349#ifdef Py_DEBUG
2350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002352 Returns True and original set is restored. */
2353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354#define assertRaises(call_return_value, exception) \
2355 do { \
2356 assert(call_return_value); \
2357 assert(PyErr_ExceptionMatches(exception)); \
2358 PyErr_Clear(); \
2359 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002360
2361static PyObject *
2362test_c_api(PySetObject *so)
2363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 Py_ssize_t count;
2365 char *s;
2366 Py_ssize_t i;
2367 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2368 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002369 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 /* Verify preconditions */
2373 assert(PyAnySet_Check(ob));
2374 assert(PyAnySet_CheckExact(ob));
2375 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 /* so.clear(); so |= set("abc"); */
2378 str = PyUnicode_FromString("abc");
2379 if (str == NULL)
2380 return NULL;
2381 set_clear_internal(so);
2382 if (set_update_internal(so, str) == -1) {
2383 Py_DECREF(str);
2384 return NULL;
2385 }
2386 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 /* Exercise type/size checks */
2389 assert(PySet_Size(ob) == 3);
2390 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* Raise TypeError for non-iterable constructor arguments */
2393 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2394 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 /* Raise TypeError for unhashable key */
2397 dup = PySet_New(ob);
2398 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2399 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2400 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 /* Exercise successful pop, contains, add, and discard */
2403 elem = PySet_Pop(ob);
2404 assert(PySet_Contains(ob, elem) == 0);
2405 assert(PySet_GET_SIZE(ob) == 2);
2406 assert(PySet_Add(ob, elem) == 0);
2407 assert(PySet_Contains(ob, elem) == 1);
2408 assert(PySet_GET_SIZE(ob) == 3);
2409 assert(PySet_Discard(ob, elem) == 1);
2410 assert(PySet_GET_SIZE(ob) == 2);
2411 assert(PySet_Discard(ob, elem) == 0);
2412 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 /* Exercise clear */
2415 dup2 = PySet_New(dup);
2416 assert(PySet_Clear(dup2) == 0);
2417 assert(PySet_Size(dup2) == 0);
2418 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 /* Raise SystemError on clear or update of frozen set */
2421 f = PyFrozenSet_New(dup);
2422 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2423 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2424 assert(PySet_Add(f, elem) == 0);
2425 Py_INCREF(f);
2426 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2427 Py_DECREF(f);
2428 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 /* Exercise direct iteration */
2431 i = 0, count = 0;
2432 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2433 s = _PyUnicode_AsString(x);
2434 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2435 count++;
2436 }
2437 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* Exercise updates */
2440 dup2 = PySet_New(NULL);
2441 assert(_PySet_Update(dup2, dup) == 0);
2442 assert(PySet_Size(dup2) == 3);
2443 assert(_PySet_Update(dup2, dup) == 0);
2444 assert(PySet_Size(dup2) == 3);
2445 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 /* Raise SystemError when self argument is not a set or frozenset. */
2448 t = PyTuple_New(0);
2449 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2450 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2451 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 /* Raise SystemError when self argument is not a set. */
2454 f = PyFrozenSet_New(dup);
2455 assert(PySet_Size(f) == 3);
2456 assert(PyFrozenSet_CheckExact(f));
2457 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2458 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2459 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 /* Raise KeyError when popping from an empty set */
2462 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2463 Py_DECREF(ob);
2464 assert(PySet_GET_SIZE(ob) == 0);
2465 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 /* Restore the set from the copy using the PyNumber API */
2468 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2469 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 /* Verify constructors accept NULL arguments */
2472 f = PySet_New(NULL);
2473 assert(f != NULL);
2474 assert(PySet_GET_SIZE(f) == 0);
2475 Py_DECREF(f);
2476 f = PyFrozenSet_New(NULL);
2477 assert(f != NULL);
2478 assert(PyFrozenSet_CheckExact(f));
2479 assert(PySet_GET_SIZE(f) == 0);
2480 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 Py_DECREF(elem);
2483 Py_DECREF(dup);
2484 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002485}
2486
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002487#undef assertRaises
2488
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002489#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002490
2491/***** Dummy Struct *************************************************/
2492
2493static PyObject *
2494dummy_repr(PyObject *op)
2495{
2496 return PyUnicode_FromString("<dummy key>");
2497}
2498
2499static void
2500dummy_dealloc(PyObject* ignore)
2501{
2502 Py_FatalError("deallocating <dummy key>");
2503}
2504
2505static PyTypeObject _PySetDummy_Type = {
2506 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2507 "<dummy key> type",
2508 0,
2509 0,
2510 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2511 0, /*tp_print*/
2512 0, /*tp_getattr*/
2513 0, /*tp_setattr*/
2514 0, /*tp_reserved*/
2515 dummy_repr, /*tp_repr*/
2516 0, /*tp_as_number*/
2517 0, /*tp_as_sequence*/
2518 0, /*tp_as_mapping*/
2519 0, /*tp_hash */
2520 0, /*tp_call */
2521 0, /*tp_str */
2522 0, /*tp_getattro */
2523 0, /*tp_setattro */
2524 0, /*tp_as_buffer */
2525 Py_TPFLAGS_DEFAULT, /*tp_flags */
2526};
2527
2528static PyObject _dummy_struct = {
2529 _PyObject_EXTRA_INIT
2530 2, &_PySetDummy_Type
2531};
2532