blob: 0db7e885e9dbb0306c17fac6c62c54d903dd7aa5 [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
71All arithmetic on hash should ignore overflow.
72
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000073Unlike the dictionary implementation, the lookkey functions can return
74NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075*/
76
77static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020078set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000079{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020080 size_t i; /* Unsigned for defined overflow behavior. */
81 size_t perturb;
82 setentry *freeslot;
83 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020085 setentry *entry;
86 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000088
Mark Dickinson57e683e2011-09-24 18:18:40 +010089 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 entry = &table[i];
91 if (entry->key == NULL || entry->key == key)
92 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Raymond Hettinger237b34b2013-08-17 02:31:53 -070094 if (entry->hash == hash) {
95 startkey = entry->key;
96 Py_INCREF(startkey);
97 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
98 Py_DECREF(startkey);
99 if (cmp < 0)
100 return NULL;
101 if (table == so->table && entry->key == startkey) {
102 if (cmp > 0)
103 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 }
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700105 else {
106 /* Start over if the compare altered the set */
107 return set_lookkey(so, key, hash);
108 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000110
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700111 freeslot = (entry->key == dummy) ? entry : NULL;
112
113 /* In the loop, key == dummy is by far (factor of 100s)
114 the least likely outcome, so test for that last. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700116 i = i * 5 + perturb + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 entry = &table[i & mask];
118 if (entry->key == NULL) {
119 if (freeslot != NULL)
120 entry = freeslot;
121 break;
122 }
123 if (entry->key == key)
124 break;
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700125 if (entry->hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 startkey = entry->key;
127 Py_INCREF(startkey);
128 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
129 Py_DECREF(startkey);
130 if (cmp < 0)
131 return NULL;
132 if (table == so->table && entry->key == startkey) {
133 if (cmp > 0)
134 break;
135 }
136 else {
137 /* The compare did major nasty stuff to the
138 * set: start over.
139 */
140 return set_lookkey(so, key, hash);
141 }
142 }
Raymond Hettinger07351a02013-08-17 02:39:46 -0700143 if (entry->key == dummy && freeslot == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 freeslot = entry;
145 }
146 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000147}
148
149/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000150 * Hacked up version of set_lookkey which can assume keys are always unicode;
151 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000152 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000153 */
154static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200155set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000156{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200157 size_t i; /* Unsigned for defined overflow behavior. */
158 size_t perturb;
159 setentry *freeslot;
160 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200162 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 /* Make sure this function doesn't have to handle non-unicode keys,
165 including subclasses of str; e.g., one reason to subclass
166 strings is to override __eq__, and for speed we don't cater to
167 that here. */
168 if (!PyUnicode_CheckExact(key)) {
169 so->lookup = set_lookkey;
170 return set_lookkey(so, key, hash);
171 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100172 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 entry = &table[i];
174 if (entry->key == NULL || entry->key == key)
175 return entry;
176 if (entry->key == dummy)
177 freeslot = entry;
178 else {
179 if (entry->hash == hash && unicode_eq(entry->key, key))
180 return entry;
181 freeslot = NULL;
182 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 /* In the loop, key == dummy is by far (factor of 100s) the
185 least likely outcome, so test for that last. */
186 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700187 i = i * 5 + perturb + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 entry = &table[i & mask];
189 if (entry->key == NULL)
190 return freeslot == NULL ? entry : freeslot;
191 if (entry->key == key
192 || (entry->hash == hash
193 && entry->key != dummy
194 && unicode_eq(entry->key, key)))
195 return entry;
196 if (entry->key == dummy && freeslot == NULL)
197 freeslot = entry;
198 }
199 assert(0); /* NOT REACHED */
200 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000201}
202
203/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000204Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000205Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206Eats a reference to key.
207*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000208static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200209set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000210{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200211 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 assert(so->lookup != NULL);
214 entry = so->lookup(so, key, hash);
215 if (entry == NULL)
216 return -1;
217 if (entry->key == NULL) {
218 /* UNUSED */
219 so->fill++;
220 entry->key = key;
221 entry->hash = hash;
222 so->used++;
223 } else if (entry->key == dummy) {
224 /* DUMMY */
225 entry->key = key;
226 entry->hash = hash;
227 so->used++;
228 Py_DECREF(dummy);
229 } else {
230 /* ACTIVE */
231 Py_DECREF(key);
232 }
233 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234}
235
236/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000237Internal routine used by set_table_resize() to insert an item which is
238known to be absent from the set. This routine also assumes that
239the set contains no deleted entries. Besides the performance benefit,
240using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
241Note that no refcounts are changed by this routine; if needed, the caller
242is responsible for incref'ing `key`.
243*/
244static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200245set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000246{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200247 size_t i;
248 size_t perturb;
249 size_t mask = (size_t)so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200251 setentry *entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252
Mark Dickinson57e683e2011-09-24 18:18:40 +0100253 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 entry = &table[i];
255 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700256 i = i * 5 + perturb + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 entry = &table[i & mask];
258 }
259 so->fill++;
260 entry->key = key;
261 entry->hash = hash;
262 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000263}
264
265/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000267keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000268actually be smaller than the old one.
269*/
270static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 Py_ssize_t newsize;
274 setentry *oldtable, *newtable, *entry;
275 Py_ssize_t i;
276 int is_oldtable_malloced;
277 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700278 PyObject *dummy_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 /* Find the smallest table size > minused. */
283 for (newsize = PySet_MINSIZE;
284 newsize <= minused && newsize > 0;
285 newsize <<= 1)
286 ;
287 if (newsize <= 0) {
288 PyErr_NoMemory();
289 return -1;
290 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 /* Get space for a new table. */
293 oldtable = so->table;
294 assert(oldtable != NULL);
295 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 if (newsize == PySet_MINSIZE) {
298 /* A large table is shrinking, or we can't get any smaller. */
299 newtable = so->smalltable;
300 if (newtable == oldtable) {
301 if (so->fill == so->used) {
302 /* No dummies, so no point doing anything. */
303 return 0;
304 }
305 /* We're not going to resize it, but rebuild the
306 table anyway to purge old dummy entries.
307 Subtle: This is *necessary* if fill==size,
308 as set_lookkey needs at least one virgin slot to
309 terminate failing searches. If fill < size, it's
310 merely desirable, as dummies slow searches. */
311 assert(so->fill > so->used);
312 memcpy(small_copy, oldtable, sizeof(small_copy));
313 oldtable = small_copy;
314 }
315 }
316 else {
317 newtable = PyMem_NEW(setentry, newsize);
318 if (newtable == NULL) {
319 PyErr_NoMemory();
320 return -1;
321 }
322 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 /* Make the set empty, using the new table. */
325 assert(newtable != oldtable);
326 so->table = newtable;
327 so->mask = newsize - 1;
328 memset(newtable, 0, sizeof(setentry) * newsize);
329 so->used = 0;
330 i = so->fill;
331 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* Copy the data over; this is refcount-neutral for active entries;
334 dummy entries aren't copied over, of course */
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700335 dummy_entry = dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 for (entry = oldtable; i > 0; entry++) {
337 if (entry->key == NULL) {
338 /* UNUSED */
339 ;
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700340 } else if (entry->key == dummy_entry) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 /* DUMMY */
342 --i;
343 assert(entry->key == dummy);
344 Py_DECREF(entry->key);
345 } else {
346 /* ACTIVE */
347 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000348 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 }
350 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (is_oldtable_malloced)
353 PyMem_DEL(oldtable);
354 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355}
356
Raymond Hettingerc991db22005-08-11 07:58:45 +0000357/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
358
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000359static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200360set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200362 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000363 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100364 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 assert(so->fill <= so->mask); /* at least one empty slot */
367 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000368 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100369 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000370 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 return -1;
372 }
373 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
374 return 0;
375 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000376}
377
378static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200379set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200381 Py_hash_t hash;
382 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200385 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 hash = PyObject_Hash(key);
387 if (hash == -1)
388 return -1;
389 }
390 assert(so->fill <= so->mask); /* at least one empty slot */
391 n_used = so->used;
392 Py_INCREF(key);
393 if (set_insert_key(so, key, hash) == -1) {
394 Py_DECREF(key);
395 return -1;
396 }
397 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
398 return 0;
399 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000400}
401
402#define DISCARD_NOTFOUND 0
403#define DISCARD_FOUND 1
404
405static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000406set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200407{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000409
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000410 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (entry == NULL)
412 return -1;
413 if (entry->key == NULL || entry->key == dummy)
414 return DISCARD_NOTFOUND;
415 old_key = entry->key;
416 Py_INCREF(dummy);
417 entry->key = dummy;
418 so->used--;
419 Py_DECREF(old_key);
420 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000421}
422
423static int
424set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000425{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200426 Py_hash_t hash;
427 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200433 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 hash = PyObject_Hash(key);
435 if (hash == -1)
436 return -1;
437 }
438 entry = (so->lookup)(so, key, hash);
439 if (entry == NULL)
440 return -1;
441 if (entry->key == NULL || entry->key == dummy)
442 return DISCARD_NOTFOUND;
443 old_key = entry->key;
444 Py_INCREF(dummy);
445 entry->key = dummy;
446 so->used--;
447 Py_DECREF(old_key);
448 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449}
450
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000451static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000452set_clear_internal(PySetObject *so)
453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 setentry *entry, *table;
455 int table_is_malloced;
456 Py_ssize_t fill;
457 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000458#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 Py_ssize_t i, n;
460 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 n = so->mask + 1;
463 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464#endif
465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 table = so->table;
467 assert(table != NULL);
468 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 /* This is delicate. During the process of clearing the set,
471 * decrefs can cause the set to mutate. To avoid fatal confusion
472 * (voice of experience), we have to make the set empty before
473 * clearing the slots, and never refer to anything via so->ref while
474 * clearing.
475 */
476 fill = so->fill;
477 if (table_is_malloced)
478 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 else if (fill > 0) {
481 /* It's a small table with something that needs to be cleared.
482 * Afraid the only safe way is to copy the set entries into
483 * another small table first.
484 */
485 memcpy(small_copy, table, sizeof(small_copy));
486 table = small_copy;
487 EMPTY_TO_MINSIZE(so);
488 }
489 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 /* Now we can finally clear things. If C had refcounts, we could
492 * assert that the refcount on table is 1 now, i.e. that this function
493 * has unique access to it, so decref side-effects can't alter it.
494 */
495 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 assert(i < n);
498 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (entry->key) {
501 --fill;
502 Py_DECREF(entry->key);
503 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000504#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 else
506 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000507#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 if (table_is_malloced)
511 PyMem_DEL(table);
512 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513}
514
515/*
516 * Iterate over a set table. Use like so:
517 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000518 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000519 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000520 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000521 * while (set_next(yourset, &pos, &entry)) {
522 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523 * }
524 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000525 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000527 */
528static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000529set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 Py_ssize_t i;
532 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200533 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 assert (PyAnySet_Check(so));
536 i = *pos_ptr;
537 assert(i >= 0);
538 table = so->table;
539 mask = so->mask;
540 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
541 i++;
542 *pos_ptr = i+1;
543 if (i > mask)
544 return 0;
545 assert(table[i].key != NULL);
546 *entry_ptr = &table[i];
547 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000548}
549
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000550static void
551set_dealloc(PySetObject *so)
552{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200553 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 Py_ssize_t fill = so->fill;
555 PyObject_GC_UnTrack(so);
556 Py_TRASHCAN_SAFE_BEGIN(so)
557 if (so->weakreflist != NULL)
558 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 for (entry = so->table; fill > 0; entry++) {
561 if (entry->key) {
562 --fill;
563 Py_DECREF(entry->key);
564 }
565 }
566 if (so->table != so->smalltable)
567 PyMem_DEL(so->table);
568 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
569 free_list[numfree++] = so;
570 else
571 Py_TYPE(so)->tp_free(so);
572 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000573}
574
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000575static PyObject *
576set_repr(PySetObject *so)
577{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200578 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (status != 0) {
582 if (status < 0)
583 return NULL;
584 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
585 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 /* shortcut for the empty set */
588 if (!so->used) {
589 Py_ReprLeave((PyObject*)so);
590 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
591 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 keys = PySequence_List((PyObject *)so);
594 if (keys == NULL)
595 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000596
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200597 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 listrepr = PyObject_Repr(keys);
599 Py_DECREF(keys);
600 if (listrepr == NULL)
601 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200602 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200604 if (tmp == NULL)
605 goto done;
606 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200607
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200608 if (Py_TYPE(so) != &PySet_Type)
609 result = PyUnicode_FromFormat("%s({%U})",
610 Py_TYPE(so)->tp_name,
611 listrepr);
612 else
613 result = PyUnicode_FromFormat("{%U}", listrepr);
614 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000615done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 Py_ReprLeave((PyObject*)so);
617 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000618}
619
Martin v. Löwis18e16552006-02-15 17:27:45 +0000620static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000621set_len(PyObject *so)
622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000624}
625
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000626static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000627set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000630 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100631 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200632 Py_ssize_t i;
633 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 assert (PyAnySet_Check(so));
636 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 other = (PySetObject*)otherset;
639 if (other == so || other->used == 0)
640 /* a.update(a) or a.update({}); nothing to do */
641 return 0;
642 /* Do one big resize at the start, rather than
643 * incrementally resizing as we insert new keys. Expect
644 * that there will be no (or few) overlapping keys.
645 */
646 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
647 if (set_table_resize(so, (so->used + other->used)*2) != 0)
648 return -1;
649 }
650 for (i = 0; i <= other->mask; i++) {
651 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000652 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100653 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000654 if (key != NULL &&
655 key != dummy) {
656 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100657 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000658 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 return -1;
660 }
661 }
662 }
663 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000664}
665
666static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000667set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000669 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200673 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 hash = PyObject_Hash(key);
675 if (hash == -1)
676 return -1;
677 }
678 entry = (so->lookup)(so, key, hash);
679 if (entry == NULL)
680 return -1;
681 key = entry->key;
682 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000683}
684
Raymond Hettingerc991db22005-08-11 07:58:45 +0000685static int
686set_contains_entry(PySetObject *so, setentry *entry)
687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyObject *key;
689 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000690
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000691 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (lu_entry == NULL)
693 return -1;
694 key = lu_entry->key;
695 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000696}
697
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000698static PyObject *
699set_pop(PySetObject *so)
700{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200701 Py_ssize_t i = 0;
702 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 assert (PyAnySet_Check(so));
706 if (so->used == 0) {
707 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
708 return NULL;
709 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 /* Set entry to "the first" unused or dummy set entry. We abuse
712 * the hash field of slot 0 to hold a search finger:
713 * If slot 0 has a value, use slot 0.
714 * Else slot 0 is being used to hold a search finger,
715 * and we use its hash value as the first index to look.
716 */
717 entry = &so->table[0];
718 if (entry->key == NULL || entry->key == dummy) {
719 i = entry->hash;
720 /* The hash field may be a real hash value, or it may be a
721 * legit search finger, or it may be a once-legit search
722 * finger that's out of bounds now because it wrapped around
723 * or the table shrunk -- simply make sure it's in bounds now.
724 */
725 if (i > so->mask || i < 1)
726 i = 1; /* skip slot 0 */
727 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
728 i++;
729 if (i > so->mask)
730 i = 1;
731 }
732 }
733 key = entry->key;
734 Py_INCREF(dummy);
735 entry->key = dummy;
736 so->used--;
737 so->table[0].hash = i + 1; /* next place to start */
738 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000739}
740
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000741PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
742Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000743
744static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000745set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 Py_ssize_t pos = 0;
748 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 while (set_next(so, &pos, &entry))
751 Py_VISIT(entry->key);
752 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000753}
754
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000755static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000756frozenset_hash(PyObject *self)
757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800759 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 setentry *entry;
761 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 if (so->hash != -1)
764 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000765
Mark Dickinson57e683e2011-09-24 18:18:40 +0100766 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 while (set_next(so, &pos, &entry)) {
768 /* Work to increase the bit dispersion for closely spaced hash
769 values. The is important because some use cases have many
770 combinations of a small number of elements with nearby
771 hashes so that many distinct combinations collapse to only
772 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000773 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800774 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800776 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800778 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 so->hash = hash;
780 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000781}
782
Raymond Hettingera9d99362005-08-05 00:01:15 +0000783/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000784
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000785typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 PyObject_HEAD
787 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
788 Py_ssize_t si_used;
789 Py_ssize_t si_pos;
790 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791} setiterobject;
792
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793static void
794setiter_dealloc(setiterobject *si)
795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 Py_XDECREF(si->si_set);
797 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000798}
799
800static int
801setiter_traverse(setiterobject *si, visitproc visit, void *arg)
802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 Py_VISIT(si->si_set);
804 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805}
806
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000807static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000808setiter_len(setiterobject *si)
809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 Py_ssize_t len = 0;
811 if (si->si_set != NULL && si->si_used == si->si_set->used)
812 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000813 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000814}
815
Armin Rigof5b3e362006-02-11 21:32:43 +0000816PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000817
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000818static PyObject *setiter_iternext(setiterobject *si);
819
820static PyObject *
821setiter_reduce(setiterobject *si)
822{
823 PyObject *list;
824 setiterobject tmp;
825
826 list = PyList_New(0);
827 if (!list)
828 return NULL;
829
Ezio Melotti0e1af282012-09-28 16:43:40 +0300830 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000831 tmp = *si;
832 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300833
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000834 /* iterate the temporary into a list */
835 for(;;) {
836 PyObject *element = setiter_iternext(&tmp);
837 if (element) {
838 if (PyList_Append(list, element)) {
839 Py_DECREF(element);
840 Py_DECREF(list);
841 Py_XDECREF(tmp.si_set);
842 return NULL;
843 }
844 Py_DECREF(element);
845 } else
846 break;
847 }
848 Py_XDECREF(tmp.si_set);
849 /* check for error */
850 if (tmp.si_set != NULL) {
851 /* we have an error */
852 Py_DECREF(list);
853 return NULL;
854 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200855 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000856}
857
858PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
859
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000860static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000862 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000864};
865
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000866static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200869 Py_ssize_t i, mask;
870 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (so == NULL)
874 return NULL;
875 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (si->si_used != so->used) {
878 PyErr_SetString(PyExc_RuntimeError,
879 "Set changed size during iteration");
880 si->si_used = -1; /* Make this state sticky */
881 return NULL;
882 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 i = si->si_pos;
885 assert(i>=0);
886 entry = so->table;
887 mask = so->mask;
888 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
889 i++;
890 si->si_pos = i+1;
891 if (i > mask)
892 goto fail;
893 si->len--;
894 key = entry[i].key;
895 Py_INCREF(key);
896 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000897
898fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 Py_DECREF(so);
900 si->si_set = NULL;
901 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000902}
903
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000904PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 PyVarObject_HEAD_INIT(&PyType_Type, 0)
906 "set_iterator", /* tp_name */
907 sizeof(setiterobject), /* tp_basicsize */
908 0, /* tp_itemsize */
909 /* methods */
910 (destructor)setiter_dealloc, /* tp_dealloc */
911 0, /* tp_print */
912 0, /* tp_getattr */
913 0, /* tp_setattr */
914 0, /* tp_reserved */
915 0, /* tp_repr */
916 0, /* tp_as_number */
917 0, /* tp_as_sequence */
918 0, /* tp_as_mapping */
919 0, /* tp_hash */
920 0, /* tp_call */
921 0, /* tp_str */
922 PyObject_GenericGetAttr, /* tp_getattro */
923 0, /* tp_setattro */
924 0, /* tp_as_buffer */
925 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
926 0, /* tp_doc */
927 (traverseproc)setiter_traverse, /* tp_traverse */
928 0, /* tp_clear */
929 0, /* tp_richcompare */
930 0, /* tp_weaklistoffset */
931 PyObject_SelfIter, /* tp_iter */
932 (iternextfunc)setiter_iternext, /* tp_iternext */
933 setiter_methods, /* tp_methods */
934 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000935};
936
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000937static PyObject *
938set_iter(PySetObject *so)
939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
941 if (si == NULL)
942 return NULL;
943 Py_INCREF(so);
944 si->si_set = so;
945 si->si_used = so->used;
946 si->si_pos = 0;
947 si->len = so->used;
948 _PyObject_GC_TRACK(si);
949 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000950}
951
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000953set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 if (PyAnySet_Check(other))
958 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 if (PyDict_CheckExact(other)) {
961 PyObject *value;
962 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000963 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 /* Do one big resize at the start, rather than
967 * incrementally resizing as we insert new keys. Expect
968 * that there will be no (or few) overlapping keys.
969 */
970 if (dictsize == -1)
971 return -1;
972 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
973 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
974 return -1;
975 }
976 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
977 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 an_entry.hash = hash;
980 an_entry.key = key;
981 if (set_add_entry(so, &an_entry) == -1)
982 return -1;
983 }
984 return 0;
985 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 it = PyObject_GetIter(other);
988 if (it == NULL)
989 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 while ((key = PyIter_Next(it)) != NULL) {
992 if (set_add_key(so, key) == -1) {
993 Py_DECREF(it);
994 Py_DECREF(key);
995 return -1;
996 }
997 Py_DECREF(key);
998 }
999 Py_DECREF(it);
1000 if (PyErr_Occurred())
1001 return -1;
1002 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001003}
1004
1005static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001006set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1011 PyObject *other = PyTuple_GET_ITEM(args, i);
1012 if (set_update_internal(so, other) == -1)
1013 return NULL;
1014 }
1015 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001016}
1017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001019"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001020
1021static PyObject *
1022make_new_set(PyTypeObject *type, PyObject *iterable)
1023{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001024 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (dummy == NULL) { /* Auto-initialize dummy */
Raymond Hettinger237b34b2013-08-17 02:31:53 -07001027 dummy = _PyObject_New(&PyBaseObject_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (dummy == NULL)
1029 return NULL;
1030 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 /* create PySetObject structure */
1033 if (numfree &&
1034 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1035 so = free_list[--numfree];
1036 assert (so != NULL && PyAnySet_CheckExact(so));
1037 Py_TYPE(so) = type;
1038 _Py_NewReference((PyObject *)so);
1039 EMPTY_TO_MINSIZE(so);
1040 PyObject_GC_Track(so);
1041 } else {
1042 so = (PySetObject *)type->tp_alloc(type, 0);
1043 if (so == NULL)
1044 return NULL;
1045 /* tp_alloc has already zeroed the structure */
1046 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1047 INIT_NONZERO_SET_SLOTS(so);
1048 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 so->lookup = set_lookkey_unicode;
1051 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (iterable != NULL) {
1054 if (set_update_internal(so, iterable) == -1) {
1055 Py_DECREF(so);
1056 return NULL;
1057 }
1058 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001061}
1062
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001063static PyObject *
1064make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1067 if (PyType_IsSubtype(type, &PySet_Type))
1068 type = &PySet_Type;
1069 else
1070 type = &PyFrozenSet_Type;
1071 }
1072 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001073}
1074
Raymond Hettingerd7946662005-08-01 21:39:29 +00001075/* The empty frozenset is a singleton */
1076static PyObject *emptyfrozenset = NULL;
1077
Raymond Hettingera690a992003-11-16 16:17:49 +00001078static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001079frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1084 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1087 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 if (type != &PyFrozenSet_Type)
1090 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 if (iterable != NULL) {
1093 /* frozenset(f) is idempotent */
1094 if (PyFrozenSet_CheckExact(iterable)) {
1095 Py_INCREF(iterable);
1096 return iterable;
1097 }
1098 result = make_new_set(type, iterable);
1099 if (result == NULL || PySet_GET_SIZE(result))
1100 return result;
1101 Py_DECREF(result);
1102 }
1103 /* The empty frozenset is a singleton */
1104 if (emptyfrozenset == NULL)
1105 emptyfrozenset = make_new_set(type, NULL);
1106 Py_XINCREF(emptyfrozenset);
1107 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001108}
1109
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001110int
1111PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001112{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001113 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 while (numfree) {
1117 numfree--;
1118 so = free_list[numfree];
1119 PyObject_GC_Del(so);
1120 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001121 return freelist_size;
1122}
1123
1124void
1125PySet_Fini(void)
1126{
1127 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 Py_CLEAR(dummy);
1129 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001130}
1131
David Malcolm49526f42012-06-22 14:55:41 -04001132/* Print summary info about the state of the optimized allocator */
1133void
1134_PySet_DebugMallocStats(FILE *out)
1135{
1136 _PyDebugAllocatorStats(out,
1137 "free PySetObject",
1138 numfree, sizeof(PySetObject));
1139}
1140
1141
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001142static PyObject *
1143set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1146 return NULL;
1147
1148 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001149}
1150
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001151/* set_swap_bodies() switches the contents of any two sets by moving their
1152 internal data pointers and, if needed, copying the internal smalltables.
1153 Semantically equivalent to:
1154
1155 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1156
1157 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001159 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001161 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001162*/
1163
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001164static void
1165set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 Py_ssize_t t;
1168 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001169 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001171 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 t = a->fill; a->fill = b->fill; b->fill = t;
1174 t = a->used; a->used = b->used; b->used = t;
1175 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 u = a->table;
1178 if (a->table == a->smalltable)
1179 u = b->smalltable;
1180 a->table = b->table;
1181 if (b->table == b->smalltable)
1182 a->table = a->smalltable;
1183 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (a->table == a->smalltable || b->table == b->smalltable) {
1188 memcpy(tab, a->smalltable, sizeof(tab));
1189 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1190 memcpy(b->smalltable, tab, sizeof(tab));
1191 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1194 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1195 h = a->hash; a->hash = b->hash; b->hash = h;
1196 } else {
1197 a->hash = -1;
1198 b->hash = -1;
1199 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001200}
1201
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001202static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001203set_copy(PySetObject *so)
1204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001206}
1207
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001208static PyObject *
1209frozenset_copy(PySetObject *so)
1210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (PyFrozenSet_CheckExact(so)) {
1212 Py_INCREF(so);
1213 return (PyObject *)so;
1214 }
1215 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001216}
1217
Raymond Hettingera690a992003-11-16 16:17:49 +00001218PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1219
1220static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001221set_clear(PySetObject *so)
1222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 set_clear_internal(so);
1224 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001225}
1226
1227PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1228
1229static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001230set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 PySetObject *result;
1233 PyObject *other;
1234 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 result = (PySetObject *)set_copy(so);
1237 if (result == NULL)
1238 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1241 other = PyTuple_GET_ITEM(args, i);
1242 if ((PyObject *)so == other)
1243 continue;
1244 if (set_update_internal(result, other) == -1) {
1245 Py_DECREF(result);
1246 return NULL;
1247 }
1248 }
1249 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001250}
1251
1252PyDoc_STRVAR(union_doc,
1253 "Return the union of sets as a new set.\n\
1254\n\
1255(i.e. all elements that are in either set.)");
1256
1257static PyObject *
1258set_or(PySetObject *so, PyObject *other)
1259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001261
Brian Curtindfc80e32011-08-10 20:28:54 -05001262 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1263 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 result = (PySetObject *)set_copy(so);
1266 if (result == NULL)
1267 return NULL;
1268 if ((PyObject *)so == other)
1269 return (PyObject *)result;
1270 if (set_update_internal(result, other) == -1) {
1271 Py_DECREF(result);
1272 return NULL;
1273 }
1274 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001275}
1276
Raymond Hettingera690a992003-11-16 16:17:49 +00001277static PyObject *
1278set_ior(PySetObject *so, PyObject *other)
1279{
Brian Curtindfc80e32011-08-10 20:28:54 -05001280 if (!PyAnySet_Check(other))
1281 Py_RETURN_NOTIMPLEMENTED;
1282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (set_update_internal(so, other) == -1)
1284 return NULL;
1285 Py_INCREF(so);
1286 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001287}
1288
1289static PyObject *
1290set_intersection(PySetObject *so, PyObject *other)
1291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 PySetObject *result;
1293 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if ((PyObject *)so == other)
1296 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1299 if (result == NULL)
1300 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 if (PyAnySet_Check(other)) {
1303 Py_ssize_t pos = 0;
1304 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1307 tmp = (PyObject *)so;
1308 so = (PySetObject *)other;
1309 other = tmp;
1310 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 while (set_next((PySetObject *)other, &pos, &entry)) {
1313 int rv = set_contains_entry(so, entry);
1314 if (rv == -1) {
1315 Py_DECREF(result);
1316 return NULL;
1317 }
1318 if (rv) {
1319 if (set_add_entry(result, entry) == -1) {
1320 Py_DECREF(result);
1321 return NULL;
1322 }
1323 }
1324 }
1325 return (PyObject *)result;
1326 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 it = PyObject_GetIter(other);
1329 if (it == NULL) {
1330 Py_DECREF(result);
1331 return NULL;
1332 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 while ((key = PyIter_Next(it)) != NULL) {
1335 int rv;
1336 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001337 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (hash == -1) {
1340 Py_DECREF(it);
1341 Py_DECREF(result);
1342 Py_DECREF(key);
1343 return NULL;
1344 }
1345 entry.hash = hash;
1346 entry.key = key;
1347 rv = set_contains_entry(so, &entry);
1348 if (rv == -1) {
1349 Py_DECREF(it);
1350 Py_DECREF(result);
1351 Py_DECREF(key);
1352 return NULL;
1353 }
1354 if (rv) {
1355 if (set_add_entry(result, &entry) == -1) {
1356 Py_DECREF(it);
1357 Py_DECREF(result);
1358 Py_DECREF(key);
1359 return NULL;
1360 }
1361 }
1362 Py_DECREF(key);
1363 }
1364 Py_DECREF(it);
1365 if (PyErr_Occurred()) {
1366 Py_DECREF(result);
1367 return NULL;
1368 }
1369 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001370}
1371
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001372static PyObject *
1373set_intersection_multi(PySetObject *so, PyObject *args)
1374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 Py_ssize_t i;
1376 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 if (PyTuple_GET_SIZE(args) == 0)
1379 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 Py_INCREF(so);
1382 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1383 PyObject *other = PyTuple_GET_ITEM(args, i);
1384 PyObject *newresult = set_intersection((PySetObject *)result, other);
1385 if (newresult == NULL) {
1386 Py_DECREF(result);
1387 return NULL;
1388 }
1389 Py_DECREF(result);
1390 result = newresult;
1391 }
1392 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001393}
1394
Raymond Hettingera690a992003-11-16 16:17:49 +00001395PyDoc_STRVAR(intersection_doc,
1396"Return the intersection of two sets as a new set.\n\
1397\n\
1398(i.e. all elements that are in both sets.)");
1399
1400static PyObject *
1401set_intersection_update(PySetObject *so, PyObject *other)
1402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 tmp = set_intersection(so, other);
1406 if (tmp == NULL)
1407 return NULL;
1408 set_swap_bodies(so, (PySetObject *)tmp);
1409 Py_DECREF(tmp);
1410 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001411}
1412
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001413static PyObject *
1414set_intersection_update_multi(PySetObject *so, PyObject *args)
1415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 tmp = set_intersection_multi(so, args);
1419 if (tmp == NULL)
1420 return NULL;
1421 set_swap_bodies(so, (PySetObject *)tmp);
1422 Py_DECREF(tmp);
1423 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001424}
1425
Raymond Hettingera690a992003-11-16 16:17:49 +00001426PyDoc_STRVAR(intersection_update_doc,
1427"Update a set with the intersection of itself and another.");
1428
1429static PyObject *
1430set_and(PySetObject *so, PyObject *other)
1431{
Brian Curtindfc80e32011-08-10 20:28:54 -05001432 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1433 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001435}
1436
1437static PyObject *
1438set_iand(PySetObject *so, PyObject *other)
1439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001441
Brian Curtindfc80e32011-08-10 20:28:54 -05001442 if (!PyAnySet_Check(other))
1443 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 result = set_intersection_update(so, other);
1445 if (result == NULL)
1446 return NULL;
1447 Py_DECREF(result);
1448 Py_INCREF(so);
1449 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001450}
1451
Guido van Rossum58da9312007-11-10 23:39:45 +00001452static PyObject *
1453set_isdisjoint(PySetObject *so, PyObject *other)
1454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if ((PyObject *)so == other) {
1458 if (PySet_GET_SIZE(so) == 0)
1459 Py_RETURN_TRUE;
1460 else
1461 Py_RETURN_FALSE;
1462 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (PyAnySet_CheckExact(other)) {
1465 Py_ssize_t pos = 0;
1466 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1469 tmp = (PyObject *)so;
1470 so = (PySetObject *)other;
1471 other = tmp;
1472 }
1473 while (set_next((PySetObject *)other, &pos, &entry)) {
1474 int rv = set_contains_entry(so, entry);
1475 if (rv == -1)
1476 return NULL;
1477 if (rv)
1478 Py_RETURN_FALSE;
1479 }
1480 Py_RETURN_TRUE;
1481 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 it = PyObject_GetIter(other);
1484 if (it == NULL)
1485 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 while ((key = PyIter_Next(it)) != NULL) {
1488 int rv;
1489 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001490 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (hash == -1) {
1493 Py_DECREF(key);
1494 Py_DECREF(it);
1495 return NULL;
1496 }
1497 entry.hash = hash;
1498 entry.key = key;
1499 rv = set_contains_entry(so, &entry);
1500 Py_DECREF(key);
1501 if (rv == -1) {
1502 Py_DECREF(it);
1503 return NULL;
1504 }
1505 if (rv) {
1506 Py_DECREF(it);
1507 Py_RETURN_FALSE;
1508 }
1509 }
1510 Py_DECREF(it);
1511 if (PyErr_Occurred())
1512 return NULL;
1513 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001514}
1515
1516PyDoc_STRVAR(isdisjoint_doc,
1517"Return True if two sets have a null intersection.");
1518
Neal Norwitz6576bd82005-11-13 18:41:28 +00001519static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001520set_difference_update_internal(PySetObject *so, PyObject *other)
1521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 if ((PyObject *)so == other)
1523 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (PyAnySet_Check(other)) {
1526 setentry *entry;
1527 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 while (set_next((PySetObject *)other, &pos, &entry))
1530 if (set_discard_entry(so, entry) == -1)
1531 return -1;
1532 } else {
1533 PyObject *key, *it;
1534 it = PyObject_GetIter(other);
1535 if (it == NULL)
1536 return -1;
1537
1538 while ((key = PyIter_Next(it)) != NULL) {
1539 if (set_discard_key(so, key) == -1) {
1540 Py_DECREF(it);
1541 Py_DECREF(key);
1542 return -1;
1543 }
1544 Py_DECREF(key);
1545 }
1546 Py_DECREF(it);
1547 if (PyErr_Occurred())
1548 return -1;
1549 }
1550 /* If more than 1/5 are dummies, then resize them away. */
1551 if ((so->fill - so->used) * 5 < so->mask)
1552 return 0;
1553 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001554}
1555
Raymond Hettingera690a992003-11-16 16:17:49 +00001556static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001557set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1562 PyObject *other = PyTuple_GET_ITEM(args, i);
1563 if (set_difference_update_internal(so, other) == -1)
1564 return NULL;
1565 }
1566 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001567}
1568
1569PyDoc_STRVAR(difference_update_doc,
1570"Remove all elements of another set from this set.");
1571
1572static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001573set_copy_and_difference(PySetObject *so, PyObject *other)
1574{
1575 PyObject *result;
1576
1577 result = set_copy(so);
1578 if (result == NULL)
1579 return NULL;
1580 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1581 return result;
1582 Py_DECREF(result);
1583 return NULL;
1584}
1585
1586static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001587set_difference(PySetObject *so, PyObject *other)
1588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 PyObject *result;
1590 setentry *entry;
1591 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001594 return set_copy_and_difference(so, other);
1595 }
1596
1597 /* If len(so) much more than len(other), it's more efficient to simply copy
1598 * so and then iterate other looking for common elements. */
1599 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1600 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 result = make_new_set_basetype(Py_TYPE(so), NULL);
1604 if (result == NULL)
1605 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 if (PyDict_CheckExact(other)) {
1608 while (set_next(so, &pos, &entry)) {
1609 setentry entrycopy;
1610 entrycopy.hash = entry->hash;
1611 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001612 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1614 Py_DECREF(result);
1615 return NULL;
1616 }
1617 }
1618 }
1619 return result;
1620 }
1621
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001622 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 while (set_next(so, &pos, &entry)) {
1624 int rv = set_contains_entry((PySetObject *)other, entry);
1625 if (rv == -1) {
1626 Py_DECREF(result);
1627 return NULL;
1628 }
1629 if (!rv) {
1630 if (set_add_entry((PySetObject *)result, entry) == -1) {
1631 Py_DECREF(result);
1632 return NULL;
1633 }
1634 }
1635 }
1636 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001637}
1638
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001639static PyObject *
1640set_difference_multi(PySetObject *so, PyObject *args)
1641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 Py_ssize_t i;
1643 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 if (PyTuple_GET_SIZE(args) == 0)
1646 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 other = PyTuple_GET_ITEM(args, 0);
1649 result = set_difference(so, other);
1650 if (result == NULL)
1651 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1654 other = PyTuple_GET_ITEM(args, i);
1655 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1656 Py_DECREF(result);
1657 return NULL;
1658 }
1659 }
1660 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001661}
1662
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001663PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001664"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001665\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001666(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001667static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001668set_sub(PySetObject *so, PyObject *other)
1669{
Brian Curtindfc80e32011-08-10 20:28:54 -05001670 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1671 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001673}
1674
1675static PyObject *
1676set_isub(PySetObject *so, PyObject *other)
1677{
Brian Curtindfc80e32011-08-10 20:28:54 -05001678 if (!PyAnySet_Check(other))
1679 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 if (set_difference_update_internal(so, other) == -1)
1681 return NULL;
1682 Py_INCREF(so);
1683 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001684}
1685
1686static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001687set_symmetric_difference_update(PySetObject *so, PyObject *other)
1688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 PySetObject *otherset;
1690 PyObject *key;
1691 Py_ssize_t pos = 0;
1692 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 if ((PyObject *)so == other)
1695 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 if (PyDict_CheckExact(other)) {
1698 PyObject *value;
1699 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001700 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1702 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001703
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001704 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 an_entry.hash = hash;
1706 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001709 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001710 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001713 if (rv == DISCARD_NOTFOUND) {
1714 if (set_add_entry(so, &an_entry) == -1) {
1715 Py_DECREF(key);
1716 return NULL;
1717 }
1718 }
Georg Brandl2d444492010-09-03 10:52:55 +00001719 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 }
1721 Py_RETURN_NONE;
1722 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (PyAnySet_Check(other)) {
1725 Py_INCREF(other);
1726 otherset = (PySetObject *)other;
1727 } else {
1728 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1729 if (otherset == NULL)
1730 return NULL;
1731 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 while (set_next(otherset, &pos, &entry)) {
1734 int rv = set_discard_entry(so, entry);
1735 if (rv == -1) {
1736 Py_DECREF(otherset);
1737 return NULL;
1738 }
1739 if (rv == DISCARD_NOTFOUND) {
1740 if (set_add_entry(so, entry) == -1) {
1741 Py_DECREF(otherset);
1742 return NULL;
1743 }
1744 }
1745 }
1746 Py_DECREF(otherset);
1747 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001748}
1749
1750PyDoc_STRVAR(symmetric_difference_update_doc,
1751"Update a set with the symmetric difference of itself and another.");
1752
1753static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001754set_symmetric_difference(PySetObject *so, PyObject *other)
1755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 PyObject *rv;
1757 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1760 if (otherset == NULL)
1761 return NULL;
1762 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1763 if (rv == NULL)
1764 return NULL;
1765 Py_DECREF(rv);
1766 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001767}
1768
1769PyDoc_STRVAR(symmetric_difference_doc,
1770"Return the symmetric difference of two sets as a new set.\n\
1771\n\
1772(i.e. all elements that are in exactly one of the sets.)");
1773
1774static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001775set_xor(PySetObject *so, PyObject *other)
1776{
Brian Curtindfc80e32011-08-10 20:28:54 -05001777 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1778 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001780}
1781
1782static PyObject *
1783set_ixor(PySetObject *so, PyObject *other)
1784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001786
Brian Curtindfc80e32011-08-10 20:28:54 -05001787 if (!PyAnySet_Check(other))
1788 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 result = set_symmetric_difference_update(so, other);
1790 if (result == NULL)
1791 return NULL;
1792 Py_DECREF(result);
1793 Py_INCREF(so);
1794 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001795}
1796
1797static PyObject *
1798set_issubset(PySetObject *so, PyObject *other)
1799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 setentry *entry;
1801 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 if (!PyAnySet_Check(other)) {
1804 PyObject *tmp, *result;
1805 tmp = make_new_set(&PySet_Type, other);
1806 if (tmp == NULL)
1807 return NULL;
1808 result = set_issubset(so, tmp);
1809 Py_DECREF(tmp);
1810 return result;
1811 }
1812 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1813 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 while (set_next(so, &pos, &entry)) {
1816 int rv = set_contains_entry((PySetObject *)other, entry);
1817 if (rv == -1)
1818 return NULL;
1819 if (!rv)
1820 Py_RETURN_FALSE;
1821 }
1822 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001823}
1824
1825PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1826
1827static PyObject *
1828set_issuperset(PySetObject *so, PyObject *other)
1829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 if (!PyAnySet_Check(other)) {
1833 tmp = make_new_set(&PySet_Type, other);
1834 if (tmp == NULL)
1835 return NULL;
1836 result = set_issuperset(so, tmp);
1837 Py_DECREF(tmp);
1838 return result;
1839 }
1840 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001841}
1842
1843PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1844
Raymond Hettingera690a992003-11-16 16:17:49 +00001845static PyObject *
1846set_richcompare(PySetObject *v, PyObject *w, int op)
1847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001849
Brian Curtindfc80e32011-08-10 20:28:54 -05001850 if(!PyAnySet_Check(w))
1851 Py_RETURN_NOTIMPLEMENTED;
1852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 switch (op) {
1854 case Py_EQ:
1855 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1856 Py_RETURN_FALSE;
1857 if (v->hash != -1 &&
1858 ((PySetObject *)w)->hash != -1 &&
1859 v->hash != ((PySetObject *)w)->hash)
1860 Py_RETURN_FALSE;
1861 return set_issubset(v, w);
1862 case Py_NE:
1863 r1 = set_richcompare(v, w, Py_EQ);
1864 if (r1 == NULL)
1865 return NULL;
1866 r2 = PyBool_FromLong(PyObject_Not(r1));
1867 Py_DECREF(r1);
1868 return r2;
1869 case Py_LE:
1870 return set_issubset(v, w);
1871 case Py_GE:
1872 return set_issuperset(v, w);
1873 case Py_LT:
1874 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1875 Py_RETURN_FALSE;
1876 return set_issubset(v, w);
1877 case Py_GT:
1878 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1879 Py_RETURN_FALSE;
1880 return set_issuperset(v, w);
1881 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001882 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001883}
1884
Raymond Hettingera690a992003-11-16 16:17:49 +00001885static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001886set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 if (set_add_key(so, key) == -1)
1889 return NULL;
1890 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001891}
1892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001894"Add an element to a set.\n\
1895\n\
1896This has no effect if the element is already present.");
1897
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001898static int
1899set_contains(PySetObject *so, PyObject *key)
1900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 PyObject *tmpkey;
1902 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 rv = set_contains_key(so, key);
1905 if (rv == -1) {
1906 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1907 return -1;
1908 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001909 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 if (tmpkey == NULL)
1911 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001912 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 Py_DECREF(tmpkey);
1914 }
1915 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001916}
1917
1918static PyObject *
1919set_direct_contains(PySetObject *so, PyObject *key)
1920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 result = set_contains(so, key);
1924 if (result == -1)
1925 return NULL;
1926 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001927}
1928
1929PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1930
Raymond Hettingera690a992003-11-16 16:17:49 +00001931static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001932set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 PyObject *tmpkey;
1935 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 rv = set_discard_key(so, key);
1938 if (rv == -1) {
1939 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1940 return NULL;
1941 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001942 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 if (tmpkey == NULL)
1944 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 Py_DECREF(tmpkey);
1947 if (rv == -1)
1948 return NULL;
1949 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 if (rv == DISCARD_NOTFOUND) {
1952 set_key_error(key);
1953 return NULL;
1954 }
1955 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001956}
1957
1958PyDoc_STRVAR(remove_doc,
1959"Remove an element from a set; it must be a member.\n\
1960\n\
1961If the element is not a member, raise a KeyError.");
1962
1963static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001964set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001965{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001966 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 rv = set_discard_key(so, key);
1970 if (rv == -1) {
1971 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1972 return NULL;
1973 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001974 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 if (tmpkey == NULL)
1976 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001977 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001979 if (rv == -1)
1980 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 }
1982 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001983}
1984
1985PyDoc_STRVAR(discard_doc,
1986"Remove an element from a set if it is a member.\n\
1987\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001989
1990static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001991set_reduce(PySetObject *so)
1992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001994 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 keys = PySequence_List((PyObject *)so);
1997 if (keys == NULL)
1998 goto done;
1999 args = PyTuple_Pack(1, keys);
2000 if (args == NULL)
2001 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002002 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 if (dict == NULL) {
2004 PyErr_Clear();
2005 dict = Py_None;
2006 Py_INCREF(dict);
2007 }
2008 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002009done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 Py_XDECREF(args);
2011 Py_XDECREF(keys);
2012 Py_XDECREF(dict);
2013 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002014}
2015
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002016static PyObject *
2017set_sizeof(PySetObject *so)
2018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 res = sizeof(PySetObject);
2022 if (so->table != so->smalltable)
2023 res = res + (so->mask + 1) * sizeof(setentry);
2024 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002025}
2026
2027PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002028static int
2029set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 if (!PyAnySet_Check(self))
2034 return -1;
2035 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2036 return -1;
2037 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2038 return -1;
2039 set_clear_internal(self);
2040 self->hash = -1;
2041 if (iterable == NULL)
2042 return 0;
2043 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002044}
2045
Raymond Hettingera690a992003-11-16 16:17:49 +00002046static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 set_len, /* sq_length */
2048 0, /* sq_concat */
2049 0, /* sq_repeat */
2050 0, /* sq_item */
2051 0, /* sq_slice */
2052 0, /* sq_ass_item */
2053 0, /* sq_ass_slice */
2054 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002055};
2056
2057/* set object ********************************************************/
2058
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002059#ifdef Py_DEBUG
2060static PyObject *test_c_api(PySetObject *so);
2061
2062PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2063All is well if assertions don't fail.");
2064#endif
2065
Raymond Hettingera690a992003-11-16 16:17:49 +00002066static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 {"add", (PyCFunction)set_add, METH_O,
2068 add_doc},
2069 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2070 clear_doc},
2071 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2072 contains_doc},
2073 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2074 copy_doc},
2075 {"discard", (PyCFunction)set_discard, METH_O,
2076 discard_doc},
2077 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2078 difference_doc},
2079 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2080 difference_update_doc},
2081 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2082 intersection_doc},
2083 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2084 intersection_update_doc},
2085 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2086 isdisjoint_doc},
2087 {"issubset", (PyCFunction)set_issubset, METH_O,
2088 issubset_doc},
2089 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2090 issuperset_doc},
2091 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2092 pop_doc},
2093 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2094 reduce_doc},
2095 {"remove", (PyCFunction)set_remove, METH_O,
2096 remove_doc},
2097 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2098 sizeof_doc},
2099 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2100 symmetric_difference_doc},
2101 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2102 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002103#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2105 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 {"union", (PyCFunction)set_union, METH_VARARGS,
2108 union_doc},
2109 {"update", (PyCFunction)set_update, METH_VARARGS,
2110 update_doc},
2111 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002112};
2113
2114static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 0, /*nb_add*/
2116 (binaryfunc)set_sub, /*nb_subtract*/
2117 0, /*nb_multiply*/
2118 0, /*nb_remainder*/
2119 0, /*nb_divmod*/
2120 0, /*nb_power*/
2121 0, /*nb_negative*/
2122 0, /*nb_positive*/
2123 0, /*nb_absolute*/
2124 0, /*nb_bool*/
2125 0, /*nb_invert*/
2126 0, /*nb_lshift*/
2127 0, /*nb_rshift*/
2128 (binaryfunc)set_and, /*nb_and*/
2129 (binaryfunc)set_xor, /*nb_xor*/
2130 (binaryfunc)set_or, /*nb_or*/
2131 0, /*nb_int*/
2132 0, /*nb_reserved*/
2133 0, /*nb_float*/
2134 0, /*nb_inplace_add*/
2135 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2136 0, /*nb_inplace_multiply*/
2137 0, /*nb_inplace_remainder*/
2138 0, /*nb_inplace_power*/
2139 0, /*nb_inplace_lshift*/
2140 0, /*nb_inplace_rshift*/
2141 (binaryfunc)set_iand, /*nb_inplace_and*/
2142 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2143 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002144};
2145
2146PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002147"set() -> new empty set object\n\
2148set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002149\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002150Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002151
2152PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2154 "set", /* tp_name */
2155 sizeof(PySetObject), /* tp_basicsize */
2156 0, /* tp_itemsize */
2157 /* methods */
2158 (destructor)set_dealloc, /* tp_dealloc */
2159 0, /* tp_print */
2160 0, /* tp_getattr */
2161 0, /* tp_setattr */
2162 0, /* tp_reserved */
2163 (reprfunc)set_repr, /* tp_repr */
2164 &set_as_number, /* tp_as_number */
2165 &set_as_sequence, /* tp_as_sequence */
2166 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002167 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 0, /* tp_call */
2169 0, /* tp_str */
2170 PyObject_GenericGetAttr, /* tp_getattro */
2171 0, /* tp_setattro */
2172 0, /* tp_as_buffer */
2173 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2174 Py_TPFLAGS_BASETYPE, /* tp_flags */
2175 set_doc, /* tp_doc */
2176 (traverseproc)set_traverse, /* tp_traverse */
2177 (inquiry)set_clear_internal, /* tp_clear */
2178 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002179 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2180 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 0, /* tp_iternext */
2182 set_methods, /* tp_methods */
2183 0, /* tp_members */
2184 0, /* tp_getset */
2185 0, /* tp_base */
2186 0, /* tp_dict */
2187 0, /* tp_descr_get */
2188 0, /* tp_descr_set */
2189 0, /* tp_dictoffset */
2190 (initproc)set_init, /* tp_init */
2191 PyType_GenericAlloc, /* tp_alloc */
2192 set_new, /* tp_new */
2193 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002194};
2195
2196/* frozenset object ********************************************************/
2197
2198
2199static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2201 contains_doc},
2202 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2203 copy_doc},
2204 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2205 difference_doc},
2206 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2207 intersection_doc},
2208 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2209 isdisjoint_doc},
2210 {"issubset", (PyCFunction)set_issubset, METH_O,
2211 issubset_doc},
2212 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2213 issuperset_doc},
2214 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2215 reduce_doc},
2216 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2217 sizeof_doc},
2218 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2219 symmetric_difference_doc},
2220 {"union", (PyCFunction)set_union, METH_VARARGS,
2221 union_doc},
2222 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002223};
2224
2225static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 0, /*nb_add*/
2227 (binaryfunc)set_sub, /*nb_subtract*/
2228 0, /*nb_multiply*/
2229 0, /*nb_remainder*/
2230 0, /*nb_divmod*/
2231 0, /*nb_power*/
2232 0, /*nb_negative*/
2233 0, /*nb_positive*/
2234 0, /*nb_absolute*/
2235 0, /*nb_bool*/
2236 0, /*nb_invert*/
2237 0, /*nb_lshift*/
2238 0, /*nb_rshift*/
2239 (binaryfunc)set_and, /*nb_and*/
2240 (binaryfunc)set_xor, /*nb_xor*/
2241 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002242};
2243
2244PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002245"frozenset() -> empty frozenset object\n\
2246frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002247\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002248Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002249
2250PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2252 "frozenset", /* tp_name */
2253 sizeof(PySetObject), /* tp_basicsize */
2254 0, /* tp_itemsize */
2255 /* methods */
2256 (destructor)set_dealloc, /* tp_dealloc */
2257 0, /* tp_print */
2258 0, /* tp_getattr */
2259 0, /* tp_setattr */
2260 0, /* tp_reserved */
2261 (reprfunc)set_repr, /* tp_repr */
2262 &frozenset_as_number, /* tp_as_number */
2263 &set_as_sequence, /* tp_as_sequence */
2264 0, /* tp_as_mapping */
2265 frozenset_hash, /* tp_hash */
2266 0, /* tp_call */
2267 0, /* tp_str */
2268 PyObject_GenericGetAttr, /* tp_getattro */
2269 0, /* tp_setattro */
2270 0, /* tp_as_buffer */
2271 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2272 Py_TPFLAGS_BASETYPE, /* tp_flags */
2273 frozenset_doc, /* tp_doc */
2274 (traverseproc)set_traverse, /* tp_traverse */
2275 (inquiry)set_clear_internal, /* tp_clear */
2276 (richcmpfunc)set_richcompare, /* tp_richcompare */
2277 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2278 (getiterfunc)set_iter, /* tp_iter */
2279 0, /* tp_iternext */
2280 frozenset_methods, /* tp_methods */
2281 0, /* tp_members */
2282 0, /* tp_getset */
2283 0, /* tp_base */
2284 0, /* tp_dict */
2285 0, /* tp_descr_get */
2286 0, /* tp_descr_set */
2287 0, /* tp_dictoffset */
2288 0, /* tp_init */
2289 PyType_GenericAlloc, /* tp_alloc */
2290 frozenset_new, /* tp_new */
2291 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002292};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002293
2294
2295/***** C API functions *************************************************/
2296
2297PyObject *
2298PySet_New(PyObject *iterable)
2299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002301}
2302
2303PyObject *
2304PyFrozenSet_New(PyObject *iterable)
2305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002307}
2308
Neal Norwitz8c49c822006-03-04 18:41:19 +00002309Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002310PySet_Size(PyObject *anyset)
2311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 if (!PyAnySet_Check(anyset)) {
2313 PyErr_BadInternalCall();
2314 return -1;
2315 }
2316 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002317}
2318
2319int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002320PySet_Clear(PyObject *set)
2321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 if (!PySet_Check(set)) {
2323 PyErr_BadInternalCall();
2324 return -1;
2325 }
2326 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002327}
2328
2329int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002330PySet_Contains(PyObject *anyset, PyObject *key)
2331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 if (!PyAnySet_Check(anyset)) {
2333 PyErr_BadInternalCall();
2334 return -1;
2335 }
2336 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002337}
2338
2339int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002340PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 if (!PySet_Check(set)) {
2343 PyErr_BadInternalCall();
2344 return -1;
2345 }
2346 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002347}
2348
2349int
Christian Heimesfd66e512008-01-29 12:18:50 +00002350PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 if (!PySet_Check(anyset) &&
2353 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2354 PyErr_BadInternalCall();
2355 return -1;
2356 }
2357 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002358}
2359
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002360int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002361_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 if (!PyAnySet_Check(set)) {
2366 PyErr_BadInternalCall();
2367 return -1;
2368 }
2369 if (set_next((PySetObject *)set, pos, &entry) == 0)
2370 return 0;
2371 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002372 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002374}
2375
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002376PyObject *
2377PySet_Pop(PyObject *set)
2378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 if (!PySet_Check(set)) {
2380 PyErr_BadInternalCall();
2381 return NULL;
2382 }
2383 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002384}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002385
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002386int
2387_PySet_Update(PyObject *set, PyObject *iterable)
2388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 if (!PySet_Check(set)) {
2390 PyErr_BadInternalCall();
2391 return -1;
2392 }
2393 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002394}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002395
2396#ifdef Py_DEBUG
2397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002399 Returns True and original set is restored. */
2400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401#define assertRaises(call_return_value, exception) \
2402 do { \
2403 assert(call_return_value); \
2404 assert(PyErr_ExceptionMatches(exception)); \
2405 PyErr_Clear(); \
2406 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002407
2408static PyObject *
2409test_c_api(PySetObject *so)
2410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 Py_ssize_t count;
2412 char *s;
2413 Py_ssize_t i;
2414 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2415 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002416 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* Verify preconditions */
2420 assert(PyAnySet_Check(ob));
2421 assert(PyAnySet_CheckExact(ob));
2422 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* so.clear(); so |= set("abc"); */
2425 str = PyUnicode_FromString("abc");
2426 if (str == NULL)
2427 return NULL;
2428 set_clear_internal(so);
2429 if (set_update_internal(so, str) == -1) {
2430 Py_DECREF(str);
2431 return NULL;
2432 }
2433 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* Exercise type/size checks */
2436 assert(PySet_Size(ob) == 3);
2437 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* Raise TypeError for non-iterable constructor arguments */
2440 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2441 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 /* Raise TypeError for unhashable key */
2444 dup = PySet_New(ob);
2445 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2446 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2447 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 /* Exercise successful pop, contains, add, and discard */
2450 elem = PySet_Pop(ob);
2451 assert(PySet_Contains(ob, elem) == 0);
2452 assert(PySet_GET_SIZE(ob) == 2);
2453 assert(PySet_Add(ob, elem) == 0);
2454 assert(PySet_Contains(ob, elem) == 1);
2455 assert(PySet_GET_SIZE(ob) == 3);
2456 assert(PySet_Discard(ob, elem) == 1);
2457 assert(PySet_GET_SIZE(ob) == 2);
2458 assert(PySet_Discard(ob, elem) == 0);
2459 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 /* Exercise clear */
2462 dup2 = PySet_New(dup);
2463 assert(PySet_Clear(dup2) == 0);
2464 assert(PySet_Size(dup2) == 0);
2465 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 /* Raise SystemError on clear or update of frozen set */
2468 f = PyFrozenSet_New(dup);
2469 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2470 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2471 assert(PySet_Add(f, elem) == 0);
2472 Py_INCREF(f);
2473 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2474 Py_DECREF(f);
2475 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 /* Exercise direct iteration */
2478 i = 0, count = 0;
2479 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2480 s = _PyUnicode_AsString(x);
2481 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2482 count++;
2483 }
2484 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* Exercise updates */
2487 dup2 = PySet_New(NULL);
2488 assert(_PySet_Update(dup2, dup) == 0);
2489 assert(PySet_Size(dup2) == 3);
2490 assert(_PySet_Update(dup2, dup) == 0);
2491 assert(PySet_Size(dup2) == 3);
2492 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 /* Raise SystemError when self argument is not a set or frozenset. */
2495 t = PyTuple_New(0);
2496 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2497 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2498 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 /* Raise SystemError when self argument is not a set. */
2501 f = PyFrozenSet_New(dup);
2502 assert(PySet_Size(f) == 3);
2503 assert(PyFrozenSet_CheckExact(f));
2504 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2505 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2506 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 /* Raise KeyError when popping from an empty set */
2509 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2510 Py_DECREF(ob);
2511 assert(PySet_GET_SIZE(ob) == 0);
2512 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 /* Restore the set from the copy using the PyNumber API */
2515 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2516 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 /* Verify constructors accept NULL arguments */
2519 f = PySet_New(NULL);
2520 assert(f != NULL);
2521 assert(PySet_GET_SIZE(f) == 0);
2522 Py_DECREF(f);
2523 f = PyFrozenSet_New(NULL);
2524 assert(f != NULL);
2525 assert(PyFrozenSet_CheckExact(f));
2526 assert(PySet_GET_SIZE(f) == 0);
2527 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 Py_DECREF(elem);
2530 Py_DECREF(dup);
2531 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002532}
2533
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002534#undef assertRaises
2535
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002536#endif