blob: 05549e6cfffc05646b2261d1a3450d8de1087c4b [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 = ", ";
Georg Brandlc4996ba2006-08-28 19:37:11 +0000532 int literalform = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000533
Georg Brandlc4996ba2006-08-28 19:37:11 +0000534 if (!so->used) {
535 fprintf(fp, "%s()", so->ob_type->tp_name);
536 return 0;
537 }
538
539 if (so->ob_type == &PySet_Type) {
540 literalform = 1;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000541 fprintf(fp, "{");
Georg Brandlc4996ba2006-08-28 19:37:11 +0000542 } else
Guido van Rossum86e58e22006-08-28 15:27:34 +0000543 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000544 while (set_next(so, &pos, &entry)) {
545 fputs(emit, fp);
546 emit = separator;
547 if (PyObject_Print(entry->key, fp, 0) != 0)
548 return -1;
549 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000550 if (literalform)
Guido van Rossum86e58e22006-08-28 15:27:34 +0000551 fputs("}", fp);
552 else
553 fputs("])", fp);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000554 return 0;
555}
556
557static PyObject *
558set_repr(PySetObject *so)
559{
560 PyObject *keys, *result, *listrepr;
561
Georg Brandlc4996ba2006-08-28 19:37:11 +0000562 /* shortcut for the empty set */
563 if (!so->used)
564 return PyString_FromFormat("%s()", so->ob_type->tp_name);
565
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000566 keys = PySequence_List((PyObject *)so);
567 if (keys == NULL)
568 return NULL;
569 listrepr = PyObject_Repr(keys);
570 Py_DECREF(keys);
571 if (listrepr == NULL)
572 return NULL;
573
Guido van Rossum86e58e22006-08-28 15:27:34 +0000574 if (so->ob_type == &PySet_Type) {
575 char *s = PyString_AS_STRING(listrepr);
576 s += 1;
577 s[strlen(s)-1] = 0;
578 result = PyString_FromFormat("{%s}", s);
579 } else {
580 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
Georg Brandlc4996ba2006-08-28 19:37:11 +0000581 PyString_AS_STRING(listrepr));
Guido van Rossum86e58e22006-08-28 15:27:34 +0000582 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000583 Py_DECREF(listrepr);
584 return result;
585}
586
Martin v. Löwis18e16552006-02-15 17:27:45 +0000587static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000588set_len(PyObject *so)
589{
590 return ((PySetObject *)so)->used;
591}
592
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000594set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000595{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000596 PySetObject *other;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000597 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000598 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000599
600 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000601 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000602
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000603 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000604 if (other == so || other->used == 0)
605 /* a.update(a) or a.update({}); nothing to do */
606 return 0;
607 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000608 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000609 * that there will be no (or few) overlapping keys.
610 */
611 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
612 if (set_table_resize(so, (so->used + other->used)*2) != 0)
613 return -1;
614 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000615 for (i = 0; i <= other->mask; i++) {
616 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000617 if (entry->key != NULL &&
618 entry->key != dummy) {
619 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000620 if (set_insert_key(so, entry->key, entry->hash) == -1) {
621 Py_DECREF(entry->key);
622 return -1;
623 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000624 }
625 }
626 return 0;
627}
628
629static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000630set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000631{
632 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000633 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000634
635 if (!PyString_CheckExact(key) ||
636 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
637 hash = PyObject_Hash(key);
638 if (hash == -1)
639 return -1;
640 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000641 entry = (so->lookup)(so, key, hash);
642 if (entry == NULL)
643 return -1;
644 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000645 return key != NULL && key != dummy;
646}
647
Raymond Hettingerc991db22005-08-11 07:58:45 +0000648static int
649set_contains_entry(PySetObject *so, setentry *entry)
650{
651 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000652 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000653
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000654 lu_entry = (so->lookup)(so, entry->key, entry->hash);
655 if (lu_entry == NULL)
656 return -1;
657 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000658 return key != NULL && key != dummy;
659}
660
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000661static PyObject *
662set_pop(PySetObject *so)
663{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000664 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000665 register setentry *entry;
666 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000667
668 assert (PyAnySet_Check(so));
669 if (so->used == 0) {
670 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
671 return NULL;
672 }
673
674 /* Set entry to "the first" unused or dummy set entry. We abuse
675 * the hash field of slot 0 to hold a search finger:
676 * If slot 0 has a value, use slot 0.
677 * Else slot 0 is being used to hold a search finger,
678 * and we use its hash value as the first index to look.
679 */
680 entry = &so->table[0];
681 if (entry->key == NULL || entry->key == dummy) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000682 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000683 /* The hash field may be a real hash value, or it may be a
684 * legit search finger, or it may be a once-legit search
685 * finger that's out of bounds now because it wrapped around
686 * or the table shrunk -- simply make sure it's in bounds now.
687 */
688 if (i > so->mask || i < 1)
689 i = 1; /* skip slot 0 */
690 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
691 i++;
692 if (i > so->mask)
693 i = 1;
694 }
695 }
696 key = entry->key;
697 Py_INCREF(dummy);
698 entry->key = dummy;
699 so->used--;
700 so->table[0].hash = i + 1; /* next place to start */
701 return key;
702}
703
704PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
705
706static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000707set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000708{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000709 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000710 setentry *entry;
711
712 while (set_next(so, &pos, &entry))
713 Py_VISIT(entry->key);
714 return 0;
715}
716
717static long
718frozenset_hash(PyObject *self)
719{
720 PySetObject *so = (PySetObject *)self;
721 long h, hash = 1927868237L;
722 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000723 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000724
725 if (so->hash != -1)
726 return so->hash;
727
728 hash *= PySet_GET_SIZE(self) + 1;
729 while (set_next(so, &pos, &entry)) {
730 /* Work to increase the bit dispersion for closely spaced hash
731 values. The is important because some use cases have many
732 combinations of a small number of elements with nearby
733 hashes so that many distinct combinations collapse to only
734 a handful of distinct hash values. */
735 h = entry->hash;
736 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
737 }
738 hash = hash * 69069L + 907133923L;
739 if (hash == -1)
740 hash = 590923713L;
741 so->hash = hash;
742 return hash;
743}
744
Raymond Hettingera9d99362005-08-05 00:01:15 +0000745/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000746
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000747typedef struct {
748 PyObject_HEAD
749 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000750 Py_ssize_t si_used;
751 Py_ssize_t si_pos;
752 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000753} setiterobject;
754
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000755static void
756setiter_dealloc(setiterobject *si)
757{
758 Py_XDECREF(si->si_set);
759 PyObject_Del(si);
760}
761
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000762static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000763setiter_len(setiterobject *si)
764{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000765 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000766 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000767 len = si->len;
768 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000769}
770
Armin Rigof5b3e362006-02-11 21:32:43 +0000771PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000772
773static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000774 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000775 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000776};
777
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000778static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000779{
780 PyObject *key;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000781 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000782 register setentry *entry;
783 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000784
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000785 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000786 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000787 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000789 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790 PyErr_SetString(PyExc_RuntimeError,
791 "Set changed size during iteration");
792 si->si_used = -1; /* Make this state sticky */
793 return NULL;
794 }
795
796 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000797 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000798 entry = so->table;
799 mask = so->mask;
800 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801 i++;
802 si->si_pos = i+1;
803 if (i > mask)
804 goto fail;
805 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000806 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807 Py_INCREF(key);
808 return key;
809
810fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000811 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812 si->si_set = NULL;
813 return NULL;
814}
815
Hye-Shik Change2956762005-08-01 05:26:41 +0000816static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000817 PyObject_HEAD_INIT(&PyType_Type)
818 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000819 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000820 sizeof(setiterobject), /* tp_basicsize */
821 0, /* tp_itemsize */
822 /* methods */
823 (destructor)setiter_dealloc, /* tp_dealloc */
824 0, /* tp_print */
825 0, /* tp_getattr */
826 0, /* tp_setattr */
827 0, /* tp_compare */
828 0, /* tp_repr */
829 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000830 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831 0, /* tp_as_mapping */
832 0, /* tp_hash */
833 0, /* tp_call */
834 0, /* tp_str */
835 PyObject_GenericGetAttr, /* tp_getattro */
836 0, /* tp_setattro */
837 0, /* tp_as_buffer */
838 Py_TPFLAGS_DEFAULT, /* tp_flags */
839 0, /* tp_doc */
840 0, /* tp_traverse */
841 0, /* tp_clear */
842 0, /* tp_richcompare */
843 0, /* tp_weaklistoffset */
844 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000845 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000846 setiter_methods, /* tp_methods */
847 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848};
849
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000850static PyObject *
851set_iter(PySetObject *so)
852{
853 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
854 if (si == NULL)
855 return NULL;
856 Py_INCREF(so);
857 si->si_set = so;
858 si->si_used = so->used;
859 si->si_pos = 0;
860 si->len = so->used;
861 return (PyObject *)si;
862}
863
Raymond Hettingerd7946662005-08-01 21:39:29 +0000864static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000865set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000866{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000867 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000868
Raymond Hettingerd7946662005-08-01 21:39:29 +0000869 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000870 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000871
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872 if (PyDict_Check(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000873 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000874 Py_ssize_t pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000875 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000876 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000877 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000878 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000879 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880 }
881
Raymond Hettingera38123e2003-11-24 22:18:49 +0000882 it = PyObject_GetIter(other);
883 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000884 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000885
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000886 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000887 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000888 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000889 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000890 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000891 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000892 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000893 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000894 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000895 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000896 return -1;
897 return 0;
898}
899
900static PyObject *
901set_update(PySetObject *so, PyObject *other)
902{
903 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000904 return NULL;
905 Py_RETURN_NONE;
906}
907
908PyDoc_STRVAR(update_doc,
909"Update a set with the union of itself and another.");
910
911static PyObject *
912make_new_set(PyTypeObject *type, PyObject *iterable)
913{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000914 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000915
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000916 if (dummy == NULL) { /* Auto-initialize dummy */
917 dummy = PyString_FromString("<dummy key>");
918 if (dummy == NULL)
919 return NULL;
920 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000921
922 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000923 if (num_free_sets &&
924 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
925 so = free_sets[--num_free_sets];
926 assert (so != NULL && PyAnySet_CheckExact(so));
927 so->ob_type = type;
928 _Py_NewReference((PyObject *)so);
929 EMPTY_TO_MINSIZE(so);
930 PyObject_GC_Track(so);
931 } else {
932 so = (PySetObject *)type->tp_alloc(type, 0);
933 if (so == NULL)
934 return NULL;
935 /* tp_alloc has already zeroed the structure */
936 assert(so->table == NULL && so->fill == 0 && so->used == 0);
937 INIT_NONZERO_SET_SLOTS(so);
938 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000939
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000940 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000941 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000942
Raymond Hettingera38123e2003-11-24 22:18:49 +0000943 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000944 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000945 Py_DECREF(so);
946 return NULL;
947 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000948 }
949
Raymond Hettingera690a992003-11-16 16:17:49 +0000950 return (PyObject *)so;
951}
952
Raymond Hettingerd7946662005-08-01 21:39:29 +0000953/* The empty frozenset is a singleton */
954static PyObject *emptyfrozenset = NULL;
955
Raymond Hettingera690a992003-11-16 16:17:49 +0000956static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000957frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000958{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000959 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000960
Georg Brandl02c42872005-08-26 06:42:30 +0000961 if (!_PyArg_NoKeywords("frozenset()", kwds))
962 return NULL;
963
Raymond Hettingera690a992003-11-16 16:17:49 +0000964 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
965 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000966
967 if (type != &PyFrozenSet_Type)
968 return make_new_set(type, iterable);
969
970 if (iterable != NULL) {
971 /* frozenset(f) is idempotent */
972 if (PyFrozenSet_CheckExact(iterable)) {
973 Py_INCREF(iterable);
974 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000975 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000976 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000977 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000978 return result;
979 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000980 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000981 /* The empty frozenset is a singleton */
982 if (emptyfrozenset == NULL)
983 emptyfrozenset = make_new_set(type, NULL);
984 Py_XINCREF(emptyfrozenset);
985 return emptyfrozenset;
986}
987
988void
989PySet_Fini(void)
990{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000991 PySetObject *so;
992
993 while (num_free_sets) {
994 num_free_sets--;
995 so = free_sets[num_free_sets];
996 PyObject_GC_Del(so);
997 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998 Py_CLEAR(dummy);
999 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001000}
1001
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001002static PyObject *
1003set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1004{
Georg Brandl02c42872005-08-26 06:42:30 +00001005 if (!_PyArg_NoKeywords("set()", kwds))
1006 return NULL;
1007
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001008 return make_new_set(type, NULL);
1009}
1010
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001011/* set_swap_bodies() switches the contents of any two sets by moving their
1012 internal data pointers and, if needed, copying the internal smalltables.
1013 Semantically equivalent to:
1014
1015 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1016
1017 The function always succeeds and it leaves both objects in a stable state.
1018 Useful for creating temporary frozensets from sets for membership testing
1019 in __contains__(), discard(), and remove(). Also useful for operations
1020 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001021 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001022*/
1023
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001024static void
1025set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001026{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001027 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001028 setentry *u;
1029 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1030 setentry tab[PySet_MINSIZE];
1031 long h;
1032
1033 t = a->fill; a->fill = b->fill; b->fill = t;
1034 t = a->used; a->used = b->used; b->used = t;
1035 t = a->mask; a->mask = b->mask; b->mask = t;
1036
1037 u = a->table;
1038 if (a->table == a->smalltable)
1039 u = b->smalltable;
1040 a->table = b->table;
1041 if (b->table == b->smalltable)
1042 a->table = a->smalltable;
1043 b->table = u;
1044
1045 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1046
1047 if (a->table == a->smalltable || b->table == b->smalltable) {
1048 memcpy(tab, a->smalltable, sizeof(tab));
1049 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1050 memcpy(b->smalltable, tab, sizeof(tab));
1051 }
1052
Raymond Hettingera580c472005-08-05 17:19:54 +00001053 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1054 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1055 h = a->hash; a->hash = b->hash; b->hash = h;
1056 } else {
1057 a->hash = -1;
1058 b->hash = -1;
1059 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001060}
1061
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001062static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001063set_copy(PySetObject *so)
1064{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001065 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001066}
1067
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001068static PyObject *
1069frozenset_copy(PySetObject *so)
1070{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001071 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001072 Py_INCREF(so);
1073 return (PyObject *)so;
1074 }
1075 return set_copy(so);
1076}
1077
Raymond Hettingera690a992003-11-16 16:17:49 +00001078PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1079
1080static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001081set_clear(PySetObject *so)
1082{
1083 set_clear_internal(so);
1084 Py_RETURN_NONE;
1085}
1086
1087PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1088
1089static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001090set_union(PySetObject *so, PyObject *other)
1091{
1092 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001093
1094 result = (PySetObject *)set_copy(so);
1095 if (result == NULL)
1096 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001097 if ((PyObject *)so == other)
1098 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001099 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001100 Py_DECREF(result);
1101 return NULL;
1102 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001103 return (PyObject *)result;
1104}
1105
1106PyDoc_STRVAR(union_doc,
1107 "Return the union of two sets as a new set.\n\
1108\n\
1109(i.e. all elements that are in either set.)");
1110
1111static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001112set_or(PySetObject *so, PyObject *other)
1113{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001114 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001115 Py_INCREF(Py_NotImplemented);
1116 return Py_NotImplemented;
1117 }
1118 return set_union(so, other);
1119}
1120
1121static PyObject *
1122set_ior(PySetObject *so, PyObject *other)
1123{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001124 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001125 Py_INCREF(Py_NotImplemented);
1126 return Py_NotImplemented;
1127 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001128 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001129 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001130 Py_INCREF(so);
1131 return (PyObject *)so;
1132}
1133
1134static PyObject *
1135set_intersection(PySetObject *so, PyObject *other)
1136{
1137 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001138 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001139
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001140 if ((PyObject *)so == other)
1141 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001142
Raymond Hettingera690a992003-11-16 16:17:49 +00001143 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1144 if (result == NULL)
1145 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001146
Raymond Hettingerc991db22005-08-11 07:58:45 +00001147 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001148 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001149 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001150
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001151 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001152 tmp = (PyObject *)so;
1153 so = (PySetObject *)other;
1154 other = tmp;
1155 }
1156
Raymond Hettingerc991db22005-08-11 07:58:45 +00001157 while (set_next((PySetObject *)other, &pos, &entry)) {
1158 if (set_contains_entry(so, entry)) {
1159 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001160 Py_DECREF(result);
1161 return NULL;
1162 }
1163 }
1164 }
1165 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001166 }
1167
Raymond Hettingera690a992003-11-16 16:17:49 +00001168 it = PyObject_GetIter(other);
1169 if (it == NULL) {
1170 Py_DECREF(result);
1171 return NULL;
1172 }
1173
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001174 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001175 if (set_contains_key(so, key)) {
1176 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001177 Py_DECREF(it);
1178 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001179 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001180 return NULL;
1181 }
1182 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001183 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001184 }
1185 Py_DECREF(it);
1186 if (PyErr_Occurred()) {
1187 Py_DECREF(result);
1188 return NULL;
1189 }
1190 return (PyObject *)result;
1191}
1192
1193PyDoc_STRVAR(intersection_doc,
1194"Return the intersection of two sets as a new set.\n\
1195\n\
1196(i.e. all elements that are in both sets.)");
1197
1198static PyObject *
1199set_intersection_update(PySetObject *so, PyObject *other)
1200{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001201 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001202
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001203 tmp = set_intersection(so, other);
1204 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001205 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001206 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001207 Py_DECREF(tmp);
1208 Py_RETURN_NONE;
1209}
1210
1211PyDoc_STRVAR(intersection_update_doc,
1212"Update a set with the intersection of itself and another.");
1213
1214static PyObject *
1215set_and(PySetObject *so, PyObject *other)
1216{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001217 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001218 Py_INCREF(Py_NotImplemented);
1219 return Py_NotImplemented;
1220 }
1221 return set_intersection(so, other);
1222}
1223
1224static PyObject *
1225set_iand(PySetObject *so, PyObject *other)
1226{
1227 PyObject *result;
1228
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001229 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001230 Py_INCREF(Py_NotImplemented);
1231 return Py_NotImplemented;
1232 }
1233 result = set_intersection_update(so, other);
1234 if (result == NULL)
1235 return NULL;
1236 Py_DECREF(result);
1237 Py_INCREF(so);
1238 return (PyObject *)so;
1239}
1240
Neal Norwitz6576bd82005-11-13 18:41:28 +00001241static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001242set_difference_update_internal(PySetObject *so, PyObject *other)
1243{
1244 if ((PyObject *)so == other)
1245 return set_clear_internal(so);
1246
1247 if (PyAnySet_Check(other)) {
1248 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001249 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001250
1251 while (set_next((PySetObject *)other, &pos, &entry))
1252 set_discard_entry(so, entry);
1253 } else {
1254 PyObject *key, *it;
1255 it = PyObject_GetIter(other);
1256 if (it == NULL)
1257 return -1;
1258
1259 while ((key = PyIter_Next(it)) != NULL) {
1260 if (set_discard_key(so, key) == -1) {
1261 Py_DECREF(it);
1262 Py_DECREF(key);
1263 return -1;
1264 }
1265 Py_DECREF(key);
1266 }
1267 Py_DECREF(it);
1268 if (PyErr_Occurred())
1269 return -1;
1270 }
1271 /* If more than 1/5 are dummies, then resize them away. */
1272 if ((so->fill - so->used) * 5 < so->mask)
1273 return 0;
1274 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1275}
1276
Raymond Hettingera690a992003-11-16 16:17:49 +00001277static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001278set_difference_update(PySetObject *so, PyObject *other)
1279{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001280 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001281 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001282 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001283}
1284
1285PyDoc_STRVAR(difference_update_doc,
1286"Remove all elements of another set from this set.");
1287
1288static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001289set_difference(PySetObject *so, PyObject *other)
1290{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001291 PyObject *result;
1292 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001293 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001294
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001295 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001296 result = set_copy(so);
1297 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001298 return NULL;
1299 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001300 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001301 Py_DECREF(result);
1302 return NULL;
1303 }
1304
1305 result = make_new_set(so->ob_type, NULL);
1306 if (result == NULL)
1307 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001308
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001309 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001310 while (set_next(so, &pos, &entry)) {
1311 setentry entrycopy;
1312 entrycopy.hash = entry->hash;
1313 entrycopy.key = entry->key;
1314 if (!PyDict_Contains(other, entry->key)) {
1315 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001316 return NULL;
1317 }
1318 }
1319 return result;
1320 }
1321
Raymond Hettingerc991db22005-08-11 07:58:45 +00001322 while (set_next(so, &pos, &entry)) {
1323 if (!set_contains_entry((PySetObject *)other, entry)) {
1324 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001325 return NULL;
1326 }
1327 }
1328 return result;
1329}
1330
1331PyDoc_STRVAR(difference_doc,
1332"Return the difference of two sets as a new set.\n\
1333\n\
1334(i.e. all elements that are in this set but not the other.)");
1335static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001336set_sub(PySetObject *so, PyObject *other)
1337{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001338 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001339 Py_INCREF(Py_NotImplemented);
1340 return Py_NotImplemented;
1341 }
1342 return set_difference(so, other);
1343}
1344
1345static PyObject *
1346set_isub(PySetObject *so, PyObject *other)
1347{
1348 PyObject *result;
1349
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001350 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001351 Py_INCREF(Py_NotImplemented);
1352 return Py_NotImplemented;
1353 }
1354 result = set_difference_update(so, other);
1355 if (result == NULL)
1356 return NULL;
1357 Py_DECREF(result);
1358 Py_INCREF(so);
1359 return (PyObject *)so;
1360}
1361
1362static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001363set_symmetric_difference_update(PySetObject *so, PyObject *other)
1364{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001365 PySetObject *otherset;
1366 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001367 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001368 setentry *entry;
1369
1370 if ((PyObject *)so == other)
1371 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001372
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001373 if (PyDict_Check(other)) {
1374 PyObject *value;
1375 int rv;
1376 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001377 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001378 if (rv == -1)
1379 return NULL;
1380 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001381 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001382 return NULL;
1383 }
1384 }
1385 Py_RETURN_NONE;
1386 }
1387
1388 if (PyAnySet_Check(other)) {
1389 Py_INCREF(other);
1390 otherset = (PySetObject *)other;
1391 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001392 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1393 if (otherset == NULL)
1394 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001395 }
1396
Raymond Hettingerc991db22005-08-11 07:58:45 +00001397 while (set_next(otherset, &pos, &entry)) {
1398 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001399 if (rv == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001400 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001401 return NULL;
1402 }
1403 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001404 if (set_add_entry(so, entry) == -1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001405 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001406 return NULL;
1407 }
1408 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001409 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001410 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001411 Py_RETURN_NONE;
1412}
1413
1414PyDoc_STRVAR(symmetric_difference_update_doc,
1415"Update a set with the symmetric difference of itself and another.");
1416
1417static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001418set_symmetric_difference(PySetObject *so, PyObject *other)
1419{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001420 PyObject *rv;
1421 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001422
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001423 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1424 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001425 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001426 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1427 if (rv == NULL)
1428 return NULL;
1429 Py_DECREF(rv);
1430 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001431}
1432
1433PyDoc_STRVAR(symmetric_difference_doc,
1434"Return the symmetric difference of two sets as a new set.\n\
1435\n\
1436(i.e. all elements that are in exactly one of the sets.)");
1437
1438static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001439set_xor(PySetObject *so, PyObject *other)
1440{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001441 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001442 Py_INCREF(Py_NotImplemented);
1443 return Py_NotImplemented;
1444 }
1445 return set_symmetric_difference(so, other);
1446}
1447
1448static PyObject *
1449set_ixor(PySetObject *so, PyObject *other)
1450{
1451 PyObject *result;
1452
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001453 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001454 Py_INCREF(Py_NotImplemented);
1455 return Py_NotImplemented;
1456 }
1457 result = set_symmetric_difference_update(so, other);
1458 if (result == NULL)
1459 return NULL;
1460 Py_DECREF(result);
1461 Py_INCREF(so);
1462 return (PyObject *)so;
1463}
1464
1465static PyObject *
1466set_issubset(PySetObject *so, PyObject *other)
1467{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001468 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001469 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001470
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001471 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001472 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001473 tmp = make_new_set(&PySet_Type, other);
1474 if (tmp == NULL)
1475 return NULL;
1476 result = set_issubset(so, tmp);
1477 Py_DECREF(tmp);
1478 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001479 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001480 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001481 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001482
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001483 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001484 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001485 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001486 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001487 Py_RETURN_TRUE;
1488}
1489
1490PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1491
1492static PyObject *
1493set_issuperset(PySetObject *so, PyObject *other)
1494{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001495 PyObject *tmp, *result;
1496
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001497 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001498 tmp = make_new_set(&PySet_Type, other);
1499 if (tmp == NULL)
1500 return NULL;
1501 result = set_issuperset(so, tmp);
1502 Py_DECREF(tmp);
1503 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001504 }
1505 return set_issubset((PySetObject *)other, (PyObject *)so);
1506}
1507
1508PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1509
Raymond Hettingera690a992003-11-16 16:17:49 +00001510static PyObject *
1511set_richcompare(PySetObject *v, PyObject *w, int op)
1512{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001513 PyObject *r1, *r2;
1514
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001515 if(!PyAnySet_Check(w)) {
1516 if (op == Py_EQ)
1517 Py_RETURN_FALSE;
1518 if (op == Py_NE)
1519 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001520 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1521 return NULL;
1522 }
1523 switch (op) {
1524 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001525 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001526 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001527 if (v->hash != -1 &&
1528 ((PySetObject *)w)->hash != -1 &&
1529 v->hash != ((PySetObject *)w)->hash)
1530 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001531 return set_issubset(v, w);
1532 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001533 r1 = set_richcompare(v, w, Py_EQ);
1534 if (r1 == NULL)
1535 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001536 r2 = PyBool_FromLong(PyObject_Not(r1));
1537 Py_DECREF(r1);
1538 return r2;
1539 case Py_LE:
1540 return set_issubset(v, w);
1541 case Py_GE:
1542 return set_issuperset(v, w);
1543 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001544 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001545 Py_RETURN_FALSE;
1546 return set_issubset(v, w);
1547 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001548 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001549 Py_RETURN_FALSE;
1550 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001551 }
1552 Py_INCREF(Py_NotImplemented);
1553 return Py_NotImplemented;
1554}
1555
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001556static int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001557set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001558{
1559 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1560 return -1;
1561}
1562
Raymond Hettingera690a992003-11-16 16:17:49 +00001563static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001564set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001565{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001566 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001567 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001568 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001569}
1570
1571PyDoc_STRVAR(add_doc,
1572"Add an element to a set.\n\
1573\n\
1574This has no effect if the element is already present.");
1575
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001576static int
1577set_contains(PySetObject *so, PyObject *key)
1578{
1579 PyObject *tmpkey;
1580 int rv;
1581
1582 rv = set_contains_key(so, key);
1583 if (rv == -1) {
1584 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1585 return -1;
1586 PyErr_Clear();
1587 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1588 if (tmpkey == NULL)
1589 return -1;
1590 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1591 rv = set_contains(so, tmpkey);
1592 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1593 Py_DECREF(tmpkey);
1594 }
1595 return rv;
1596}
1597
1598static PyObject *
1599set_direct_contains(PySetObject *so, PyObject *key)
1600{
1601 long result;
1602
1603 result = set_contains(so, key);
1604 if (result == -1)
1605 return NULL;
1606 return PyBool_FromLong(result);
1607}
1608
1609PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1610
Raymond Hettingera690a992003-11-16 16:17:49 +00001611static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001612set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001613{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001614 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001615 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001616
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001617 rv = set_discard_key(so, key);
1618 if (rv == -1) {
1619 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1620 return NULL;
1621 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001622 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1623 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001624 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001625 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001626 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001627 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001628 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001629 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001630 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001631 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001632 return NULL;
1633 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001634 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001635}
1636
1637PyDoc_STRVAR(remove_doc,
1638"Remove an element from a set; it must be a member.\n\
1639\n\
1640If the element is not a member, raise a KeyError.");
1641
1642static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001643set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001644{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001645 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001646 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001647
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001648 rv = set_discard_key(so, key);
1649 if (rv == -1) {
1650 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1651 return NULL;
1652 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001653 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1654 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001655 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001656 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001657 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001658 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001659 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001660 return result;
1661 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001662 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001663}
1664
1665PyDoc_STRVAR(discard_doc,
1666"Remove an element from a set if it is a member.\n\
1667\n\
1668If the element is not a member, do nothing.");
1669
1670static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001671set_reduce(PySetObject *so)
1672{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001673 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001674
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001675 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001676 if (keys == NULL)
1677 goto done;
1678 args = PyTuple_Pack(1, keys);
1679 if (args == NULL)
1680 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001681 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1682 if (dict == NULL) {
1683 PyErr_Clear();
1684 dict = Py_None;
1685 Py_INCREF(dict);
1686 }
1687 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001688done:
1689 Py_XDECREF(args);
1690 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001691 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001692 return result;
1693}
1694
1695PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1696
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001697static int
1698set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1699{
1700 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001701
1702 if (!PyAnySet_Check(self))
1703 return -1;
1704 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1705 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001706 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001707 self->hash = -1;
1708 if (iterable == NULL)
1709 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001710 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001711}
1712
Raymond Hettingera690a992003-11-16 16:17:49 +00001713static PySequenceMethods set_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001714 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001715 0, /* sq_concat */
1716 0, /* sq_repeat */
1717 0, /* sq_item */
1718 0, /* sq_slice */
1719 0, /* sq_ass_item */
1720 0, /* sq_ass_slice */
1721 (objobjproc)set_contains, /* sq_contains */
1722};
1723
1724/* set object ********************************************************/
1725
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001726#ifdef Py_DEBUG
1727static PyObject *test_c_api(PySetObject *so);
1728
1729PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1730All is well if assertions don't fail.");
1731#endif
1732
Raymond Hettingera690a992003-11-16 16:17:49 +00001733static PyMethodDef set_methods[] = {
1734 {"add", (PyCFunction)set_add, METH_O,
1735 add_doc},
1736 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1737 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001738 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001739 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001740 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1741 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001742 {"discard", (PyCFunction)set_discard, METH_O,
1743 discard_doc},
1744 {"difference", (PyCFunction)set_difference, METH_O,
1745 difference_doc},
1746 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1747 difference_update_doc},
1748 {"intersection",(PyCFunction)set_intersection, METH_O,
1749 intersection_doc},
1750 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1751 intersection_update_doc},
1752 {"issubset", (PyCFunction)set_issubset, METH_O,
1753 issubset_doc},
1754 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1755 issuperset_doc},
1756 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1757 pop_doc},
1758 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1759 reduce_doc},
1760 {"remove", (PyCFunction)set_remove, METH_O,
1761 remove_doc},
1762 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1763 symmetric_difference_doc},
1764 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1765 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001766#ifdef Py_DEBUG
1767 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1768 test_c_api_doc},
1769#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001770 {"union", (PyCFunction)set_union, METH_O,
1771 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001772 {"update", (PyCFunction)set_update, METH_O,
1773 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001774 {NULL, NULL} /* sentinel */
1775};
1776
1777static PyNumberMethods set_as_number = {
1778 0, /*nb_add*/
1779 (binaryfunc)set_sub, /*nb_subtract*/
1780 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001781 0, /*nb_remainder*/
1782 0, /*nb_divmod*/
1783 0, /*nb_power*/
1784 0, /*nb_negative*/
1785 0, /*nb_positive*/
1786 0, /*nb_absolute*/
1787 0, /*nb_nonzero*/
1788 0, /*nb_invert*/
1789 0, /*nb_lshift*/
1790 0, /*nb_rshift*/
1791 (binaryfunc)set_and, /*nb_and*/
1792 (binaryfunc)set_xor, /*nb_xor*/
1793 (binaryfunc)set_or, /*nb_or*/
1794 0, /*nb_coerce*/
1795 0, /*nb_int*/
1796 0, /*nb_long*/
1797 0, /*nb_float*/
1798 0, /*nb_oct*/
1799 0, /*nb_hex*/
1800 0, /*nb_inplace_add*/
1801 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1802 0, /*nb_inplace_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001803 0, /*nb_inplace_remainder*/
1804 0, /*nb_inplace_power*/
1805 0, /*nb_inplace_lshift*/
1806 0, /*nb_inplace_rshift*/
1807 (binaryfunc)set_iand, /*nb_inplace_and*/
1808 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1809 (binaryfunc)set_ior, /*nb_inplace_or*/
1810};
1811
1812PyDoc_STRVAR(set_doc,
1813"set(iterable) --> set object\n\
1814\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001815Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001816
1817PyTypeObject PySet_Type = {
1818 PyObject_HEAD_INIT(&PyType_Type)
1819 0, /* ob_size */
1820 "set", /* tp_name */
1821 sizeof(PySetObject), /* tp_basicsize */
1822 0, /* tp_itemsize */
1823 /* methods */
1824 (destructor)set_dealloc, /* tp_dealloc */
1825 (printfunc)set_tp_print, /* tp_print */
1826 0, /* tp_getattr */
1827 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001828 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001829 (reprfunc)set_repr, /* tp_repr */
1830 &set_as_number, /* tp_as_number */
1831 &set_as_sequence, /* tp_as_sequence */
1832 0, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001833 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00001834 0, /* tp_call */
1835 0, /* tp_str */
1836 PyObject_GenericGetAttr, /* tp_getattro */
1837 0, /* tp_setattro */
1838 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001839 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001840 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001841 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001842 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001843 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001844 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001845 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001846 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001847 0, /* tp_iternext */
1848 set_methods, /* tp_methods */
1849 0, /* tp_members */
1850 0, /* tp_getset */
1851 0, /* tp_base */
1852 0, /* tp_dict */
1853 0, /* tp_descr_get */
1854 0, /* tp_descr_set */
1855 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001856 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001857 PyType_GenericAlloc, /* tp_alloc */
1858 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001859 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001860};
1861
1862/* frozenset object ********************************************************/
1863
1864
1865static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001866 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001867 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001868 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001869 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001870 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001871 difference_doc},
1872 {"intersection",(PyCFunction)set_intersection, METH_O,
1873 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001874 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001875 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001876 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001877 issuperset_doc},
1878 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1879 reduce_doc},
1880 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1881 symmetric_difference_doc},
1882 {"union", (PyCFunction)set_union, METH_O,
1883 union_doc},
1884 {NULL, NULL} /* sentinel */
1885};
1886
1887static PyNumberMethods frozenset_as_number = {
1888 0, /*nb_add*/
1889 (binaryfunc)set_sub, /*nb_subtract*/
1890 0, /*nb_multiply*/
Raymond Hettingera690a992003-11-16 16:17:49 +00001891 0, /*nb_remainder*/
1892 0, /*nb_divmod*/
1893 0, /*nb_power*/
1894 0, /*nb_negative*/
1895 0, /*nb_positive*/
1896 0, /*nb_absolute*/
1897 0, /*nb_nonzero*/
1898 0, /*nb_invert*/
1899 0, /*nb_lshift*/
1900 0, /*nb_rshift*/
1901 (binaryfunc)set_and, /*nb_and*/
1902 (binaryfunc)set_xor, /*nb_xor*/
1903 (binaryfunc)set_or, /*nb_or*/
1904};
1905
1906PyDoc_STRVAR(frozenset_doc,
1907"frozenset(iterable) --> frozenset object\n\
1908\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001909Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001910
1911PyTypeObject PyFrozenSet_Type = {
1912 PyObject_HEAD_INIT(&PyType_Type)
1913 0, /* ob_size */
1914 "frozenset", /* tp_name */
1915 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001916 0, /* tp_itemsize */
1917 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001918 (destructor)set_dealloc, /* tp_dealloc */
1919 (printfunc)set_tp_print, /* tp_print */
1920 0, /* tp_getattr */
1921 0, /* tp_setattr */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001922 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001923 (reprfunc)set_repr, /* tp_repr */
1924 &frozenset_as_number, /* tp_as_number */
1925 &set_as_sequence, /* tp_as_sequence */
1926 0, /* tp_as_mapping */
1927 frozenset_hash, /* tp_hash */
1928 0, /* tp_call */
1929 0, /* tp_str */
1930 PyObject_GenericGetAttr, /* tp_getattro */
1931 0, /* tp_setattro */
1932 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001933 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001934 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001935 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001936 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001937 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001938 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001939 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001940 (getiterfunc)set_iter, /* tp_iter */
1941 0, /* tp_iternext */
1942 frozenset_methods, /* tp_methods */
1943 0, /* tp_members */
1944 0, /* tp_getset */
1945 0, /* tp_base */
1946 0, /* tp_dict */
1947 0, /* tp_descr_get */
1948 0, /* tp_descr_set */
1949 0, /* tp_dictoffset */
1950 0, /* tp_init */
1951 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001952 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001953 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001954};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001955
1956
1957/***** C API functions *************************************************/
1958
1959PyObject *
1960PySet_New(PyObject *iterable)
1961{
1962 return make_new_set(&PySet_Type, iterable);
1963}
1964
1965PyObject *
1966PyFrozenSet_New(PyObject *iterable)
1967{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001968 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001969
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001970 if (iterable == NULL)
1971 args = PyTuple_New(0);
1972 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001973 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001974 if (args == NULL)
1975 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001976 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001977 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001978 return result;
1979}
1980
Neal Norwitz8c49c822006-03-04 18:41:19 +00001981Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001982PySet_Size(PyObject *anyset)
1983{
1984 if (!PyAnySet_Check(anyset)) {
1985 PyErr_BadInternalCall();
1986 return -1;
1987 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001988 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001989}
1990
1991int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001992PySet_Clear(PyObject *set)
1993{
1994 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1995 PyErr_BadInternalCall();
1996 return -1;
1997 }
1998 return set_clear_internal((PySetObject *)set);
1999}
2000
2001int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002002PySet_Contains(PyObject *anyset, PyObject *key)
2003{
2004 if (!PyAnySet_Check(anyset)) {
2005 PyErr_BadInternalCall();
2006 return -1;
2007 }
2008 return set_contains_key((PySetObject *)anyset, key);
2009}
2010
2011int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002012PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002013{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002014 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002015 PyErr_BadInternalCall();
2016 return -1;
2017 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002018 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002019}
2020
2021int
2022PySet_Add(PyObject *set, PyObject *key)
2023{
2024 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2025 PyErr_BadInternalCall();
2026 return -1;
2027 }
2028 return set_add_key((PySetObject *)set, key);
2029}
2030
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002031int
2032_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2033{
2034 setentry *entry_ptr;
2035
2036 if (!PyAnySet_Check(set)) {
2037 PyErr_BadInternalCall();
2038 return -1;
2039 }
2040 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2041 return 0;
2042 *entry = entry_ptr->key;
2043 return 1;
2044}
2045
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002046PyObject *
2047PySet_Pop(PyObject *set)
2048{
2049 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2050 PyErr_BadInternalCall();
2051 return NULL;
2052 }
2053 return set_pop((PySetObject *)set);
2054}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002055
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002056int
2057_PySet_Update(PyObject *set, PyObject *iterable)
2058{
2059 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2060 PyErr_BadInternalCall();
2061 return -1;
2062 }
2063 return set_update_internal((PySetObject *)set, iterable);
2064}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002065
2066#ifdef Py_DEBUG
2067
2068/* Test code to be called with any three element set.
2069 Returns True and original set is restored. */
2070
2071#define assertRaises(call_return_value, exception) \
2072 do { \
2073 assert(call_return_value); \
2074 assert(PyErr_ExceptionMatches(exception)); \
2075 PyErr_Clear(); \
2076 } while(0)
2077
2078static PyObject *
2079test_c_api(PySetObject *so)
2080{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002081 Py_ssize_t count;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002082 char *s;
2083 Py_ssize_t i;
2084 PyObject *elem, *dup, *t, *f, *dup2;
2085 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002086
2087 /* Verify preconditions and exercise type/size checks */
2088 assert(PyAnySet_Check(ob));
2089 assert(PyAnySet_CheckExact(ob));
2090 assert(!PyFrozenSet_CheckExact(ob));
2091 assert(PySet_Size(ob) == 3);
2092 assert(PySet_GET_SIZE(ob) == 3);
2093
2094 /* Raise TypeError for non-iterable constructor arguments */
2095 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2096 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2097
2098 /* Raise TypeError for unhashable key */
2099 dup = PySet_New(ob);
2100 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2101 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2102 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2103
2104 /* Exercise successful pop, contains, add, and discard */
2105 elem = PySet_Pop(ob);
2106 assert(PySet_Contains(ob, elem) == 0);
2107 assert(PySet_GET_SIZE(ob) == 2);
2108 assert(PySet_Add(ob, elem) == 0);
2109 assert(PySet_Contains(ob, elem) == 1);
2110 assert(PySet_GET_SIZE(ob) == 3);
2111 assert(PySet_Discard(ob, elem) == 1);
2112 assert(PySet_GET_SIZE(ob) == 2);
2113 assert(PySet_Discard(ob, elem) == 0);
2114 assert(PySet_GET_SIZE(ob) == 2);
2115
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002116 /* Exercise clear */
2117 dup2 = PySet_New(dup);
2118 assert(PySet_Clear(dup2) == 0);
2119 assert(PySet_Size(dup2) == 0);
2120 Py_DECREF(dup2);
2121
2122 /* Raise SystemError on clear or update of frozen set */
2123 f = PyFrozenSet_New(dup);
2124 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2125 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2126 Py_DECREF(f);
2127
2128 /* Exercise direct iteration */
2129 i = 0, count = 0;
2130 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2131 s = PyString_AsString(elem);
2132 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2133 count++;
2134 }
2135 assert(count == 3);
2136
2137 /* Exercise updates */
2138 dup2 = PySet_New(NULL);
2139 assert(_PySet_Update(dup2, dup) == 0);
2140 assert(PySet_Size(dup2) == 3);
2141 assert(_PySet_Update(dup2, dup) == 0);
2142 assert(PySet_Size(dup2) == 3);
2143 Py_DECREF(dup2);
2144
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002145 /* Raise SystemError when self argument is not a set or frozenset. */
2146 t = PyTuple_New(0);
2147 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2148 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2149 Py_DECREF(t);
2150
2151 /* Raise SystemError when self argument is not a set. */
2152 f = PyFrozenSet_New(dup);
2153 assert(PySet_Size(f) == 3);
2154 assert(PyFrozenSet_CheckExact(f));
2155 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2156 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2157 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2158 Py_DECREF(f);
2159
2160 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002161 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2162 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002163 assert(PySet_GET_SIZE(ob) == 0);
2164 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2165
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002166 /* Restore the set from the copy using the PyNumber API */
2167 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2168 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002169
2170 /* Verify constructors accept NULL arguments */
2171 f = PySet_New(NULL);
2172 assert(f != NULL);
2173 assert(PySet_GET_SIZE(f) == 0);
2174 Py_DECREF(f);
2175 f = PyFrozenSet_New(NULL);
2176 assert(f != NULL);
2177 assert(PyFrozenSet_CheckExact(f));
2178 assert(PySet_GET_SIZE(f) == 0);
2179 Py_DECREF(f);
2180
2181 Py_DECREF(elem);
2182 Py_DECREF(dup);
2183 Py_RETURN_TRUE;
2184}
2185
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002186#undef assertRaises
2187
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002188#endif