blob: b379845d7a979f6bbddeea27b79d14597045cfb4 [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öwis68192102007-07-21 06:55:02 +00006 Copyright (c) 2003-2007 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
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
19 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
25}
26
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000027/* This must be >= 1. */
28#define PERTURB_SHIFT 5
29
30/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000031static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032
Armin Rigoe1709372006-04-12 17:06:05 +000033#ifdef Py_REF_DEBUG
34PyObject *
35_PySet_Dummy(void)
36{
37 return dummy;
38}
39#endif
40
Raymond Hettingerbc841a12005-08-07 13:02:53 +000041#define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
44 (so)->hash = -1; \
45 } while(0)
46
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047#define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000050 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051 } while(0)
52
Raymond Hettingerbc841a12005-08-07 13:02:53 +000053/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes5b970ad2008-02-06 13:33:44 +000054#ifndef PySet_MAXFREELIST
55#define PySet_MAXFREELIST 80
56#endif
57static PySetObject *free_list[PySet_MAXFREELIST];
58static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000059
60/*
61The basic lookup function used by all operations.
62This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
63Open addressing is preferred over chaining since the link overhead for
64chaining would be substantial (100% with typical malloc overhead).
65
66The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000067probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000068
69All arithmetic on hash should ignore overflow.
70
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000071Unlike the dictionary implementation, the lookkey functions can return
72NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073*/
74
75static setentry *
76set_lookkey(PySetObject *so, PyObject *key, register long hash)
77{
Martin v. Löwis18e16552006-02-15 17:27:45 +000078 register Py_ssize_t i;
79 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000080 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +000081 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000082 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000083 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000084 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000085 PyObject *startkey;
86
87 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000088 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000089 if (entry->key == NULL || entry->key == key)
90 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000091
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000092 if (entry->key == dummy)
93 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000094 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000095 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000096 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000097 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
98 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000099 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000100 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000102 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000103 }
104 else {
105 /* The compare did major nasty stuff to the
106 * set: start over.
107 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000108 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000109 }
110 }
111 freeslot = NULL;
112 }
113
114 /* In the loop, key == dummy is by far (factor of 100s) the
115 least likely outcome, so test for that last. */
116 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
117 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000118 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000119 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000121 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 break;
123 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000124 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000125 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000126 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000127 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000128 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
129 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000130 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000131 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000132 if (cmp > 0)
133 break;
134 }
135 else {
136 /* The compare did major nasty stuff to the
137 * set: start over.
138 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000139 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000140 }
141 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000142 else if (entry->key == dummy && freeslot == NULL)
143 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000144 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000145 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146}
147
148/*
149 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000150 * This means we can always use _PyString_Eq directly and not have to check to
151 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152 */
153static setentry *
154set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
155{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156 register Py_ssize_t i;
157 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000159 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000160 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000161 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000162
163 /* Make sure this function doesn't have to handle non-string keys,
164 including subclasses of str; e.g., one reason to subclass
165 strings is to override __eq__, and for speed we don't cater to
166 that here. */
167 if (!PyString_CheckExact(key)) {
168 so->lookup = set_lookkey;
169 return set_lookkey(so, key, hash);
170 }
171 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000172 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000173 if (entry->key == NULL || entry->key == key)
174 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000175 if (entry->key == dummy)
176 freeslot = entry;
177 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000178 if (entry->hash == hash && _PyString_Eq(entry->key, key))
179 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000180 freeslot = NULL;
181 }
182
183 /* In the loop, key == dummy is by far (factor of 100s) the
184 least likely outcome, so test for that last. */
185 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
186 i = (i << 2) + i + perturb + 1;
187 entry = &table[i & mask];
188 if (entry->key == NULL)
189 return freeslot == NULL ? entry : freeslot;
190 if (entry->key == key
191 || (entry->hash == hash
192 && entry->key != dummy
193 && _PyString_Eq(entry->key, key)))
194 return entry;
195 if (entry->key == dummy && freeslot == NULL)
196 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000197 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000198 assert(0); /* NOT REACHED */
199 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000200}
201
202/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000203Internal routine to insert a new key into the table.
Raymond Hettinger0c850862006-12-08 04:24:33 +0000204Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000205Eats a reference to key.
206*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000207static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208set_insert_key(register PySetObject *so, PyObject *key, long hash)
209{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000210 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
212
213 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000214 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000215 if (entry == NULL)
216 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000217 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000218 /* UNUSED */
219 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220 entry->key = key;
221 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000224 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000225 entry->key = key;
226 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000227 so->used++;
228 Py_DECREF(dummy);
229 } else {
230 /* ACTIVE */
231 Py_DECREF(key);
232 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000233 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234}
235
236/*
Raymond Hettinger0c850862006-12-08 04:24:33 +0000237Internal routine used by set_table_resize() to insert an item which is
238known to be absent from the set. This routine also assumes that
239the set contains no deleted entries. Besides the performance benefit,
240using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
241Note that no refcounts are changed by this routine; if needed, the caller
242is responsible for incref'ing `key`.
243*/
244static void
245set_insert_clean(register PySetObject *so, PyObject *key, long hash)
246{
247 register size_t i;
248 register size_t perturb;
249 register size_t mask = (size_t)so->mask;
250 setentry *table = so->table;
251 register setentry *entry;
252
253 i = hash & mask;
254 entry = &table[i];
255 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
256 i = (i << 2) + i + perturb + 1;
257 entry = &table[i & mask];
258 }
259 so->fill++;
260 entry->key = key;
261 entry->hash = hash;
262 so->used++;
263}
264
265/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000267keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000268actually be smaller than the old one.
269*/
270static int
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000271set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000273 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000274 setentry *oldtable, *newtable, *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000275 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000276 int is_oldtable_malloced;
277 setentry small_copy[PySet_MINSIZE];
278
279 assert(minused >= 0);
280
281 /* Find the smallest table size > minused. */
282 for (newsize = PySet_MINSIZE;
283 newsize <= minused && newsize > 0;
284 newsize <<= 1)
285 ;
286 if (newsize <= 0) {
287 PyErr_NoMemory();
288 return -1;
289 }
290
291 /* Get space for a new table. */
292 oldtable = so->table;
293 assert(oldtable != NULL);
294 is_oldtable_malloced = oldtable != so->smalltable;
295
296 if (newsize == PySet_MINSIZE) {
297 /* A large table is shrinking, or we can't get any smaller. */
298 newtable = so->smalltable;
299 if (newtable == oldtable) {
300 if (so->fill == so->used) {
301 /* No dummies, so no point doing anything. */
302 return 0;
303 }
304 /* We're not going to resize it, but rebuild the
305 table anyway to purge old dummy entries.
306 Subtle: This is *necessary* if fill==size,
307 as set_lookkey needs at least one virgin slot to
308 terminate failing searches. If fill < size, it's
309 merely desirable, as dummies slow searches. */
310 assert(so->fill > so->used);
311 memcpy(small_copy, oldtable, sizeof(small_copy));
312 oldtable = small_copy;
313 }
314 }
315 else {
316 newtable = PyMem_NEW(setentry, newsize);
317 if (newtable == NULL) {
318 PyErr_NoMemory();
319 return -1;
320 }
321 }
322
323 /* Make the set empty, using the new table. */
324 assert(newtable != oldtable);
325 so->table = newtable;
326 so->mask = newsize - 1;
327 memset(newtable, 0, sizeof(setentry) * newsize);
328 so->used = 0;
329 i = so->fill;
330 so->fill = 0;
331
332 /* Copy the data over; this is refcount-neutral for active entries;
333 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000334 for (entry = oldtable; i > 0; entry++) {
335 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000336 /* UNUSED */
337 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000338 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000339 /* DUMMY */
340 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000341 assert(entry->key == dummy);
342 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000343 } else {
344 /* ACTIVE */
345 --i;
Raymond Hettinger0c850862006-12-08 04:24:33 +0000346 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347 }
348 }
349
350 if (is_oldtable_malloced)
351 PyMem_DEL(oldtable);
352 return 0;
353}
354
Raymond Hettingerc991db22005-08-11 07:58:45 +0000355/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
356
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000357static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000358set_add_entry(register PySetObject *so, setentry *entry)
359{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000360 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361
362 assert(so->fill <= so->mask); /* at least one empty slot */
363 n_used = so->used;
364 Py_INCREF(entry->key);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000365 if (set_insert_key(so, entry->key, entry->hash) == -1) {
366 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000367 return -1;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000368 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000369 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
370 return 0;
371 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
372}
373
374static int
375set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376{
377 register long hash;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000378 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000379
Raymond Hettingerc991db22005-08-11 07:58:45 +0000380 if (!PyString_CheckExact(key) ||
381 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382 hash = PyObject_Hash(key);
383 if (hash == -1)
384 return -1;
385 }
386 assert(so->fill <= so->mask); /* at least one empty slot */
387 n_used = so->used;
388 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000389 if (set_insert_key(so, key, hash) == -1) {
390 Py_DECREF(key);
391 return -1;
392 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000393 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
394 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000395 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000396}
397
398#define DISCARD_NOTFOUND 0
399#define DISCARD_FOUND 1
400
401static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000402set_discard_entry(PySetObject *so, setentry *oldentry)
403{ register setentry *entry;
404 PyObject *old_key;
405
406 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000407 if (entry == NULL)
408 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000409 if (entry->key == NULL || entry->key == dummy)
410 return DISCARD_NOTFOUND;
411 old_key = entry->key;
412 Py_INCREF(dummy);
413 entry->key = dummy;
414 so->used--;
415 Py_DECREF(old_key);
416 return DISCARD_FOUND;
417}
418
419static int
420set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421{
422 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000423 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424 PyObject *old_key;
425
426 assert (PyAnySet_Check(so));
427 if (!PyString_CheckExact(key) ||
428 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
429 hash = PyObject_Hash(key);
430 if (hash == -1)
431 return -1;
432 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000433 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000434 if (entry == NULL)
435 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000438 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000440 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000441 so->used--;
442 Py_DECREF(old_key);
443 return DISCARD_FOUND;
444}
445
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000446static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000447set_clear_internal(PySetObject *so)
448{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000449 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450 int table_is_malloced;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000451 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000452 setentry small_copy[PySet_MINSIZE];
453#ifdef Py_DEBUG
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000454 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000456
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457 n = so->mask + 1;
458 i = 0;
459#endif
460
461 table = so->table;
462 assert(table != NULL);
463 table_is_malloced = table != so->smalltable;
464
465 /* This is delicate. During the process of clearing the set,
466 * decrefs can cause the set to mutate. To avoid fatal confusion
467 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000468 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000469 * clearing.
470 */
471 fill = so->fill;
472 if (table_is_malloced)
473 EMPTY_TO_MINSIZE(so);
474
475 else if (fill > 0) {
476 /* It's a small table with something that needs to be cleared.
477 * Afraid the only safe way is to copy the set entries into
478 * another small table first.
479 */
480 memcpy(small_copy, table, sizeof(small_copy));
481 table = small_copy;
482 EMPTY_TO_MINSIZE(so);
483 }
484 /* else it's a small table that's already empty */
485
486 /* Now we can finally clear things. If C had refcounts, we could
487 * assert that the refcount on table is 1 now, i.e. that this function
488 * has unique access to it, so decref side-effects can't alter it.
489 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000490 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491#ifdef Py_DEBUG
492 assert(i < n);
493 ++i;
494#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000495 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000497 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498 }
499#ifdef Py_DEBUG
500 else
Raymond Hettinger334b5b22006-03-26 03:11:29 +0000501 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502#endif
503 }
504
505 if (table_is_malloced)
506 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000507 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508}
509
510/*
511 * Iterate over a set table. Use like so:
512 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000513 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000514 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000515 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000516 * while (set_next(yourset, &pos, &entry)) {
517 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000518 * }
519 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000520 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000521 * mutates the table.
522 */
523static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000526 Py_ssize_t i;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000527 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000528 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529
530 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000531 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000532 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000533 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000537 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000538 if (i > mask)
539 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000540 assert(table[i].key != NULL);
541 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000542 return 1;
543}
544
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000545static void
546set_dealloc(PySetObject *so)
547{
548 register setentry *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000549 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000550 PyObject_GC_UnTrack(so);
551 Py_TRASHCAN_SAFE_BEGIN(so)
552 if (so->weakreflist != NULL)
553 PyObject_ClearWeakRefs((PyObject *) so);
554
555 for (entry = so->table; fill > 0; entry++) {
556 if (entry->key) {
557 --fill;
558 Py_DECREF(entry->key);
559 }
560 }
561 if (so->table != so->smalltable)
562 PyMem_DEL(so->table);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000563 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
564 free_list[numfree++] = so;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000565 else
Christian Heimese93237d2007-12-19 02:37:44 +0000566 Py_TYPE(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000567 Py_TRASHCAN_SAFE_END(so)
568}
569
570static int
571set_tp_print(PySetObject *so, FILE *fp, int flags)
572{
573 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000574 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000575 char *emit = ""; /* No separator emitted on first pass */
576 char *separator = ", ";
Raymond Hettinger53999102006-12-30 04:01:17 +0000577 int status = Py_ReprEnter((PyObject*)so);
578
579 if (status != 0) {
580 if (status < 0)
581 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000582 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000583 fprintf(fp, "%s(...)", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000584 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000585 return 0;
586 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000587
Brett Cannon01531592007-09-17 03:28:34 +0000588 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000589 fprintf(fp, "%s([", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000590 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591 while (set_next(so, &pos, &entry)) {
Brett Cannon01531592007-09-17 03:28:34 +0000592 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000593 fputs(emit, fp);
Brett Cannon01531592007-09-17 03:28:34 +0000594 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000595 emit = separator;
Raymond Hettinger53999102006-12-30 04:01:17 +0000596 if (PyObject_Print(entry->key, fp, 0) != 0) {
597 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000598 return -1;
Raymond Hettinger53999102006-12-30 04:01:17 +0000599 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000600 }
Brett Cannon01531592007-09-17 03:28:34 +0000601 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000602 fputs("])", fp);
Brett Cannon01531592007-09-17 03:28:34 +0000603 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000604 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000605 return 0;
606}
607
608static PyObject *
609set_repr(PySetObject *so)
610{
Raymond Hettinger53999102006-12-30 04:01:17 +0000611 PyObject *keys, *result=NULL, *listrepr;
612 int status = Py_ReprEnter((PyObject*)so);
613
614 if (status != 0) {
615 if (status < 0)
616 return NULL;
617 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
618 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000619
620 keys = PySequence_List((PyObject *)so);
621 if (keys == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000622 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000623 listrepr = PyObject_Repr(keys);
624 Py_DECREF(keys);
625 if (listrepr == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000626 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000627
628 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
629 PyString_AS_STRING(listrepr));
630 Py_DECREF(listrepr);
Raymond Hettinger53999102006-12-30 04:01:17 +0000631done:
632 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000633 return result;
634}
635
Martin v. Löwis18e16552006-02-15 17:27:45 +0000636static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000637set_len(PyObject *so)
638{
639 return ((PySetObject *)so)->used;
640}
641
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000642static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000643set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000644{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000645 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000646 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000647 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648
649 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000650 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000651
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000652 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000653 if (other == so || other->used == 0)
654 /* a.update(a) or a.update({}); nothing to do */
655 return 0;
656 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000657 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000658 * that there will be no (or few) overlapping keys.
659 */
660 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
661 if (set_table_resize(so, (so->used + other->used)*2) != 0)
662 return -1;
663 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000664 for (i = 0; i <= other->mask; i++) {
665 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000666 if (entry->key != NULL &&
667 entry->key != dummy) {
668 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000669 if (set_insert_key(so, entry->key, entry->hash) == -1) {
670 Py_DECREF(entry->key);
671 return -1;
672 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673 }
674 }
675 return 0;
676}
677
678static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000679set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000680{
681 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000682 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000683
684 if (!PyString_CheckExact(key) ||
685 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
686 hash = PyObject_Hash(key);
687 if (hash == -1)
688 return -1;
689 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000690 entry = (so->lookup)(so, key, hash);
691 if (entry == NULL)
692 return -1;
693 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000694 return key != NULL && key != dummy;
695}
696
Raymond Hettingerc991db22005-08-11 07:58:45 +0000697static int
698set_contains_entry(PySetObject *so, setentry *entry)
699{
700 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000701 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000702
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000703 lu_entry = (so->lookup)(so, entry->key, entry->hash);
704 if (lu_entry == NULL)
705 return -1;
706 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000707 return key != NULL && key != dummy;
708}
709
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000710static PyObject *
711set_pop(PySetObject *so)
712{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000713 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000714 register setentry *entry;
715 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000716
717 assert (PyAnySet_Check(so));
718 if (so->used == 0) {
719 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
720 return NULL;
721 }
722
723 /* Set entry to "the first" unused or dummy set entry. We abuse
724 * the hash field of slot 0 to hold a search finger:
725 * If slot 0 has a value, use slot 0.
726 * Else slot 0 is being used to hold a search finger,
727 * and we use its hash value as the first index to look.
728 */
729 entry = &so->table[0];
730 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000731 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000732 /* The hash field may be a real hash value, or it may be a
733 * legit search finger, or it may be a once-legit search
734 * finger that's out of bounds now because it wrapped around
735 * or the table shrunk -- simply make sure it's in bounds now.
736 */
737 if (i > so->mask || i < 1)
738 i = 1; /* skip slot 0 */
739 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
740 i++;
741 if (i > so->mask)
742 i = 1;
743 }
744 }
745 key = entry->key;
746 Py_INCREF(dummy);
747 entry->key = dummy;
748 so->used--;
749 so->table[0].hash = i + 1; /* next place to start */
750 return key;
751}
752
753PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
754
755static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000756set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000757{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000758 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000759 setentry *entry;
760
761 while (set_next(so, &pos, &entry))
762 Py_VISIT(entry->key);
763 return 0;
764}
765
766static long
767frozenset_hash(PyObject *self)
768{
769 PySetObject *so = (PySetObject *)self;
770 long h, hash = 1927868237L;
771 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000772 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000773
774 if (so->hash != -1)
775 return so->hash;
776
777 hash *= PySet_GET_SIZE(self) + 1;
778 while (set_next(so, &pos, &entry)) {
779 /* Work to increase the bit dispersion for closely spaced hash
780 values. The is important because some use cases have many
781 combinations of a small number of elements with nearby
782 hashes so that many distinct combinations collapse to only
783 a handful of distinct hash values. */
784 h = entry->hash;
785 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
786 }
787 hash = hash * 69069L + 907133923L;
788 if (hash == -1)
789 hash = 590923713L;
790 so->hash = hash;
791 return hash;
792}
793
Raymond Hettingera9d99362005-08-05 00:01:15 +0000794/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000795
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796typedef struct {
797 PyObject_HEAD
798 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000799 Py_ssize_t si_used;
800 Py_ssize_t si_pos;
801 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802} setiterobject;
803
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804static void
805setiter_dealloc(setiterobject *si)
806{
807 Py_XDECREF(si->si_set);
808 PyObject_Del(si);
809}
810
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000811static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812setiter_len(setiterobject *si)
813{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000814 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000816 len = si->len;
817 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818}
819
Armin Rigof5b3e362006-02-11 21:32:43 +0000820PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000821
822static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000823 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000824 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825};
826
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000827static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828{
829 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000830 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000831 register setentry *entry;
832 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000834 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000836 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000838 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839 PyErr_SetString(PyExc_RuntimeError,
840 "Set changed size during iteration");
841 si->si_used = -1; /* Make this state sticky */
842 return NULL;
843 }
844
845 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000846 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000847 entry = so->table;
848 mask = so->mask;
849 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850 i++;
851 si->si_pos = i+1;
852 if (i > mask)
853 goto fail;
854 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000855 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856 Py_INCREF(key);
857 return key;
858
859fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000860 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861 si->si_set = NULL;
862 return NULL;
863}
864
Hye-Shik Change2956762005-08-01 05:26:41 +0000865static PyTypeObject PySetIter_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000866 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000867 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868 sizeof(setiterobject), /* tp_basicsize */
869 0, /* tp_itemsize */
870 /* methods */
871 (destructor)setiter_dealloc, /* tp_dealloc */
872 0, /* tp_print */
873 0, /* tp_getattr */
874 0, /* tp_setattr */
875 0, /* tp_compare */
876 0, /* tp_repr */
877 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000878 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000879 0, /* tp_as_mapping */
880 0, /* tp_hash */
881 0, /* tp_call */
882 0, /* tp_str */
883 PyObject_GenericGetAttr, /* tp_getattro */
884 0, /* tp_setattro */
885 0, /* tp_as_buffer */
886 Py_TPFLAGS_DEFAULT, /* tp_flags */
887 0, /* tp_doc */
888 0, /* tp_traverse */
889 0, /* tp_clear */
890 0, /* tp_richcompare */
891 0, /* tp_weaklistoffset */
892 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000893 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000894 setiter_methods, /* tp_methods */
895 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000896};
897
Martin v. Löwis72d20672006-04-11 09:04:12 +0000898static PyObject *
899set_iter(PySetObject *so)
900{
901 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
902 if (si == NULL)
903 return NULL;
904 Py_INCREF(so);
905 si->si_set = so;
906 si->si_used = so->used;
907 si->si_pos = 0;
908 si->len = so->used;
909 return (PyObject *)si;
910}
911
Raymond Hettingerd7946662005-08-01 21:39:29 +0000912static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000913set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000914{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000915 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000916
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +0000917 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000918 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000919
Raymond Hettingerdb67aef2007-02-01 21:02:59 +0000920 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000921 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000922 Py_ssize_t pos = 0;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000923 long hash;
Raymond Hettinger15cade02007-02-19 20:44:04 +0000924 Py_ssize_t dictsize = PyDict_Size(other);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000925
Raymond Hettinger15cade02007-02-19 20:44:04 +0000926 /* Do one big resize at the start, rather than
927 * incrementally resizing as we insert new keys. Expect
928 * that there will be no (or few) overlapping keys.
929 */
930 if (dictsize == -1)
931 return -1;
932 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
933 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
934 return -1;
935 }
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000936 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
937 setentry an_entry;
938
939 an_entry.hash = hash;
940 an_entry.key = key;
941 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000943 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000944 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000945 }
946
Raymond Hettingera38123e2003-11-24 22:18:49 +0000947 it = PyObject_GetIter(other);
948 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000950
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000951 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000952 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000953 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000954 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000955 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000956 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000957 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000958 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000959 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000960 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000961 return -1;
962 return 0;
963}
964
965static PyObject *
966set_update(PySetObject *so, PyObject *other)
967{
968 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000969 return NULL;
970 Py_RETURN_NONE;
971}
972
973PyDoc_STRVAR(update_doc,
974"Update a set with the union of itself and another.");
975
976static PyObject *
977make_new_set(PyTypeObject *type, PyObject *iterable)
978{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000979 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000980
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000981 if (dummy == NULL) { /* Auto-initialize dummy */
982 dummy = PyString_FromString("<dummy key>");
983 if (dummy == NULL)
984 return NULL;
985 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000986
987 /* create PySetObject structure */
Christian Heimes5b970ad2008-02-06 13:33:44 +0000988 if (numfree &&
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000989 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
Christian Heimes5b970ad2008-02-06 13:33:44 +0000990 so = free_list[--numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000991 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimese93237d2007-12-19 02:37:44 +0000992 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000993 _Py_NewReference((PyObject *)so);
994 EMPTY_TO_MINSIZE(so);
995 PyObject_GC_Track(so);
996 } else {
997 so = (PySetObject *)type->tp_alloc(type, 0);
998 if (so == NULL)
999 return NULL;
1000 /* tp_alloc has already zeroed the structure */
1001 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1002 INIT_NONZERO_SET_SLOTS(so);
1003 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001004
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001005 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001006 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001007
Raymond Hettingera38123e2003-11-24 22:18:49 +00001008 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001009 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001010 Py_DECREF(so);
1011 return NULL;
1012 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001013 }
1014
Raymond Hettingera690a992003-11-16 16:17:49 +00001015 return (PyObject *)so;
1016}
1017
Raymond Hettingerd7946662005-08-01 21:39:29 +00001018/* The empty frozenset is a singleton */
1019static PyObject *emptyfrozenset = NULL;
1020
Raymond Hettingera690a992003-11-16 16:17:49 +00001021static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001022frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001023{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001024 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001025
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001026 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001027 return NULL;
1028
Raymond Hettingera690a992003-11-16 16:17:49 +00001029 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1030 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001031
1032 if (type != &PyFrozenSet_Type)
1033 return make_new_set(type, iterable);
1034
1035 if (iterable != NULL) {
1036 /* frozenset(f) is idempotent */
1037 if (PyFrozenSet_CheckExact(iterable)) {
1038 Py_INCREF(iterable);
1039 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001040 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001041 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001042 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001043 return result;
1044 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001045 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001046 /* The empty frozenset is a singleton */
1047 if (emptyfrozenset == NULL)
1048 emptyfrozenset = make_new_set(type, NULL);
1049 Py_XINCREF(emptyfrozenset);
1050 return emptyfrozenset;
1051}
1052
1053void
1054PySet_Fini(void)
1055{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001056 PySetObject *so;
1057
Christian Heimes5b970ad2008-02-06 13:33:44 +00001058 while (numfree) {
1059 numfree--;
1060 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001061 PyObject_GC_Del(so);
1062 }
Martin v. Löwised8f7832006-04-15 12:47:23 +00001063 Py_CLEAR(dummy);
1064 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001065}
1066
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001067static PyObject *
1068set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1069{
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001070 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001071 return NULL;
1072
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001073 return make_new_set(type, NULL);
1074}
1075
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001076/* set_swap_bodies() switches the contents of any two sets by moving their
1077 internal data pointers and, if needed, copying the internal smalltables.
1078 Semantically equivalent to:
1079
1080 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1081
1082 The function always succeeds and it leaves both objects in a stable state.
1083 Useful for creating temporary frozensets from sets for membership testing
1084 in __contains__(), discard(), and remove(). Also useful for operations
1085 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001086 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001087*/
1088
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001089static void
1090set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001091{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001092 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001093 setentry *u;
1094 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1095 setentry tab[PySet_MINSIZE];
1096 long h;
1097
1098 t = a->fill; a->fill = b->fill; b->fill = t;
1099 t = a->used; a->used = b->used; b->used = t;
1100 t = a->mask; a->mask = b->mask; b->mask = t;
1101
1102 u = a->table;
1103 if (a->table == a->smalltable)
1104 u = b->smalltable;
1105 a->table = b->table;
1106 if (b->table == b->smalltable)
1107 a->table = a->smalltable;
1108 b->table = u;
1109
1110 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1111
1112 if (a->table == a->smalltable || b->table == b->smalltable) {
1113 memcpy(tab, a->smalltable, sizeof(tab));
1114 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1115 memcpy(b->smalltable, tab, sizeof(tab));
1116 }
1117
Christian Heimese93237d2007-12-19 02:37:44 +00001118 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1119 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001120 h = a->hash; a->hash = b->hash; b->hash = h;
1121 } else {
1122 a->hash = -1;
1123 b->hash = -1;
1124 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001125}
1126
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001127static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001128set_copy(PySetObject *so)
1129{
Christian Heimese93237d2007-12-19 02:37:44 +00001130 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001131}
1132
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001133static PyObject *
1134frozenset_copy(PySetObject *so)
1135{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001136 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001137 Py_INCREF(so);
1138 return (PyObject *)so;
1139 }
1140 return set_copy(so);
1141}
1142
Raymond Hettingera690a992003-11-16 16:17:49 +00001143PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1144
1145static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001146set_clear(PySetObject *so)
1147{
1148 set_clear_internal(so);
1149 Py_RETURN_NONE;
1150}
1151
1152PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1153
1154static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001155set_union(PySetObject *so, PyObject *other)
1156{
1157 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001158
1159 result = (PySetObject *)set_copy(so);
1160 if (result == NULL)
1161 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001162 if ((PyObject *)so == other)
1163 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001164 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001165 Py_DECREF(result);
1166 return NULL;
1167 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001168 return (PyObject *)result;
1169}
1170
1171PyDoc_STRVAR(union_doc,
1172 "Return the union of two sets as a new set.\n\
1173\n\
1174(i.e. all elements that are in either set.)");
1175
1176static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001177set_or(PySetObject *so, PyObject *other)
1178{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001179 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001180 Py_INCREF(Py_NotImplemented);
1181 return Py_NotImplemented;
1182 }
1183 return set_union(so, other);
1184}
1185
1186static PyObject *
1187set_ior(PySetObject *so, PyObject *other)
1188{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001189 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001190 Py_INCREF(Py_NotImplemented);
1191 return Py_NotImplemented;
1192 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001193 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001194 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001195 Py_INCREF(so);
1196 return (PyObject *)so;
1197}
1198
1199static PyObject *
1200set_intersection(PySetObject *so, PyObject *other)
1201{
1202 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001203 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001204
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001205 if ((PyObject *)so == other)
1206 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001207
Christian Heimese93237d2007-12-19 02:37:44 +00001208 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001209 if (result == NULL)
1210 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001211
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001212 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001213 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001214 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001215
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001216 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001217 tmp = (PyObject *)so;
1218 so = (PySetObject *)other;
1219 other = tmp;
1220 }
1221
Raymond Hettingerc991db22005-08-11 07:58:45 +00001222 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001223 int rv = set_contains_entry(so, entry);
1224 if (rv == -1) {
1225 Py_DECREF(result);
1226 return NULL;
1227 }
1228 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001229 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001230 Py_DECREF(result);
1231 return NULL;
1232 }
1233 }
1234 }
1235 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001236 }
1237
Raymond Hettingera690a992003-11-16 16:17:49 +00001238 it = PyObject_GetIter(other);
1239 if (it == NULL) {
1240 Py_DECREF(result);
1241 return NULL;
1242 }
1243
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001244 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001245 int rv;
1246 setentry entry;
1247 long hash = PyObject_Hash(key);
1248
1249 if (hash == -1) {
1250 Py_DECREF(it);
1251 Py_DECREF(result);
1252 Py_DECREF(key);
1253 return NULL;
1254 }
1255 entry.hash = hash;
1256 entry.key = key;
1257 rv = set_contains_entry(so, &entry);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001258 if (rv == -1) {
1259 Py_DECREF(it);
1260 Py_DECREF(result);
1261 Py_DECREF(key);
1262 return NULL;
1263 }
1264 if (rv) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001265 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001266 Py_DECREF(it);
1267 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001268 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001269 return NULL;
1270 }
1271 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001272 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001273 }
1274 Py_DECREF(it);
1275 if (PyErr_Occurred()) {
1276 Py_DECREF(result);
1277 return NULL;
1278 }
1279 return (PyObject *)result;
1280}
1281
1282PyDoc_STRVAR(intersection_doc,
1283"Return the intersection of two sets as a new set.\n\
1284\n\
1285(i.e. all elements that are in both sets.)");
1286
1287static PyObject *
1288set_intersection_update(PySetObject *so, PyObject *other)
1289{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001290 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001291
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001292 tmp = set_intersection(so, other);
1293 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001294 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001295 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001296 Py_DECREF(tmp);
1297 Py_RETURN_NONE;
1298}
1299
1300PyDoc_STRVAR(intersection_update_doc,
1301"Update a set with the intersection of itself and another.");
1302
1303static PyObject *
1304set_and(PySetObject *so, PyObject *other)
1305{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001306 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001307 Py_INCREF(Py_NotImplemented);
1308 return Py_NotImplemented;
1309 }
1310 return set_intersection(so, other);
1311}
1312
1313static PyObject *
1314set_iand(PySetObject *so, PyObject *other)
1315{
1316 PyObject *result;
1317
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001318 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001319 Py_INCREF(Py_NotImplemented);
1320 return Py_NotImplemented;
1321 }
1322 result = set_intersection_update(so, other);
1323 if (result == NULL)
1324 return NULL;
1325 Py_DECREF(result);
1326 Py_INCREF(so);
1327 return (PyObject *)so;
1328}
1329
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001330static PyObject *
1331set_isdisjoint(PySetObject *so, PyObject *other)
1332{
1333 PyObject *key, *it, *tmp;
1334
1335 if ((PyObject *)so == other) {
1336 if (PySet_GET_SIZE(so) == 0)
1337 Py_RETURN_TRUE;
1338 else
1339 Py_RETURN_FALSE;
1340 }
1341
1342 if (PyAnySet_CheckExact(other)) {
1343 Py_ssize_t pos = 0;
1344 setentry *entry;
1345
1346 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1347 tmp = (PyObject *)so;
1348 so = (PySetObject *)other;
1349 other = tmp;
1350 }
1351 while (set_next((PySetObject *)other, &pos, &entry)) {
1352 int rv = set_contains_entry(so, entry);
1353 if (rv == -1)
1354 return NULL;
1355 if (rv)
1356 Py_RETURN_FALSE;
1357 }
1358 Py_RETURN_TRUE;
1359 }
1360
1361 it = PyObject_GetIter(other);
1362 if (it == NULL)
1363 return NULL;
1364
1365 while ((key = PyIter_Next(it)) != NULL) {
1366 int rv;
1367 setentry entry;
1368 long hash = PyObject_Hash(key);
1369
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001370 if (hash == -1) {
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001371 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001372 Py_DECREF(it);
1373 return NULL;
1374 }
1375 entry.hash = hash;
1376 entry.key = key;
1377 rv = set_contains_entry(so, &entry);
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001378 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001379 if (rv == -1) {
1380 Py_DECREF(it);
1381 return NULL;
1382 }
1383 if (rv) {
1384 Py_DECREF(it);
1385 Py_RETURN_FALSE;
1386 }
1387 }
1388 Py_DECREF(it);
1389 if (PyErr_Occurred())
1390 return NULL;
1391 Py_RETURN_TRUE;
1392}
1393
1394PyDoc_STRVAR(isdisjoint_doc,
1395"Return True if two sets have a null intersection.");
1396
Neal Norwitz6576bd82005-11-13 18:41:28 +00001397static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001398set_difference_update_internal(PySetObject *so, PyObject *other)
1399{
1400 if ((PyObject *)so == other)
1401 return set_clear_internal(so);
1402
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001403 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001404 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001405 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001406
1407 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001408 if (set_discard_entry(so, entry) == -1)
1409 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001410 } else {
1411 PyObject *key, *it;
1412 it = PyObject_GetIter(other);
1413 if (it == NULL)
1414 return -1;
1415
1416 while ((key = PyIter_Next(it)) != NULL) {
1417 if (set_discard_key(so, key) == -1) {
1418 Py_DECREF(it);
1419 Py_DECREF(key);
1420 return -1;
1421 }
1422 Py_DECREF(key);
1423 }
1424 Py_DECREF(it);
1425 if (PyErr_Occurred())
1426 return -1;
1427 }
1428 /* If more than 1/5 are dummies, then resize them away. */
1429 if ((so->fill - so->used) * 5 < so->mask)
1430 return 0;
1431 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1432}
1433
Raymond Hettingera690a992003-11-16 16:17:49 +00001434static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001435set_difference_update(PySetObject *so, PyObject *other)
1436{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001437 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001438 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001439 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001440}
1441
1442PyDoc_STRVAR(difference_update_doc,
1443"Remove all elements of another set from this set.");
1444
1445static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001446set_difference(PySetObject *so, PyObject *other)
1447{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001448 PyObject *result;
1449 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001450 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001451
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001452 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001453 result = set_copy(so);
1454 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001455 return NULL;
1456 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001457 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001458 Py_DECREF(result);
1459 return NULL;
1460 }
1461
Christian Heimese93237d2007-12-19 02:37:44 +00001462 result = make_new_set(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001463 if (result == NULL)
1464 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001465
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001466 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001467 while (set_next(so, &pos, &entry)) {
1468 setentry entrycopy;
1469 entrycopy.hash = entry->hash;
1470 entrycopy.key = entry->key;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001471 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001472 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1473 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001474 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001475 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001476 }
1477 }
1478 return result;
1479 }
1480
Raymond Hettingerc991db22005-08-11 07:58:45 +00001481 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001482 int rv = set_contains_entry((PySetObject *)other, entry);
1483 if (rv == -1) {
1484 Py_DECREF(result);
1485 return NULL;
1486 }
1487 if (!rv) {
1488 if (set_add_entry((PySetObject *)result, entry) == -1) {
1489 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001490 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001491 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001492 }
1493 }
1494 return result;
1495}
1496
1497PyDoc_STRVAR(difference_doc,
1498"Return the difference of two sets as a new set.\n\
1499\n\
1500(i.e. all elements that are in this set but not the other.)");
1501static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001502set_sub(PySetObject *so, PyObject *other)
1503{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001504 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001505 Py_INCREF(Py_NotImplemented);
1506 return Py_NotImplemented;
1507 }
1508 return set_difference(so, other);
1509}
1510
1511static PyObject *
1512set_isub(PySetObject *so, PyObject *other)
1513{
1514 PyObject *result;
1515
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001516 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001517 Py_INCREF(Py_NotImplemented);
1518 return Py_NotImplemented;
1519 }
1520 result = set_difference_update(so, other);
1521 if (result == NULL)
1522 return NULL;
1523 Py_DECREF(result);
1524 Py_INCREF(so);
1525 return (PyObject *)so;
1526}
1527
1528static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001529set_symmetric_difference_update(PySetObject *so, PyObject *other)
1530{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001531 PySetObject *otherset;
1532 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001533 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001534 setentry *entry;
1535
1536 if ((PyObject *)so == other)
1537 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001538
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001539 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001540 PyObject *value;
1541 int rv;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001542 long hash;
1543 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001544 setentry an_entry;
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001545
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001546 an_entry.hash = hash;
1547 an_entry.key = key;
1548 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001549 if (rv == -1)
1550 return NULL;
1551 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001552 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001553 return NULL;
1554 }
1555 }
1556 Py_RETURN_NONE;
1557 }
1558
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001559 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001560 Py_INCREF(other);
1561 otherset = (PySetObject *)other;
1562 } else {
Christian Heimese93237d2007-12-19 02:37:44 +00001563 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001564 if (otherset == NULL)
1565 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001566 }
1567
Raymond Hettingerc991db22005-08-11 07:58:45 +00001568 while (set_next(otherset, &pos, &entry)) {
1569 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001570 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001571 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001572 return NULL;
1573 }
1574 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001575 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001576 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001577 return NULL;
1578 }
1579 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001580 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001581 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001582 Py_RETURN_NONE;
1583}
1584
1585PyDoc_STRVAR(symmetric_difference_update_doc,
1586"Update a set with the symmetric difference of itself and another.");
1587
1588static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001589set_symmetric_difference(PySetObject *so, PyObject *other)
1590{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001591 PyObject *rv;
1592 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001593
Christian Heimese93237d2007-12-19 02:37:44 +00001594 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001595 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001596 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001597 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1598 if (rv == NULL)
1599 return NULL;
1600 Py_DECREF(rv);
1601 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001602}
1603
1604PyDoc_STRVAR(symmetric_difference_doc,
1605"Return the symmetric difference of two sets as a new set.\n\
1606\n\
1607(i.e. all elements that are in exactly one of the sets.)");
1608
1609static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001610set_xor(PySetObject *so, PyObject *other)
1611{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001612 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001613 Py_INCREF(Py_NotImplemented);
1614 return Py_NotImplemented;
1615 }
1616 return set_symmetric_difference(so, other);
1617}
1618
1619static PyObject *
1620set_ixor(PySetObject *so, PyObject *other)
1621{
1622 PyObject *result;
1623
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001624 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001625 Py_INCREF(Py_NotImplemented);
1626 return Py_NotImplemented;
1627 }
1628 result = set_symmetric_difference_update(so, other);
1629 if (result == NULL)
1630 return NULL;
1631 Py_DECREF(result);
1632 Py_INCREF(so);
1633 return (PyObject *)so;
1634}
1635
1636static PyObject *
1637set_issubset(PySetObject *so, PyObject *other)
1638{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001639 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001640 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001641
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001642 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001643 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001644 tmp = make_new_set(&PySet_Type, other);
1645 if (tmp == NULL)
1646 return NULL;
1647 result = set_issubset(so, tmp);
1648 Py_DECREF(tmp);
1649 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001650 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001651 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001652 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001653
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001654 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001655 int rv = set_contains_entry((PySetObject *)other, entry);
1656 if (rv == -1)
1657 return NULL;
1658 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001659 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001660 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001661 Py_RETURN_TRUE;
1662}
1663
1664PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1665
1666static PyObject *
1667set_issuperset(PySetObject *so, PyObject *other)
1668{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001669 PyObject *tmp, *result;
1670
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001671 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001672 tmp = make_new_set(&PySet_Type, other);
1673 if (tmp == NULL)
1674 return NULL;
1675 result = set_issuperset(so, tmp);
1676 Py_DECREF(tmp);
1677 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001678 }
1679 return set_issubset((PySetObject *)other, (PyObject *)so);
1680}
1681
1682PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1683
Raymond Hettingera690a992003-11-16 16:17:49 +00001684static PyObject *
1685set_richcompare(PySetObject *v, PyObject *w, int op)
1686{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001687 PyObject *r1, *r2;
1688
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001689 if(!PyAnySet_Check(w)) {
1690 if (op == Py_EQ)
1691 Py_RETURN_FALSE;
1692 if (op == Py_NE)
1693 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001694 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1695 return NULL;
1696 }
1697 switch (op) {
1698 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001699 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001700 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001701 if (v->hash != -1 &&
1702 ((PySetObject *)w)->hash != -1 &&
1703 v->hash != ((PySetObject *)w)->hash)
1704 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001705 return set_issubset(v, w);
1706 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001707 r1 = set_richcompare(v, w, Py_EQ);
1708 if (r1 == NULL)
1709 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001710 r2 = PyBool_FromLong(PyObject_Not(r1));
1711 Py_DECREF(r1);
1712 return r2;
1713 case Py_LE:
1714 return set_issubset(v, w);
1715 case Py_GE:
1716 return set_issuperset(v, w);
1717 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001718 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001719 Py_RETURN_FALSE;
1720 return set_issubset(v, w);
1721 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001722 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001723 Py_RETURN_FALSE;
1724 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001725 }
1726 Py_INCREF(Py_NotImplemented);
1727 return Py_NotImplemented;
1728}
1729
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001730static int
Georg Brandl347b3002006-03-30 11:57:00 +00001731set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001732{
1733 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1734 return -1;
1735}
1736
Raymond Hettingera690a992003-11-16 16:17:49 +00001737static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001738set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001739{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001740 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001741 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001742 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001743}
1744
1745PyDoc_STRVAR(add_doc,
1746"Add an element to a set.\n\
1747\n\
1748This has no effect if the element is already present.");
1749
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001750static int
1751set_contains(PySetObject *so, PyObject *key)
1752{
1753 PyObject *tmpkey;
1754 int rv;
1755
1756 rv = set_contains_key(so, key);
1757 if (rv == -1) {
Raymond Hettingerc5a1cc52008-05-08 04:35:20 +00001758 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001759 return -1;
1760 PyErr_Clear();
1761 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1762 if (tmpkey == NULL)
1763 return -1;
1764 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1765 rv = set_contains(so, tmpkey);
1766 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1767 Py_DECREF(tmpkey);
1768 }
1769 return rv;
1770}
1771
1772static PyObject *
1773set_direct_contains(PySetObject *so, PyObject *key)
1774{
1775 long result;
1776
1777 result = set_contains(so, key);
1778 if (result == -1)
1779 return NULL;
1780 return PyBool_FromLong(result);
1781}
1782
1783PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1784
Raymond Hettingera690a992003-11-16 16:17:49 +00001785static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001786set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001787{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001788 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001789 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001790
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001791 rv = set_discard_key(so, key);
1792 if (rv == -1) {
Raymond Hettingerc5a1cc52008-05-08 04:35:20 +00001793 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001794 return NULL;
1795 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001796 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1797 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001798 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001799 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001800 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001801 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001802 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001803 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001804 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +00001805 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001806 return NULL;
1807 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001808 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001809}
1810
1811PyDoc_STRVAR(remove_doc,
1812"Remove an element from a set; it must be a member.\n\
1813\n\
1814If the element is not a member, raise a KeyError.");
1815
1816static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001817set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001818{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001819 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001820 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001821
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001822 rv = set_discard_key(so, key);
1823 if (rv == -1) {
Raymond Hettingerc5a1cc52008-05-08 04:35:20 +00001824 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001825 return NULL;
1826 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001827 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1828 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001829 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001830 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001831 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001832 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001833 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001834 return result;
1835 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001836 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001837}
1838
1839PyDoc_STRVAR(discard_doc,
1840"Remove an element from a set if it is a member.\n\
1841\n\
1842If the element is not a member, do nothing.");
1843
1844static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001845set_reduce(PySetObject *so)
1846{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001847 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001848
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001849 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001850 if (keys == NULL)
1851 goto done;
1852 args = PyTuple_Pack(1, keys);
1853 if (args == NULL)
1854 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001855 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1856 if (dict == NULL) {
1857 PyErr_Clear();
1858 dict = Py_None;
1859 Py_INCREF(dict);
1860 }
Christian Heimese93237d2007-12-19 02:37:44 +00001861 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001862done:
1863 Py_XDECREF(args);
1864 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001865 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001866 return result;
1867}
1868
1869PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1870
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001871static int
1872set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1873{
1874 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001875
1876 if (!PyAnySet_Check(self))
1877 return -1;
Christian Heimese93237d2007-12-19 02:37:44 +00001878 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001879 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001880 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001881 self->hash = -1;
1882 if (iterable == NULL)
1883 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001884 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001885}
1886
Raymond Hettingera690a992003-11-16 16:17:49 +00001887static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001888 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001889 0, /* sq_concat */
1890 0, /* sq_repeat */
1891 0, /* sq_item */
1892 0, /* sq_slice */
1893 0, /* sq_ass_item */
1894 0, /* sq_ass_slice */
1895 (objobjproc)set_contains, /* sq_contains */
1896};
1897
1898/* set object ********************************************************/
1899
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001900#ifdef Py_DEBUG
1901static PyObject *test_c_api(PySetObject *so);
1902
1903PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1904All is well if assertions don't fail.");
1905#endif
1906
Raymond Hettingera690a992003-11-16 16:17:49 +00001907static PyMethodDef set_methods[] = {
1908 {"add", (PyCFunction)set_add, METH_O,
1909 add_doc},
1910 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1911 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001912 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001913 contains_doc},
Raymond Hettingera37430a2008-02-12 19:05:36 +00001914 {"copy", (PyCFunction)set_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001915 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001916 {"discard", (PyCFunction)set_discard, METH_O,
1917 discard_doc},
1918 {"difference", (PyCFunction)set_difference, METH_O,
1919 difference_doc},
1920 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1921 difference_update_doc},
1922 {"intersection",(PyCFunction)set_intersection, METH_O,
1923 intersection_doc},
1924 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1925 intersection_update_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001926 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1927 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001928 {"issubset", (PyCFunction)set_issubset, METH_O,
1929 issubset_doc},
1930 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1931 issuperset_doc},
1932 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1933 pop_doc},
1934 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1935 reduce_doc},
1936 {"remove", (PyCFunction)set_remove, METH_O,
1937 remove_doc},
1938 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1939 symmetric_difference_doc},
1940 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1941 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001942#ifdef Py_DEBUG
1943 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1944 test_c_api_doc},
1945#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001946 {"union", (PyCFunction)set_union, METH_O,
1947 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001948 {"update", (PyCFunction)set_update, METH_O,
1949 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001950 {NULL, NULL} /* sentinel */
1951};
1952
1953static PyNumberMethods set_as_number = {
1954 0, /*nb_add*/
1955 (binaryfunc)set_sub, /*nb_subtract*/
1956 0, /*nb_multiply*/
1957 0, /*nb_divide*/
1958 0, /*nb_remainder*/
1959 0, /*nb_divmod*/
1960 0, /*nb_power*/
1961 0, /*nb_negative*/
1962 0, /*nb_positive*/
1963 0, /*nb_absolute*/
1964 0, /*nb_nonzero*/
1965 0, /*nb_invert*/
1966 0, /*nb_lshift*/
1967 0, /*nb_rshift*/
1968 (binaryfunc)set_and, /*nb_and*/
1969 (binaryfunc)set_xor, /*nb_xor*/
1970 (binaryfunc)set_or, /*nb_or*/
1971 0, /*nb_coerce*/
1972 0, /*nb_int*/
1973 0, /*nb_long*/
1974 0, /*nb_float*/
1975 0, /*nb_oct*/
1976 0, /*nb_hex*/
1977 0, /*nb_inplace_add*/
1978 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1979 0, /*nb_inplace_multiply*/
1980 0, /*nb_inplace_divide*/
1981 0, /*nb_inplace_remainder*/
1982 0, /*nb_inplace_power*/
1983 0, /*nb_inplace_lshift*/
1984 0, /*nb_inplace_rshift*/
1985 (binaryfunc)set_iand, /*nb_inplace_and*/
1986 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1987 (binaryfunc)set_ior, /*nb_inplace_or*/
1988};
1989
1990PyDoc_STRVAR(set_doc,
1991"set(iterable) --> set object\n\
1992\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001993Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001994
1995PyTypeObject PySet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001996 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00001997 "set", /* tp_name */
1998 sizeof(PySetObject), /* tp_basicsize */
1999 0, /* tp_itemsize */
2000 /* methods */
2001 (destructor)set_dealloc, /* tp_dealloc */
2002 (printfunc)set_tp_print, /* tp_print */
2003 0, /* tp_getattr */
2004 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002005 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002006 (reprfunc)set_repr, /* tp_repr */
2007 &set_as_number, /* tp_as_number */
2008 &set_as_sequence, /* tp_as_sequence */
2009 0, /* tp_as_mapping */
Guido van Rossum64c06e32007-11-22 00:55:51 +00002010 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00002011 0, /* tp_call */
2012 0, /* tp_str */
2013 PyObject_GenericGetAttr, /* tp_getattro */
2014 0, /* tp_setattro */
2015 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002016 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002017 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002018 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002019 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002020 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002021 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002022 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002023 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002024 0, /* tp_iternext */
2025 set_methods, /* tp_methods */
2026 0, /* tp_members */
2027 0, /* tp_getset */
2028 0, /* tp_base */
2029 0, /* tp_dict */
2030 0, /* tp_descr_get */
2031 0, /* tp_descr_set */
2032 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002033 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002034 PyType_GenericAlloc, /* tp_alloc */
2035 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002036 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002037};
2038
2039/* frozenset object ********************************************************/
2040
2041
2042static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002043 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002044 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002045 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002046 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002047 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002048 difference_doc},
2049 {"intersection",(PyCFunction)set_intersection, METH_O,
2050 intersection_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00002051 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2052 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002053 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002054 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002055 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002056 issuperset_doc},
2057 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2058 reduce_doc},
2059 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2060 symmetric_difference_doc},
2061 {"union", (PyCFunction)set_union, METH_O,
2062 union_doc},
2063 {NULL, NULL} /* sentinel */
2064};
2065
2066static PyNumberMethods frozenset_as_number = {
2067 0, /*nb_add*/
2068 (binaryfunc)set_sub, /*nb_subtract*/
2069 0, /*nb_multiply*/
2070 0, /*nb_divide*/
2071 0, /*nb_remainder*/
2072 0, /*nb_divmod*/
2073 0, /*nb_power*/
2074 0, /*nb_negative*/
2075 0, /*nb_positive*/
2076 0, /*nb_absolute*/
2077 0, /*nb_nonzero*/
2078 0, /*nb_invert*/
2079 0, /*nb_lshift*/
2080 0, /*nb_rshift*/
2081 (binaryfunc)set_and, /*nb_and*/
2082 (binaryfunc)set_xor, /*nb_xor*/
2083 (binaryfunc)set_or, /*nb_or*/
2084};
2085
2086PyDoc_STRVAR(frozenset_doc,
2087"frozenset(iterable) --> frozenset object\n\
2088\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002089Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002090
2091PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002092 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002093 "frozenset", /* tp_name */
2094 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002095 0, /* tp_itemsize */
2096 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002097 (destructor)set_dealloc, /* tp_dealloc */
2098 (printfunc)set_tp_print, /* tp_print */
2099 0, /* tp_getattr */
2100 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002101 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002102 (reprfunc)set_repr, /* tp_repr */
2103 &frozenset_as_number, /* tp_as_number */
2104 &set_as_sequence, /* tp_as_sequence */
2105 0, /* tp_as_mapping */
2106 frozenset_hash, /* tp_hash */
2107 0, /* tp_call */
2108 0, /* tp_str */
2109 PyObject_GenericGetAttr, /* tp_getattro */
2110 0, /* tp_setattro */
2111 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002112 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002113 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002114 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002115 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002116 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002117 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002118 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002119 (getiterfunc)set_iter, /* tp_iter */
2120 0, /* tp_iternext */
2121 frozenset_methods, /* tp_methods */
2122 0, /* tp_members */
2123 0, /* tp_getset */
2124 0, /* tp_base */
2125 0, /* tp_dict */
2126 0, /* tp_descr_get */
2127 0, /* tp_descr_set */
2128 0, /* tp_dictoffset */
2129 0, /* tp_init */
2130 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002131 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002132 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002133};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002134
2135
2136/***** C API functions *************************************************/
2137
2138PyObject *
2139PySet_New(PyObject *iterable)
2140{
2141 return make_new_set(&PySet_Type, iterable);
2142}
2143
2144PyObject *
2145PyFrozenSet_New(PyObject *iterable)
2146{
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002147 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002148}
2149
Neal Norwitz8c49c822006-03-04 18:41:19 +00002150Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002151PySet_Size(PyObject *anyset)
2152{
2153 if (!PyAnySet_Check(anyset)) {
2154 PyErr_BadInternalCall();
2155 return -1;
2156 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002157 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002158}
2159
2160int
Barry Warsaw176014f2006-03-30 22:45:35 +00002161PySet_Clear(PyObject *set)
2162{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002163 if (!PySet_Check(set)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002164 PyErr_BadInternalCall();
2165 return -1;
2166 }
2167 return set_clear_internal((PySetObject *)set);
2168}
2169
2170int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002171PySet_Contains(PyObject *anyset, PyObject *key)
2172{
2173 if (!PyAnySet_Check(anyset)) {
2174 PyErr_BadInternalCall();
2175 return -1;
2176 }
2177 return set_contains_key((PySetObject *)anyset, key);
2178}
2179
2180int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002181PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002182{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002183 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002184 PyErr_BadInternalCall();
2185 return -1;
2186 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002187 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002188}
2189
2190int
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002191PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002192{
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +00002193 if (!PySet_Check(anyset) &&
2194 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerdee3f652008-01-26 09:31:11 +00002195 PyErr_BadInternalCall();
2196 return -1;
2197 }
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002198 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002199}
2200
Barry Warsaw176014f2006-03-30 22:45:35 +00002201int
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002202_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Barry Warsaw176014f2006-03-30 22:45:35 +00002203{
2204 setentry *entry_ptr;
2205
2206 if (!PyAnySet_Check(set)) {
2207 PyErr_BadInternalCall();
2208 return -1;
2209 }
2210 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2211 return 0;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002212 *key = entry_ptr->key;
2213 return 1;
2214}
2215
2216int
2217_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2218{
2219 setentry *entry;
2220
2221 if (!PyAnySet_Check(set)) {
2222 PyErr_BadInternalCall();
2223 return -1;
2224 }
2225 if (set_next((PySetObject *)set, pos, &entry) == 0)
2226 return 0;
2227 *key = entry->key;
2228 *hash = entry->hash;
Barry Warsaw176014f2006-03-30 22:45:35 +00002229 return 1;
2230}
2231
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002232PyObject *
2233PySet_Pop(PyObject *set)
2234{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002235 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002236 PyErr_BadInternalCall();
2237 return NULL;
2238 }
2239 return set_pop((PySetObject *)set);
2240}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002241
Barry Warsaw176014f2006-03-30 22:45:35 +00002242int
2243_PySet_Update(PyObject *set, PyObject *iterable)
2244{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002245 if (!PySet_Check(set)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002246 PyErr_BadInternalCall();
2247 return -1;
2248 }
2249 return set_update_internal((PySetObject *)set, iterable);
2250}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002251
2252#ifdef Py_DEBUG
2253
2254/* Test code to be called with any three element set.
2255 Returns True and original set is restored. */
2256
2257#define assertRaises(call_return_value, exception) \
2258 do { \
2259 assert(call_return_value); \
2260 assert(PyErr_ExceptionMatches(exception)); \
2261 PyErr_Clear(); \
2262 } while(0)
2263
2264static PyObject *
2265test_c_api(PySetObject *so)
2266{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002267 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002268 char *s;
2269 Py_ssize_t i;
Guido van Rossum360496d2007-05-10 17:20:15 +00002270 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Barry Warsaw176014f2006-03-30 22:45:35 +00002271 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002272
2273 /* Verify preconditions and exercise type/size checks */
2274 assert(PyAnySet_Check(ob));
2275 assert(PyAnySet_CheckExact(ob));
2276 assert(!PyFrozenSet_CheckExact(ob));
2277 assert(PySet_Size(ob) == 3);
2278 assert(PySet_GET_SIZE(ob) == 3);
2279
2280 /* Raise TypeError for non-iterable constructor arguments */
2281 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2282 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2283
2284 /* Raise TypeError for unhashable key */
2285 dup = PySet_New(ob);
2286 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2287 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2288 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2289
2290 /* Exercise successful pop, contains, add, and discard */
2291 elem = PySet_Pop(ob);
2292 assert(PySet_Contains(ob, elem) == 0);
2293 assert(PySet_GET_SIZE(ob) == 2);
2294 assert(PySet_Add(ob, elem) == 0);
2295 assert(PySet_Contains(ob, elem) == 1);
2296 assert(PySet_GET_SIZE(ob) == 3);
2297 assert(PySet_Discard(ob, elem) == 1);
2298 assert(PySet_GET_SIZE(ob) == 2);
2299 assert(PySet_Discard(ob, elem) == 0);
2300 assert(PySet_GET_SIZE(ob) == 2);
2301
Barry Warsaw176014f2006-03-30 22:45:35 +00002302 /* Exercise clear */
2303 dup2 = PySet_New(dup);
2304 assert(PySet_Clear(dup2) == 0);
2305 assert(PySet_Size(dup2) == 0);
2306 Py_DECREF(dup2);
2307
2308 /* Raise SystemError on clear or update of frozen set */
2309 f = PyFrozenSet_New(dup);
2310 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2311 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +00002312 assert(PySet_Add(f, elem) == 0);
2313 Py_INCREF(f);
2314 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2315 Py_DECREF(f);
Barry Warsaw176014f2006-03-30 22:45:35 +00002316 Py_DECREF(f);
2317
2318 /* Exercise direct iteration */
2319 i = 0, count = 0;
Guido van Rossum360496d2007-05-10 17:20:15 +00002320 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2321 s = PyString_AsString(x);
Barry Warsaw176014f2006-03-30 22:45:35 +00002322 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2323 count++;
2324 }
2325 assert(count == 3);
2326
2327 /* Exercise updates */
2328 dup2 = PySet_New(NULL);
2329 assert(_PySet_Update(dup2, dup) == 0);
2330 assert(PySet_Size(dup2) == 3);
2331 assert(_PySet_Update(dup2, dup) == 0);
2332 assert(PySet_Size(dup2) == 3);
2333 Py_DECREF(dup2);
2334
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002335 /* Raise SystemError when self argument is not a set or frozenset. */
2336 t = PyTuple_New(0);
2337 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2338 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2339 Py_DECREF(t);
2340
2341 /* Raise SystemError when self argument is not a set. */
2342 f = PyFrozenSet_New(dup);
2343 assert(PySet_Size(f) == 3);
2344 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002345 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2346 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2347 Py_DECREF(f);
2348
2349 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002350 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2351 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002352 assert(PySet_GET_SIZE(ob) == 0);
2353 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2354
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002355 /* Restore the set from the copy using the PyNumber API */
2356 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2357 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002358
2359 /* Verify constructors accept NULL arguments */
2360 f = PySet_New(NULL);
2361 assert(f != NULL);
2362 assert(PySet_GET_SIZE(f) == 0);
2363 Py_DECREF(f);
2364 f = PyFrozenSet_New(NULL);
2365 assert(f != NULL);
2366 assert(PyFrozenSet_CheckExact(f));
2367 assert(PySet_GET_SIZE(f) == 0);
2368 Py_DECREF(f);
2369
2370 Py_DECREF(elem);
2371 Py_DECREF(dup);
2372 Py_RETURN_TRUE;
2373}
2374
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002375#undef assertRaises
2376
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002377#endif