blob: 9a54aed876837e211ba4ce2e0dc5f775d9b887a5 [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
Raymond Hettingera9d99362005-08-05 00:01:15 +00006 Copyright (c) 2003-5 Python Software Foundation.
7 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
Raymond Hettingerbc841a12005-08-07 13:02:53 +000019#define INIT_NONZERO_SET_SLOTS(so) do { \
20 (so)->table = (so)->smalltable; \
21 (so)->mask = PySet_MINSIZE - 1; \
22 (so)->hash = -1; \
23 } while(0)
24
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000025#define EMPTY_TO_MINSIZE(so) do { \
26 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
27 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000028 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000029 } while(0)
30
Raymond Hettingerbc841a12005-08-07 13:02:53 +000031/* Reuse scheme to save calls to malloc, free, and memset */
32#define MAXFREESETS 80
33static PySetObject *free_sets[MAXFREESETS];
34static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035
36/*
37The basic lookup function used by all operations.
38This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
39Open addressing is preferred over chaining since the link overhead for
40chaining would be substantial (100% with typical malloc overhead).
41
42The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000043probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000044
45All arithmetic on hash should ignore overflow.
46
Raymond Hettingerbc841a12005-08-07 13:02:53 +000047The lookup function always succeeds and nevers return NULL. This simplifies
48and speeds client functions which do won't have to test for and handle
49errors. To meet that requirement, any errors generated by a user defined
50__cmp__() function are simply cleared and ignored.
51Previously outstanding exceptions are maintained.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052*/
53
54static setentry *
55set_lookkey(PySetObject *so, PyObject *key, register long hash)
56{
57 register int i;
58 register unsigned int perturb;
59 register setentry *freeslot;
60 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000061 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000062 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000063 register int restore_error;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +000064 register int checked_error;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000065 register int cmp;
66 PyObject *err_type, *err_value, *err_tb;
67 PyObject *startkey;
68
69 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000070 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000071 if (entry->key == NULL || entry->key == key)
72 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073
Raymond Hettingered6c1ef2005-08-13 08:28:03 +000074 restore_error = checked_error = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000075 if (entry->key == dummy)
76 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000077 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000078 if (entry->hash == hash) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +000079 /* error can't have been checked yet */
80 checked_error = 1;
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +000081 if (_PyErr_OCCURRED()) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 restore_error = 1;
83 PyErr_Fetch(&err_type, &err_value, &err_tb);
84 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000085 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000086 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
87 if (cmp < 0)
88 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +000089 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000090 if (cmp > 0)
91 goto Done;
92 }
93 else {
94 /* The compare did major nasty stuff to the
95 * set: start over.
96 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000097 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000098 goto Done;
99 }
100 }
101 freeslot = NULL;
102 }
103
104 /* In the loop, key == dummy is by far (factor of 100s) the
105 least likely outcome, so test for that last. */
106 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
107 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000108 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000109 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000110 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000111 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000112 break;
113 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000114 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000115 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000116 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000117 if (!checked_error) {
118 checked_error = 1;
119 if (_PyErr_OCCURRED()) {
120 restore_error = 1;
121 PyErr_Fetch(&err_type, &err_value,
122 &err_tb);
123 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
127 if (cmp < 0)
128 PyErr_Clear();
Raymond Hettingera580c472005-08-05 17:19:54 +0000129 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130 if (cmp > 0)
131 break;
132 }
133 else {
134 /* The compare did major nasty stuff to the
135 * set: start over.
136 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000137 entry = set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138 break;
139 }
140 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000141 else if (entry->key == dummy && freeslot == NULL)
142 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000143 }
144
145Done:
146 if (restore_error)
147 PyErr_Restore(err_type, err_value, err_tb);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000148 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000149}
150
151/*
152 * Hacked up version of set_lookkey which can assume keys are always strings;
153 * this assumption allows testing for errors during PyObject_Compare() to
154 * be dropped; string-string comparisons never raise exceptions. This also
155 * means we don't need to go through PyObject_Compare(); we can always use
156 * _PyString_Eq directly.
157 *
158 * This is valuable because the general-case error handling in set_lookkey() is
159 * expensive, and sets with pure-string keys may be very common.
160 */
161static setentry *
162set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
163{
164 register int i;
165 register unsigned int perturb;
166 register setentry *freeslot;
167 register unsigned int mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000168 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000169 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000170
171 /* Make sure this function doesn't have to handle non-string keys,
172 including subclasses of str; e.g., one reason to subclass
173 strings is to override __eq__, and for speed we don't cater to
174 that here. */
175 if (!PyString_CheckExact(key)) {
176 so->lookup = set_lookkey;
177 return set_lookkey(so, key, hash);
178 }
179 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000180 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000181 if (entry->key == NULL || entry->key == key)
182 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000183 if (entry->key == dummy)
184 freeslot = entry;
185 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000186 if (entry->hash == hash && _PyString_Eq(entry->key, key))
187 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000188 freeslot = NULL;
189 }
190
191 /* In the loop, key == dummy is by far (factor of 100s) the
192 least likely outcome, so test for that last. */
193 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
194 i = (i << 2) + i + perturb + 1;
195 entry = &table[i & mask];
196 if (entry->key == NULL)
197 return freeslot == NULL ? entry : freeslot;
198 if (entry->key == key
199 || (entry->hash == hash
200 && entry->key != dummy
201 && _PyString_Eq(entry->key, key)))
202 return entry;
203 if (entry->key == dummy && freeslot == NULL)
204 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000205 }
206}
207
208/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000210Used both by the internal resize routine and by the public insert routine.
211Eats a reference to key.
212*/
213static void
214set_insert_key(register PySetObject *so, PyObject *key, long hash)
215{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000216 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
218
219 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220 entry = so->lookup(so, key, hash);
221 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 /* UNUSED */
223 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000224 entry->key = key;
225 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000226 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000227 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000228 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000229 entry->key = key;
230 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000231 so->used++;
232 Py_DECREF(dummy);
233 } else {
234 /* ACTIVE */
235 Py_DECREF(key);
236 }
237}
238
239/*
240Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000241keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000242actually be smaller than the old one.
243*/
244static int
245set_table_resize(PySetObject *so, int minused)
246{
247 int newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000248 setentry *oldtable, *newtable, *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000249 int i;
250 int is_oldtable_malloced;
251 setentry small_copy[PySet_MINSIZE];
252
253 assert(minused >= 0);
254
255 /* Find the smallest table size > minused. */
256 for (newsize = PySet_MINSIZE;
257 newsize <= minused && newsize > 0;
258 newsize <<= 1)
259 ;
260 if (newsize <= 0) {
261 PyErr_NoMemory();
262 return -1;
263 }
264
265 /* Get space for a new table. */
266 oldtable = so->table;
267 assert(oldtable != NULL);
268 is_oldtable_malloced = oldtable != so->smalltable;
269
270 if (newsize == PySet_MINSIZE) {
271 /* A large table is shrinking, or we can't get any smaller. */
272 newtable = so->smalltable;
273 if (newtable == oldtable) {
274 if (so->fill == so->used) {
275 /* No dummies, so no point doing anything. */
276 return 0;
277 }
278 /* We're not going to resize it, but rebuild the
279 table anyway to purge old dummy entries.
280 Subtle: This is *necessary* if fill==size,
281 as set_lookkey needs at least one virgin slot to
282 terminate failing searches. If fill < size, it's
283 merely desirable, as dummies slow searches. */
284 assert(so->fill > so->used);
285 memcpy(small_copy, oldtable, sizeof(small_copy));
286 oldtable = small_copy;
287 }
288 }
289 else {
290 newtable = PyMem_NEW(setentry, newsize);
291 if (newtable == NULL) {
292 PyErr_NoMemory();
293 return -1;
294 }
295 }
296
297 /* Make the set empty, using the new table. */
298 assert(newtable != oldtable);
299 so->table = newtable;
300 so->mask = newsize - 1;
301 memset(newtable, 0, sizeof(setentry) * newsize);
302 so->used = 0;
303 i = so->fill;
304 so->fill = 0;
305
306 /* Copy the data over; this is refcount-neutral for active entries;
307 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000308 for (entry = oldtable; i > 0; entry++) {
309 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000310 /* UNUSED */
311 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000312 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000313 /* DUMMY */
314 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000315 assert(entry->key == dummy);
316 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000317 } else {
318 /* ACTIVE */
319 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000320 set_insert_key(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000321 }
322 }
323
324 if (is_oldtable_malloced)
325 PyMem_DEL(oldtable);
326 return 0;
327}
328
Raymond Hettingerc991db22005-08-11 07:58:45 +0000329/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
330
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000332set_add_entry(register PySetObject *so, setentry *entry)
333{
334 register int n_used;
335
336 assert(so->fill <= so->mask); /* at least one empty slot */
337 n_used = so->used;
338 Py_INCREF(entry->key);
339 set_insert_key(so, entry->key, entry->hash);
340 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
341 return 0;
342 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
343}
344
345static int
346set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347{
348 register long hash;
349 register int n_used;
350
Raymond Hettingerc991db22005-08-11 07:58:45 +0000351 if (!PyString_CheckExact(key) ||
352 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000353 hash = PyObject_Hash(key);
354 if (hash == -1)
355 return -1;
356 }
357 assert(so->fill <= so->mask); /* at least one empty slot */
358 n_used = so->used;
359 Py_INCREF(key);
360 set_insert_key(so, key, hash);
361 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
362 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000363 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000364}
365
366#define DISCARD_NOTFOUND 0
367#define DISCARD_FOUND 1
368
369static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000370set_discard_entry(PySetObject *so, setentry *oldentry)
371{ register setentry *entry;
372 PyObject *old_key;
373
374 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
375 if (entry->key == NULL || entry->key == dummy)
376 return DISCARD_NOTFOUND;
377 old_key = entry->key;
378 Py_INCREF(dummy);
379 entry->key = dummy;
380 so->used--;
381 Py_DECREF(old_key);
382 return DISCARD_FOUND;
383}
384
385static int
386set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000387{
388 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000389 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000390 PyObject *old_key;
391
392 assert (PyAnySet_Check(so));
393 if (!PyString_CheckExact(key) ||
394 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
395 hash = PyObject_Hash(key);
396 if (hash == -1)
397 return -1;
398 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000399 entry = (so->lookup)(so, key, hash);
400 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000401 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000402 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000403 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000404 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405 so->used--;
406 Py_DECREF(old_key);
407 return DISCARD_FOUND;
408}
409
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000410static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000411set_clear_internal(PySetObject *so)
412{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000413 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000414 int table_is_malloced;
415 int fill;
416 setentry small_copy[PySet_MINSIZE];
417#ifdef Py_DEBUG
418 int i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000419 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000420
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421 n = so->mask + 1;
422 i = 0;
423#endif
424
425 table = so->table;
426 assert(table != NULL);
427 table_is_malloced = table != so->smalltable;
428
429 /* This is delicate. During the process of clearing the set,
430 * decrefs can cause the set to mutate. To avoid fatal confusion
431 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000432 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000433 * clearing.
434 */
435 fill = so->fill;
436 if (table_is_malloced)
437 EMPTY_TO_MINSIZE(so);
438
439 else if (fill > 0) {
440 /* It's a small table with something that needs to be cleared.
441 * Afraid the only safe way is to copy the set entries into
442 * another small table first.
443 */
444 memcpy(small_copy, table, sizeof(small_copy));
445 table = small_copy;
446 EMPTY_TO_MINSIZE(so);
447 }
448 /* else it's a small table that's already empty */
449
450 /* Now we can finally clear things. If C had refcounts, we could
451 * assert that the refcount on table is 1 now, i.e. that this function
452 * has unique access to it, so decref side-effects can't alter it.
453 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000454 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455#ifdef Py_DEBUG
456 assert(i < n);
457 ++i;
458#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000459 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000460 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000461 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462 }
463#ifdef Py_DEBUG
464 else
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000465 assert(entry->key == NULL || entry->key == dummy);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000466#endif
467 }
468
469 if (table_is_malloced)
470 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000471 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472}
473
474/*
475 * Iterate over a set table. Use like so:
476 *
Raymond Hettingerd7946662005-08-01 21:39:29 +0000477 * int pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000478 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000479 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000480 * while (set_next(yourset, &pos, &entry)) {
481 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482 * }
483 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000484 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000485 * mutates the table.
486 */
487static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000488set_next(PySetObject *so, int *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489{
490 register int i, mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000491 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000492
493 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000494 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000495 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000496 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000497 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000498 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000499 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000500 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501 if (i > mask)
502 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000503 assert(table[i].key != NULL);
504 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505 return 1;
506}
507
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000508static void
509set_dealloc(PySetObject *so)
510{
511 register setentry *entry;
512 int fill = so->fill;
513 PyObject_GC_UnTrack(so);
514 Py_TRASHCAN_SAFE_BEGIN(so)
515 if (so->weakreflist != NULL)
516 PyObject_ClearWeakRefs((PyObject *) so);
517
518 for (entry = so->table; fill > 0; entry++) {
519 if (entry->key) {
520 --fill;
521 Py_DECREF(entry->key);
522 }
523 }
524 if (so->table != so->smalltable)
525 PyMem_DEL(so->table);
526 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
527 free_sets[num_free_sets++] = so;
528 else
529 so->ob_type->tp_free(so);
530 Py_TRASHCAN_SAFE_END(so)
531}
532
533static int
534set_tp_print(PySetObject *so, FILE *fp, int flags)
535{
536 setentry *entry;
537 int pos=0;
538 char *emit = ""; /* No separator emitted on first pass */
539 char *separator = ", ";
540
541 fprintf(fp, "%s([", so->ob_type->tp_name);
542 while (set_next(so, &pos, &entry)) {
543 fputs(emit, fp);
544 emit = separator;
545 if (PyObject_Print(entry->key, fp, 0) != 0)
546 return -1;
547 }
548 fputs("])", fp);
549 return 0;
550}
551
552static PyObject *
553set_repr(PySetObject *so)
554{
555 PyObject *keys, *result, *listrepr;
556
557 keys = PySequence_List((PyObject *)so);
558 if (keys == NULL)
559 return NULL;
560 listrepr = PyObject_Repr(keys);
561 Py_DECREF(keys);
562 if (listrepr == NULL)
563 return NULL;
564
565 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
566 PyString_AS_STRING(listrepr));
567 Py_DECREF(listrepr);
568 return result;
569}
570
571static int
572set_len(PyObject *so)
573{
574 return ((PySetObject *)so)->used;
575}
576
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000577static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000578set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000579{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000580 PySetObject *other;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000581 register int i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000582 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000583
584 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000585 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000586
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000587 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000588 if (other == so || other->used == 0)
589 /* a.update(a) or a.update({}); nothing to do */
590 return 0;
591 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000592 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593 * that there will be no (or few) overlapping keys.
594 */
595 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
596 if (set_table_resize(so, (so->used + other->used)*2) != 0)
597 return -1;
598 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000599 for (i = 0; i <= other->mask; i++) {
600 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000601 if (entry->key != NULL &&
602 entry->key != dummy) {
603 Py_INCREF(entry->key);
604 set_insert_key(so, entry->key, entry->hash);
605 }
606 }
607 return 0;
608}
609
610static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000611set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000612{
613 long hash;
614
615 if (!PyString_CheckExact(key) ||
616 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
617 hash = PyObject_Hash(key);
618 if (hash == -1)
619 return -1;
620 }
621 key = (so->lookup)(so, key, hash)->key;
622 return key != NULL && key != dummy;
623}
624
Raymond Hettingerc991db22005-08-11 07:58:45 +0000625static int
626set_contains_entry(PySetObject *so, setentry *entry)
627{
628 PyObject *key;
629
630 key = (so->lookup)(so, entry->key, entry->hash)->key;
631 return key != NULL && key != dummy;
632}
633
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000634static PyObject *
635set_pop(PySetObject *so)
636{
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000637 register int i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000638 register setentry *entry;
639 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000640
641 assert (PyAnySet_Check(so));
642 if (so->used == 0) {
643 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
644 return NULL;
645 }
646
647 /* Set entry to "the first" unused or dummy set entry. We abuse
648 * the hash field of slot 0 to hold a search finger:
649 * If slot 0 has a value, use slot 0.
650 * Else slot 0 is being used to hold a search finger,
651 * and we use its hash value as the first index to look.
652 */
653 entry = &so->table[0];
654 if (entry->key == NULL || entry->key == dummy) {
655 i = (int)entry->hash;
656 /* The hash field may be a real hash value, or it may be a
657 * legit search finger, or it may be a once-legit search
658 * finger that's out of bounds now because it wrapped around
659 * or the table shrunk -- simply make sure it's in bounds now.
660 */
661 if (i > so->mask || i < 1)
662 i = 1; /* skip slot 0 */
663 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
664 i++;
665 if (i > so->mask)
666 i = 1;
667 }
668 }
669 key = entry->key;
670 Py_INCREF(dummy);
671 entry->key = dummy;
672 so->used--;
673 so->table[0].hash = i + 1; /* next place to start */
674 return key;
675}
676
677PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
678
679static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000680set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000681{
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000682 int pos = 0;
683 setentry *entry;
684
685 while (set_next(so, &pos, &entry))
686 Py_VISIT(entry->key);
687 return 0;
688}
689
690static long
691frozenset_hash(PyObject *self)
692{
693 PySetObject *so = (PySetObject *)self;
694 long h, hash = 1927868237L;
695 setentry *entry;
696 int pos = 0;
697
698 if (so->hash != -1)
699 return so->hash;
700
701 hash *= PySet_GET_SIZE(self) + 1;
702 while (set_next(so, &pos, &entry)) {
703 /* Work to increase the bit dispersion for closely spaced hash
704 values. The is important because some use cases have many
705 combinations of a small number of elements with nearby
706 hashes so that many distinct combinations collapse to only
707 a handful of distinct hash values. */
708 h = entry->hash;
709 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
710 }
711 hash = hash * 69069L + 907133923L;
712 if (hash == -1)
713 hash = 590923713L;
714 so->hash = hash;
715 return hash;
716}
717
718static long
719set_nohash(PyObject *self)
720{
721 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
722 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000723}
724
Raymond Hettingera9d99362005-08-05 00:01:15 +0000725/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000726
Raymond Hettingerd7946662005-08-01 21:39:29 +0000727static PyTypeObject PySetIter_Type; /* Forward */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000728
729typedef struct {
730 PyObject_HEAD
731 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
732 int si_used;
733 int si_pos;
734 long len;
735} setiterobject;
736
737static PyObject *
738set_iter(PySetObject *so)
739{
740 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
741 if (si == NULL)
742 return NULL;
743 Py_INCREF(so);
744 si->si_set = so;
745 si->si_used = so->used;
746 si->si_pos = 0;
747 si->len = so->used;
748 return (PyObject *)si;
749}
750
751static void
752setiter_dealloc(setiterobject *si)
753{
754 Py_XDECREF(si->si_set);
755 PyObject_Del(si);
756}
757
758static int
759setiter_len(setiterobject *si)
760{
761 if (si->si_set != NULL && si->si_used == si->si_set->used)
762 return si->len;
763 return 0;
764}
765
766static PySequenceMethods setiter_as_sequence = {
767 (inquiry)setiter_len, /* sq_length */
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000768 0, /* sq_concat */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000769};
770
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000771static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000772{
773 PyObject *key;
774 register int i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000775 register setentry *entry;
776 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000778 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000779 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000780 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000781
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000782 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000783 PyErr_SetString(PyExc_RuntimeError,
784 "Set changed size during iteration");
785 si->si_used = -1; /* Make this state sticky */
786 return NULL;
787 }
788
789 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000790 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000791 entry = so->table;
792 mask = so->mask;
793 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794 i++;
795 si->si_pos = i+1;
796 if (i > mask)
797 goto fail;
798 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000799 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800 Py_INCREF(key);
801 return key;
802
803fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000804 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805 si->si_set = NULL;
806 return NULL;
807}
808
Hye-Shik Change2956762005-08-01 05:26:41 +0000809static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810 PyObject_HEAD_INIT(&PyType_Type)
811 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000812 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813 sizeof(setiterobject), /* tp_basicsize */
814 0, /* tp_itemsize */
815 /* methods */
816 (destructor)setiter_dealloc, /* tp_dealloc */
817 0, /* tp_print */
818 0, /* tp_getattr */
819 0, /* tp_setattr */
820 0, /* tp_compare */
821 0, /* tp_repr */
822 0, /* tp_as_number */
823 &setiter_as_sequence, /* tp_as_sequence */
824 0, /* tp_as_mapping */
825 0, /* tp_hash */
826 0, /* tp_call */
827 0, /* tp_str */
828 PyObject_GenericGetAttr, /* tp_getattro */
829 0, /* tp_setattro */
830 0, /* tp_as_buffer */
831 Py_TPFLAGS_DEFAULT, /* tp_flags */
832 0, /* tp_doc */
833 0, /* tp_traverse */
834 0, /* tp_clear */
835 0, /* tp_richcompare */
836 0, /* tp_weaklistoffset */
837 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000838 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839};
840
Raymond Hettingerd7946662005-08-01 21:39:29 +0000841static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000842set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000843{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000844 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000845
Raymond Hettingerd7946662005-08-01 21:39:29 +0000846 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000847 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000848
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000849 if (PyDict_Check(other)) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000850 PyObject *key, *value;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000851 int pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000852 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000853 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000854 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000855 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000856 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000857 }
858
Raymond Hettingera38123e2003-11-24 22:18:49 +0000859 it = PyObject_GetIter(other);
860 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000861 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000862
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000863 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000864 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000865 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000866 Py_DECREF(key);
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 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000870 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000871 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000872 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000873 return -1;
874 return 0;
875}
876
877static PyObject *
878set_update(PySetObject *so, PyObject *other)
879{
880 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000881 return NULL;
882 Py_RETURN_NONE;
883}
884
885PyDoc_STRVAR(update_doc,
886"Update a set with the union of itself and another.");
887
888static PyObject *
889make_new_set(PyTypeObject *type, PyObject *iterable)
890{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000892
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000893 if (dummy == NULL) { /* Auto-initialize dummy */
894 dummy = PyString_FromString("<dummy key>");
895 if (dummy == NULL)
896 return NULL;
897 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000898
899 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000900 if (num_free_sets &&
901 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
902 so = free_sets[--num_free_sets];
903 assert (so != NULL && PyAnySet_CheckExact(so));
904 so->ob_type = type;
905 _Py_NewReference((PyObject *)so);
906 EMPTY_TO_MINSIZE(so);
907 PyObject_GC_Track(so);
908 } else {
909 so = (PySetObject *)type->tp_alloc(type, 0);
910 if (so == NULL)
911 return NULL;
912 /* tp_alloc has already zeroed the structure */
913 assert(so->table == NULL && so->fill == 0 && so->used == 0);
914 INIT_NONZERO_SET_SLOTS(so);
915 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000916
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000917 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000918 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000919
Raymond Hettingera38123e2003-11-24 22:18:49 +0000920 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000921 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000922 Py_DECREF(so);
923 return NULL;
924 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000925 }
926
Raymond Hettingera690a992003-11-16 16:17:49 +0000927 return (PyObject *)so;
928}
929
Raymond Hettingerd7946662005-08-01 21:39:29 +0000930/* The empty frozenset is a singleton */
931static PyObject *emptyfrozenset = NULL;
932
Raymond Hettingera690a992003-11-16 16:17:49 +0000933static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000934frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000935{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000936 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000937
938 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
939 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000940
941 if (type != &PyFrozenSet_Type)
942 return make_new_set(type, iterable);
943
944 if (iterable != NULL) {
945 /* frozenset(f) is idempotent */
946 if (PyFrozenSet_CheckExact(iterable)) {
947 Py_INCREF(iterable);
948 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000949 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000950 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000951 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952 return result;
953 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000954 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000955 /* The empty frozenset is a singleton */
956 if (emptyfrozenset == NULL)
957 emptyfrozenset = make_new_set(type, NULL);
958 Py_XINCREF(emptyfrozenset);
959 return emptyfrozenset;
960}
961
962void
963PySet_Fini(void)
964{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000965 PySetObject *so;
966
967 while (num_free_sets) {
968 num_free_sets--;
969 so = free_sets[num_free_sets];
970 PyObject_GC_Del(so);
971 }
Raymond Hettingera9d99362005-08-05 00:01:15 +0000972 Py_XDECREF(dummy);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000973 Py_XDECREF(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000974}
975
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000976static PyObject *
977set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
978{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000979 return make_new_set(type, NULL);
980}
981
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000982/* set_swap_bodies() switches the contents of any two sets by moving their
983 internal data pointers and, if needed, copying the internal smalltables.
984 Semantically equivalent to:
985
986 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
987
988 The function always succeeds and it leaves both objects in a stable state.
989 Useful for creating temporary frozensets from sets for membership testing
990 in __contains__(), discard(), and remove(). Also useful for operations
991 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +0000992 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000993*/
994
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000995static void
996set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +0000997{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000998 int t;
999 setentry *u;
1000 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1001 setentry tab[PySet_MINSIZE];
1002 long h;
1003
1004 t = a->fill; a->fill = b->fill; b->fill = t;
1005 t = a->used; a->used = b->used; b->used = t;
1006 t = a->mask; a->mask = b->mask; b->mask = t;
1007
1008 u = a->table;
1009 if (a->table == a->smalltable)
1010 u = b->smalltable;
1011 a->table = b->table;
1012 if (b->table == b->smalltable)
1013 a->table = a->smalltable;
1014 b->table = u;
1015
1016 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1017
1018 if (a->table == a->smalltable || b->table == b->smalltable) {
1019 memcpy(tab, a->smalltable, sizeof(tab));
1020 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1021 memcpy(b->smalltable, tab, sizeof(tab));
1022 }
1023
Raymond Hettingera580c472005-08-05 17:19:54 +00001024 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1025 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1026 h = a->hash; a->hash = b->hash; b->hash = h;
1027 } else {
1028 a->hash = -1;
1029 b->hash = -1;
1030 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001031}
1032
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001033static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001034set_copy(PySetObject *so)
1035{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001036 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001037}
1038
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001039static PyObject *
1040frozenset_copy(PySetObject *so)
1041{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001042 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001043 Py_INCREF(so);
1044 return (PyObject *)so;
1045 }
1046 return set_copy(so);
1047}
1048
Raymond Hettingera690a992003-11-16 16:17:49 +00001049PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1050
1051static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001052set_clear(PySetObject *so)
1053{
1054 set_clear_internal(so);
1055 Py_RETURN_NONE;
1056}
1057
1058PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1059
1060static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001061set_union(PySetObject *so, PyObject *other)
1062{
1063 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001064
1065 result = (PySetObject *)set_copy(so);
1066 if (result == NULL)
1067 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001068 if ((PyObject *)so == other)
1069 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001070 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001071 Py_DECREF(result);
1072 return NULL;
1073 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001074 return (PyObject *)result;
1075}
1076
1077PyDoc_STRVAR(union_doc,
1078 "Return the union of two sets as a new set.\n\
1079\n\
1080(i.e. all elements that are in either set.)");
1081
1082static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001083set_or(PySetObject *so, PyObject *other)
1084{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001085 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001086 Py_INCREF(Py_NotImplemented);
1087 return Py_NotImplemented;
1088 }
1089 return set_union(so, other);
1090}
1091
1092static PyObject *
1093set_ior(PySetObject *so, PyObject *other)
1094{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001095 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001096 Py_INCREF(Py_NotImplemented);
1097 return Py_NotImplemented;
1098 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001099 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001100 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001101 Py_INCREF(so);
1102 return (PyObject *)so;
1103}
1104
1105static PyObject *
1106set_intersection(PySetObject *so, PyObject *other)
1107{
1108 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001109 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001110
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001111 if ((PyObject *)so == other)
1112 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001113
Raymond Hettingera690a992003-11-16 16:17:49 +00001114 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1115 if (result == NULL)
1116 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001117
Raymond Hettingerc991db22005-08-11 07:58:45 +00001118 if (PyAnySet_Check(other)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001119 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001120 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001121
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001122 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001123 tmp = (PyObject *)so;
1124 so = (PySetObject *)other;
1125 other = tmp;
1126 }
1127
Raymond Hettingerc991db22005-08-11 07:58:45 +00001128 while (set_next((PySetObject *)other, &pos, &entry)) {
1129 if (set_contains_entry(so, entry)) {
1130 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001131 Py_DECREF(result);
1132 return NULL;
1133 }
1134 }
1135 }
1136 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001137 }
1138
Raymond Hettingera690a992003-11-16 16:17:49 +00001139 it = PyObject_GetIter(other);
1140 if (it == NULL) {
1141 Py_DECREF(result);
1142 return NULL;
1143 }
1144
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001145 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001146 if (set_contains_key(so, key)) {
1147 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001148 Py_DECREF(it);
1149 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001150 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001151 return NULL;
1152 }
1153 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001154 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001155 }
1156 Py_DECREF(it);
1157 if (PyErr_Occurred()) {
1158 Py_DECREF(result);
1159 return NULL;
1160 }
1161 return (PyObject *)result;
1162}
1163
1164PyDoc_STRVAR(intersection_doc,
1165"Return the intersection of two sets as a new set.\n\
1166\n\
1167(i.e. all elements that are in both sets.)");
1168
1169static PyObject *
1170set_intersection_update(PySetObject *so, PyObject *other)
1171{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001172 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001173
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001174 tmp = set_intersection(so, other);
1175 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001176 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001177 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001178 Py_DECREF(tmp);
1179 Py_RETURN_NONE;
1180}
1181
1182PyDoc_STRVAR(intersection_update_doc,
1183"Update a set with the intersection of itself and another.");
1184
1185static PyObject *
1186set_and(PySetObject *so, PyObject *other)
1187{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001188 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001189 Py_INCREF(Py_NotImplemented);
1190 return Py_NotImplemented;
1191 }
1192 return set_intersection(so, other);
1193}
1194
1195static PyObject *
1196set_iand(PySetObject *so, PyObject *other)
1197{
1198 PyObject *result;
1199
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001200 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001201 Py_INCREF(Py_NotImplemented);
1202 return Py_NotImplemented;
1203 }
1204 result = set_intersection_update(so, other);
1205 if (result == NULL)
1206 return NULL;
1207 Py_DECREF(result);
1208 Py_INCREF(so);
1209 return (PyObject *)so;
1210}
1211
Raymond Hettingerc991db22005-08-11 07:58:45 +00001212int
1213set_difference_update_internal(PySetObject *so, PyObject *other)
1214{
1215 if ((PyObject *)so == other)
1216 return set_clear_internal(so);
1217
1218 if (PyAnySet_Check(other)) {
1219 setentry *entry;
1220 int pos = 0;
1221
1222 while (set_next((PySetObject *)other, &pos, &entry))
1223 set_discard_entry(so, entry);
1224 } else {
1225 PyObject *key, *it;
1226 it = PyObject_GetIter(other);
1227 if (it == NULL)
1228 return -1;
1229
1230 while ((key = PyIter_Next(it)) != NULL) {
1231 if (set_discard_key(so, key) == -1) {
1232 Py_DECREF(it);
1233 Py_DECREF(key);
1234 return -1;
1235 }
1236 Py_DECREF(key);
1237 }
1238 Py_DECREF(it);
1239 if (PyErr_Occurred())
1240 return -1;
1241 }
1242 /* If more than 1/5 are dummies, then resize them away. */
1243 if ((so->fill - so->used) * 5 < so->mask)
1244 return 0;
1245 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1246}
1247
Raymond Hettingera690a992003-11-16 16:17:49 +00001248static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001249set_difference_update(PySetObject *so, PyObject *other)
1250{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001251 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001252 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001253 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001254}
1255
1256PyDoc_STRVAR(difference_update_doc,
1257"Remove all elements of another set from this set.");
1258
1259static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001260set_difference(PySetObject *so, PyObject *other)
1261{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001262 PyObject *result;
1263 setentry *entry;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001264 int pos = 0;
1265
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001266 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001267 result = set_copy(so);
1268 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001269 return NULL;
1270 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001271 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001272 Py_DECREF(result);
1273 return NULL;
1274 }
1275
1276 result = make_new_set(so->ob_type, NULL);
1277 if (result == NULL)
1278 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001279
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001280 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001281 while (set_next(so, &pos, &entry)) {
1282 setentry entrycopy;
1283 entrycopy.hash = entry->hash;
1284 entrycopy.key = entry->key;
1285 if (!PyDict_Contains(other, entry->key)) {
1286 if (set_add_entry((PySetObject *)result, &entrycopy) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001287 return NULL;
1288 }
1289 }
1290 return result;
1291 }
1292
Raymond Hettingerc991db22005-08-11 07:58:45 +00001293 while (set_next(so, &pos, &entry)) {
1294 if (!set_contains_entry((PySetObject *)other, entry)) {
1295 if (set_add_entry((PySetObject *)result, entry) == -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001296 return NULL;
1297 }
1298 }
1299 return result;
1300}
1301
1302PyDoc_STRVAR(difference_doc,
1303"Return the difference of two sets as a new set.\n\
1304\n\
1305(i.e. all elements that are in this set but not the other.)");
1306static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001307set_sub(PySetObject *so, PyObject *other)
1308{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001309 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001310 Py_INCREF(Py_NotImplemented);
1311 return Py_NotImplemented;
1312 }
1313 return set_difference(so, other);
1314}
1315
1316static PyObject *
1317set_isub(PySetObject *so, PyObject *other)
1318{
1319 PyObject *result;
1320
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001321 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001322 Py_INCREF(Py_NotImplemented);
1323 return Py_NotImplemented;
1324 }
1325 result = set_difference_update(so, other);
1326 if (result == NULL)
1327 return NULL;
1328 Py_DECREF(result);
1329 Py_INCREF(so);
1330 return (PyObject *)so;
1331}
1332
1333static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001334set_symmetric_difference_update(PySetObject *so, PyObject *other)
1335{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001336 PySetObject *otherset;
1337 PyObject *key;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +00001338 int pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001339 setentry *entry;
1340
1341 if ((PyObject *)so == other)
1342 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001343
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001344 if (PyDict_Check(other)) {
1345 PyObject *value;
1346 int rv;
1347 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001348 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001349 if (rv == -1)
1350 return NULL;
1351 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001352 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001353 return NULL;
1354 }
1355 }
1356 Py_RETURN_NONE;
1357 }
1358
1359 if (PyAnySet_Check(other)) {
1360 Py_INCREF(other);
1361 otherset = (PySetObject *)other;
1362 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001363 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1364 if (otherset == NULL)
1365 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001366 }
1367
Raymond Hettingerc991db22005-08-11 07:58:45 +00001368 while (set_next(otherset, &pos, &entry)) {
1369 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001370 if (rv == -1) {
1371 Py_XDECREF(otherset);
1372 return NULL;
1373 }
1374 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001375 if (set_add_entry(so, entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001376 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001377 return NULL;
1378 }
1379 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001380 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001381 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001382 Py_RETURN_NONE;
1383}
1384
1385PyDoc_STRVAR(symmetric_difference_update_doc,
1386"Update a set with the symmetric difference of itself and another.");
1387
1388static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001389set_symmetric_difference(PySetObject *so, PyObject *other)
1390{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001391 PyObject *rv;
1392 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001393
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001394 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1395 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001396 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001397 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1398 if (rv == NULL)
1399 return NULL;
1400 Py_DECREF(rv);
1401 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001402}
1403
1404PyDoc_STRVAR(symmetric_difference_doc,
1405"Return the symmetric difference of two sets as a new set.\n\
1406\n\
1407(i.e. all elements that are in exactly one of the sets.)");
1408
1409static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001410set_xor(PySetObject *so, PyObject *other)
1411{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001412 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001413 Py_INCREF(Py_NotImplemented);
1414 return Py_NotImplemented;
1415 }
1416 return set_symmetric_difference(so, other);
1417}
1418
1419static PyObject *
1420set_ixor(PySetObject *so, PyObject *other)
1421{
1422 PyObject *result;
1423
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001424 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001425 Py_INCREF(Py_NotImplemented);
1426 return Py_NotImplemented;
1427 }
1428 result = set_symmetric_difference_update(so, other);
1429 if (result == NULL)
1430 return NULL;
1431 Py_DECREF(result);
1432 Py_INCREF(so);
1433 return (PyObject *)so;
1434}
1435
1436static PyObject *
1437set_issubset(PySetObject *so, PyObject *other)
1438{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001439 setentry *entry;
1440 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001441
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001442 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001443 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001444 tmp = make_new_set(&PySet_Type, other);
1445 if (tmp == NULL)
1446 return NULL;
1447 result = set_issubset(so, tmp);
1448 Py_DECREF(tmp);
1449 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001450 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001451 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001452 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001453
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001454 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001455 if (!set_contains_entry((PySetObject *)other, entry))
Raymond Hettingera690a992003-11-16 16:17:49 +00001456 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001457 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001458 Py_RETURN_TRUE;
1459}
1460
1461PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1462
1463static PyObject *
1464set_issuperset(PySetObject *so, PyObject *other)
1465{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001466 PyObject *tmp, *result;
1467
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001468 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001469 tmp = make_new_set(&PySet_Type, other);
1470 if (tmp == NULL)
1471 return NULL;
1472 result = set_issuperset(so, tmp);
1473 Py_DECREF(tmp);
1474 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001475 }
1476 return set_issubset((PySetObject *)other, (PyObject *)so);
1477}
1478
1479PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1480
Raymond Hettingera690a992003-11-16 16:17:49 +00001481static PyObject *
1482set_richcompare(PySetObject *v, PyObject *w, int op)
1483{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001484 PyObject *r1, *r2;
1485
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001486 if(!PyAnySet_Check(w)) {
1487 if (op == Py_EQ)
1488 Py_RETURN_FALSE;
1489 if (op == Py_NE)
1490 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001491 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1492 return NULL;
1493 }
1494 switch (op) {
1495 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001496 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001497 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001498 return set_issubset(v, w);
1499 case Py_NE:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001500 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001501 Py_RETURN_TRUE;
1502 r1 = set_issubset(v, w);
1503 assert (r1 != NULL);
1504 r2 = PyBool_FromLong(PyObject_Not(r1));
1505 Py_DECREF(r1);
1506 return r2;
1507 case Py_LE:
1508 return set_issubset(v, w);
1509 case Py_GE:
1510 return set_issuperset(v, w);
1511 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001512 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001513 Py_RETURN_FALSE;
1514 return set_issubset(v, w);
1515 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001516 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001517 Py_RETURN_FALSE;
1518 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001519 }
1520 Py_INCREF(Py_NotImplemented);
1521 return Py_NotImplemented;
1522}
1523
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001524static int
1525set_nocmp(PyObject *self)
1526{
1527 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1528 return -1;
1529}
1530
Raymond Hettingera690a992003-11-16 16:17:49 +00001531static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001532set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001533{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001534 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001535 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001536 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001537}
1538
1539PyDoc_STRVAR(add_doc,
1540"Add an element to a set.\n\
1541\n\
1542This has no effect if the element is already present.");
1543
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001544static int
1545set_contains(PySetObject *so, PyObject *key)
1546{
1547 PyObject *tmpkey;
1548 int rv;
1549
1550 rv = set_contains_key(so, key);
1551 if (rv == -1) {
1552 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1553 return -1;
1554 PyErr_Clear();
1555 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1556 if (tmpkey == NULL)
1557 return -1;
1558 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1559 rv = set_contains(so, tmpkey);
1560 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1561 Py_DECREF(tmpkey);
1562 }
1563 return rv;
1564}
1565
1566static PyObject *
1567set_direct_contains(PySetObject *so, PyObject *key)
1568{
1569 long result;
1570
1571 result = set_contains(so, key);
1572 if (result == -1)
1573 return NULL;
1574 return PyBool_FromLong(result);
1575}
1576
1577PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1578
Raymond Hettingera690a992003-11-16 16:17:49 +00001579static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001580set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001581{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001582 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001583 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001584
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001585 rv = set_discard_key(so, key);
1586 if (rv == -1) {
1587 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1588 return NULL;
1589 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001590 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1591 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001592 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001593 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001594 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001595 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001596 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001597 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001598 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001599 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001600 return NULL;
1601 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001602 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001603}
1604
1605PyDoc_STRVAR(remove_doc,
1606"Remove an element from a set; it must be a member.\n\
1607\n\
1608If the element is not a member, raise a KeyError.");
1609
1610static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001611set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001612{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001613 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001614 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001615
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001616 rv = set_discard_key(so, key);
1617 if (rv == -1) {
1618 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1619 return NULL;
1620 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001621 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1622 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001623 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001624 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001625 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001626 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001627 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001628 return result;
1629 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001630 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001631}
1632
1633PyDoc_STRVAR(discard_doc,
1634"Remove an element from a set if it is a member.\n\
1635\n\
1636If the element is not a member, do nothing.");
1637
1638static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001639set_reduce(PySetObject *so)
1640{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001641 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001642
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001643 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001644 if (keys == NULL)
1645 goto done;
1646 args = PyTuple_Pack(1, keys);
1647 if (args == NULL)
1648 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001649 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1650 if (dict == NULL) {
1651 PyErr_Clear();
1652 dict = Py_None;
1653 Py_INCREF(dict);
1654 }
1655 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001656done:
1657 Py_XDECREF(args);
1658 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001659 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001660 return result;
1661}
1662
1663PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1664
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001665static int
1666set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1667{
1668 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001669
1670 if (!PyAnySet_Check(self))
1671 return -1;
1672 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1673 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001674 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001675 self->hash = -1;
1676 if (iterable == NULL)
1677 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001678 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001679}
1680
Raymond Hettingera690a992003-11-16 16:17:49 +00001681static PySequenceMethods set_as_sequence = {
1682 (inquiry)set_len, /* sq_length */
1683 0, /* sq_concat */
1684 0, /* sq_repeat */
1685 0, /* sq_item */
1686 0, /* sq_slice */
1687 0, /* sq_ass_item */
1688 0, /* sq_ass_slice */
1689 (objobjproc)set_contains, /* sq_contains */
1690};
1691
1692/* set object ********************************************************/
1693
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001694#ifdef Py_DEBUG
1695static PyObject *test_c_api(PySetObject *so);
1696
1697PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1698All is well if assertions don't fail.");
1699#endif
1700
Raymond Hettingera690a992003-11-16 16:17:49 +00001701static PyMethodDef set_methods[] = {
1702 {"add", (PyCFunction)set_add, METH_O,
1703 add_doc},
1704 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1705 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001706 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001707 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001708 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1709 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001710 {"discard", (PyCFunction)set_discard, METH_O,
1711 discard_doc},
1712 {"difference", (PyCFunction)set_difference, METH_O,
1713 difference_doc},
1714 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1715 difference_update_doc},
1716 {"intersection",(PyCFunction)set_intersection, METH_O,
1717 intersection_doc},
1718 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1719 intersection_update_doc},
1720 {"issubset", (PyCFunction)set_issubset, METH_O,
1721 issubset_doc},
1722 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1723 issuperset_doc},
1724 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1725 pop_doc},
1726 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1727 reduce_doc},
1728 {"remove", (PyCFunction)set_remove, METH_O,
1729 remove_doc},
1730 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1731 symmetric_difference_doc},
1732 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1733 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001734#ifdef Py_DEBUG
1735 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1736 test_c_api_doc},
1737#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001738 {"union", (PyCFunction)set_union, METH_O,
1739 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001740 {"update", (PyCFunction)set_update, METH_O,
1741 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001742 {NULL, NULL} /* sentinel */
1743};
1744
1745static PyNumberMethods set_as_number = {
1746 0, /*nb_add*/
1747 (binaryfunc)set_sub, /*nb_subtract*/
1748 0, /*nb_multiply*/
1749 0, /*nb_divide*/
1750 0, /*nb_remainder*/
1751 0, /*nb_divmod*/
1752 0, /*nb_power*/
1753 0, /*nb_negative*/
1754 0, /*nb_positive*/
1755 0, /*nb_absolute*/
1756 0, /*nb_nonzero*/
1757 0, /*nb_invert*/
1758 0, /*nb_lshift*/
1759 0, /*nb_rshift*/
1760 (binaryfunc)set_and, /*nb_and*/
1761 (binaryfunc)set_xor, /*nb_xor*/
1762 (binaryfunc)set_or, /*nb_or*/
1763 0, /*nb_coerce*/
1764 0, /*nb_int*/
1765 0, /*nb_long*/
1766 0, /*nb_float*/
1767 0, /*nb_oct*/
1768 0, /*nb_hex*/
1769 0, /*nb_inplace_add*/
1770 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1771 0, /*nb_inplace_multiply*/
1772 0, /*nb_inplace_divide*/
1773 0, /*nb_inplace_remainder*/
1774 0, /*nb_inplace_power*/
1775 0, /*nb_inplace_lshift*/
1776 0, /*nb_inplace_rshift*/
1777 (binaryfunc)set_iand, /*nb_inplace_and*/
1778 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1779 (binaryfunc)set_ior, /*nb_inplace_or*/
1780};
1781
1782PyDoc_STRVAR(set_doc,
1783"set(iterable) --> set object\n\
1784\n\
1785Build an unordered collection.");
1786
1787PyTypeObject PySet_Type = {
1788 PyObject_HEAD_INIT(&PyType_Type)
1789 0, /* ob_size */
1790 "set", /* tp_name */
1791 sizeof(PySetObject), /* tp_basicsize */
1792 0, /* tp_itemsize */
1793 /* methods */
1794 (destructor)set_dealloc, /* tp_dealloc */
1795 (printfunc)set_tp_print, /* tp_print */
1796 0, /* tp_getattr */
1797 0, /* tp_setattr */
1798 (cmpfunc)set_nocmp, /* tp_compare */
1799 (reprfunc)set_repr, /* tp_repr */
1800 &set_as_number, /* tp_as_number */
1801 &set_as_sequence, /* tp_as_sequence */
1802 0, /* tp_as_mapping */
1803 set_nohash, /* tp_hash */
1804 0, /* tp_call */
1805 0, /* tp_str */
1806 PyObject_GenericGetAttr, /* tp_getattro */
1807 0, /* tp_setattro */
1808 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001809 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001810 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001811 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001812 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001813 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001814 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001815 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001816 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001817 0, /* tp_iternext */
1818 set_methods, /* tp_methods */
1819 0, /* tp_members */
1820 0, /* tp_getset */
1821 0, /* tp_base */
1822 0, /* tp_dict */
1823 0, /* tp_descr_get */
1824 0, /* tp_descr_set */
1825 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001826 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001827 PyType_GenericAlloc, /* tp_alloc */
1828 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001829 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001830};
1831
1832/* frozenset object ********************************************************/
1833
1834
1835static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001836 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001837 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001838 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001839 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001840 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001841 difference_doc},
1842 {"intersection",(PyCFunction)set_intersection, METH_O,
1843 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001844 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001845 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001846 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001847 issuperset_doc},
1848 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1849 reduce_doc},
1850 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1851 symmetric_difference_doc},
1852 {"union", (PyCFunction)set_union, METH_O,
1853 union_doc},
1854 {NULL, NULL} /* sentinel */
1855};
1856
1857static PyNumberMethods frozenset_as_number = {
1858 0, /*nb_add*/
1859 (binaryfunc)set_sub, /*nb_subtract*/
1860 0, /*nb_multiply*/
1861 0, /*nb_divide*/
1862 0, /*nb_remainder*/
1863 0, /*nb_divmod*/
1864 0, /*nb_power*/
1865 0, /*nb_negative*/
1866 0, /*nb_positive*/
1867 0, /*nb_absolute*/
1868 0, /*nb_nonzero*/
1869 0, /*nb_invert*/
1870 0, /*nb_lshift*/
1871 0, /*nb_rshift*/
1872 (binaryfunc)set_and, /*nb_and*/
1873 (binaryfunc)set_xor, /*nb_xor*/
1874 (binaryfunc)set_or, /*nb_or*/
1875};
1876
1877PyDoc_STRVAR(frozenset_doc,
1878"frozenset(iterable) --> frozenset object\n\
1879\n\
1880Build an immutable unordered collection.");
1881
1882PyTypeObject PyFrozenSet_Type = {
1883 PyObject_HEAD_INIT(&PyType_Type)
1884 0, /* ob_size */
1885 "frozenset", /* tp_name */
1886 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001887 0, /* tp_itemsize */
1888 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001889 (destructor)set_dealloc, /* tp_dealloc */
1890 (printfunc)set_tp_print, /* tp_print */
1891 0, /* tp_getattr */
1892 0, /* tp_setattr */
1893 (cmpfunc)set_nocmp, /* tp_compare */
1894 (reprfunc)set_repr, /* tp_repr */
1895 &frozenset_as_number, /* tp_as_number */
1896 &set_as_sequence, /* tp_as_sequence */
1897 0, /* tp_as_mapping */
1898 frozenset_hash, /* tp_hash */
1899 0, /* tp_call */
1900 0, /* tp_str */
1901 PyObject_GenericGetAttr, /* tp_getattro */
1902 0, /* tp_setattro */
1903 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001904 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001905 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001906 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001907 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001908 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001909 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001910 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001911 (getiterfunc)set_iter, /* tp_iter */
1912 0, /* tp_iternext */
1913 frozenset_methods, /* tp_methods */
1914 0, /* tp_members */
1915 0, /* tp_getset */
1916 0, /* tp_base */
1917 0, /* tp_dict */
1918 0, /* tp_descr_get */
1919 0, /* tp_descr_set */
1920 0, /* tp_dictoffset */
1921 0, /* tp_init */
1922 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001923 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001924 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001925};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001926
1927
1928/***** C API functions *************************************************/
1929
1930PyObject *
1931PySet_New(PyObject *iterable)
1932{
1933 return make_new_set(&PySet_Type, iterable);
1934}
1935
1936PyObject *
1937PyFrozenSet_New(PyObject *iterable)
1938{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001939 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001940
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001941 if (iterable == NULL)
1942 args = PyTuple_New(0);
1943 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001944 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001945 if (args == NULL)
1946 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001947 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001948 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001949 return result;
1950}
1951
1952int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001953PySet_Size(PyObject *anyset)
1954{
1955 if (!PyAnySet_Check(anyset)) {
1956 PyErr_BadInternalCall();
1957 return -1;
1958 }
1959 return ((PySetObject *)anyset)->used;
1960}
1961
1962int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001963PySet_Contains(PyObject *anyset, PyObject *key)
1964{
1965 if (!PyAnySet_Check(anyset)) {
1966 PyErr_BadInternalCall();
1967 return -1;
1968 }
1969 return set_contains_key((PySetObject *)anyset, key);
1970}
1971
1972int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001973PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001974{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001975 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001976 PyErr_BadInternalCall();
1977 return -1;
1978 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001979 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001980}
1981
1982int
1983PySet_Add(PyObject *set, PyObject *key)
1984{
1985 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1986 PyErr_BadInternalCall();
1987 return -1;
1988 }
1989 return set_add_key((PySetObject *)set, key);
1990}
1991
1992PyObject *
1993PySet_Pop(PyObject *set)
1994{
1995 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
1996 PyErr_BadInternalCall();
1997 return NULL;
1998 }
1999 return set_pop((PySetObject *)set);
2000}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002001
2002
2003#ifdef Py_DEBUG
2004
2005/* Test code to be called with any three element set.
2006 Returns True and original set is restored. */
2007
2008#define assertRaises(call_return_value, exception) \
2009 do { \
2010 assert(call_return_value); \
2011 assert(PyErr_ExceptionMatches(exception)); \
2012 PyErr_Clear(); \
2013 } while(0)
2014
2015static PyObject *
2016test_c_api(PySetObject *so)
2017{
2018 PyObject *elem, *dup, *t, *f, *ob = (PyObject *)so;
2019
2020 /* Verify preconditions and exercise type/size checks */
2021 assert(PyAnySet_Check(ob));
2022 assert(PyAnySet_CheckExact(ob));
2023 assert(!PyFrozenSet_CheckExact(ob));
2024 assert(PySet_Size(ob) == 3);
2025 assert(PySet_GET_SIZE(ob) == 3);
2026
2027 /* Raise TypeError for non-iterable constructor arguments */
2028 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2029 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2030
2031 /* Raise TypeError for unhashable key */
2032 dup = PySet_New(ob);
2033 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2034 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2035 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2036
2037 /* Exercise successful pop, contains, add, and discard */
2038 elem = PySet_Pop(ob);
2039 assert(PySet_Contains(ob, elem) == 0);
2040 assert(PySet_GET_SIZE(ob) == 2);
2041 assert(PySet_Add(ob, elem) == 0);
2042 assert(PySet_Contains(ob, elem) == 1);
2043 assert(PySet_GET_SIZE(ob) == 3);
2044 assert(PySet_Discard(ob, elem) == 1);
2045 assert(PySet_GET_SIZE(ob) == 2);
2046 assert(PySet_Discard(ob, elem) == 0);
2047 assert(PySet_GET_SIZE(ob) == 2);
2048
2049 /* Raise SystemError when self argument is not a set or frozenset. */
2050 t = PyTuple_New(0);
2051 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2052 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2053 Py_DECREF(t);
2054
2055 /* Raise SystemError when self argument is not a set. */
2056 f = PyFrozenSet_New(dup);
2057 assert(PySet_Size(f) == 3);
2058 assert(PyFrozenSet_CheckExact(f));
2059 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2060 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2061 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2062 Py_DECREF(f);
2063
2064 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002065 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2066 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002067 assert(PySet_GET_SIZE(ob) == 0);
2068 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2069
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002070 /* Restore the set from the copy using the PyNumber API */
2071 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2072 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002073
2074 /* Verify constructors accept NULL arguments */
2075 f = PySet_New(NULL);
2076 assert(f != NULL);
2077 assert(PySet_GET_SIZE(f) == 0);
2078 Py_DECREF(f);
2079 f = PyFrozenSet_New(NULL);
2080 assert(f != NULL);
2081 assert(PyFrozenSet_CheckExact(f));
2082 assert(PySet_GET_SIZE(f) == 0);
2083 Py_DECREF(f);
2084
2085 Py_DECREF(elem);
2086 Py_DECREF(dup);
2087 Py_RETURN_TRUE;
2088}
2089
2090#endif