blob: f84c929a47a9249ef3eadea828c6ad0a760fec12 [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 j = i;
219 perturb = hash;
220 while (1) {
221 j ^= 1;
222 entry = &table[j];
223 if (entry->key == NULL)
224 return freeslot == NULL ? entry : freeslot;
225 if (entry->key == key
226 || (entry->hash == hash
227 && entry->key != dummy
228 && unicode_eq(entry->key, key)))
229 return entry;
230 if (entry->key == dummy && freeslot == NULL)
231 freeslot = entry;
232
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700233 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700234 j = i & mask;
235 perturb >>= PERTURB_SHIFT;
236
237 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (entry->key == NULL)
239 return freeslot == NULL ? entry : freeslot;
240 if (entry->key == key
241 || (entry->hash == hash
242 && entry->key != dummy
243 && unicode_eq(entry->key, key)))
244 return entry;
245 if (entry->key == dummy && freeslot == NULL)
246 freeslot = entry;
247 }
248 assert(0); /* NOT REACHED */
249 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000250}
251
252/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000253Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000254Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000255Eats a reference to key.
256*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000257static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200258set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000259{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200260 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 assert(so->lookup != NULL);
263 entry = so->lookup(so, key, hash);
264 if (entry == NULL)
265 return -1;
266 if (entry->key == NULL) {
267 /* UNUSED */
268 so->fill++;
269 entry->key = key;
270 entry->hash = hash;
271 so->used++;
272 } else if (entry->key == dummy) {
273 /* DUMMY */
274 entry->key = key;
275 entry->hash = hash;
276 so->used++;
277 Py_DECREF(dummy);
278 } else {
279 /* ACTIVE */
280 Py_DECREF(key);
281 }
282 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000283}
284
285/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000286Internal routine used by set_table_resize() to insert an item which is
287known to be absent from the set. This routine also assumes that
288the set contains no deleted entries. Besides the performance benefit,
289using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
290Note that no refcounts are changed by this routine; if needed, the caller
291is responsible for incref'ing `key`.
292*/
293static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200294set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200297 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700298 size_t perturb = hash;
299 size_t mask = (size_t)so->mask;
300 size_t i, j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000301
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700302 i = j = (size_t)hash & mask;
303 while (1) {
304 entry = &table[j];
305 if (entry->key == NULL)
306 break;
307 entry = &table[j ^ 1];
308 if (entry->key == NULL)
309 break;
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700310 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700311 j = i & mask;
312 perturb >>= PERTURB_SHIFT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 }
314 so->fill++;
315 entry->key = key;
316 entry->hash = hash;
317 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000318}
319
320/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000321Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000322keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323actually be smaller than the old one.
324*/
325static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000326set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 Py_ssize_t newsize;
329 setentry *oldtable, *newtable, *entry;
330 Py_ssize_t i;
331 int is_oldtable_malloced;
332 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700333 PyObject *dummy_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 /* Find the smallest table size > minused. */
338 for (newsize = PySet_MINSIZE;
339 newsize <= minused && newsize > 0;
340 newsize <<= 1)
341 ;
342 if (newsize <= 0) {
343 PyErr_NoMemory();
344 return -1;
345 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 /* Get space for a new table. */
348 oldtable = so->table;
349 assert(oldtable != NULL);
350 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (newsize == PySet_MINSIZE) {
353 /* A large table is shrinking, or we can't get any smaller. */
354 newtable = so->smalltable;
355 if (newtable == oldtable) {
356 if (so->fill == so->used) {
357 /* No dummies, so no point doing anything. */
358 return 0;
359 }
360 /* We're not going to resize it, but rebuild the
361 table anyway to purge old dummy entries.
362 Subtle: This is *necessary* if fill==size,
363 as set_lookkey needs at least one virgin slot to
364 terminate failing searches. If fill < size, it's
365 merely desirable, as dummies slow searches. */
366 assert(so->fill > so->used);
367 memcpy(small_copy, oldtable, sizeof(small_copy));
368 oldtable = small_copy;
369 }
370 }
371 else {
372 newtable = PyMem_NEW(setentry, newsize);
373 if (newtable == NULL) {
374 PyErr_NoMemory();
375 return -1;
376 }
377 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 /* Make the set empty, using the new table. */
380 assert(newtable != oldtable);
381 so->table = newtable;
382 so->mask = newsize - 1;
383 memset(newtable, 0, sizeof(setentry) * newsize);
384 so->used = 0;
385 i = so->fill;
386 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 /* Copy the data over; this is refcount-neutral for active entries;
389 dummy entries aren't copied over, of course */
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700390 dummy_entry = dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 for (entry = oldtable; i > 0; entry++) {
392 if (entry->key == NULL) {
393 /* UNUSED */
394 ;
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700395 } else if (entry->key == dummy_entry) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 /* DUMMY */
397 --i;
398 assert(entry->key == dummy);
399 Py_DECREF(entry->key);
400 } else {
401 /* ACTIVE */
402 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000403 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 }
405 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (is_oldtable_malloced)
408 PyMem_DEL(oldtable);
409 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000410}
411
Raymond Hettingerc991db22005-08-11 07:58:45 +0000412/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
413
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000414static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200415set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000416{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200417 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000418 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100419 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 assert(so->fill <= so->mask); /* at least one empty slot */
422 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000423 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100424 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000425 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 return -1;
427 }
428 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
429 return 0;
430 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000431}
432
433static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200434set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000435{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200436 Py_hash_t hash;
437 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200440 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 hash = PyObject_Hash(key);
442 if (hash == -1)
443 return -1;
444 }
445 assert(so->fill <= so->mask); /* at least one empty slot */
446 n_used = so->used;
447 Py_INCREF(key);
448 if (set_insert_key(so, key, hash) == -1) {
449 Py_DECREF(key);
450 return -1;
451 }
452 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
453 return 0;
454 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455}
456
457#define DISCARD_NOTFOUND 0
458#define DISCARD_FOUND 1
459
460static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000461set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200462{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000464
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000465 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (entry == NULL)
467 return -1;
468 if (entry->key == NULL || entry->key == dummy)
469 return DISCARD_NOTFOUND;
470 old_key = entry->key;
471 Py_INCREF(dummy);
472 entry->key = dummy;
473 so->used--;
474 Py_DECREF(old_key);
475 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000476}
477
478static int
479set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200481 Py_hash_t hash;
482 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200488 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 hash = PyObject_Hash(key);
490 if (hash == -1)
491 return -1;
492 }
493 entry = (so->lookup)(so, key, hash);
494 if (entry == NULL)
495 return -1;
496 if (entry->key == NULL || entry->key == dummy)
497 return DISCARD_NOTFOUND;
498 old_key = entry->key;
499 Py_INCREF(dummy);
500 entry->key = dummy;
501 so->used--;
502 Py_DECREF(old_key);
503 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000504}
505
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000506static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000507set_clear_internal(PySetObject *so)
508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 setentry *entry, *table;
510 int table_is_malloced;
511 Py_ssize_t fill;
512 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 Py_ssize_t i, n;
515 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 n = so->mask + 1;
518 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519#endif
520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 table = so->table;
522 assert(table != NULL);
523 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 /* This is delicate. During the process of clearing the set,
526 * decrefs can cause the set to mutate. To avoid fatal confusion
527 * (voice of experience), we have to make the set empty before
528 * clearing the slots, and never refer to anything via so->ref while
529 * clearing.
530 */
531 fill = so->fill;
532 if (table_is_malloced)
533 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 else if (fill > 0) {
536 /* It's a small table with something that needs to be cleared.
537 * Afraid the only safe way is to copy the set entries into
538 * another small table first.
539 */
540 memcpy(small_copy, table, sizeof(small_copy));
541 table = small_copy;
542 EMPTY_TO_MINSIZE(so);
543 }
544 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 /* Now we can finally clear things. If C had refcounts, we could
547 * assert that the refcount on table is 1 now, i.e. that this function
548 * has unique access to it, so decref side-effects can't alter it.
549 */
550 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000551#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 assert(i < n);
553 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000554#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 if (entry->key) {
556 --fill;
557 Py_DECREF(entry->key);
558 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000559#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 else
561 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000562#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 if (table_is_malloced)
566 PyMem_DEL(table);
567 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000568}
569
570/*
571 * Iterate over a set table. Use like so:
572 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000573 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000574 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000575 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000576 * while (set_next(yourset, &pos, &entry)) {
577 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000578 * }
579 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000580 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000582 */
583static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000584set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 Py_ssize_t i;
587 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200588 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 assert (PyAnySet_Check(so));
591 i = *pos_ptr;
592 assert(i >= 0);
593 table = so->table;
594 mask = so->mask;
595 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
596 i++;
597 *pos_ptr = i+1;
598 if (i > mask)
599 return 0;
600 assert(table[i].key != NULL);
601 *entry_ptr = &table[i];
602 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000603}
604
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000605static void
606set_dealloc(PySetObject *so)
607{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200608 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 Py_ssize_t fill = so->fill;
610 PyObject_GC_UnTrack(so);
611 Py_TRASHCAN_SAFE_BEGIN(so)
612 if (so->weakreflist != NULL)
613 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 for (entry = so->table; fill > 0; entry++) {
616 if (entry->key) {
617 --fill;
618 Py_DECREF(entry->key);
619 }
620 }
621 if (so->table != so->smalltable)
622 PyMem_DEL(so->table);
623 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
624 free_list[numfree++] = so;
625 else
626 Py_TYPE(so)->tp_free(so);
627 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000628}
629
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000630static PyObject *
631set_repr(PySetObject *so)
632{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200633 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 if (status != 0) {
637 if (status < 0)
638 return NULL;
639 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
640 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 /* shortcut for the empty set */
643 if (!so->used) {
644 Py_ReprLeave((PyObject*)so);
645 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
646 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 keys = PySequence_List((PyObject *)so);
649 if (keys == NULL)
650 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000651
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200652 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 listrepr = PyObject_Repr(keys);
654 Py_DECREF(keys);
655 if (listrepr == NULL)
656 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200657 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200659 if (tmp == NULL)
660 goto done;
661 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200662
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200663 if (Py_TYPE(so) != &PySet_Type)
664 result = PyUnicode_FromFormat("%s({%U})",
665 Py_TYPE(so)->tp_name,
666 listrepr);
667 else
668 result = PyUnicode_FromFormat("{%U}", listrepr);
669 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000670done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 Py_ReprLeave((PyObject*)so);
672 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000673}
674
Martin v. Löwis18e16552006-02-15 17:27:45 +0000675static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000676set_len(PyObject *so)
677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000679}
680
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000681static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000682set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000685 PyObject *key;
Raymond Hettinger5bb1b1d2013-08-21 01:34:18 -0700686 PyObject *dummy_entry;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100687 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200688 Py_ssize_t i;
689 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 assert (PyAnySet_Check(so));
692 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 other = (PySetObject*)otherset;
695 if (other == so || other->used == 0)
696 /* a.update(a) or a.update({}); nothing to do */
697 return 0;
698 /* Do one big resize at the start, rather than
699 * incrementally resizing as we insert new keys. Expect
700 * that there will be no (or few) overlapping keys.
701 */
702 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
703 if (set_table_resize(so, (so->used + other->used)*2) != 0)
704 return -1;
705 }
Raymond Hettinger5bb1b1d2013-08-21 01:34:18 -0700706 dummy_entry = dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 for (i = 0; i <= other->mask; i++) {
708 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000709 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100710 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000711 if (key != NULL &&
Raymond Hettinger5bb1b1d2013-08-21 01:34:18 -0700712 key != dummy_entry) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000713 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100714 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000715 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 return -1;
717 }
718 }
719 }
720 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000721}
722
723static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000724set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000725{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000726 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200730 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 hash = PyObject_Hash(key);
732 if (hash == -1)
733 return -1;
734 }
735 entry = (so->lookup)(so, key, hash);
736 if (entry == NULL)
737 return -1;
738 key = entry->key;
739 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000740}
741
Raymond Hettingerc991db22005-08-11 07:58:45 +0000742static int
743set_contains_entry(PySetObject *so, setentry *entry)
744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 PyObject *key;
746 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000747
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000748 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 if (lu_entry == NULL)
750 return -1;
751 key = lu_entry->key;
752 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000753}
754
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000755static PyObject *
756set_pop(PySetObject *so)
757{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200758 Py_ssize_t i = 0;
759 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 assert (PyAnySet_Check(so));
763 if (so->used == 0) {
764 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
765 return NULL;
766 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 /* Set entry to "the first" unused or dummy set entry. We abuse
769 * the hash field of slot 0 to hold a search finger:
770 * If slot 0 has a value, use slot 0.
771 * Else slot 0 is being used to hold a search finger,
772 * and we use its hash value as the first index to look.
773 */
774 entry = &so->table[0];
775 if (entry->key == NULL || entry->key == dummy) {
776 i = entry->hash;
777 /* The hash field may be a real hash value, or it may be a
778 * legit search finger, or it may be a once-legit search
779 * finger that's out of bounds now because it wrapped around
780 * or the table shrunk -- simply make sure it's in bounds now.
781 */
782 if (i > so->mask || i < 1)
783 i = 1; /* skip slot 0 */
784 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
785 i++;
786 if (i > so->mask)
787 i = 1;
788 }
789 }
790 key = entry->key;
791 Py_INCREF(dummy);
792 entry->key = dummy;
793 so->used--;
794 so->table[0].hash = i + 1; /* next place to start */
795 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000796}
797
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000798PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
799Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000800
801static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000802set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 Py_ssize_t pos = 0;
805 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 while (set_next(so, &pos, &entry))
808 Py_VISIT(entry->key);
809 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000810}
811
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000812static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000813frozenset_hash(PyObject *self)
814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800816 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 setentry *entry;
818 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 if (so->hash != -1)
821 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000822
Mark Dickinson57e683e2011-09-24 18:18:40 +0100823 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 while (set_next(so, &pos, &entry)) {
825 /* Work to increase the bit dispersion for closely spaced hash
826 values. The is important because some use cases have many
827 combinations of a small number of elements with nearby
828 hashes so that many distinct combinations collapse to only
829 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000830 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800831 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800833 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800835 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 so->hash = hash;
837 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000838}
839
Raymond Hettingera9d99362005-08-05 00:01:15 +0000840/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000841
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000842typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 PyObject_HEAD
844 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
845 Py_ssize_t si_used;
846 Py_ssize_t si_pos;
847 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848} setiterobject;
849
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850static void
851setiter_dealloc(setiterobject *si)
852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 Py_XDECREF(si->si_set);
854 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000855}
856
857static int
858setiter_traverse(setiterobject *si, visitproc visit, void *arg)
859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 Py_VISIT(si->si_set);
861 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000862}
863
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000864static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000865setiter_len(setiterobject *si)
866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 Py_ssize_t len = 0;
868 if (si->si_set != NULL && si->si_used == si->si_set->used)
869 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000870 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000871}
872
Armin Rigof5b3e362006-02-11 21:32:43 +0000873PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000874
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000875static PyObject *setiter_iternext(setiterobject *si);
876
877static PyObject *
878setiter_reduce(setiterobject *si)
879{
880 PyObject *list;
881 setiterobject tmp;
882
883 list = PyList_New(0);
884 if (!list)
885 return NULL;
886
Ezio Melotti0e1af282012-09-28 16:43:40 +0300887 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000888 tmp = *si;
889 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300890
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000891 /* iterate the temporary into a list */
892 for(;;) {
893 PyObject *element = setiter_iternext(&tmp);
894 if (element) {
895 if (PyList_Append(list, element)) {
896 Py_DECREF(element);
897 Py_DECREF(list);
898 Py_XDECREF(tmp.si_set);
899 return NULL;
900 }
901 Py_DECREF(element);
902 } else
903 break;
904 }
905 Py_XDECREF(tmp.si_set);
906 /* check for error */
907 if (tmp.si_set != NULL) {
908 /* we have an error */
909 Py_DECREF(list);
910 return NULL;
911 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200912 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000913}
914
915PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
916
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000917static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000919 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000921};
922
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000923static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200926 Py_ssize_t i, mask;
927 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 if (so == NULL)
931 return NULL;
932 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 if (si->si_used != so->used) {
935 PyErr_SetString(PyExc_RuntimeError,
936 "Set changed size during iteration");
937 si->si_used = -1; /* Make this state sticky */
938 return NULL;
939 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 i = si->si_pos;
942 assert(i>=0);
943 entry = so->table;
944 mask = so->mask;
945 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
946 i++;
947 si->si_pos = i+1;
948 if (i > mask)
949 goto fail;
950 si->len--;
951 key = entry[i].key;
952 Py_INCREF(key);
953 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000954
955fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 Py_DECREF(so);
957 si->si_set = NULL;
958 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000959}
960
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000961PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 PyVarObject_HEAD_INIT(&PyType_Type, 0)
963 "set_iterator", /* tp_name */
964 sizeof(setiterobject), /* tp_basicsize */
965 0, /* tp_itemsize */
966 /* methods */
967 (destructor)setiter_dealloc, /* tp_dealloc */
968 0, /* tp_print */
969 0, /* tp_getattr */
970 0, /* tp_setattr */
971 0, /* tp_reserved */
972 0, /* tp_repr */
973 0, /* tp_as_number */
974 0, /* tp_as_sequence */
975 0, /* tp_as_mapping */
976 0, /* tp_hash */
977 0, /* tp_call */
978 0, /* tp_str */
979 PyObject_GenericGetAttr, /* tp_getattro */
980 0, /* tp_setattro */
981 0, /* tp_as_buffer */
982 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
983 0, /* tp_doc */
984 (traverseproc)setiter_traverse, /* tp_traverse */
985 0, /* tp_clear */
986 0, /* tp_richcompare */
987 0, /* tp_weaklistoffset */
988 PyObject_SelfIter, /* tp_iter */
989 (iternextfunc)setiter_iternext, /* tp_iternext */
990 setiter_methods, /* tp_methods */
991 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000992};
993
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000994static PyObject *
995set_iter(PySetObject *so)
996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
998 if (si == NULL)
999 return NULL;
1000 Py_INCREF(so);
1001 si->si_set = so;
1002 si->si_used = so->used;
1003 si->si_pos = 0;
1004 si->len = so->used;
1005 _PyObject_GC_TRACK(si);
1006 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001007}
1008
Raymond Hettingerd7946662005-08-01 21:39:29 +00001009static int
Raymond Hettingerd7946662005-08-01 21:39:29 +00001010set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +00001011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 if (PyAnySet_Check(other))
1015 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 if (PyDict_CheckExact(other)) {
1018 PyObject *value;
1019 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001020 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 /* Do one big resize at the start, rather than
1024 * incrementally resizing as we insert new keys. Expect
1025 * that there will be no (or few) overlapping keys.
1026 */
1027 if (dictsize == -1)
1028 return -1;
1029 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
1030 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
1031 return -1;
1032 }
1033 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1034 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 an_entry.hash = hash;
1037 an_entry.key = key;
1038 if (set_add_entry(so, &an_entry) == -1)
1039 return -1;
1040 }
1041 return 0;
1042 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 it = PyObject_GetIter(other);
1045 if (it == NULL)
1046 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 while ((key = PyIter_Next(it)) != NULL) {
1049 if (set_add_key(so, key) == -1) {
1050 Py_DECREF(it);
1051 Py_DECREF(key);
1052 return -1;
1053 }
1054 Py_DECREF(key);
1055 }
1056 Py_DECREF(it);
1057 if (PyErr_Occurred())
1058 return -1;
1059 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001060}
1061
1062static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001063set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1068 PyObject *other = PyTuple_GET_ITEM(args, i);
1069 if (set_update_internal(so, other) == -1)
1070 return NULL;
1071 }
1072 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001073}
1074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001076"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001077
1078static PyObject *
1079make_new_set(PyTypeObject *type, PyObject *iterable)
1080{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001081 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 if (dummy == NULL) { /* Auto-initialize dummy */
Raymond Hettingerae9e6162013-08-20 22:28:24 -07001084 dummy = PyUnicode_FromString("<dummy key>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 if (dummy == NULL)
1086 return NULL;
1087 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 /* create PySetObject structure */
1090 if (numfree &&
1091 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1092 so = free_list[--numfree];
1093 assert (so != NULL && PyAnySet_CheckExact(so));
1094 Py_TYPE(so) = type;
1095 _Py_NewReference((PyObject *)so);
1096 EMPTY_TO_MINSIZE(so);
1097 PyObject_GC_Track(so);
1098 } else {
1099 so = (PySetObject *)type->tp_alloc(type, 0);
1100 if (so == NULL)
1101 return NULL;
1102 /* tp_alloc has already zeroed the structure */
1103 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1104 INIT_NONZERO_SET_SLOTS(so);
1105 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 so->lookup = set_lookkey_unicode;
1108 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 if (iterable != NULL) {
1111 if (set_update_internal(so, iterable) == -1) {
1112 Py_DECREF(so);
1113 return NULL;
1114 }
1115 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001118}
1119
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001120static PyObject *
1121make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1124 if (PyType_IsSubtype(type, &PySet_Type))
1125 type = &PySet_Type;
1126 else
1127 type = &PyFrozenSet_Type;
1128 }
1129 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001130}
1131
Raymond Hettingerd7946662005-08-01 21:39:29 +00001132/* The empty frozenset is a singleton */
1133static PyObject *emptyfrozenset = NULL;
1134
Raymond Hettingera690a992003-11-16 16:17:49 +00001135static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001136frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1141 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1144 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (type != &PyFrozenSet_Type)
1147 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (iterable != NULL) {
1150 /* frozenset(f) is idempotent */
1151 if (PyFrozenSet_CheckExact(iterable)) {
1152 Py_INCREF(iterable);
1153 return iterable;
1154 }
1155 result = make_new_set(type, iterable);
1156 if (result == NULL || PySet_GET_SIZE(result))
1157 return result;
1158 Py_DECREF(result);
1159 }
1160 /* The empty frozenset is a singleton */
1161 if (emptyfrozenset == NULL)
1162 emptyfrozenset = make_new_set(type, NULL);
1163 Py_XINCREF(emptyfrozenset);
1164 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001165}
1166
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001167int
1168PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001169{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001170 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 while (numfree) {
1174 numfree--;
1175 so = free_list[numfree];
1176 PyObject_GC_Del(so);
1177 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001178 return freelist_size;
1179}
1180
1181void
1182PySet_Fini(void)
1183{
1184 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 Py_CLEAR(dummy);
1186 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001187}
1188
David Malcolm49526f42012-06-22 14:55:41 -04001189/* Print summary info about the state of the optimized allocator */
1190void
1191_PySet_DebugMallocStats(FILE *out)
1192{
1193 _PyDebugAllocatorStats(out,
1194 "free PySetObject",
1195 numfree, sizeof(PySetObject));
1196}
1197
1198
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001199static PyObject *
1200set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1203 return NULL;
1204
1205 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001206}
1207
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001208/* set_swap_bodies() switches the contents of any two sets by moving their
1209 internal data pointers and, if needed, copying the internal smalltables.
1210 Semantically equivalent to:
1211
1212 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1213
1214 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001216 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001218 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001219*/
1220
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001221static void
1222set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 Py_ssize_t t;
1225 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001226 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001228 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 t = a->fill; a->fill = b->fill; b->fill = t;
1231 t = a->used; a->used = b->used; b->used = t;
1232 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 u = a->table;
1235 if (a->table == a->smalltable)
1236 u = b->smalltable;
1237 a->table = b->table;
1238 if (b->table == b->smalltable)
1239 a->table = a->smalltable;
1240 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 if (a->table == a->smalltable || b->table == b->smalltable) {
1245 memcpy(tab, a->smalltable, sizeof(tab));
1246 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1247 memcpy(b->smalltable, tab, sizeof(tab));
1248 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1251 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1252 h = a->hash; a->hash = b->hash; b->hash = h;
1253 } else {
1254 a->hash = -1;
1255 b->hash = -1;
1256 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001257}
1258
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001259static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001260set_copy(PySetObject *so)
1261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001263}
1264
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001265static PyObject *
1266frozenset_copy(PySetObject *so)
1267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (PyFrozenSet_CheckExact(so)) {
1269 Py_INCREF(so);
1270 return (PyObject *)so;
1271 }
1272 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001273}
1274
Raymond Hettingera690a992003-11-16 16:17:49 +00001275PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1276
1277static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001278set_clear(PySetObject *so)
1279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 set_clear_internal(so);
1281 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001282}
1283
1284PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1285
1286static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001287set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 PySetObject *result;
1290 PyObject *other;
1291 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 result = (PySetObject *)set_copy(so);
1294 if (result == NULL)
1295 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1298 other = PyTuple_GET_ITEM(args, i);
1299 if ((PyObject *)so == other)
1300 continue;
1301 if (set_update_internal(result, other) == -1) {
1302 Py_DECREF(result);
1303 return NULL;
1304 }
1305 }
1306 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001307}
1308
1309PyDoc_STRVAR(union_doc,
1310 "Return the union of sets as a new set.\n\
1311\n\
1312(i.e. all elements that are in either set.)");
1313
1314static PyObject *
1315set_or(PySetObject *so, PyObject *other)
1316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001318
Brian Curtindfc80e32011-08-10 20:28:54 -05001319 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1320 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 result = (PySetObject *)set_copy(so);
1323 if (result == NULL)
1324 return NULL;
1325 if ((PyObject *)so == other)
1326 return (PyObject *)result;
1327 if (set_update_internal(result, other) == -1) {
1328 Py_DECREF(result);
1329 return NULL;
1330 }
1331 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001332}
1333
Raymond Hettingera690a992003-11-16 16:17:49 +00001334static PyObject *
1335set_ior(PySetObject *so, PyObject *other)
1336{
Brian Curtindfc80e32011-08-10 20:28:54 -05001337 if (!PyAnySet_Check(other))
1338 Py_RETURN_NOTIMPLEMENTED;
1339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (set_update_internal(so, other) == -1)
1341 return NULL;
1342 Py_INCREF(so);
1343 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001344}
1345
1346static PyObject *
1347set_intersection(PySetObject *so, PyObject *other)
1348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 PySetObject *result;
1350 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if ((PyObject *)so == other)
1353 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1356 if (result == NULL)
1357 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 if (PyAnySet_Check(other)) {
1360 Py_ssize_t pos = 0;
1361 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1364 tmp = (PyObject *)so;
1365 so = (PySetObject *)other;
1366 other = tmp;
1367 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 while (set_next((PySetObject *)other, &pos, &entry)) {
1370 int rv = set_contains_entry(so, entry);
1371 if (rv == -1) {
1372 Py_DECREF(result);
1373 return NULL;
1374 }
1375 if (rv) {
1376 if (set_add_entry(result, entry) == -1) {
1377 Py_DECREF(result);
1378 return NULL;
1379 }
1380 }
1381 }
1382 return (PyObject *)result;
1383 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 it = PyObject_GetIter(other);
1386 if (it == NULL) {
1387 Py_DECREF(result);
1388 return NULL;
1389 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 while ((key = PyIter_Next(it)) != NULL) {
1392 int rv;
1393 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001394 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 if (hash == -1) {
1397 Py_DECREF(it);
1398 Py_DECREF(result);
1399 Py_DECREF(key);
1400 return NULL;
1401 }
1402 entry.hash = hash;
1403 entry.key = key;
1404 rv = set_contains_entry(so, &entry);
1405 if (rv == -1) {
1406 Py_DECREF(it);
1407 Py_DECREF(result);
1408 Py_DECREF(key);
1409 return NULL;
1410 }
1411 if (rv) {
1412 if (set_add_entry(result, &entry) == -1) {
1413 Py_DECREF(it);
1414 Py_DECREF(result);
1415 Py_DECREF(key);
1416 return NULL;
1417 }
1418 }
1419 Py_DECREF(key);
1420 }
1421 Py_DECREF(it);
1422 if (PyErr_Occurred()) {
1423 Py_DECREF(result);
1424 return NULL;
1425 }
1426 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001427}
1428
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001429static PyObject *
1430set_intersection_multi(PySetObject *so, PyObject *args)
1431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 Py_ssize_t i;
1433 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (PyTuple_GET_SIZE(args) == 0)
1436 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 Py_INCREF(so);
1439 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1440 PyObject *other = PyTuple_GET_ITEM(args, i);
1441 PyObject *newresult = set_intersection((PySetObject *)result, other);
1442 if (newresult == NULL) {
1443 Py_DECREF(result);
1444 return NULL;
1445 }
1446 Py_DECREF(result);
1447 result = newresult;
1448 }
1449 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001450}
1451
Raymond Hettingera690a992003-11-16 16:17:49 +00001452PyDoc_STRVAR(intersection_doc,
1453"Return the intersection of two sets as a new set.\n\
1454\n\
1455(i.e. all elements that are in both sets.)");
1456
1457static PyObject *
1458set_intersection_update(PySetObject *so, PyObject *other)
1459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 tmp = set_intersection(so, other);
1463 if (tmp == NULL)
1464 return NULL;
1465 set_swap_bodies(so, (PySetObject *)tmp);
1466 Py_DECREF(tmp);
1467 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001468}
1469
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001470static PyObject *
1471set_intersection_update_multi(PySetObject *so, PyObject *args)
1472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 tmp = set_intersection_multi(so, args);
1476 if (tmp == NULL)
1477 return NULL;
1478 set_swap_bodies(so, (PySetObject *)tmp);
1479 Py_DECREF(tmp);
1480 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001481}
1482
Raymond Hettingera690a992003-11-16 16:17:49 +00001483PyDoc_STRVAR(intersection_update_doc,
1484"Update a set with the intersection of itself and another.");
1485
1486static PyObject *
1487set_and(PySetObject *so, PyObject *other)
1488{
Brian Curtindfc80e32011-08-10 20:28:54 -05001489 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1490 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001492}
1493
1494static PyObject *
1495set_iand(PySetObject *so, PyObject *other)
1496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001498
Brian Curtindfc80e32011-08-10 20:28:54 -05001499 if (!PyAnySet_Check(other))
1500 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 result = set_intersection_update(so, other);
1502 if (result == NULL)
1503 return NULL;
1504 Py_DECREF(result);
1505 Py_INCREF(so);
1506 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001507}
1508
Guido van Rossum58da9312007-11-10 23:39:45 +00001509static PyObject *
1510set_isdisjoint(PySetObject *so, PyObject *other)
1511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 if ((PyObject *)so == other) {
1515 if (PySet_GET_SIZE(so) == 0)
1516 Py_RETURN_TRUE;
1517 else
1518 Py_RETURN_FALSE;
1519 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (PyAnySet_CheckExact(other)) {
1522 Py_ssize_t pos = 0;
1523 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1526 tmp = (PyObject *)so;
1527 so = (PySetObject *)other;
1528 other = tmp;
1529 }
1530 while (set_next((PySetObject *)other, &pos, &entry)) {
1531 int rv = set_contains_entry(so, entry);
1532 if (rv == -1)
1533 return NULL;
1534 if (rv)
1535 Py_RETURN_FALSE;
1536 }
1537 Py_RETURN_TRUE;
1538 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 it = PyObject_GetIter(other);
1541 if (it == NULL)
1542 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 while ((key = PyIter_Next(it)) != NULL) {
1545 int rv;
1546 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001547 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (hash == -1) {
1550 Py_DECREF(key);
1551 Py_DECREF(it);
1552 return NULL;
1553 }
1554 entry.hash = hash;
1555 entry.key = key;
1556 rv = set_contains_entry(so, &entry);
1557 Py_DECREF(key);
1558 if (rv == -1) {
1559 Py_DECREF(it);
1560 return NULL;
1561 }
1562 if (rv) {
1563 Py_DECREF(it);
1564 Py_RETURN_FALSE;
1565 }
1566 }
1567 Py_DECREF(it);
1568 if (PyErr_Occurred())
1569 return NULL;
1570 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001571}
1572
1573PyDoc_STRVAR(isdisjoint_doc,
1574"Return True if two sets have a null intersection.");
1575
Neal Norwitz6576bd82005-11-13 18:41:28 +00001576static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001577set_difference_update_internal(PySetObject *so, PyObject *other)
1578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 if ((PyObject *)so == other)
1580 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (PyAnySet_Check(other)) {
1583 setentry *entry;
1584 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 while (set_next((PySetObject *)other, &pos, &entry))
1587 if (set_discard_entry(so, entry) == -1)
1588 return -1;
1589 } else {
1590 PyObject *key, *it;
1591 it = PyObject_GetIter(other);
1592 if (it == NULL)
1593 return -1;
1594
1595 while ((key = PyIter_Next(it)) != NULL) {
1596 if (set_discard_key(so, key) == -1) {
1597 Py_DECREF(it);
1598 Py_DECREF(key);
1599 return -1;
1600 }
1601 Py_DECREF(key);
1602 }
1603 Py_DECREF(it);
1604 if (PyErr_Occurred())
1605 return -1;
1606 }
1607 /* If more than 1/5 are dummies, then resize them away. */
1608 if ((so->fill - so->used) * 5 < so->mask)
1609 return 0;
1610 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001611}
1612
Raymond Hettingera690a992003-11-16 16:17:49 +00001613static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001614set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1619 PyObject *other = PyTuple_GET_ITEM(args, i);
1620 if (set_difference_update_internal(so, other) == -1)
1621 return NULL;
1622 }
1623 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001624}
1625
1626PyDoc_STRVAR(difference_update_doc,
1627"Remove all elements of another set from this set.");
1628
1629static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001630set_copy_and_difference(PySetObject *so, PyObject *other)
1631{
1632 PyObject *result;
1633
1634 result = set_copy(so);
1635 if (result == NULL)
1636 return NULL;
1637 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1638 return result;
1639 Py_DECREF(result);
1640 return NULL;
1641}
1642
1643static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001644set_difference(PySetObject *so, PyObject *other)
1645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 PyObject *result;
1647 setentry *entry;
1648 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001651 return set_copy_and_difference(so, other);
1652 }
1653
1654 /* If len(so) much more than len(other), it's more efficient to simply copy
1655 * so and then iterate other looking for common elements. */
1656 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1657 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 result = make_new_set_basetype(Py_TYPE(so), NULL);
1661 if (result == NULL)
1662 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 if (PyDict_CheckExact(other)) {
1665 while (set_next(so, &pos, &entry)) {
1666 setentry entrycopy;
1667 entrycopy.hash = entry->hash;
1668 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001669 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1671 Py_DECREF(result);
1672 return NULL;
1673 }
1674 }
1675 }
1676 return result;
1677 }
1678
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001679 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 while (set_next(so, &pos, &entry)) {
1681 int rv = set_contains_entry((PySetObject *)other, entry);
1682 if (rv == -1) {
1683 Py_DECREF(result);
1684 return NULL;
1685 }
1686 if (!rv) {
1687 if (set_add_entry((PySetObject *)result, entry) == -1) {
1688 Py_DECREF(result);
1689 return NULL;
1690 }
1691 }
1692 }
1693 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001694}
1695
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001696static PyObject *
1697set_difference_multi(PySetObject *so, PyObject *args)
1698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 Py_ssize_t i;
1700 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 if (PyTuple_GET_SIZE(args) == 0)
1703 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 other = PyTuple_GET_ITEM(args, 0);
1706 result = set_difference(so, other);
1707 if (result == NULL)
1708 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1711 other = PyTuple_GET_ITEM(args, i);
1712 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1713 Py_DECREF(result);
1714 return NULL;
1715 }
1716 }
1717 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001718}
1719
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001720PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001721"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001722\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001723(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001724static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001725set_sub(PySetObject *so, PyObject *other)
1726{
Brian Curtindfc80e32011-08-10 20:28:54 -05001727 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1728 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001730}
1731
1732static PyObject *
1733set_isub(PySetObject *so, PyObject *other)
1734{
Brian Curtindfc80e32011-08-10 20:28:54 -05001735 if (!PyAnySet_Check(other))
1736 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 if (set_difference_update_internal(so, other) == -1)
1738 return NULL;
1739 Py_INCREF(so);
1740 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001741}
1742
1743static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001744set_symmetric_difference_update(PySetObject *so, PyObject *other)
1745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 PySetObject *otherset;
1747 PyObject *key;
1748 Py_ssize_t pos = 0;
1749 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 if ((PyObject *)so == other)
1752 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 if (PyDict_CheckExact(other)) {
1755 PyObject *value;
1756 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001757 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1759 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001760
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001761 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 an_entry.hash = hash;
1763 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001766 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001767 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001770 if (rv == DISCARD_NOTFOUND) {
1771 if (set_add_entry(so, &an_entry) == -1) {
1772 Py_DECREF(key);
1773 return NULL;
1774 }
1775 }
Georg Brandl2d444492010-09-03 10:52:55 +00001776 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 }
1778 Py_RETURN_NONE;
1779 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 if (PyAnySet_Check(other)) {
1782 Py_INCREF(other);
1783 otherset = (PySetObject *)other;
1784 } else {
1785 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1786 if (otherset == NULL)
1787 return NULL;
1788 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 while (set_next(otherset, &pos, &entry)) {
1791 int rv = set_discard_entry(so, entry);
1792 if (rv == -1) {
1793 Py_DECREF(otherset);
1794 return NULL;
1795 }
1796 if (rv == DISCARD_NOTFOUND) {
1797 if (set_add_entry(so, entry) == -1) {
1798 Py_DECREF(otherset);
1799 return NULL;
1800 }
1801 }
1802 }
1803 Py_DECREF(otherset);
1804 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001805}
1806
1807PyDoc_STRVAR(symmetric_difference_update_doc,
1808"Update a set with the symmetric difference of itself and another.");
1809
1810static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001811set_symmetric_difference(PySetObject *so, PyObject *other)
1812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 PyObject *rv;
1814 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1817 if (otherset == NULL)
1818 return NULL;
1819 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1820 if (rv == NULL)
1821 return NULL;
1822 Py_DECREF(rv);
1823 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001824}
1825
1826PyDoc_STRVAR(symmetric_difference_doc,
1827"Return the symmetric difference of two sets as a new set.\n\
1828\n\
1829(i.e. all elements that are in exactly one of the sets.)");
1830
1831static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001832set_xor(PySetObject *so, PyObject *other)
1833{
Brian Curtindfc80e32011-08-10 20:28:54 -05001834 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1835 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001837}
1838
1839static PyObject *
1840set_ixor(PySetObject *so, PyObject *other)
1841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001843
Brian Curtindfc80e32011-08-10 20:28:54 -05001844 if (!PyAnySet_Check(other))
1845 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 result = set_symmetric_difference_update(so, other);
1847 if (result == NULL)
1848 return NULL;
1849 Py_DECREF(result);
1850 Py_INCREF(so);
1851 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001852}
1853
1854static PyObject *
1855set_issubset(PySetObject *so, PyObject *other)
1856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 setentry *entry;
1858 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 if (!PyAnySet_Check(other)) {
1861 PyObject *tmp, *result;
1862 tmp = make_new_set(&PySet_Type, other);
1863 if (tmp == NULL)
1864 return NULL;
1865 result = set_issubset(so, tmp);
1866 Py_DECREF(tmp);
1867 return result;
1868 }
1869 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1870 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 while (set_next(so, &pos, &entry)) {
1873 int rv = set_contains_entry((PySetObject *)other, entry);
1874 if (rv == -1)
1875 return NULL;
1876 if (!rv)
1877 Py_RETURN_FALSE;
1878 }
1879 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001880}
1881
1882PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1883
1884static PyObject *
1885set_issuperset(PySetObject *so, PyObject *other)
1886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 if (!PyAnySet_Check(other)) {
1890 tmp = make_new_set(&PySet_Type, other);
1891 if (tmp == NULL)
1892 return NULL;
1893 result = set_issuperset(so, tmp);
1894 Py_DECREF(tmp);
1895 return result;
1896 }
1897 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001898}
1899
1900PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1901
Raymond Hettingera690a992003-11-16 16:17:49 +00001902static PyObject *
1903set_richcompare(PySetObject *v, PyObject *w, int op)
1904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001906
Brian Curtindfc80e32011-08-10 20:28:54 -05001907 if(!PyAnySet_Check(w))
1908 Py_RETURN_NOTIMPLEMENTED;
1909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 switch (op) {
1911 case Py_EQ:
1912 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1913 Py_RETURN_FALSE;
1914 if (v->hash != -1 &&
1915 ((PySetObject *)w)->hash != -1 &&
1916 v->hash != ((PySetObject *)w)->hash)
1917 Py_RETURN_FALSE;
1918 return set_issubset(v, w);
1919 case Py_NE:
1920 r1 = set_richcompare(v, w, Py_EQ);
1921 if (r1 == NULL)
1922 return NULL;
1923 r2 = PyBool_FromLong(PyObject_Not(r1));
1924 Py_DECREF(r1);
1925 return r2;
1926 case Py_LE:
1927 return set_issubset(v, w);
1928 case Py_GE:
1929 return set_issuperset(v, w);
1930 case Py_LT:
1931 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1932 Py_RETURN_FALSE;
1933 return set_issubset(v, w);
1934 case Py_GT:
1935 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1936 Py_RETURN_FALSE;
1937 return set_issuperset(v, w);
1938 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001939 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001940}
1941
Raymond Hettingera690a992003-11-16 16:17:49 +00001942static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001943set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 if (set_add_key(so, key) == -1)
1946 return NULL;
1947 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001948}
1949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001951"Add an element to a set.\n\
1952\n\
1953This has no effect if the element is already present.");
1954
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001955static int
1956set_contains(PySetObject *so, PyObject *key)
1957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 PyObject *tmpkey;
1959 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 rv = set_contains_key(so, key);
1962 if (rv == -1) {
1963 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1964 return -1;
1965 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001966 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 if (tmpkey == NULL)
1968 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001969 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 Py_DECREF(tmpkey);
1971 }
1972 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001973}
1974
1975static PyObject *
1976set_direct_contains(PySetObject *so, PyObject *key)
1977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 result = set_contains(so, key);
1981 if (result == -1)
1982 return NULL;
1983 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001984}
1985
1986PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1987
Raymond Hettingera690a992003-11-16 16:17:49 +00001988static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001989set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 PyObject *tmpkey;
1992 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 rv = set_discard_key(so, key);
1995 if (rv == -1) {
1996 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1997 return NULL;
1998 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001999 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 if (tmpkey == NULL)
2001 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 Py_DECREF(tmpkey);
2004 if (rv == -1)
2005 return NULL;
2006 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (rv == DISCARD_NOTFOUND) {
2009 set_key_error(key);
2010 return NULL;
2011 }
2012 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00002013}
2014
2015PyDoc_STRVAR(remove_doc,
2016"Remove an element from a set; it must be a member.\n\
2017\n\
2018If the element is not a member, raise a KeyError.");
2019
2020static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00002021set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00002022{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04002023 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 rv = set_discard_key(so, key);
2027 if (rv == -1) {
2028 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
2029 return NULL;
2030 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00002031 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 if (tmpkey == NULL)
2033 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002034 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002036 if (rv == -1)
2037 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 }
2039 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00002040}
2041
2042PyDoc_STRVAR(discard_doc,
2043"Remove an element from a set if it is a member.\n\
2044\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002046
2047static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00002048set_reduce(PySetObject *so)
2049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002051 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 keys = PySequence_List((PyObject *)so);
2054 if (keys == NULL)
2055 goto done;
2056 args = PyTuple_Pack(1, keys);
2057 if (args == NULL)
2058 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002059 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 if (dict == NULL) {
2061 PyErr_Clear();
2062 dict = Py_None;
2063 Py_INCREF(dict);
2064 }
2065 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002066done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 Py_XDECREF(args);
2068 Py_XDECREF(keys);
2069 Py_XDECREF(dict);
2070 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002071}
2072
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002073static PyObject *
2074set_sizeof(PySetObject *so)
2075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 res = sizeof(PySetObject);
2079 if (so->table != so->smalltable)
2080 res = res + (so->mask + 1) * sizeof(setentry);
2081 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002082}
2083
2084PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002085static int
2086set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 if (!PyAnySet_Check(self))
2091 return -1;
2092 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2093 return -1;
2094 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2095 return -1;
2096 set_clear_internal(self);
2097 self->hash = -1;
2098 if (iterable == NULL)
2099 return 0;
2100 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002101}
2102
Raymond Hettingera690a992003-11-16 16:17:49 +00002103static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 set_len, /* sq_length */
2105 0, /* sq_concat */
2106 0, /* sq_repeat */
2107 0, /* sq_item */
2108 0, /* sq_slice */
2109 0, /* sq_ass_item */
2110 0, /* sq_ass_slice */
2111 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002112};
2113
2114/* set object ********************************************************/
2115
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002116#ifdef Py_DEBUG
2117static PyObject *test_c_api(PySetObject *so);
2118
2119PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2120All is well if assertions don't fail.");
2121#endif
2122
Raymond Hettingera690a992003-11-16 16:17:49 +00002123static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 {"add", (PyCFunction)set_add, METH_O,
2125 add_doc},
2126 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2127 clear_doc},
2128 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2129 contains_doc},
2130 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2131 copy_doc},
2132 {"discard", (PyCFunction)set_discard, METH_O,
2133 discard_doc},
2134 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2135 difference_doc},
2136 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2137 difference_update_doc},
2138 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2139 intersection_doc},
2140 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2141 intersection_update_doc},
2142 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2143 isdisjoint_doc},
2144 {"issubset", (PyCFunction)set_issubset, METH_O,
2145 issubset_doc},
2146 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2147 issuperset_doc},
2148 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2149 pop_doc},
2150 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2151 reduce_doc},
2152 {"remove", (PyCFunction)set_remove, METH_O,
2153 remove_doc},
2154 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2155 sizeof_doc},
2156 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2157 symmetric_difference_doc},
2158 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2159 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002160#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2162 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002163#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 {"union", (PyCFunction)set_union, METH_VARARGS,
2165 union_doc},
2166 {"update", (PyCFunction)set_update, METH_VARARGS,
2167 update_doc},
2168 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002169};
2170
2171static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 0, /*nb_add*/
2173 (binaryfunc)set_sub, /*nb_subtract*/
2174 0, /*nb_multiply*/
2175 0, /*nb_remainder*/
2176 0, /*nb_divmod*/
2177 0, /*nb_power*/
2178 0, /*nb_negative*/
2179 0, /*nb_positive*/
2180 0, /*nb_absolute*/
2181 0, /*nb_bool*/
2182 0, /*nb_invert*/
2183 0, /*nb_lshift*/
2184 0, /*nb_rshift*/
2185 (binaryfunc)set_and, /*nb_and*/
2186 (binaryfunc)set_xor, /*nb_xor*/
2187 (binaryfunc)set_or, /*nb_or*/
2188 0, /*nb_int*/
2189 0, /*nb_reserved*/
2190 0, /*nb_float*/
2191 0, /*nb_inplace_add*/
2192 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2193 0, /*nb_inplace_multiply*/
2194 0, /*nb_inplace_remainder*/
2195 0, /*nb_inplace_power*/
2196 0, /*nb_inplace_lshift*/
2197 0, /*nb_inplace_rshift*/
2198 (binaryfunc)set_iand, /*nb_inplace_and*/
2199 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2200 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002201};
2202
2203PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002204"set() -> new empty set object\n\
2205set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002206\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002207Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002208
2209PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2211 "set", /* tp_name */
2212 sizeof(PySetObject), /* tp_basicsize */
2213 0, /* tp_itemsize */
2214 /* methods */
2215 (destructor)set_dealloc, /* tp_dealloc */
2216 0, /* tp_print */
2217 0, /* tp_getattr */
2218 0, /* tp_setattr */
2219 0, /* tp_reserved */
2220 (reprfunc)set_repr, /* tp_repr */
2221 &set_as_number, /* tp_as_number */
2222 &set_as_sequence, /* tp_as_sequence */
2223 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002224 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 0, /* tp_call */
2226 0, /* tp_str */
2227 PyObject_GenericGetAttr, /* tp_getattro */
2228 0, /* tp_setattro */
2229 0, /* tp_as_buffer */
2230 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2231 Py_TPFLAGS_BASETYPE, /* tp_flags */
2232 set_doc, /* tp_doc */
2233 (traverseproc)set_traverse, /* tp_traverse */
2234 (inquiry)set_clear_internal, /* tp_clear */
2235 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002236 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2237 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 0, /* tp_iternext */
2239 set_methods, /* tp_methods */
2240 0, /* tp_members */
2241 0, /* tp_getset */
2242 0, /* tp_base */
2243 0, /* tp_dict */
2244 0, /* tp_descr_get */
2245 0, /* tp_descr_set */
2246 0, /* tp_dictoffset */
2247 (initproc)set_init, /* tp_init */
2248 PyType_GenericAlloc, /* tp_alloc */
2249 set_new, /* tp_new */
2250 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002251};
2252
2253/* frozenset object ********************************************************/
2254
2255
2256static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2258 contains_doc},
2259 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2260 copy_doc},
2261 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2262 difference_doc},
2263 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2264 intersection_doc},
2265 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2266 isdisjoint_doc},
2267 {"issubset", (PyCFunction)set_issubset, METH_O,
2268 issubset_doc},
2269 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2270 issuperset_doc},
2271 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2272 reduce_doc},
2273 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2274 sizeof_doc},
2275 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2276 symmetric_difference_doc},
2277 {"union", (PyCFunction)set_union, METH_VARARGS,
2278 union_doc},
2279 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002280};
2281
2282static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 0, /*nb_add*/
2284 (binaryfunc)set_sub, /*nb_subtract*/
2285 0, /*nb_multiply*/
2286 0, /*nb_remainder*/
2287 0, /*nb_divmod*/
2288 0, /*nb_power*/
2289 0, /*nb_negative*/
2290 0, /*nb_positive*/
2291 0, /*nb_absolute*/
2292 0, /*nb_bool*/
2293 0, /*nb_invert*/
2294 0, /*nb_lshift*/
2295 0, /*nb_rshift*/
2296 (binaryfunc)set_and, /*nb_and*/
2297 (binaryfunc)set_xor, /*nb_xor*/
2298 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002299};
2300
2301PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002302"frozenset() -> empty frozenset object\n\
2303frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002304\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002305Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002306
2307PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2309 "frozenset", /* tp_name */
2310 sizeof(PySetObject), /* tp_basicsize */
2311 0, /* tp_itemsize */
2312 /* methods */
2313 (destructor)set_dealloc, /* tp_dealloc */
2314 0, /* tp_print */
2315 0, /* tp_getattr */
2316 0, /* tp_setattr */
2317 0, /* tp_reserved */
2318 (reprfunc)set_repr, /* tp_repr */
2319 &frozenset_as_number, /* tp_as_number */
2320 &set_as_sequence, /* tp_as_sequence */
2321 0, /* tp_as_mapping */
2322 frozenset_hash, /* tp_hash */
2323 0, /* tp_call */
2324 0, /* tp_str */
2325 PyObject_GenericGetAttr, /* tp_getattro */
2326 0, /* tp_setattro */
2327 0, /* tp_as_buffer */
2328 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2329 Py_TPFLAGS_BASETYPE, /* tp_flags */
2330 frozenset_doc, /* tp_doc */
2331 (traverseproc)set_traverse, /* tp_traverse */
2332 (inquiry)set_clear_internal, /* tp_clear */
2333 (richcmpfunc)set_richcompare, /* tp_richcompare */
2334 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2335 (getiterfunc)set_iter, /* tp_iter */
2336 0, /* tp_iternext */
2337 frozenset_methods, /* tp_methods */
2338 0, /* tp_members */
2339 0, /* tp_getset */
2340 0, /* tp_base */
2341 0, /* tp_dict */
2342 0, /* tp_descr_get */
2343 0, /* tp_descr_set */
2344 0, /* tp_dictoffset */
2345 0, /* tp_init */
2346 PyType_GenericAlloc, /* tp_alloc */
2347 frozenset_new, /* tp_new */
2348 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002349};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002350
2351
2352/***** C API functions *************************************************/
2353
2354PyObject *
2355PySet_New(PyObject *iterable)
2356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002358}
2359
2360PyObject *
2361PyFrozenSet_New(PyObject *iterable)
2362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002364}
2365
Neal Norwitz8c49c822006-03-04 18:41:19 +00002366Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002367PySet_Size(PyObject *anyset)
2368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 if (!PyAnySet_Check(anyset)) {
2370 PyErr_BadInternalCall();
2371 return -1;
2372 }
2373 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002374}
2375
2376int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002377PySet_Clear(PyObject *set)
2378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 if (!PySet_Check(set)) {
2380 PyErr_BadInternalCall();
2381 return -1;
2382 }
2383 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002384}
2385
2386int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002387PySet_Contains(PyObject *anyset, PyObject *key)
2388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 if (!PyAnySet_Check(anyset)) {
2390 PyErr_BadInternalCall();
2391 return -1;
2392 }
2393 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002394}
2395
2396int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002397PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 if (!PySet_Check(set)) {
2400 PyErr_BadInternalCall();
2401 return -1;
2402 }
2403 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002404}
2405
2406int
Christian Heimesfd66e512008-01-29 12:18:50 +00002407PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 if (!PySet_Check(anyset) &&
2410 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2411 PyErr_BadInternalCall();
2412 return -1;
2413 }
2414 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002415}
2416
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002417int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002418_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 if (!PyAnySet_Check(set)) {
2423 PyErr_BadInternalCall();
2424 return -1;
2425 }
2426 if (set_next((PySetObject *)set, pos, &entry) == 0)
2427 return 0;
2428 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002429 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002431}
2432
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002433PyObject *
2434PySet_Pop(PyObject *set)
2435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 if (!PySet_Check(set)) {
2437 PyErr_BadInternalCall();
2438 return NULL;
2439 }
2440 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002441}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002442
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002443int
2444_PySet_Update(PyObject *set, PyObject *iterable)
2445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 if (!PySet_Check(set)) {
2447 PyErr_BadInternalCall();
2448 return -1;
2449 }
2450 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002451}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002452
2453#ifdef Py_DEBUG
2454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002456 Returns True and original set is restored. */
2457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458#define assertRaises(call_return_value, exception) \
2459 do { \
2460 assert(call_return_value); \
2461 assert(PyErr_ExceptionMatches(exception)); \
2462 PyErr_Clear(); \
2463 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002464
2465static PyObject *
2466test_c_api(PySetObject *so)
2467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 Py_ssize_t count;
2469 char *s;
2470 Py_ssize_t i;
2471 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2472 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002473 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 /* Verify preconditions */
2477 assert(PyAnySet_Check(ob));
2478 assert(PyAnySet_CheckExact(ob));
2479 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 /* so.clear(); so |= set("abc"); */
2482 str = PyUnicode_FromString("abc");
2483 if (str == NULL)
2484 return NULL;
2485 set_clear_internal(so);
2486 if (set_update_internal(so, str) == -1) {
2487 Py_DECREF(str);
2488 return NULL;
2489 }
2490 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 /* Exercise type/size checks */
2493 assert(PySet_Size(ob) == 3);
2494 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 /* Raise TypeError for non-iterable constructor arguments */
2497 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2498 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 /* Raise TypeError for unhashable key */
2501 dup = PySet_New(ob);
2502 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2503 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2504 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 /* Exercise successful pop, contains, add, and discard */
2507 elem = PySet_Pop(ob);
2508 assert(PySet_Contains(ob, elem) == 0);
2509 assert(PySet_GET_SIZE(ob) == 2);
2510 assert(PySet_Add(ob, elem) == 0);
2511 assert(PySet_Contains(ob, elem) == 1);
2512 assert(PySet_GET_SIZE(ob) == 3);
2513 assert(PySet_Discard(ob, elem) == 1);
2514 assert(PySet_GET_SIZE(ob) == 2);
2515 assert(PySet_Discard(ob, elem) == 0);
2516 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 /* Exercise clear */
2519 dup2 = PySet_New(dup);
2520 assert(PySet_Clear(dup2) == 0);
2521 assert(PySet_Size(dup2) == 0);
2522 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 /* Raise SystemError on clear or update of frozen set */
2525 f = PyFrozenSet_New(dup);
2526 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2527 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2528 assert(PySet_Add(f, elem) == 0);
2529 Py_INCREF(f);
2530 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2531 Py_DECREF(f);
2532 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 /* Exercise direct iteration */
2535 i = 0, count = 0;
2536 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2537 s = _PyUnicode_AsString(x);
2538 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2539 count++;
2540 }
2541 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 /* Exercise updates */
2544 dup2 = PySet_New(NULL);
2545 assert(_PySet_Update(dup2, dup) == 0);
2546 assert(PySet_Size(dup2) == 3);
2547 assert(_PySet_Update(dup2, dup) == 0);
2548 assert(PySet_Size(dup2) == 3);
2549 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 /* Raise SystemError when self argument is not a set or frozenset. */
2552 t = PyTuple_New(0);
2553 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2554 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2555 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 /* Raise SystemError when self argument is not a set. */
2558 f = PyFrozenSet_New(dup);
2559 assert(PySet_Size(f) == 3);
2560 assert(PyFrozenSet_CheckExact(f));
2561 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2562 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2563 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 /* Raise KeyError when popping from an empty set */
2566 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2567 Py_DECREF(ob);
2568 assert(PySet_GET_SIZE(ob) == 0);
2569 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 /* Restore the set from the copy using the PyNumber API */
2572 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2573 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 /* Verify constructors accept NULL arguments */
2576 f = PySet_New(NULL);
2577 assert(f != NULL);
2578 assert(PySet_GET_SIZE(f) == 0);
2579 Py_DECREF(f);
2580 f = PyFrozenSet_New(NULL);
2581 assert(f != NULL);
2582 assert(PyFrozenSet_CheckExact(f));
2583 assert(PySet_GET_SIZE(f) == 0);
2584 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 Py_DECREF(elem);
2587 Py_DECREF(dup);
2588 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002589}
2590
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002591#undef assertRaises
2592
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002593#endif