blob: b4b58b7afaab0885d31a0b676fc986c17de2c5e9 [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
Guido van Rossum86e58e22006-08-28 15:27:34 +0000533 if (so->ob_type == &PySet_Type)
534 fprintf(fp, "{");
535 else
536 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000537 while (set_next(so, &pos, &entry)) {
538 fputs(emit, fp);
539 emit = separator;
540 if (PyObject_Print(entry->key, fp, 0) != 0)
541 return -1;
542 }
Guido van Rossum86e58e22006-08-28 15:27:34 +0000543 if (so->ob_type == &PySet_Type)
544 fputs("}", fp);
545 else
546 fputs("])", fp);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000547 return 0;
548}
549
550static PyObject *
551set_repr(PySetObject *so)
552{
553 PyObject *keys, *result, *listrepr;
554
555 keys = PySequence_List((PyObject *)so);
556 if (keys == NULL)
557 return NULL;
558 listrepr = PyObject_Repr(keys);
559 Py_DECREF(keys);
560 if (listrepr == NULL)
561 return NULL;
562
Guido van Rossum86e58e22006-08-28 15:27:34 +0000563 if (so->ob_type == &PySet_Type) {
564 char *s = PyString_AS_STRING(listrepr);
565 s += 1;
566 s[strlen(s)-1] = 0;
567 result = PyString_FromFormat("{%s}", s);
568 } else {
569 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
570 PyString_AS_STRING(listrepr));
571 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000572 Py_DECREF(listrepr);
573 return result;
574}
575
Martin v. Löwis18e16552006-02-15 17:27:45 +0000576static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000577set_len(PyObject *so)
578{
579 return ((PySetObject *)so)->used;
580}
581
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000582static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000583set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000584{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000585 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000586 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000587 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000588
589 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000590 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000591
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000592 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593 if (other == so || other->used == 0)
594 /* a.update(a) or a.update({}); nothing to do */
595 return 0;
596 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000597 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000598 * that there will be no (or few) overlapping keys.
599 */
600 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
601 if (set_table_resize(so, (so->used + other->used)*2) != 0)
602 return -1;
603 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000604 for (i = 0; i <= other->mask; i++) {
605 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000606 if (entry->key != NULL &&
607 entry->key != dummy) {
608 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000609 if (set_insert_key(so, entry->key, entry->hash) == -1) {
610 Py_DECREF(entry->key);
611 return -1;
612 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000613 }
614 }
615 return 0;
616}
617
618static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000619set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000620{
621 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000622 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000623
624 if (!PyString_CheckExact(key) ||
625 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
626 hash = PyObject_Hash(key);
627 if (hash == -1)
628 return -1;
629 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000630 entry = (so->lookup)(so, key, hash);
631 if (entry == NULL)
632 return -1;
633 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000634 return key != NULL && key != dummy;
635}
636
Raymond Hettingerc991db22005-08-11 07:58:45 +0000637static int
638set_contains_entry(PySetObject *so, setentry *entry)
639{
640 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000641 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000642
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000643 lu_entry = (so->lookup)(so, entry->key, entry->hash);
644 if (lu_entry == NULL)
645 return -1;
646 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000647 return key != NULL && key != dummy;
648}
649
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000650static PyObject *
651set_pop(PySetObject *so)
652{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000653 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000654 register setentry *entry;
655 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000656
657 assert (PyAnySet_Check(so));
658 if (so->used == 0) {
659 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
660 return NULL;
661 }
662
663 /* Set entry to "the first" unused or dummy set entry. We abuse
664 * the hash field of slot 0 to hold a search finger:
665 * If slot 0 has a value, use slot 0.
666 * Else slot 0 is being used to hold a search finger,
667 * and we use its hash value as the first index to look.
668 */
669 entry = &so->table[0];
670 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000671 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000672 /* The hash field may be a real hash value, or it may be a
673 * legit search finger, or it may be a once-legit search
674 * finger that's out of bounds now because it wrapped around
675 * or the table shrunk -- simply make sure it's in bounds now.
676 */
677 if (i > so->mask || i < 1)
678 i = 1; /* skip slot 0 */
679 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
680 i++;
681 if (i > so->mask)
682 i = 1;
683 }
684 }
685 key = entry->key;
686 Py_INCREF(dummy);
687 entry->key = dummy;
688 so->used--;
689 so->table[0].hash = i + 1; /* next place to start */
690 return key;
691}
692
693PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
694
695static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000696set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000697{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000698 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000699 setentry *entry;
700
701 while (set_next(so, &pos, &entry))
702 Py_VISIT(entry->key);
703 return 0;
704}
705
706static long
707frozenset_hash(PyObject *self)
708{
709 PySetObject *so = (PySetObject *)self;
710 long h, hash = 1927868237L;
711 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000712 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000713
714 if (so->hash != -1)
715 return so->hash;
716
717 hash *= PySet_GET_SIZE(self) + 1;
718 while (set_next(so, &pos, &entry)) {
719 /* Work to increase the bit dispersion for closely spaced hash
720 values. The is important because some use cases have many
721 combinations of a small number of elements with nearby
722 hashes so that many distinct combinations collapse to only
723 a handful of distinct hash values. */
724 h = entry->hash;
725 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
726 }
727 hash = hash * 69069L + 907133923L;
728 if (hash == -1)
729 hash = 590923713L;
730 so->hash = hash;
731 return hash;
732}
733
Raymond Hettingera9d99362005-08-05 00:01:15 +0000734/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000735
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000736typedef struct {
737 PyObject_HEAD
738 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000739 Py_ssize_t si_used;
740 Py_ssize_t si_pos;
741 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000742} setiterobject;
743
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000744static void
745setiter_dealloc(setiterobject *si)
746{
747 Py_XDECREF(si->si_set);
748 PyObject_Del(si);
749}
750
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000751static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000752setiter_len(setiterobject *si)
753{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000754 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000755 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000756 len = si->len;
757 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000758}
759
Armin Rigof5b3e362006-02-11 21:32:43 +0000760PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000761
762static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000763 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000764 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000765};
766
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000767static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000768{
769 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000770 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000771 register setentry *entry;
772 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000773
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000774 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000775 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000776 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000778 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000779 PyErr_SetString(PyExc_RuntimeError,
780 "Set changed size during iteration");
781 si->si_used = -1; /* Make this state sticky */
782 return NULL;
783 }
784
785 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000786 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000787 entry = so->table;
788 mask = so->mask;
789 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790 i++;
791 si->si_pos = i+1;
792 if (i > mask)
793 goto fail;
794 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000795 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796 Py_INCREF(key);
797 return key;
798
799fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000800 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801 si->si_set = NULL;
802 return NULL;
803}
804
Hye-Shik Change2956762005-08-01 05:26:41 +0000805static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000806 PyObject_HEAD_INIT(&PyType_Type)
807 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000808 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809 sizeof(setiterobject), /* tp_basicsize */
810 0, /* tp_itemsize */
811 /* methods */
812 (destructor)setiter_dealloc, /* tp_dealloc */
813 0, /* tp_print */
814 0, /* tp_getattr */
815 0, /* tp_setattr */
816 0, /* tp_compare */
817 0, /* tp_repr */
818 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000819 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000820 0, /* tp_as_mapping */
821 0, /* tp_hash */
822 0, /* tp_call */
823 0, /* tp_str */
824 PyObject_GenericGetAttr, /* tp_getattro */
825 0, /* tp_setattro */
826 0, /* tp_as_buffer */
827 Py_TPFLAGS_DEFAULT, /* tp_flags */
828 0, /* tp_doc */
829 0, /* tp_traverse */
830 0, /* tp_clear */
831 0, /* tp_richcompare */
832 0, /* tp_weaklistoffset */
833 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000834 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000835 setiter_methods, /* tp_methods */
836 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837};
838
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839static PyObject *
840set_iter(PySetObject *so)
841{
842 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
843 if (si == NULL)
844 return NULL;
845 Py_INCREF(so);
846 si->si_set = so;
847 si->si_used = so->used;
848 si->si_pos = 0;
849 si->len = so->used;
850 return (PyObject *)si;
851}
852
Raymond Hettingerd7946662005-08-01 21:39:29 +0000853static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000854set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000855{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000856 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000857
Raymond Hettingerd7946662005-08-01 21:39:29 +0000858 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000859 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000860
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861 if (PyDict_Check(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000862 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000863 Py_ssize_t pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000864 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000865 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000866 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000867 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000868 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000869 }
870
Raymond Hettingera38123e2003-11-24 22:18:49 +0000871 it = PyObject_GetIter(other);
872 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000873 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000874
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000875 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000876 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000877 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000878 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000879 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000880 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000881 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000882 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000883 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000884 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000885 return -1;
886 return 0;
887}
888
889static PyObject *
890set_update(PySetObject *so, PyObject *other)
891{
892 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000893 return NULL;
894 Py_RETURN_NONE;
895}
896
897PyDoc_STRVAR(update_doc,
898"Update a set with the union of itself and another.");
899
900static PyObject *
901make_new_set(PyTypeObject *type, PyObject *iterable)
902{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000903 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000904
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000905 if (dummy == NULL) { /* Auto-initialize dummy */
906 dummy = PyString_FromString("<dummy key>");
907 if (dummy == NULL)
908 return NULL;
909 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000910
911 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000912 if (num_free_sets &&
913 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
914 so = free_sets[--num_free_sets];
915 assert (so != NULL && PyAnySet_CheckExact(so));
916 so->ob_type = type;
917 _Py_NewReference((PyObject *)so);
918 EMPTY_TO_MINSIZE(so);
919 PyObject_GC_Track(so);
920 } else {
921 so = (PySetObject *)type->tp_alloc(type, 0);
922 if (so == NULL)
923 return NULL;
924 /* tp_alloc has already zeroed the structure */
925 assert(so->table == NULL && so->fill == 0 && so->used == 0);
926 INIT_NONZERO_SET_SLOTS(so);
927 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000928
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000929 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000930 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000931
Raymond Hettingera38123e2003-11-24 22:18:49 +0000932 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000933 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000934 Py_DECREF(so);
935 return NULL;
936 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000937 }
938
Raymond Hettingera690a992003-11-16 16:17:49 +0000939 return (PyObject *)so;
940}
941
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942/* The empty frozenset is a singleton */
943static PyObject *emptyfrozenset = NULL;
944
Raymond Hettingera690a992003-11-16 16:17:49 +0000945static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000946frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000947{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000948 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000949
Georg Brandl02c42872005-08-26 06:42:30 +0000950 if (!_PyArg_NoKeywords("frozenset()", kwds))
951 return NULL;
952
Raymond Hettingera690a992003-11-16 16:17:49 +0000953 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
954 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000955
956 if (type != &PyFrozenSet_Type)
957 return make_new_set(type, iterable);
958
959 if (iterable != NULL) {
960 /* frozenset(f) is idempotent */
961 if (PyFrozenSet_CheckExact(iterable)) {
962 Py_INCREF(iterable);
963 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000964 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000965 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000966 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000967 return result;
968 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000969 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000970 /* The empty frozenset is a singleton */
971 if (emptyfrozenset == NULL)
972 emptyfrozenset = make_new_set(type, NULL);
973 Py_XINCREF(emptyfrozenset);
974 return emptyfrozenset;
975}
976
977void
978PySet_Fini(void)
979{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000980 PySetObject *so;
981
982 while (num_free_sets) {
983 num_free_sets--;
984 so = free_sets[num_free_sets];
985 PyObject_GC_Del(so);
986 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000987 Py_CLEAR(dummy);
988 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000989}
990
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000991static PyObject *
992set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
993{
Georg Brandl02c42872005-08-26 06:42:30 +0000994 if (!_PyArg_NoKeywords("set()", kwds))
995 return NULL;
996
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000997 return make_new_set(type, NULL);
998}
999
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001000/* set_swap_bodies() switches the contents of any two sets by moving their
1001 internal data pointers and, if needed, copying the internal smalltables.
1002 Semantically equivalent to:
1003
1004 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1005
1006 The function always succeeds and it leaves both objects in a stable state.
1007 Useful for creating temporary frozensets from sets for membership testing
1008 in __contains__(), discard(), and remove(). Also useful for operations
1009 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001010 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001011*/
1012
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001013static void
1014set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001015{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001016 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001017 setentry *u;
1018 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1019 setentry tab[PySet_MINSIZE];
1020 long h;
1021
1022 t = a->fill; a->fill = b->fill; b->fill = t;
1023 t = a->used; a->used = b->used; b->used = t;
1024 t = a->mask; a->mask = b->mask; b->mask = t;
1025
1026 u = a->table;
1027 if (a->table == a->smalltable)
1028 u = b->smalltable;
1029 a->table = b->table;
1030 if (b->table == b->smalltable)
1031 a->table = a->smalltable;
1032 b->table = u;
1033
1034 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1035
1036 if (a->table == a->smalltable || b->table == b->smalltable) {
1037 memcpy(tab, a->smalltable, sizeof(tab));
1038 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1039 memcpy(b->smalltable, tab, sizeof(tab));
1040 }
1041
Raymond Hettingera580c472005-08-05 17:19:54 +00001042 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1043 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1044 h = a->hash; a->hash = b->hash; b->hash = h;
1045 } else {
1046 a->hash = -1;
1047 b->hash = -1;
1048 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001049}
1050
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001051static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001052set_copy(PySetObject *so)
1053{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001054 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001055}
1056
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001057static PyObject *
1058frozenset_copy(PySetObject *so)
1059{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001060 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001061 Py_INCREF(so);
1062 return (PyObject *)so;
1063 }
1064 return set_copy(so);
1065}
1066
Raymond Hettingera690a992003-11-16 16:17:49 +00001067PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1068
1069static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001070set_clear(PySetObject *so)
1071{
1072 set_clear_internal(so);
1073 Py_RETURN_NONE;
1074}
1075
1076PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1077
1078static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001079set_union(PySetObject *so, PyObject *other)
1080{
1081 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001082
1083 result = (PySetObject *)set_copy(so);
1084 if (result == NULL)
1085 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001086 if ((PyObject *)so == other)
1087 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001088 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001089 Py_DECREF(result);
1090 return NULL;
1091 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001092 return (PyObject *)result;
1093}
1094
1095PyDoc_STRVAR(union_doc,
1096 "Return the union of two sets as a new set.\n\
1097\n\
1098(i.e. all elements that are in either set.)");
1099
1100static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001101set_or(PySetObject *so, PyObject *other)
1102{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001103 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001104 Py_INCREF(Py_NotImplemented);
1105 return Py_NotImplemented;
1106 }
1107 return set_union(so, other);
1108}
1109
1110static PyObject *
1111set_ior(PySetObject *so, PyObject *other)
1112{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001113 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001114 Py_INCREF(Py_NotImplemented);
1115 return Py_NotImplemented;
1116 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001117 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001118 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001119 Py_INCREF(so);
1120 return (PyObject *)so;
1121}
1122
1123static PyObject *
1124set_intersection(PySetObject *so, PyObject *other)
1125{
1126 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001127 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001128
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001129 if ((PyObject *)so == other)
1130 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001131
Raymond Hettingera690a992003-11-16 16:17:49 +00001132 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1133 if (result == NULL)
1134 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001135
Raymond Hettingerc991db22005-08-11 07:58:45 +00001136 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001137 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001138 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001139
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001140 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001141 tmp = (PyObject *)so;
1142 so = (PySetObject *)other;
1143 other = tmp;
1144 }
1145
Raymond Hettingerc991db22005-08-11 07:58:45 +00001146 while (set_next((PySetObject *)other, &pos, &entry)) {
1147 if (set_contains_entry(so, entry)) {
1148 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001149 Py_DECREF(result);
1150 return NULL;
1151 }
1152 }
1153 }
1154 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001155 }
1156
Raymond Hettingera690a992003-11-16 16:17:49 +00001157 it = PyObject_GetIter(other);
1158 if (it == NULL) {
1159 Py_DECREF(result);
1160 return NULL;
1161 }
1162
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001163 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001164 if (set_contains_key(so, key)) {
1165 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001166 Py_DECREF(it);
1167 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001168 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001169 return NULL;
1170 }
1171 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001172 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001173 }
1174 Py_DECREF(it);
1175 if (PyErr_Occurred()) {
1176 Py_DECREF(result);
1177 return NULL;
1178 }
1179 return (PyObject *)result;
1180}
1181
1182PyDoc_STRVAR(intersection_doc,
1183"Return the intersection of two sets as a new set.\n\
1184\n\
1185(i.e. all elements that are in both sets.)");
1186
1187static PyObject *
1188set_intersection_update(PySetObject *so, PyObject *other)
1189{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001190 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001191
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001192 tmp = set_intersection(so, other);
1193 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001194 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001195 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001196 Py_DECREF(tmp);
1197 Py_RETURN_NONE;
1198}
1199
1200PyDoc_STRVAR(intersection_update_doc,
1201"Update a set with the intersection of itself and another.");
1202
1203static PyObject *
1204set_and(PySetObject *so, PyObject *other)
1205{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001206 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001207 Py_INCREF(Py_NotImplemented);
1208 return Py_NotImplemented;
1209 }
1210 return set_intersection(so, other);
1211}
1212
1213static PyObject *
1214set_iand(PySetObject *so, PyObject *other)
1215{
1216 PyObject *result;
1217
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001218 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001219 Py_INCREF(Py_NotImplemented);
1220 return Py_NotImplemented;
1221 }
1222 result = set_intersection_update(so, other);
1223 if (result == NULL)
1224 return NULL;
1225 Py_DECREF(result);
1226 Py_INCREF(so);
1227 return (PyObject *)so;
1228}
1229
Neal Norwitz6576bd82005-11-13 18:41:28 +00001230static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001231set_difference_update_internal(PySetObject *so, PyObject *other)
1232{
1233 if ((PyObject *)so == other)
1234 return set_clear_internal(so);
1235
1236 if (PyAnySet_Check(other)) {
1237 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001238 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001239
1240 while (set_next((PySetObject *)other, &pos, &entry))
1241 set_discard_entry(so, entry);
1242 } else {
1243 PyObject *key, *it;
1244 it = PyObject_GetIter(other);
1245 if (it == NULL)
1246 return -1;
1247
1248 while ((key = PyIter_Next(it)) != NULL) {
1249 if (set_discard_key(so, key) == -1) {
1250 Py_DECREF(it);
1251 Py_DECREF(key);
1252 return -1;
1253 }
1254 Py_DECREF(key);
1255 }
1256 Py_DECREF(it);
1257 if (PyErr_Occurred())
1258 return -1;
1259 }
1260 /* If more than 1/5 are dummies, then resize them away. */
1261 if ((so->fill - so->used) * 5 < so->mask)
1262 return 0;
1263 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1264}
1265
Raymond Hettingera690a992003-11-16 16:17:49 +00001266static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001267set_difference_update(PySetObject *so, PyObject *other)
1268{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001269 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001270 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001271 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001272}
1273
1274PyDoc_STRVAR(difference_update_doc,
1275"Remove all elements of another set from this set.");
1276
1277static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001278set_difference(PySetObject *so, PyObject *other)
1279{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001280 PyObject *result;
1281 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001282 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001283
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001284 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001285 result = set_copy(so);
1286 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001287 return NULL;
1288 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001289 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001290 Py_DECREF(result);
1291 return NULL;
1292 }
1293
1294 result = make_new_set(so->ob_type, NULL);
1295 if (result == NULL)
1296 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001297
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001298 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001299 while (set_next(so, &pos, &entry)) {
1300 setentry entrycopy;
1301 entrycopy.hash = entry->hash;
1302 entrycopy.key = entry->key;
1303 if (!PyDict_Contains(other, entry->key)) {
1304 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001305 return NULL;
1306 }
1307 }
1308 return result;
1309 }
1310
Raymond Hettingerc991db22005-08-11 07:58:45 +00001311 while (set_next(so, &pos, &entry)) {
1312 if (!set_contains_entry((PySetObject *)other, entry)) {
1313 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001314 return NULL;
1315 }
1316 }
1317 return result;
1318}
1319
1320PyDoc_STRVAR(difference_doc,
1321"Return the difference of two sets as a new set.\n\
1322\n\
1323(i.e. all elements that are in this set but not the other.)");
1324static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001325set_sub(PySetObject *so, PyObject *other)
1326{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001327 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001328 Py_INCREF(Py_NotImplemented);
1329 return Py_NotImplemented;
1330 }
1331 return set_difference(so, other);
1332}
1333
1334static PyObject *
1335set_isub(PySetObject *so, PyObject *other)
1336{
1337 PyObject *result;
1338
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001339 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001340 Py_INCREF(Py_NotImplemented);
1341 return Py_NotImplemented;
1342 }
1343 result = set_difference_update(so, other);
1344 if (result == NULL)
1345 return NULL;
1346 Py_DECREF(result);
1347 Py_INCREF(so);
1348 return (PyObject *)so;
1349}
1350
1351static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001352set_symmetric_difference_update(PySetObject *so, PyObject *other)
1353{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001354 PySetObject *otherset;
1355 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001356 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001357 setentry *entry;
1358
1359 if ((PyObject *)so == other)
1360 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001361
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001362 if (PyDict_Check(other)) {
1363 PyObject *value;
1364 int rv;
1365 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001366 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001367 if (rv == -1)
1368 return NULL;
1369 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001370 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001371 return NULL;
1372 }
1373 }
1374 Py_RETURN_NONE;
1375 }
1376
1377 if (PyAnySet_Check(other)) {
1378 Py_INCREF(other);
1379 otherset = (PySetObject *)other;
1380 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001381 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1382 if (otherset == NULL)
1383 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001384 }
1385
Raymond Hettingerc991db22005-08-11 07:58:45 +00001386 while (set_next(otherset, &pos, &entry)) {
1387 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001388 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001389 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001390 return NULL;
1391 }
1392 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001393 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001394 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001395 return NULL;
1396 }
1397 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001398 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001399 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001400 Py_RETURN_NONE;
1401}
1402
1403PyDoc_STRVAR(symmetric_difference_update_doc,
1404"Update a set with the symmetric difference of itself and another.");
1405
1406static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001407set_symmetric_difference(PySetObject *so, PyObject *other)
1408{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001409 PyObject *rv;
1410 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001411
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001412 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1413 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001414 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001415 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1416 if (rv == NULL)
1417 return NULL;
1418 Py_DECREF(rv);
1419 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001420}
1421
1422PyDoc_STRVAR(symmetric_difference_doc,
1423"Return the symmetric difference of two sets as a new set.\n\
1424\n\
1425(i.e. all elements that are in exactly one of the sets.)");
1426
1427static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001428set_xor(PySetObject *so, PyObject *other)
1429{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001430 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001431 Py_INCREF(Py_NotImplemented);
1432 return Py_NotImplemented;
1433 }
1434 return set_symmetric_difference(so, other);
1435}
1436
1437static PyObject *
1438set_ixor(PySetObject *so, PyObject *other)
1439{
1440 PyObject *result;
1441
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001442 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001443 Py_INCREF(Py_NotImplemented);
1444 return Py_NotImplemented;
1445 }
1446 result = set_symmetric_difference_update(so, other);
1447 if (result == NULL)
1448 return NULL;
1449 Py_DECREF(result);
1450 Py_INCREF(so);
1451 return (PyObject *)so;
1452}
1453
1454static PyObject *
1455set_issubset(PySetObject *so, PyObject *other)
1456{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001457 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001458 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001459
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001460 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001461 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001462 tmp = make_new_set(&PySet_Type, other);
1463 if (tmp == NULL)
1464 return NULL;
1465 result = set_issubset(so, tmp);
1466 Py_DECREF(tmp);
1467 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001468 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001469 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001470 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001471
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001472 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001473 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001474 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001475 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001476 Py_RETURN_TRUE;
1477}
1478
1479PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1480
1481static PyObject *
1482set_issuperset(PySetObject *so, PyObject *other)
1483{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001484 PyObject *tmp, *result;
1485
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001486 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001487 tmp = make_new_set(&PySet_Type, other);
1488 if (tmp == NULL)
1489 return NULL;
1490 result = set_issuperset(so, tmp);
1491 Py_DECREF(tmp);
1492 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001493 }
1494 return set_issubset((PySetObject *)other, (PyObject *)so);
1495}
1496
1497PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1498
Raymond Hettingera690a992003-11-16 16:17:49 +00001499static PyObject *
1500set_richcompare(PySetObject *v, PyObject *w, int op)
1501{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001502 PyObject *r1, *r2;
1503
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001504 if(!PyAnySet_Check(w)) {
1505 if (op == Py_EQ)
1506 Py_RETURN_FALSE;
1507 if (op == Py_NE)
1508 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001509 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1510 return NULL;
1511 }
1512 switch (op) {
1513 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001514 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001515 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001516 if (v->hash != -1 &&
1517 ((PySetObject *)w)->hash != -1 &&
1518 v->hash != ((PySetObject *)w)->hash)
1519 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001520 return set_issubset(v, w);
1521 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001522 r1 = set_richcompare(v, w, Py_EQ);
1523 if (r1 == NULL)
1524 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001525 r2 = PyBool_FromLong(PyObject_Not(r1));
1526 Py_DECREF(r1);
1527 return r2;
1528 case Py_LE:
1529 return set_issubset(v, w);
1530 case Py_GE:
1531 return set_issuperset(v, w);
1532 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001533 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001534 Py_RETURN_FALSE;
1535 return set_issubset(v, w);
1536 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001537 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001538 Py_RETURN_FALSE;
1539 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001540 }
1541 Py_INCREF(Py_NotImplemented);
1542 return Py_NotImplemented;
1543}
1544
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001545static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001546set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001547{
1548 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1549 return -1;
1550}
1551
Raymond Hettingera690a992003-11-16 16:17:49 +00001552static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001553set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001554{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001555 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001556 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001557 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001558}
1559
1560PyDoc_STRVAR(add_doc,
1561"Add an element to a set.\n\
1562\n\
1563This has no effect if the element is already present.");
1564
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001565static int
1566set_contains(PySetObject *so, PyObject *key)
1567{
1568 PyObject *tmpkey;
1569 int rv;
1570
1571 rv = set_contains_key(so, key);
1572 if (rv == -1) {
1573 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1574 return -1;
1575 PyErr_Clear();
1576 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1577 if (tmpkey == NULL)
1578 return -1;
1579 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1580 rv = set_contains(so, tmpkey);
1581 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1582 Py_DECREF(tmpkey);
1583 }
1584 return rv;
1585}
1586
1587static PyObject *
1588set_direct_contains(PySetObject *so, PyObject *key)
1589{
1590 long result;
1591
1592 result = set_contains(so, key);
1593 if (result == -1)
1594 return NULL;
1595 return PyBool_FromLong(result);
1596}
1597
1598PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1599
Raymond Hettingera690a992003-11-16 16:17:49 +00001600static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001601set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001602{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001603 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001604 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001605
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001606 rv = set_discard_key(so, key);
1607 if (rv == -1) {
1608 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1609 return NULL;
1610 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001611 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1612 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001613 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001614 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001615 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001616 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001617 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001618 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001619 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001620 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001621 return NULL;
1622 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001623 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001624}
1625
1626PyDoc_STRVAR(remove_doc,
1627"Remove an element from a set; it must be a member.\n\
1628\n\
1629If the element is not a member, raise a KeyError.");
1630
1631static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001632set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001633{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001634 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001635 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001636
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001637 rv = set_discard_key(so, key);
1638 if (rv == -1) {
1639 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1640 return NULL;
1641 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001642 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1643 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001644 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001645 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001646 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001647 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001648 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001649 return result;
1650 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001651 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001652}
1653
1654PyDoc_STRVAR(discard_doc,
1655"Remove an element from a set if it is a member.\n\
1656\n\
1657If the element is not a member, do nothing.");
1658
1659static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001660set_reduce(PySetObject *so)
1661{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001662 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001663
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001664 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001665 if (keys == NULL)
1666 goto done;
1667 args = PyTuple_Pack(1, keys);
1668 if (args == NULL)
1669 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001670 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1671 if (dict == NULL) {
1672 PyErr_Clear();
1673 dict = Py_None;
1674 Py_INCREF(dict);
1675 }
1676 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001677done:
1678 Py_XDECREF(args);
1679 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001680 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001681 return result;
1682}
1683
1684PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1685
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001686static int
1687set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1688{
1689 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001690
1691 if (!PyAnySet_Check(self))
1692 return -1;
1693 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1694 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001695 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001696 self->hash = -1;
1697 if (iterable == NULL)
1698 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001699 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001700}
1701
Raymond Hettingera690a992003-11-16 16:17:49 +00001702static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001703 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001704 0, /* sq_concat */
1705 0, /* sq_repeat */
1706 0, /* sq_item */
1707 0, /* sq_slice */
1708 0, /* sq_ass_item */
1709 0, /* sq_ass_slice */
1710 (objobjproc)set_contains, /* sq_contains */
1711};
1712
1713/* set object ********************************************************/
1714
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001715#ifdef Py_DEBUG
1716static PyObject *test_c_api(PySetObject *so);
1717
1718PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1719All is well if assertions don't fail.");
1720#endif
1721
Raymond Hettingera690a992003-11-16 16:17:49 +00001722static PyMethodDef set_methods[] = {
1723 {"add", (PyCFunction)set_add, METH_O,
1724 add_doc},
1725 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1726 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001727 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001728 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001729 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1730 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001731 {"discard", (PyCFunction)set_discard, METH_O,
1732 discard_doc},
1733 {"difference", (PyCFunction)set_difference, METH_O,
1734 difference_doc},
1735 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1736 difference_update_doc},
1737 {"intersection",(PyCFunction)set_intersection, METH_O,
1738 intersection_doc},
1739 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1740 intersection_update_doc},
1741 {"issubset", (PyCFunction)set_issubset, METH_O,
1742 issubset_doc},
1743 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1744 issuperset_doc},
1745 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1746 pop_doc},
1747 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1748 reduce_doc},
1749 {"remove", (PyCFunction)set_remove, METH_O,
1750 remove_doc},
1751 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1752 symmetric_difference_doc},
1753 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1754 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001755#ifdef Py_DEBUG
1756 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1757 test_c_api_doc},
1758#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001759 {"union", (PyCFunction)set_union, METH_O,
1760 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001761 {"update", (PyCFunction)set_update, METH_O,
1762 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001763 {NULL, NULL} /* sentinel */
1764};
1765
1766static PyNumberMethods set_as_number = {
1767 0, /*nb_add*/
1768 (binaryfunc)set_sub, /*nb_subtract*/
1769 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001770 0, /*nb_remainder*/
1771 0, /*nb_divmod*/
1772 0, /*nb_power*/
1773 0, /*nb_negative*/
1774 0, /*nb_positive*/
1775 0, /*nb_absolute*/
1776 0, /*nb_nonzero*/
1777 0, /*nb_invert*/
1778 0, /*nb_lshift*/
1779 0, /*nb_rshift*/
1780 (binaryfunc)set_and, /*nb_and*/
1781 (binaryfunc)set_xor, /*nb_xor*/
1782 (binaryfunc)set_or, /*nb_or*/
1783 0, /*nb_coerce*/
1784 0, /*nb_int*/
1785 0, /*nb_long*/
1786 0, /*nb_float*/
1787 0, /*nb_oct*/
1788 0, /*nb_hex*/
1789 0, /*nb_inplace_add*/
1790 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1791 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001792 0, /*nb_inplace_remainder*/
1793 0, /*nb_inplace_power*/
1794 0, /*nb_inplace_lshift*/
1795 0, /*nb_inplace_rshift*/
1796 (binaryfunc)set_iand, /*nb_inplace_and*/
1797 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1798 (binaryfunc)set_ior, /*nb_inplace_or*/
1799};
1800
1801PyDoc_STRVAR(set_doc,
1802"set(iterable) --> set object\n\
1803\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001804Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001805
1806PyTypeObject PySet_Type = {
1807 PyObject_HEAD_INIT(&PyType_Type)
1808 0, /* ob_size */
1809 "set", /* tp_name */
1810 sizeof(PySetObject), /* tp_basicsize */
1811 0, /* tp_itemsize */
1812 /* methods */
1813 (destructor)set_dealloc, /* tp_dealloc */
1814 (printfunc)set_tp_print, /* tp_print */
1815 0, /* tp_getattr */
1816 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001817 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001818 (reprfunc)set_repr, /* tp_repr */
1819 &set_as_number, /* tp_as_number */
1820 &set_as_sequence, /* tp_as_sequence */
1821 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001822 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001823 0, /* tp_call */
1824 0, /* tp_str */
1825 PyObject_GenericGetAttr, /* tp_getattro */
1826 0, /* tp_setattro */
1827 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001828 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001829 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001830 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001831 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001832 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001833 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001834 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001835 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001836 0, /* tp_iternext */
1837 set_methods, /* tp_methods */
1838 0, /* tp_members */
1839 0, /* tp_getset */
1840 0, /* tp_base */
1841 0, /* tp_dict */
1842 0, /* tp_descr_get */
1843 0, /* tp_descr_set */
1844 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001845 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001846 PyType_GenericAlloc, /* tp_alloc */
1847 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001848 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001849};
1850
1851/* frozenset object ********************************************************/
1852
1853
1854static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001855 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001856 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001857 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001858 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001859 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001860 difference_doc},
1861 {"intersection",(PyCFunction)set_intersection, METH_O,
1862 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001863 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001864 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001865 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001866 issuperset_doc},
1867 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1868 reduce_doc},
1869 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1870 symmetric_difference_doc},
1871 {"union", (PyCFunction)set_union, METH_O,
1872 union_doc},
1873 {NULL, NULL} /* sentinel */
1874};
1875
1876static PyNumberMethods frozenset_as_number = {
1877 0, /*nb_add*/
1878 (binaryfunc)set_sub, /*nb_subtract*/
1879 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001880 0, /*nb_remainder*/
1881 0, /*nb_divmod*/
1882 0, /*nb_power*/
1883 0, /*nb_negative*/
1884 0, /*nb_positive*/
1885 0, /*nb_absolute*/
1886 0, /*nb_nonzero*/
1887 0, /*nb_invert*/
1888 0, /*nb_lshift*/
1889 0, /*nb_rshift*/
1890 (binaryfunc)set_and, /*nb_and*/
1891 (binaryfunc)set_xor, /*nb_xor*/
1892 (binaryfunc)set_or, /*nb_or*/
1893};
1894
1895PyDoc_STRVAR(frozenset_doc,
1896"frozenset(iterable) --> frozenset object\n\
1897\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001898Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001899
1900PyTypeObject PyFrozenSet_Type = {
1901 PyObject_HEAD_INIT(&PyType_Type)
1902 0, /* ob_size */
1903 "frozenset", /* tp_name */
1904 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001905 0, /* tp_itemsize */
1906 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001907 (destructor)set_dealloc, /* tp_dealloc */
1908 (printfunc)set_tp_print, /* tp_print */
1909 0, /* tp_getattr */
1910 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001911 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001912 (reprfunc)set_repr, /* tp_repr */
1913 &frozenset_as_number, /* tp_as_number */
1914 &set_as_sequence, /* tp_as_sequence */
1915 0, /* tp_as_mapping */
1916 frozenset_hash, /* tp_hash */
1917 0, /* tp_call */
1918 0, /* tp_str */
1919 PyObject_GenericGetAttr, /* tp_getattro */
1920 0, /* tp_setattro */
1921 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001922 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001923 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001924 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001925 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001926 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001927 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001928 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001929 (getiterfunc)set_iter, /* tp_iter */
1930 0, /* tp_iternext */
1931 frozenset_methods, /* tp_methods */
1932 0, /* tp_members */
1933 0, /* tp_getset */
1934 0, /* tp_base */
1935 0, /* tp_dict */
1936 0, /* tp_descr_get */
1937 0, /* tp_descr_set */
1938 0, /* tp_dictoffset */
1939 0, /* tp_init */
1940 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001941 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001942 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001943};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001944
1945
1946/***** C API functions *************************************************/
1947
1948PyObject *
1949PySet_New(PyObject *iterable)
1950{
1951 return make_new_set(&PySet_Type, iterable);
1952}
1953
1954PyObject *
1955PyFrozenSet_New(PyObject *iterable)
1956{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001957 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001958
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001959 if (iterable == NULL)
1960 args = PyTuple_New(0);
1961 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001962 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001963 if (args == NULL)
1964 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001965 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001966 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001967 return result;
1968}
1969
Neal Norwitz8c49c822006-03-04 18:41:19 +00001970Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001971PySet_Size(PyObject *anyset)
1972{
1973 if (!PyAnySet_Check(anyset)) {
1974 PyErr_BadInternalCall();
1975 return -1;
1976 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001977 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001978}
1979
1980int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001981PySet_Clear(PyObject *set)
1982{
1983 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1984 PyErr_BadInternalCall();
1985 return -1;
1986 }
1987 return set_clear_internal((PySetObject *)set);
1988}
1989
1990int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001991PySet_Contains(PyObject *anyset, PyObject *key)
1992{
1993 if (!PyAnySet_Check(anyset)) {
1994 PyErr_BadInternalCall();
1995 return -1;
1996 }
1997 return set_contains_key((PySetObject *)anyset, key);
1998}
1999
2000int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002001PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002002{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002003 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002004 PyErr_BadInternalCall();
2005 return -1;
2006 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002007 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002008}
2009
2010int
2011PySet_Add(PyObject *set, PyObject *key)
2012{
2013 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2014 PyErr_BadInternalCall();
2015 return -1;
2016 }
2017 return set_add_key((PySetObject *)set, key);
2018}
2019
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002020int
2021_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2022{
2023 setentry *entry_ptr;
2024
2025 if (!PyAnySet_Check(set)) {
2026 PyErr_BadInternalCall();
2027 return -1;
2028 }
2029 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2030 return 0;
2031 *entry = entry_ptr->key;
2032 return 1;
2033}
2034
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002035PyObject *
2036PySet_Pop(PyObject *set)
2037{
2038 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2039 PyErr_BadInternalCall();
2040 return NULL;
2041 }
2042 return set_pop((PySetObject *)set);
2043}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002044
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002045int
2046_PySet_Update(PyObject *set, PyObject *iterable)
2047{
2048 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2049 PyErr_BadInternalCall();
2050 return -1;
2051 }
2052 return set_update_internal((PySetObject *)set, iterable);
2053}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002054
2055#ifdef Py_DEBUG
2056
2057/* Test code to be called with any three element set.
2058 Returns True and original set is restored. */
2059
2060#define assertRaises(call_return_value, exception) \
2061 do { \
2062 assert(call_return_value); \
2063 assert(PyErr_ExceptionMatches(exception)); \
2064 PyErr_Clear(); \
2065 } while(0)
2066
2067static PyObject *
2068test_c_api(PySetObject *so)
2069{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002070 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002071 char *s;
2072 Py_ssize_t i;
2073 PyObject *elem, *dup, *t, *f, *dup2;
2074 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002075
2076 /* Verify preconditions and exercise type/size checks */
2077 assert(PyAnySet_Check(ob));
2078 assert(PyAnySet_CheckExact(ob));
2079 assert(!PyFrozenSet_CheckExact(ob));
2080 assert(PySet_Size(ob) == 3);
2081 assert(PySet_GET_SIZE(ob) == 3);
2082
2083 /* Raise TypeError for non-iterable constructor arguments */
2084 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2085 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2086
2087 /* Raise TypeError for unhashable key */
2088 dup = PySet_New(ob);
2089 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2090 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2091 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2092
2093 /* Exercise successful pop, contains, add, and discard */
2094 elem = PySet_Pop(ob);
2095 assert(PySet_Contains(ob, elem) == 0);
2096 assert(PySet_GET_SIZE(ob) == 2);
2097 assert(PySet_Add(ob, elem) == 0);
2098 assert(PySet_Contains(ob, elem) == 1);
2099 assert(PySet_GET_SIZE(ob) == 3);
2100 assert(PySet_Discard(ob, elem) == 1);
2101 assert(PySet_GET_SIZE(ob) == 2);
2102 assert(PySet_Discard(ob, elem) == 0);
2103 assert(PySet_GET_SIZE(ob) == 2);
2104
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002105 /* Exercise clear */
2106 dup2 = PySet_New(dup);
2107 assert(PySet_Clear(dup2) == 0);
2108 assert(PySet_Size(dup2) == 0);
2109 Py_DECREF(dup2);
2110
2111 /* Raise SystemError on clear or update of frozen set */
2112 f = PyFrozenSet_New(dup);
2113 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2114 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2115 Py_DECREF(f);
2116
2117 /* Exercise direct iteration */
2118 i = 0, count = 0;
2119 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2120 s = PyString_AsString(elem);
2121 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2122 count++;
2123 }
2124 assert(count == 3);
2125
2126 /* Exercise updates */
2127 dup2 = PySet_New(NULL);
2128 assert(_PySet_Update(dup2, dup) == 0);
2129 assert(PySet_Size(dup2) == 3);
2130 assert(_PySet_Update(dup2, dup) == 0);
2131 assert(PySet_Size(dup2) == 3);
2132 Py_DECREF(dup2);
2133
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002134 /* Raise SystemError when self argument is not a set or frozenset. */
2135 t = PyTuple_New(0);
2136 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2137 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2138 Py_DECREF(t);
2139
2140 /* Raise SystemError when self argument is not a set. */
2141 f = PyFrozenSet_New(dup);
2142 assert(PySet_Size(f) == 3);
2143 assert(PyFrozenSet_CheckExact(f));
2144 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2145 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2146 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2147 Py_DECREF(f);
2148
2149 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002150 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2151 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002152 assert(PySet_GET_SIZE(ob) == 0);
2153 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2154
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002155 /* Restore the set from the copy using the PyNumber API */
2156 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2157 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002158
2159 /* Verify constructors accept NULL arguments */
2160 f = PySet_New(NULL);
2161 assert(f != NULL);
2162 assert(PySet_GET_SIZE(f) == 0);
2163 Py_DECREF(f);
2164 f = PyFrozenSet_New(NULL);
2165 assert(f != NULL);
2166 assert(PyFrozenSet_CheckExact(f));
2167 assert(PySet_GET_SIZE(f) == 0);
2168 Py_DECREF(f);
2169
2170 Py_DECREF(elem);
2171 Py_DECREF(dup);
2172 Py_RETURN_TRUE;
2173}
2174
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002175#undef assertRaises
2176
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002177#endif