blob: ac501b6000374afe8c8d65852bd756367f70bb9d [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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 if (entry->key == dummy)
95 freeslot = entry;
96 else {
97 if (entry->hash == hash) {
98 startkey = entry->key;
99 Py_INCREF(startkey);
100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
101 Py_DECREF(startkey);
102 if (cmp < 0)
103 return NULL;
104 if (table == so->table && entry->key == startkey) {
105 if (cmp > 0)
106 return entry;
107 }
108 else {
109 /* The compare did major nasty stuff to the
110 * set: start over.
111 */
112 return set_lookkey(so, key, hash);
113 }
114 }
115 freeslot = NULL;
116 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /* In the loop, key == dummy is by far (factor of 100s) the
119 least likely outcome, so test for that last. */
120 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700121 i = i * 5 + perturb + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 entry = &table[i & mask];
123 if (entry->key == NULL) {
124 if (freeslot != NULL)
125 entry = freeslot;
126 break;
127 }
128 if (entry->key == key)
129 break;
130 if (entry->hash == hash && entry->key != dummy) {
131 startkey = entry->key;
132 Py_INCREF(startkey);
133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
134 Py_DECREF(startkey);
135 if (cmp < 0)
136 return NULL;
137 if (table == so->table && entry->key == startkey) {
138 if (cmp > 0)
139 break;
140 }
141 else {
142 /* The compare did major nasty stuff to the
143 * set: start over.
144 */
145 return set_lookkey(so, key, hash);
146 }
147 }
148 else if (entry->key == dummy && freeslot == NULL)
149 freeslot = entry;
150 }
151 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152}
153
154/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000155 * Hacked up version of set_lookkey which can assume keys are always unicode;
156 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000157 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158 */
159static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200160set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000161{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200162 size_t i; /* Unsigned for defined overflow behavior. */
163 size_t perturb;
164 setentry *freeslot;
165 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200167 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 /* Make sure this function doesn't have to handle non-unicode keys,
170 including subclasses of str; e.g., one reason to subclass
171 strings is to override __eq__, and for speed we don't cater to
172 that here. */
173 if (!PyUnicode_CheckExact(key)) {
174 so->lookup = set_lookkey;
175 return set_lookkey(so, key, hash);
176 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100177 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 entry = &table[i];
179 if (entry->key == NULL || entry->key == key)
180 return entry;
181 if (entry->key == dummy)
182 freeslot = entry;
183 else {
184 if (entry->hash == hash && unicode_eq(entry->key, key))
185 return entry;
186 freeslot = NULL;
187 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 /* In the loop, key == dummy is by far (factor of 100s) the
190 least likely outcome, so test for that last. */
191 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700192 i = i * 5 + perturb + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 entry = &table[i & mask];
194 if (entry->key == NULL)
195 return freeslot == NULL ? entry : freeslot;
196 if (entry->key == key
197 || (entry->hash == hash
198 && entry->key != dummy
199 && unicode_eq(entry->key, key)))
200 return entry;
201 if (entry->key == dummy && freeslot == NULL)
202 freeslot = entry;
203 }
204 assert(0); /* NOT REACHED */
205 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206}
207
208/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211Eats a reference to key.
212*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200214set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000215{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200216 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 assert(so->lookup != NULL);
219 entry = so->lookup(so, key, hash);
220 if (entry == NULL)
221 return -1;
222 if (entry->key == NULL) {
223 /* UNUSED */
224 so->fill++;
225 entry->key = key;
226 entry->hash = hash;
227 so->used++;
228 } else if (entry->key == dummy) {
229 /* DUMMY */
230 entry->key = key;
231 entry->hash = hash;
232 so->used++;
233 Py_DECREF(dummy);
234 } else {
235 /* ACTIVE */
236 Py_DECREF(key);
237 }
238 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000239}
240
241/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000242Internal routine used by set_table_resize() to insert an item which is
243known to be absent from the set. This routine also assumes that
244the set contains no deleted entries. Besides the performance benefit,
245using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
246Note that no refcounts are changed by this routine; if needed, the caller
247is responsible for incref'ing `key`.
248*/
249static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200250set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000251{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200252 size_t i;
253 size_t perturb;
254 size_t mask = (size_t)so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200256 setentry *entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000257
Mark Dickinson57e683e2011-09-24 18:18:40 +0100258 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 entry = &table[i];
260 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700261 i = i * 5 + perturb + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 entry = &table[i & mask];
263 }
264 so->fill++;
265 entry->key = key;
266 entry->hash = hash;
267 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000268}
269
270/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000271Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000272keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000273actually be smaller than the old one.
274*/
275static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000276set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 Py_ssize_t newsize;
279 setentry *oldtable, *newtable, *entry;
280 Py_ssize_t i;
281 int is_oldtable_malloced;
282 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700283 PyObject *dummy_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Find the smallest table size > minused. */
288 for (newsize = PySet_MINSIZE;
289 newsize <= minused && newsize > 0;
290 newsize <<= 1)
291 ;
292 if (newsize <= 0) {
293 PyErr_NoMemory();
294 return -1;
295 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL);
300 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (newsize == PySet_MINSIZE) {
303 /* A large table is shrinking, or we can't get any smaller. */
304 newtable = so->smalltable;
305 if (newtable == oldtable) {
306 if (so->fill == so->used) {
307 /* No dummies, so no point doing anything. */
308 return 0;
309 }
310 /* We're not going to resize it, but rebuild the
311 table anyway to purge old dummy entries.
312 Subtle: This is *necessary* if fill==size,
313 as set_lookkey needs at least one virgin slot to
314 terminate failing searches. If fill < size, it's
315 merely desirable, as dummies slow searches. */
316 assert(so->fill > so->used);
317 memcpy(small_copy, oldtable, sizeof(small_copy));
318 oldtable = small_copy;
319 }
320 }
321 else {
322 newtable = PyMem_NEW(setentry, newsize);
323 if (newtable == NULL) {
324 PyErr_NoMemory();
325 return -1;
326 }
327 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* Make the set empty, using the new table. */
330 assert(newtable != oldtable);
331 so->table = newtable;
332 so->mask = newsize - 1;
333 memset(newtable, 0, sizeof(setentry) * newsize);
334 so->used = 0;
335 i = so->fill;
336 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700340 dummy_entry = dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 for (entry = oldtable; i > 0; entry++) {
342 if (entry->key == NULL) {
343 /* UNUSED */
344 ;
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700345 } else if (entry->key == dummy_entry) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 /* DUMMY */
347 --i;
348 assert(entry->key == dummy);
349 Py_DECREF(entry->key);
350 } else {
351 /* ACTIVE */
352 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000353 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 }
355 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 if (is_oldtable_malloced)
358 PyMem_DEL(oldtable);
359 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360}
361
Raymond Hettingerc991db22005-08-11 07:58:45 +0000362/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
363
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000364static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200365set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000366{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200367 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000368 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100369 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 assert(so->fill <= so->mask); /* at least one empty slot */
372 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000373 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100374 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000375 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 return -1;
377 }
378 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
379 return 0;
380 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000381}
382
383static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200384set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000385{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200386 Py_hash_t hash;
387 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200390 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 hash = PyObject_Hash(key);
392 if (hash == -1)
393 return -1;
394 }
395 assert(so->fill <= so->mask); /* at least one empty slot */
396 n_used = so->used;
397 Py_INCREF(key);
398 if (set_insert_key(so, key, hash) == -1) {
399 Py_DECREF(key);
400 return -1;
401 }
402 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
403 return 0;
404 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405}
406
407#define DISCARD_NOTFOUND 0
408#define DISCARD_FOUND 1
409
410static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000411set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200412{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000414
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000415 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (entry == NULL)
417 return -1;
418 if (entry->key == NULL || entry->key == dummy)
419 return DISCARD_NOTFOUND;
420 old_key = entry->key;
421 Py_INCREF(dummy);
422 entry->key = dummy;
423 so->used--;
424 Py_DECREF(old_key);
425 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000426}
427
428static int
429set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000430{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200431 Py_hash_t hash;
432 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200438 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 hash = PyObject_Hash(key);
440 if (hash == -1)
441 return -1;
442 }
443 entry = (so->lookup)(so, key, hash);
444 if (entry == NULL)
445 return -1;
446 if (entry->key == NULL || entry->key == dummy)
447 return DISCARD_NOTFOUND;
448 old_key = entry->key;
449 Py_INCREF(dummy);
450 entry->key = dummy;
451 so->used--;
452 Py_DECREF(old_key);
453 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454}
455
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000456static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457set_clear_internal(PySetObject *so)
458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 setentry *entry, *table;
460 int table_is_malloced;
461 Py_ssize_t fill;
462 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000463#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 Py_ssize_t i, n;
465 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 n = so->mask + 1;
468 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000469#endif
470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 table = so->table;
472 assert(table != NULL);
473 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 /* This is delicate. During the process of clearing the set,
476 * decrefs can cause the set to mutate. To avoid fatal confusion
477 * (voice of experience), we have to make the set empty before
478 * clearing the slots, and never refer to anything via so->ref while
479 * clearing.
480 */
481 fill = so->fill;
482 if (table_is_malloced)
483 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 else if (fill > 0) {
486 /* It's a small table with something that needs to be cleared.
487 * Afraid the only safe way is to copy the set entries into
488 * another small table first.
489 */
490 memcpy(small_copy, table, sizeof(small_copy));
491 table = small_copy;
492 EMPTY_TO_MINSIZE(so);
493 }
494 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 /* Now we can finally clear things. If C had refcounts, we could
497 * assert that the refcount on table is 1 now, i.e. that this function
498 * has unique access to it, so decref side-effects can't alter it.
499 */
500 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 assert(i < n);
503 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000504#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (entry->key) {
506 --fill;
507 Py_DECREF(entry->key);
508 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 else
511 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if (table_is_malloced)
516 PyMem_DEL(table);
517 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000518}
519
520/*
521 * Iterate over a set table. Use like so:
522 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000523 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000524 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000525 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000526 * while (set_next(yourset, &pos, &entry)) {
527 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000528 * }
529 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000530 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532 */
533static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000534set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_ssize_t i;
537 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200538 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 assert (PyAnySet_Check(so));
541 i = *pos_ptr;
542 assert(i >= 0);
543 table = so->table;
544 mask = so->mask;
545 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
546 i++;
547 *pos_ptr = i+1;
548 if (i > mask)
549 return 0;
550 assert(table[i].key != NULL);
551 *entry_ptr = &table[i];
552 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000553}
554
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000555static void
556set_dealloc(PySetObject *so)
557{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200558 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 Py_ssize_t fill = so->fill;
560 PyObject_GC_UnTrack(so);
561 Py_TRASHCAN_SAFE_BEGIN(so)
562 if (so->weakreflist != NULL)
563 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 for (entry = so->table; fill > 0; entry++) {
566 if (entry->key) {
567 --fill;
568 Py_DECREF(entry->key);
569 }
570 }
571 if (so->table != so->smalltable)
572 PyMem_DEL(so->table);
573 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
574 free_list[numfree++] = so;
575 else
576 Py_TYPE(so)->tp_free(so);
577 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000578}
579
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000580static PyObject *
581set_repr(PySetObject *so)
582{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200583 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (status != 0) {
587 if (status < 0)
588 return NULL;
589 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
590 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 /* shortcut for the empty set */
593 if (!so->used) {
594 Py_ReprLeave((PyObject*)so);
595 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
596 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 keys = PySequence_List((PyObject *)so);
599 if (keys == NULL)
600 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200602 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 listrepr = PyObject_Repr(keys);
604 Py_DECREF(keys);
605 if (listrepr == NULL)
606 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200607 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200609 if (tmp == NULL)
610 goto done;
611 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200612
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200613 if (Py_TYPE(so) != &PySet_Type)
614 result = PyUnicode_FromFormat("%s({%U})",
615 Py_TYPE(so)->tp_name,
616 listrepr);
617 else
618 result = PyUnicode_FromFormat("{%U}", listrepr);
619 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000620done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 Py_ReprLeave((PyObject*)so);
622 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000623}
624
Martin v. Löwis18e16552006-02-15 17:27:45 +0000625static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000626set_len(PyObject *so)
627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000629}
630
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000631static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000632set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000635 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100636 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200637 Py_ssize_t i;
638 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 assert (PyAnySet_Check(so));
641 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 other = (PySetObject*)otherset;
644 if (other == so || other->used == 0)
645 /* a.update(a) or a.update({}); nothing to do */
646 return 0;
647 /* Do one big resize at the start, rather than
648 * incrementally resizing as we insert new keys. Expect
649 * that there will be no (or few) overlapping keys.
650 */
651 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
652 if (set_table_resize(so, (so->used + other->used)*2) != 0)
653 return -1;
654 }
655 for (i = 0; i <= other->mask; i++) {
656 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000657 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100658 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000659 if (key != NULL &&
660 key != dummy) {
661 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100662 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000663 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 return -1;
665 }
666 }
667 }
668 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000669}
670
671static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000672set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000674 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200678 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 hash = PyObject_Hash(key);
680 if (hash == -1)
681 return -1;
682 }
683 entry = (so->lookup)(so, key, hash);
684 if (entry == NULL)
685 return -1;
686 key = entry->key;
687 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000688}
689
Raymond Hettingerc991db22005-08-11 07:58:45 +0000690static int
691set_contains_entry(PySetObject *so, setentry *entry)
692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyObject *key;
694 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000695
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000696 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 if (lu_entry == NULL)
698 return -1;
699 key = lu_entry->key;
700 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000701}
702
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000703static PyObject *
704set_pop(PySetObject *so)
705{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200706 Py_ssize_t i = 0;
707 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 assert (PyAnySet_Check(so));
711 if (so->used == 0) {
712 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
713 return NULL;
714 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 /* Set entry to "the first" unused or dummy set entry. We abuse
717 * the hash field of slot 0 to hold a search finger:
718 * If slot 0 has a value, use slot 0.
719 * Else slot 0 is being used to hold a search finger,
720 * and we use its hash value as the first index to look.
721 */
722 entry = &so->table[0];
723 if (entry->key == NULL || entry->key == dummy) {
724 i = entry->hash;
725 /* The hash field may be a real hash value, or it may be a
726 * legit search finger, or it may be a once-legit search
727 * finger that's out of bounds now because it wrapped around
728 * or the table shrunk -- simply make sure it's in bounds now.
729 */
730 if (i > so->mask || i < 1)
731 i = 1; /* skip slot 0 */
732 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
733 i++;
734 if (i > so->mask)
735 i = 1;
736 }
737 }
738 key = entry->key;
739 Py_INCREF(dummy);
740 entry->key = dummy;
741 so->used--;
742 so->table[0].hash = i + 1; /* next place to start */
743 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000744}
745
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000746PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
747Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000748
749static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000750set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 Py_ssize_t pos = 0;
753 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 while (set_next(so, &pos, &entry))
756 Py_VISIT(entry->key);
757 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000758}
759
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000760static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000761frozenset_hash(PyObject *self)
762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800764 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 setentry *entry;
766 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 if (so->hash != -1)
769 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000770
Mark Dickinson57e683e2011-09-24 18:18:40 +0100771 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 while (set_next(so, &pos, &entry)) {
773 /* Work to increase the bit dispersion for closely spaced hash
774 values. The is important because some use cases have many
775 combinations of a small number of elements with nearby
776 hashes so that many distinct combinations collapse to only
777 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000778 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800779 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800781 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800783 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 so->hash = hash;
785 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000786}
787
Raymond Hettingera9d99362005-08-05 00:01:15 +0000788/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 PyObject_HEAD
792 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
793 Py_ssize_t si_used;
794 Py_ssize_t si_pos;
795 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796} setiterobject;
797
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000798static void
799setiter_dealloc(setiterobject *si)
800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 Py_XDECREF(si->si_set);
802 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000803}
804
805static int
806setiter_traverse(setiterobject *si, visitproc visit, void *arg)
807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 Py_VISIT(si->si_set);
809 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810}
811
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000812static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813setiter_len(setiterobject *si)
814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 Py_ssize_t len = 0;
816 if (si->si_set != NULL && si->si_used == si->si_set->used)
817 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000818 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000819}
820
Armin Rigof5b3e362006-02-11 21:32:43 +0000821PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000822
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000823static PyObject *setiter_iternext(setiterobject *si);
824
825static PyObject *
826setiter_reduce(setiterobject *si)
827{
828 PyObject *list;
829 setiterobject tmp;
830
831 list = PyList_New(0);
832 if (!list)
833 return NULL;
834
Ezio Melotti0e1af282012-09-28 16:43:40 +0300835 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000836 tmp = *si;
837 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300838
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000839 /* iterate the temporary into a list */
840 for(;;) {
841 PyObject *element = setiter_iternext(&tmp);
842 if (element) {
843 if (PyList_Append(list, element)) {
844 Py_DECREF(element);
845 Py_DECREF(list);
846 Py_XDECREF(tmp.si_set);
847 return NULL;
848 }
849 Py_DECREF(element);
850 } else
851 break;
852 }
853 Py_XDECREF(tmp.si_set);
854 /* check for error */
855 if (tmp.si_set != NULL) {
856 /* we have an error */
857 Py_DECREF(list);
858 return NULL;
859 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200860 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000861}
862
863PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
864
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000865static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000867 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000869};
870
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000871static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200874 Py_ssize_t i, mask;
875 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (so == NULL)
879 return NULL;
880 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 if (si->si_used != so->used) {
883 PyErr_SetString(PyExc_RuntimeError,
884 "Set changed size during iteration");
885 si->si_used = -1; /* Make this state sticky */
886 return NULL;
887 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 i = si->si_pos;
890 assert(i>=0);
891 entry = so->table;
892 mask = so->mask;
893 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
894 i++;
895 si->si_pos = i+1;
896 if (i > mask)
897 goto fail;
898 si->len--;
899 key = entry[i].key;
900 Py_INCREF(key);
901 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000902
903fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 Py_DECREF(so);
905 si->si_set = NULL;
906 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000907}
908
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000909PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 PyVarObject_HEAD_INIT(&PyType_Type, 0)
911 "set_iterator", /* tp_name */
912 sizeof(setiterobject), /* tp_basicsize */
913 0, /* tp_itemsize */
914 /* methods */
915 (destructor)setiter_dealloc, /* tp_dealloc */
916 0, /* tp_print */
917 0, /* tp_getattr */
918 0, /* tp_setattr */
919 0, /* tp_reserved */
920 0, /* tp_repr */
921 0, /* tp_as_number */
922 0, /* tp_as_sequence */
923 0, /* tp_as_mapping */
924 0, /* tp_hash */
925 0, /* tp_call */
926 0, /* tp_str */
927 PyObject_GenericGetAttr, /* tp_getattro */
928 0, /* tp_setattro */
929 0, /* tp_as_buffer */
930 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
931 0, /* tp_doc */
932 (traverseproc)setiter_traverse, /* tp_traverse */
933 0, /* tp_clear */
934 0, /* tp_richcompare */
935 0, /* tp_weaklistoffset */
936 PyObject_SelfIter, /* tp_iter */
937 (iternextfunc)setiter_iternext, /* tp_iternext */
938 setiter_methods, /* tp_methods */
939 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000940};
941
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000942static PyObject *
943set_iter(PySetObject *so)
944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
946 if (si == NULL)
947 return NULL;
948 Py_INCREF(so);
949 si->si_set = so;
950 si->si_used = so->used;
951 si->si_pos = 0;
952 si->len = so->used;
953 _PyObject_GC_TRACK(si);
954 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000955}
956
Raymond Hettingerd7946662005-08-01 21:39:29 +0000957static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000958set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (PyAnySet_Check(other))
963 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 if (PyDict_CheckExact(other)) {
966 PyObject *value;
967 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000968 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 /* Do one big resize at the start, rather than
972 * incrementally resizing as we insert new keys. Expect
973 * that there will be no (or few) overlapping keys.
974 */
975 if (dictsize == -1)
976 return -1;
977 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
978 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
979 return -1;
980 }
981 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
982 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 an_entry.hash = hash;
985 an_entry.key = key;
986 if (set_add_entry(so, &an_entry) == -1)
987 return -1;
988 }
989 return 0;
990 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 it = PyObject_GetIter(other);
993 if (it == NULL)
994 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 while ((key = PyIter_Next(it)) != NULL) {
997 if (set_add_key(so, key) == -1) {
998 Py_DECREF(it);
999 Py_DECREF(key);
1000 return -1;
1001 }
1002 Py_DECREF(key);
1003 }
1004 Py_DECREF(it);
1005 if (PyErr_Occurred())
1006 return -1;
1007 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001008}
1009
1010static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001011set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1016 PyObject *other = PyTuple_GET_ITEM(args, i);
1017 if (set_update_internal(so, other) == -1)
1018 return NULL;
1019 }
1020 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001021}
1022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001024"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001025
1026static PyObject *
1027make_new_set(PyTypeObject *type, PyObject *iterable)
1028{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001029 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 if (dummy == NULL) { /* Auto-initialize dummy */
1032 dummy = PyUnicode_FromString("<dummy key>");
1033 if (dummy == NULL)
1034 return NULL;
1035 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 /* create PySetObject structure */
1038 if (numfree &&
1039 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1040 so = free_list[--numfree];
1041 assert (so != NULL && PyAnySet_CheckExact(so));
1042 Py_TYPE(so) = type;
1043 _Py_NewReference((PyObject *)so);
1044 EMPTY_TO_MINSIZE(so);
1045 PyObject_GC_Track(so);
1046 } else {
1047 so = (PySetObject *)type->tp_alloc(type, 0);
1048 if (so == NULL)
1049 return NULL;
1050 /* tp_alloc has already zeroed the structure */
1051 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1052 INIT_NONZERO_SET_SLOTS(so);
1053 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 so->lookup = set_lookkey_unicode;
1056 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 if (iterable != NULL) {
1059 if (set_update_internal(so, iterable) == -1) {
1060 Py_DECREF(so);
1061 return NULL;
1062 }
1063 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001066}
1067
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001068static PyObject *
1069make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1072 if (PyType_IsSubtype(type, &PySet_Type))
1073 type = &PySet_Type;
1074 else
1075 type = &PyFrozenSet_Type;
1076 }
1077 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001078}
1079
Raymond Hettingerd7946662005-08-01 21:39:29 +00001080/* The empty frozenset is a singleton */
1081static PyObject *emptyfrozenset = NULL;
1082
Raymond Hettingera690a992003-11-16 16:17:49 +00001083static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001084frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1089 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1092 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (type != &PyFrozenSet_Type)
1095 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (iterable != NULL) {
1098 /* frozenset(f) is idempotent */
1099 if (PyFrozenSet_CheckExact(iterable)) {
1100 Py_INCREF(iterable);
1101 return iterable;
1102 }
1103 result = make_new_set(type, iterable);
1104 if (result == NULL || PySet_GET_SIZE(result))
1105 return result;
1106 Py_DECREF(result);
1107 }
1108 /* The empty frozenset is a singleton */
1109 if (emptyfrozenset == NULL)
1110 emptyfrozenset = make_new_set(type, NULL);
1111 Py_XINCREF(emptyfrozenset);
1112 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001113}
1114
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001115int
1116PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001117{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001118 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 while (numfree) {
1122 numfree--;
1123 so = free_list[numfree];
1124 PyObject_GC_Del(so);
1125 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001126 return freelist_size;
1127}
1128
1129void
1130PySet_Fini(void)
1131{
1132 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 Py_CLEAR(dummy);
1134 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001135}
1136
David Malcolm49526f42012-06-22 14:55:41 -04001137/* Print summary info about the state of the optimized allocator */
1138void
1139_PySet_DebugMallocStats(FILE *out)
1140{
1141 _PyDebugAllocatorStats(out,
1142 "free PySetObject",
1143 numfree, sizeof(PySetObject));
1144}
1145
1146
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001147static PyObject *
1148set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1151 return NULL;
1152
1153 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001154}
1155
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001156/* set_swap_bodies() switches the contents of any two sets by moving their
1157 internal data pointers and, if needed, copying the internal smalltables.
1158 Semantically equivalent to:
1159
1160 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1161
1162 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001164 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001166 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001167*/
1168
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001169static void
1170set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 Py_ssize_t t;
1173 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001174 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001176 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 t = a->fill; a->fill = b->fill; b->fill = t;
1179 t = a->used; a->used = b->used; b->used = t;
1180 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 u = a->table;
1183 if (a->table == a->smalltable)
1184 u = b->smalltable;
1185 a->table = b->table;
1186 if (b->table == b->smalltable)
1187 a->table = a->smalltable;
1188 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 if (a->table == a->smalltable || b->table == b->smalltable) {
1193 memcpy(tab, a->smalltable, sizeof(tab));
1194 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1195 memcpy(b->smalltable, tab, sizeof(tab));
1196 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1199 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1200 h = a->hash; a->hash = b->hash; b->hash = h;
1201 } else {
1202 a->hash = -1;
1203 b->hash = -1;
1204 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001205}
1206
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001207static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001208set_copy(PySetObject *so)
1209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001211}
1212
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001213static PyObject *
1214frozenset_copy(PySetObject *so)
1215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if (PyFrozenSet_CheckExact(so)) {
1217 Py_INCREF(so);
1218 return (PyObject *)so;
1219 }
1220 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001221}
1222
Raymond Hettingera690a992003-11-16 16:17:49 +00001223PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1224
1225static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001226set_clear(PySetObject *so)
1227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 set_clear_internal(so);
1229 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001230}
1231
1232PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1233
1234static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001235set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 PySetObject *result;
1238 PyObject *other;
1239 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 result = (PySetObject *)set_copy(so);
1242 if (result == NULL)
1243 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1246 other = PyTuple_GET_ITEM(args, i);
1247 if ((PyObject *)so == other)
1248 continue;
1249 if (set_update_internal(result, other) == -1) {
1250 Py_DECREF(result);
1251 return NULL;
1252 }
1253 }
1254 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001255}
1256
1257PyDoc_STRVAR(union_doc,
1258 "Return the union of sets as a new set.\n\
1259\n\
1260(i.e. all elements that are in either set.)");
1261
1262static PyObject *
1263set_or(PySetObject *so, PyObject *other)
1264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001266
Brian Curtindfc80e32011-08-10 20:28:54 -05001267 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1268 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 result = (PySetObject *)set_copy(so);
1271 if (result == NULL)
1272 return NULL;
1273 if ((PyObject *)so == other)
1274 return (PyObject *)result;
1275 if (set_update_internal(result, other) == -1) {
1276 Py_DECREF(result);
1277 return NULL;
1278 }
1279 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001280}
1281
Raymond Hettingera690a992003-11-16 16:17:49 +00001282static PyObject *
1283set_ior(PySetObject *so, PyObject *other)
1284{
Brian Curtindfc80e32011-08-10 20:28:54 -05001285 if (!PyAnySet_Check(other))
1286 Py_RETURN_NOTIMPLEMENTED;
1287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (set_update_internal(so, other) == -1)
1289 return NULL;
1290 Py_INCREF(so);
1291 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001292}
1293
1294static PyObject *
1295set_intersection(PySetObject *so, PyObject *other)
1296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 PySetObject *result;
1298 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 if ((PyObject *)so == other)
1301 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1304 if (result == NULL)
1305 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (PyAnySet_Check(other)) {
1308 Py_ssize_t pos = 0;
1309 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1312 tmp = (PyObject *)so;
1313 so = (PySetObject *)other;
1314 other = tmp;
1315 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 while (set_next((PySetObject *)other, &pos, &entry)) {
1318 int rv = set_contains_entry(so, entry);
1319 if (rv == -1) {
1320 Py_DECREF(result);
1321 return NULL;
1322 }
1323 if (rv) {
1324 if (set_add_entry(result, entry) == -1) {
1325 Py_DECREF(result);
1326 return NULL;
1327 }
1328 }
1329 }
1330 return (PyObject *)result;
1331 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 it = PyObject_GetIter(other);
1334 if (it == NULL) {
1335 Py_DECREF(result);
1336 return NULL;
1337 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 while ((key = PyIter_Next(it)) != NULL) {
1340 int rv;
1341 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001342 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (hash == -1) {
1345 Py_DECREF(it);
1346 Py_DECREF(result);
1347 Py_DECREF(key);
1348 return NULL;
1349 }
1350 entry.hash = hash;
1351 entry.key = key;
1352 rv = set_contains_entry(so, &entry);
1353 if (rv == -1) {
1354 Py_DECREF(it);
1355 Py_DECREF(result);
1356 Py_DECREF(key);
1357 return NULL;
1358 }
1359 if (rv) {
1360 if (set_add_entry(result, &entry) == -1) {
1361 Py_DECREF(it);
1362 Py_DECREF(result);
1363 Py_DECREF(key);
1364 return NULL;
1365 }
1366 }
1367 Py_DECREF(key);
1368 }
1369 Py_DECREF(it);
1370 if (PyErr_Occurred()) {
1371 Py_DECREF(result);
1372 return NULL;
1373 }
1374 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001375}
1376
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001377static PyObject *
1378set_intersection_multi(PySetObject *so, PyObject *args)
1379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 Py_ssize_t i;
1381 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 if (PyTuple_GET_SIZE(args) == 0)
1384 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 Py_INCREF(so);
1387 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1388 PyObject *other = PyTuple_GET_ITEM(args, i);
1389 PyObject *newresult = set_intersection((PySetObject *)result, other);
1390 if (newresult == NULL) {
1391 Py_DECREF(result);
1392 return NULL;
1393 }
1394 Py_DECREF(result);
1395 result = newresult;
1396 }
1397 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001398}
1399
Raymond Hettingera690a992003-11-16 16:17:49 +00001400PyDoc_STRVAR(intersection_doc,
1401"Return the intersection of two sets as a new set.\n\
1402\n\
1403(i.e. all elements that are in both sets.)");
1404
1405static PyObject *
1406set_intersection_update(PySetObject *so, PyObject *other)
1407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 tmp = set_intersection(so, other);
1411 if (tmp == NULL)
1412 return NULL;
1413 set_swap_bodies(so, (PySetObject *)tmp);
1414 Py_DECREF(tmp);
1415 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001416}
1417
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001418static PyObject *
1419set_intersection_update_multi(PySetObject *so, PyObject *args)
1420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 tmp = set_intersection_multi(so, args);
1424 if (tmp == NULL)
1425 return NULL;
1426 set_swap_bodies(so, (PySetObject *)tmp);
1427 Py_DECREF(tmp);
1428 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001429}
1430
Raymond Hettingera690a992003-11-16 16:17:49 +00001431PyDoc_STRVAR(intersection_update_doc,
1432"Update a set with the intersection of itself and another.");
1433
1434static PyObject *
1435set_and(PySetObject *so, PyObject *other)
1436{
Brian Curtindfc80e32011-08-10 20:28:54 -05001437 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1438 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001440}
1441
1442static PyObject *
1443set_iand(PySetObject *so, PyObject *other)
1444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001446
Brian Curtindfc80e32011-08-10 20:28:54 -05001447 if (!PyAnySet_Check(other))
1448 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 result = set_intersection_update(so, other);
1450 if (result == NULL)
1451 return NULL;
1452 Py_DECREF(result);
1453 Py_INCREF(so);
1454 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001455}
1456
Guido van Rossum58da9312007-11-10 23:39:45 +00001457static PyObject *
1458set_isdisjoint(PySetObject *so, PyObject *other)
1459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 if ((PyObject *)so == other) {
1463 if (PySet_GET_SIZE(so) == 0)
1464 Py_RETURN_TRUE;
1465 else
1466 Py_RETURN_FALSE;
1467 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 if (PyAnySet_CheckExact(other)) {
1470 Py_ssize_t pos = 0;
1471 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1474 tmp = (PyObject *)so;
1475 so = (PySetObject *)other;
1476 other = tmp;
1477 }
1478 while (set_next((PySetObject *)other, &pos, &entry)) {
1479 int rv = set_contains_entry(so, entry);
1480 if (rv == -1)
1481 return NULL;
1482 if (rv)
1483 Py_RETURN_FALSE;
1484 }
1485 Py_RETURN_TRUE;
1486 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 it = PyObject_GetIter(other);
1489 if (it == NULL)
1490 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 while ((key = PyIter_Next(it)) != NULL) {
1493 int rv;
1494 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001495 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (hash == -1) {
1498 Py_DECREF(key);
1499 Py_DECREF(it);
1500 return NULL;
1501 }
1502 entry.hash = hash;
1503 entry.key = key;
1504 rv = set_contains_entry(so, &entry);
1505 Py_DECREF(key);
1506 if (rv == -1) {
1507 Py_DECREF(it);
1508 return NULL;
1509 }
1510 if (rv) {
1511 Py_DECREF(it);
1512 Py_RETURN_FALSE;
1513 }
1514 }
1515 Py_DECREF(it);
1516 if (PyErr_Occurred())
1517 return NULL;
1518 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001519}
1520
1521PyDoc_STRVAR(isdisjoint_doc,
1522"Return True if two sets have a null intersection.");
1523
Neal Norwitz6576bd82005-11-13 18:41:28 +00001524static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001525set_difference_update_internal(PySetObject *so, PyObject *other)
1526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 if ((PyObject *)so == other)
1528 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 if (PyAnySet_Check(other)) {
1531 setentry *entry;
1532 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 while (set_next((PySetObject *)other, &pos, &entry))
1535 if (set_discard_entry(so, entry) == -1)
1536 return -1;
1537 } else {
1538 PyObject *key, *it;
1539 it = PyObject_GetIter(other);
1540 if (it == NULL)
1541 return -1;
1542
1543 while ((key = PyIter_Next(it)) != NULL) {
1544 if (set_discard_key(so, key) == -1) {
1545 Py_DECREF(it);
1546 Py_DECREF(key);
1547 return -1;
1548 }
1549 Py_DECREF(key);
1550 }
1551 Py_DECREF(it);
1552 if (PyErr_Occurred())
1553 return -1;
1554 }
1555 /* If more than 1/5 are dummies, then resize them away. */
1556 if ((so->fill - so->used) * 5 < so->mask)
1557 return 0;
1558 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001559}
1560
Raymond Hettingera690a992003-11-16 16:17:49 +00001561static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001562set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1567 PyObject *other = PyTuple_GET_ITEM(args, i);
1568 if (set_difference_update_internal(so, other) == -1)
1569 return NULL;
1570 }
1571 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001572}
1573
1574PyDoc_STRVAR(difference_update_doc,
1575"Remove all elements of another set from this set.");
1576
1577static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001578set_copy_and_difference(PySetObject *so, PyObject *other)
1579{
1580 PyObject *result;
1581
1582 result = set_copy(so);
1583 if (result == NULL)
1584 return NULL;
1585 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1586 return result;
1587 Py_DECREF(result);
1588 return NULL;
1589}
1590
1591static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001592set_difference(PySetObject *so, PyObject *other)
1593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 PyObject *result;
1595 setentry *entry;
1596 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001599 return set_copy_and_difference(so, other);
1600 }
1601
1602 /* If len(so) much more than len(other), it's more efficient to simply copy
1603 * so and then iterate other looking for common elements. */
1604 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1605 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 result = make_new_set_basetype(Py_TYPE(so), NULL);
1609 if (result == NULL)
1610 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (PyDict_CheckExact(other)) {
1613 while (set_next(so, &pos, &entry)) {
1614 setentry entrycopy;
1615 entrycopy.hash = entry->hash;
1616 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001617 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1619 Py_DECREF(result);
1620 return NULL;
1621 }
1622 }
1623 }
1624 return result;
1625 }
1626
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001627 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 while (set_next(so, &pos, &entry)) {
1629 int rv = set_contains_entry((PySetObject *)other, entry);
1630 if (rv == -1) {
1631 Py_DECREF(result);
1632 return NULL;
1633 }
1634 if (!rv) {
1635 if (set_add_entry((PySetObject *)result, entry) == -1) {
1636 Py_DECREF(result);
1637 return NULL;
1638 }
1639 }
1640 }
1641 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001642}
1643
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001644static PyObject *
1645set_difference_multi(PySetObject *so, PyObject *args)
1646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 Py_ssize_t i;
1648 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 if (PyTuple_GET_SIZE(args) == 0)
1651 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 other = PyTuple_GET_ITEM(args, 0);
1654 result = set_difference(so, other);
1655 if (result == NULL)
1656 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1659 other = PyTuple_GET_ITEM(args, i);
1660 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1661 Py_DECREF(result);
1662 return NULL;
1663 }
1664 }
1665 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001666}
1667
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001668PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001669"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001670\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001671(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001672static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001673set_sub(PySetObject *so, PyObject *other)
1674{
Brian Curtindfc80e32011-08-10 20:28:54 -05001675 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1676 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001678}
1679
1680static PyObject *
1681set_isub(PySetObject *so, PyObject *other)
1682{
Brian Curtindfc80e32011-08-10 20:28:54 -05001683 if (!PyAnySet_Check(other))
1684 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 if (set_difference_update_internal(so, other) == -1)
1686 return NULL;
1687 Py_INCREF(so);
1688 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001689}
1690
1691static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001692set_symmetric_difference_update(PySetObject *so, PyObject *other)
1693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 PySetObject *otherset;
1695 PyObject *key;
1696 Py_ssize_t pos = 0;
1697 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 if ((PyObject *)so == other)
1700 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 if (PyDict_CheckExact(other)) {
1703 PyObject *value;
1704 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001705 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1707 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001708
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001709 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 an_entry.hash = hash;
1711 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001714 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001715 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001718 if (rv == DISCARD_NOTFOUND) {
1719 if (set_add_entry(so, &an_entry) == -1) {
1720 Py_DECREF(key);
1721 return NULL;
1722 }
1723 }
Georg Brandl2d444492010-09-03 10:52:55 +00001724 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 }
1726 Py_RETURN_NONE;
1727 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 if (PyAnySet_Check(other)) {
1730 Py_INCREF(other);
1731 otherset = (PySetObject *)other;
1732 } else {
1733 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1734 if (otherset == NULL)
1735 return NULL;
1736 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 while (set_next(otherset, &pos, &entry)) {
1739 int rv = set_discard_entry(so, entry);
1740 if (rv == -1) {
1741 Py_DECREF(otherset);
1742 return NULL;
1743 }
1744 if (rv == DISCARD_NOTFOUND) {
1745 if (set_add_entry(so, entry) == -1) {
1746 Py_DECREF(otherset);
1747 return NULL;
1748 }
1749 }
1750 }
1751 Py_DECREF(otherset);
1752 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001753}
1754
1755PyDoc_STRVAR(symmetric_difference_update_doc,
1756"Update a set with the symmetric difference of itself and another.");
1757
1758static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001759set_symmetric_difference(PySetObject *so, PyObject *other)
1760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 PyObject *rv;
1762 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1765 if (otherset == NULL)
1766 return NULL;
1767 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1768 if (rv == NULL)
1769 return NULL;
1770 Py_DECREF(rv);
1771 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001772}
1773
1774PyDoc_STRVAR(symmetric_difference_doc,
1775"Return the symmetric difference of two sets as a new set.\n\
1776\n\
1777(i.e. all elements that are in exactly one of the sets.)");
1778
1779static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001780set_xor(PySetObject *so, PyObject *other)
1781{
Brian Curtindfc80e32011-08-10 20:28:54 -05001782 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1783 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001785}
1786
1787static PyObject *
1788set_ixor(PySetObject *so, PyObject *other)
1789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001791
Brian Curtindfc80e32011-08-10 20:28:54 -05001792 if (!PyAnySet_Check(other))
1793 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 result = set_symmetric_difference_update(so, other);
1795 if (result == NULL)
1796 return NULL;
1797 Py_DECREF(result);
1798 Py_INCREF(so);
1799 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001800}
1801
1802static PyObject *
1803set_issubset(PySetObject *so, PyObject *other)
1804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 setentry *entry;
1806 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 if (!PyAnySet_Check(other)) {
1809 PyObject *tmp, *result;
1810 tmp = make_new_set(&PySet_Type, other);
1811 if (tmp == NULL)
1812 return NULL;
1813 result = set_issubset(so, tmp);
1814 Py_DECREF(tmp);
1815 return result;
1816 }
1817 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1818 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 while (set_next(so, &pos, &entry)) {
1821 int rv = set_contains_entry((PySetObject *)other, entry);
1822 if (rv == -1)
1823 return NULL;
1824 if (!rv)
1825 Py_RETURN_FALSE;
1826 }
1827 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001828}
1829
1830PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1831
1832static PyObject *
1833set_issuperset(PySetObject *so, PyObject *other)
1834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 if (!PyAnySet_Check(other)) {
1838 tmp = make_new_set(&PySet_Type, other);
1839 if (tmp == NULL)
1840 return NULL;
1841 result = set_issuperset(so, tmp);
1842 Py_DECREF(tmp);
1843 return result;
1844 }
1845 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001846}
1847
1848PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1849
Raymond Hettingera690a992003-11-16 16:17:49 +00001850static PyObject *
1851set_richcompare(PySetObject *v, PyObject *w, int op)
1852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001854
Brian Curtindfc80e32011-08-10 20:28:54 -05001855 if(!PyAnySet_Check(w))
1856 Py_RETURN_NOTIMPLEMENTED;
1857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 switch (op) {
1859 case Py_EQ:
1860 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1861 Py_RETURN_FALSE;
1862 if (v->hash != -1 &&
1863 ((PySetObject *)w)->hash != -1 &&
1864 v->hash != ((PySetObject *)w)->hash)
1865 Py_RETURN_FALSE;
1866 return set_issubset(v, w);
1867 case Py_NE:
1868 r1 = set_richcompare(v, w, Py_EQ);
1869 if (r1 == NULL)
1870 return NULL;
1871 r2 = PyBool_FromLong(PyObject_Not(r1));
1872 Py_DECREF(r1);
1873 return r2;
1874 case Py_LE:
1875 return set_issubset(v, w);
1876 case Py_GE:
1877 return set_issuperset(v, w);
1878 case Py_LT:
1879 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1880 Py_RETURN_FALSE;
1881 return set_issubset(v, w);
1882 case Py_GT:
1883 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1884 Py_RETURN_FALSE;
1885 return set_issuperset(v, w);
1886 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001887 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001888}
1889
Raymond Hettingera690a992003-11-16 16:17:49 +00001890static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001891set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 if (set_add_key(so, key) == -1)
1894 return NULL;
1895 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001896}
1897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001899"Add an element to a set.\n\
1900\n\
1901This has no effect if the element is already present.");
1902
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001903static int
1904set_contains(PySetObject *so, PyObject *key)
1905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 PyObject *tmpkey;
1907 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 rv = set_contains_key(so, key);
1910 if (rv == -1) {
1911 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1912 return -1;
1913 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001914 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (tmpkey == NULL)
1916 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001917 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 Py_DECREF(tmpkey);
1919 }
1920 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001921}
1922
1923static PyObject *
1924set_direct_contains(PySetObject *so, PyObject *key)
1925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 result = set_contains(so, key);
1929 if (result == -1)
1930 return NULL;
1931 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001932}
1933
1934PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1935
Raymond Hettingera690a992003-11-16 16:17:49 +00001936static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001937set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 PyObject *tmpkey;
1940 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 rv = set_discard_key(so, key);
1943 if (rv == -1) {
1944 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1945 return NULL;
1946 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001947 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 if (tmpkey == NULL)
1949 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 Py_DECREF(tmpkey);
1952 if (rv == -1)
1953 return NULL;
1954 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 if (rv == DISCARD_NOTFOUND) {
1957 set_key_error(key);
1958 return NULL;
1959 }
1960 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001961}
1962
1963PyDoc_STRVAR(remove_doc,
1964"Remove an element from a set; it must be a member.\n\
1965\n\
1966If the element is not a member, raise a KeyError.");
1967
1968static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001969set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001970{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001971 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 rv = set_discard_key(so, key);
1975 if (rv == -1) {
1976 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1977 return NULL;
1978 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001979 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 if (tmpkey == NULL)
1981 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001982 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001984 if (rv == -1)
1985 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 }
1987 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001988}
1989
1990PyDoc_STRVAR(discard_doc,
1991"Remove an element from a set if it is a member.\n\
1992\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001994
1995static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001996set_reduce(PySetObject *so)
1997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001999 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00002000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 keys = PySequence_List((PyObject *)so);
2002 if (keys == NULL)
2003 goto done;
2004 args = PyTuple_Pack(1, keys);
2005 if (args == NULL)
2006 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002007 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (dict == NULL) {
2009 PyErr_Clear();
2010 dict = Py_None;
2011 Py_INCREF(dict);
2012 }
2013 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002014done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 Py_XDECREF(args);
2016 Py_XDECREF(keys);
2017 Py_XDECREF(dict);
2018 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002019}
2020
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002021static PyObject *
2022set_sizeof(PySetObject *so)
2023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 res = sizeof(PySetObject);
2027 if (so->table != so->smalltable)
2028 res = res + (so->mask + 1) * sizeof(setentry);
2029 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002030}
2031
2032PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002033static int
2034set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 if (!PyAnySet_Check(self))
2039 return -1;
2040 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2041 return -1;
2042 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2043 return -1;
2044 set_clear_internal(self);
2045 self->hash = -1;
2046 if (iterable == NULL)
2047 return 0;
2048 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002049}
2050
Raymond Hettingera690a992003-11-16 16:17:49 +00002051static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 set_len, /* sq_length */
2053 0, /* sq_concat */
2054 0, /* sq_repeat */
2055 0, /* sq_item */
2056 0, /* sq_slice */
2057 0, /* sq_ass_item */
2058 0, /* sq_ass_slice */
2059 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002060};
2061
2062/* set object ********************************************************/
2063
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002064#ifdef Py_DEBUG
2065static PyObject *test_c_api(PySetObject *so);
2066
2067PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2068All is well if assertions don't fail.");
2069#endif
2070
Raymond Hettingera690a992003-11-16 16:17:49 +00002071static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 {"add", (PyCFunction)set_add, METH_O,
2073 add_doc},
2074 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2075 clear_doc},
2076 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2077 contains_doc},
2078 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2079 copy_doc},
2080 {"discard", (PyCFunction)set_discard, METH_O,
2081 discard_doc},
2082 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2083 difference_doc},
2084 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2085 difference_update_doc},
2086 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2087 intersection_doc},
2088 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2089 intersection_update_doc},
2090 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2091 isdisjoint_doc},
2092 {"issubset", (PyCFunction)set_issubset, METH_O,
2093 issubset_doc},
2094 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2095 issuperset_doc},
2096 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2097 pop_doc},
2098 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2099 reduce_doc},
2100 {"remove", (PyCFunction)set_remove, METH_O,
2101 remove_doc},
2102 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2103 sizeof_doc},
2104 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2105 symmetric_difference_doc},
2106 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2107 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002108#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2110 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002111#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 {"union", (PyCFunction)set_union, METH_VARARGS,
2113 union_doc},
2114 {"update", (PyCFunction)set_update, METH_VARARGS,
2115 update_doc},
2116 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002117};
2118
2119static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 0, /*nb_add*/
2121 (binaryfunc)set_sub, /*nb_subtract*/
2122 0, /*nb_multiply*/
2123 0, /*nb_remainder*/
2124 0, /*nb_divmod*/
2125 0, /*nb_power*/
2126 0, /*nb_negative*/
2127 0, /*nb_positive*/
2128 0, /*nb_absolute*/
2129 0, /*nb_bool*/
2130 0, /*nb_invert*/
2131 0, /*nb_lshift*/
2132 0, /*nb_rshift*/
2133 (binaryfunc)set_and, /*nb_and*/
2134 (binaryfunc)set_xor, /*nb_xor*/
2135 (binaryfunc)set_or, /*nb_or*/
2136 0, /*nb_int*/
2137 0, /*nb_reserved*/
2138 0, /*nb_float*/
2139 0, /*nb_inplace_add*/
2140 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2141 0, /*nb_inplace_multiply*/
2142 0, /*nb_inplace_remainder*/
2143 0, /*nb_inplace_power*/
2144 0, /*nb_inplace_lshift*/
2145 0, /*nb_inplace_rshift*/
2146 (binaryfunc)set_iand, /*nb_inplace_and*/
2147 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2148 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002149};
2150
2151PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002152"set() -> new empty set object\n\
2153set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002154\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002155Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002156
2157PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2159 "set", /* tp_name */
2160 sizeof(PySetObject), /* tp_basicsize */
2161 0, /* tp_itemsize */
2162 /* methods */
2163 (destructor)set_dealloc, /* tp_dealloc */
2164 0, /* tp_print */
2165 0, /* tp_getattr */
2166 0, /* tp_setattr */
2167 0, /* tp_reserved */
2168 (reprfunc)set_repr, /* tp_repr */
2169 &set_as_number, /* tp_as_number */
2170 &set_as_sequence, /* tp_as_sequence */
2171 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002172 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 0, /* tp_call */
2174 0, /* tp_str */
2175 PyObject_GenericGetAttr, /* tp_getattro */
2176 0, /* tp_setattro */
2177 0, /* tp_as_buffer */
2178 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2179 Py_TPFLAGS_BASETYPE, /* tp_flags */
2180 set_doc, /* tp_doc */
2181 (traverseproc)set_traverse, /* tp_traverse */
2182 (inquiry)set_clear_internal, /* tp_clear */
2183 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002184 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2185 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 0, /* tp_iternext */
2187 set_methods, /* tp_methods */
2188 0, /* tp_members */
2189 0, /* tp_getset */
2190 0, /* tp_base */
2191 0, /* tp_dict */
2192 0, /* tp_descr_get */
2193 0, /* tp_descr_set */
2194 0, /* tp_dictoffset */
2195 (initproc)set_init, /* tp_init */
2196 PyType_GenericAlloc, /* tp_alloc */
2197 set_new, /* tp_new */
2198 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002199};
2200
2201/* frozenset object ********************************************************/
2202
2203
2204static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2206 contains_doc},
2207 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2208 copy_doc},
2209 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2210 difference_doc},
2211 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2212 intersection_doc},
2213 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2214 isdisjoint_doc},
2215 {"issubset", (PyCFunction)set_issubset, METH_O,
2216 issubset_doc},
2217 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2218 issuperset_doc},
2219 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2220 reduce_doc},
2221 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2222 sizeof_doc},
2223 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2224 symmetric_difference_doc},
2225 {"union", (PyCFunction)set_union, METH_VARARGS,
2226 union_doc},
2227 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002228};
2229
2230static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 0, /*nb_add*/
2232 (binaryfunc)set_sub, /*nb_subtract*/
2233 0, /*nb_multiply*/
2234 0, /*nb_remainder*/
2235 0, /*nb_divmod*/
2236 0, /*nb_power*/
2237 0, /*nb_negative*/
2238 0, /*nb_positive*/
2239 0, /*nb_absolute*/
2240 0, /*nb_bool*/
2241 0, /*nb_invert*/
2242 0, /*nb_lshift*/
2243 0, /*nb_rshift*/
2244 (binaryfunc)set_and, /*nb_and*/
2245 (binaryfunc)set_xor, /*nb_xor*/
2246 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002247};
2248
2249PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002250"frozenset() -> empty frozenset object\n\
2251frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002252\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002253Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002254
2255PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2257 "frozenset", /* tp_name */
2258 sizeof(PySetObject), /* tp_basicsize */
2259 0, /* tp_itemsize */
2260 /* methods */
2261 (destructor)set_dealloc, /* tp_dealloc */
2262 0, /* tp_print */
2263 0, /* tp_getattr */
2264 0, /* tp_setattr */
2265 0, /* tp_reserved */
2266 (reprfunc)set_repr, /* tp_repr */
2267 &frozenset_as_number, /* tp_as_number */
2268 &set_as_sequence, /* tp_as_sequence */
2269 0, /* tp_as_mapping */
2270 frozenset_hash, /* tp_hash */
2271 0, /* tp_call */
2272 0, /* tp_str */
2273 PyObject_GenericGetAttr, /* tp_getattro */
2274 0, /* tp_setattro */
2275 0, /* tp_as_buffer */
2276 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2277 Py_TPFLAGS_BASETYPE, /* tp_flags */
2278 frozenset_doc, /* tp_doc */
2279 (traverseproc)set_traverse, /* tp_traverse */
2280 (inquiry)set_clear_internal, /* tp_clear */
2281 (richcmpfunc)set_richcompare, /* tp_richcompare */
2282 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2283 (getiterfunc)set_iter, /* tp_iter */
2284 0, /* tp_iternext */
2285 frozenset_methods, /* tp_methods */
2286 0, /* tp_members */
2287 0, /* tp_getset */
2288 0, /* tp_base */
2289 0, /* tp_dict */
2290 0, /* tp_descr_get */
2291 0, /* tp_descr_set */
2292 0, /* tp_dictoffset */
2293 0, /* tp_init */
2294 PyType_GenericAlloc, /* tp_alloc */
2295 frozenset_new, /* tp_new */
2296 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002297};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002298
2299
2300/***** C API functions *************************************************/
2301
2302PyObject *
2303PySet_New(PyObject *iterable)
2304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002306}
2307
2308PyObject *
2309PyFrozenSet_New(PyObject *iterable)
2310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002312}
2313
Neal Norwitz8c49c822006-03-04 18:41:19 +00002314Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002315PySet_Size(PyObject *anyset)
2316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!PyAnySet_Check(anyset)) {
2318 PyErr_BadInternalCall();
2319 return -1;
2320 }
2321 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002322}
2323
2324int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002325PySet_Clear(PyObject *set)
2326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 if (!PySet_Check(set)) {
2328 PyErr_BadInternalCall();
2329 return -1;
2330 }
2331 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002332}
2333
2334int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002335PySet_Contains(PyObject *anyset, PyObject *key)
2336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 if (!PyAnySet_Check(anyset)) {
2338 PyErr_BadInternalCall();
2339 return -1;
2340 }
2341 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002342}
2343
2344int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002345PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 if (!PySet_Check(set)) {
2348 PyErr_BadInternalCall();
2349 return -1;
2350 }
2351 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002352}
2353
2354int
Christian Heimesfd66e512008-01-29 12:18:50 +00002355PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 if (!PySet_Check(anyset) &&
2358 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2359 PyErr_BadInternalCall();
2360 return -1;
2361 }
2362 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002363}
2364
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002365int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002366_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 if (!PyAnySet_Check(set)) {
2371 PyErr_BadInternalCall();
2372 return -1;
2373 }
2374 if (set_next((PySetObject *)set, pos, &entry) == 0)
2375 return 0;
2376 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002377 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002379}
2380
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002381PyObject *
2382PySet_Pop(PyObject *set)
2383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 if (!PySet_Check(set)) {
2385 PyErr_BadInternalCall();
2386 return NULL;
2387 }
2388 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002389}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002390
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002391int
2392_PySet_Update(PyObject *set, PyObject *iterable)
2393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 if (!PySet_Check(set)) {
2395 PyErr_BadInternalCall();
2396 return -1;
2397 }
2398 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002399}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002400
2401#ifdef Py_DEBUG
2402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002404 Returns True and original set is restored. */
2405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406#define assertRaises(call_return_value, exception) \
2407 do { \
2408 assert(call_return_value); \
2409 assert(PyErr_ExceptionMatches(exception)); \
2410 PyErr_Clear(); \
2411 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002412
2413static PyObject *
2414test_c_api(PySetObject *so)
2415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 Py_ssize_t count;
2417 char *s;
2418 Py_ssize_t i;
2419 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2420 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002421 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* Verify preconditions */
2425 assert(PyAnySet_Check(ob));
2426 assert(PyAnySet_CheckExact(ob));
2427 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* so.clear(); so |= set("abc"); */
2430 str = PyUnicode_FromString("abc");
2431 if (str == NULL)
2432 return NULL;
2433 set_clear_internal(so);
2434 if (set_update_internal(so, str) == -1) {
2435 Py_DECREF(str);
2436 return NULL;
2437 }
2438 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 /* Exercise type/size checks */
2441 assert(PySet_Size(ob) == 3);
2442 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 /* Raise TypeError for non-iterable constructor arguments */
2445 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2446 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 /* Raise TypeError for unhashable key */
2449 dup = PySet_New(ob);
2450 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2451 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2452 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 /* Exercise successful pop, contains, add, and discard */
2455 elem = PySet_Pop(ob);
2456 assert(PySet_Contains(ob, elem) == 0);
2457 assert(PySet_GET_SIZE(ob) == 2);
2458 assert(PySet_Add(ob, elem) == 0);
2459 assert(PySet_Contains(ob, elem) == 1);
2460 assert(PySet_GET_SIZE(ob) == 3);
2461 assert(PySet_Discard(ob, elem) == 1);
2462 assert(PySet_GET_SIZE(ob) == 2);
2463 assert(PySet_Discard(ob, elem) == 0);
2464 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 /* Exercise clear */
2467 dup2 = PySet_New(dup);
2468 assert(PySet_Clear(dup2) == 0);
2469 assert(PySet_Size(dup2) == 0);
2470 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 /* Raise SystemError on clear or update of frozen set */
2473 f = PyFrozenSet_New(dup);
2474 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2475 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2476 assert(PySet_Add(f, elem) == 0);
2477 Py_INCREF(f);
2478 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2479 Py_DECREF(f);
2480 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* Exercise direct iteration */
2483 i = 0, count = 0;
2484 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2485 s = _PyUnicode_AsString(x);
2486 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2487 count++;
2488 }
2489 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 /* Exercise updates */
2492 dup2 = PySet_New(NULL);
2493 assert(_PySet_Update(dup2, dup) == 0);
2494 assert(PySet_Size(dup2) == 3);
2495 assert(_PySet_Update(dup2, dup) == 0);
2496 assert(PySet_Size(dup2) == 3);
2497 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 /* Raise SystemError when self argument is not a set or frozenset. */
2500 t = PyTuple_New(0);
2501 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2502 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2503 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 /* Raise SystemError when self argument is not a set. */
2506 f = PyFrozenSet_New(dup);
2507 assert(PySet_Size(f) == 3);
2508 assert(PyFrozenSet_CheckExact(f));
2509 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2510 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2511 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 /* Raise KeyError when popping from an empty set */
2514 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2515 Py_DECREF(ob);
2516 assert(PySet_GET_SIZE(ob) == 0);
2517 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 /* Restore the set from the copy using the PyNumber API */
2520 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2521 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 /* Verify constructors accept NULL arguments */
2524 f = PySet_New(NULL);
2525 assert(f != NULL);
2526 assert(PySet_GET_SIZE(f) == 0);
2527 Py_DECREF(f);
2528 f = PyFrozenSet_New(NULL);
2529 assert(f != NULL);
2530 assert(PyFrozenSet_CheckExact(f));
2531 assert(PySet_GET_SIZE(f) == 0);
2532 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 Py_DECREF(elem);
2535 Py_DECREF(dup);
2536 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002537}
2538
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002539#undef assertRaises
2540
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002541#endif