blob: f10fdd795f245d5a6fc512cb5a06dee1009e2e95 [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
Martin v. Löwis72d20672006-04-11 09:04:12 +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
Armin Rigoe1709372006-04-12 17:06:05 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000224set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000225{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000226 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000227 setentry *oldtable, *newtable, *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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{
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000406 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000407 setentry small_copy[PySet_MINSIZE];
408#ifdef Py_DEBUG
Neal Norwitz0f2783c2006-06-19 05:40:44 +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
Raymond Hettinger334b5b22006-03-26 03:11:29 +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 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +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;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000504 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000505 PyObject_GC_UnTrack(so);
506 Py_TRASHCAN_SAFE_BEGIN(so)
507 if (so->weakreflist != NULL)
508 PyObject_ClearWeakRefs((PyObject *) so);
509
510 for (entry = so->table; fill > 0; entry++) {
511 if (entry->key) {
512 --fill;
513 Py_DECREF(entry->key);
514 }
515 }
516 if (so->table != so->smalltable)
517 PyMem_DEL(so->table);
518 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
519 free_sets[num_free_sets++] = so;
520 else
521 so->ob_type->tp_free(so);
522 Py_TRASHCAN_SAFE_END(so)
523}
524
525static int
526set_tp_print(PySetObject *so, FILE *fp, int flags)
527{
528 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000529 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000530 char *emit = ""; /* No separator emitted on first pass */
531 char *separator = ", ";
532
533 fprintf(fp, "%s([", so->ob_type->tp_name);
534 while (set_next(so, &pos, &entry)) {
535 fputs(emit, fp);
536 emit = separator;
537 if (PyObject_Print(entry->key, fp, 0) != 0)
538 return -1;
539 }
540 fputs("])", fp);
541 return 0;
542}
543
544static PyObject *
545set_repr(PySetObject *so)
546{
547 PyObject *keys, *result, *listrepr;
548
549 keys = PySequence_List((PyObject *)so);
550 if (keys == NULL)
551 return NULL;
552 listrepr = PyObject_Repr(keys);
553 Py_DECREF(keys);
554 if (listrepr == NULL)
555 return NULL;
556
557 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
558 PyString_AS_STRING(listrepr));
559 Py_DECREF(listrepr);
560 return result;
561}
562
Martin v. Löwis18e16552006-02-15 17:27:45 +0000563static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000564set_len(PyObject *so)
565{
566 return ((PySetObject *)so)->used;
567}
568
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000569static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000570set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000571{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000572 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000573 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000574 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000575
576 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000577 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000578
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000579 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000580 if (other == so || other->used == 0)
581 /* a.update(a) or a.update({}); nothing to do */
582 return 0;
583 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000584 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000585 * that there will be no (or few) overlapping keys.
586 */
587 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
588 if (set_table_resize(so, (so->used + other->used)*2) != 0)
589 return -1;
590 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591 for (i = 0; i <= other->mask; i++) {
592 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593 if (entry->key != NULL &&
594 entry->key != dummy) {
595 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000596 if (set_insert_key(so, entry->key, entry->hash) == -1) {
597 Py_DECREF(entry->key);
598 return -1;
599 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000600 }
601 }
602 return 0;
603}
604
605static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000606set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000607{
608 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000609 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000610
611 if (!PyString_CheckExact(key) ||
612 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
613 hash = PyObject_Hash(key);
614 if (hash == -1)
615 return -1;
616 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000617 entry = (so->lookup)(so, key, hash);
618 if (entry == NULL)
619 return -1;
620 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621 return key != NULL && key != dummy;
622}
623
Raymond Hettingerc991db22005-08-11 07:58:45 +0000624static int
625set_contains_entry(PySetObject *so, setentry *entry)
626{
627 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000628 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000629
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000630 lu_entry = (so->lookup)(so, entry->key, entry->hash);
631 if (lu_entry == NULL)
632 return -1;
633 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000634 return key != NULL && key != dummy;
635}
636
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000637static PyObject *
638set_pop(PySetObject *so)
639{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000640 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000641 register setentry *entry;
642 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000643
644 assert (PyAnySet_Check(so));
645 if (so->used == 0) {
646 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
647 return NULL;
648 }
649
650 /* Set entry to "the first" unused or dummy set entry. We abuse
651 * the hash field of slot 0 to hold a search finger:
652 * If slot 0 has a value, use slot 0.
653 * Else slot 0 is being used to hold a search finger,
654 * and we use its hash value as the first index to look.
655 */
656 entry = &so->table[0];
657 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000658 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000659 /* The hash field may be a real hash value, or it may be a
660 * legit search finger, or it may be a once-legit search
661 * finger that's out of bounds now because it wrapped around
662 * or the table shrunk -- simply make sure it's in bounds now.
663 */
664 if (i > so->mask || i < 1)
665 i = 1; /* skip slot 0 */
666 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
667 i++;
668 if (i > so->mask)
669 i = 1;
670 }
671 }
672 key = entry->key;
673 Py_INCREF(dummy);
674 entry->key = dummy;
675 so->used--;
676 so->table[0].hash = i + 1; /* next place to start */
677 return key;
678}
679
680PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
681
682static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000683set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000684{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000685 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000686 setentry *entry;
687
688 while (set_next(so, &pos, &entry))
689 Py_VISIT(entry->key);
690 return 0;
691}
692
693static long
694frozenset_hash(PyObject *self)
695{
696 PySetObject *so = (PySetObject *)self;
697 long h, hash = 1927868237L;
698 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000699 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000700
701 if (so->hash != -1)
702 return so->hash;
703
704 hash *= PySet_GET_SIZE(self) + 1;
705 while (set_next(so, &pos, &entry)) {
706 /* Work to increase the bit dispersion for closely spaced hash
707 values. The is important because some use cases have many
708 combinations of a small number of elements with nearby
709 hashes so that many distinct combinations collapse to only
710 a handful of distinct hash values. */
711 h = entry->hash;
712 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
713 }
714 hash = hash * 69069L + 907133923L;
715 if (hash == -1)
716 hash = 590923713L;
717 so->hash = hash;
718 return hash;
719}
720
721static long
722set_nohash(PyObject *self)
723{
724 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
725 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000726}
727
Raymond Hettingera9d99362005-08-05 00:01:15 +0000728/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000729
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000730typedef struct {
731 PyObject_HEAD
732 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000733 Py_ssize_t si_used;
734 Py_ssize_t si_pos;
735 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000736} setiterobject;
737
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000738static void
739setiter_dealloc(setiterobject *si)
740{
741 Py_XDECREF(si->si_set);
742 PyObject_Del(si);
743}
744
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000745static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000746setiter_len(setiterobject *si)
747{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000748 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000749 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000750 len = si->len;
751 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000752}
753
Armin Rigof5b3e362006-02-11 21:32:43 +0000754PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000755
756static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000757 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000758 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000759};
760
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000761static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000762{
763 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000764 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000765 register setentry *entry;
766 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000767
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000768 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000769 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000770 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000771
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000772 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000773 PyErr_SetString(PyExc_RuntimeError,
774 "Set changed size during iteration");
775 si->si_used = -1; /* Make this state sticky */
776 return NULL;
777 }
778
779 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000780 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000781 entry = so->table;
782 mask = so->mask;
783 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000784 i++;
785 si->si_pos = i+1;
786 if (i > mask)
787 goto fail;
788 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000789 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000790 Py_INCREF(key);
791 return key;
792
793fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000794 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000795 si->si_set = NULL;
796 return NULL;
797}
798
Hye-Shik Change2956762005-08-01 05:26:41 +0000799static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800 PyObject_HEAD_INIT(&PyType_Type)
801 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000802 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000803 sizeof(setiterobject), /* tp_basicsize */
804 0, /* tp_itemsize */
805 /* methods */
806 (destructor)setiter_dealloc, /* tp_dealloc */
807 0, /* tp_print */
808 0, /* tp_getattr */
809 0, /* tp_setattr */
810 0, /* tp_compare */
811 0, /* tp_repr */
812 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000813 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000814 0, /* tp_as_mapping */
815 0, /* tp_hash */
816 0, /* tp_call */
817 0, /* tp_str */
818 PyObject_GenericGetAttr, /* tp_getattro */
819 0, /* tp_setattro */
820 0, /* tp_as_buffer */
821 Py_TPFLAGS_DEFAULT, /* tp_flags */
822 0, /* tp_doc */
823 0, /* tp_traverse */
824 0, /* tp_clear */
825 0, /* tp_richcompare */
826 0, /* tp_weaklistoffset */
827 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000828 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000829 setiter_methods, /* tp_methods */
830 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831};
832
Martin v. Löwis72d20672006-04-11 09:04:12 +0000833static PyObject *
834set_iter(PySetObject *so)
835{
836 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
837 if (si == NULL)
838 return NULL;
839 Py_INCREF(so);
840 si->si_set = so;
841 si->si_used = so->used;
842 si->si_pos = 0;
843 si->len = so->used;
844 return (PyObject *)si;
845}
846
Raymond Hettingerd7946662005-08-01 21:39:29 +0000847static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000848set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000849{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000850 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000851
Raymond Hettingerd7946662005-08-01 21:39:29 +0000852 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000853 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000854
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000855 if (PyDict_Check(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000856 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000857 Py_ssize_t pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000858 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000859 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000860 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000862 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000863 }
864
Raymond Hettingera38123e2003-11-24 22:18:49 +0000865 it = PyObject_GetIter(other);
866 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000867 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000868
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000869 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000870 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000871 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000872 Py_DECREF(key);
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 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000876 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000877 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000878 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000879 return -1;
880 return 0;
881}
882
883static PyObject *
884set_update(PySetObject *so, PyObject *other)
885{
886 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000887 return NULL;
888 Py_RETURN_NONE;
889}
890
891PyDoc_STRVAR(update_doc,
892"Update a set with the union of itself and another.");
893
894static PyObject *
895make_new_set(PyTypeObject *type, PyObject *iterable)
896{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000897 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000898
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000899 if (dummy == NULL) { /* Auto-initialize dummy */
900 dummy = PyString_FromString("<dummy key>");
901 if (dummy == NULL)
902 return NULL;
903 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000904
905 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000906 if (num_free_sets &&
907 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
908 so = free_sets[--num_free_sets];
909 assert (so != NULL && PyAnySet_CheckExact(so));
910 so->ob_type = type;
911 _Py_NewReference((PyObject *)so);
912 EMPTY_TO_MINSIZE(so);
913 PyObject_GC_Track(so);
914 } else {
915 so = (PySetObject *)type->tp_alloc(type, 0);
916 if (so == NULL)
917 return NULL;
918 /* tp_alloc has already zeroed the structure */
919 assert(so->table == NULL && so->fill == 0 && so->used == 0);
920 INIT_NONZERO_SET_SLOTS(so);
921 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000922
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000923 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000924 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000925
Raymond Hettingera38123e2003-11-24 22:18:49 +0000926 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000927 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000928 Py_DECREF(so);
929 return NULL;
930 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000931 }
932
Raymond Hettingera690a992003-11-16 16:17:49 +0000933 return (PyObject *)so;
934}
935
Raymond Hettingerd7946662005-08-01 21:39:29 +0000936/* The empty frozenset is a singleton */
937static PyObject *emptyfrozenset = NULL;
938
Raymond Hettingera690a992003-11-16 16:17:49 +0000939static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000940frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000941{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000943
Georg Brandl02c42872005-08-26 06:42:30 +0000944 if (!_PyArg_NoKeywords("frozenset()", kwds))
945 return NULL;
946
Raymond Hettingera690a992003-11-16 16:17:49 +0000947 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
948 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949
950 if (type != &PyFrozenSet_Type)
951 return make_new_set(type, iterable);
952
953 if (iterable != NULL) {
954 /* frozenset(f) is idempotent */
955 if (PyFrozenSet_CheckExact(iterable)) {
956 Py_INCREF(iterable);
957 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000958 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000959 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000960 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000961 return result;
962 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000963 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000964 /* The empty frozenset is a singleton */
965 if (emptyfrozenset == NULL)
966 emptyfrozenset = make_new_set(type, NULL);
967 Py_XINCREF(emptyfrozenset);
968 return emptyfrozenset;
969}
970
971void
972PySet_Fini(void)
973{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000974 PySetObject *so;
975
976 while (num_free_sets) {
977 num_free_sets--;
978 so = free_sets[num_free_sets];
979 PyObject_GC_Del(so);
980 }
Martin v. Löwised8f7832006-04-15 12:47:23 +0000981 Py_CLEAR(dummy);
982 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000983}
984
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000985static PyObject *
986set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
987{
Georg Brandl02c42872005-08-26 06:42:30 +0000988 if (!_PyArg_NoKeywords("set()", kwds))
989 return NULL;
990
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000991 return make_new_set(type, NULL);
992}
993
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000994/* set_swap_bodies() switches the contents of any two sets by moving their
995 internal data pointers and, if needed, copying the internal smalltables.
996 Semantically equivalent to:
997
998 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
999
1000 The function always succeeds and it leaves both objects in a stable state.
1001 Useful for creating temporary frozensets from sets for membership testing
1002 in __contains__(), discard(), and remove(). Also useful for operations
1003 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001004 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001005*/
1006
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001007static void
1008set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001009{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001010 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001011 setentry *u;
1012 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1013 setentry tab[PySet_MINSIZE];
1014 long h;
1015
1016 t = a->fill; a->fill = b->fill; b->fill = t;
1017 t = a->used; a->used = b->used; b->used = t;
1018 t = a->mask; a->mask = b->mask; b->mask = t;
1019
1020 u = a->table;
1021 if (a->table == a->smalltable)
1022 u = b->smalltable;
1023 a->table = b->table;
1024 if (b->table == b->smalltable)
1025 a->table = a->smalltable;
1026 b->table = u;
1027
1028 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1029
1030 if (a->table == a->smalltable || b->table == b->smalltable) {
1031 memcpy(tab, a->smalltable, sizeof(tab));
1032 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1033 memcpy(b->smalltable, tab, sizeof(tab));
1034 }
1035
Raymond Hettingera580c472005-08-05 17:19:54 +00001036 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1037 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1038 h = a->hash; a->hash = b->hash; b->hash = h;
1039 } else {
1040 a->hash = -1;
1041 b->hash = -1;
1042 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001043}
1044
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001045static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001046set_copy(PySetObject *so)
1047{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001048 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001049}
1050
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001051static PyObject *
1052frozenset_copy(PySetObject *so)
1053{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001054 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001055 Py_INCREF(so);
1056 return (PyObject *)so;
1057 }
1058 return set_copy(so);
1059}
1060
Raymond Hettingera690a992003-11-16 16:17:49 +00001061PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1062
1063static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001064set_clear(PySetObject *so)
1065{
1066 set_clear_internal(so);
1067 Py_RETURN_NONE;
1068}
1069
1070PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1071
1072static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001073set_union(PySetObject *so, PyObject *other)
1074{
1075 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001076
1077 result = (PySetObject *)set_copy(so);
1078 if (result == NULL)
1079 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001080 if ((PyObject *)so == other)
1081 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001082 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001083 Py_DECREF(result);
1084 return NULL;
1085 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001086 return (PyObject *)result;
1087}
1088
1089PyDoc_STRVAR(union_doc,
1090 "Return the union of two sets as a new set.\n\
1091\n\
1092(i.e. all elements that are in either set.)");
1093
1094static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001095set_or(PySetObject *so, PyObject *other)
1096{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001097 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001098 Py_INCREF(Py_NotImplemented);
1099 return Py_NotImplemented;
1100 }
1101 return set_union(so, other);
1102}
1103
1104static PyObject *
1105set_ior(PySetObject *so, PyObject *other)
1106{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001107 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001108 Py_INCREF(Py_NotImplemented);
1109 return Py_NotImplemented;
1110 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001111 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001112 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001113 Py_INCREF(so);
1114 return (PyObject *)so;
1115}
1116
1117static PyObject *
1118set_intersection(PySetObject *so, PyObject *other)
1119{
1120 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001121 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001122
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001123 if ((PyObject *)so == other)
1124 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001125
Raymond Hettingera690a992003-11-16 16:17:49 +00001126 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1127 if (result == NULL)
1128 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001129
Raymond Hettingerc991db22005-08-11 07:58:45 +00001130 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001131 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001132 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001133
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001134 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001135 tmp = (PyObject *)so;
1136 so = (PySetObject *)other;
1137 other = tmp;
1138 }
1139
Raymond Hettingerc991db22005-08-11 07:58:45 +00001140 while (set_next((PySetObject *)other, &pos, &entry)) {
1141 if (set_contains_entry(so, entry)) {
1142 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001143 Py_DECREF(result);
1144 return NULL;
1145 }
1146 }
1147 }
1148 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001149 }
1150
Raymond Hettingera690a992003-11-16 16:17:49 +00001151 it = PyObject_GetIter(other);
1152 if (it == NULL) {
1153 Py_DECREF(result);
1154 return NULL;
1155 }
1156
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001157 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001158 if (set_contains_key(so, key)) {
1159 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001160 Py_DECREF(it);
1161 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001162 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001163 return NULL;
1164 }
1165 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001166 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001167 }
1168 Py_DECREF(it);
1169 if (PyErr_Occurred()) {
1170 Py_DECREF(result);
1171 return NULL;
1172 }
1173 return (PyObject *)result;
1174}
1175
1176PyDoc_STRVAR(intersection_doc,
1177"Return the intersection of two sets as a new set.\n\
1178\n\
1179(i.e. all elements that are in both sets.)");
1180
1181static PyObject *
1182set_intersection_update(PySetObject *so, PyObject *other)
1183{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001184 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001185
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001186 tmp = set_intersection(so, other);
1187 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001188 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001189 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001190 Py_DECREF(tmp);
1191 Py_RETURN_NONE;
1192}
1193
1194PyDoc_STRVAR(intersection_update_doc,
1195"Update a set with the intersection of itself and another.");
1196
1197static PyObject *
1198set_and(PySetObject *so, PyObject *other)
1199{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001200 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001201 Py_INCREF(Py_NotImplemented);
1202 return Py_NotImplemented;
1203 }
1204 return set_intersection(so, other);
1205}
1206
1207static PyObject *
1208set_iand(PySetObject *so, PyObject *other)
1209{
1210 PyObject *result;
1211
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001212 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001213 Py_INCREF(Py_NotImplemented);
1214 return Py_NotImplemented;
1215 }
1216 result = set_intersection_update(so, other);
1217 if (result == NULL)
1218 return NULL;
1219 Py_DECREF(result);
1220 Py_INCREF(so);
1221 return (PyObject *)so;
1222}
1223
Neal Norwitz6576bd82005-11-13 18:41:28 +00001224static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001225set_difference_update_internal(PySetObject *so, PyObject *other)
1226{
1227 if ((PyObject *)so == other)
1228 return set_clear_internal(so);
1229
1230 if (PyAnySet_Check(other)) {
1231 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001232 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001233
1234 while (set_next((PySetObject *)other, &pos, &entry))
1235 set_discard_entry(so, entry);
1236 } else {
1237 PyObject *key, *it;
1238 it = PyObject_GetIter(other);
1239 if (it == NULL)
1240 return -1;
1241
1242 while ((key = PyIter_Next(it)) != NULL) {
1243 if (set_discard_key(so, key) == -1) {
1244 Py_DECREF(it);
1245 Py_DECREF(key);
1246 return -1;
1247 }
1248 Py_DECREF(key);
1249 }
1250 Py_DECREF(it);
1251 if (PyErr_Occurred())
1252 return -1;
1253 }
1254 /* If more than 1/5 are dummies, then resize them away. */
1255 if ((so->fill - so->used) * 5 < so->mask)
1256 return 0;
1257 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1258}
1259
Raymond Hettingera690a992003-11-16 16:17:49 +00001260static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001261set_difference_update(PySetObject *so, PyObject *other)
1262{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001263 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001264 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001265 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001266}
1267
1268PyDoc_STRVAR(difference_update_doc,
1269"Remove all elements of another set from this set.");
1270
1271static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001272set_difference(PySetObject *so, PyObject *other)
1273{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001274 PyObject *result;
1275 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001276 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001277
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001278 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001279 result = set_copy(so);
1280 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001281 return NULL;
1282 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001283 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001284 Py_DECREF(result);
1285 return NULL;
1286 }
1287
1288 result = make_new_set(so->ob_type, NULL);
1289 if (result == NULL)
1290 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001291
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001292 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001293 while (set_next(so, &pos, &entry)) {
1294 setentry entrycopy;
1295 entrycopy.hash = entry->hash;
1296 entrycopy.key = entry->key;
1297 if (!PyDict_Contains(other, entry->key)) {
1298 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001299 return NULL;
1300 }
1301 }
1302 return result;
1303 }
1304
Raymond Hettingerc991db22005-08-11 07:58:45 +00001305 while (set_next(so, &pos, &entry)) {
1306 if (!set_contains_entry((PySetObject *)other, entry)) {
1307 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001308 return NULL;
1309 }
1310 }
1311 return result;
1312}
1313
1314PyDoc_STRVAR(difference_doc,
1315"Return the difference of two sets as a new set.\n\
1316\n\
1317(i.e. all elements that are in this set but not the other.)");
1318static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001319set_sub(PySetObject *so, PyObject *other)
1320{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001321 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001322 Py_INCREF(Py_NotImplemented);
1323 return Py_NotImplemented;
1324 }
1325 return set_difference(so, other);
1326}
1327
1328static PyObject *
1329set_isub(PySetObject *so, PyObject *other)
1330{
1331 PyObject *result;
1332
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001333 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001334 Py_INCREF(Py_NotImplemented);
1335 return Py_NotImplemented;
1336 }
1337 result = set_difference_update(so, other);
1338 if (result == NULL)
1339 return NULL;
1340 Py_DECREF(result);
1341 Py_INCREF(so);
1342 return (PyObject *)so;
1343}
1344
1345static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001346set_symmetric_difference_update(PySetObject *so, PyObject *other)
1347{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001348 PySetObject *otherset;
1349 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001350 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001351 setentry *entry;
1352
1353 if ((PyObject *)so == other)
1354 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001355
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001356 if (PyDict_Check(other)) {
1357 PyObject *value;
1358 int rv;
1359 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001360 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001361 if (rv == -1)
1362 return NULL;
1363 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001364 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001365 return NULL;
1366 }
1367 }
1368 Py_RETURN_NONE;
1369 }
1370
1371 if (PyAnySet_Check(other)) {
1372 Py_INCREF(other);
1373 otherset = (PySetObject *)other;
1374 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001375 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1376 if (otherset == NULL)
1377 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001378 }
1379
Raymond Hettingerc991db22005-08-11 07:58:45 +00001380 while (set_next(otherset, &pos, &entry)) {
1381 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001382 if (rv == -1) {
1383 Py_XDECREF(otherset);
1384 return NULL;
1385 }
1386 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001387 if (set_add_entry(so, entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001388 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001389 return NULL;
1390 }
1391 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001392 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001393 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001394 Py_RETURN_NONE;
1395}
1396
1397PyDoc_STRVAR(symmetric_difference_update_doc,
1398"Update a set with the symmetric difference of itself and another.");
1399
1400static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001401set_symmetric_difference(PySetObject *so, PyObject *other)
1402{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001403 PyObject *rv;
1404 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001405
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001406 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1407 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001408 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001409 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1410 if (rv == NULL)
1411 return NULL;
1412 Py_DECREF(rv);
1413 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001414}
1415
1416PyDoc_STRVAR(symmetric_difference_doc,
1417"Return the symmetric difference of two sets as a new set.\n\
1418\n\
1419(i.e. all elements that are in exactly one of the sets.)");
1420
1421static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001422set_xor(PySetObject *so, PyObject *other)
1423{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001424 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001425 Py_INCREF(Py_NotImplemented);
1426 return Py_NotImplemented;
1427 }
1428 return set_symmetric_difference(so, other);
1429}
1430
1431static PyObject *
1432set_ixor(PySetObject *so, PyObject *other)
1433{
1434 PyObject *result;
1435
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001436 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001437 Py_INCREF(Py_NotImplemented);
1438 return Py_NotImplemented;
1439 }
1440 result = set_symmetric_difference_update(so, other);
1441 if (result == NULL)
1442 return NULL;
1443 Py_DECREF(result);
1444 Py_INCREF(so);
1445 return (PyObject *)so;
1446}
1447
1448static PyObject *
1449set_issubset(PySetObject *so, PyObject *other)
1450{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001451 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001452 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001453
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001454 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001455 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001456 tmp = make_new_set(&PySet_Type, other);
1457 if (tmp == NULL)
1458 return NULL;
1459 result = set_issubset(so, tmp);
1460 Py_DECREF(tmp);
1461 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001462 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001463 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001464 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001465
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001466 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001467 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001468 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001469 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001470 Py_RETURN_TRUE;
1471}
1472
1473PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1474
1475static PyObject *
1476set_issuperset(PySetObject *so, PyObject *other)
1477{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001478 PyObject *tmp, *result;
1479
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001480 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001481 tmp = make_new_set(&PySet_Type, other);
1482 if (tmp == NULL)
1483 return NULL;
1484 result = set_issuperset(so, tmp);
1485 Py_DECREF(tmp);
1486 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001487 }
1488 return set_issubset((PySetObject *)other, (PyObject *)so);
1489}
1490
1491PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1492
Raymond Hettingera690a992003-11-16 16:17:49 +00001493static PyObject *
1494set_richcompare(PySetObject *v, PyObject *w, int op)
1495{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001496 PyObject *r1, *r2;
1497
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001498 if(!PyAnySet_Check(w)) {
1499 if (op == Py_EQ)
1500 Py_RETURN_FALSE;
1501 if (op == Py_NE)
1502 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001503 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1504 return NULL;
1505 }
1506 switch (op) {
1507 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001508 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001509 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001510 if (v->hash != -1 &&
1511 ((PySetObject *)w)->hash != -1 &&
1512 v->hash != ((PySetObject *)w)->hash)
1513 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001514 return set_issubset(v, w);
1515 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001516 r1 = set_richcompare(v, w, Py_EQ);
1517 if (r1 == NULL)
1518 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001519 r2 = PyBool_FromLong(PyObject_Not(r1));
1520 Py_DECREF(r1);
1521 return r2;
1522 case Py_LE:
1523 return set_issubset(v, w);
1524 case Py_GE:
1525 return set_issuperset(v, w);
1526 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001527 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001528 Py_RETURN_FALSE;
1529 return set_issubset(v, w);
1530 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001531 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001532 Py_RETURN_FALSE;
1533 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001534 }
1535 Py_INCREF(Py_NotImplemented);
1536 return Py_NotImplemented;
1537}
1538
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001539static int
Georg Brandl347b3002006-03-30 11:57:00 +00001540set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001541{
1542 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1543 return -1;
1544}
1545
Raymond Hettingera690a992003-11-16 16:17:49 +00001546static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001547set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001548{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001549 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001550 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001551 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001552}
1553
1554PyDoc_STRVAR(add_doc,
1555"Add an element to a set.\n\
1556\n\
1557This has no effect if the element is already present.");
1558
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001559static int
1560set_contains(PySetObject *so, PyObject *key)
1561{
1562 PyObject *tmpkey;
1563 int rv;
1564
1565 rv = set_contains_key(so, key);
1566 if (rv == -1) {
1567 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1568 return -1;
1569 PyErr_Clear();
1570 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1571 if (tmpkey == NULL)
1572 return -1;
1573 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1574 rv = set_contains(so, tmpkey);
1575 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1576 Py_DECREF(tmpkey);
1577 }
1578 return rv;
1579}
1580
1581static PyObject *
1582set_direct_contains(PySetObject *so, PyObject *key)
1583{
1584 long result;
1585
1586 result = set_contains(so, key);
1587 if (result == -1)
1588 return NULL;
1589 return PyBool_FromLong(result);
1590}
1591
1592PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1593
Raymond Hettingera690a992003-11-16 16:17:49 +00001594static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001595set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001596{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001597 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001598 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001599
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001600 rv = set_discard_key(so, key);
1601 if (rv == -1) {
1602 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1603 return NULL;
1604 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001605 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1606 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001607 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001608 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001609 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001610 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001611 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001612 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001613 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001614 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001615 return NULL;
1616 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001617 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001618}
1619
1620PyDoc_STRVAR(remove_doc,
1621"Remove an element from a set; it must be a member.\n\
1622\n\
1623If the element is not a member, raise a KeyError.");
1624
1625static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001626set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001627{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001628 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001629 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001630
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001631 rv = set_discard_key(so, key);
1632 if (rv == -1) {
1633 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1634 return NULL;
1635 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001636 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1637 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001638 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001639 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001640 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001641 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001642 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001643 return result;
1644 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001645 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001646}
1647
1648PyDoc_STRVAR(discard_doc,
1649"Remove an element from a set if it is a member.\n\
1650\n\
1651If the element is not a member, do nothing.");
1652
1653static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001654set_reduce(PySetObject *so)
1655{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001656 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001657
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001658 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001659 if (keys == NULL)
1660 goto done;
1661 args = PyTuple_Pack(1, keys);
1662 if (args == NULL)
1663 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001664 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1665 if (dict == NULL) {
1666 PyErr_Clear();
1667 dict = Py_None;
1668 Py_INCREF(dict);
1669 }
1670 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001671done:
1672 Py_XDECREF(args);
1673 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001674 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001675 return result;
1676}
1677
1678PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1679
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001680static int
1681set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1682{
1683 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001684
1685 if (!PyAnySet_Check(self))
1686 return -1;
1687 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1688 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001689 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001690 self->hash = -1;
1691 if (iterable == NULL)
1692 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001693 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001694}
1695
Raymond Hettingera690a992003-11-16 16:17:49 +00001696static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001697 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001698 0, /* sq_concat */
1699 0, /* sq_repeat */
1700 0, /* sq_item */
1701 0, /* sq_slice */
1702 0, /* sq_ass_item */
1703 0, /* sq_ass_slice */
1704 (objobjproc)set_contains, /* sq_contains */
1705};
1706
1707/* set object ********************************************************/
1708
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001709#ifdef Py_DEBUG
1710static PyObject *test_c_api(PySetObject *so);
1711
1712PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1713All is well if assertions don't fail.");
1714#endif
1715
Raymond Hettingera690a992003-11-16 16:17:49 +00001716static PyMethodDef set_methods[] = {
1717 {"add", (PyCFunction)set_add, METH_O,
1718 add_doc},
1719 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1720 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001721 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001722 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001723 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1724 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001725 {"discard", (PyCFunction)set_discard, METH_O,
1726 discard_doc},
1727 {"difference", (PyCFunction)set_difference, METH_O,
1728 difference_doc},
1729 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1730 difference_update_doc},
1731 {"intersection",(PyCFunction)set_intersection, METH_O,
1732 intersection_doc},
1733 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1734 intersection_update_doc},
1735 {"issubset", (PyCFunction)set_issubset, METH_O,
1736 issubset_doc},
1737 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1738 issuperset_doc},
1739 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1740 pop_doc},
1741 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1742 reduce_doc},
1743 {"remove", (PyCFunction)set_remove, METH_O,
1744 remove_doc},
1745 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1746 symmetric_difference_doc},
1747 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1748 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001749#ifdef Py_DEBUG
1750 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1751 test_c_api_doc},
1752#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001753 {"union", (PyCFunction)set_union, METH_O,
1754 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001755 {"update", (PyCFunction)set_update, METH_O,
1756 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001757 {NULL, NULL} /* sentinel */
1758};
1759
1760static PyNumberMethods set_as_number = {
1761 0, /*nb_add*/
1762 (binaryfunc)set_sub, /*nb_subtract*/
1763 0, /*nb_multiply*/
1764 0, /*nb_divide*/
1765 0, /*nb_remainder*/
1766 0, /*nb_divmod*/
1767 0, /*nb_power*/
1768 0, /*nb_negative*/
1769 0, /*nb_positive*/
1770 0, /*nb_absolute*/
1771 0, /*nb_nonzero*/
1772 0, /*nb_invert*/
1773 0, /*nb_lshift*/
1774 0, /*nb_rshift*/
1775 (binaryfunc)set_and, /*nb_and*/
1776 (binaryfunc)set_xor, /*nb_xor*/
1777 (binaryfunc)set_or, /*nb_or*/
1778 0, /*nb_coerce*/
1779 0, /*nb_int*/
1780 0, /*nb_long*/
1781 0, /*nb_float*/
1782 0, /*nb_oct*/
1783 0, /*nb_hex*/
1784 0, /*nb_inplace_add*/
1785 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1786 0, /*nb_inplace_multiply*/
1787 0, /*nb_inplace_divide*/
1788 0, /*nb_inplace_remainder*/
1789 0, /*nb_inplace_power*/
1790 0, /*nb_inplace_lshift*/
1791 0, /*nb_inplace_rshift*/
1792 (binaryfunc)set_iand, /*nb_inplace_and*/
1793 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1794 (binaryfunc)set_ior, /*nb_inplace_or*/
1795};
1796
1797PyDoc_STRVAR(set_doc,
1798"set(iterable) --> set object\n\
1799\n\
1800Build an unordered collection.");
1801
1802PyTypeObject PySet_Type = {
1803 PyObject_HEAD_INIT(&PyType_Type)
1804 0, /* ob_size */
1805 "set", /* tp_name */
1806 sizeof(PySetObject), /* tp_basicsize */
1807 0, /* tp_itemsize */
1808 /* methods */
1809 (destructor)set_dealloc, /* tp_dealloc */
1810 (printfunc)set_tp_print, /* tp_print */
1811 0, /* tp_getattr */
1812 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001813 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001814 (reprfunc)set_repr, /* tp_repr */
1815 &set_as_number, /* tp_as_number */
1816 &set_as_sequence, /* tp_as_sequence */
1817 0, /* tp_as_mapping */
1818 set_nohash, /* tp_hash */
1819 0, /* tp_call */
1820 0, /* tp_str */
1821 PyObject_GenericGetAttr, /* tp_getattro */
1822 0, /* tp_setattro */
1823 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001824 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001825 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001826 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001827 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001828 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001829 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001830 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001831 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001832 0, /* tp_iternext */
1833 set_methods, /* tp_methods */
1834 0, /* tp_members */
1835 0, /* tp_getset */
1836 0, /* tp_base */
1837 0, /* tp_dict */
1838 0, /* tp_descr_get */
1839 0, /* tp_descr_set */
1840 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001841 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001842 PyType_GenericAlloc, /* tp_alloc */
1843 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001844 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001845};
1846
1847/* frozenset object ********************************************************/
1848
1849
1850static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001851 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001852 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001853 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001854 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001855 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001856 difference_doc},
1857 {"intersection",(PyCFunction)set_intersection, METH_O,
1858 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001859 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001860 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001861 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001862 issuperset_doc},
1863 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1864 reduce_doc},
1865 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1866 symmetric_difference_doc},
1867 {"union", (PyCFunction)set_union, METH_O,
1868 union_doc},
1869 {NULL, NULL} /* sentinel */
1870};
1871
1872static PyNumberMethods frozenset_as_number = {
1873 0, /*nb_add*/
1874 (binaryfunc)set_sub, /*nb_subtract*/
1875 0, /*nb_multiply*/
1876 0, /*nb_divide*/
1877 0, /*nb_remainder*/
1878 0, /*nb_divmod*/
1879 0, /*nb_power*/
1880 0, /*nb_negative*/
1881 0, /*nb_positive*/
1882 0, /*nb_absolute*/
1883 0, /*nb_nonzero*/
1884 0, /*nb_invert*/
1885 0, /*nb_lshift*/
1886 0, /*nb_rshift*/
1887 (binaryfunc)set_and, /*nb_and*/
1888 (binaryfunc)set_xor, /*nb_xor*/
1889 (binaryfunc)set_or, /*nb_or*/
1890};
1891
1892PyDoc_STRVAR(frozenset_doc,
1893"frozenset(iterable) --> frozenset object\n\
1894\n\
1895Build an immutable unordered collection.");
1896
1897PyTypeObject PyFrozenSet_Type = {
1898 PyObject_HEAD_INIT(&PyType_Type)
1899 0, /* ob_size */
1900 "frozenset", /* tp_name */
1901 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001902 0, /* tp_itemsize */
1903 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001904 (destructor)set_dealloc, /* tp_dealloc */
1905 (printfunc)set_tp_print, /* tp_print */
1906 0, /* tp_getattr */
1907 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001908 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001909 (reprfunc)set_repr, /* tp_repr */
1910 &frozenset_as_number, /* tp_as_number */
1911 &set_as_sequence, /* tp_as_sequence */
1912 0, /* tp_as_mapping */
1913 frozenset_hash, /* tp_hash */
1914 0, /* tp_call */
1915 0, /* tp_str */
1916 PyObject_GenericGetAttr, /* tp_getattro */
1917 0, /* tp_setattro */
1918 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001919 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001920 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001921 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001922 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001923 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001924 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001925 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001926 (getiterfunc)set_iter, /* tp_iter */
1927 0, /* tp_iternext */
1928 frozenset_methods, /* tp_methods */
1929 0, /* tp_members */
1930 0, /* tp_getset */
1931 0, /* tp_base */
1932 0, /* tp_dict */
1933 0, /* tp_descr_get */
1934 0, /* tp_descr_set */
1935 0, /* tp_dictoffset */
1936 0, /* tp_init */
1937 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001938 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001939 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001940};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001941
1942
1943/***** C API functions *************************************************/
1944
1945PyObject *
1946PySet_New(PyObject *iterable)
1947{
1948 return make_new_set(&PySet_Type, iterable);
1949}
1950
1951PyObject *
1952PyFrozenSet_New(PyObject *iterable)
1953{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001954 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001955
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001956 if (iterable == NULL)
1957 args = PyTuple_New(0);
1958 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001959 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001960 if (args == NULL)
1961 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001962 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001963 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001964 return result;
1965}
1966
Neal Norwitz8c49c822006-03-04 18:41:19 +00001967Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001968PySet_Size(PyObject *anyset)
1969{
1970 if (!PyAnySet_Check(anyset)) {
1971 PyErr_BadInternalCall();
1972 return -1;
1973 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001974 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001975}
1976
1977int
Barry Warsaw176014f2006-03-30 22:45:35 +00001978PySet_Clear(PyObject *set)
1979{
1980 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1981 PyErr_BadInternalCall();
1982 return -1;
1983 }
1984 return set_clear_internal((PySetObject *)set);
1985}
1986
1987int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001988PySet_Contains(PyObject *anyset, PyObject *key)
1989{
1990 if (!PyAnySet_Check(anyset)) {
1991 PyErr_BadInternalCall();
1992 return -1;
1993 }
1994 return set_contains_key((PySetObject *)anyset, key);
1995}
1996
1997int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001998PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001999{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002000 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002001 PyErr_BadInternalCall();
2002 return -1;
2003 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002004 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002005}
2006
2007int
2008PySet_Add(PyObject *set, PyObject *key)
2009{
2010 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2011 PyErr_BadInternalCall();
2012 return -1;
2013 }
2014 return set_add_key((PySetObject *)set, key);
2015}
2016
Barry Warsaw176014f2006-03-30 22:45:35 +00002017int
2018_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2019{
2020 setentry *entry_ptr;
2021
2022 if (!PyAnySet_Check(set)) {
2023 PyErr_BadInternalCall();
2024 return -1;
2025 }
2026 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2027 return 0;
2028 *entry = entry_ptr->key;
2029 return 1;
2030}
2031
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002032PyObject *
2033PySet_Pop(PyObject *set)
2034{
2035 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2036 PyErr_BadInternalCall();
2037 return NULL;
2038 }
2039 return set_pop((PySetObject *)set);
2040}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002041
Barry Warsaw176014f2006-03-30 22:45:35 +00002042int
2043_PySet_Update(PyObject *set, PyObject *iterable)
2044{
2045 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2046 PyErr_BadInternalCall();
2047 return -1;
2048 }
2049 return set_update_internal((PySetObject *)set, iterable);
2050}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002051
2052#ifdef Py_DEBUG
2053
2054/* Test code to be called with any three element set.
2055 Returns True and original set is restored. */
2056
2057#define assertRaises(call_return_value, exception) \
2058 do { \
2059 assert(call_return_value); \
2060 assert(PyErr_ExceptionMatches(exception)); \
2061 PyErr_Clear(); \
2062 } while(0)
2063
2064static PyObject *
2065test_c_api(PySetObject *so)
2066{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002067 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002068 char *s;
2069 Py_ssize_t i;
2070 PyObject *elem, *dup, *t, *f, *dup2;
2071 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002072
2073 /* Verify preconditions and exercise type/size checks */
2074 assert(PyAnySet_Check(ob));
2075 assert(PyAnySet_CheckExact(ob));
2076 assert(!PyFrozenSet_CheckExact(ob));
2077 assert(PySet_Size(ob) == 3);
2078 assert(PySet_GET_SIZE(ob) == 3);
2079
2080 /* Raise TypeError for non-iterable constructor arguments */
2081 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2082 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2083
2084 /* Raise TypeError for unhashable key */
2085 dup = PySet_New(ob);
2086 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2087 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2088 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2089
2090 /* Exercise successful pop, contains, add, and discard */
2091 elem = PySet_Pop(ob);
2092 assert(PySet_Contains(ob, elem) == 0);
2093 assert(PySet_GET_SIZE(ob) == 2);
2094 assert(PySet_Add(ob, elem) == 0);
2095 assert(PySet_Contains(ob, elem) == 1);
2096 assert(PySet_GET_SIZE(ob) == 3);
2097 assert(PySet_Discard(ob, elem) == 1);
2098 assert(PySet_GET_SIZE(ob) == 2);
2099 assert(PySet_Discard(ob, elem) == 0);
2100 assert(PySet_GET_SIZE(ob) == 2);
2101
Barry Warsaw176014f2006-03-30 22:45:35 +00002102 /* Exercise clear */
2103 dup2 = PySet_New(dup);
2104 assert(PySet_Clear(dup2) == 0);
2105 assert(PySet_Size(dup2) == 0);
2106 Py_DECREF(dup2);
2107
2108 /* Raise SystemError on clear or update of frozen set */
2109 f = PyFrozenSet_New(dup);
2110 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2111 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2112 Py_DECREF(f);
2113
2114 /* Exercise direct iteration */
2115 i = 0, count = 0;
2116 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2117 s = PyString_AsString(elem);
2118 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2119 count++;
2120 }
2121 assert(count == 3);
2122
2123 /* Exercise updates */
2124 dup2 = PySet_New(NULL);
2125 assert(_PySet_Update(dup2, dup) == 0);
2126 assert(PySet_Size(dup2) == 3);
2127 assert(_PySet_Update(dup2, dup) == 0);
2128 assert(PySet_Size(dup2) == 3);
2129 Py_DECREF(dup2);
2130
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002131 /* Raise SystemError when self argument is not a set or frozenset. */
2132 t = PyTuple_New(0);
2133 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2134 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2135 Py_DECREF(t);
2136
2137 /* Raise SystemError when self argument is not a set. */
2138 f = PyFrozenSet_New(dup);
2139 assert(PySet_Size(f) == 3);
2140 assert(PyFrozenSet_CheckExact(f));
2141 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2142 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2143 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2144 Py_DECREF(f);
2145
2146 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002147 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2148 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002149 assert(PySet_GET_SIZE(ob) == 0);
2150 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2151
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002152 /* Restore the set from the copy using the PyNumber API */
2153 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2154 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002155
2156 /* Verify constructors accept NULL arguments */
2157 f = PySet_New(NULL);
2158 assert(f != NULL);
2159 assert(PySet_GET_SIZE(f) == 0);
2160 Py_DECREF(f);
2161 f = PyFrozenSet_New(NULL);
2162 assert(f != NULL);
2163 assert(PyFrozenSet_CheckExact(f));
2164 assert(PySet_GET_SIZE(f) == 0);
2165 Py_DECREF(f);
2166
2167 Py_DECREF(elem);
2168 Py_DECREF(dup);
2169 Py_RETURN_TRUE;
2170}
2171
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002172#undef assertRaises
2173
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002174#endif