blob: d33fff93fe247019c1b428192a39a0580a0e03a0 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis72d20672006-04-11 09:04:12 +00006 Copyright (c) 2003-6 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
Raymond Hettinger775ebe22006-12-08 18:12:24 +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 */
54#define MAXFREESETS 80
55static PySetObject *free_sets[MAXFREESETS];
56static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057
58/*
59The basic lookup function used by all operations.
60This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
61Open addressing is preferred over chaining since the link overhead for
62chaining would be substantial (100% with typical malloc overhead).
63
64The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000065probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000066
67All arithmetic on hash should ignore overflow.
68
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000069Unlike the dictionary implementation, the lookkey functions can return
70NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000071*/
72
73static setentry *
74set_lookkey(PySetObject *so, PyObject *key, register long hash)
75{
Martin v. Löwis18e16552006-02-15 17:27:45 +000076 register Py_ssize_t i;
77 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000078 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +000079 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000080 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000081 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000082 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000083 PyObject *startkey;
84
85 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000086 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000087 if (entry->key == NULL || entry->key == key)
88 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000089
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000090 if (entry->key == dummy)
91 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000092 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000093 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000094 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000095 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
96 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000097 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +000098 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000099 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000100 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 }
102 else {
103 /* The compare did major nasty stuff to the
104 * set: start over.
105 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000106 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000107 }
108 }
109 freeslot = NULL;
110 }
111
112 /* In the loop, key == dummy is by far (factor of 100s) the
113 least likely outcome, so test for that last. */
114 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
115 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000116 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000117 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000118 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000119 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 break;
121 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000122 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000123 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000124 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000125 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000126 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
127 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000128 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000129 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130 if (cmp > 0)
131 break;
132 }
133 else {
134 /* The compare did major nasty stuff to the
135 * set: start over.
136 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000137 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000138 }
139 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000140 else if (entry->key == dummy && freeslot == NULL)
141 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000142 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000143 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000144}
145
146/*
147 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000148 * This means we can always use _PyString_Eq directly and not have to check to
149 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150 */
151static setentry *
152set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
153{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000154 register Py_ssize_t i;
155 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000156 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000157 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000158 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000159 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000160
161 /* Make sure this function doesn't have to handle non-string keys,
162 including subclasses of str; e.g., one reason to subclass
163 strings is to override __eq__, and for speed we don't cater to
164 that here. */
165 if (!PyString_CheckExact(key)) {
166 so->lookup = set_lookkey;
167 return set_lookkey(so, key, hash);
168 }
169 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000170 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000171 if (entry->key == NULL || entry->key == key)
172 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000173 if (entry->key == dummy)
174 freeslot = entry;
175 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000176 if (entry->hash == hash && _PyString_Eq(entry->key, key))
177 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000178 freeslot = NULL;
179 }
180
181 /* In the loop, key == dummy is by far (factor of 100s) the
182 least likely outcome, so test for that last. */
183 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
184 i = (i << 2) + i + perturb + 1;
185 entry = &table[i & mask];
186 if (entry->key == NULL)
187 return freeslot == NULL ? entry : freeslot;
188 if (entry->key == key
189 || (entry->hash == hash
190 && entry->key != dummy
191 && _PyString_Eq(entry->key, key)))
192 return entry;
193 if (entry->key == dummy && freeslot == NULL)
194 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000195 }
Neal Norwitz7e3ec042006-10-28 21:37:16 +0000196 assert(0); /* NOT REACHED */
197 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000198}
199
200/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000201Internal routine to insert a new key into the table.
Raymond Hettinger775ebe22006-12-08 18:12:24 +0000202Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000203Eats a reference to key.
204*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000205static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206set_insert_key(register PySetObject *so, PyObject *key, long hash)
207{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000208 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
210
211 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000212 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000213 if (entry == NULL)
214 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000215 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000216 /* UNUSED */
217 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000218 entry->key = key;
219 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000221 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 entry->key = key;
224 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000225 so->used++;
226 Py_DECREF(dummy);
227 } else {
228 /* ACTIVE */
229 Py_DECREF(key);
230 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000231 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000232}
233
234/*
Raymond Hettinger775ebe22006-12-08 18:12:24 +0000235Internal routine used by set_table_resize() to insert an item which is
236known to be absent from the set. This routine also assumes that
237the set contains no deleted entries. Besides the performance benefit,
238using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
239Note that no refcounts are changed by this routine; if needed, the caller
240is responsible for incref'ing `key`.
241*/
242static void
243set_insert_clean(register PySetObject *so, PyObject *key, long hash)
244{
245 register size_t i;
246 register size_t perturb;
247 register size_t mask = (size_t)so->mask;
248 setentry *table = so->table;
249 register setentry *entry;
250
251 i = hash & mask;
252 entry = &table[i];
253 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
254 i = (i << 2) + i + perturb + 1;
255 entry = &table[i & mask];
256 }
257 so->fill++;
258 entry->key = key;
259 entry->hash = hash;
260 so->used++;
261}
262
263/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000264Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000265keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266actually be smaller than the old one.
267*/
268static int
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000269set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000271 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000272 setentry *oldtable, *newtable, *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000273 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274 int is_oldtable_malloced;
275 setentry small_copy[PySet_MINSIZE];
276
277 assert(minused >= 0);
278
279 /* Find the smallest table size > minused. */
280 for (newsize = PySet_MINSIZE;
281 newsize <= minused && newsize > 0;
282 newsize <<= 1)
283 ;
284 if (newsize <= 0) {
285 PyErr_NoMemory();
286 return -1;
287 }
288
289 /* Get space for a new table. */
290 oldtable = so->table;
291 assert(oldtable != NULL);
292 is_oldtable_malloced = oldtable != so->smalltable;
293
294 if (newsize == PySet_MINSIZE) {
295 /* A large table is shrinking, or we can't get any smaller. */
296 newtable = so->smalltable;
297 if (newtable == oldtable) {
298 if (so->fill == so->used) {
299 /* No dummies, so no point doing anything. */
300 return 0;
301 }
302 /* We're not going to resize it, but rebuild the
303 table anyway to purge old dummy entries.
304 Subtle: This is *necessary* if fill==size,
305 as set_lookkey needs at least one virgin slot to
306 terminate failing searches. If fill < size, it's
307 merely desirable, as dummies slow searches. */
308 assert(so->fill > so->used);
309 memcpy(small_copy, oldtable, sizeof(small_copy));
310 oldtable = small_copy;
311 }
312 }
313 else {
314 newtable = PyMem_NEW(setentry, newsize);
315 if (newtable == NULL) {
316 PyErr_NoMemory();
317 return -1;
318 }
319 }
320
321 /* Make the set empty, using the new table. */
322 assert(newtable != oldtable);
323 so->table = newtable;
324 so->mask = newsize - 1;
325 memset(newtable, 0, sizeof(setentry) * newsize);
326 so->used = 0;
327 i = so->fill;
328 so->fill = 0;
329
330 /* Copy the data over; this is refcount-neutral for active entries;
331 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000332 for (entry = oldtable; i > 0; entry++) {
333 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334 /* UNUSED */
335 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000336 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337 /* DUMMY */
338 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000339 assert(entry->key == dummy);
340 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341 } else {
342 /* ACTIVE */
343 --i;
Raymond Hettinger775ebe22006-12-08 18:12:24 +0000344 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345 }
346 }
347
348 if (is_oldtable_malloced)
349 PyMem_DEL(oldtable);
350 return 0;
351}
352
Raymond Hettingerc991db22005-08-11 07:58:45 +0000353/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
354
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356set_add_entry(register PySetObject *so, setentry *entry)
357{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000358 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000359
360 assert(so->fill <= so->mask); /* at least one empty slot */
361 n_used = so->used;
362 Py_INCREF(entry->key);
Georg Brandl8de403a2006-09-08 06:02:26 +0000363 if (set_insert_key(so, entry->key, entry->hash) == -1) {
364 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000365 return -1;
Georg Brandl8de403a2006-09-08 06:02:26 +0000366 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000367 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
368 return 0;
369 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
370}
371
372static int
373set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000374{
375 register long hash;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000376 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377
Raymond Hettingerc991db22005-08-11 07:58:45 +0000378 if (!PyString_CheckExact(key) ||
379 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380 hash = PyObject_Hash(key);
381 if (hash == -1)
382 return -1;
383 }
384 assert(so->fill <= so->mask); /* at least one empty slot */
385 n_used = so->used;
386 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000387 if (set_insert_key(so, key, hash) == -1) {
388 Py_DECREF(key);
389 return -1;
390 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000391 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
392 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000393 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394}
395
396#define DISCARD_NOTFOUND 0
397#define DISCARD_FOUND 1
398
399static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000400set_discard_entry(PySetObject *so, setentry *oldentry)
401{ register setentry *entry;
402 PyObject *old_key;
403
404 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000405 if (entry == NULL)
406 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000407 if (entry->key == NULL || entry->key == dummy)
408 return DISCARD_NOTFOUND;
409 old_key = entry->key;
410 Py_INCREF(dummy);
411 entry->key = dummy;
412 so->used--;
413 Py_DECREF(old_key);
414 return DISCARD_FOUND;
415}
416
417static int
418set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000419{
420 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000421 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000422 PyObject *old_key;
423
424 assert (PyAnySet_Check(so));
425 if (!PyString_CheckExact(key) ||
426 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
427 hash = PyObject_Hash(key);
428 if (hash == -1)
429 return -1;
430 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000431 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000432 if (entry == NULL)
433 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000434 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000435 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000438 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439 so->used--;
440 Py_DECREF(old_key);
441 return DISCARD_FOUND;
442}
443
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000444static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445set_clear_internal(PySetObject *so)
446{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000447 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000448 int table_is_malloced;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000449 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450 setentry small_copy[PySet_MINSIZE];
451#ifdef Py_DEBUG
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000452 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000454
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455 n = so->mask + 1;
456 i = 0;
457#endif
458
459 table = so->table;
460 assert(table != NULL);
461 table_is_malloced = table != so->smalltable;
462
463 /* This is delicate. During the process of clearing the set,
464 * decrefs can cause the set to mutate. To avoid fatal confusion
465 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000466 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467 * clearing.
468 */
469 fill = so->fill;
470 if (table_is_malloced)
471 EMPTY_TO_MINSIZE(so);
472
473 else if (fill > 0) {
474 /* It's a small table with something that needs to be cleared.
475 * Afraid the only safe way is to copy the set entries into
476 * another small table first.
477 */
478 memcpy(small_copy, table, sizeof(small_copy));
479 table = small_copy;
480 EMPTY_TO_MINSIZE(so);
481 }
482 /* else it's a small table that's already empty */
483
484 /* Now we can finally clear things. If C had refcounts, we could
485 * assert that the refcount on table is 1 now, i.e. that this function
486 * has unique access to it, so decref side-effects can't alter it.
487 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000488 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489#ifdef Py_DEBUG
490 assert(i < n);
491 ++i;
492#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000493 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000495 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496 }
497#ifdef Py_DEBUG
498 else
Raymond Hettinger334b5b22006-03-26 03:11:29 +0000499 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500#endif
501 }
502
503 if (table_is_malloced)
504 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000505 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506}
507
508/*
509 * Iterate over a set table. Use like so:
510 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000511 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000512 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000513 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000514 * while (set_next(yourset, &pos, &entry)) {
515 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000516 * }
517 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000518 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519 * mutates the table.
520 */
521static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000522set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524 Py_ssize_t i;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000525 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000526 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000527
528 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000529 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000530 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000531 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000532 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000533 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536 if (i > mask)
537 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000538 assert(table[i].key != NULL);
539 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540 return 1;
541}
542
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000543static void
544set_dealloc(PySetObject *so)
545{
546 register setentry *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000547 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000548 PyObject_GC_UnTrack(so);
549 Py_TRASHCAN_SAFE_BEGIN(so)
550 if (so->weakreflist != NULL)
551 PyObject_ClearWeakRefs((PyObject *) so);
552
553 for (entry = so->table; fill > 0; entry++) {
554 if (entry->key) {
555 --fill;
556 Py_DECREF(entry->key);
557 }
558 }
559 if (so->table != so->smalltable)
560 PyMem_DEL(so->table);
561 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
562 free_sets[num_free_sets++] = so;
563 else
564 so->ob_type->tp_free(so);
565 Py_TRASHCAN_SAFE_END(so)
566}
567
568static int
569set_tp_print(PySetObject *so, FILE *fp, int flags)
570{
571 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000572 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000573 char *emit = ""; /* No separator emitted on first pass */
574 char *separator = ", ";
575
576 fprintf(fp, "%s([", so->ob_type->tp_name);
577 while (set_next(so, &pos, &entry)) {
578 fputs(emit, fp);
579 emit = separator;
580 if (PyObject_Print(entry->key, fp, 0) != 0)
581 return -1;
582 }
583 fputs("])", fp);
584 return 0;
585}
586
587static PyObject *
588set_repr(PySetObject *so)
589{
590 PyObject *keys, *result, *listrepr;
591
592 keys = PySequence_List((PyObject *)so);
593 if (keys == NULL)
594 return NULL;
595 listrepr = PyObject_Repr(keys);
596 Py_DECREF(keys);
597 if (listrepr == NULL)
598 return NULL;
599
600 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
601 PyString_AS_STRING(listrepr));
602 Py_DECREF(listrepr);
603 return result;
604}
605
Martin v. Löwis18e16552006-02-15 17:27:45 +0000606static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000607set_len(PyObject *so)
608{
609 return ((PySetObject *)so)->used;
610}
611
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000612static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000613set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000614{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000615 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000616 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000617 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000618
619 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000620 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000622 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000623 if (other == so || other->used == 0)
624 /* a.update(a) or a.update({}); nothing to do */
625 return 0;
626 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000627 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000628 * that there will be no (or few) overlapping keys.
629 */
630 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
631 if (set_table_resize(so, (so->used + other->used)*2) != 0)
632 return -1;
633 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000634 for (i = 0; i <= other->mask; i++) {
635 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000636 if (entry->key != NULL &&
637 entry->key != dummy) {
638 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000639 if (set_insert_key(so, entry->key, entry->hash) == -1) {
640 Py_DECREF(entry->key);
641 return -1;
642 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000643 }
644 }
645 return 0;
646}
647
648static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000649set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000650{
651 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000652 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000653
654 if (!PyString_CheckExact(key) ||
655 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
656 hash = PyObject_Hash(key);
657 if (hash == -1)
658 return -1;
659 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000660 entry = (so->lookup)(so, key, hash);
661 if (entry == NULL)
662 return -1;
663 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000664 return key != NULL && key != dummy;
665}
666
Raymond Hettingerc991db22005-08-11 07:58:45 +0000667static int
668set_contains_entry(PySetObject *so, setentry *entry)
669{
670 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000671 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000672
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000673 lu_entry = (so->lookup)(so, entry->key, entry->hash);
674 if (lu_entry == NULL)
675 return -1;
676 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000677 return key != NULL && key != dummy;
678}
679
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000680static PyObject *
681set_pop(PySetObject *so)
682{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000683 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000684 register setentry *entry;
685 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000686
687 assert (PyAnySet_Check(so));
688 if (so->used == 0) {
689 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
690 return NULL;
691 }
692
693 /* Set entry to "the first" unused or dummy set entry. We abuse
694 * the hash field of slot 0 to hold a search finger:
695 * If slot 0 has a value, use slot 0.
696 * Else slot 0 is being used to hold a search finger,
697 * and we use its hash value as the first index to look.
698 */
699 entry = &so->table[0];
700 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000701 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000702 /* The hash field may be a real hash value, or it may be a
703 * legit search finger, or it may be a once-legit search
704 * finger that's out of bounds now because it wrapped around
705 * or the table shrunk -- simply make sure it's in bounds now.
706 */
707 if (i > so->mask || i < 1)
708 i = 1; /* skip slot 0 */
709 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
710 i++;
711 if (i > so->mask)
712 i = 1;
713 }
714 }
715 key = entry->key;
716 Py_INCREF(dummy);
717 entry->key = dummy;
718 so->used--;
719 so->table[0].hash = i + 1; /* next place to start */
720 return key;
721}
722
723PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
724
725static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000726set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000727{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000728 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000729 setentry *entry;
730
731 while (set_next(so, &pos, &entry))
732 Py_VISIT(entry->key);
733 return 0;
734}
735
736static long
737frozenset_hash(PyObject *self)
738{
739 PySetObject *so = (PySetObject *)self;
740 long h, hash = 1927868237L;
741 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000742 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000743
744 if (so->hash != -1)
745 return so->hash;
746
747 hash *= PySet_GET_SIZE(self) + 1;
748 while (set_next(so, &pos, &entry)) {
749 /* Work to increase the bit dispersion for closely spaced hash
750 values. The is important because some use cases have many
751 combinations of a small number of elements with nearby
752 hashes so that many distinct combinations collapse to only
753 a handful of distinct hash values. */
754 h = entry->hash;
755 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
756 }
757 hash = hash * 69069L + 907133923L;
758 if (hash == -1)
759 hash = 590923713L;
760 so->hash = hash;
761 return hash;
762}
763
764static long
765set_nohash(PyObject *self)
766{
767 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
768 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000769}
770
Raymond Hettingera9d99362005-08-05 00:01:15 +0000771/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000772
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000773typedef struct {
774 PyObject_HEAD
775 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000776 Py_ssize_t si_used;
777 Py_ssize_t si_pos;
778 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000779} setiterobject;
780
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000781static void
782setiter_dealloc(setiterobject *si)
783{
784 Py_XDECREF(si->si_set);
785 PyObject_Del(si);
786}
787
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000788static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789setiter_len(setiterobject *si)
790{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000791 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000793 len = si->len;
794 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000795}
796
Armin Rigof5b3e362006-02-11 21:32:43 +0000797PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000798
799static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000800 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000801 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802};
803
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000804static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000805{
806 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000807 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000808 register setentry *entry;
809 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000811 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000813 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000814
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000815 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000816 PyErr_SetString(PyExc_RuntimeError,
817 "Set changed size during iteration");
818 si->si_used = -1; /* Make this state sticky */
819 return NULL;
820 }
821
822 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000823 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000824 entry = so->table;
825 mask = so->mask;
826 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000827 i++;
828 si->si_pos = i+1;
829 if (i > mask)
830 goto fail;
831 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000832 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833 Py_INCREF(key);
834 return key;
835
836fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000837 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000838 si->si_set = NULL;
839 return NULL;
840}
841
Hye-Shik Change2956762005-08-01 05:26:41 +0000842static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000843 PyObject_HEAD_INIT(&PyType_Type)
844 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000845 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000846 sizeof(setiterobject), /* tp_basicsize */
847 0, /* tp_itemsize */
848 /* methods */
849 (destructor)setiter_dealloc, /* tp_dealloc */
850 0, /* tp_print */
851 0, /* tp_getattr */
852 0, /* tp_setattr */
853 0, /* tp_compare */
854 0, /* tp_repr */
855 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000856 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000857 0, /* tp_as_mapping */
858 0, /* tp_hash */
859 0, /* tp_call */
860 0, /* tp_str */
861 PyObject_GenericGetAttr, /* tp_getattro */
862 0, /* tp_setattro */
863 0, /* tp_as_buffer */
864 Py_TPFLAGS_DEFAULT, /* tp_flags */
865 0, /* tp_doc */
866 0, /* tp_traverse */
867 0, /* tp_clear */
868 0, /* tp_richcompare */
869 0, /* tp_weaklistoffset */
870 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000871 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000872 setiter_methods, /* tp_methods */
873 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000874};
875
Martin v. Löwis72d20672006-04-11 09:04:12 +0000876static PyObject *
877set_iter(PySetObject *so)
878{
879 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
880 if (si == NULL)
881 return NULL;
882 Py_INCREF(so);
883 si->si_set = so;
884 si->si_used = so->used;
885 si->si_pos = 0;
886 si->len = so->used;
887 return (PyObject *)si;
888}
889
Raymond Hettingerd7946662005-08-01 21:39:29 +0000890static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000891set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000892{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000893 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000894
Raymond Hettingerd7946662005-08-01 21:39:29 +0000895 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000896 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000897
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000898 if (PyDict_Check(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000899 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000900 Py_ssize_t pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000901 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000902 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000903 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000904 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000905 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000906 }
907
Raymond Hettingera38123e2003-11-24 22:18:49 +0000908 it = PyObject_GetIter(other);
909 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000910 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000911
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000912 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000913 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000914 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000915 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000916 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000917 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000918 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000919 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000920 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000921 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000922 return -1;
923 return 0;
924}
925
926static PyObject *
927set_update(PySetObject *so, PyObject *other)
928{
929 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000930 return NULL;
931 Py_RETURN_NONE;
932}
933
934PyDoc_STRVAR(update_doc,
935"Update a set with the union of itself and another.");
936
937static PyObject *
938make_new_set(PyTypeObject *type, PyObject *iterable)
939{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000940 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000941
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000942 if (dummy == NULL) { /* Auto-initialize dummy */
943 dummy = PyString_FromString("<dummy key>");
944 if (dummy == NULL)
945 return NULL;
946 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000947
948 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000949 if (num_free_sets &&
950 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
951 so = free_sets[--num_free_sets];
952 assert (so != NULL && PyAnySet_CheckExact(so));
953 so->ob_type = type;
954 _Py_NewReference((PyObject *)so);
955 EMPTY_TO_MINSIZE(so);
956 PyObject_GC_Track(so);
957 } else {
958 so = (PySetObject *)type->tp_alloc(type, 0);
959 if (so == NULL)
960 return NULL;
961 /* tp_alloc has already zeroed the structure */
962 assert(so->table == NULL && so->fill == 0 && so->used == 0);
963 INIT_NONZERO_SET_SLOTS(so);
964 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000965
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000966 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000967 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000968
Raymond Hettingera38123e2003-11-24 22:18:49 +0000969 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000970 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000971 Py_DECREF(so);
972 return NULL;
973 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000974 }
975
Raymond Hettingera690a992003-11-16 16:17:49 +0000976 return (PyObject *)so;
977}
978
Raymond Hettingerd7946662005-08-01 21:39:29 +0000979/* The empty frozenset is a singleton */
980static PyObject *emptyfrozenset = NULL;
981
Raymond Hettingera690a992003-11-16 16:17:49 +0000982static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000983frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000984{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000985 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000986
Georg Brandl02c42872005-08-26 06:42:30 +0000987 if (!_PyArg_NoKeywords("frozenset()", kwds))
988 return NULL;
989
Raymond Hettingera690a992003-11-16 16:17:49 +0000990 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
991 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000992
993 if (type != &PyFrozenSet_Type)
994 return make_new_set(type, iterable);
995
996 if (iterable != NULL) {
997 /* frozenset(f) is idempotent */
998 if (PyFrozenSet_CheckExact(iterable)) {
999 Py_INCREF(iterable);
1000 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001001 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001002 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001003 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001004 return result;
1005 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001006 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001007 /* The empty frozenset is a singleton */
1008 if (emptyfrozenset == NULL)
1009 emptyfrozenset = make_new_set(type, NULL);
1010 Py_XINCREF(emptyfrozenset);
1011 return emptyfrozenset;
1012}
1013
1014void
1015PySet_Fini(void)
1016{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001017 PySetObject *so;
1018
1019 while (num_free_sets) {
1020 num_free_sets--;
1021 so = free_sets[num_free_sets];
1022 PyObject_GC_Del(so);
1023 }
Martin v. Löwised8f7832006-04-15 12:47:23 +00001024 Py_CLEAR(dummy);
1025 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001026}
1027
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001028static PyObject *
1029set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1030{
Georg Brandl02c42872005-08-26 06:42:30 +00001031 if (!_PyArg_NoKeywords("set()", kwds))
1032 return NULL;
1033
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001034 return make_new_set(type, NULL);
1035}
1036
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001037/* set_swap_bodies() switches the contents of any two sets by moving their
1038 internal data pointers and, if needed, copying the internal smalltables.
1039 Semantically equivalent to:
1040
1041 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1042
1043 The function always succeeds and it leaves both objects in a stable state.
1044 Useful for creating temporary frozensets from sets for membership testing
1045 in __contains__(), discard(), and remove(). Also useful for operations
1046 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001047 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001048*/
1049
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001050static void
1051set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001052{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001053 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001054 setentry *u;
1055 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1056 setentry tab[PySet_MINSIZE];
1057 long h;
1058
1059 t = a->fill; a->fill = b->fill; b->fill = t;
1060 t = a->used; a->used = b->used; b->used = t;
1061 t = a->mask; a->mask = b->mask; b->mask = t;
1062
1063 u = a->table;
1064 if (a->table == a->smalltable)
1065 u = b->smalltable;
1066 a->table = b->table;
1067 if (b->table == b->smalltable)
1068 a->table = a->smalltable;
1069 b->table = u;
1070
1071 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1072
1073 if (a->table == a->smalltable || b->table == b->smalltable) {
1074 memcpy(tab, a->smalltable, sizeof(tab));
1075 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1076 memcpy(b->smalltable, tab, sizeof(tab));
1077 }
1078
Raymond Hettingera580c472005-08-05 17:19:54 +00001079 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1080 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1081 h = a->hash; a->hash = b->hash; b->hash = h;
1082 } else {
1083 a->hash = -1;
1084 b->hash = -1;
1085 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001086}
1087
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001088static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001089set_copy(PySetObject *so)
1090{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001091 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001092}
1093
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001094static PyObject *
1095frozenset_copy(PySetObject *so)
1096{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001097 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001098 Py_INCREF(so);
1099 return (PyObject *)so;
1100 }
1101 return set_copy(so);
1102}
1103
Raymond Hettingera690a992003-11-16 16:17:49 +00001104PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1105
1106static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001107set_clear(PySetObject *so)
1108{
1109 set_clear_internal(so);
1110 Py_RETURN_NONE;
1111}
1112
1113PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1114
1115static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001116set_union(PySetObject *so, PyObject *other)
1117{
1118 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001119
1120 result = (PySetObject *)set_copy(so);
1121 if (result == NULL)
1122 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001123 if ((PyObject *)so == other)
1124 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001125 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001126 Py_DECREF(result);
1127 return NULL;
1128 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001129 return (PyObject *)result;
1130}
1131
1132PyDoc_STRVAR(union_doc,
1133 "Return the union of two sets as a new set.\n\
1134\n\
1135(i.e. all elements that are in either set.)");
1136
1137static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001138set_or(PySetObject *so, PyObject *other)
1139{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001140 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001141 Py_INCREF(Py_NotImplemented);
1142 return Py_NotImplemented;
1143 }
1144 return set_union(so, other);
1145}
1146
1147static PyObject *
1148set_ior(PySetObject *so, PyObject *other)
1149{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001150 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001151 Py_INCREF(Py_NotImplemented);
1152 return Py_NotImplemented;
1153 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001154 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001155 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001156 Py_INCREF(so);
1157 return (PyObject *)so;
1158}
1159
1160static PyObject *
1161set_intersection(PySetObject *so, PyObject *other)
1162{
1163 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001164 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001165
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001166 if ((PyObject *)so == other)
1167 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001168
Raymond Hettingera690a992003-11-16 16:17:49 +00001169 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1170 if (result == NULL)
1171 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001172
Raymond Hettingerc991db22005-08-11 07:58:45 +00001173 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001174 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001175 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001176
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001177 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001178 tmp = (PyObject *)so;
1179 so = (PySetObject *)other;
1180 other = tmp;
1181 }
1182
Raymond Hettingerc991db22005-08-11 07:58:45 +00001183 while (set_next((PySetObject *)other, &pos, &entry)) {
Georg Brandl8de403a2006-09-08 06:02:26 +00001184 int rv = set_contains_entry(so, entry);
1185 if (rv == -1) {
1186 Py_DECREF(result);
1187 return NULL;
1188 }
1189 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001190 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001191 Py_DECREF(result);
1192 return NULL;
1193 }
1194 }
1195 }
1196 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001197 }
1198
Raymond Hettingera690a992003-11-16 16:17:49 +00001199 it = PyObject_GetIter(other);
1200 if (it == NULL) {
1201 Py_DECREF(result);
1202 return NULL;
1203 }
1204
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001205 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger775ebe22006-12-08 18:12:24 +00001206 int rv;
1207 setentry entry;
1208 long hash = PyObject_Hash(key);
1209
1210 if (hash == -1) {
1211 Py_DECREF(it);
1212 Py_DECREF(result);
1213 Py_DECREF(key);
1214 return NULL;
1215 }
1216 entry.hash = hash;
1217 entry.key = key;
1218 rv = set_contains_entry(so, &entry);
Georg Brandl8de403a2006-09-08 06:02:26 +00001219 if (rv == -1) {
1220 Py_DECREF(it);
1221 Py_DECREF(result);
1222 Py_DECREF(key);
1223 return NULL;
1224 }
1225 if (rv) {
Raymond Hettinger775ebe22006-12-08 18:12:24 +00001226 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001227 Py_DECREF(it);
1228 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001229 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001230 return NULL;
1231 }
1232 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001233 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001234 }
1235 Py_DECREF(it);
1236 if (PyErr_Occurred()) {
1237 Py_DECREF(result);
1238 return NULL;
1239 }
1240 return (PyObject *)result;
1241}
1242
1243PyDoc_STRVAR(intersection_doc,
1244"Return the intersection of two sets as a new set.\n\
1245\n\
1246(i.e. all elements that are in both sets.)");
1247
1248static PyObject *
1249set_intersection_update(PySetObject *so, PyObject *other)
1250{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001251 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001252
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001253 tmp = set_intersection(so, other);
1254 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001255 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001256 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001257 Py_DECREF(tmp);
1258 Py_RETURN_NONE;
1259}
1260
1261PyDoc_STRVAR(intersection_update_doc,
1262"Update a set with the intersection of itself and another.");
1263
1264static PyObject *
1265set_and(PySetObject *so, PyObject *other)
1266{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001267 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001268 Py_INCREF(Py_NotImplemented);
1269 return Py_NotImplemented;
1270 }
1271 return set_intersection(so, other);
1272}
1273
1274static PyObject *
1275set_iand(PySetObject *so, PyObject *other)
1276{
1277 PyObject *result;
1278
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001279 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001280 Py_INCREF(Py_NotImplemented);
1281 return Py_NotImplemented;
1282 }
1283 result = set_intersection_update(so, other);
1284 if (result == NULL)
1285 return NULL;
1286 Py_DECREF(result);
1287 Py_INCREF(so);
1288 return (PyObject *)so;
1289}
1290
Neal Norwitz6576bd82005-11-13 18:41:28 +00001291static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001292set_difference_update_internal(PySetObject *so, PyObject *other)
1293{
1294 if ((PyObject *)so == other)
1295 return set_clear_internal(so);
1296
1297 if (PyAnySet_Check(other)) {
1298 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001299 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001300
1301 while (set_next((PySetObject *)other, &pos, &entry))
Georg Brandl8de403a2006-09-08 06:02:26 +00001302 if (set_discard_entry(so, entry) == -1)
1303 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001304 } else {
1305 PyObject *key, *it;
1306 it = PyObject_GetIter(other);
1307 if (it == NULL)
1308 return -1;
1309
1310 while ((key = PyIter_Next(it)) != NULL) {
1311 if (set_discard_key(so, key) == -1) {
1312 Py_DECREF(it);
1313 Py_DECREF(key);
1314 return -1;
1315 }
1316 Py_DECREF(key);
1317 }
1318 Py_DECREF(it);
1319 if (PyErr_Occurred())
1320 return -1;
1321 }
1322 /* If more than 1/5 are dummies, then resize them away. */
1323 if ((so->fill - so->used) * 5 < so->mask)
1324 return 0;
1325 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1326}
1327
Raymond Hettingera690a992003-11-16 16:17:49 +00001328static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001329set_difference_update(PySetObject *so, PyObject *other)
1330{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001331 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001332 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001333 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001334}
1335
1336PyDoc_STRVAR(difference_update_doc,
1337"Remove all elements of another set from this set.");
1338
1339static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001340set_difference(PySetObject *so, PyObject *other)
1341{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001342 PyObject *result;
1343 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001344 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001345
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001346 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001347 result = set_copy(so);
1348 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001349 return NULL;
1350 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001351 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001352 Py_DECREF(result);
1353 return NULL;
1354 }
1355
1356 result = make_new_set(so->ob_type, NULL);
1357 if (result == NULL)
1358 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001359
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001360 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001361 while (set_next(so, &pos, &entry)) {
1362 setentry entrycopy;
1363 entrycopy.hash = entry->hash;
1364 entrycopy.key = entry->key;
1365 if (!PyDict_Contains(other, entry->key)) {
Georg Brandl8de403a2006-09-08 06:02:26 +00001366 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1367 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001368 return NULL;
Georg Brandl8de403a2006-09-08 06:02:26 +00001369 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001370 }
1371 }
1372 return result;
1373 }
1374
Raymond Hettingerc991db22005-08-11 07:58:45 +00001375 while (set_next(so, &pos, &entry)) {
Georg Brandl8de403a2006-09-08 06:02:26 +00001376 int rv = set_contains_entry((PySetObject *)other, entry);
1377 if (rv == -1) {
1378 Py_DECREF(result);
1379 return NULL;
1380 }
1381 if (!rv) {
1382 if (set_add_entry((PySetObject *)result, entry) == -1) {
1383 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001384 return NULL;
Georg Brandl8de403a2006-09-08 06:02:26 +00001385 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001386 }
1387 }
1388 return result;
1389}
1390
1391PyDoc_STRVAR(difference_doc,
1392"Return the difference of two sets as a new set.\n\
1393\n\
1394(i.e. all elements that are in this set but not the other.)");
1395static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001396set_sub(PySetObject *so, PyObject *other)
1397{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001398 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001399 Py_INCREF(Py_NotImplemented);
1400 return Py_NotImplemented;
1401 }
1402 return set_difference(so, other);
1403}
1404
1405static PyObject *
1406set_isub(PySetObject *so, PyObject *other)
1407{
1408 PyObject *result;
1409
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001410 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001411 Py_INCREF(Py_NotImplemented);
1412 return Py_NotImplemented;
1413 }
1414 result = set_difference_update(so, other);
1415 if (result == NULL)
1416 return NULL;
1417 Py_DECREF(result);
1418 Py_INCREF(so);
1419 return (PyObject *)so;
1420}
1421
1422static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001423set_symmetric_difference_update(PySetObject *so, PyObject *other)
1424{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001425 PySetObject *otherset;
1426 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001427 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001428 setentry *entry;
1429
1430 if ((PyObject *)so == other)
1431 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001432
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001433 if (PyDict_Check(other)) {
1434 PyObject *value;
1435 int rv;
1436 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettinger775ebe22006-12-08 18:12:24 +00001437 setentry an_entry;
1438 long hash = PyObject_Hash(key);
1439
1440 if (hash == -1)
1441 return NULL;
1442 an_entry.hash = hash;
1443 an_entry.key = key;
1444 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001445 if (rv == -1)
1446 return NULL;
1447 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger775ebe22006-12-08 18:12:24 +00001448 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001449 return NULL;
1450 }
1451 }
1452 Py_RETURN_NONE;
1453 }
1454
1455 if (PyAnySet_Check(other)) {
1456 Py_INCREF(other);
1457 otherset = (PySetObject *)other;
1458 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001459 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1460 if (otherset == NULL)
1461 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001462 }
1463
Raymond Hettingerc991db22005-08-11 07:58:45 +00001464 while (set_next(otherset, &pos, &entry)) {
1465 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001466 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001467 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001468 return NULL;
1469 }
1470 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001471 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001472 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001473 return NULL;
1474 }
1475 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001476 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001477 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001478 Py_RETURN_NONE;
1479}
1480
1481PyDoc_STRVAR(symmetric_difference_update_doc,
1482"Update a set with the symmetric difference of itself and another.");
1483
1484static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001485set_symmetric_difference(PySetObject *so, PyObject *other)
1486{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001487 PyObject *rv;
1488 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001489
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001490 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1491 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001492 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001493 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1494 if (rv == NULL)
1495 return NULL;
1496 Py_DECREF(rv);
1497 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001498}
1499
1500PyDoc_STRVAR(symmetric_difference_doc,
1501"Return the symmetric difference of two sets as a new set.\n\
1502\n\
1503(i.e. all elements that are in exactly one of the sets.)");
1504
1505static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001506set_xor(PySetObject *so, PyObject *other)
1507{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001508 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001509 Py_INCREF(Py_NotImplemented);
1510 return Py_NotImplemented;
1511 }
1512 return set_symmetric_difference(so, other);
1513}
1514
1515static PyObject *
1516set_ixor(PySetObject *so, PyObject *other)
1517{
1518 PyObject *result;
1519
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001520 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001521 Py_INCREF(Py_NotImplemented);
1522 return Py_NotImplemented;
1523 }
1524 result = set_symmetric_difference_update(so, other);
1525 if (result == NULL)
1526 return NULL;
1527 Py_DECREF(result);
1528 Py_INCREF(so);
1529 return (PyObject *)so;
1530}
1531
1532static PyObject *
1533set_issubset(PySetObject *so, PyObject *other)
1534{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001535 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001536 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001537
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001538 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001539 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001540 tmp = make_new_set(&PySet_Type, other);
1541 if (tmp == NULL)
1542 return NULL;
1543 result = set_issubset(so, tmp);
1544 Py_DECREF(tmp);
1545 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001546 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001547 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001548 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001549
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001550 while (set_next(so, &pos, &entry)) {
Georg Brandl8de403a2006-09-08 06:02:26 +00001551 int rv = set_contains_entry((PySetObject *)other, entry);
1552 if (rv == -1)
1553 return NULL;
1554 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001555 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001556 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001557 Py_RETURN_TRUE;
1558}
1559
1560PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1561
1562static PyObject *
1563set_issuperset(PySetObject *so, PyObject *other)
1564{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001565 PyObject *tmp, *result;
1566
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001567 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001568 tmp = make_new_set(&PySet_Type, other);
1569 if (tmp == NULL)
1570 return NULL;
1571 result = set_issuperset(so, tmp);
1572 Py_DECREF(tmp);
1573 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001574 }
1575 return set_issubset((PySetObject *)other, (PyObject *)so);
1576}
1577
1578PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1579
Raymond Hettingera690a992003-11-16 16:17:49 +00001580static PyObject *
1581set_richcompare(PySetObject *v, PyObject *w, int op)
1582{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001583 PyObject *r1, *r2;
1584
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001585 if(!PyAnySet_Check(w)) {
1586 if (op == Py_EQ)
1587 Py_RETURN_FALSE;
1588 if (op == Py_NE)
1589 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001590 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1591 return NULL;
1592 }
1593 switch (op) {
1594 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001595 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001596 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001597 if (v->hash != -1 &&
1598 ((PySetObject *)w)->hash != -1 &&
1599 v->hash != ((PySetObject *)w)->hash)
1600 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001601 return set_issubset(v, w);
1602 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001603 r1 = set_richcompare(v, w, Py_EQ);
1604 if (r1 == NULL)
1605 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001606 r2 = PyBool_FromLong(PyObject_Not(r1));
1607 Py_DECREF(r1);
1608 return r2;
1609 case Py_LE:
1610 return set_issubset(v, w);
1611 case Py_GE:
1612 return set_issuperset(v, w);
1613 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001614 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001615 Py_RETURN_FALSE;
1616 return set_issubset(v, w);
1617 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001618 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001619 Py_RETURN_FALSE;
1620 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001621 }
1622 Py_INCREF(Py_NotImplemented);
1623 return Py_NotImplemented;
1624}
1625
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001626static int
Georg Brandl347b3002006-03-30 11:57:00 +00001627set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001628{
1629 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1630 return -1;
1631}
1632
Raymond Hettingera690a992003-11-16 16:17:49 +00001633static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001634set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001635{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001636 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001637 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001638 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001639}
1640
1641PyDoc_STRVAR(add_doc,
1642"Add an element to a set.\n\
1643\n\
1644This has no effect if the element is already present.");
1645
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001646static int
1647set_contains(PySetObject *so, PyObject *key)
1648{
1649 PyObject *tmpkey;
1650 int rv;
1651
1652 rv = set_contains_key(so, key);
1653 if (rv == -1) {
1654 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1655 return -1;
1656 PyErr_Clear();
1657 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1658 if (tmpkey == NULL)
1659 return -1;
1660 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1661 rv = set_contains(so, tmpkey);
1662 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1663 Py_DECREF(tmpkey);
1664 }
1665 return rv;
1666}
1667
1668static PyObject *
1669set_direct_contains(PySetObject *so, PyObject *key)
1670{
1671 long result;
1672
1673 result = set_contains(so, key);
1674 if (result == -1)
1675 return NULL;
1676 return PyBool_FromLong(result);
1677}
1678
1679PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1680
Raymond Hettingera690a992003-11-16 16:17:49 +00001681static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001682set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001683{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001684 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001685 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001686
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001687 rv = set_discard_key(so, key);
1688 if (rv == -1) {
1689 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1690 return NULL;
1691 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001692 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1693 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001694 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001695 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001696 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001697 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001698 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001699 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001700 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger775ebe22006-12-08 18:12:24 +00001701 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001702 return NULL;
1703 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001704 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001705}
1706
1707PyDoc_STRVAR(remove_doc,
1708"Remove an element from a set; it must be a member.\n\
1709\n\
1710If the element is not a member, raise a KeyError.");
1711
1712static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001713set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001714{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001715 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001716 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001717
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001718 rv = set_discard_key(so, key);
1719 if (rv == -1) {
1720 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1721 return NULL;
1722 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001723 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1724 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001725 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001726 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001727 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001728 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001729 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001730 return result;
1731 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001732 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001733}
1734
1735PyDoc_STRVAR(discard_doc,
1736"Remove an element from a set if it is a member.\n\
1737\n\
1738If the element is not a member, do nothing.");
1739
1740static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001741set_reduce(PySetObject *so)
1742{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001743 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001744
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001745 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001746 if (keys == NULL)
1747 goto done;
1748 args = PyTuple_Pack(1, keys);
1749 if (args == NULL)
1750 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001751 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1752 if (dict == NULL) {
1753 PyErr_Clear();
1754 dict = Py_None;
1755 Py_INCREF(dict);
1756 }
1757 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001758done:
1759 Py_XDECREF(args);
1760 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001761 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001762 return result;
1763}
1764
1765PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1766
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001767static int
1768set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1769{
1770 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001771
1772 if (!PyAnySet_Check(self))
1773 return -1;
1774 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1775 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001776 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001777 self->hash = -1;
1778 if (iterable == NULL)
1779 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001780 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001781}
1782
Raymond Hettingera690a992003-11-16 16:17:49 +00001783static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001784 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001785 0, /* sq_concat */
1786 0, /* sq_repeat */
1787 0, /* sq_item */
1788 0, /* sq_slice */
1789 0, /* sq_ass_item */
1790 0, /* sq_ass_slice */
1791 (objobjproc)set_contains, /* sq_contains */
1792};
1793
1794/* set object ********************************************************/
1795
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001796#ifdef Py_DEBUG
1797static PyObject *test_c_api(PySetObject *so);
1798
1799PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1800All is well if assertions don't fail.");
1801#endif
1802
Raymond Hettingera690a992003-11-16 16:17:49 +00001803static PyMethodDef set_methods[] = {
1804 {"add", (PyCFunction)set_add, METH_O,
1805 add_doc},
1806 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1807 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001808 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001809 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001810 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1811 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001812 {"discard", (PyCFunction)set_discard, METH_O,
1813 discard_doc},
1814 {"difference", (PyCFunction)set_difference, METH_O,
1815 difference_doc},
1816 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1817 difference_update_doc},
1818 {"intersection",(PyCFunction)set_intersection, METH_O,
1819 intersection_doc},
1820 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1821 intersection_update_doc},
1822 {"issubset", (PyCFunction)set_issubset, METH_O,
1823 issubset_doc},
1824 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1825 issuperset_doc},
1826 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1827 pop_doc},
1828 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1829 reduce_doc},
1830 {"remove", (PyCFunction)set_remove, METH_O,
1831 remove_doc},
1832 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1833 symmetric_difference_doc},
1834 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1835 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001836#ifdef Py_DEBUG
1837 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1838 test_c_api_doc},
1839#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001840 {"union", (PyCFunction)set_union, METH_O,
1841 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001842 {"update", (PyCFunction)set_update, METH_O,
1843 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001844 {NULL, NULL} /* sentinel */
1845};
1846
1847static PyNumberMethods set_as_number = {
1848 0, /*nb_add*/
1849 (binaryfunc)set_sub, /*nb_subtract*/
1850 0, /*nb_multiply*/
1851 0, /*nb_divide*/
1852 0, /*nb_remainder*/
1853 0, /*nb_divmod*/
1854 0, /*nb_power*/
1855 0, /*nb_negative*/
1856 0, /*nb_positive*/
1857 0, /*nb_absolute*/
1858 0, /*nb_nonzero*/
1859 0, /*nb_invert*/
1860 0, /*nb_lshift*/
1861 0, /*nb_rshift*/
1862 (binaryfunc)set_and, /*nb_and*/
1863 (binaryfunc)set_xor, /*nb_xor*/
1864 (binaryfunc)set_or, /*nb_or*/
1865 0, /*nb_coerce*/
1866 0, /*nb_int*/
1867 0, /*nb_long*/
1868 0, /*nb_float*/
1869 0, /*nb_oct*/
1870 0, /*nb_hex*/
1871 0, /*nb_inplace_add*/
1872 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1873 0, /*nb_inplace_multiply*/
1874 0, /*nb_inplace_divide*/
1875 0, /*nb_inplace_remainder*/
1876 0, /*nb_inplace_power*/
1877 0, /*nb_inplace_lshift*/
1878 0, /*nb_inplace_rshift*/
1879 (binaryfunc)set_iand, /*nb_inplace_and*/
1880 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1881 (binaryfunc)set_ior, /*nb_inplace_or*/
1882};
1883
1884PyDoc_STRVAR(set_doc,
1885"set(iterable) --> set object\n\
1886\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001887Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001888
1889PyTypeObject PySet_Type = {
1890 PyObject_HEAD_INIT(&PyType_Type)
1891 0, /* ob_size */
1892 "set", /* tp_name */
1893 sizeof(PySetObject), /* tp_basicsize */
1894 0, /* tp_itemsize */
1895 /* methods */
1896 (destructor)set_dealloc, /* tp_dealloc */
1897 (printfunc)set_tp_print, /* tp_print */
1898 0, /* tp_getattr */
1899 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001900 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001901 (reprfunc)set_repr, /* tp_repr */
1902 &set_as_number, /* tp_as_number */
1903 &set_as_sequence, /* tp_as_sequence */
1904 0, /* tp_as_mapping */
1905 set_nohash, /* tp_hash */
1906 0, /* tp_call */
1907 0, /* tp_str */
1908 PyObject_GenericGetAttr, /* tp_getattro */
1909 0, /* tp_setattro */
1910 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001911 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001912 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001913 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001914 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001915 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001916 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001917 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001918 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001919 0, /* tp_iternext */
1920 set_methods, /* tp_methods */
1921 0, /* tp_members */
1922 0, /* tp_getset */
1923 0, /* tp_base */
1924 0, /* tp_dict */
1925 0, /* tp_descr_get */
1926 0, /* tp_descr_set */
1927 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001928 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001929 PyType_GenericAlloc, /* tp_alloc */
1930 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001931 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001932};
1933
1934/* frozenset object ********************************************************/
1935
1936
1937static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001938 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001939 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001940 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001941 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001942 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001943 difference_doc},
1944 {"intersection",(PyCFunction)set_intersection, METH_O,
1945 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001946 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001947 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001948 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001949 issuperset_doc},
1950 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1951 reduce_doc},
1952 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1953 symmetric_difference_doc},
1954 {"union", (PyCFunction)set_union, METH_O,
1955 union_doc},
1956 {NULL, NULL} /* sentinel */
1957};
1958
1959static PyNumberMethods frozenset_as_number = {
1960 0, /*nb_add*/
1961 (binaryfunc)set_sub, /*nb_subtract*/
1962 0, /*nb_multiply*/
1963 0, /*nb_divide*/
1964 0, /*nb_remainder*/
1965 0, /*nb_divmod*/
1966 0, /*nb_power*/
1967 0, /*nb_negative*/
1968 0, /*nb_positive*/
1969 0, /*nb_absolute*/
1970 0, /*nb_nonzero*/
1971 0, /*nb_invert*/
1972 0, /*nb_lshift*/
1973 0, /*nb_rshift*/
1974 (binaryfunc)set_and, /*nb_and*/
1975 (binaryfunc)set_xor, /*nb_xor*/
1976 (binaryfunc)set_or, /*nb_or*/
1977};
1978
1979PyDoc_STRVAR(frozenset_doc,
1980"frozenset(iterable) --> frozenset object\n\
1981\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001982Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001983
1984PyTypeObject PyFrozenSet_Type = {
1985 PyObject_HEAD_INIT(&PyType_Type)
1986 0, /* ob_size */
1987 "frozenset", /* tp_name */
1988 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001989 0, /* tp_itemsize */
1990 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001991 (destructor)set_dealloc, /* tp_dealloc */
1992 (printfunc)set_tp_print, /* tp_print */
1993 0, /* tp_getattr */
1994 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001995 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001996 (reprfunc)set_repr, /* tp_repr */
1997 &frozenset_as_number, /* tp_as_number */
1998 &set_as_sequence, /* tp_as_sequence */
1999 0, /* tp_as_mapping */
2000 frozenset_hash, /* tp_hash */
2001 0, /* tp_call */
2002 0, /* tp_str */
2003 PyObject_GenericGetAttr, /* tp_getattro */
2004 0, /* tp_setattro */
2005 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002006 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002007 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002008 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002009 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002010 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002011 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002012 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002013 (getiterfunc)set_iter, /* tp_iter */
2014 0, /* tp_iternext */
2015 frozenset_methods, /* tp_methods */
2016 0, /* tp_members */
2017 0, /* tp_getset */
2018 0, /* tp_base */
2019 0, /* tp_dict */
2020 0, /* tp_descr_get */
2021 0, /* tp_descr_set */
2022 0, /* tp_dictoffset */
2023 0, /* tp_init */
2024 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002025 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002026 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002027};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002028
2029
2030/***** C API functions *************************************************/
2031
2032PyObject *
2033PySet_New(PyObject *iterable)
2034{
2035 return make_new_set(&PySet_Type, iterable);
2036}
2037
2038PyObject *
2039PyFrozenSet_New(PyObject *iterable)
2040{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002041 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002042
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002043 if (iterable == NULL)
2044 args = PyTuple_New(0);
2045 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002046 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002047 if (args == NULL)
2048 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002049 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002050 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002051 return result;
2052}
2053
Neal Norwitz8c49c822006-03-04 18:41:19 +00002054Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002055PySet_Size(PyObject *anyset)
2056{
2057 if (!PyAnySet_Check(anyset)) {
2058 PyErr_BadInternalCall();
2059 return -1;
2060 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002061 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002062}
2063
2064int
Barry Warsaw176014f2006-03-30 22:45:35 +00002065PySet_Clear(PyObject *set)
2066{
2067 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2068 PyErr_BadInternalCall();
2069 return -1;
2070 }
2071 return set_clear_internal((PySetObject *)set);
2072}
2073
2074int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002075PySet_Contains(PyObject *anyset, PyObject *key)
2076{
2077 if (!PyAnySet_Check(anyset)) {
2078 PyErr_BadInternalCall();
2079 return -1;
2080 }
2081 return set_contains_key((PySetObject *)anyset, key);
2082}
2083
2084int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002085PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002086{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002087 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002088 PyErr_BadInternalCall();
2089 return -1;
2090 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002091 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002092}
2093
2094int
2095PySet_Add(PyObject *set, PyObject *key)
2096{
2097 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2098 PyErr_BadInternalCall();
2099 return -1;
2100 }
2101 return set_add_key((PySetObject *)set, key);
2102}
2103
Barry Warsaw176014f2006-03-30 22:45:35 +00002104int
2105_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2106{
2107 setentry *entry_ptr;
2108
2109 if (!PyAnySet_Check(set)) {
2110 PyErr_BadInternalCall();
2111 return -1;
2112 }
2113 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2114 return 0;
2115 *entry = entry_ptr->key;
2116 return 1;
2117}
2118
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002119PyObject *
2120PySet_Pop(PyObject *set)
2121{
2122 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2123 PyErr_BadInternalCall();
2124 return NULL;
2125 }
2126 return set_pop((PySetObject *)set);
2127}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002128
Barry Warsaw176014f2006-03-30 22:45:35 +00002129int
2130_PySet_Update(PyObject *set, PyObject *iterable)
2131{
2132 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2133 PyErr_BadInternalCall();
2134 return -1;
2135 }
2136 return set_update_internal((PySetObject *)set, iterable);
2137}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002138
2139#ifdef Py_DEBUG
2140
2141/* Test code to be called with any three element set.
2142 Returns True and original set is restored. */
2143
2144#define assertRaises(call_return_value, exception) \
2145 do { \
2146 assert(call_return_value); \
2147 assert(PyErr_ExceptionMatches(exception)); \
2148 PyErr_Clear(); \
2149 } while(0)
2150
2151static PyObject *
2152test_c_api(PySetObject *so)
2153{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002154 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002155 char *s;
2156 Py_ssize_t i;
2157 PyObject *elem, *dup, *t, *f, *dup2;
2158 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002159
2160 /* Verify preconditions and exercise type/size checks */
2161 assert(PyAnySet_Check(ob));
2162 assert(PyAnySet_CheckExact(ob));
2163 assert(!PyFrozenSet_CheckExact(ob));
2164 assert(PySet_Size(ob) == 3);
2165 assert(PySet_GET_SIZE(ob) == 3);
2166
2167 /* Raise TypeError for non-iterable constructor arguments */
2168 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2169 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2170
2171 /* Raise TypeError for unhashable key */
2172 dup = PySet_New(ob);
2173 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2174 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2175 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2176
2177 /* Exercise successful pop, contains, add, and discard */
2178 elem = PySet_Pop(ob);
2179 assert(PySet_Contains(ob, elem) == 0);
2180 assert(PySet_GET_SIZE(ob) == 2);
2181 assert(PySet_Add(ob, elem) == 0);
2182 assert(PySet_Contains(ob, elem) == 1);
2183 assert(PySet_GET_SIZE(ob) == 3);
2184 assert(PySet_Discard(ob, elem) == 1);
2185 assert(PySet_GET_SIZE(ob) == 2);
2186 assert(PySet_Discard(ob, elem) == 0);
2187 assert(PySet_GET_SIZE(ob) == 2);
2188
Barry Warsaw176014f2006-03-30 22:45:35 +00002189 /* Exercise clear */
2190 dup2 = PySet_New(dup);
2191 assert(PySet_Clear(dup2) == 0);
2192 assert(PySet_Size(dup2) == 0);
2193 Py_DECREF(dup2);
2194
2195 /* Raise SystemError on clear or update of frozen set */
2196 f = PyFrozenSet_New(dup);
2197 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2198 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2199 Py_DECREF(f);
2200
2201 /* Exercise direct iteration */
2202 i = 0, count = 0;
2203 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2204 s = PyString_AsString(elem);
2205 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2206 count++;
2207 }
2208 assert(count == 3);
2209
2210 /* Exercise updates */
2211 dup2 = PySet_New(NULL);
2212 assert(_PySet_Update(dup2, dup) == 0);
2213 assert(PySet_Size(dup2) == 3);
2214 assert(_PySet_Update(dup2, dup) == 0);
2215 assert(PySet_Size(dup2) == 3);
2216 Py_DECREF(dup2);
2217
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002218 /* Raise SystemError when self argument is not a set or frozenset. */
2219 t = PyTuple_New(0);
2220 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2221 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2222 Py_DECREF(t);
2223
2224 /* Raise SystemError when self argument is not a set. */
2225 f = PyFrozenSet_New(dup);
2226 assert(PySet_Size(f) == 3);
2227 assert(PyFrozenSet_CheckExact(f));
2228 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2229 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2230 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2231 Py_DECREF(f);
2232
2233 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002234 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2235 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002236 assert(PySet_GET_SIZE(ob) == 0);
2237 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2238
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002239 /* Restore the set from the copy using the PyNumber API */
2240 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2241 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002242
2243 /* Verify constructors accept NULL arguments */
2244 f = PySet_New(NULL);
2245 assert(f != NULL);
2246 assert(PySet_GET_SIZE(f) == 0);
2247 Py_DECREF(f);
2248 f = PyFrozenSet_New(NULL);
2249 assert(f != NULL);
2250 assert(PyFrozenSet_CheckExact(f));
2251 assert(PySet_GET_SIZE(f) == 0);
2252 Py_DECREF(f);
2253
2254 Py_DECREF(elem);
2255 Py_DECREF(dup);
2256 Py_RETURN_TRUE;
2257}
2258
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002259#undef assertRaises
2260
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002261#endif