blob: d65145730c0550b25e8e1246d369f757f7a165da [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006 Copyright (c) 2003-6 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"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
13/* This must be >= 1. */
14#define PERTURB_SHIFT 5
15
16/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000017static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000018
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019#ifdef Py_REF_DEBUG
20PyObject *
21_PySet_Dummy(void)
22{
23 return dummy;
24}
25#endif
26
Raymond Hettingerbc841a12005-08-07 13:02:53 +000027#define INIT_NONZERO_SET_SLOTS(so) do { \
28 (so)->table = (so)->smalltable; \
29 (so)->mask = PySet_MINSIZE - 1; \
30 (so)->hash = -1; \
31 } while(0)
32
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033#define EMPTY_TO_MINSIZE(so) do { \
34 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
35 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000036 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000037 } while(0)
38
Raymond Hettingerbc841a12005-08-07 13:02:53 +000039/* Reuse scheme to save calls to malloc, free, and memset */
40#define MAXFREESETS 80
41static PySetObject *free_sets[MAXFREESETS];
42static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000043
44/*
45The basic lookup function used by all operations.
46This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
47Open addressing is preferred over chaining since the link overhead for
48chaining would be substantial (100% with typical malloc overhead).
49
50The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000051probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052
53All arithmetic on hash should ignore overflow.
54
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000055Unlike the dictionary implementation, the lookkey functions can return
56NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057*/
58
59static setentry *
60set_lookkey(PySetObject *so, PyObject *key, register long hash)
61{
Martin v. Löwis18e16552006-02-15 17:27:45 +000062 register Py_ssize_t i;
63 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000064 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000066 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000067 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000068 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000069 PyObject *startkey;
70
71 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000072 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000073 if (entry->key == NULL || entry->key == key)
74 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000076 if (entry->key == dummy)
77 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000078 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000079 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000080 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000081 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
82 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000083 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +000084 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000085 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000086 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000087 }
88 else {
89 /* The compare did major nasty stuff to the
90 * set: start over.
91 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000092 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093 }
94 }
95 freeslot = NULL;
96 }
97
98 /* In the loop, key == dummy is by far (factor of 100s) the
99 least likely outcome, so test for that last. */
100 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
101 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000102 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000103 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000104 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000105 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000106 break;
107 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000108 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000109 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000110 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000111 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000112 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
113 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000114 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000115 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000116 if (cmp > 0)
117 break;
118 }
119 else {
120 /* The compare did major nasty stuff to the
121 * set: start over.
122 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000123 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 }
125 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000126 else if (entry->key == dummy && freeslot == NULL)
127 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000128 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000129 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130}
131
132/*
133 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000134 * This means we can always use _PyString_Eq directly and not have to check to
135 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000136 */
137static setentry *
138set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
139{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000140 register Py_ssize_t i;
141 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000142 register setentry *freeslot;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000144 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000145 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146
147 /* Make sure this function doesn't have to handle non-string keys,
148 including subclasses of str; e.g., one reason to subclass
149 strings is to override __eq__, and for speed we don't cater to
150 that here. */
151 if (!PyString_CheckExact(key)) {
152 so->lookup = set_lookkey;
153 return set_lookkey(so, key, hash);
154 }
155 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000156 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000157 if (entry->key == NULL || entry->key == key)
158 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000159 if (entry->key == dummy)
160 freeslot = entry;
161 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000162 if (entry->hash == hash && _PyString_Eq(entry->key, key))
163 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000164 freeslot = NULL;
165 }
166
167 /* In the loop, key == dummy is by far (factor of 100s) the
168 least likely outcome, so test for that last. */
169 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
170 i = (i << 2) + i + perturb + 1;
171 entry = &table[i & mask];
172 if (entry->key == NULL)
173 return freeslot == NULL ? entry : freeslot;
174 if (entry->key == key
175 || (entry->hash == hash
176 && entry->key != dummy
177 && _PyString_Eq(entry->key, key)))
178 return entry;
179 if (entry->key == dummy && freeslot == NULL)
180 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000181 }
182}
183
184/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000185Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000186Used both by the internal resize routine and by the public insert routine.
187Eats a reference to key.
188*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000189static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000190set_insert_key(register PySetObject *so, PyObject *key, long hash)
191{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000192 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000193 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
194
195 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000196 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000197 if (entry == NULL)
198 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000199 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000200 /* UNUSED */
201 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000202 entry->key = key;
203 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000204 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000205 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000207 entry->key = key;
208 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209 so->used++;
210 Py_DECREF(dummy);
211 } else {
212 /* ACTIVE */
213 Py_DECREF(key);
214 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000215 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000216}
217
218/*
219Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000221actually be smaller than the old one.
222*/
223static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000224set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000225{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000226 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000227 setentry *oldtable, *newtable, *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000228 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000229 int is_oldtable_malloced;
230 setentry small_copy[PySet_MINSIZE];
231
232 assert(minused >= 0);
233
234 /* Find the smallest table size > minused. */
235 for (newsize = PySet_MINSIZE;
236 newsize <= minused && newsize > 0;
237 newsize <<= 1)
238 ;
239 if (newsize <= 0) {
240 PyErr_NoMemory();
241 return -1;
242 }
243
244 /* Get space for a new table. */
245 oldtable = so->table;
246 assert(oldtable != NULL);
247 is_oldtable_malloced = oldtable != so->smalltable;
248
249 if (newsize == PySet_MINSIZE) {
250 /* A large table is shrinking, or we can't get any smaller. */
251 newtable = so->smalltable;
252 if (newtable == oldtable) {
253 if (so->fill == so->used) {
254 /* No dummies, so no point doing anything. */
255 return 0;
256 }
257 /* We're not going to resize it, but rebuild the
258 table anyway to purge old dummy entries.
259 Subtle: This is *necessary* if fill==size,
260 as set_lookkey needs at least one virgin slot to
261 terminate failing searches. If fill < size, it's
262 merely desirable, as dummies slow searches. */
263 assert(so->fill > so->used);
264 memcpy(small_copy, oldtable, sizeof(small_copy));
265 oldtable = small_copy;
266 }
267 }
268 else {
269 newtable = PyMem_NEW(setentry, newsize);
270 if (newtable == NULL) {
271 PyErr_NoMemory();
272 return -1;
273 }
274 }
275
276 /* Make the set empty, using the new table. */
277 assert(newtable != oldtable);
278 so->table = newtable;
279 so->mask = newsize - 1;
280 memset(newtable, 0, sizeof(setentry) * newsize);
281 so->used = 0;
282 i = so->fill;
283 so->fill = 0;
284
285 /* Copy the data over; this is refcount-neutral for active entries;
286 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000287 for (entry = oldtable; i > 0; entry++) {
288 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000289 /* UNUSED */
290 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000291 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000292 /* DUMMY */
293 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000294 assert(entry->key == dummy);
295 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296 } else {
297 /* ACTIVE */
298 --i;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000299 if(set_insert_key(so, entry->key, entry->hash) == -1) {
300 if (is_oldtable_malloced)
301 PyMem_DEL(oldtable);
302 return -1;
303 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000304 }
305 }
306
307 if (is_oldtable_malloced)
308 PyMem_DEL(oldtable);
309 return 0;
310}
311
Raymond Hettingerc991db22005-08-11 07:58:45 +0000312/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
313
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000314static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000315set_add_entry(register PySetObject *so, setentry *entry)
316{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000317 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000318
319 assert(so->fill <= so->mask); /* at least one empty slot */
320 n_used = so->used;
321 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000322 if (set_insert_key(so, entry->key, entry->hash) == -1)
323 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000324 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
325 return 0;
326 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
327}
328
329static int
330set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331{
332 register long hash;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000333 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334
Raymond Hettingerc991db22005-08-11 07:58:45 +0000335 if (!PyString_CheckExact(key) ||
336 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337 hash = PyObject_Hash(key);
338 if (hash == -1)
339 return -1;
340 }
341 assert(so->fill <= so->mask); /* at least one empty slot */
342 n_used = so->used;
343 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000344 if (set_insert_key(so, key, hash) == -1) {
345 Py_DECREF(key);
346 return -1;
347 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000348 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
349 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000350 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000351}
352
353#define DISCARD_NOTFOUND 0
354#define DISCARD_FOUND 1
355
356static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000357set_discard_entry(PySetObject *so, setentry *oldentry)
358{ register setentry *entry;
359 PyObject *old_key;
360
361 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000362 if (entry == NULL)
363 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000364 if (entry->key == NULL || entry->key == dummy)
365 return DISCARD_NOTFOUND;
366 old_key = entry->key;
367 Py_INCREF(dummy);
368 entry->key = dummy;
369 so->used--;
370 Py_DECREF(old_key);
371 return DISCARD_FOUND;
372}
373
374static int
375set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376{
377 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000378 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000379 PyObject *old_key;
380
381 assert (PyAnySet_Check(so));
382 if (!PyString_CheckExact(key) ||
383 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
384 hash = PyObject_Hash(key);
385 if (hash == -1)
386 return -1;
387 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000388 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000389 if (entry == NULL)
390 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000391 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000392 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000393 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000395 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000396 so->used--;
397 Py_DECREF(old_key);
398 return DISCARD_FOUND;
399}
400
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000401static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000402set_clear_internal(PySetObject *so)
403{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000404 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405 int table_is_malloced;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000406 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000407 setentry small_copy[PySet_MINSIZE];
408#ifdef Py_DEBUG
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000409 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000410 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000411
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000412 n = so->mask + 1;
413 i = 0;
414#endif
415
416 table = so->table;
417 assert(table != NULL);
418 table_is_malloced = table != so->smalltable;
419
420 /* This is delicate. During the process of clearing the set,
421 * decrefs can cause the set to mutate. To avoid fatal confusion
422 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000423 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424 * clearing.
425 */
426 fill = so->fill;
427 if (table_is_malloced)
428 EMPTY_TO_MINSIZE(so);
429
430 else if (fill > 0) {
431 /* It's a small table with something that needs to be cleared.
432 * Afraid the only safe way is to copy the set entries into
433 * another small table first.
434 */
435 memcpy(small_copy, table, sizeof(small_copy));
436 table = small_copy;
437 EMPTY_TO_MINSIZE(so);
438 }
439 /* else it's a small table that's already empty */
440
441 /* Now we can finally clear things. If C had refcounts, we could
442 * assert that the refcount on table is 1 now, i.e. that this function
443 * has unique access to it, so decref side-effects can't alter it.
444 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000445 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000446#ifdef Py_DEBUG
447 assert(i < n);
448 ++i;
449#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000450 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000452 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 }
454#ifdef Py_DEBUG
455 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000456 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457#endif
458 }
459
460 if (table_is_malloced)
461 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000462 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000463}
464
465/*
466 * Iterate over a set table. Use like so:
467 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000469 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000470 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000471 * while (set_next(yourset, &pos, &entry)) {
472 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473 * }
474 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000475 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476 * mutates the table.
477 */
478static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000479set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000481 Py_ssize_t i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000482 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000483 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484
485 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000486 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000487 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000488 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000490 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000492 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493 if (i > mask)
494 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000495 assert(table[i].key != NULL);
496 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000497 return 1;
498}
499
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000500static void
501set_dealloc(PySetObject *so)
502{
503 register setentry *entry;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000504 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000505 PyObject_GC_UnTrack(so);
506 Py_TRASHCAN_SAFE_BEGIN(so)
507 if (so->weakreflist != NULL)
508 PyObject_ClearWeakRefs((PyObject *) so);
509
510 for (entry = so->table; fill > 0; entry++) {
511 if (entry->key) {
512 --fill;
513 Py_DECREF(entry->key);
514 }
515 }
516 if (so->table != so->smalltable)
517 PyMem_DEL(so->table);
518 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
519 free_sets[num_free_sets++] = so;
520 else
521 so->ob_type->tp_free(so);
522 Py_TRASHCAN_SAFE_END(so)
523}
524
525static int
526set_tp_print(PySetObject *so, FILE *fp, int flags)
527{
528 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000529 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000530 char *emit = ""; /* No separator emitted on first pass */
531 char *separator = ", ";
532
533 fprintf(fp, "%s([", so->ob_type->tp_name);
534 while (set_next(so, &pos, &entry)) {
535 fputs(emit, fp);
536 emit = separator;
537 if (PyObject_Print(entry->key, fp, 0) != 0)
538 return -1;
539 }
540 fputs("])", fp);
541 return 0;
542}
543
544static PyObject *
545set_repr(PySetObject *so)
546{
547 PyObject *keys, *result, *listrepr;
548
549 keys = PySequence_List((PyObject *)so);
550 if (keys == NULL)
551 return NULL;
552 listrepr = PyObject_Repr(keys);
553 Py_DECREF(keys);
554 if (listrepr == NULL)
555 return NULL;
556
557 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
558 PyString_AS_STRING(listrepr));
559 Py_DECREF(listrepr);
560 return result;
561}
562
Martin v. Löwis18e16552006-02-15 17:27:45 +0000563static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000564set_len(PyObject *so)
565{
566 return ((PySetObject *)so)->used;
567}
568
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000569static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000570set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000571{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000572 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000573 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000574 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000575
576 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000577 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000578
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000579 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000580 if (other == so || other->used == 0)
581 /* a.update(a) or a.update({}); nothing to do */
582 return 0;
583 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000584 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000585 * that there will be no (or few) overlapping keys.
586 */
587 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
588 if (set_table_resize(so, (so->used + other->used)*2) != 0)
589 return -1;
590 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591 for (i = 0; i <= other->mask; i++) {
592 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593 if (entry->key != NULL &&
594 entry->key != dummy) {
595 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000596 if (set_insert_key(so, entry->key, entry->hash) == -1) {
597 Py_DECREF(entry->key);
598 return -1;
599 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000600 }
601 }
602 return 0;
603}
604
605static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000606set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000607{
608 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000609 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000610
611 if (!PyString_CheckExact(key) ||
612 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
613 hash = PyObject_Hash(key);
614 if (hash == -1)
615 return -1;
616 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000617 entry = (so->lookup)(so, key, hash);
618 if (entry == NULL)
619 return -1;
620 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621 return key != NULL && key != dummy;
622}
623
Raymond Hettingerc991db22005-08-11 07:58:45 +0000624static int
625set_contains_entry(PySetObject *so, setentry *entry)
626{
627 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000628 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000629
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000630 lu_entry = (so->lookup)(so, entry->key, entry->hash);
631 if (lu_entry == NULL)
632 return -1;
633 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000634 return key != NULL && key != dummy;
635}
636
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000637static PyObject *
638set_pop(PySetObject *so)
639{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000641 register setentry *entry;
642 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000643
644 assert (PyAnySet_Check(so));
645 if (so->used == 0) {
646 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
647 return NULL;
648 }
649
650 /* Set entry to "the first" unused or dummy set entry. We abuse
651 * the hash field of slot 0 to hold a search finger:
652 * If slot 0 has a value, use slot 0.
653 * Else slot 0 is being used to hold a search finger,
654 * and we use its hash value as the first index to look.
655 */
656 entry = &so->table[0];
657 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000658 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000659 /* The hash field may be a real hash value, or it may be a
660 * legit search finger, or it may be a once-legit search
661 * finger that's out of bounds now because it wrapped around
662 * or the table shrunk -- simply make sure it's in bounds now.
663 */
664 if (i > so->mask || i < 1)
665 i = 1; /* skip slot 0 */
666 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
667 i++;
668 if (i > so->mask)
669 i = 1;
670 }
671 }
672 key = entry->key;
673 Py_INCREF(dummy);
674 entry->key = dummy;
675 so->used--;
676 so->table[0].hash = i + 1; /* next place to start */
677 return key;
678}
679
680PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
681
682static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000683set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000684{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000685 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000686 setentry *entry;
687
688 while (set_next(so, &pos, &entry))
689 Py_VISIT(entry->key);
690 return 0;
691}
692
693static long
694frozenset_hash(PyObject *self)
695{
696 PySetObject *so = (PySetObject *)self;
697 long h, hash = 1927868237L;
698 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000699 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000700
701 if (so->hash != -1)
702 return so->hash;
703
704 hash *= PySet_GET_SIZE(self) + 1;
705 while (set_next(so, &pos, &entry)) {
706 /* Work to increase the bit dispersion for closely spaced hash
707 values. The is important because some use cases have many
708 combinations of a small number of elements with nearby
709 hashes so that many distinct combinations collapse to only
710 a handful of distinct hash values. */
711 h = entry->hash;
712 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
713 }
714 hash = hash * 69069L + 907133923L;
715 if (hash == -1)
716 hash = 590923713L;
717 so->hash = hash;
718 return hash;
719}
720
Raymond Hettingera9d99362005-08-05 00:01:15 +0000721/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000722
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000723typedef struct {
724 PyObject_HEAD
725 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000726 Py_ssize_t si_used;
727 Py_ssize_t si_pos;
728 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000729} setiterobject;
730
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000731static void
732setiter_dealloc(setiterobject *si)
733{
734 Py_XDECREF(si->si_set);
735 PyObject_Del(si);
736}
737
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000738static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000739setiter_len(setiterobject *si)
740{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000741 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000742 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000743 len = si->len;
744 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000745}
746
Armin Rigof5b3e362006-02-11 21:32:43 +0000747PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000748
749static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000750 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000751 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000752};
753
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000754static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000755{
756 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000757 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000758 register setentry *entry;
759 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000760
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000761 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000762 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000763 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000764
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000765 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000766 PyErr_SetString(PyExc_RuntimeError,
767 "Set changed size during iteration");
768 si->si_used = -1; /* Make this state sticky */
769 return NULL;
770 }
771
772 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000773 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000774 entry = so->table;
775 mask = so->mask;
776 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777 i++;
778 si->si_pos = i+1;
779 if (i > mask)
780 goto fail;
781 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000782 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000783 Py_INCREF(key);
784 return key;
785
786fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000787 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788 si->si_set = NULL;
789 return NULL;
790}
791
Hye-Shik Change2956762005-08-01 05:26:41 +0000792static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793 PyObject_HEAD_INIT(&PyType_Type)
794 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000795 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796 sizeof(setiterobject), /* tp_basicsize */
797 0, /* tp_itemsize */
798 /* methods */
799 (destructor)setiter_dealloc, /* tp_dealloc */
800 0, /* tp_print */
801 0, /* tp_getattr */
802 0, /* tp_setattr */
803 0, /* tp_compare */
804 0, /* tp_repr */
805 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000806 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807 0, /* tp_as_mapping */
808 0, /* tp_hash */
809 0, /* tp_call */
810 0, /* tp_str */
811 PyObject_GenericGetAttr, /* tp_getattro */
812 0, /* tp_setattro */
813 0, /* tp_as_buffer */
814 Py_TPFLAGS_DEFAULT, /* tp_flags */
815 0, /* tp_doc */
816 0, /* tp_traverse */
817 0, /* tp_clear */
818 0, /* tp_richcompare */
819 0, /* tp_weaklistoffset */
820 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000821 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000822 setiter_methods, /* tp_methods */
823 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824};
825
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826static PyObject *
827set_iter(PySetObject *so)
828{
829 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
830 if (si == NULL)
831 return NULL;
832 Py_INCREF(so);
833 si->si_set = so;
834 si->si_used = so->used;
835 si->si_pos = 0;
836 si->len = so->used;
837 return (PyObject *)si;
838}
839
Raymond Hettingerd7946662005-08-01 21:39:29 +0000840static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000841set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000842{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000843 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000844
Raymond Hettingerd7946662005-08-01 21:39:29 +0000845 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000846 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000847
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848 if (PyDict_Check(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000849 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000850 Py_ssize_t pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000851 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000852 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000853 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000854 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000855 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856 }
857
Raymond Hettingera38123e2003-11-24 22:18:49 +0000858 it = PyObject_GetIter(other);
859 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000860 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000861
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000862 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000863 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000864 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000865 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000866 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000867 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000868 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000869 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000870 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000871 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000872 return -1;
873 return 0;
874}
875
876static PyObject *
877set_update(PySetObject *so, PyObject *other)
878{
879 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000880 return NULL;
881 Py_RETURN_NONE;
882}
883
884PyDoc_STRVAR(update_doc,
885"Update a set with the union of itself and another.");
886
887static PyObject *
888make_new_set(PyTypeObject *type, PyObject *iterable)
889{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000890 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000891
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000892 if (dummy == NULL) { /* Auto-initialize dummy */
893 dummy = PyString_FromString("<dummy key>");
894 if (dummy == NULL)
895 return NULL;
896 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000897
898 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000899 if (num_free_sets &&
900 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
901 so = free_sets[--num_free_sets];
902 assert (so != NULL && PyAnySet_CheckExact(so));
903 so->ob_type = type;
904 _Py_NewReference((PyObject *)so);
905 EMPTY_TO_MINSIZE(so);
906 PyObject_GC_Track(so);
907 } else {
908 so = (PySetObject *)type->tp_alloc(type, 0);
909 if (so == NULL)
910 return NULL;
911 /* tp_alloc has already zeroed the structure */
912 assert(so->table == NULL && so->fill == 0 && so->used == 0);
913 INIT_NONZERO_SET_SLOTS(so);
914 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000915
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000916 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000917 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000918
Raymond Hettingera38123e2003-11-24 22:18:49 +0000919 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000920 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000921 Py_DECREF(so);
922 return NULL;
923 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000924 }
925
Raymond Hettingera690a992003-11-16 16:17:49 +0000926 return (PyObject *)so;
927}
928
Raymond Hettingerd7946662005-08-01 21:39:29 +0000929/* The empty frozenset is a singleton */
930static PyObject *emptyfrozenset = NULL;
931
Raymond Hettingera690a992003-11-16 16:17:49 +0000932static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000933frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000934{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000935 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000936
Georg Brandl02c42872005-08-26 06:42:30 +0000937 if (!_PyArg_NoKeywords("frozenset()", kwds))
938 return NULL;
939
Raymond Hettingera690a992003-11-16 16:17:49 +0000940 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
941 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942
943 if (type != &PyFrozenSet_Type)
944 return make_new_set(type, iterable);
945
946 if (iterable != NULL) {
947 /* frozenset(f) is idempotent */
948 if (PyFrozenSet_CheckExact(iterable)) {
949 Py_INCREF(iterable);
950 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000951 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000953 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000954 return result;
955 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000956 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000957 /* The empty frozenset is a singleton */
958 if (emptyfrozenset == NULL)
959 emptyfrozenset = make_new_set(type, NULL);
960 Py_XINCREF(emptyfrozenset);
961 return emptyfrozenset;
962}
963
964void
965PySet_Fini(void)
966{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000967 PySetObject *so;
968
969 while (num_free_sets) {
970 num_free_sets--;
971 so = free_sets[num_free_sets];
972 PyObject_GC_Del(so);
973 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000974 Py_CLEAR(dummy);
975 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000976}
977
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000978static PyObject *
979set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
980{
Georg Brandl02c42872005-08-26 06:42:30 +0000981 if (!_PyArg_NoKeywords("set()", kwds))
982 return NULL;
983
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000984 return make_new_set(type, NULL);
985}
986
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000987/* set_swap_bodies() switches the contents of any two sets by moving their
988 internal data pointers and, if needed, copying the internal smalltables.
989 Semantically equivalent to:
990
991 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
992
993 The function always succeeds and it leaves both objects in a stable state.
994 Useful for creating temporary frozensets from sets for membership testing
995 in __contains__(), discard(), and remove(). Also useful for operations
996 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000997 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000998*/
999
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001000static void
1001set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001002{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001003 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001004 setentry *u;
1005 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1006 setentry tab[PySet_MINSIZE];
1007 long h;
1008
1009 t = a->fill; a->fill = b->fill; b->fill = t;
1010 t = a->used; a->used = b->used; b->used = t;
1011 t = a->mask; a->mask = b->mask; b->mask = t;
1012
1013 u = a->table;
1014 if (a->table == a->smalltable)
1015 u = b->smalltable;
1016 a->table = b->table;
1017 if (b->table == b->smalltable)
1018 a->table = a->smalltable;
1019 b->table = u;
1020
1021 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1022
1023 if (a->table == a->smalltable || b->table == b->smalltable) {
1024 memcpy(tab, a->smalltable, sizeof(tab));
1025 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1026 memcpy(b->smalltable, tab, sizeof(tab));
1027 }
1028
Raymond Hettingera580c472005-08-05 17:19:54 +00001029 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1030 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1031 h = a->hash; a->hash = b->hash; b->hash = h;
1032 } else {
1033 a->hash = -1;
1034 b->hash = -1;
1035 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001036}
1037
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001038static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001039set_copy(PySetObject *so)
1040{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001041 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001042}
1043
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001044static PyObject *
1045frozenset_copy(PySetObject *so)
1046{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001047 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001048 Py_INCREF(so);
1049 return (PyObject *)so;
1050 }
1051 return set_copy(so);
1052}
1053
Raymond Hettingera690a992003-11-16 16:17:49 +00001054PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1055
1056static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001057set_clear(PySetObject *so)
1058{
1059 set_clear_internal(so);
1060 Py_RETURN_NONE;
1061}
1062
1063PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1064
1065static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001066set_union(PySetObject *so, PyObject *other)
1067{
1068 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001069
1070 result = (PySetObject *)set_copy(so);
1071 if (result == NULL)
1072 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001073 if ((PyObject *)so == other)
1074 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001075 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001076 Py_DECREF(result);
1077 return NULL;
1078 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001079 return (PyObject *)result;
1080}
1081
1082PyDoc_STRVAR(union_doc,
1083 "Return the union of two sets as a new set.\n\
1084\n\
1085(i.e. all elements that are in either set.)");
1086
1087static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001088set_or(PySetObject *so, PyObject *other)
1089{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001090 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001091 Py_INCREF(Py_NotImplemented);
1092 return Py_NotImplemented;
1093 }
1094 return set_union(so, other);
1095}
1096
1097static PyObject *
1098set_ior(PySetObject *so, PyObject *other)
1099{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001100 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001101 Py_INCREF(Py_NotImplemented);
1102 return Py_NotImplemented;
1103 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001104 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001105 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001106 Py_INCREF(so);
1107 return (PyObject *)so;
1108}
1109
1110static PyObject *
1111set_intersection(PySetObject *so, PyObject *other)
1112{
1113 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001114 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001115
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001116 if ((PyObject *)so == other)
1117 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001118
Raymond Hettingera690a992003-11-16 16:17:49 +00001119 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1120 if (result == NULL)
1121 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001122
Raymond Hettingerc991db22005-08-11 07:58:45 +00001123 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001124 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001125 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001126
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001127 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001128 tmp = (PyObject *)so;
1129 so = (PySetObject *)other;
1130 other = tmp;
1131 }
1132
Raymond Hettingerc991db22005-08-11 07:58:45 +00001133 while (set_next((PySetObject *)other, &pos, &entry)) {
1134 if (set_contains_entry(so, entry)) {
1135 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001136 Py_DECREF(result);
1137 return NULL;
1138 }
1139 }
1140 }
1141 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001142 }
1143
Raymond Hettingera690a992003-11-16 16:17:49 +00001144 it = PyObject_GetIter(other);
1145 if (it == NULL) {
1146 Py_DECREF(result);
1147 return NULL;
1148 }
1149
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001150 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001151 if (set_contains_key(so, key)) {
1152 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001153 Py_DECREF(it);
1154 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001155 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001156 return NULL;
1157 }
1158 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001159 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001160 }
1161 Py_DECREF(it);
1162 if (PyErr_Occurred()) {
1163 Py_DECREF(result);
1164 return NULL;
1165 }
1166 return (PyObject *)result;
1167}
1168
1169PyDoc_STRVAR(intersection_doc,
1170"Return the intersection of two sets as a new set.\n\
1171\n\
1172(i.e. all elements that are in both sets.)");
1173
1174static PyObject *
1175set_intersection_update(PySetObject *so, PyObject *other)
1176{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001177 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001178
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001179 tmp = set_intersection(so, other);
1180 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001181 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001182 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001183 Py_DECREF(tmp);
1184 Py_RETURN_NONE;
1185}
1186
1187PyDoc_STRVAR(intersection_update_doc,
1188"Update a set with the intersection of itself and another.");
1189
1190static PyObject *
1191set_and(PySetObject *so, PyObject *other)
1192{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001193 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001194 Py_INCREF(Py_NotImplemented);
1195 return Py_NotImplemented;
1196 }
1197 return set_intersection(so, other);
1198}
1199
1200static PyObject *
1201set_iand(PySetObject *so, PyObject *other)
1202{
1203 PyObject *result;
1204
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001205 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001206 Py_INCREF(Py_NotImplemented);
1207 return Py_NotImplemented;
1208 }
1209 result = set_intersection_update(so, other);
1210 if (result == NULL)
1211 return NULL;
1212 Py_DECREF(result);
1213 Py_INCREF(so);
1214 return (PyObject *)so;
1215}
1216
Neal Norwitz6576bd82005-11-13 18:41:28 +00001217static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001218set_difference_update_internal(PySetObject *so, PyObject *other)
1219{
1220 if ((PyObject *)so == other)
1221 return set_clear_internal(so);
1222
1223 if (PyAnySet_Check(other)) {
1224 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001225 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001226
1227 while (set_next((PySetObject *)other, &pos, &entry))
1228 set_discard_entry(so, entry);
1229 } else {
1230 PyObject *key, *it;
1231 it = PyObject_GetIter(other);
1232 if (it == NULL)
1233 return -1;
1234
1235 while ((key = PyIter_Next(it)) != NULL) {
1236 if (set_discard_key(so, key) == -1) {
1237 Py_DECREF(it);
1238 Py_DECREF(key);
1239 return -1;
1240 }
1241 Py_DECREF(key);
1242 }
1243 Py_DECREF(it);
1244 if (PyErr_Occurred())
1245 return -1;
1246 }
1247 /* If more than 1/5 are dummies, then resize them away. */
1248 if ((so->fill - so->used) * 5 < so->mask)
1249 return 0;
1250 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1251}
1252
Raymond Hettingera690a992003-11-16 16:17:49 +00001253static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001254set_difference_update(PySetObject *so, PyObject *other)
1255{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001256 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001257 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001258 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001259}
1260
1261PyDoc_STRVAR(difference_update_doc,
1262"Remove all elements of another set from this set.");
1263
1264static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001265set_difference(PySetObject *so, PyObject *other)
1266{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001267 PyObject *result;
1268 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001269 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001270
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001271 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001272 result = set_copy(so);
1273 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001274 return NULL;
1275 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001276 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001277 Py_DECREF(result);
1278 return NULL;
1279 }
1280
1281 result = make_new_set(so->ob_type, NULL);
1282 if (result == NULL)
1283 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001284
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001285 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001286 while (set_next(so, &pos, &entry)) {
1287 setentry entrycopy;
1288 entrycopy.hash = entry->hash;
1289 entrycopy.key = entry->key;
1290 if (!PyDict_Contains(other, entry->key)) {
1291 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001292 return NULL;
1293 }
1294 }
1295 return result;
1296 }
1297
Raymond Hettingerc991db22005-08-11 07:58:45 +00001298 while (set_next(so, &pos, &entry)) {
1299 if (!set_contains_entry((PySetObject *)other, entry)) {
1300 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001301 return NULL;
1302 }
1303 }
1304 return result;
1305}
1306
1307PyDoc_STRVAR(difference_doc,
1308"Return the difference of two sets as a new set.\n\
1309\n\
1310(i.e. all elements that are in this set but not the other.)");
1311static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001312set_sub(PySetObject *so, PyObject *other)
1313{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001314 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001315 Py_INCREF(Py_NotImplemented);
1316 return Py_NotImplemented;
1317 }
1318 return set_difference(so, other);
1319}
1320
1321static PyObject *
1322set_isub(PySetObject *so, PyObject *other)
1323{
1324 PyObject *result;
1325
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001326 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001327 Py_INCREF(Py_NotImplemented);
1328 return Py_NotImplemented;
1329 }
1330 result = set_difference_update(so, other);
1331 if (result == NULL)
1332 return NULL;
1333 Py_DECREF(result);
1334 Py_INCREF(so);
1335 return (PyObject *)so;
1336}
1337
1338static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001339set_symmetric_difference_update(PySetObject *so, PyObject *other)
1340{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001341 PySetObject *otherset;
1342 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001343 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001344 setentry *entry;
1345
1346 if ((PyObject *)so == other)
1347 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001348
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001349 if (PyDict_Check(other)) {
1350 PyObject *value;
1351 int rv;
1352 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001353 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001354 if (rv == -1)
1355 return NULL;
1356 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001357 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001358 return NULL;
1359 }
1360 }
1361 Py_RETURN_NONE;
1362 }
1363
1364 if (PyAnySet_Check(other)) {
1365 Py_INCREF(other);
1366 otherset = (PySetObject *)other;
1367 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001368 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1369 if (otherset == NULL)
1370 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001371 }
1372
Raymond Hettingerc991db22005-08-11 07:58:45 +00001373 while (set_next(otherset, &pos, &entry)) {
1374 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001375 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001376 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001377 return NULL;
1378 }
1379 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001380 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001381 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001382 return NULL;
1383 }
1384 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001385 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001386 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001387 Py_RETURN_NONE;
1388}
1389
1390PyDoc_STRVAR(symmetric_difference_update_doc,
1391"Update a set with the symmetric difference of itself and another.");
1392
1393static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001394set_symmetric_difference(PySetObject *so, PyObject *other)
1395{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001396 PyObject *rv;
1397 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001398
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001399 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1400 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001401 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001402 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1403 if (rv == NULL)
1404 return NULL;
1405 Py_DECREF(rv);
1406 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001407}
1408
1409PyDoc_STRVAR(symmetric_difference_doc,
1410"Return the symmetric difference of two sets as a new set.\n\
1411\n\
1412(i.e. all elements that are in exactly one of the sets.)");
1413
1414static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001415set_xor(PySetObject *so, PyObject *other)
1416{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001417 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001418 Py_INCREF(Py_NotImplemented);
1419 return Py_NotImplemented;
1420 }
1421 return set_symmetric_difference(so, other);
1422}
1423
1424static PyObject *
1425set_ixor(PySetObject *so, PyObject *other)
1426{
1427 PyObject *result;
1428
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001429 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001430 Py_INCREF(Py_NotImplemented);
1431 return Py_NotImplemented;
1432 }
1433 result = set_symmetric_difference_update(so, other);
1434 if (result == NULL)
1435 return NULL;
1436 Py_DECREF(result);
1437 Py_INCREF(so);
1438 return (PyObject *)so;
1439}
1440
1441static PyObject *
1442set_issubset(PySetObject *so, PyObject *other)
1443{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001444 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001445 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001446
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001447 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001448 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001449 tmp = make_new_set(&PySet_Type, other);
1450 if (tmp == NULL)
1451 return NULL;
1452 result = set_issubset(so, tmp);
1453 Py_DECREF(tmp);
1454 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001455 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001456 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001457 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001458
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001459 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001460 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001461 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001462 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001463 Py_RETURN_TRUE;
1464}
1465
1466PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1467
1468static PyObject *
1469set_issuperset(PySetObject *so, PyObject *other)
1470{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001471 PyObject *tmp, *result;
1472
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001473 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001474 tmp = make_new_set(&PySet_Type, other);
1475 if (tmp == NULL)
1476 return NULL;
1477 result = set_issuperset(so, tmp);
1478 Py_DECREF(tmp);
1479 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001480 }
1481 return set_issubset((PySetObject *)other, (PyObject *)so);
1482}
1483
1484PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1485
Raymond Hettingera690a992003-11-16 16:17:49 +00001486static PyObject *
1487set_richcompare(PySetObject *v, PyObject *w, int op)
1488{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001489 PyObject *r1, *r2;
1490
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001491 if(!PyAnySet_Check(w)) {
1492 if (op == Py_EQ)
1493 Py_RETURN_FALSE;
1494 if (op == Py_NE)
1495 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001496 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1497 return NULL;
1498 }
1499 switch (op) {
1500 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001501 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001502 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001503 if (v->hash != -1 &&
1504 ((PySetObject *)w)->hash != -1 &&
1505 v->hash != ((PySetObject *)w)->hash)
1506 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001507 return set_issubset(v, w);
1508 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001509 r1 = set_richcompare(v, w, Py_EQ);
1510 if (r1 == NULL)
1511 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001512 r2 = PyBool_FromLong(PyObject_Not(r1));
1513 Py_DECREF(r1);
1514 return r2;
1515 case Py_LE:
1516 return set_issubset(v, w);
1517 case Py_GE:
1518 return set_issuperset(v, w);
1519 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001520 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001521 Py_RETURN_FALSE;
1522 return set_issubset(v, w);
1523 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001524 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001525 Py_RETURN_FALSE;
1526 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001527 }
1528 Py_INCREF(Py_NotImplemented);
1529 return Py_NotImplemented;
1530}
1531
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001532static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001533set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001534{
1535 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1536 return -1;
1537}
1538
Raymond Hettingera690a992003-11-16 16:17:49 +00001539static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001540set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001541{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001542 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001543 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001544 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001545}
1546
1547PyDoc_STRVAR(add_doc,
1548"Add an element to a set.\n\
1549\n\
1550This has no effect if the element is already present.");
1551
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001552static int
1553set_contains(PySetObject *so, PyObject *key)
1554{
1555 PyObject *tmpkey;
1556 int rv;
1557
1558 rv = set_contains_key(so, key);
1559 if (rv == -1) {
1560 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1561 return -1;
1562 PyErr_Clear();
1563 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1564 if (tmpkey == NULL)
1565 return -1;
1566 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1567 rv = set_contains(so, tmpkey);
1568 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1569 Py_DECREF(tmpkey);
1570 }
1571 return rv;
1572}
1573
1574static PyObject *
1575set_direct_contains(PySetObject *so, PyObject *key)
1576{
1577 long result;
1578
1579 result = set_contains(so, key);
1580 if (result == -1)
1581 return NULL;
1582 return PyBool_FromLong(result);
1583}
1584
1585PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1586
Raymond Hettingera690a992003-11-16 16:17:49 +00001587static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001588set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001589{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001590 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001591 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001592
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001593 rv = set_discard_key(so, key);
1594 if (rv == -1) {
1595 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1596 return NULL;
1597 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001598 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1599 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001600 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001601 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001602 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001603 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001604 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001605 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001606 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001607 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001608 return NULL;
1609 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001610 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001611}
1612
1613PyDoc_STRVAR(remove_doc,
1614"Remove an element from a set; it must be a member.\n\
1615\n\
1616If the element is not a member, raise a KeyError.");
1617
1618static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001619set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001620{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001621 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001622 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001623
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001624 rv = set_discard_key(so, key);
1625 if (rv == -1) {
1626 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1627 return NULL;
1628 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001629 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1630 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001631 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001632 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001633 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001634 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001635 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001636 return result;
1637 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001638 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001639}
1640
1641PyDoc_STRVAR(discard_doc,
1642"Remove an element from a set if it is a member.\n\
1643\n\
1644If the element is not a member, do nothing.");
1645
1646static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001647set_reduce(PySetObject *so)
1648{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001649 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001650
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001651 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001652 if (keys == NULL)
1653 goto done;
1654 args = PyTuple_Pack(1, keys);
1655 if (args == NULL)
1656 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001657 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1658 if (dict == NULL) {
1659 PyErr_Clear();
1660 dict = Py_None;
1661 Py_INCREF(dict);
1662 }
1663 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001664done:
1665 Py_XDECREF(args);
1666 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001667 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001668 return result;
1669}
1670
1671PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1672
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001673static int
1674set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1675{
1676 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001677
1678 if (!PyAnySet_Check(self))
1679 return -1;
1680 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1681 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001682 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001683 self->hash = -1;
1684 if (iterable == NULL)
1685 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001686 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001687}
1688
Raymond Hettingera690a992003-11-16 16:17:49 +00001689static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001690 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001691 0, /* sq_concat */
1692 0, /* sq_repeat */
1693 0, /* sq_item */
1694 0, /* sq_slice */
1695 0, /* sq_ass_item */
1696 0, /* sq_ass_slice */
1697 (objobjproc)set_contains, /* sq_contains */
1698};
1699
1700/* set object ********************************************************/
1701
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001702#ifdef Py_DEBUG
1703static PyObject *test_c_api(PySetObject *so);
1704
1705PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1706All is well if assertions don't fail.");
1707#endif
1708
Raymond Hettingera690a992003-11-16 16:17:49 +00001709static PyMethodDef set_methods[] = {
1710 {"add", (PyCFunction)set_add, METH_O,
1711 add_doc},
1712 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1713 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001714 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001715 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001716 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1717 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001718 {"discard", (PyCFunction)set_discard, METH_O,
1719 discard_doc},
1720 {"difference", (PyCFunction)set_difference, METH_O,
1721 difference_doc},
1722 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1723 difference_update_doc},
1724 {"intersection",(PyCFunction)set_intersection, METH_O,
1725 intersection_doc},
1726 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1727 intersection_update_doc},
1728 {"issubset", (PyCFunction)set_issubset, METH_O,
1729 issubset_doc},
1730 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1731 issuperset_doc},
1732 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1733 pop_doc},
1734 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1735 reduce_doc},
1736 {"remove", (PyCFunction)set_remove, METH_O,
1737 remove_doc},
1738 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1739 symmetric_difference_doc},
1740 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1741 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001742#ifdef Py_DEBUG
1743 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1744 test_c_api_doc},
1745#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001746 {"union", (PyCFunction)set_union, METH_O,
1747 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001748 {"update", (PyCFunction)set_update, METH_O,
1749 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001750 {NULL, NULL} /* sentinel */
1751};
1752
1753static PyNumberMethods set_as_number = {
1754 0, /*nb_add*/
1755 (binaryfunc)set_sub, /*nb_subtract*/
1756 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001757 0, /*nb_remainder*/
1758 0, /*nb_divmod*/
1759 0, /*nb_power*/
1760 0, /*nb_negative*/
1761 0, /*nb_positive*/
1762 0, /*nb_absolute*/
1763 0, /*nb_nonzero*/
1764 0, /*nb_invert*/
1765 0, /*nb_lshift*/
1766 0, /*nb_rshift*/
1767 (binaryfunc)set_and, /*nb_and*/
1768 (binaryfunc)set_xor, /*nb_xor*/
1769 (binaryfunc)set_or, /*nb_or*/
1770 0, /*nb_coerce*/
1771 0, /*nb_int*/
1772 0, /*nb_long*/
1773 0, /*nb_float*/
1774 0, /*nb_oct*/
1775 0, /*nb_hex*/
1776 0, /*nb_inplace_add*/
1777 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1778 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001779 0, /*nb_inplace_remainder*/
1780 0, /*nb_inplace_power*/
1781 0, /*nb_inplace_lshift*/
1782 0, /*nb_inplace_rshift*/
1783 (binaryfunc)set_iand, /*nb_inplace_and*/
1784 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1785 (binaryfunc)set_ior, /*nb_inplace_or*/
1786};
1787
1788PyDoc_STRVAR(set_doc,
1789"set(iterable) --> set object\n\
1790\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001791Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001792
1793PyTypeObject PySet_Type = {
1794 PyObject_HEAD_INIT(&PyType_Type)
1795 0, /* ob_size */
1796 "set", /* tp_name */
1797 sizeof(PySetObject), /* tp_basicsize */
1798 0, /* tp_itemsize */
1799 /* methods */
1800 (destructor)set_dealloc, /* tp_dealloc */
1801 (printfunc)set_tp_print, /* tp_print */
1802 0, /* tp_getattr */
1803 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001804 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001805 (reprfunc)set_repr, /* tp_repr */
1806 &set_as_number, /* tp_as_number */
1807 &set_as_sequence, /* tp_as_sequence */
1808 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001809 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001810 0, /* tp_call */
1811 0, /* tp_str */
1812 PyObject_GenericGetAttr, /* tp_getattro */
1813 0, /* tp_setattro */
1814 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001815 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001816 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001817 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001818 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001819 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001820 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001821 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001822 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001823 0, /* tp_iternext */
1824 set_methods, /* tp_methods */
1825 0, /* tp_members */
1826 0, /* tp_getset */
1827 0, /* tp_base */
1828 0, /* tp_dict */
1829 0, /* tp_descr_get */
1830 0, /* tp_descr_set */
1831 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001832 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001833 PyType_GenericAlloc, /* tp_alloc */
1834 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001835 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001836};
1837
1838/* frozenset object ********************************************************/
1839
1840
1841static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001842 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001843 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001844 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001845 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001846 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001847 difference_doc},
1848 {"intersection",(PyCFunction)set_intersection, METH_O,
1849 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001850 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001851 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001852 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001853 issuperset_doc},
1854 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1855 reduce_doc},
1856 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1857 symmetric_difference_doc},
1858 {"union", (PyCFunction)set_union, METH_O,
1859 union_doc},
1860 {NULL, NULL} /* sentinel */
1861};
1862
1863static PyNumberMethods frozenset_as_number = {
1864 0, /*nb_add*/
1865 (binaryfunc)set_sub, /*nb_subtract*/
1866 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001867 0, /*nb_remainder*/
1868 0, /*nb_divmod*/
1869 0, /*nb_power*/
1870 0, /*nb_negative*/
1871 0, /*nb_positive*/
1872 0, /*nb_absolute*/
1873 0, /*nb_nonzero*/
1874 0, /*nb_invert*/
1875 0, /*nb_lshift*/
1876 0, /*nb_rshift*/
1877 (binaryfunc)set_and, /*nb_and*/
1878 (binaryfunc)set_xor, /*nb_xor*/
1879 (binaryfunc)set_or, /*nb_or*/
1880};
1881
1882PyDoc_STRVAR(frozenset_doc,
1883"frozenset(iterable) --> frozenset object\n\
1884\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001885Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001886
1887PyTypeObject PyFrozenSet_Type = {
1888 PyObject_HEAD_INIT(&PyType_Type)
1889 0, /* ob_size */
1890 "frozenset", /* tp_name */
1891 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001892 0, /* tp_itemsize */
1893 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001894 (destructor)set_dealloc, /* tp_dealloc */
1895 (printfunc)set_tp_print, /* tp_print */
1896 0, /* tp_getattr */
1897 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001898 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001899 (reprfunc)set_repr, /* tp_repr */
1900 &frozenset_as_number, /* tp_as_number */
1901 &set_as_sequence, /* tp_as_sequence */
1902 0, /* tp_as_mapping */
1903 frozenset_hash, /* tp_hash */
1904 0, /* tp_call */
1905 0, /* tp_str */
1906 PyObject_GenericGetAttr, /* tp_getattro */
1907 0, /* tp_setattro */
1908 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001909 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001910 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001911 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001912 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001913 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001914 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001915 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001916 (getiterfunc)set_iter, /* tp_iter */
1917 0, /* tp_iternext */
1918 frozenset_methods, /* tp_methods */
1919 0, /* tp_members */
1920 0, /* tp_getset */
1921 0, /* tp_base */
1922 0, /* tp_dict */
1923 0, /* tp_descr_get */
1924 0, /* tp_descr_set */
1925 0, /* tp_dictoffset */
1926 0, /* tp_init */
1927 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001928 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001929 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001930};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001931
1932
1933/***** C API functions *************************************************/
1934
1935PyObject *
1936PySet_New(PyObject *iterable)
1937{
1938 return make_new_set(&PySet_Type, iterable);
1939}
1940
1941PyObject *
1942PyFrozenSet_New(PyObject *iterable)
1943{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001944 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001945
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001946 if (iterable == NULL)
1947 args = PyTuple_New(0);
1948 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001949 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001950 if (args == NULL)
1951 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001952 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001953 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001954 return result;
1955}
1956
Neal Norwitz8c49c822006-03-04 18:41:19 +00001957Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001958PySet_Size(PyObject *anyset)
1959{
1960 if (!PyAnySet_Check(anyset)) {
1961 PyErr_BadInternalCall();
1962 return -1;
1963 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001964 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001965}
1966
1967int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001968PySet_Clear(PyObject *set)
1969{
1970 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1971 PyErr_BadInternalCall();
1972 return -1;
1973 }
1974 return set_clear_internal((PySetObject *)set);
1975}
1976
1977int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001978PySet_Contains(PyObject *anyset, PyObject *key)
1979{
1980 if (!PyAnySet_Check(anyset)) {
1981 PyErr_BadInternalCall();
1982 return -1;
1983 }
1984 return set_contains_key((PySetObject *)anyset, key);
1985}
1986
1987int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001988PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001989{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001990 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001991 PyErr_BadInternalCall();
1992 return -1;
1993 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001994 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001995}
1996
1997int
1998PySet_Add(PyObject *set, PyObject *key)
1999{
2000 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2001 PyErr_BadInternalCall();
2002 return -1;
2003 }
2004 return set_add_key((PySetObject *)set, key);
2005}
2006
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002007int
2008_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2009{
2010 setentry *entry_ptr;
2011
2012 if (!PyAnySet_Check(set)) {
2013 PyErr_BadInternalCall();
2014 return -1;
2015 }
2016 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2017 return 0;
2018 *entry = entry_ptr->key;
2019 return 1;
2020}
2021
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002022PyObject *
2023PySet_Pop(PyObject *set)
2024{
2025 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2026 PyErr_BadInternalCall();
2027 return NULL;
2028 }
2029 return set_pop((PySetObject *)set);
2030}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002031
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002032int
2033_PySet_Update(PyObject *set, PyObject *iterable)
2034{
2035 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2036 PyErr_BadInternalCall();
2037 return -1;
2038 }
2039 return set_update_internal((PySetObject *)set, iterable);
2040}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002041
2042#ifdef Py_DEBUG
2043
2044/* Test code to be called with any three element set.
2045 Returns True and original set is restored. */
2046
2047#define assertRaises(call_return_value, exception) \
2048 do { \
2049 assert(call_return_value); \
2050 assert(PyErr_ExceptionMatches(exception)); \
2051 PyErr_Clear(); \
2052 } while(0)
2053
2054static PyObject *
2055test_c_api(PySetObject *so)
2056{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002057 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002058 char *s;
2059 Py_ssize_t i;
2060 PyObject *elem, *dup, *t, *f, *dup2;
2061 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002062
2063 /* Verify preconditions and exercise type/size checks */
2064 assert(PyAnySet_Check(ob));
2065 assert(PyAnySet_CheckExact(ob));
2066 assert(!PyFrozenSet_CheckExact(ob));
2067 assert(PySet_Size(ob) == 3);
2068 assert(PySet_GET_SIZE(ob) == 3);
2069
2070 /* Raise TypeError for non-iterable constructor arguments */
2071 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2072 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2073
2074 /* Raise TypeError for unhashable key */
2075 dup = PySet_New(ob);
2076 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2077 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2078 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2079
2080 /* Exercise successful pop, contains, add, and discard */
2081 elem = PySet_Pop(ob);
2082 assert(PySet_Contains(ob, elem) == 0);
2083 assert(PySet_GET_SIZE(ob) == 2);
2084 assert(PySet_Add(ob, elem) == 0);
2085 assert(PySet_Contains(ob, elem) == 1);
2086 assert(PySet_GET_SIZE(ob) == 3);
2087 assert(PySet_Discard(ob, elem) == 1);
2088 assert(PySet_GET_SIZE(ob) == 2);
2089 assert(PySet_Discard(ob, elem) == 0);
2090 assert(PySet_GET_SIZE(ob) == 2);
2091
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002092 /* Exercise clear */
2093 dup2 = PySet_New(dup);
2094 assert(PySet_Clear(dup2) == 0);
2095 assert(PySet_Size(dup2) == 0);
2096 Py_DECREF(dup2);
2097
2098 /* Raise SystemError on clear or update of frozen set */
2099 f = PyFrozenSet_New(dup);
2100 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2101 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2102 Py_DECREF(f);
2103
2104 /* Exercise direct iteration */
2105 i = 0, count = 0;
2106 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2107 s = PyString_AsString(elem);
2108 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2109 count++;
2110 }
2111 assert(count == 3);
2112
2113 /* Exercise updates */
2114 dup2 = PySet_New(NULL);
2115 assert(_PySet_Update(dup2, dup) == 0);
2116 assert(PySet_Size(dup2) == 3);
2117 assert(_PySet_Update(dup2, dup) == 0);
2118 assert(PySet_Size(dup2) == 3);
2119 Py_DECREF(dup2);
2120
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002121 /* Raise SystemError when self argument is not a set or frozenset. */
2122 t = PyTuple_New(0);
2123 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2124 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2125 Py_DECREF(t);
2126
2127 /* Raise SystemError when self argument is not a set. */
2128 f = PyFrozenSet_New(dup);
2129 assert(PySet_Size(f) == 3);
2130 assert(PyFrozenSet_CheckExact(f));
2131 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2132 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2133 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2134 Py_DECREF(f);
2135
2136 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002137 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2138 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002139 assert(PySet_GET_SIZE(ob) == 0);
2140 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2141
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002142 /* Restore the set from the copy using the PyNumber API */
2143 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2144 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002145
2146 /* Verify constructors accept NULL arguments */
2147 f = PySet_New(NULL);
2148 assert(f != NULL);
2149 assert(PySet_GET_SIZE(f) == 0);
2150 Py_DECREF(f);
2151 f = PyFrozenSet_New(NULL);
2152 assert(f != NULL);
2153 assert(PyFrozenSet_CheckExact(f));
2154 assert(PySet_GET_SIZE(f) == 0);
2155 Py_DECREF(f);
2156
2157 Py_DECREF(elem);
2158 Py_DECREF(dup);
2159 Py_RETURN_TRUE;
2160}
2161
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002162#undef assertRaises
2163
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002164#endif