blob: 8a855a3bd949509320fe00d0da4ad03f7b2ab7bf [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingera9d99362005-08-05 00:01:15 +00003 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Raymond Hettinger2212e522008-11-30 23:43:36 +00006 Copyright (c) 2003-2008 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000012#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
Thomas Wouters89f507f2006-12-13 04:49:30 +000026}
27
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028/* This must be >= 1. */
29#define PERTURB_SHIFT 5
30
31/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050032
33static PyObject _dummy_struct;
34
35#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000036
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef Py_REF_DEBUG
38PyObject *
39_PySet_Dummy(void)
40{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042}
43#endif
44
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045#define INIT_NONZERO_SET_SLOTS(so) do { \
46 (so)->table = (so)->smalltable; \
47 (so)->mask = PySet_MINSIZE - 1; \
48 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000049 } while(0)
50
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051#define EMPTY_TO_MINSIZE(so) do { \
52 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
53 (so)->used = (so)->fill = 0; \
54 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000055 } while(0)
56
Raymond Hettingerbc841a12005-08-07 13:02:53 +000057/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000058#ifndef PySet_MAXFREELIST
59#define PySet_MAXFREELIST 80
60#endif
61static PySetObject *free_list[PySet_MAXFREELIST];
62static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000063
Christian Heimes0ded5b52007-12-10 15:50:56 +000064
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000065/*
66The basic lookup function used by all operations.
67This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
68Open addressing is preferred over chaining since the link overhead for
69chaining would be substantial (100% with typical malloc overhead).
70
71The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000072probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070074To improve cache locality, each probe is done in pairs.
75After the probe is examined, an adjacent entry is then examined as well.
76The likelihood is that an adjacent entry is in the same cache line and
77can be examined more cheaply than another probe elsewhere in memory.
78
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000079All arithmetic on hash should ignore overflow.
80
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000081Unlike the dictionary implementation, the lookkey functions can return
82NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000083*/
84
85static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020086set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000087{
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070088 size_t i, j; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020089 size_t perturb;
90 setentry *freeslot;
91 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020093 setentry *entry;
94 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000096
Mark Dickinson57e683e2011-09-24 18:18:40 +010097 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 entry = &table[i];
99 if (entry->key == NULL || entry->key == key)
100 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -0700101 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700102 startkey = entry->key;
103 Py_INCREF(startkey);
104 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
105 Py_DECREF(startkey);
106 if (cmp < 0)
107 return NULL;
108 if (table == so->table && entry->key == startkey) {
109 if (cmp > 0)
110 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 }
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700112 else {
113 /* Start over if the compare altered the set */
114 return set_lookkey(so, key, hash);
115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 }
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700117 freeslot = (entry->key == dummy) ? entry : NULL;
118
119 /* In the loop, key == dummy is by far (factor of 100s)
120 the least likely outcome, so test for that last. */
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700121 j = i;
122 perturb = hash;
123 while (1) {
124 j ^= 1;
125 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 if (entry->key == NULL) {
127 if (freeslot != NULL)
128 entry = freeslot;
129 break;
130 }
131 if (entry->key == key)
132 break;
Raymond Hettingerae9e6162013-08-20 22:28:24 -0700133 if (entry->hash == hash && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 startkey = entry->key;
135 Py_INCREF(startkey);
136 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
137 Py_DECREF(startkey);
138 if (cmp < 0)
139 return NULL;
140 if (table == so->table && entry->key == startkey) {
141 if (cmp > 0)
142 break;
143 }
144 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return set_lookkey(so, key, hash);
146 }
147 }
Raymond Hettinger07351a02013-08-17 02:39:46 -0700148 if (entry->key == dummy && freeslot == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 freeslot = entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700150
151 i = i * 5 + perturb + 1;
152 j = i & mask;
153 perturb >>= PERTURB_SHIFT;
154
155 entry = &table[j];
156 if (entry->key == NULL) {
157 if (freeslot != NULL)
158 entry = freeslot;
159 break;
160 }
161 if (entry->key == key)
162 break;
Raymond Hettingerae9e6162013-08-20 22:28:24 -0700163 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700164 startkey = entry->key;
165 Py_INCREF(startkey);
166 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
167 Py_DECREF(startkey);
168 if (cmp < 0)
169 return NULL;
170 if (table == so->table && entry->key == startkey) {
171 if (cmp > 0)
172 break;
173 }
174 else {
175 return set_lookkey(so, key, hash);
176 }
177 }
178 if (entry->key == dummy && freeslot == NULL)
179 freeslot = entry;
180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 }
182 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000183}
184
185/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000186 * Hacked up version of set_lookkey which can assume keys are always unicode;
187 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000188 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000189 */
190static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200191set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000192{
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700193 size_t i, j; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200194 size_t perturb;
195 setentry *freeslot;
196 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200198 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 /* Make sure this function doesn't have to handle non-unicode keys,
201 including subclasses of str; e.g., one reason to subclass
202 strings is to override __eq__, and for speed we don't cater to
203 that here. */
204 if (!PyUnicode_CheckExact(key)) {
205 so->lookup = set_lookkey;
206 return set_lookkey(so, key, hash);
207 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700208
Mark Dickinson57e683e2011-09-24 18:18:40 +0100209 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 entry = &table[i];
211 if (entry->key == NULL || entry->key == key)
212 return entry;
213 if (entry->key == dummy)
214 freeslot = entry;
215 else {
216 if (entry->hash == hash && unicode_eq(entry->key, key))
217 return entry;
218 freeslot = NULL;
219 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000220
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700221 j = i;
222 perturb = hash;
223 while (1) {
224 j ^= 1;
225 entry = &table[j];
226 if (entry->key == NULL)
227 return freeslot == NULL ? entry : freeslot;
228 if (entry->key == key
229 || (entry->hash == hash
230 && entry->key != dummy
231 && unicode_eq(entry->key, key)))
232 return entry;
233 if (entry->key == dummy && freeslot == NULL)
234 freeslot = entry;
235
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700236 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700237 j = i & mask;
238 perturb >>= PERTURB_SHIFT;
239
240 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 if (entry->key == NULL)
242 return freeslot == NULL ? entry : freeslot;
243 if (entry->key == key
244 || (entry->hash == hash
245 && entry->key != dummy
246 && unicode_eq(entry->key, key)))
247 return entry;
248 if (entry->key == dummy && freeslot == NULL)
249 freeslot = entry;
250 }
251 assert(0); /* NOT REACHED */
252 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000253}
254
255/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000256Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000257Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000258Eats a reference to key.
259*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000260static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200261set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000262{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200263 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 assert(so->lookup != NULL);
266 entry = so->lookup(so, key, hash);
267 if (entry == NULL)
268 return -1;
269 if (entry->key == NULL) {
270 /* UNUSED */
271 so->fill++;
272 entry->key = key;
273 entry->hash = hash;
274 so->used++;
275 } else if (entry->key == dummy) {
276 /* DUMMY */
277 entry->key = key;
278 entry->hash = hash;
279 so->used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 } else {
281 /* ACTIVE */
282 Py_DECREF(key);
283 }
284 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000285}
286
287/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000288Internal routine used by set_table_resize() to insert an item which is
289known to be absent from the set. This routine also assumes that
290the set contains no deleted entries. Besides the performance benefit,
291using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
292Note that no refcounts are changed by this routine; if needed, the caller
293is responsible for incref'ing `key`.
294*/
295static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200296set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200299 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700300 size_t perturb = hash;
301 size_t mask = (size_t)so->mask;
302 size_t i, j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000303
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700304 i = j = (size_t)hash & mask;
305 while (1) {
306 entry = &table[j];
307 if (entry->key == NULL)
308 break;
309 entry = &table[j ^ 1];
310 if (entry->key == NULL)
311 break;
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700312 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700313 j = i & mask;
314 perturb >>= PERTURB_SHIFT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 }
316 so->fill++;
317 entry->key = key;
318 entry->hash = hash;
319 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000320}
321
322/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000324keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000325actually be smaller than the old one.
326*/
327static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000328set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 Py_ssize_t newsize;
331 setentry *oldtable, *newtable, *entry;
332 Py_ssize_t i;
333 int is_oldtable_malloced;
334 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Find the smallest table size > minused. */
339 for (newsize = PySet_MINSIZE;
340 newsize <= minused && newsize > 0;
341 newsize <<= 1)
342 ;
343 if (newsize <= 0) {
344 PyErr_NoMemory();
345 return -1;
346 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* Get space for a new table. */
349 oldtable = so->table;
350 assert(oldtable != NULL);
351 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if (newsize == PySet_MINSIZE) {
354 /* A large table is shrinking, or we can't get any smaller. */
355 newtable = so->smalltable;
356 if (newtable == oldtable) {
357 if (so->fill == so->used) {
358 /* No dummies, so no point doing anything. */
359 return 0;
360 }
361 /* We're not going to resize it, but rebuild the
362 table anyway to purge old dummy entries.
363 Subtle: This is *necessary* if fill==size,
364 as set_lookkey needs at least one virgin slot to
365 terminate failing searches. If fill < size, it's
366 merely desirable, as dummies slow searches. */
367 assert(so->fill > so->used);
368 memcpy(small_copy, oldtable, sizeof(small_copy));
369 oldtable = small_copy;
370 }
371 }
372 else {
373 newtable = PyMem_NEW(setentry, newsize);
374 if (newtable == NULL) {
375 PyErr_NoMemory();
376 return -1;
377 }
378 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 /* Make the set empty, using the new table. */
381 assert(newtable != oldtable);
382 so->table = newtable;
383 so->mask = newsize - 1;
384 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700385 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* Copy the data over; this is refcount-neutral for active entries;
390 dummy entries aren't copied over, of course */
391 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500392 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000394 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 }
396 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (is_oldtable_malloced)
399 PyMem_DEL(oldtable);
400 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000401}
402
Raymond Hettingerc991db22005-08-11 07:58:45 +0000403/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
404
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200406set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000407{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200408 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000409 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100410 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 assert(so->fill <= so->mask); /* at least one empty slot */
413 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000414 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100415 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000416 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return -1;
418 }
419 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
420 return 0;
421 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000422}
423
424static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200425set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000426{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200427 Py_hash_t hash;
428 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200431 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 hash = PyObject_Hash(key);
433 if (hash == -1)
434 return -1;
435 }
436 assert(so->fill <= so->mask); /* at least one empty slot */
437 n_used = so->used;
438 Py_INCREF(key);
439 if (set_insert_key(so, key, hash) == -1) {
440 Py_DECREF(key);
441 return -1;
442 }
443 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
444 return 0;
445 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000446}
447
448#define DISCARD_NOTFOUND 0
449#define DISCARD_FOUND 1
450
451static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000452set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200453{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000455
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000456 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (entry == NULL)
458 return -1;
459 if (entry->key == NULL || entry->key == dummy)
460 return DISCARD_NOTFOUND;
461 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 entry->key = dummy;
463 so->used--;
464 Py_DECREF(old_key);
465 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000466}
467
468static int
469set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200471 Py_hash_t hash;
472 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200478 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 hash = PyObject_Hash(key);
480 if (hash == -1)
481 return -1;
482 }
483 entry = (so->lookup)(so, key, hash);
484 if (entry == NULL)
485 return -1;
486 if (entry->key == NULL || entry->key == dummy)
487 return DISCARD_NOTFOUND;
488 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 entry->key = dummy;
490 so->used--;
491 Py_DECREF(old_key);
492 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493}
494
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000495static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496set_clear_internal(PySetObject *so)
497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 setentry *entry, *table;
499 int table_is_malloced;
500 Py_ssize_t fill;
501 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 Py_ssize_t i, n;
504 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 n = so->mask + 1;
507 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508#endif
509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 table = so->table;
511 assert(table != NULL);
512 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 /* This is delicate. During the process of clearing the set,
515 * decrefs can cause the set to mutate. To avoid fatal confusion
516 * (voice of experience), we have to make the set empty before
517 * clearing the slots, and never refer to anything via so->ref while
518 * clearing.
519 */
520 fill = so->fill;
521 if (table_is_malloced)
522 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 else if (fill > 0) {
525 /* It's a small table with something that needs to be cleared.
526 * Afraid the only safe way is to copy the set entries into
527 * another small table first.
528 */
529 memcpy(small_copy, table, sizeof(small_copy));
530 table = small_copy;
531 EMPTY_TO_MINSIZE(so);
532 }
533 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 /* Now we can finally clear things. If C had refcounts, we could
536 * assert that the refcount on table is 1 now, i.e. that this function
537 * has unique access to it, so decref side-effects can't alter it.
538 */
539 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 assert(i < n);
542 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (entry->key) {
545 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700546 if (entry->key != dummy)
547 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000549#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 else
551 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000552#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 if (table_is_malloced)
556 PyMem_DEL(table);
557 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000558}
559
560/*
561 * Iterate over a set table. Use like so:
562 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000563 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000564 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000565 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000566 * while (set_next(yourset, &pos, &entry)) {
567 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000568 * }
569 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000570 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000572 */
573static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000574set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 Py_ssize_t i;
577 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200578 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 assert (PyAnySet_Check(so));
581 i = *pos_ptr;
582 assert(i >= 0);
583 table = so->table;
584 mask = so->mask;
585 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
586 i++;
587 *pos_ptr = i+1;
588 if (i > mask)
589 return 0;
590 assert(table[i].key != NULL);
591 *entry_ptr = &table[i];
592 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593}
594
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000595static void
596set_dealloc(PySetObject *so)
597{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200598 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 Py_ssize_t fill = so->fill;
600 PyObject_GC_UnTrack(so);
601 Py_TRASHCAN_SAFE_BEGIN(so)
602 if (so->weakreflist != NULL)
603 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 for (entry = so->table; fill > 0; entry++) {
606 if (entry->key) {
607 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700608 if (entry->key != dummy)
609 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 }
611 }
612 if (so->table != so->smalltable)
613 PyMem_DEL(so->table);
614 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
615 free_list[numfree++] = so;
616 else
617 Py_TYPE(so)->tp_free(so);
618 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000619}
620
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000621static PyObject *
622set_repr(PySetObject *so)
623{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200624 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if (status != 0) {
628 if (status < 0)
629 return NULL;
630 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
631 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 /* shortcut for the empty set */
634 if (!so->used) {
635 Py_ReprLeave((PyObject*)so);
636 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
637 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 keys = PySequence_List((PyObject *)so);
640 if (keys == NULL)
641 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000642
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200643 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 listrepr = PyObject_Repr(keys);
645 Py_DECREF(keys);
646 if (listrepr == NULL)
647 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200648 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200650 if (tmp == NULL)
651 goto done;
652 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200653
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654 if (Py_TYPE(so) != &PySet_Type)
655 result = PyUnicode_FromFormat("%s({%U})",
656 Py_TYPE(so)->tp_name,
657 listrepr);
658 else
659 result = PyUnicode_FromFormat("{%U}", listrepr);
660 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000661done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 Py_ReprLeave((PyObject*)so);
663 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000664}
665
Martin v. Löwis18e16552006-02-15 17:27:45 +0000666static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000667set_len(PyObject *so)
668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000670}
671
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000672static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000673set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000676 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100677 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200678 Py_ssize_t i;
679 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 assert (PyAnySet_Check(so));
682 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 other = (PySetObject*)otherset;
685 if (other == so || other->used == 0)
686 /* a.update(a) or a.update({}); nothing to do */
687 return 0;
688 /* Do one big resize at the start, rather than
689 * incrementally resizing as we insert new keys. Expect
690 * that there will be no (or few) overlapping keys.
691 */
692 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
693 if (set_table_resize(so, (so->used + other->used)*2) != 0)
694 return -1;
695 }
696 for (i = 0; i <= other->mask; i++) {
697 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000698 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100699 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000700 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500701 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000702 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100703 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000704 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 return -1;
706 }
707 }
708 }
709 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000710}
711
712static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000713set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000714{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000715 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200719 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 hash = PyObject_Hash(key);
721 if (hash == -1)
722 return -1;
723 }
724 entry = (so->lookup)(so, key, hash);
725 if (entry == NULL)
726 return -1;
727 key = entry->key;
728 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000729}
730
Raymond Hettingerc991db22005-08-11 07:58:45 +0000731static int
732set_contains_entry(PySetObject *so, setentry *entry)
733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 PyObject *key;
735 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000736
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000737 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 if (lu_entry == NULL)
739 return -1;
740 key = lu_entry->key;
741 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000742}
743
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000744static PyObject *
745set_pop(PySetObject *so)
746{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200747 Py_ssize_t i = 0;
748 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 assert (PyAnySet_Check(so));
752 if (so->used == 0) {
753 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
754 return NULL;
755 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 /* Set entry to "the first" unused or dummy set entry. We abuse
758 * the hash field of slot 0 to hold a search finger:
759 * If slot 0 has a value, use slot 0.
760 * Else slot 0 is being used to hold a search finger,
761 * and we use its hash value as the first index to look.
762 */
763 entry = &so->table[0];
764 if (entry->key == NULL || entry->key == dummy) {
765 i = entry->hash;
766 /* The hash field may be a real hash value, or it may be a
767 * legit search finger, or it may be a once-legit search
768 * finger that's out of bounds now because it wrapped around
769 * or the table shrunk -- simply make sure it's in bounds now.
770 */
771 if (i > so->mask || i < 1)
772 i = 1; /* skip slot 0 */
773 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
774 i++;
775 if (i > so->mask)
776 i = 1;
777 }
778 }
779 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 entry->key = dummy;
781 so->used--;
782 so->table[0].hash = i + 1; /* next place to start */
783 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000784}
785
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000786PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
787Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000788
789static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000790set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 Py_ssize_t pos = 0;
793 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 while (set_next(so, &pos, &entry))
796 Py_VISIT(entry->key);
797 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000798}
799
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000800static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000801frozenset_hash(PyObject *self)
802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800804 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 setentry *entry;
806 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 if (so->hash != -1)
809 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000810
Mark Dickinson57e683e2011-09-24 18:18:40 +0100811 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 while (set_next(so, &pos, &entry)) {
813 /* Work to increase the bit dispersion for closely spaced hash
814 values. The is important because some use cases have many
815 combinations of a small number of elements with nearby
816 hashes so that many distinct combinations collapse to only
817 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000818 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800819 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800821 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800823 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 so->hash = hash;
825 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000826}
827
Raymond Hettingera9d99362005-08-05 00:01:15 +0000828/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000830typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 PyObject_HEAD
832 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
833 Py_ssize_t si_used;
834 Py_ssize_t si_pos;
835 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000836} setiterobject;
837
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000838static void
839setiter_dealloc(setiterobject *si)
840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 Py_XDECREF(si->si_set);
842 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000843}
844
845static int
846setiter_traverse(setiterobject *si, visitproc visit, void *arg)
847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 Py_VISIT(si->si_set);
849 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850}
851
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000852static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853setiter_len(setiterobject *si)
854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 Py_ssize_t len = 0;
856 if (si->si_set != NULL && si->si_used == si->si_set->used)
857 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000858 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000859}
860
Armin Rigof5b3e362006-02-11 21:32:43 +0000861PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000862
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000863static PyObject *setiter_iternext(setiterobject *si);
864
865static PyObject *
866setiter_reduce(setiterobject *si)
867{
868 PyObject *list;
869 setiterobject tmp;
870
871 list = PyList_New(0);
872 if (!list)
873 return NULL;
874
Ezio Melotti0e1af282012-09-28 16:43:40 +0300875 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000876 tmp = *si;
877 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300878
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000879 /* iterate the temporary into a list */
880 for(;;) {
881 PyObject *element = setiter_iternext(&tmp);
882 if (element) {
883 if (PyList_Append(list, element)) {
884 Py_DECREF(element);
885 Py_DECREF(list);
886 Py_XDECREF(tmp.si_set);
887 return NULL;
888 }
889 Py_DECREF(element);
890 } else
891 break;
892 }
893 Py_XDECREF(tmp.si_set);
894 /* check for error */
895 if (tmp.si_set != NULL) {
896 /* we have an error */
897 Py_DECREF(list);
898 return NULL;
899 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200900 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000901}
902
903PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
904
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000905static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000907 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000909};
910
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000911static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200914 Py_ssize_t i, mask;
915 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (so == NULL)
919 return NULL;
920 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 if (si->si_used != so->used) {
923 PyErr_SetString(PyExc_RuntimeError,
924 "Set changed size during iteration");
925 si->si_used = -1; /* Make this state sticky */
926 return NULL;
927 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 i = si->si_pos;
930 assert(i>=0);
931 entry = so->table;
932 mask = so->mask;
933 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
934 i++;
935 si->si_pos = i+1;
936 if (i > mask)
937 goto fail;
938 si->len--;
939 key = entry[i].key;
940 Py_INCREF(key);
941 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000942
943fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 Py_DECREF(so);
945 si->si_set = NULL;
946 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000947}
948
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000949PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 PyVarObject_HEAD_INIT(&PyType_Type, 0)
951 "set_iterator", /* tp_name */
952 sizeof(setiterobject), /* tp_basicsize */
953 0, /* tp_itemsize */
954 /* methods */
955 (destructor)setiter_dealloc, /* tp_dealloc */
956 0, /* tp_print */
957 0, /* tp_getattr */
958 0, /* tp_setattr */
959 0, /* tp_reserved */
960 0, /* tp_repr */
961 0, /* tp_as_number */
962 0, /* tp_as_sequence */
963 0, /* tp_as_mapping */
964 0, /* tp_hash */
965 0, /* tp_call */
966 0, /* tp_str */
967 PyObject_GenericGetAttr, /* tp_getattro */
968 0, /* tp_setattro */
969 0, /* tp_as_buffer */
970 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
971 0, /* tp_doc */
972 (traverseproc)setiter_traverse, /* tp_traverse */
973 0, /* tp_clear */
974 0, /* tp_richcompare */
975 0, /* tp_weaklistoffset */
976 PyObject_SelfIter, /* tp_iter */
977 (iternextfunc)setiter_iternext, /* tp_iternext */
978 setiter_methods, /* tp_methods */
979 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000980};
981
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000982static PyObject *
983set_iter(PySetObject *so)
984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
986 if (si == NULL)
987 return NULL;
988 Py_INCREF(so);
989 si->si_set = so;
990 si->si_used = so->used;
991 si->si_pos = 0;
992 si->len = so->used;
993 _PyObject_GC_TRACK(si);
994 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000995}
996
Raymond Hettingerd7946662005-08-01 21:39:29 +0000997static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000998set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (PyAnySet_Check(other))
1003 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 if (PyDict_CheckExact(other)) {
1006 PyObject *value;
1007 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001008 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 /* Do one big resize at the start, rather than
1012 * incrementally resizing as we insert new keys. Expect
1013 * that there will be no (or few) overlapping keys.
1014 */
1015 if (dictsize == -1)
1016 return -1;
1017 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
1018 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
1019 return -1;
1020 }
1021 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1022 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 an_entry.hash = hash;
1025 an_entry.key = key;
1026 if (set_add_entry(so, &an_entry) == -1)
1027 return -1;
1028 }
1029 return 0;
1030 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 it = PyObject_GetIter(other);
1033 if (it == NULL)
1034 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 while ((key = PyIter_Next(it)) != NULL) {
1037 if (set_add_key(so, key) == -1) {
1038 Py_DECREF(it);
1039 Py_DECREF(key);
1040 return -1;
1041 }
1042 Py_DECREF(key);
1043 }
1044 Py_DECREF(it);
1045 if (PyErr_Occurred())
1046 return -1;
1047 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001048}
1049
1050static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001051set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1056 PyObject *other = PyTuple_GET_ITEM(args, i);
1057 if (set_update_internal(so, other) == -1)
1058 return NULL;
1059 }
1060 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001061}
1062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001064"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001065
1066static PyObject *
1067make_new_set(PyTypeObject *type, PyObject *iterable)
1068{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001069 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 /* create PySetObject structure */
1072 if (numfree &&
1073 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1074 so = free_list[--numfree];
1075 assert (so != NULL && PyAnySet_CheckExact(so));
1076 Py_TYPE(so) = type;
1077 _Py_NewReference((PyObject *)so);
1078 EMPTY_TO_MINSIZE(so);
1079 PyObject_GC_Track(so);
1080 } else {
1081 so = (PySetObject *)type->tp_alloc(type, 0);
1082 if (so == NULL)
1083 return NULL;
1084 /* tp_alloc has already zeroed the structure */
1085 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1086 INIT_NONZERO_SET_SLOTS(so);
1087 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 so->lookup = set_lookkey_unicode;
1090 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 if (iterable != NULL) {
1093 if (set_update_internal(so, iterable) == -1) {
1094 Py_DECREF(so);
1095 return NULL;
1096 }
1097 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001100}
1101
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001102static PyObject *
1103make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1106 if (PyType_IsSubtype(type, &PySet_Type))
1107 type = &PySet_Type;
1108 else
1109 type = &PyFrozenSet_Type;
1110 }
1111 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001112}
1113
Raymond Hettingerd7946662005-08-01 21:39:29 +00001114/* The empty frozenset is a singleton */
1115static PyObject *emptyfrozenset = NULL;
1116
Raymond Hettingera690a992003-11-16 16:17:49 +00001117static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001118frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1123 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1126 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 if (type != &PyFrozenSet_Type)
1129 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (iterable != NULL) {
1132 /* frozenset(f) is idempotent */
1133 if (PyFrozenSet_CheckExact(iterable)) {
1134 Py_INCREF(iterable);
1135 return iterable;
1136 }
1137 result = make_new_set(type, iterable);
1138 if (result == NULL || PySet_GET_SIZE(result))
1139 return result;
1140 Py_DECREF(result);
1141 }
1142 /* The empty frozenset is a singleton */
1143 if (emptyfrozenset == NULL)
1144 emptyfrozenset = make_new_set(type, NULL);
1145 Py_XINCREF(emptyfrozenset);
1146 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001147}
1148
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001149int
1150PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001151{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001152 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 while (numfree) {
1156 numfree--;
1157 so = free_list[numfree];
1158 PyObject_GC_Del(so);
1159 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001160 return freelist_size;
1161}
1162
1163void
1164PySet_Fini(void)
1165{
1166 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001168}
1169
David Malcolm49526f42012-06-22 14:55:41 -04001170/* Print summary info about the state of the optimized allocator */
1171void
1172_PySet_DebugMallocStats(FILE *out)
1173{
1174 _PyDebugAllocatorStats(out,
1175 "free PySetObject",
1176 numfree, sizeof(PySetObject));
1177}
1178
1179
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001180static PyObject *
1181set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1184 return NULL;
1185
1186 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001187}
1188
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001189/* set_swap_bodies() switches the contents of any two sets by moving their
1190 internal data pointers and, if needed, copying the internal smalltables.
1191 Semantically equivalent to:
1192
1193 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1194
1195 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001197 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001199 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001200*/
1201
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001202static void
1203set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 Py_ssize_t t;
1206 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001207 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001209 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 t = a->fill; a->fill = b->fill; b->fill = t;
1212 t = a->used; a->used = b->used; b->used = t;
1213 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 u = a->table;
1216 if (a->table == a->smalltable)
1217 u = b->smalltable;
1218 a->table = b->table;
1219 if (b->table == b->smalltable)
1220 a->table = a->smalltable;
1221 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 if (a->table == a->smalltable || b->table == b->smalltable) {
1226 memcpy(tab, a->smalltable, sizeof(tab));
1227 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1228 memcpy(b->smalltable, tab, sizeof(tab));
1229 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1232 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1233 h = a->hash; a->hash = b->hash; b->hash = h;
1234 } else {
1235 a->hash = -1;
1236 b->hash = -1;
1237 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001238}
1239
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001240static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001241set_copy(PySetObject *so)
1242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001244}
1245
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001246static PyObject *
1247frozenset_copy(PySetObject *so)
1248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 if (PyFrozenSet_CheckExact(so)) {
1250 Py_INCREF(so);
1251 return (PyObject *)so;
1252 }
1253 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001254}
1255
Raymond Hettingera690a992003-11-16 16:17:49 +00001256PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1257
1258static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001259set_clear(PySetObject *so)
1260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 set_clear_internal(so);
1262 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001263}
1264
1265PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1266
1267static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001268set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 PySetObject *result;
1271 PyObject *other;
1272 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 result = (PySetObject *)set_copy(so);
1275 if (result == NULL)
1276 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1279 other = PyTuple_GET_ITEM(args, i);
1280 if ((PyObject *)so == other)
1281 continue;
1282 if (set_update_internal(result, other) == -1) {
1283 Py_DECREF(result);
1284 return NULL;
1285 }
1286 }
1287 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001288}
1289
1290PyDoc_STRVAR(union_doc,
1291 "Return the union of sets as a new set.\n\
1292\n\
1293(i.e. all elements that are in either set.)");
1294
1295static PyObject *
1296set_or(PySetObject *so, PyObject *other)
1297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001299
Brian Curtindfc80e32011-08-10 20:28:54 -05001300 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1301 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 result = (PySetObject *)set_copy(so);
1304 if (result == NULL)
1305 return NULL;
1306 if ((PyObject *)so == other)
1307 return (PyObject *)result;
1308 if (set_update_internal(result, other) == -1) {
1309 Py_DECREF(result);
1310 return NULL;
1311 }
1312 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001313}
1314
Raymond Hettingera690a992003-11-16 16:17:49 +00001315static PyObject *
1316set_ior(PySetObject *so, PyObject *other)
1317{
Brian Curtindfc80e32011-08-10 20:28:54 -05001318 if (!PyAnySet_Check(other))
1319 Py_RETURN_NOTIMPLEMENTED;
1320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 if (set_update_internal(so, other) == -1)
1322 return NULL;
1323 Py_INCREF(so);
1324 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001325}
1326
1327static PyObject *
1328set_intersection(PySetObject *so, PyObject *other)
1329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 PySetObject *result;
1331 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if ((PyObject *)so == other)
1334 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1337 if (result == NULL)
1338 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (PyAnySet_Check(other)) {
1341 Py_ssize_t pos = 0;
1342 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1345 tmp = (PyObject *)so;
1346 so = (PySetObject *)other;
1347 other = tmp;
1348 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 while (set_next((PySetObject *)other, &pos, &entry)) {
1351 int rv = set_contains_entry(so, entry);
1352 if (rv == -1) {
1353 Py_DECREF(result);
1354 return NULL;
1355 }
1356 if (rv) {
1357 if (set_add_entry(result, entry) == -1) {
1358 Py_DECREF(result);
1359 return NULL;
1360 }
1361 }
1362 }
1363 return (PyObject *)result;
1364 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 it = PyObject_GetIter(other);
1367 if (it == NULL) {
1368 Py_DECREF(result);
1369 return NULL;
1370 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 while ((key = PyIter_Next(it)) != NULL) {
1373 int rv;
1374 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001375 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (hash == -1) {
1378 Py_DECREF(it);
1379 Py_DECREF(result);
1380 Py_DECREF(key);
1381 return NULL;
1382 }
1383 entry.hash = hash;
1384 entry.key = key;
1385 rv = set_contains_entry(so, &entry);
1386 if (rv == -1) {
1387 Py_DECREF(it);
1388 Py_DECREF(result);
1389 Py_DECREF(key);
1390 return NULL;
1391 }
1392 if (rv) {
1393 if (set_add_entry(result, &entry) == -1) {
1394 Py_DECREF(it);
1395 Py_DECREF(result);
1396 Py_DECREF(key);
1397 return NULL;
1398 }
1399 }
1400 Py_DECREF(key);
1401 }
1402 Py_DECREF(it);
1403 if (PyErr_Occurred()) {
1404 Py_DECREF(result);
1405 return NULL;
1406 }
1407 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001408}
1409
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001410static PyObject *
1411set_intersection_multi(PySetObject *so, PyObject *args)
1412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 Py_ssize_t i;
1414 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if (PyTuple_GET_SIZE(args) == 0)
1417 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 Py_INCREF(so);
1420 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1421 PyObject *other = PyTuple_GET_ITEM(args, i);
1422 PyObject *newresult = set_intersection((PySetObject *)result, other);
1423 if (newresult == NULL) {
1424 Py_DECREF(result);
1425 return NULL;
1426 }
1427 Py_DECREF(result);
1428 result = newresult;
1429 }
1430 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001431}
1432
Raymond Hettingera690a992003-11-16 16:17:49 +00001433PyDoc_STRVAR(intersection_doc,
1434"Return the intersection of two sets as a new set.\n\
1435\n\
1436(i.e. all elements that are in both sets.)");
1437
1438static PyObject *
1439set_intersection_update(PySetObject *so, PyObject *other)
1440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 tmp = set_intersection(so, other);
1444 if (tmp == NULL)
1445 return NULL;
1446 set_swap_bodies(so, (PySetObject *)tmp);
1447 Py_DECREF(tmp);
1448 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001449}
1450
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001451static PyObject *
1452set_intersection_update_multi(PySetObject *so, PyObject *args)
1453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 tmp = set_intersection_multi(so, args);
1457 if (tmp == NULL)
1458 return NULL;
1459 set_swap_bodies(so, (PySetObject *)tmp);
1460 Py_DECREF(tmp);
1461 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001462}
1463
Raymond Hettingera690a992003-11-16 16:17:49 +00001464PyDoc_STRVAR(intersection_update_doc,
1465"Update a set with the intersection of itself and another.");
1466
1467static PyObject *
1468set_and(PySetObject *so, PyObject *other)
1469{
Brian Curtindfc80e32011-08-10 20:28:54 -05001470 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1471 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001473}
1474
1475static PyObject *
1476set_iand(PySetObject *so, PyObject *other)
1477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001479
Brian Curtindfc80e32011-08-10 20:28:54 -05001480 if (!PyAnySet_Check(other))
1481 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 result = set_intersection_update(so, other);
1483 if (result == NULL)
1484 return NULL;
1485 Py_DECREF(result);
1486 Py_INCREF(so);
1487 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001488}
1489
Guido van Rossum58da9312007-11-10 23:39:45 +00001490static PyObject *
1491set_isdisjoint(PySetObject *so, PyObject *other)
1492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 if ((PyObject *)so == other) {
1496 if (PySet_GET_SIZE(so) == 0)
1497 Py_RETURN_TRUE;
1498 else
1499 Py_RETURN_FALSE;
1500 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 if (PyAnySet_CheckExact(other)) {
1503 Py_ssize_t pos = 0;
1504 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1507 tmp = (PyObject *)so;
1508 so = (PySetObject *)other;
1509 other = tmp;
1510 }
1511 while (set_next((PySetObject *)other, &pos, &entry)) {
1512 int rv = set_contains_entry(so, entry);
1513 if (rv == -1)
1514 return NULL;
1515 if (rv)
1516 Py_RETURN_FALSE;
1517 }
1518 Py_RETURN_TRUE;
1519 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 it = PyObject_GetIter(other);
1522 if (it == NULL)
1523 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 while ((key = PyIter_Next(it)) != NULL) {
1526 int rv;
1527 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001528 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 if (hash == -1) {
1531 Py_DECREF(key);
1532 Py_DECREF(it);
1533 return NULL;
1534 }
1535 entry.hash = hash;
1536 entry.key = key;
1537 rv = set_contains_entry(so, &entry);
1538 Py_DECREF(key);
1539 if (rv == -1) {
1540 Py_DECREF(it);
1541 return NULL;
1542 }
1543 if (rv) {
1544 Py_DECREF(it);
1545 Py_RETURN_FALSE;
1546 }
1547 }
1548 Py_DECREF(it);
1549 if (PyErr_Occurred())
1550 return NULL;
1551 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001552}
1553
1554PyDoc_STRVAR(isdisjoint_doc,
1555"Return True if two sets have a null intersection.");
1556
Neal Norwitz6576bd82005-11-13 18:41:28 +00001557static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001558set_difference_update_internal(PySetObject *so, PyObject *other)
1559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 if ((PyObject *)so == other)
1561 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if (PyAnySet_Check(other)) {
1564 setentry *entry;
1565 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 while (set_next((PySetObject *)other, &pos, &entry))
1568 if (set_discard_entry(so, entry) == -1)
1569 return -1;
1570 } else {
1571 PyObject *key, *it;
1572 it = PyObject_GetIter(other);
1573 if (it == NULL)
1574 return -1;
1575
1576 while ((key = PyIter_Next(it)) != NULL) {
1577 if (set_discard_key(so, key) == -1) {
1578 Py_DECREF(it);
1579 Py_DECREF(key);
1580 return -1;
1581 }
1582 Py_DECREF(key);
1583 }
1584 Py_DECREF(it);
1585 if (PyErr_Occurred())
1586 return -1;
1587 }
1588 /* If more than 1/5 are dummies, then resize them away. */
1589 if ((so->fill - so->used) * 5 < so->mask)
1590 return 0;
1591 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001592}
1593
Raymond Hettingera690a992003-11-16 16:17:49 +00001594static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001595set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1600 PyObject *other = PyTuple_GET_ITEM(args, i);
1601 if (set_difference_update_internal(so, other) == -1)
1602 return NULL;
1603 }
1604 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001605}
1606
1607PyDoc_STRVAR(difference_update_doc,
1608"Remove all elements of another set from this set.");
1609
1610static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001611set_copy_and_difference(PySetObject *so, PyObject *other)
1612{
1613 PyObject *result;
1614
1615 result = set_copy(so);
1616 if (result == NULL)
1617 return NULL;
1618 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1619 return result;
1620 Py_DECREF(result);
1621 return NULL;
1622}
1623
1624static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001625set_difference(PySetObject *so, PyObject *other)
1626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 PyObject *result;
1628 setentry *entry;
1629 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001632 return set_copy_and_difference(so, other);
1633 }
1634
1635 /* If len(so) much more than len(other), it's more efficient to simply copy
1636 * so and then iterate other looking for common elements. */
1637 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1638 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 result = make_new_set_basetype(Py_TYPE(so), NULL);
1642 if (result == NULL)
1643 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 if (PyDict_CheckExact(other)) {
1646 while (set_next(so, &pos, &entry)) {
1647 setentry entrycopy;
1648 entrycopy.hash = entry->hash;
1649 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001650 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1652 Py_DECREF(result);
1653 return NULL;
1654 }
1655 }
1656 }
1657 return result;
1658 }
1659
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001660 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 while (set_next(so, &pos, &entry)) {
1662 int rv = set_contains_entry((PySetObject *)other, entry);
1663 if (rv == -1) {
1664 Py_DECREF(result);
1665 return NULL;
1666 }
1667 if (!rv) {
1668 if (set_add_entry((PySetObject *)result, entry) == -1) {
1669 Py_DECREF(result);
1670 return NULL;
1671 }
1672 }
1673 }
1674 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001675}
1676
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001677static PyObject *
1678set_difference_multi(PySetObject *so, PyObject *args)
1679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 Py_ssize_t i;
1681 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 if (PyTuple_GET_SIZE(args) == 0)
1684 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 other = PyTuple_GET_ITEM(args, 0);
1687 result = set_difference(so, other);
1688 if (result == NULL)
1689 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1692 other = PyTuple_GET_ITEM(args, i);
1693 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1694 Py_DECREF(result);
1695 return NULL;
1696 }
1697 }
1698 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001699}
1700
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001701PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001702"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001703\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001704(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001705static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001706set_sub(PySetObject *so, PyObject *other)
1707{
Brian Curtindfc80e32011-08-10 20:28:54 -05001708 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1709 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001711}
1712
1713static PyObject *
1714set_isub(PySetObject *so, PyObject *other)
1715{
Brian Curtindfc80e32011-08-10 20:28:54 -05001716 if (!PyAnySet_Check(other))
1717 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 if (set_difference_update_internal(so, other) == -1)
1719 return NULL;
1720 Py_INCREF(so);
1721 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001722}
1723
1724static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001725set_symmetric_difference_update(PySetObject *so, PyObject *other)
1726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 PySetObject *otherset;
1728 PyObject *key;
1729 Py_ssize_t pos = 0;
1730 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 if ((PyObject *)so == other)
1733 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 if (PyDict_CheckExact(other)) {
1736 PyObject *value;
1737 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001738 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1740 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001741
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001742 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 an_entry.hash = hash;
1744 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001747 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001748 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001751 if (rv == DISCARD_NOTFOUND) {
1752 if (set_add_entry(so, &an_entry) == -1) {
1753 Py_DECREF(key);
1754 return NULL;
1755 }
1756 }
Georg Brandl2d444492010-09-03 10:52:55 +00001757 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 }
1759 Py_RETURN_NONE;
1760 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (PyAnySet_Check(other)) {
1763 Py_INCREF(other);
1764 otherset = (PySetObject *)other;
1765 } else {
1766 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1767 if (otherset == NULL)
1768 return NULL;
1769 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 while (set_next(otherset, &pos, &entry)) {
1772 int rv = set_discard_entry(so, entry);
1773 if (rv == -1) {
1774 Py_DECREF(otherset);
1775 return NULL;
1776 }
1777 if (rv == DISCARD_NOTFOUND) {
1778 if (set_add_entry(so, entry) == -1) {
1779 Py_DECREF(otherset);
1780 return NULL;
1781 }
1782 }
1783 }
1784 Py_DECREF(otherset);
1785 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001786}
1787
1788PyDoc_STRVAR(symmetric_difference_update_doc,
1789"Update a set with the symmetric difference of itself and another.");
1790
1791static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001792set_symmetric_difference(PySetObject *so, PyObject *other)
1793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 PyObject *rv;
1795 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1798 if (otherset == NULL)
1799 return NULL;
1800 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1801 if (rv == NULL)
1802 return NULL;
1803 Py_DECREF(rv);
1804 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001805}
1806
1807PyDoc_STRVAR(symmetric_difference_doc,
1808"Return the symmetric difference of two sets as a new set.\n\
1809\n\
1810(i.e. all elements that are in exactly one of the sets.)");
1811
1812static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001813set_xor(PySetObject *so, PyObject *other)
1814{
Brian Curtindfc80e32011-08-10 20:28:54 -05001815 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1816 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001818}
1819
1820static PyObject *
1821set_ixor(PySetObject *so, PyObject *other)
1822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001824
Brian Curtindfc80e32011-08-10 20:28:54 -05001825 if (!PyAnySet_Check(other))
1826 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 result = set_symmetric_difference_update(so, other);
1828 if (result == NULL)
1829 return NULL;
1830 Py_DECREF(result);
1831 Py_INCREF(so);
1832 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001833}
1834
1835static PyObject *
1836set_issubset(PySetObject *so, PyObject *other)
1837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 setentry *entry;
1839 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 if (!PyAnySet_Check(other)) {
1842 PyObject *tmp, *result;
1843 tmp = make_new_set(&PySet_Type, other);
1844 if (tmp == NULL)
1845 return NULL;
1846 result = set_issubset(so, tmp);
1847 Py_DECREF(tmp);
1848 return result;
1849 }
1850 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1851 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 while (set_next(so, &pos, &entry)) {
1854 int rv = set_contains_entry((PySetObject *)other, entry);
1855 if (rv == -1)
1856 return NULL;
1857 if (!rv)
1858 Py_RETURN_FALSE;
1859 }
1860 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001861}
1862
1863PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1864
1865static PyObject *
1866set_issuperset(PySetObject *so, PyObject *other)
1867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 if (!PyAnySet_Check(other)) {
1871 tmp = make_new_set(&PySet_Type, other);
1872 if (tmp == NULL)
1873 return NULL;
1874 result = set_issuperset(so, tmp);
1875 Py_DECREF(tmp);
1876 return result;
1877 }
1878 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001879}
1880
1881PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1882
Raymond Hettingera690a992003-11-16 16:17:49 +00001883static PyObject *
1884set_richcompare(PySetObject *v, PyObject *w, int op)
1885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001887
Brian Curtindfc80e32011-08-10 20:28:54 -05001888 if(!PyAnySet_Check(w))
1889 Py_RETURN_NOTIMPLEMENTED;
1890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 switch (op) {
1892 case Py_EQ:
1893 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1894 Py_RETURN_FALSE;
1895 if (v->hash != -1 &&
1896 ((PySetObject *)w)->hash != -1 &&
1897 v->hash != ((PySetObject *)w)->hash)
1898 Py_RETURN_FALSE;
1899 return set_issubset(v, w);
1900 case Py_NE:
1901 r1 = set_richcompare(v, w, Py_EQ);
1902 if (r1 == NULL)
1903 return NULL;
1904 r2 = PyBool_FromLong(PyObject_Not(r1));
1905 Py_DECREF(r1);
1906 return r2;
1907 case Py_LE:
1908 return set_issubset(v, w);
1909 case Py_GE:
1910 return set_issuperset(v, w);
1911 case Py_LT:
1912 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1913 Py_RETURN_FALSE;
1914 return set_issubset(v, w);
1915 case Py_GT:
1916 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1917 Py_RETURN_FALSE;
1918 return set_issuperset(v, w);
1919 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001920 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001921}
1922
Raymond Hettingera690a992003-11-16 16:17:49 +00001923static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001924set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 if (set_add_key(so, key) == -1)
1927 return NULL;
1928 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001929}
1930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001932"Add an element to a set.\n\
1933\n\
1934This has no effect if the element is already present.");
1935
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001936static int
1937set_contains(PySetObject *so, PyObject *key)
1938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 PyObject *tmpkey;
1940 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 rv = set_contains_key(so, key);
1943 if (rv == -1) {
1944 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1945 return -1;
1946 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001947 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 if (tmpkey == NULL)
1949 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001950 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 Py_DECREF(tmpkey);
1952 }
1953 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001954}
1955
1956static PyObject *
1957set_direct_contains(PySetObject *so, PyObject *key)
1958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 result = set_contains(so, key);
1962 if (result == -1)
1963 return NULL;
1964 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001965}
1966
1967PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1968
Raymond Hettingera690a992003-11-16 16:17:49 +00001969static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001970set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 PyObject *tmpkey;
1973 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 rv = set_discard_key(so, key);
1976 if (rv == -1) {
1977 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1978 return NULL;
1979 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001980 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 if (tmpkey == NULL)
1982 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 Py_DECREF(tmpkey);
1985 if (rv == -1)
1986 return NULL;
1987 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 if (rv == DISCARD_NOTFOUND) {
1990 set_key_error(key);
1991 return NULL;
1992 }
1993 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001994}
1995
1996PyDoc_STRVAR(remove_doc,
1997"Remove an element from a set; it must be a member.\n\
1998\n\
1999If the element is not a member, raise a KeyError.");
2000
2001static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00002002set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00002003{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04002004 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 rv = set_discard_key(so, key);
2008 if (rv == -1) {
2009 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
2010 return NULL;
2011 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00002012 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 if (tmpkey == NULL)
2014 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002015 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002017 if (rv == -1)
2018 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 }
2020 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00002021}
2022
2023PyDoc_STRVAR(discard_doc,
2024"Remove an element from a set if it is a member.\n\
2025\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002027
2028static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00002029set_reduce(PySetObject *so)
2030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002032 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00002033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 keys = PySequence_List((PyObject *)so);
2035 if (keys == NULL)
2036 goto done;
2037 args = PyTuple_Pack(1, keys);
2038 if (args == NULL)
2039 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002040 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 if (dict == NULL) {
2042 PyErr_Clear();
2043 dict = Py_None;
2044 Py_INCREF(dict);
2045 }
2046 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002047done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 Py_XDECREF(args);
2049 Py_XDECREF(keys);
2050 Py_XDECREF(dict);
2051 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002052}
2053
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002054static PyObject *
2055set_sizeof(PySetObject *so)
2056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 res = sizeof(PySetObject);
2060 if (so->table != so->smalltable)
2061 res = res + (so->mask + 1) * sizeof(setentry);
2062 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002063}
2064
2065PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002066static int
2067set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 if (!PyAnySet_Check(self))
2072 return -1;
2073 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2074 return -1;
2075 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2076 return -1;
2077 set_clear_internal(self);
2078 self->hash = -1;
2079 if (iterable == NULL)
2080 return 0;
2081 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002082}
2083
Raymond Hettingera690a992003-11-16 16:17:49 +00002084static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 set_len, /* sq_length */
2086 0, /* sq_concat */
2087 0, /* sq_repeat */
2088 0, /* sq_item */
2089 0, /* sq_slice */
2090 0, /* sq_ass_item */
2091 0, /* sq_ass_slice */
2092 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002093};
2094
2095/* set object ********************************************************/
2096
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002097#ifdef Py_DEBUG
2098static PyObject *test_c_api(PySetObject *so);
2099
2100PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2101All is well if assertions don't fail.");
2102#endif
2103
Raymond Hettingera690a992003-11-16 16:17:49 +00002104static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 {"add", (PyCFunction)set_add, METH_O,
2106 add_doc},
2107 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2108 clear_doc},
2109 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2110 contains_doc},
2111 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2112 copy_doc},
2113 {"discard", (PyCFunction)set_discard, METH_O,
2114 discard_doc},
2115 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2116 difference_doc},
2117 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2118 difference_update_doc},
2119 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2120 intersection_doc},
2121 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2122 intersection_update_doc},
2123 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2124 isdisjoint_doc},
2125 {"issubset", (PyCFunction)set_issubset, METH_O,
2126 issubset_doc},
2127 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2128 issuperset_doc},
2129 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2130 pop_doc},
2131 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2132 reduce_doc},
2133 {"remove", (PyCFunction)set_remove, METH_O,
2134 remove_doc},
2135 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2136 sizeof_doc},
2137 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2138 symmetric_difference_doc},
2139 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2140 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002141#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2143 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002144#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 {"union", (PyCFunction)set_union, METH_VARARGS,
2146 union_doc},
2147 {"update", (PyCFunction)set_update, METH_VARARGS,
2148 update_doc},
2149 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002150};
2151
2152static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 0, /*nb_add*/
2154 (binaryfunc)set_sub, /*nb_subtract*/
2155 0, /*nb_multiply*/
2156 0, /*nb_remainder*/
2157 0, /*nb_divmod*/
2158 0, /*nb_power*/
2159 0, /*nb_negative*/
2160 0, /*nb_positive*/
2161 0, /*nb_absolute*/
2162 0, /*nb_bool*/
2163 0, /*nb_invert*/
2164 0, /*nb_lshift*/
2165 0, /*nb_rshift*/
2166 (binaryfunc)set_and, /*nb_and*/
2167 (binaryfunc)set_xor, /*nb_xor*/
2168 (binaryfunc)set_or, /*nb_or*/
2169 0, /*nb_int*/
2170 0, /*nb_reserved*/
2171 0, /*nb_float*/
2172 0, /*nb_inplace_add*/
2173 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2174 0, /*nb_inplace_multiply*/
2175 0, /*nb_inplace_remainder*/
2176 0, /*nb_inplace_power*/
2177 0, /*nb_inplace_lshift*/
2178 0, /*nb_inplace_rshift*/
2179 (binaryfunc)set_iand, /*nb_inplace_and*/
2180 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2181 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002182};
2183
2184PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002185"set() -> new empty set object\n\
2186set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002187\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002188Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002189
2190PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2192 "set", /* tp_name */
2193 sizeof(PySetObject), /* tp_basicsize */
2194 0, /* tp_itemsize */
2195 /* methods */
2196 (destructor)set_dealloc, /* tp_dealloc */
2197 0, /* tp_print */
2198 0, /* tp_getattr */
2199 0, /* tp_setattr */
2200 0, /* tp_reserved */
2201 (reprfunc)set_repr, /* tp_repr */
2202 &set_as_number, /* tp_as_number */
2203 &set_as_sequence, /* tp_as_sequence */
2204 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002205 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 0, /* tp_call */
2207 0, /* tp_str */
2208 PyObject_GenericGetAttr, /* tp_getattro */
2209 0, /* tp_setattro */
2210 0, /* tp_as_buffer */
2211 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2212 Py_TPFLAGS_BASETYPE, /* tp_flags */
2213 set_doc, /* tp_doc */
2214 (traverseproc)set_traverse, /* tp_traverse */
2215 (inquiry)set_clear_internal, /* tp_clear */
2216 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002217 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2218 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 0, /* tp_iternext */
2220 set_methods, /* tp_methods */
2221 0, /* tp_members */
2222 0, /* tp_getset */
2223 0, /* tp_base */
2224 0, /* tp_dict */
2225 0, /* tp_descr_get */
2226 0, /* tp_descr_set */
2227 0, /* tp_dictoffset */
2228 (initproc)set_init, /* tp_init */
2229 PyType_GenericAlloc, /* tp_alloc */
2230 set_new, /* tp_new */
2231 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002232};
2233
2234/* frozenset object ********************************************************/
2235
2236
2237static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2239 contains_doc},
2240 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2241 copy_doc},
2242 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2243 difference_doc},
2244 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2245 intersection_doc},
2246 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2247 isdisjoint_doc},
2248 {"issubset", (PyCFunction)set_issubset, METH_O,
2249 issubset_doc},
2250 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2251 issuperset_doc},
2252 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2253 reduce_doc},
2254 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2255 sizeof_doc},
2256 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2257 symmetric_difference_doc},
2258 {"union", (PyCFunction)set_union, METH_VARARGS,
2259 union_doc},
2260 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002261};
2262
2263static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 0, /*nb_add*/
2265 (binaryfunc)set_sub, /*nb_subtract*/
2266 0, /*nb_multiply*/
2267 0, /*nb_remainder*/
2268 0, /*nb_divmod*/
2269 0, /*nb_power*/
2270 0, /*nb_negative*/
2271 0, /*nb_positive*/
2272 0, /*nb_absolute*/
2273 0, /*nb_bool*/
2274 0, /*nb_invert*/
2275 0, /*nb_lshift*/
2276 0, /*nb_rshift*/
2277 (binaryfunc)set_and, /*nb_and*/
2278 (binaryfunc)set_xor, /*nb_xor*/
2279 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002280};
2281
2282PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002283"frozenset() -> empty frozenset object\n\
2284frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002285\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002286Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002287
2288PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2290 "frozenset", /* tp_name */
2291 sizeof(PySetObject), /* tp_basicsize */
2292 0, /* tp_itemsize */
2293 /* methods */
2294 (destructor)set_dealloc, /* tp_dealloc */
2295 0, /* tp_print */
2296 0, /* tp_getattr */
2297 0, /* tp_setattr */
2298 0, /* tp_reserved */
2299 (reprfunc)set_repr, /* tp_repr */
2300 &frozenset_as_number, /* tp_as_number */
2301 &set_as_sequence, /* tp_as_sequence */
2302 0, /* tp_as_mapping */
2303 frozenset_hash, /* tp_hash */
2304 0, /* tp_call */
2305 0, /* tp_str */
2306 PyObject_GenericGetAttr, /* tp_getattro */
2307 0, /* tp_setattro */
2308 0, /* tp_as_buffer */
2309 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2310 Py_TPFLAGS_BASETYPE, /* tp_flags */
2311 frozenset_doc, /* tp_doc */
2312 (traverseproc)set_traverse, /* tp_traverse */
2313 (inquiry)set_clear_internal, /* tp_clear */
2314 (richcmpfunc)set_richcompare, /* tp_richcompare */
2315 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2316 (getiterfunc)set_iter, /* tp_iter */
2317 0, /* tp_iternext */
2318 frozenset_methods, /* tp_methods */
2319 0, /* tp_members */
2320 0, /* tp_getset */
2321 0, /* tp_base */
2322 0, /* tp_dict */
2323 0, /* tp_descr_get */
2324 0, /* tp_descr_set */
2325 0, /* tp_dictoffset */
2326 0, /* tp_init */
2327 PyType_GenericAlloc, /* tp_alloc */
2328 frozenset_new, /* tp_new */
2329 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002330};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002331
2332
2333/***** C API functions *************************************************/
2334
2335PyObject *
2336PySet_New(PyObject *iterable)
2337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002339}
2340
2341PyObject *
2342PyFrozenSet_New(PyObject *iterable)
2343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002345}
2346
Neal Norwitz8c49c822006-03-04 18:41:19 +00002347Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002348PySet_Size(PyObject *anyset)
2349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 if (!PyAnySet_Check(anyset)) {
2351 PyErr_BadInternalCall();
2352 return -1;
2353 }
2354 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002355}
2356
2357int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002358PySet_Clear(PyObject *set)
2359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 if (!PySet_Check(set)) {
2361 PyErr_BadInternalCall();
2362 return -1;
2363 }
2364 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002365}
2366
2367int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002368PySet_Contains(PyObject *anyset, PyObject *key)
2369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 if (!PyAnySet_Check(anyset)) {
2371 PyErr_BadInternalCall();
2372 return -1;
2373 }
2374 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002375}
2376
2377int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002378PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 if (!PySet_Check(set)) {
2381 PyErr_BadInternalCall();
2382 return -1;
2383 }
2384 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002385}
2386
2387int
Christian Heimesfd66e512008-01-29 12:18:50 +00002388PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 if (!PySet_Check(anyset) &&
2391 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2392 PyErr_BadInternalCall();
2393 return -1;
2394 }
2395 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002396}
2397
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002398int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002399_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 if (!PyAnySet_Check(set)) {
2404 PyErr_BadInternalCall();
2405 return -1;
2406 }
2407 if (set_next((PySetObject *)set, pos, &entry) == 0)
2408 return 0;
2409 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002410 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002412}
2413
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002414PyObject *
2415PySet_Pop(PyObject *set)
2416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 if (!PySet_Check(set)) {
2418 PyErr_BadInternalCall();
2419 return NULL;
2420 }
2421 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002422}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002423
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002424int
2425_PySet_Update(PyObject *set, PyObject *iterable)
2426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 if (!PySet_Check(set)) {
2428 PyErr_BadInternalCall();
2429 return -1;
2430 }
2431 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002432}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002433
2434#ifdef Py_DEBUG
2435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002437 Returns True and original set is restored. */
2438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439#define assertRaises(call_return_value, exception) \
2440 do { \
2441 assert(call_return_value); \
2442 assert(PyErr_ExceptionMatches(exception)); \
2443 PyErr_Clear(); \
2444 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002445
2446static PyObject *
2447test_c_api(PySetObject *so)
2448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 Py_ssize_t count;
2450 char *s;
2451 Py_ssize_t i;
2452 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2453 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002454 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 /* Verify preconditions */
2458 assert(PyAnySet_Check(ob));
2459 assert(PyAnySet_CheckExact(ob));
2460 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 /* so.clear(); so |= set("abc"); */
2463 str = PyUnicode_FromString("abc");
2464 if (str == NULL)
2465 return NULL;
2466 set_clear_internal(so);
2467 if (set_update_internal(so, str) == -1) {
2468 Py_DECREF(str);
2469 return NULL;
2470 }
2471 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 /* Exercise type/size checks */
2474 assert(PySet_Size(ob) == 3);
2475 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 /* Raise TypeError for non-iterable constructor arguments */
2478 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2479 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 /* Raise TypeError for unhashable key */
2482 dup = PySet_New(ob);
2483 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2484 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2485 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 /* Exercise successful pop, contains, add, and discard */
2488 elem = PySet_Pop(ob);
2489 assert(PySet_Contains(ob, elem) == 0);
2490 assert(PySet_GET_SIZE(ob) == 2);
2491 assert(PySet_Add(ob, elem) == 0);
2492 assert(PySet_Contains(ob, elem) == 1);
2493 assert(PySet_GET_SIZE(ob) == 3);
2494 assert(PySet_Discard(ob, elem) == 1);
2495 assert(PySet_GET_SIZE(ob) == 2);
2496 assert(PySet_Discard(ob, elem) == 0);
2497 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 /* Exercise clear */
2500 dup2 = PySet_New(dup);
2501 assert(PySet_Clear(dup2) == 0);
2502 assert(PySet_Size(dup2) == 0);
2503 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 /* Raise SystemError on clear or update of frozen set */
2506 f = PyFrozenSet_New(dup);
2507 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2508 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2509 assert(PySet_Add(f, elem) == 0);
2510 Py_INCREF(f);
2511 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2512 Py_DECREF(f);
2513 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 /* Exercise direct iteration */
2516 i = 0, count = 0;
2517 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2518 s = _PyUnicode_AsString(x);
2519 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2520 count++;
2521 }
2522 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 /* Exercise updates */
2525 dup2 = PySet_New(NULL);
2526 assert(_PySet_Update(dup2, dup) == 0);
2527 assert(PySet_Size(dup2) == 3);
2528 assert(_PySet_Update(dup2, dup) == 0);
2529 assert(PySet_Size(dup2) == 3);
2530 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 /* Raise SystemError when self argument is not a set or frozenset. */
2533 t = PyTuple_New(0);
2534 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2535 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2536 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 /* Raise SystemError when self argument is not a set. */
2539 f = PyFrozenSet_New(dup);
2540 assert(PySet_Size(f) == 3);
2541 assert(PyFrozenSet_CheckExact(f));
2542 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2543 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2544 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 /* Raise KeyError when popping from an empty set */
2547 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2548 Py_DECREF(ob);
2549 assert(PySet_GET_SIZE(ob) == 0);
2550 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 /* Restore the set from the copy using the PyNumber API */
2553 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2554 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 /* Verify constructors accept NULL arguments */
2557 f = PySet_New(NULL);
2558 assert(f != NULL);
2559 assert(PySet_GET_SIZE(f) == 0);
2560 Py_DECREF(f);
2561 f = PyFrozenSet_New(NULL);
2562 assert(f != NULL);
2563 assert(PyFrozenSet_CheckExact(f));
2564 assert(PySet_GET_SIZE(f) == 0);
2565 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 Py_DECREF(elem);
2568 Py_DECREF(dup);
2569 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002570}
2571
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002572#undef assertRaises
2573
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002574#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002575
2576/***** Dummy Struct *************************************************/
2577
2578static PyObject *
2579dummy_repr(PyObject *op)
2580{
2581 return PyUnicode_FromString("<dummy key>");
2582}
2583
2584static void
2585dummy_dealloc(PyObject* ignore)
2586{
2587 Py_FatalError("deallocating <dummy key>");
2588}
2589
2590static PyTypeObject _PySetDummy_Type = {
2591 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2592 "<dummy key> type",
2593 0,
2594 0,
2595 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2596 0, /*tp_print*/
2597 0, /*tp_getattr*/
2598 0, /*tp_setattr*/
2599 0, /*tp_reserved*/
2600 dummy_repr, /*tp_repr*/
2601 0, /*tp_as_number*/
2602 0, /*tp_as_sequence*/
2603 0, /*tp_as_mapping*/
2604 0, /*tp_hash */
2605 0, /*tp_call */
2606 0, /*tp_str */
2607 0, /*tp_getattro */
2608 0, /*tp_setattro */
2609 0, /*tp_as_buffer */
2610 Py_TPFLAGS_DEFAULT, /*tp_flags */
2611};
2612
2613static PyObject _dummy_struct = {
2614 _PyObject_EXTRA_INIT
2615 2, &_PySetDummy_Type
2616};
2617