blob: 3d9deac096f172ded9d57144d0751aea5857f7b2 [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 Hettingera9d99362005-08-05 00:01:15 +000032static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#ifdef Py_REF_DEBUG
35PyObject *
36_PySet_Dummy(void)
37{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039}
40#endif
41
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042#define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
45 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000046 } while(0)
47
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
51 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052 } while(0)
53
Raymond Hettingerbc841a12005-08-07 13:02:53 +000054/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000055#ifndef PySet_MAXFREELIST
56#define PySet_MAXFREELIST 80
57#endif
58static PySetObject *free_list[PySet_MAXFREELIST];
59static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060
Christian Heimes0ded5b52007-12-10 15:50:56 +000061
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062/*
63The basic lookup function used by all operations.
64This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65Open addressing is preferred over chaining since the link overhead for
66chaining would be substantial (100% with typical malloc overhead).
67
68The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000069probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070071To improve cache locality, each probe is done in pairs.
72After the probe is examined, an adjacent entry is then examined as well.
73The likelihood is that an adjacent entry is in the same cache line and
74can be examined more cheaply than another probe elsewhere in memory.
75
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000076All arithmetic on hash should ignore overflow.
77
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000078Unlike the dictionary implementation, the lookkey functions can return
79NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000080*/
81
82static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020083set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000084{
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070085 size_t i, j; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020086 size_t perturb;
87 setentry *freeslot;
88 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020090 setentry *entry;
91 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Mark Dickinson57e683e2011-09-24 18:18:40 +010094 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 entry = &table[i];
96 if (entry->key == NULL || entry->key == key)
97 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070098 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger237b34b2013-08-17 02:31:53 -070099 startkey = entry->key;
100 Py_INCREF(startkey);
101 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
102 Py_DECREF(startkey);
103 if (cmp < 0)
104 return NULL;
105 if (table == so->table && entry->key == startkey) {
106 if (cmp > 0)
107 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 }
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700109 else {
110 /* Start over if the compare altered the set */
111 return set_lookkey(so, key, hash);
112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 }
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700114 freeslot = (entry->key == dummy) ? entry : NULL;
115
116 /* In the loop, key == dummy is by far (factor of 100s)
117 the least likely outcome, so test for that last. */
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700118 j = i;
119 perturb = hash;
120 while (1) {
121 j ^= 1;
122 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 if (entry->key == NULL) {
124 if (freeslot != NULL)
125 entry = freeslot;
126 break;
127 }
128 if (entry->key == key)
129 break;
Raymond Hettingerae9e6162013-08-20 22:28:24 -0700130 if (entry->hash == hash && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 startkey = entry->key;
132 Py_INCREF(startkey);
133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
134 Py_DECREF(startkey);
135 if (cmp < 0)
136 return NULL;
137 if (table == so->table && entry->key == startkey) {
138 if (cmp > 0)
139 break;
140 }
141 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 return set_lookkey(so, key, hash);
143 }
144 }
Raymond Hettinger07351a02013-08-17 02:39:46 -0700145 if (entry->key == dummy && freeslot == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 freeslot = entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700147
148 i = i * 5 + perturb + 1;
149 j = i & mask;
150 perturb >>= PERTURB_SHIFT;
151
152 entry = &table[j];
153 if (entry->key == NULL) {
154 if (freeslot != NULL)
155 entry = freeslot;
156 break;
157 }
158 if (entry->key == key)
159 break;
Raymond Hettingerae9e6162013-08-20 22:28:24 -0700160 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700161 startkey = entry->key;
162 Py_INCREF(startkey);
163 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
164 Py_DECREF(startkey);
165 if (cmp < 0)
166 return NULL;
167 if (table == so->table && entry->key == startkey) {
168 if (cmp > 0)
169 break;
170 }
171 else {
172 return set_lookkey(so, key, hash);
173 }
174 }
175 if (entry->key == dummy && freeslot == NULL)
176 freeslot = entry;
177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 }
179 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000180}
181
182/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000183 * Hacked up version of set_lookkey which can assume keys are always unicode;
184 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000185 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000186 */
187static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200188set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000189{
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700190 size_t i, j; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200191 size_t perturb;
192 setentry *freeslot;
193 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200195 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 /* Make sure this function doesn't have to handle non-unicode keys,
198 including subclasses of str; e.g., one reason to subclass
199 strings is to override __eq__, and for speed we don't cater to
200 that here. */
201 if (!PyUnicode_CheckExact(key)) {
202 so->lookup = set_lookkey;
203 return set_lookkey(so, key, hash);
204 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700205
Mark Dickinson57e683e2011-09-24 18:18:40 +0100206 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 entry = &table[i];
208 if (entry->key == NULL || entry->key == key)
209 return entry;
210 if (entry->key == dummy)
211 freeslot = entry;
212 else {
213 if (entry->hash == hash && unicode_eq(entry->key, key))
214 return entry;
215 freeslot = NULL;
216 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000217
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700218 entry = &table[i ^ 1];
219 if (entry->key == NULL)
220 return freeslot == NULL ? entry : freeslot;
221 if (entry->key == key
222 || (entry->hash == hash
223 && entry->key != dummy
224 && unicode_eq(entry->key, key)))
225 return entry;
226 if (entry->key == dummy && freeslot == NULL)
227 freeslot = entry;
228
229 j = i;
230 perturb = hash;
231 while (1) {
232 j ^= 1;
233 entry = &table[j];
234 if (entry->key == NULL)
235 return freeslot == NULL ? entry : freeslot;
236 if (entry->key == key
237 || (entry->hash == hash
238 && entry->key != dummy
239 && unicode_eq(entry->key, key)))
240 return entry;
241 if (entry->key == dummy && freeslot == NULL)
242 freeslot = entry;
243
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700244 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700245 j = i & mask;
246 perturb >>= PERTURB_SHIFT;
247
248 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 if (entry->key == NULL)
250 return freeslot == NULL ? entry : freeslot;
251 if (entry->key == key
252 || (entry->hash == hash
253 && entry->key != dummy
254 && unicode_eq(entry->key, key)))
255 return entry;
256 if (entry->key == dummy && freeslot == NULL)
257 freeslot = entry;
258 }
259 assert(0); /* NOT REACHED */
260 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000261}
262
263/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000264Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000265Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266Eats a reference to key.
267*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000268static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200269set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200271 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 assert(so->lookup != NULL);
274 entry = so->lookup(so, key, hash);
275 if (entry == NULL)
276 return -1;
277 if (entry->key == NULL) {
278 /* UNUSED */
279 so->fill++;
280 entry->key = key;
281 entry->hash = hash;
282 so->used++;
283 } else if (entry->key == dummy) {
284 /* DUMMY */
285 entry->key = key;
286 entry->hash = hash;
287 so->used++;
288 Py_DECREF(dummy);
289 } else {
290 /* ACTIVE */
291 Py_DECREF(key);
292 }
293 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000294}
295
296/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000297Internal routine used by set_table_resize() to insert an item which is
298known to be absent from the set. This routine also assumes that
299the set contains no deleted entries. Besides the performance benefit,
300using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
301Note that no refcounts are changed by this routine; if needed, the caller
302is responsible for incref'ing `key`.
303*/
304static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200305set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200308 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700309 size_t perturb = hash;
310 size_t mask = (size_t)so->mask;
311 size_t i, j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000312
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700313 i = j = (size_t)hash & mask;
314 while (1) {
315 entry = &table[j];
316 if (entry->key == NULL)
317 break;
318 entry = &table[j ^ 1];
319 if (entry->key == NULL)
320 break;
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700321 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700322 j = i & mask;
323 perturb >>= PERTURB_SHIFT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 }
325 so->fill++;
326 entry->key = key;
327 entry->hash = hash;
328 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000329}
330
331/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000332Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000333keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334actually be smaller than the old one.
335*/
336static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000337set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 Py_ssize_t newsize;
340 setentry *oldtable, *newtable, *entry;
341 Py_ssize_t i;
342 int is_oldtable_malloced;
343 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700344 PyObject *dummy_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* Find the smallest table size > minused. */
349 for (newsize = PySet_MINSIZE;
350 newsize <= minused && newsize > 0;
351 newsize <<= 1)
352 ;
353 if (newsize <= 0) {
354 PyErr_NoMemory();
355 return -1;
356 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 /* Get space for a new table. */
359 oldtable = so->table;
360 assert(oldtable != NULL);
361 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (newsize == PySet_MINSIZE) {
364 /* A large table is shrinking, or we can't get any smaller. */
365 newtable = so->smalltable;
366 if (newtable == oldtable) {
367 if (so->fill == so->used) {
368 /* No dummies, so no point doing anything. */
369 return 0;
370 }
371 /* We're not going to resize it, but rebuild the
372 table anyway to purge old dummy entries.
373 Subtle: This is *necessary* if fill==size,
374 as set_lookkey needs at least one virgin slot to
375 terminate failing searches. If fill < size, it's
376 merely desirable, as dummies slow searches. */
377 assert(so->fill > so->used);
378 memcpy(small_copy, oldtable, sizeof(small_copy));
379 oldtable = small_copy;
380 }
381 }
382 else {
383 newtable = PyMem_NEW(setentry, newsize);
384 if (newtable == NULL) {
385 PyErr_NoMemory();
386 return -1;
387 }
388 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 /* Make the set empty, using the new table. */
391 assert(newtable != oldtable);
392 so->table = newtable;
393 so->mask = newsize - 1;
394 memset(newtable, 0, sizeof(setentry) * newsize);
395 so->used = 0;
396 i = so->fill;
397 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 /* Copy the data over; this is refcount-neutral for active entries;
400 dummy entries aren't copied over, of course */
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700401 dummy_entry = dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 for (entry = oldtable; i > 0; entry++) {
403 if (entry->key == NULL) {
404 /* UNUSED */
405 ;
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700406 } else if (entry->key == dummy_entry) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* DUMMY */
408 --i;
409 assert(entry->key == dummy);
410 Py_DECREF(entry->key);
411 } else {
412 /* ACTIVE */
413 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000414 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 }
416 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (is_oldtable_malloced)
419 PyMem_DEL(oldtable);
420 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421}
422
Raymond Hettingerc991db22005-08-11 07:58:45 +0000423/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
424
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000425static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200426set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000427{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200428 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000429 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100430 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 assert(so->fill <= so->mask); /* at least one empty slot */
433 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000434 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100435 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000436 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 return -1;
438 }
439 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
440 return 0;
441 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000442}
443
444static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200445set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000446{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200447 Py_hash_t hash;
448 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200451 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 hash = PyObject_Hash(key);
453 if (hash == -1)
454 return -1;
455 }
456 assert(so->fill <= so->mask); /* at least one empty slot */
457 n_used = so->used;
458 Py_INCREF(key);
459 if (set_insert_key(so, key, hash) == -1) {
460 Py_DECREF(key);
461 return -1;
462 }
463 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
464 return 0;
465 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000466}
467
468#define DISCARD_NOTFOUND 0
469#define DISCARD_FOUND 1
470
471static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000472set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200473{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000475
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000476 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (entry == NULL)
478 return -1;
479 if (entry->key == NULL || entry->key == dummy)
480 return DISCARD_NOTFOUND;
481 old_key = entry->key;
482 Py_INCREF(dummy);
483 entry->key = dummy;
484 so->used--;
485 Py_DECREF(old_key);
486 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000487}
488
489static int
490set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200492 Py_hash_t hash;
493 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200499 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 hash = PyObject_Hash(key);
501 if (hash == -1)
502 return -1;
503 }
504 entry = (so->lookup)(so, key, hash);
505 if (entry == NULL)
506 return -1;
507 if (entry->key == NULL || entry->key == dummy)
508 return DISCARD_NOTFOUND;
509 old_key = entry->key;
510 Py_INCREF(dummy);
511 entry->key = dummy;
512 so->used--;
513 Py_DECREF(old_key);
514 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515}
516
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000517static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000518set_clear_internal(PySetObject *so)
519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 setentry *entry, *table;
521 int table_is_malloced;
522 Py_ssize_t fill;
523 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000524#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 Py_ssize_t i, n;
526 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 n = so->mask + 1;
529 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000530#endif
531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 table = so->table;
533 assert(table != NULL);
534 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 /* This is delicate. During the process of clearing the set,
537 * decrefs can cause the set to mutate. To avoid fatal confusion
538 * (voice of experience), we have to make the set empty before
539 * clearing the slots, and never refer to anything via so->ref while
540 * clearing.
541 */
542 fill = so->fill;
543 if (table_is_malloced)
544 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 else if (fill > 0) {
547 /* It's a small table with something that needs to be cleared.
548 * Afraid the only safe way is to copy the set entries into
549 * another small table first.
550 */
551 memcpy(small_copy, table, sizeof(small_copy));
552 table = small_copy;
553 EMPTY_TO_MINSIZE(so);
554 }
555 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* Now we can finally clear things. If C had refcounts, we could
558 * assert that the refcount on table is 1 now, i.e. that this function
559 * has unique access to it, so decref side-effects can't alter it.
560 */
561 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000562#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 assert(i < n);
564 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000565#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 if (entry->key) {
567 --fill;
568 Py_DECREF(entry->key);
569 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000570#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 else
572 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000573#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (table_is_malloced)
577 PyMem_DEL(table);
578 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000579}
580
581/*
582 * Iterate over a set table. Use like so:
583 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000584 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000585 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000586 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000587 * while (set_next(yourset, &pos, &entry)) {
588 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000589 * }
590 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000591 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593 */
594static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000595set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_ssize_t i;
598 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200599 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 assert (PyAnySet_Check(so));
602 i = *pos_ptr;
603 assert(i >= 0);
604 table = so->table;
605 mask = so->mask;
606 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
607 i++;
608 *pos_ptr = i+1;
609 if (i > mask)
610 return 0;
611 assert(table[i].key != NULL);
612 *entry_ptr = &table[i];
613 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000614}
615
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000616static void
617set_dealloc(PySetObject *so)
618{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200619 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 Py_ssize_t fill = so->fill;
621 PyObject_GC_UnTrack(so);
622 Py_TRASHCAN_SAFE_BEGIN(so)
623 if (so->weakreflist != NULL)
624 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 for (entry = so->table; fill > 0; entry++) {
627 if (entry->key) {
628 --fill;
629 Py_DECREF(entry->key);
630 }
631 }
632 if (so->table != so->smalltable)
633 PyMem_DEL(so->table);
634 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
635 free_list[numfree++] = so;
636 else
637 Py_TYPE(so)->tp_free(so);
638 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000639}
640
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000641static PyObject *
642set_repr(PySetObject *so)
643{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200644 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 if (status != 0) {
648 if (status < 0)
649 return NULL;
650 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
651 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 /* shortcut for the empty set */
654 if (!so->used) {
655 Py_ReprLeave((PyObject*)so);
656 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
657 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 keys = PySequence_List((PyObject *)so);
660 if (keys == NULL)
661 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000662
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200663 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 listrepr = PyObject_Repr(keys);
665 Py_DECREF(keys);
666 if (listrepr == NULL)
667 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200668 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200670 if (tmp == NULL)
671 goto done;
672 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200673
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200674 if (Py_TYPE(so) != &PySet_Type)
675 result = PyUnicode_FromFormat("%s({%U})",
676 Py_TYPE(so)->tp_name,
677 listrepr);
678 else
679 result = PyUnicode_FromFormat("{%U}", listrepr);
680 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000681done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 Py_ReprLeave((PyObject*)so);
683 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000684}
685
Martin v. Löwis18e16552006-02-15 17:27:45 +0000686static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000687set_len(PyObject *so)
688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000690}
691
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000692static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000693set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000696 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100697 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200698 Py_ssize_t i;
699 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 assert (PyAnySet_Check(so));
702 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 other = (PySetObject*)otherset;
705 if (other == so || other->used == 0)
706 /* a.update(a) or a.update({}); nothing to do */
707 return 0;
708 /* Do one big resize at the start, rather than
709 * incrementally resizing as we insert new keys. Expect
710 * that there will be no (or few) overlapping keys.
711 */
712 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
713 if (set_table_resize(so, (so->used + other->used)*2) != 0)
714 return -1;
715 }
716 for (i = 0; i <= other->mask; i++) {
717 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000718 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100719 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000720 if (key != NULL &&
721 key != dummy) {
722 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100723 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000724 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 return -1;
726 }
727 }
728 }
729 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000730}
731
732static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000733set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000734{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000735 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200739 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 hash = PyObject_Hash(key);
741 if (hash == -1)
742 return -1;
743 }
744 entry = (so->lookup)(so, key, hash);
745 if (entry == NULL)
746 return -1;
747 key = entry->key;
748 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000749}
750
Raymond Hettingerc991db22005-08-11 07:58:45 +0000751static int
752set_contains_entry(PySetObject *so, setentry *entry)
753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 PyObject *key;
755 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000756
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000757 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 if (lu_entry == NULL)
759 return -1;
760 key = lu_entry->key;
761 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000762}
763
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000764static PyObject *
765set_pop(PySetObject *so)
766{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200767 Py_ssize_t i = 0;
768 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 assert (PyAnySet_Check(so));
772 if (so->used == 0) {
773 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
774 return NULL;
775 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 /* Set entry to "the first" unused or dummy set entry. We abuse
778 * the hash field of slot 0 to hold a search finger:
779 * If slot 0 has a value, use slot 0.
780 * Else slot 0 is being used to hold a search finger,
781 * and we use its hash value as the first index to look.
782 */
783 entry = &so->table[0];
784 if (entry->key == NULL || entry->key == dummy) {
785 i = entry->hash;
786 /* The hash field may be a real hash value, or it may be a
787 * legit search finger, or it may be a once-legit search
788 * finger that's out of bounds now because it wrapped around
789 * or the table shrunk -- simply make sure it's in bounds now.
790 */
791 if (i > so->mask || i < 1)
792 i = 1; /* skip slot 0 */
793 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
794 i++;
795 if (i > so->mask)
796 i = 1;
797 }
798 }
799 key = entry->key;
800 Py_INCREF(dummy);
801 entry->key = dummy;
802 so->used--;
803 so->table[0].hash = i + 1; /* next place to start */
804 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000805}
806
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000807PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
808Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000809
810static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000811set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 Py_ssize_t pos = 0;
814 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 while (set_next(so, &pos, &entry))
817 Py_VISIT(entry->key);
818 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000819}
820
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000821static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000822frozenset_hash(PyObject *self)
823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800825 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 setentry *entry;
827 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 if (so->hash != -1)
830 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000831
Mark Dickinson57e683e2011-09-24 18:18:40 +0100832 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 while (set_next(so, &pos, &entry)) {
834 /* Work to increase the bit dispersion for closely spaced hash
835 values. The is important because some use cases have many
836 combinations of a small number of elements with nearby
837 hashes so that many distinct combinations collapse to only
838 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000839 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800840 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800842 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800844 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 so->hash = hash;
846 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000847}
848
Raymond Hettingera9d99362005-08-05 00:01:15 +0000849/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000851typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 PyObject_HEAD
853 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
854 Py_ssize_t si_used;
855 Py_ssize_t si_pos;
856 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000857} setiterobject;
858
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000859static void
860setiter_dealloc(setiterobject *si)
861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 Py_XDECREF(si->si_set);
863 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000864}
865
866static int
867setiter_traverse(setiterobject *si, visitproc visit, void *arg)
868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 Py_VISIT(si->si_set);
870 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000871}
872
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000873static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000874setiter_len(setiterobject *si)
875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 Py_ssize_t len = 0;
877 if (si->si_set != NULL && si->si_used == si->si_set->used)
878 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000879 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880}
881
Armin Rigof5b3e362006-02-11 21:32:43 +0000882PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000883
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000884static PyObject *setiter_iternext(setiterobject *si);
885
886static PyObject *
887setiter_reduce(setiterobject *si)
888{
889 PyObject *list;
890 setiterobject tmp;
891
892 list = PyList_New(0);
893 if (!list)
894 return NULL;
895
Ezio Melotti0e1af282012-09-28 16:43:40 +0300896 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000897 tmp = *si;
898 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300899
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000900 /* iterate the temporary into a list */
901 for(;;) {
902 PyObject *element = setiter_iternext(&tmp);
903 if (element) {
904 if (PyList_Append(list, element)) {
905 Py_DECREF(element);
906 Py_DECREF(list);
907 Py_XDECREF(tmp.si_set);
908 return NULL;
909 }
910 Py_DECREF(element);
911 } else
912 break;
913 }
914 Py_XDECREF(tmp.si_set);
915 /* check for error */
916 if (tmp.si_set != NULL) {
917 /* we have an error */
918 Py_DECREF(list);
919 return NULL;
920 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200921 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000922}
923
924PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
925
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000926static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000928 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000930};
931
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000932static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200935 Py_ssize_t i, mask;
936 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 if (so == NULL)
940 return NULL;
941 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 if (si->si_used != so->used) {
944 PyErr_SetString(PyExc_RuntimeError,
945 "Set changed size during iteration");
946 si->si_used = -1; /* Make this state sticky */
947 return NULL;
948 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 i = si->si_pos;
951 assert(i>=0);
952 entry = so->table;
953 mask = so->mask;
954 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
955 i++;
956 si->si_pos = i+1;
957 if (i > mask)
958 goto fail;
959 si->len--;
960 key = entry[i].key;
961 Py_INCREF(key);
962 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000963
964fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 Py_DECREF(so);
966 si->si_set = NULL;
967 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000968}
969
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000970PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyVarObject_HEAD_INIT(&PyType_Type, 0)
972 "set_iterator", /* tp_name */
973 sizeof(setiterobject), /* tp_basicsize */
974 0, /* tp_itemsize */
975 /* methods */
976 (destructor)setiter_dealloc, /* tp_dealloc */
977 0, /* tp_print */
978 0, /* tp_getattr */
979 0, /* tp_setattr */
980 0, /* tp_reserved */
981 0, /* tp_repr */
982 0, /* tp_as_number */
983 0, /* tp_as_sequence */
984 0, /* tp_as_mapping */
985 0, /* tp_hash */
986 0, /* tp_call */
987 0, /* tp_str */
988 PyObject_GenericGetAttr, /* tp_getattro */
989 0, /* tp_setattro */
990 0, /* tp_as_buffer */
991 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
992 0, /* tp_doc */
993 (traverseproc)setiter_traverse, /* tp_traverse */
994 0, /* tp_clear */
995 0, /* tp_richcompare */
996 0, /* tp_weaklistoffset */
997 PyObject_SelfIter, /* tp_iter */
998 (iternextfunc)setiter_iternext, /* tp_iternext */
999 setiter_methods, /* tp_methods */
1000 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001001};
1002
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001003static PyObject *
1004set_iter(PySetObject *so)
1005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
1007 if (si == NULL)
1008 return NULL;
1009 Py_INCREF(so);
1010 si->si_set = so;
1011 si->si_used = so->used;
1012 si->si_pos = 0;
1013 si->len = so->used;
1014 _PyObject_GC_TRACK(si);
1015 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001016}
1017
Raymond Hettingerd7946662005-08-01 21:39:29 +00001018static int
Raymond Hettingerd7946662005-08-01 21:39:29 +00001019set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +00001020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 if (PyAnySet_Check(other))
1024 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (PyDict_CheckExact(other)) {
1027 PyObject *value;
1028 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001029 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 /* Do one big resize at the start, rather than
1033 * incrementally resizing as we insert new keys. Expect
1034 * that there will be no (or few) overlapping keys.
1035 */
1036 if (dictsize == -1)
1037 return -1;
1038 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
1039 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
1040 return -1;
1041 }
1042 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1043 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 an_entry.hash = hash;
1046 an_entry.key = key;
1047 if (set_add_entry(so, &an_entry) == -1)
1048 return -1;
1049 }
1050 return 0;
1051 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 it = PyObject_GetIter(other);
1054 if (it == NULL)
1055 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 while ((key = PyIter_Next(it)) != NULL) {
1058 if (set_add_key(so, key) == -1) {
1059 Py_DECREF(it);
1060 Py_DECREF(key);
1061 return -1;
1062 }
1063 Py_DECREF(key);
1064 }
1065 Py_DECREF(it);
1066 if (PyErr_Occurred())
1067 return -1;
1068 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001069}
1070
1071static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001072set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1077 PyObject *other = PyTuple_GET_ITEM(args, i);
1078 if (set_update_internal(so, other) == -1)
1079 return NULL;
1080 }
1081 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001082}
1083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001085"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001086
1087static PyObject *
1088make_new_set(PyTypeObject *type, PyObject *iterable)
1089{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001090 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 if (dummy == NULL) { /* Auto-initialize dummy */
Raymond Hettingerae9e6162013-08-20 22:28:24 -07001093 dummy = PyUnicode_FromString("<dummy key>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (dummy == NULL)
1095 return NULL;
1096 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 /* create PySetObject structure */
1099 if (numfree &&
1100 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1101 so = free_list[--numfree];
1102 assert (so != NULL && PyAnySet_CheckExact(so));
1103 Py_TYPE(so) = type;
1104 _Py_NewReference((PyObject *)so);
1105 EMPTY_TO_MINSIZE(so);
1106 PyObject_GC_Track(so);
1107 } else {
1108 so = (PySetObject *)type->tp_alloc(type, 0);
1109 if (so == NULL)
1110 return NULL;
1111 /* tp_alloc has already zeroed the structure */
1112 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1113 INIT_NONZERO_SET_SLOTS(so);
1114 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 so->lookup = set_lookkey_unicode;
1117 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 if (iterable != NULL) {
1120 if (set_update_internal(so, iterable) == -1) {
1121 Py_DECREF(so);
1122 return NULL;
1123 }
1124 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001127}
1128
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001129static PyObject *
1130make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1133 if (PyType_IsSubtype(type, &PySet_Type))
1134 type = &PySet_Type;
1135 else
1136 type = &PyFrozenSet_Type;
1137 }
1138 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001139}
1140
Raymond Hettingerd7946662005-08-01 21:39:29 +00001141/* The empty frozenset is a singleton */
1142static PyObject *emptyfrozenset = NULL;
1143
Raymond Hettingera690a992003-11-16 16:17:49 +00001144static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001145frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1150 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1153 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (type != &PyFrozenSet_Type)
1156 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (iterable != NULL) {
1159 /* frozenset(f) is idempotent */
1160 if (PyFrozenSet_CheckExact(iterable)) {
1161 Py_INCREF(iterable);
1162 return iterable;
1163 }
1164 result = make_new_set(type, iterable);
1165 if (result == NULL || PySet_GET_SIZE(result))
1166 return result;
1167 Py_DECREF(result);
1168 }
1169 /* The empty frozenset is a singleton */
1170 if (emptyfrozenset == NULL)
1171 emptyfrozenset = make_new_set(type, NULL);
1172 Py_XINCREF(emptyfrozenset);
1173 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001174}
1175
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001176int
1177PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001178{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001179 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 while (numfree) {
1183 numfree--;
1184 so = free_list[numfree];
1185 PyObject_GC_Del(so);
1186 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001187 return freelist_size;
1188}
1189
1190void
1191PySet_Fini(void)
1192{
1193 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 Py_CLEAR(dummy);
1195 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001196}
1197
David Malcolm49526f42012-06-22 14:55:41 -04001198/* Print summary info about the state of the optimized allocator */
1199void
1200_PySet_DebugMallocStats(FILE *out)
1201{
1202 _PyDebugAllocatorStats(out,
1203 "free PySetObject",
1204 numfree, sizeof(PySetObject));
1205}
1206
1207
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001208static PyObject *
1209set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1212 return NULL;
1213
1214 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001215}
1216
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001217/* set_swap_bodies() switches the contents of any two sets by moving their
1218 internal data pointers and, if needed, copying the internal smalltables.
1219 Semantically equivalent to:
1220
1221 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1222
1223 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001225 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001227 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001228*/
1229
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001230static void
1231set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 Py_ssize_t t;
1234 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001235 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001237 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 t = a->fill; a->fill = b->fill; b->fill = t;
1240 t = a->used; a->used = b->used; b->used = t;
1241 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 u = a->table;
1244 if (a->table == a->smalltable)
1245 u = b->smalltable;
1246 a->table = b->table;
1247 if (b->table == b->smalltable)
1248 a->table = a->smalltable;
1249 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (a->table == a->smalltable || b->table == b->smalltable) {
1254 memcpy(tab, a->smalltable, sizeof(tab));
1255 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1256 memcpy(b->smalltable, tab, sizeof(tab));
1257 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1260 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1261 h = a->hash; a->hash = b->hash; b->hash = h;
1262 } else {
1263 a->hash = -1;
1264 b->hash = -1;
1265 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001266}
1267
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001268static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001269set_copy(PySetObject *so)
1270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001272}
1273
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001274static PyObject *
1275frozenset_copy(PySetObject *so)
1276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (PyFrozenSet_CheckExact(so)) {
1278 Py_INCREF(so);
1279 return (PyObject *)so;
1280 }
1281 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001282}
1283
Raymond Hettingera690a992003-11-16 16:17:49 +00001284PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1285
1286static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001287set_clear(PySetObject *so)
1288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 set_clear_internal(so);
1290 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001291}
1292
1293PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1294
1295static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001296set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 PySetObject *result;
1299 PyObject *other;
1300 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 result = (PySetObject *)set_copy(so);
1303 if (result == NULL)
1304 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1307 other = PyTuple_GET_ITEM(args, i);
1308 if ((PyObject *)so == other)
1309 continue;
1310 if (set_update_internal(result, other) == -1) {
1311 Py_DECREF(result);
1312 return NULL;
1313 }
1314 }
1315 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001316}
1317
1318PyDoc_STRVAR(union_doc,
1319 "Return the union of sets as a new set.\n\
1320\n\
1321(i.e. all elements that are in either set.)");
1322
1323static PyObject *
1324set_or(PySetObject *so, PyObject *other)
1325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001327
Brian Curtindfc80e32011-08-10 20:28:54 -05001328 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1329 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 result = (PySetObject *)set_copy(so);
1332 if (result == NULL)
1333 return NULL;
1334 if ((PyObject *)so == other)
1335 return (PyObject *)result;
1336 if (set_update_internal(result, other) == -1) {
1337 Py_DECREF(result);
1338 return NULL;
1339 }
1340 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001341}
1342
Raymond Hettingera690a992003-11-16 16:17:49 +00001343static PyObject *
1344set_ior(PySetObject *so, PyObject *other)
1345{
Brian Curtindfc80e32011-08-10 20:28:54 -05001346 if (!PyAnySet_Check(other))
1347 Py_RETURN_NOTIMPLEMENTED;
1348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 if (set_update_internal(so, other) == -1)
1350 return NULL;
1351 Py_INCREF(so);
1352 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001353}
1354
1355static PyObject *
1356set_intersection(PySetObject *so, PyObject *other)
1357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 PySetObject *result;
1359 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 if ((PyObject *)so == other)
1362 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1365 if (result == NULL)
1366 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 if (PyAnySet_Check(other)) {
1369 Py_ssize_t pos = 0;
1370 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1373 tmp = (PyObject *)so;
1374 so = (PySetObject *)other;
1375 other = tmp;
1376 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 while (set_next((PySetObject *)other, &pos, &entry)) {
1379 int rv = set_contains_entry(so, entry);
1380 if (rv == -1) {
1381 Py_DECREF(result);
1382 return NULL;
1383 }
1384 if (rv) {
1385 if (set_add_entry(result, entry) == -1) {
1386 Py_DECREF(result);
1387 return NULL;
1388 }
1389 }
1390 }
1391 return (PyObject *)result;
1392 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 it = PyObject_GetIter(other);
1395 if (it == NULL) {
1396 Py_DECREF(result);
1397 return NULL;
1398 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 while ((key = PyIter_Next(it)) != NULL) {
1401 int rv;
1402 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001403 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 if (hash == -1) {
1406 Py_DECREF(it);
1407 Py_DECREF(result);
1408 Py_DECREF(key);
1409 return NULL;
1410 }
1411 entry.hash = hash;
1412 entry.key = key;
1413 rv = set_contains_entry(so, &entry);
1414 if (rv == -1) {
1415 Py_DECREF(it);
1416 Py_DECREF(result);
1417 Py_DECREF(key);
1418 return NULL;
1419 }
1420 if (rv) {
1421 if (set_add_entry(result, &entry) == -1) {
1422 Py_DECREF(it);
1423 Py_DECREF(result);
1424 Py_DECREF(key);
1425 return NULL;
1426 }
1427 }
1428 Py_DECREF(key);
1429 }
1430 Py_DECREF(it);
1431 if (PyErr_Occurred()) {
1432 Py_DECREF(result);
1433 return NULL;
1434 }
1435 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001436}
1437
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001438static PyObject *
1439set_intersection_multi(PySetObject *so, PyObject *args)
1440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 Py_ssize_t i;
1442 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 if (PyTuple_GET_SIZE(args) == 0)
1445 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 Py_INCREF(so);
1448 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1449 PyObject *other = PyTuple_GET_ITEM(args, i);
1450 PyObject *newresult = set_intersection((PySetObject *)result, other);
1451 if (newresult == NULL) {
1452 Py_DECREF(result);
1453 return NULL;
1454 }
1455 Py_DECREF(result);
1456 result = newresult;
1457 }
1458 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001459}
1460
Raymond Hettingera690a992003-11-16 16:17:49 +00001461PyDoc_STRVAR(intersection_doc,
1462"Return the intersection of two sets as a new set.\n\
1463\n\
1464(i.e. all elements that are in both sets.)");
1465
1466static PyObject *
1467set_intersection_update(PySetObject *so, PyObject *other)
1468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 tmp = set_intersection(so, other);
1472 if (tmp == NULL)
1473 return NULL;
1474 set_swap_bodies(so, (PySetObject *)tmp);
1475 Py_DECREF(tmp);
1476 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001477}
1478
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001479static PyObject *
1480set_intersection_update_multi(PySetObject *so, PyObject *args)
1481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 tmp = set_intersection_multi(so, args);
1485 if (tmp == NULL)
1486 return NULL;
1487 set_swap_bodies(so, (PySetObject *)tmp);
1488 Py_DECREF(tmp);
1489 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001490}
1491
Raymond Hettingera690a992003-11-16 16:17:49 +00001492PyDoc_STRVAR(intersection_update_doc,
1493"Update a set with the intersection of itself and another.");
1494
1495static PyObject *
1496set_and(PySetObject *so, PyObject *other)
1497{
Brian Curtindfc80e32011-08-10 20:28:54 -05001498 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1499 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001501}
1502
1503static PyObject *
1504set_iand(PySetObject *so, PyObject *other)
1505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001507
Brian Curtindfc80e32011-08-10 20:28:54 -05001508 if (!PyAnySet_Check(other))
1509 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 result = set_intersection_update(so, other);
1511 if (result == NULL)
1512 return NULL;
1513 Py_DECREF(result);
1514 Py_INCREF(so);
1515 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001516}
1517
Guido van Rossum58da9312007-11-10 23:39:45 +00001518static PyObject *
1519set_isdisjoint(PySetObject *so, PyObject *other)
1520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if ((PyObject *)so == other) {
1524 if (PySet_GET_SIZE(so) == 0)
1525 Py_RETURN_TRUE;
1526 else
1527 Py_RETURN_FALSE;
1528 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 if (PyAnySet_CheckExact(other)) {
1531 Py_ssize_t pos = 0;
1532 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1535 tmp = (PyObject *)so;
1536 so = (PySetObject *)other;
1537 other = tmp;
1538 }
1539 while (set_next((PySetObject *)other, &pos, &entry)) {
1540 int rv = set_contains_entry(so, entry);
1541 if (rv == -1)
1542 return NULL;
1543 if (rv)
1544 Py_RETURN_FALSE;
1545 }
1546 Py_RETURN_TRUE;
1547 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 it = PyObject_GetIter(other);
1550 if (it == NULL)
1551 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 while ((key = PyIter_Next(it)) != NULL) {
1554 int rv;
1555 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001556 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 if (hash == -1) {
1559 Py_DECREF(key);
1560 Py_DECREF(it);
1561 return NULL;
1562 }
1563 entry.hash = hash;
1564 entry.key = key;
1565 rv = set_contains_entry(so, &entry);
1566 Py_DECREF(key);
1567 if (rv == -1) {
1568 Py_DECREF(it);
1569 return NULL;
1570 }
1571 if (rv) {
1572 Py_DECREF(it);
1573 Py_RETURN_FALSE;
1574 }
1575 }
1576 Py_DECREF(it);
1577 if (PyErr_Occurred())
1578 return NULL;
1579 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001580}
1581
1582PyDoc_STRVAR(isdisjoint_doc,
1583"Return True if two sets have a null intersection.");
1584
Neal Norwitz6576bd82005-11-13 18:41:28 +00001585static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001586set_difference_update_internal(PySetObject *so, PyObject *other)
1587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 if ((PyObject *)so == other)
1589 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 if (PyAnySet_Check(other)) {
1592 setentry *entry;
1593 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 while (set_next((PySetObject *)other, &pos, &entry))
1596 if (set_discard_entry(so, entry) == -1)
1597 return -1;
1598 } else {
1599 PyObject *key, *it;
1600 it = PyObject_GetIter(other);
1601 if (it == NULL)
1602 return -1;
1603
1604 while ((key = PyIter_Next(it)) != NULL) {
1605 if (set_discard_key(so, key) == -1) {
1606 Py_DECREF(it);
1607 Py_DECREF(key);
1608 return -1;
1609 }
1610 Py_DECREF(key);
1611 }
1612 Py_DECREF(it);
1613 if (PyErr_Occurred())
1614 return -1;
1615 }
1616 /* If more than 1/5 are dummies, then resize them away. */
1617 if ((so->fill - so->used) * 5 < so->mask)
1618 return 0;
1619 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001620}
1621
Raymond Hettingera690a992003-11-16 16:17:49 +00001622static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001623set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1628 PyObject *other = PyTuple_GET_ITEM(args, i);
1629 if (set_difference_update_internal(so, other) == -1)
1630 return NULL;
1631 }
1632 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001633}
1634
1635PyDoc_STRVAR(difference_update_doc,
1636"Remove all elements of another set from this set.");
1637
1638static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001639set_copy_and_difference(PySetObject *so, PyObject *other)
1640{
1641 PyObject *result;
1642
1643 result = set_copy(so);
1644 if (result == NULL)
1645 return NULL;
1646 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1647 return result;
1648 Py_DECREF(result);
1649 return NULL;
1650}
1651
1652static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001653set_difference(PySetObject *so, PyObject *other)
1654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 PyObject *result;
1656 setentry *entry;
1657 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001660 return set_copy_and_difference(so, other);
1661 }
1662
1663 /* If len(so) much more than len(other), it's more efficient to simply copy
1664 * so and then iterate other looking for common elements. */
1665 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1666 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 result = make_new_set_basetype(Py_TYPE(so), NULL);
1670 if (result == NULL)
1671 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 if (PyDict_CheckExact(other)) {
1674 while (set_next(so, &pos, &entry)) {
1675 setentry entrycopy;
1676 entrycopy.hash = entry->hash;
1677 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001678 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1680 Py_DECREF(result);
1681 return NULL;
1682 }
1683 }
1684 }
1685 return result;
1686 }
1687
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001688 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 while (set_next(so, &pos, &entry)) {
1690 int rv = set_contains_entry((PySetObject *)other, entry);
1691 if (rv == -1) {
1692 Py_DECREF(result);
1693 return NULL;
1694 }
1695 if (!rv) {
1696 if (set_add_entry((PySetObject *)result, entry) == -1) {
1697 Py_DECREF(result);
1698 return NULL;
1699 }
1700 }
1701 }
1702 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001703}
1704
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001705static PyObject *
1706set_difference_multi(PySetObject *so, PyObject *args)
1707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 Py_ssize_t i;
1709 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 if (PyTuple_GET_SIZE(args) == 0)
1712 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 other = PyTuple_GET_ITEM(args, 0);
1715 result = set_difference(so, other);
1716 if (result == NULL)
1717 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1720 other = PyTuple_GET_ITEM(args, i);
1721 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1722 Py_DECREF(result);
1723 return NULL;
1724 }
1725 }
1726 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001727}
1728
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001729PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001730"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001731\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001732(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001733static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001734set_sub(PySetObject *so, PyObject *other)
1735{
Brian Curtindfc80e32011-08-10 20:28:54 -05001736 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1737 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001739}
1740
1741static PyObject *
1742set_isub(PySetObject *so, PyObject *other)
1743{
Brian Curtindfc80e32011-08-10 20:28:54 -05001744 if (!PyAnySet_Check(other))
1745 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 if (set_difference_update_internal(so, other) == -1)
1747 return NULL;
1748 Py_INCREF(so);
1749 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001750}
1751
1752static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001753set_symmetric_difference_update(PySetObject *so, PyObject *other)
1754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 PySetObject *otherset;
1756 PyObject *key;
1757 Py_ssize_t pos = 0;
1758 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 if ((PyObject *)so == other)
1761 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 if (PyDict_CheckExact(other)) {
1764 PyObject *value;
1765 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001766 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1768 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001769
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001770 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 an_entry.hash = hash;
1772 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001775 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001776 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001779 if (rv == DISCARD_NOTFOUND) {
1780 if (set_add_entry(so, &an_entry) == -1) {
1781 Py_DECREF(key);
1782 return NULL;
1783 }
1784 }
Georg Brandl2d444492010-09-03 10:52:55 +00001785 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 }
1787 Py_RETURN_NONE;
1788 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 if (PyAnySet_Check(other)) {
1791 Py_INCREF(other);
1792 otherset = (PySetObject *)other;
1793 } else {
1794 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1795 if (otherset == NULL)
1796 return NULL;
1797 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 while (set_next(otherset, &pos, &entry)) {
1800 int rv = set_discard_entry(so, entry);
1801 if (rv == -1) {
1802 Py_DECREF(otherset);
1803 return NULL;
1804 }
1805 if (rv == DISCARD_NOTFOUND) {
1806 if (set_add_entry(so, entry) == -1) {
1807 Py_DECREF(otherset);
1808 return NULL;
1809 }
1810 }
1811 }
1812 Py_DECREF(otherset);
1813 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001814}
1815
1816PyDoc_STRVAR(symmetric_difference_update_doc,
1817"Update a set with the symmetric difference of itself and another.");
1818
1819static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001820set_symmetric_difference(PySetObject *so, PyObject *other)
1821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyObject *rv;
1823 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1826 if (otherset == NULL)
1827 return NULL;
1828 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1829 if (rv == NULL)
1830 return NULL;
1831 Py_DECREF(rv);
1832 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001833}
1834
1835PyDoc_STRVAR(symmetric_difference_doc,
1836"Return the symmetric difference of two sets as a new set.\n\
1837\n\
1838(i.e. all elements that are in exactly one of the sets.)");
1839
1840static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001841set_xor(PySetObject *so, PyObject *other)
1842{
Brian Curtindfc80e32011-08-10 20:28:54 -05001843 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1844 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001846}
1847
1848static PyObject *
1849set_ixor(PySetObject *so, PyObject *other)
1850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001852
Brian Curtindfc80e32011-08-10 20:28:54 -05001853 if (!PyAnySet_Check(other))
1854 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 result = set_symmetric_difference_update(so, other);
1856 if (result == NULL)
1857 return NULL;
1858 Py_DECREF(result);
1859 Py_INCREF(so);
1860 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001861}
1862
1863static PyObject *
1864set_issubset(PySetObject *so, PyObject *other)
1865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 setentry *entry;
1867 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 if (!PyAnySet_Check(other)) {
1870 PyObject *tmp, *result;
1871 tmp = make_new_set(&PySet_Type, other);
1872 if (tmp == NULL)
1873 return NULL;
1874 result = set_issubset(so, tmp);
1875 Py_DECREF(tmp);
1876 return result;
1877 }
1878 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1879 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 while (set_next(so, &pos, &entry)) {
1882 int rv = set_contains_entry((PySetObject *)other, entry);
1883 if (rv == -1)
1884 return NULL;
1885 if (!rv)
1886 Py_RETURN_FALSE;
1887 }
1888 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001889}
1890
1891PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1892
1893static PyObject *
1894set_issuperset(PySetObject *so, PyObject *other)
1895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 if (!PyAnySet_Check(other)) {
1899 tmp = make_new_set(&PySet_Type, other);
1900 if (tmp == NULL)
1901 return NULL;
1902 result = set_issuperset(so, tmp);
1903 Py_DECREF(tmp);
1904 return result;
1905 }
1906 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001907}
1908
1909PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1910
Raymond Hettingera690a992003-11-16 16:17:49 +00001911static PyObject *
1912set_richcompare(PySetObject *v, PyObject *w, int op)
1913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001915
Brian Curtindfc80e32011-08-10 20:28:54 -05001916 if(!PyAnySet_Check(w))
1917 Py_RETURN_NOTIMPLEMENTED;
1918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 switch (op) {
1920 case Py_EQ:
1921 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1922 Py_RETURN_FALSE;
1923 if (v->hash != -1 &&
1924 ((PySetObject *)w)->hash != -1 &&
1925 v->hash != ((PySetObject *)w)->hash)
1926 Py_RETURN_FALSE;
1927 return set_issubset(v, w);
1928 case Py_NE:
1929 r1 = set_richcompare(v, w, Py_EQ);
1930 if (r1 == NULL)
1931 return NULL;
1932 r2 = PyBool_FromLong(PyObject_Not(r1));
1933 Py_DECREF(r1);
1934 return r2;
1935 case Py_LE:
1936 return set_issubset(v, w);
1937 case Py_GE:
1938 return set_issuperset(v, w);
1939 case Py_LT:
1940 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1941 Py_RETURN_FALSE;
1942 return set_issubset(v, w);
1943 case Py_GT:
1944 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1945 Py_RETURN_FALSE;
1946 return set_issuperset(v, w);
1947 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001948 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001949}
1950
Raymond Hettingera690a992003-11-16 16:17:49 +00001951static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001952set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 if (set_add_key(so, key) == -1)
1955 return NULL;
1956 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001957}
1958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001960"Add an element to a set.\n\
1961\n\
1962This has no effect if the element is already present.");
1963
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001964static int
1965set_contains(PySetObject *so, PyObject *key)
1966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 PyObject *tmpkey;
1968 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 rv = set_contains_key(so, key);
1971 if (rv == -1) {
1972 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1973 return -1;
1974 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001975 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 if (tmpkey == NULL)
1977 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001978 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 Py_DECREF(tmpkey);
1980 }
1981 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001982}
1983
1984static PyObject *
1985set_direct_contains(PySetObject *so, PyObject *key)
1986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 result = set_contains(so, key);
1990 if (result == -1)
1991 return NULL;
1992 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001993}
1994
1995PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1996
Raymond Hettingera690a992003-11-16 16:17:49 +00001997static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001998set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 PyObject *tmpkey;
2001 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 rv = set_discard_key(so, key);
2004 if (rv == -1) {
2005 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
2006 return NULL;
2007 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00002008 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 if (tmpkey == NULL)
2010 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 Py_DECREF(tmpkey);
2013 if (rv == -1)
2014 return NULL;
2015 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 if (rv == DISCARD_NOTFOUND) {
2018 set_key_error(key);
2019 return NULL;
2020 }
2021 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00002022}
2023
2024PyDoc_STRVAR(remove_doc,
2025"Remove an element from a set; it must be a member.\n\
2026\n\
2027If the element is not a member, raise a KeyError.");
2028
2029static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00002030set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00002031{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04002032 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00002034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 rv = set_discard_key(so, key);
2036 if (rv == -1) {
2037 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
2038 return NULL;
2039 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00002040 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 if (tmpkey == NULL)
2042 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002043 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002045 if (rv == -1)
2046 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 }
2048 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00002049}
2050
2051PyDoc_STRVAR(discard_doc,
2052"Remove an element from a set if it is a member.\n\
2053\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002055
2056static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00002057set_reduce(PySetObject *so)
2058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002060 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00002061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 keys = PySequence_List((PyObject *)so);
2063 if (keys == NULL)
2064 goto done;
2065 args = PyTuple_Pack(1, keys);
2066 if (args == NULL)
2067 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002068 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 if (dict == NULL) {
2070 PyErr_Clear();
2071 dict = Py_None;
2072 Py_INCREF(dict);
2073 }
2074 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002075done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 Py_XDECREF(args);
2077 Py_XDECREF(keys);
2078 Py_XDECREF(dict);
2079 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002080}
2081
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002082static PyObject *
2083set_sizeof(PySetObject *so)
2084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 res = sizeof(PySetObject);
2088 if (so->table != so->smalltable)
2089 res = res + (so->mask + 1) * sizeof(setentry);
2090 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002091}
2092
2093PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002094static int
2095set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 if (!PyAnySet_Check(self))
2100 return -1;
2101 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2102 return -1;
2103 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2104 return -1;
2105 set_clear_internal(self);
2106 self->hash = -1;
2107 if (iterable == NULL)
2108 return 0;
2109 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002110}
2111
Raymond Hettingera690a992003-11-16 16:17:49 +00002112static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 set_len, /* sq_length */
2114 0, /* sq_concat */
2115 0, /* sq_repeat */
2116 0, /* sq_item */
2117 0, /* sq_slice */
2118 0, /* sq_ass_item */
2119 0, /* sq_ass_slice */
2120 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002121};
2122
2123/* set object ********************************************************/
2124
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002125#ifdef Py_DEBUG
2126static PyObject *test_c_api(PySetObject *so);
2127
2128PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2129All is well if assertions don't fail.");
2130#endif
2131
Raymond Hettingera690a992003-11-16 16:17:49 +00002132static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 {"add", (PyCFunction)set_add, METH_O,
2134 add_doc},
2135 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2136 clear_doc},
2137 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2138 contains_doc},
2139 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2140 copy_doc},
2141 {"discard", (PyCFunction)set_discard, METH_O,
2142 discard_doc},
2143 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2144 difference_doc},
2145 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2146 difference_update_doc},
2147 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2148 intersection_doc},
2149 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2150 intersection_update_doc},
2151 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2152 isdisjoint_doc},
2153 {"issubset", (PyCFunction)set_issubset, METH_O,
2154 issubset_doc},
2155 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2156 issuperset_doc},
2157 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2158 pop_doc},
2159 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2160 reduce_doc},
2161 {"remove", (PyCFunction)set_remove, METH_O,
2162 remove_doc},
2163 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2164 sizeof_doc},
2165 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2166 symmetric_difference_doc},
2167 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2168 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002169#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2171 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002172#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 {"union", (PyCFunction)set_union, METH_VARARGS,
2174 union_doc},
2175 {"update", (PyCFunction)set_update, METH_VARARGS,
2176 update_doc},
2177 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002178};
2179
2180static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 0, /*nb_add*/
2182 (binaryfunc)set_sub, /*nb_subtract*/
2183 0, /*nb_multiply*/
2184 0, /*nb_remainder*/
2185 0, /*nb_divmod*/
2186 0, /*nb_power*/
2187 0, /*nb_negative*/
2188 0, /*nb_positive*/
2189 0, /*nb_absolute*/
2190 0, /*nb_bool*/
2191 0, /*nb_invert*/
2192 0, /*nb_lshift*/
2193 0, /*nb_rshift*/
2194 (binaryfunc)set_and, /*nb_and*/
2195 (binaryfunc)set_xor, /*nb_xor*/
2196 (binaryfunc)set_or, /*nb_or*/
2197 0, /*nb_int*/
2198 0, /*nb_reserved*/
2199 0, /*nb_float*/
2200 0, /*nb_inplace_add*/
2201 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2202 0, /*nb_inplace_multiply*/
2203 0, /*nb_inplace_remainder*/
2204 0, /*nb_inplace_power*/
2205 0, /*nb_inplace_lshift*/
2206 0, /*nb_inplace_rshift*/
2207 (binaryfunc)set_iand, /*nb_inplace_and*/
2208 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2209 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002210};
2211
2212PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002213"set() -> new empty set object\n\
2214set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002215\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002216Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002217
2218PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2220 "set", /* tp_name */
2221 sizeof(PySetObject), /* tp_basicsize */
2222 0, /* tp_itemsize */
2223 /* methods */
2224 (destructor)set_dealloc, /* tp_dealloc */
2225 0, /* tp_print */
2226 0, /* tp_getattr */
2227 0, /* tp_setattr */
2228 0, /* tp_reserved */
2229 (reprfunc)set_repr, /* tp_repr */
2230 &set_as_number, /* tp_as_number */
2231 &set_as_sequence, /* tp_as_sequence */
2232 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002233 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 0, /* tp_call */
2235 0, /* tp_str */
2236 PyObject_GenericGetAttr, /* tp_getattro */
2237 0, /* tp_setattro */
2238 0, /* tp_as_buffer */
2239 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2240 Py_TPFLAGS_BASETYPE, /* tp_flags */
2241 set_doc, /* tp_doc */
2242 (traverseproc)set_traverse, /* tp_traverse */
2243 (inquiry)set_clear_internal, /* tp_clear */
2244 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002245 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2246 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 0, /* tp_iternext */
2248 set_methods, /* tp_methods */
2249 0, /* tp_members */
2250 0, /* tp_getset */
2251 0, /* tp_base */
2252 0, /* tp_dict */
2253 0, /* tp_descr_get */
2254 0, /* tp_descr_set */
2255 0, /* tp_dictoffset */
2256 (initproc)set_init, /* tp_init */
2257 PyType_GenericAlloc, /* tp_alloc */
2258 set_new, /* tp_new */
2259 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002260};
2261
2262/* frozenset object ********************************************************/
2263
2264
2265static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2267 contains_doc},
2268 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2269 copy_doc},
2270 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2271 difference_doc},
2272 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2273 intersection_doc},
2274 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2275 isdisjoint_doc},
2276 {"issubset", (PyCFunction)set_issubset, METH_O,
2277 issubset_doc},
2278 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2279 issuperset_doc},
2280 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2281 reduce_doc},
2282 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2283 sizeof_doc},
2284 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2285 symmetric_difference_doc},
2286 {"union", (PyCFunction)set_union, METH_VARARGS,
2287 union_doc},
2288 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002289};
2290
2291static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 0, /*nb_add*/
2293 (binaryfunc)set_sub, /*nb_subtract*/
2294 0, /*nb_multiply*/
2295 0, /*nb_remainder*/
2296 0, /*nb_divmod*/
2297 0, /*nb_power*/
2298 0, /*nb_negative*/
2299 0, /*nb_positive*/
2300 0, /*nb_absolute*/
2301 0, /*nb_bool*/
2302 0, /*nb_invert*/
2303 0, /*nb_lshift*/
2304 0, /*nb_rshift*/
2305 (binaryfunc)set_and, /*nb_and*/
2306 (binaryfunc)set_xor, /*nb_xor*/
2307 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002308};
2309
2310PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002311"frozenset() -> empty frozenset object\n\
2312frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002313\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002314Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002315
2316PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2318 "frozenset", /* tp_name */
2319 sizeof(PySetObject), /* tp_basicsize */
2320 0, /* tp_itemsize */
2321 /* methods */
2322 (destructor)set_dealloc, /* tp_dealloc */
2323 0, /* tp_print */
2324 0, /* tp_getattr */
2325 0, /* tp_setattr */
2326 0, /* tp_reserved */
2327 (reprfunc)set_repr, /* tp_repr */
2328 &frozenset_as_number, /* tp_as_number */
2329 &set_as_sequence, /* tp_as_sequence */
2330 0, /* tp_as_mapping */
2331 frozenset_hash, /* tp_hash */
2332 0, /* tp_call */
2333 0, /* tp_str */
2334 PyObject_GenericGetAttr, /* tp_getattro */
2335 0, /* tp_setattro */
2336 0, /* tp_as_buffer */
2337 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2338 Py_TPFLAGS_BASETYPE, /* tp_flags */
2339 frozenset_doc, /* tp_doc */
2340 (traverseproc)set_traverse, /* tp_traverse */
2341 (inquiry)set_clear_internal, /* tp_clear */
2342 (richcmpfunc)set_richcompare, /* tp_richcompare */
2343 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2344 (getiterfunc)set_iter, /* tp_iter */
2345 0, /* tp_iternext */
2346 frozenset_methods, /* tp_methods */
2347 0, /* tp_members */
2348 0, /* tp_getset */
2349 0, /* tp_base */
2350 0, /* tp_dict */
2351 0, /* tp_descr_get */
2352 0, /* tp_descr_set */
2353 0, /* tp_dictoffset */
2354 0, /* tp_init */
2355 PyType_GenericAlloc, /* tp_alloc */
2356 frozenset_new, /* tp_new */
2357 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002358};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002359
2360
2361/***** C API functions *************************************************/
2362
2363PyObject *
2364PySet_New(PyObject *iterable)
2365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002367}
2368
2369PyObject *
2370PyFrozenSet_New(PyObject *iterable)
2371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002373}
2374
Neal Norwitz8c49c822006-03-04 18:41:19 +00002375Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002376PySet_Size(PyObject *anyset)
2377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 if (!PyAnySet_Check(anyset)) {
2379 PyErr_BadInternalCall();
2380 return -1;
2381 }
2382 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002383}
2384
2385int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002386PySet_Clear(PyObject *set)
2387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 if (!PySet_Check(set)) {
2389 PyErr_BadInternalCall();
2390 return -1;
2391 }
2392 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002393}
2394
2395int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002396PySet_Contains(PyObject *anyset, PyObject *key)
2397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 if (!PyAnySet_Check(anyset)) {
2399 PyErr_BadInternalCall();
2400 return -1;
2401 }
2402 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002403}
2404
2405int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002406PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 if (!PySet_Check(set)) {
2409 PyErr_BadInternalCall();
2410 return -1;
2411 }
2412 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002413}
2414
2415int
Christian Heimesfd66e512008-01-29 12:18:50 +00002416PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 if (!PySet_Check(anyset) &&
2419 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2420 PyErr_BadInternalCall();
2421 return -1;
2422 }
2423 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002424}
2425
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002426int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002427_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 if (!PyAnySet_Check(set)) {
2432 PyErr_BadInternalCall();
2433 return -1;
2434 }
2435 if (set_next((PySetObject *)set, pos, &entry) == 0)
2436 return 0;
2437 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002438 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002440}
2441
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002442PyObject *
2443PySet_Pop(PyObject *set)
2444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 if (!PySet_Check(set)) {
2446 PyErr_BadInternalCall();
2447 return NULL;
2448 }
2449 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002450}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002451
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002452int
2453_PySet_Update(PyObject *set, PyObject *iterable)
2454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 if (!PySet_Check(set)) {
2456 PyErr_BadInternalCall();
2457 return -1;
2458 }
2459 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002460}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002461
2462#ifdef Py_DEBUG
2463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002465 Returns True and original set is restored. */
2466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467#define assertRaises(call_return_value, exception) \
2468 do { \
2469 assert(call_return_value); \
2470 assert(PyErr_ExceptionMatches(exception)); \
2471 PyErr_Clear(); \
2472 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002473
2474static PyObject *
2475test_c_api(PySetObject *so)
2476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 Py_ssize_t count;
2478 char *s;
2479 Py_ssize_t i;
2480 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2481 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002482 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 /* Verify preconditions */
2486 assert(PyAnySet_Check(ob));
2487 assert(PyAnySet_CheckExact(ob));
2488 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 /* so.clear(); so |= set("abc"); */
2491 str = PyUnicode_FromString("abc");
2492 if (str == NULL)
2493 return NULL;
2494 set_clear_internal(so);
2495 if (set_update_internal(so, str) == -1) {
2496 Py_DECREF(str);
2497 return NULL;
2498 }
2499 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 /* Exercise type/size checks */
2502 assert(PySet_Size(ob) == 3);
2503 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 /* Raise TypeError for non-iterable constructor arguments */
2506 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2507 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 /* Raise TypeError for unhashable key */
2510 dup = PySet_New(ob);
2511 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2512 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2513 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 /* Exercise successful pop, contains, add, and discard */
2516 elem = PySet_Pop(ob);
2517 assert(PySet_Contains(ob, elem) == 0);
2518 assert(PySet_GET_SIZE(ob) == 2);
2519 assert(PySet_Add(ob, elem) == 0);
2520 assert(PySet_Contains(ob, elem) == 1);
2521 assert(PySet_GET_SIZE(ob) == 3);
2522 assert(PySet_Discard(ob, elem) == 1);
2523 assert(PySet_GET_SIZE(ob) == 2);
2524 assert(PySet_Discard(ob, elem) == 0);
2525 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 /* Exercise clear */
2528 dup2 = PySet_New(dup);
2529 assert(PySet_Clear(dup2) == 0);
2530 assert(PySet_Size(dup2) == 0);
2531 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 /* Raise SystemError on clear or update of frozen set */
2534 f = PyFrozenSet_New(dup);
2535 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2536 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2537 assert(PySet_Add(f, elem) == 0);
2538 Py_INCREF(f);
2539 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2540 Py_DECREF(f);
2541 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 /* Exercise direct iteration */
2544 i = 0, count = 0;
2545 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2546 s = _PyUnicode_AsString(x);
2547 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2548 count++;
2549 }
2550 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 /* Exercise updates */
2553 dup2 = PySet_New(NULL);
2554 assert(_PySet_Update(dup2, dup) == 0);
2555 assert(PySet_Size(dup2) == 3);
2556 assert(_PySet_Update(dup2, dup) == 0);
2557 assert(PySet_Size(dup2) == 3);
2558 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 /* Raise SystemError when self argument is not a set or frozenset. */
2561 t = PyTuple_New(0);
2562 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2563 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2564 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 /* Raise SystemError when self argument is not a set. */
2567 f = PyFrozenSet_New(dup);
2568 assert(PySet_Size(f) == 3);
2569 assert(PyFrozenSet_CheckExact(f));
2570 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2571 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2572 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 /* Raise KeyError when popping from an empty set */
2575 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2576 Py_DECREF(ob);
2577 assert(PySet_GET_SIZE(ob) == 0);
2578 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 /* Restore the set from the copy using the PyNumber API */
2581 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2582 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 /* Verify constructors accept NULL arguments */
2585 f = PySet_New(NULL);
2586 assert(f != NULL);
2587 assert(PySet_GET_SIZE(f) == 0);
2588 Py_DECREF(f);
2589 f = PyFrozenSet_New(NULL);
2590 assert(f != NULL);
2591 assert(PyFrozenSet_CheckExact(f));
2592 assert(PySet_GET_SIZE(f) == 0);
2593 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 Py_DECREF(elem);
2596 Py_DECREF(dup);
2597 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002598}
2599
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002600#undef assertRaises
2601
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002602#endif