blob: a55bbb70adb950228448ed7e6ea2b5097dd42d03 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis68192102007-07-21 06:55:02 +00006 Copyright (c) 2003-2007 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
19 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
25}
26
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000027/* This must be >= 1. */
28#define PERTURB_SHIFT 5
29
30/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000031static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032
Armin Rigoe1709372006-04-12 17:06:05 +000033#ifdef Py_REF_DEBUG
34PyObject *
35_PySet_Dummy(void)
36{
37 return dummy;
38}
39#endif
40
Raymond Hettingerbc841a12005-08-07 13:02:53 +000041#define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
44 (so)->hash = -1; \
45 } while(0)
46
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047#define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000050 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051 } while(0)
52
Raymond Hettingerbc841a12005-08-07 13:02:53 +000053/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes5b970ad2008-02-06 13:33:44 +000054#ifndef PySet_MAXFREELIST
55#define PySet_MAXFREELIST 80
56#endif
57static PySetObject *free_list[PySet_MAXFREELIST];
58static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000059
60/*
61The basic lookup function used by all operations.
62This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
63Open addressing is preferred over chaining since the link overhead for
64chaining would be substantial (100% with typical malloc overhead).
65
66The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000067probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000068
69All arithmetic on hash should ignore overflow.
70
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000071Unlike the dictionary implementation, the lookkey functions can return
72NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073*/
74
75static setentry *
76set_lookkey(PySetObject *so, PyObject *key, register long hash)
77{
Martin v. Löwis18e16552006-02-15 17:27:45 +000078 register Py_ssize_t i;
79 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000080 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +000081 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000082 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000083 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000084 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000085 PyObject *startkey;
86
87 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000088 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000089 if (entry->key == NULL || entry->key == key)
90 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000091
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000092 if (entry->key == dummy)
93 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000094 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000095 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000096 startkey = entry->key;
Raymond Hettingerd99bee72008-05-30 06:49:47 +000097 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000098 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Raymond Hettingerd99bee72008-05-30 06:49:47 +000099 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000100 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000101 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000102 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000103 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000104 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000105 }
106 else {
107 /* The compare did major nasty stuff to the
108 * set: start over.
109 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000110 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000111 }
112 }
113 freeslot = NULL;
114 }
115
116 /* In the loop, key == dummy is by far (factor of 100s) the
117 least likely outcome, so test for that last. */
118 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
119 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000120 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000121 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000123 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 break;
125 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000126 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000127 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000128 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000129 startkey = entry->key;
Raymond Hettingerd99bee72008-05-30 06:49:47 +0000130 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000131 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Raymond Hettingerd99bee72008-05-30 06:49:47 +0000132 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000133 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000134 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000135 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000136 if (cmp > 0)
137 break;
138 }
139 else {
140 /* The compare did major nasty stuff to the
141 * set: start over.
142 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000143 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000144 }
145 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000146 else if (entry->key == dummy && freeslot == NULL)
147 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000148 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000149 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150}
151
152/*
153 * Hacked up version of set_lookkey which can assume keys are always strings;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000154 * This means we can always use _PyString_Eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000155 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000156 */
157static setentry *
158set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
159{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000160 register Py_ssize_t i;
161 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000162 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000163 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000164 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000165 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000166
167 /* Make sure this function doesn't have to handle non-string keys,
168 including subclasses of str; e.g., one reason to subclass
169 strings is to override __eq__, and for speed we don't cater to
170 that here. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000171 if (!PyString_CheckExact(key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000172 so->lookup = set_lookkey;
173 return set_lookkey(so, key, hash);
174 }
175 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000176 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000177 if (entry->key == NULL || entry->key == key)
178 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000179 if (entry->key == dummy)
180 freeslot = entry;
181 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000182 if (entry->hash == hash && _PyString_Eq(entry->key, key))
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000183 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000184 freeslot = NULL;
185 }
186
187 /* In the loop, key == dummy is by far (factor of 100s) the
188 least likely outcome, so test for that last. */
189 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
190 i = (i << 2) + i + perturb + 1;
191 entry = &table[i & mask];
192 if (entry->key == NULL)
193 return freeslot == NULL ? entry : freeslot;
194 if (entry->key == key
195 || (entry->hash == hash
196 && entry->key != dummy
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000197 && _PyString_Eq(entry->key, key)))
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000198 return entry;
199 if (entry->key == dummy && freeslot == NULL)
200 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000201 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000202 assert(0); /* NOT REACHED */
203 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000204}
205
206/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000207Internal routine to insert a new key into the table.
Raymond Hettinger0c850862006-12-08 04:24:33 +0000208Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209Eats a reference to key.
210*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000211static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000212set_insert_key(register PySetObject *so, PyObject *key, long hash)
213{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000214 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000215 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
216
217 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000218 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000219 if (entry == NULL)
220 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000221 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 /* UNUSED */
223 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000224 entry->key = key;
225 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000226 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000227 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000228 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000229 entry->key = key;
230 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000231 so->used++;
232 Py_DECREF(dummy);
233 } else {
234 /* ACTIVE */
235 Py_DECREF(key);
236 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000237 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000238}
239
240/*
Raymond Hettinger0c850862006-12-08 04:24:33 +0000241Internal routine used by set_table_resize() to insert an item which is
242known to be absent from the set. This routine also assumes that
243the set contains no deleted entries. Besides the performance benefit,
244using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
245Note that no refcounts are changed by this routine; if needed, the caller
246is responsible for incref'ing `key`.
247*/
248static void
249set_insert_clean(register PySetObject *so, PyObject *key, long hash)
250{
251 register size_t i;
252 register size_t perturb;
253 register size_t mask = (size_t)so->mask;
254 setentry *table = so->table;
255 register setentry *entry;
256
257 i = hash & mask;
258 entry = &table[i];
259 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
260 i = (i << 2) + i + perturb + 1;
261 entry = &table[i & mask];
262 }
263 so->fill++;
264 entry->key = key;
265 entry->hash = hash;
266 so->used++;
267}
268
269/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000271keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272actually be smaller than the old one.
273*/
274static int
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000275set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000276{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000277 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000278 setentry *oldtable, *newtable, *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000279 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000280 int is_oldtable_malloced;
281 setentry small_copy[PySet_MINSIZE];
282
283 assert(minused >= 0);
284
285 /* Find the smallest table size > minused. */
286 for (newsize = PySet_MINSIZE;
287 newsize <= minused && newsize > 0;
288 newsize <<= 1)
289 ;
290 if (newsize <= 0) {
291 PyErr_NoMemory();
292 return -1;
293 }
294
295 /* Get space for a new table. */
296 oldtable = so->table;
297 assert(oldtable != NULL);
298 is_oldtable_malloced = oldtable != so->smalltable;
299
300 if (newsize == PySet_MINSIZE) {
301 /* A large table is shrinking, or we can't get any smaller. */
302 newtable = so->smalltable;
303 if (newtable == oldtable) {
304 if (so->fill == so->used) {
305 /* No dummies, so no point doing anything. */
306 return 0;
307 }
308 /* We're not going to resize it, but rebuild the
309 table anyway to purge old dummy entries.
310 Subtle: This is *necessary* if fill==size,
311 as set_lookkey needs at least one virgin slot to
312 terminate failing searches. If fill < size, it's
313 merely desirable, as dummies slow searches. */
314 assert(so->fill > so->used);
315 memcpy(small_copy, oldtable, sizeof(small_copy));
316 oldtable = small_copy;
317 }
318 }
319 else {
320 newtable = PyMem_NEW(setentry, newsize);
321 if (newtable == NULL) {
322 PyErr_NoMemory();
323 return -1;
324 }
325 }
326
327 /* Make the set empty, using the new table. */
328 assert(newtable != oldtable);
329 so->table = newtable;
330 so->mask = newsize - 1;
331 memset(newtable, 0, sizeof(setentry) * newsize);
332 so->used = 0;
333 i = so->fill;
334 so->fill = 0;
335
336 /* Copy the data over; this is refcount-neutral for active entries;
337 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000338 for (entry = oldtable; i > 0; entry++) {
339 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000340 /* UNUSED */
341 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000342 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000343 /* DUMMY */
344 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000345 assert(entry->key == dummy);
346 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347 } else {
348 /* ACTIVE */
349 --i;
Raymond Hettinger0c850862006-12-08 04:24:33 +0000350 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000351 }
352 }
353
354 if (is_oldtable_malloced)
355 PyMem_DEL(oldtable);
356 return 0;
357}
358
Raymond Hettingerc991db22005-08-11 07:58:45 +0000359/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
360
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000361static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000362set_add_entry(register PySetObject *so, setentry *entry)
363{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000364 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000365
366 assert(so->fill <= so->mask); /* at least one empty slot */
367 n_used = so->used;
368 Py_INCREF(entry->key);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000369 if (set_insert_key(so, entry->key, entry->hash) == -1) {
370 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000371 return -1;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000372 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000373 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
374 return 0;
375 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
376}
377
378static int
379set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380{
381 register long hash;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000382 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000383
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000384 if (!PyString_CheckExact(key) ||
385 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386 hash = PyObject_Hash(key);
387 if (hash == -1)
388 return -1;
389 }
390 assert(so->fill <= so->mask); /* at least one empty slot */
391 n_used = so->used;
392 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000393 if (set_insert_key(so, key, hash) == -1) {
394 Py_DECREF(key);
395 return -1;
396 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
398 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000399 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000400}
401
402#define DISCARD_NOTFOUND 0
403#define DISCARD_FOUND 1
404
405static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000406set_discard_entry(PySetObject *so, setentry *oldentry)
407{ register setentry *entry;
408 PyObject *old_key;
409
410 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000411 if (entry == NULL)
412 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000413 if (entry->key == NULL || entry->key == dummy)
414 return DISCARD_NOTFOUND;
415 old_key = entry->key;
416 Py_INCREF(dummy);
417 entry->key = dummy;
418 so->used--;
419 Py_DECREF(old_key);
420 return DISCARD_FOUND;
421}
422
423static int
424set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000425{
426 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000427 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000428 PyObject *old_key;
429
430 assert (PyAnySet_Check(so));
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000431 if (!PyString_CheckExact(key) ||
432 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000433 hash = PyObject_Hash(key);
434 if (hash == -1)
435 return -1;
436 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000437 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000438 if (entry == NULL)
439 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000440 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000441 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000442 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000444 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445 so->used--;
446 Py_DECREF(old_key);
447 return DISCARD_FOUND;
448}
449
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000450static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451set_clear_internal(PySetObject *so)
452{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000453 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454 int table_is_malloced;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000455 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456 setentry small_copy[PySet_MINSIZE];
457#ifdef Py_DEBUG
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000458 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000459 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000460
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000461 n = so->mask + 1;
462 i = 0;
463#endif
464
465 table = so->table;
466 assert(table != NULL);
467 table_is_malloced = table != so->smalltable;
468
469 /* This is delicate. During the process of clearing the set,
470 * decrefs can cause the set to mutate. To avoid fatal confusion
471 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000472 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473 * clearing.
474 */
475 fill = so->fill;
476 if (table_is_malloced)
477 EMPTY_TO_MINSIZE(so);
478
479 else if (fill > 0) {
480 /* It's a small table with something that needs to be cleared.
481 * Afraid the only safe way is to copy the set entries into
482 * another small table first.
483 */
484 memcpy(small_copy, table, sizeof(small_copy));
485 table = small_copy;
486 EMPTY_TO_MINSIZE(so);
487 }
488 /* else it's a small table that's already empty */
489
490 /* Now we can finally clear things. If C had refcounts, we could
491 * assert that the refcount on table is 1 now, i.e. that this function
492 * has unique access to it, so decref side-effects can't alter it.
493 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000494 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495#ifdef Py_DEBUG
496 assert(i < n);
497 ++i;
498#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000499 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000501 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502 }
503#ifdef Py_DEBUG
504 else
Raymond Hettinger334b5b22006-03-26 03:11:29 +0000505 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506#endif
507 }
508
509 if (table_is_malloced)
510 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000511 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512}
513
514/*
515 * Iterate over a set table. Use like so:
516 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000517 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000518 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000519 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000520 * while (set_next(yourset, &pos, &entry)) {
521 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522 * }
523 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000524 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525 * mutates the table.
526 */
527static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000528set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000530 Py_ssize_t i;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000531 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000532 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000533
534 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000536 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000537 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000538 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000539 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000541 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000542 if (i > mask)
543 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000544 assert(table[i].key != NULL);
545 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000546 return 1;
547}
548
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000549static void
550set_dealloc(PySetObject *so)
551{
552 register setentry *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000553 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000554 PyObject_GC_UnTrack(so);
555 Py_TRASHCAN_SAFE_BEGIN(so)
556 if (so->weakreflist != NULL)
557 PyObject_ClearWeakRefs((PyObject *) so);
558
559 for (entry = so->table; fill > 0; entry++) {
560 if (entry->key) {
561 --fill;
562 Py_DECREF(entry->key);
563 }
564 }
565 if (so->table != so->smalltable)
566 PyMem_DEL(so->table);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000567 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
568 free_list[numfree++] = so;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000569 else
Christian Heimese93237d2007-12-19 02:37:44 +0000570 Py_TYPE(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000571 Py_TRASHCAN_SAFE_END(so)
572}
573
574static int
575set_tp_print(PySetObject *so, FILE *fp, int flags)
576{
577 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000578 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000579 char *emit = ""; /* No separator emitted on first pass */
580 char *separator = ", ";
Raymond Hettinger53999102006-12-30 04:01:17 +0000581 int status = Py_ReprEnter((PyObject*)so);
582
583 if (status != 0) {
584 if (status < 0)
585 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000586 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000587 fprintf(fp, "%s(...)", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000588 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000589 return 0;
590 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591
Brett Cannon01531592007-09-17 03:28:34 +0000592 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000593 fprintf(fp, "%s([", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000594 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000595 while (set_next(so, &pos, &entry)) {
Brett Cannon01531592007-09-17 03:28:34 +0000596 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000597 fputs(emit, fp);
Brett Cannon01531592007-09-17 03:28:34 +0000598 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000599 emit = separator;
Raymond Hettinger53999102006-12-30 04:01:17 +0000600 if (PyObject_Print(entry->key, fp, 0) != 0) {
601 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000602 return -1;
Raymond Hettinger53999102006-12-30 04:01:17 +0000603 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000604 }
Brett Cannon01531592007-09-17 03:28:34 +0000605 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000606 fputs("])", fp);
Brett Cannon01531592007-09-17 03:28:34 +0000607 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000608 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000609 return 0;
610}
611
612static PyObject *
613set_repr(PySetObject *so)
614{
Raymond Hettinger53999102006-12-30 04:01:17 +0000615 PyObject *keys, *result=NULL, *listrepr;
616 int status = Py_ReprEnter((PyObject*)so);
617
618 if (status != 0) {
619 if (status < 0)
620 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000621 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
Raymond Hettinger53999102006-12-30 04:01:17 +0000622 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000623
624 keys = PySequence_List((PyObject *)so);
625 if (keys == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000626 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000627 listrepr = PyObject_Repr(keys);
628 Py_DECREF(keys);
629 if (listrepr == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000630 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000631
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000632 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
633 PyString_AS_STRING(listrepr));
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000634 Py_DECREF(listrepr);
Raymond Hettinger53999102006-12-30 04:01:17 +0000635done:
636 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000637 return result;
638}
639
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000641set_len(PyObject *so)
642{
643 return ((PySetObject *)so)->used;
644}
645
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000646static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000647set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000649 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000650 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000651 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000652
653 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000654 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000655
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000656 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000657 if (other == so || other->used == 0)
658 /* a.update(a) or a.update({}); nothing to do */
659 return 0;
660 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000661 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000662 * that there will be no (or few) overlapping keys.
663 */
664 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
665 if (set_table_resize(so, (so->used + other->used)*2) != 0)
666 return -1;
667 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000668 for (i = 0; i <= other->mask; i++) {
669 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670 if (entry->key != NULL &&
671 entry->key != dummy) {
672 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000673 if (set_insert_key(so, entry->key, entry->hash) == -1) {
674 Py_DECREF(entry->key);
675 return -1;
676 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000677 }
678 }
679 return 0;
680}
681
682static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000683set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000684{
685 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000686 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000687
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000688 if (!PyString_CheckExact(key) ||
689 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000690 hash = PyObject_Hash(key);
691 if (hash == -1)
692 return -1;
693 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000694 entry = (so->lookup)(so, key, hash);
695 if (entry == NULL)
696 return -1;
697 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000698 return key != NULL && key != dummy;
699}
700
Raymond Hettingerc991db22005-08-11 07:58:45 +0000701static int
702set_contains_entry(PySetObject *so, setentry *entry)
703{
704 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000705 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000706
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000707 lu_entry = (so->lookup)(so, entry->key, entry->hash);
708 if (lu_entry == NULL)
709 return -1;
710 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000711 return key != NULL && key != dummy;
712}
713
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000714static PyObject *
715set_pop(PySetObject *so)
716{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000717 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000718 register setentry *entry;
719 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000720
721 assert (PyAnySet_Check(so));
722 if (so->used == 0) {
723 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
724 return NULL;
725 }
726
727 /* Set entry to "the first" unused or dummy set entry. We abuse
728 * the hash field of slot 0 to hold a search finger:
729 * If slot 0 has a value, use slot 0.
730 * Else slot 0 is being used to hold a search finger,
731 * and we use its hash value as the first index to look.
732 */
733 entry = &so->table[0];
734 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000735 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000736 /* The hash field may be a real hash value, or it may be a
737 * legit search finger, or it may be a once-legit search
738 * finger that's out of bounds now because it wrapped around
739 * or the table shrunk -- simply make sure it's in bounds now.
740 */
741 if (i > so->mask || i < 1)
742 i = 1; /* skip slot 0 */
743 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
744 i++;
745 if (i > so->mask)
746 i = 1;
747 }
748 }
749 key = entry->key;
750 Py_INCREF(dummy);
751 entry->key = dummy;
752 so->used--;
753 so->table[0].hash = i + 1; /* next place to start */
754 return key;
755}
756
Andrew M. Kuchlingd7b7dde2008-10-03 16:29:19 +0000757PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
758Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000759
760static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000761set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000762{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000763 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000764 setentry *entry;
765
766 while (set_next(so, &pos, &entry))
767 Py_VISIT(entry->key);
768 return 0;
769}
770
771static long
772frozenset_hash(PyObject *self)
773{
774 PySetObject *so = (PySetObject *)self;
775 long h, hash = 1927868237L;
776 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000777 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000778
779 if (so->hash != -1)
780 return so->hash;
781
782 hash *= PySet_GET_SIZE(self) + 1;
783 while (set_next(so, &pos, &entry)) {
784 /* Work to increase the bit dispersion for closely spaced hash
785 values. The is important because some use cases have many
786 combinations of a small number of elements with nearby
787 hashes so that many distinct combinations collapse to only
788 a handful of distinct hash values. */
789 h = entry->hash;
790 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
791 }
792 hash = hash * 69069L + 907133923L;
793 if (hash == -1)
794 hash = 590923713L;
795 so->hash = hash;
796 return hash;
797}
798
Raymond Hettingera9d99362005-08-05 00:01:15 +0000799/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801typedef struct {
802 PyObject_HEAD
803 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000804 Py_ssize_t si_used;
805 Py_ssize_t si_pos;
806 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807} setiterobject;
808
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809static void
810setiter_dealloc(setiterobject *si)
811{
812 Py_XDECREF(si->si_set);
Antoine Pitrouaa687902009-01-01 14:11:22 +0000813 PyObject_GC_Del(si);
814}
815
816static int
817setiter_traverse(setiterobject *si, visitproc visit, void *arg)
818{
819 Py_VISIT(si->si_set);
820 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000821}
822
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000823static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824setiter_len(setiterobject *si)
825{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000826 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000827 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000828 len = si->len;
829 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000830}
831
Armin Rigof5b3e362006-02-11 21:32:43 +0000832PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000833
834static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000835 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000836 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837};
838
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000839static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000840{
841 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000842 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000843 register setentry *entry;
844 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000845
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000846 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000848 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000849
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000850 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000851 PyErr_SetString(PyExc_RuntimeError,
852 "Set changed size during iteration");
853 si->si_used = -1; /* Make this state sticky */
854 return NULL;
855 }
856
857 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000858 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000859 entry = so->table;
860 mask = so->mask;
861 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000862 i++;
863 si->si_pos = i+1;
864 if (i > mask)
865 goto fail;
866 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000867 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868 Py_INCREF(key);
869 return key;
870
871fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000872 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000873 si->si_set = NULL;
874 return NULL;
875}
876
Hye-Shik Change2956762005-08-01 05:26:41 +0000877static PyTypeObject PySetIter_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000878 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000879 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880 sizeof(setiterobject), /* tp_basicsize */
881 0, /* tp_itemsize */
882 /* methods */
883 (destructor)setiter_dealloc, /* tp_dealloc */
884 0, /* tp_print */
885 0, /* tp_getattr */
886 0, /* tp_setattr */
887 0, /* tp_compare */
888 0, /* tp_repr */
889 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000890 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891 0, /* tp_as_mapping */
892 0, /* tp_hash */
893 0, /* tp_call */
894 0, /* tp_str */
895 PyObject_GenericGetAttr, /* tp_getattro */
896 0, /* tp_setattro */
897 0, /* tp_as_buffer */
Antoine Pitrouaa687902009-01-01 14:11:22 +0000898 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000899 0, /* tp_doc */
Antoine Pitrouaa687902009-01-01 14:11:22 +0000900 (traverseproc)setiter_traverse, /* tp_traverse */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000901 0, /* tp_clear */
902 0, /* tp_richcompare */
903 0, /* tp_weaklistoffset */
904 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000905 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000906 setiter_methods, /* tp_methods */
907 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000908};
909
Martin v. Löwis72d20672006-04-11 09:04:12 +0000910static PyObject *
911set_iter(PySetObject *so)
912{
Antoine Pitrouaa687902009-01-01 14:11:22 +0000913 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
Martin v. Löwis72d20672006-04-11 09:04:12 +0000914 if (si == NULL)
915 return NULL;
916 Py_INCREF(so);
917 si->si_set = so;
918 si->si_used = so->used;
919 si->si_pos = 0;
920 si->len = so->used;
Antoine Pitrouaa687902009-01-01 14:11:22 +0000921 _PyObject_GC_TRACK(si);
Martin v. Löwis72d20672006-04-11 09:04:12 +0000922 return (PyObject *)si;
923}
924
Raymond Hettingerd7946662005-08-01 21:39:29 +0000925static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000926set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000927{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000928 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000929
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +0000930 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000931 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000932
Raymond Hettingerdb67aef2007-02-01 21:02:59 +0000933 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000934 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000935 Py_ssize_t pos = 0;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000936 long hash;
Raymond Hettinger15cade02007-02-19 20:44:04 +0000937 Py_ssize_t dictsize = PyDict_Size(other);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000938
Raymond Hettinger15cade02007-02-19 20:44:04 +0000939 /* Do one big resize at the start, rather than
940 * incrementally resizing as we insert new keys. Expect
941 * that there will be no (or few) overlapping keys.
942 */
943 if (dictsize == -1)
944 return -1;
945 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
946 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
947 return -1;
948 }
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000949 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
950 setentry an_entry;
951
952 an_entry.hash = hash;
953 an_entry.key = key;
954 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000955 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000956 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000957 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000958 }
959
Raymond Hettingera38123e2003-11-24 22:18:49 +0000960 it = PyObject_GetIter(other);
961 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000962 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000963
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000964 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000965 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000966 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000967 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000968 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000969 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000970 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000971 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000972 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000973 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000974 return -1;
975 return 0;
976}
977
978static PyObject *
Raymond Hettingeree4bcad2008-06-09 08:33:37 +0000979set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000980{
Raymond Hettingeree4bcad2008-06-09 08:33:37 +0000981 Py_ssize_t i;
982
983 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
984 PyObject *other = PyTuple_GET_ITEM(args, i);
985 if (set_update_internal(so, other) == -1)
986 return NULL;
987 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000988 Py_RETURN_NONE;
989}
990
991PyDoc_STRVAR(update_doc,
Raymond Hettingeree4bcad2008-06-09 08:33:37 +0000992"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000993
994static PyObject *
995make_new_set(PyTypeObject *type, PyObject *iterable)
996{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000997 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000998
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000999 if (dummy == NULL) { /* Auto-initialize dummy */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001000 dummy = PyString_FromString("<dummy key>");
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001001 if (dummy == NULL)
1002 return NULL;
1003 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001004
1005 /* create PySetObject structure */
Christian Heimes5b970ad2008-02-06 13:33:44 +00001006 if (numfree &&
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001007 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
Christian Heimes5b970ad2008-02-06 13:33:44 +00001008 so = free_list[--numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001009 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimese93237d2007-12-19 02:37:44 +00001010 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001011 _Py_NewReference((PyObject *)so);
1012 EMPTY_TO_MINSIZE(so);
1013 PyObject_GC_Track(so);
1014 } else {
1015 so = (PySetObject *)type->tp_alloc(type, 0);
1016 if (so == NULL)
1017 return NULL;
1018 /* tp_alloc has already zeroed the structure */
1019 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1020 INIT_NONZERO_SET_SLOTS(so);
1021 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001022
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001023 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001024 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001025
Raymond Hettingera38123e2003-11-24 22:18:49 +00001026 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001027 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001028 Py_DECREF(so);
1029 return NULL;
1030 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001031 }
1032
Raymond Hettingera690a992003-11-16 16:17:49 +00001033 return (PyObject *)so;
1034}
1035
Raymond Hettingerd7946662005-08-01 21:39:29 +00001036/* The empty frozenset is a singleton */
1037static PyObject *emptyfrozenset = NULL;
1038
Raymond Hettingera690a992003-11-16 16:17:49 +00001039static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001040frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001041{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001042 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001043
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001044 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001045 return NULL;
1046
Raymond Hettingera690a992003-11-16 16:17:49 +00001047 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1048 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001049
1050 if (type != &PyFrozenSet_Type)
1051 return make_new_set(type, iterable);
1052
1053 if (iterable != NULL) {
1054 /* frozenset(f) is idempotent */
1055 if (PyFrozenSet_CheckExact(iterable)) {
1056 Py_INCREF(iterable);
1057 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001058 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001059 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001060 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001061 return result;
1062 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001063 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001064 /* The empty frozenset is a singleton */
1065 if (emptyfrozenset == NULL)
1066 emptyfrozenset = make_new_set(type, NULL);
1067 Py_XINCREF(emptyfrozenset);
1068 return emptyfrozenset;
1069}
1070
1071void
1072PySet_Fini(void)
1073{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001074 PySetObject *so;
1075
Christian Heimes5b970ad2008-02-06 13:33:44 +00001076 while (numfree) {
1077 numfree--;
1078 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001079 PyObject_GC_Del(so);
1080 }
Martin v. Löwised8f7832006-04-15 12:47:23 +00001081 Py_CLEAR(dummy);
1082 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001083}
1084
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001085static PyObject *
1086set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1087{
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001088 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001089 return NULL;
1090
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001091 return make_new_set(type, NULL);
1092}
1093
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001094/* set_swap_bodies() switches the contents of any two sets by moving their
1095 internal data pointers and, if needed, copying the internal smalltables.
1096 Semantically equivalent to:
1097
1098 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1099
1100 The function always succeeds and it leaves both objects in a stable state.
1101 Useful for creating temporary frozensets from sets for membership testing
1102 in __contains__(), discard(), and remove(). Also useful for operations
1103 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001104 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001105*/
1106
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001107static void
1108set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001109{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001110 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001111 setentry *u;
1112 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1113 setentry tab[PySet_MINSIZE];
1114 long h;
1115
1116 t = a->fill; a->fill = b->fill; b->fill = t;
1117 t = a->used; a->used = b->used; b->used = t;
1118 t = a->mask; a->mask = b->mask; b->mask = t;
1119
1120 u = a->table;
1121 if (a->table == a->smalltable)
1122 u = b->smalltable;
1123 a->table = b->table;
1124 if (b->table == b->smalltable)
1125 a->table = a->smalltable;
1126 b->table = u;
1127
1128 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1129
1130 if (a->table == a->smalltable || b->table == b->smalltable) {
1131 memcpy(tab, a->smalltable, sizeof(tab));
1132 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1133 memcpy(b->smalltable, tab, sizeof(tab));
1134 }
1135
Christian Heimese93237d2007-12-19 02:37:44 +00001136 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1137 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001138 h = a->hash; a->hash = b->hash; b->hash = h;
1139 } else {
1140 a->hash = -1;
1141 b->hash = -1;
1142 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001143}
1144
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001145static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001146set_copy(PySetObject *so)
1147{
Christian Heimese93237d2007-12-19 02:37:44 +00001148 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001149}
1150
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001151static PyObject *
1152frozenset_copy(PySetObject *so)
1153{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001154 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001155 Py_INCREF(so);
1156 return (PyObject *)so;
1157 }
1158 return set_copy(so);
1159}
1160
Raymond Hettingera690a992003-11-16 16:17:49 +00001161PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1162
1163static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001164set_clear(PySetObject *so)
1165{
1166 set_clear_internal(so);
1167 Py_RETURN_NONE;
1168}
1169
1170PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1171
1172static PyObject *
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001173set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001174{
1175 PySetObject *result;
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00001176 PyObject *other;
1177 Py_ssize_t i;
1178
1179 result = (PySetObject *)set_copy(so);
1180 if (result == NULL)
1181 return NULL;
1182
1183 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1184 other = PyTuple_GET_ITEM(args, i);
1185 if ((PyObject *)so == other)
1186 return (PyObject *)result;
1187 if (set_update_internal(result, other) == -1) {
1188 Py_DECREF(result);
1189 return NULL;
1190 }
1191 }
1192 return (PyObject *)result;
1193}
1194
1195PyDoc_STRVAR(union_doc,
1196 "Return the union of sets as a new set.\n\
1197\n\
1198(i.e. all elements that are in either set.)");
1199
1200static PyObject *
1201set_or(PySetObject *so, PyObject *other)
1202{
1203 PySetObject *result;
1204
1205 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1206 Py_INCREF(Py_NotImplemented);
1207 return Py_NotImplemented;
1208 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001209
1210 result = (PySetObject *)set_copy(so);
1211 if (result == NULL)
1212 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001213 if ((PyObject *)so == other)
1214 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001215 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001216 Py_DECREF(result);
1217 return NULL;
1218 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001219 return (PyObject *)result;
1220}
1221
Raymond Hettingera690a992003-11-16 16:17:49 +00001222static PyObject *
1223set_ior(PySetObject *so, PyObject *other)
1224{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001225 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001226 Py_INCREF(Py_NotImplemented);
1227 return Py_NotImplemented;
1228 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001229 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001230 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001231 Py_INCREF(so);
1232 return (PyObject *)so;
1233}
1234
1235static PyObject *
1236set_intersection(PySetObject *so, PyObject *other)
1237{
1238 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001239 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001240
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001241 if ((PyObject *)so == other)
1242 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001243
Christian Heimese93237d2007-12-19 02:37:44 +00001244 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001245 if (result == NULL)
1246 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001247
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001248 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001249 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001250 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001251
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001252 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001253 tmp = (PyObject *)so;
1254 so = (PySetObject *)other;
1255 other = tmp;
1256 }
1257
Raymond Hettingerc991db22005-08-11 07:58:45 +00001258 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001259 int rv = set_contains_entry(so, entry);
1260 if (rv == -1) {
1261 Py_DECREF(result);
1262 return NULL;
1263 }
1264 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001265 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001266 Py_DECREF(result);
1267 return NULL;
1268 }
1269 }
1270 }
1271 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001272 }
1273
Raymond Hettingera690a992003-11-16 16:17:49 +00001274 it = PyObject_GetIter(other);
1275 if (it == NULL) {
1276 Py_DECREF(result);
1277 return NULL;
1278 }
1279
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001280 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001281 int rv;
1282 setentry entry;
1283 long hash = PyObject_Hash(key);
1284
1285 if (hash == -1) {
1286 Py_DECREF(it);
1287 Py_DECREF(result);
1288 Py_DECREF(key);
1289 return NULL;
1290 }
1291 entry.hash = hash;
1292 entry.key = key;
1293 rv = set_contains_entry(so, &entry);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001294 if (rv == -1) {
1295 Py_DECREF(it);
1296 Py_DECREF(result);
1297 Py_DECREF(key);
1298 return NULL;
1299 }
1300 if (rv) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001301 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001302 Py_DECREF(it);
1303 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001304 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001305 return NULL;
1306 }
1307 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001308 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001309 }
1310 Py_DECREF(it);
1311 if (PyErr_Occurred()) {
1312 Py_DECREF(result);
1313 return NULL;
1314 }
1315 return (PyObject *)result;
1316}
1317
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001318static PyObject *
1319set_intersection_multi(PySetObject *so, PyObject *args)
1320{
1321 Py_ssize_t i;
1322 PyObject *result = (PyObject *)so;
1323
Raymond Hettinger610a93e2008-06-11 00:44:47 +00001324 if (PyTuple_GET_SIZE(args) == 0)
1325 return set_copy(so);
1326
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001327 Py_INCREF(so);
1328 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1329 PyObject *other = PyTuple_GET_ITEM(args, i);
1330 PyObject *newresult = set_intersection((PySetObject *)result, other);
1331 if (newresult == NULL) {
1332 Py_DECREF(result);
1333 return NULL;
1334 }
1335 Py_DECREF(result);
1336 result = newresult;
1337 }
1338 return result;
1339}
1340
Raymond Hettingera690a992003-11-16 16:17:49 +00001341PyDoc_STRVAR(intersection_doc,
1342"Return the intersection of two sets as a new set.\n\
1343\n\
1344(i.e. all elements that are in both sets.)");
1345
1346static PyObject *
1347set_intersection_update(PySetObject *so, PyObject *other)
1348{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001349 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001350
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001351 tmp = set_intersection(so, other);
1352 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001353 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001354 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001355 Py_DECREF(tmp);
1356 Py_RETURN_NONE;
1357}
1358
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00001359static PyObject *
1360set_intersection_update_multi(PySetObject *so, PyObject *args)
1361{
1362 PyObject *tmp;
1363
1364 tmp = set_intersection_multi(so, args);
1365 if (tmp == NULL)
1366 return NULL;
1367 set_swap_bodies(so, (PySetObject *)tmp);
1368 Py_DECREF(tmp);
1369 Py_RETURN_NONE;
1370}
1371
Raymond Hettingera690a992003-11-16 16:17:49 +00001372PyDoc_STRVAR(intersection_update_doc,
1373"Update a set with the intersection of itself and another.");
1374
1375static PyObject *
1376set_and(PySetObject *so, PyObject *other)
1377{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001378 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001379 Py_INCREF(Py_NotImplemented);
1380 return Py_NotImplemented;
1381 }
1382 return set_intersection(so, other);
1383}
1384
1385static PyObject *
1386set_iand(PySetObject *so, PyObject *other)
1387{
1388 PyObject *result;
1389
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001390 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001391 Py_INCREF(Py_NotImplemented);
1392 return Py_NotImplemented;
1393 }
1394 result = set_intersection_update(so, other);
1395 if (result == NULL)
1396 return NULL;
1397 Py_DECREF(result);
1398 Py_INCREF(so);
1399 return (PyObject *)so;
1400}
1401
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001402static PyObject *
1403set_isdisjoint(PySetObject *so, PyObject *other)
1404{
1405 PyObject *key, *it, *tmp;
1406
1407 if ((PyObject *)so == other) {
1408 if (PySet_GET_SIZE(so) == 0)
1409 Py_RETURN_TRUE;
1410 else
1411 Py_RETURN_FALSE;
1412 }
1413
1414 if (PyAnySet_CheckExact(other)) {
1415 Py_ssize_t pos = 0;
1416 setentry *entry;
1417
1418 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1419 tmp = (PyObject *)so;
1420 so = (PySetObject *)other;
1421 other = tmp;
1422 }
1423 while (set_next((PySetObject *)other, &pos, &entry)) {
1424 int rv = set_contains_entry(so, entry);
1425 if (rv == -1)
1426 return NULL;
1427 if (rv)
1428 Py_RETURN_FALSE;
1429 }
1430 Py_RETURN_TRUE;
1431 }
1432
1433 it = PyObject_GetIter(other);
1434 if (it == NULL)
1435 return NULL;
1436
1437 while ((key = PyIter_Next(it)) != NULL) {
1438 int rv;
1439 setentry entry;
1440 long hash = PyObject_Hash(key);
1441
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001442 if (hash == -1) {
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001443 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001444 Py_DECREF(it);
1445 return NULL;
1446 }
1447 entry.hash = hash;
1448 entry.key = key;
1449 rv = set_contains_entry(so, &entry);
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001450 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001451 if (rv == -1) {
1452 Py_DECREF(it);
1453 return NULL;
1454 }
1455 if (rv) {
1456 Py_DECREF(it);
1457 Py_RETURN_FALSE;
1458 }
1459 }
1460 Py_DECREF(it);
1461 if (PyErr_Occurred())
1462 return NULL;
1463 Py_RETURN_TRUE;
1464}
1465
1466PyDoc_STRVAR(isdisjoint_doc,
1467"Return True if two sets have a null intersection.");
1468
Neal Norwitz6576bd82005-11-13 18:41:28 +00001469static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001470set_difference_update_internal(PySetObject *so, PyObject *other)
1471{
1472 if ((PyObject *)so == other)
1473 return set_clear_internal(so);
1474
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001475 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001476 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001477 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001478
1479 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001480 if (set_discard_entry(so, entry) == -1)
1481 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001482 } else {
1483 PyObject *key, *it;
1484 it = PyObject_GetIter(other);
1485 if (it == NULL)
1486 return -1;
1487
1488 while ((key = PyIter_Next(it)) != NULL) {
1489 if (set_discard_key(so, key) == -1) {
1490 Py_DECREF(it);
1491 Py_DECREF(key);
1492 return -1;
1493 }
1494 Py_DECREF(key);
1495 }
1496 Py_DECREF(it);
1497 if (PyErr_Occurred())
1498 return -1;
1499 }
1500 /* If more than 1/5 are dummies, then resize them away. */
1501 if ((so->fill - so->used) * 5 < so->mask)
1502 return 0;
1503 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1504}
1505
Raymond Hettingera690a992003-11-16 16:17:49 +00001506static PyObject *
Raymond Hettinger4267be62008-06-11 10:30:54 +00001507set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001508{
Raymond Hettinger4267be62008-06-11 10:30:54 +00001509 Py_ssize_t i;
1510
1511 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1512 PyObject *other = PyTuple_GET_ITEM(args, i);
1513 if (set_difference_update_internal(so, other) == -1)
1514 return NULL;
1515 }
1516 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001517}
1518
1519PyDoc_STRVAR(difference_update_doc,
1520"Remove all elements of another set from this set.");
1521
1522static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001523set_difference(PySetObject *so, PyObject *other)
1524{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001525 PyObject *result;
1526 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001527 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001528
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001529 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001530 result = set_copy(so);
1531 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001532 return NULL;
1533 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001534 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001535 Py_DECREF(result);
1536 return NULL;
1537 }
1538
Christian Heimese93237d2007-12-19 02:37:44 +00001539 result = make_new_set(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001540 if (result == NULL)
1541 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001542
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001543 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001544 while (set_next(so, &pos, &entry)) {
1545 setentry entrycopy;
1546 entrycopy.hash = entry->hash;
1547 entrycopy.key = entry->key;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001548 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001549 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1550 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001551 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001552 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001553 }
1554 }
1555 return result;
1556 }
1557
Raymond Hettingerc991db22005-08-11 07:58:45 +00001558 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001559 int rv = set_contains_entry((PySetObject *)other, entry);
1560 if (rv == -1) {
1561 Py_DECREF(result);
1562 return NULL;
1563 }
1564 if (!rv) {
1565 if (set_add_entry((PySetObject *)result, entry) == -1) {
1566 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001567 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001568 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001569 }
1570 }
1571 return result;
1572}
1573
Raymond Hettinger4267be62008-06-11 10:30:54 +00001574static PyObject *
1575set_difference_multi(PySetObject *so, PyObject *args)
1576{
1577 Py_ssize_t i;
1578 PyObject *result, *other;
1579
1580 if (PyTuple_GET_SIZE(args) == 0)
1581 return set_copy(so);
1582
1583 other = PyTuple_GET_ITEM(args, 0);
1584 result = set_difference(so, other);
1585 if (result == NULL)
1586 return NULL;
1587
1588 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1589 other = PyTuple_GET_ITEM(args, i);
1590 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1591 Py_DECREF(result);
1592 return NULL;
1593 }
1594 }
1595 return result;
1596}
1597
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001598PyDoc_STRVAR(difference_doc,
Raymond Hettinger4267be62008-06-11 10:30:54 +00001599"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001600\n\
Raymond Hettinger4267be62008-06-11 10:30:54 +00001601(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001602static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001603set_sub(PySetObject *so, PyObject *other)
1604{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001605 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001606 Py_INCREF(Py_NotImplemented);
1607 return Py_NotImplemented;
1608 }
1609 return set_difference(so, other);
1610}
1611
1612static PyObject *
1613set_isub(PySetObject *so, PyObject *other)
1614{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001615 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001616 Py_INCREF(Py_NotImplemented);
1617 return Py_NotImplemented;
1618 }
Raymond Hettinger4267be62008-06-11 10:30:54 +00001619 if (set_difference_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001620 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001621 Py_INCREF(so);
1622 return (PyObject *)so;
1623}
1624
1625static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001626set_symmetric_difference_update(PySetObject *so, PyObject *other)
1627{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001628 PySetObject *otherset;
1629 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001630 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001631 setentry *entry;
1632
1633 if ((PyObject *)so == other)
1634 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001635
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001636 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001637 PyObject *value;
1638 int rv;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001639 long hash;
1640 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001641 setentry an_entry;
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001642
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001643 an_entry.hash = hash;
1644 an_entry.key = key;
1645 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001646 if (rv == -1)
1647 return NULL;
1648 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001649 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001650 return NULL;
1651 }
1652 }
1653 Py_RETURN_NONE;
1654 }
1655
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001656 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001657 Py_INCREF(other);
1658 otherset = (PySetObject *)other;
1659 } else {
Christian Heimese93237d2007-12-19 02:37:44 +00001660 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001661 if (otherset == NULL)
1662 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001663 }
1664
Raymond Hettingerc991db22005-08-11 07:58:45 +00001665 while (set_next(otherset, &pos, &entry)) {
1666 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001667 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001668 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001669 return NULL;
1670 }
1671 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001672 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001673 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001674 return NULL;
1675 }
1676 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001677 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001678 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001679 Py_RETURN_NONE;
1680}
1681
1682PyDoc_STRVAR(symmetric_difference_update_doc,
1683"Update a set with the symmetric difference of itself and another.");
1684
1685static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001686set_symmetric_difference(PySetObject *so, PyObject *other)
1687{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001688 PyObject *rv;
1689 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001690
Christian Heimese93237d2007-12-19 02:37:44 +00001691 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001692 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001693 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001694 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1695 if (rv == NULL)
1696 return NULL;
1697 Py_DECREF(rv);
1698 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001699}
1700
1701PyDoc_STRVAR(symmetric_difference_doc,
1702"Return the symmetric difference of two sets as a new set.\n\
1703\n\
1704(i.e. all elements that are in exactly one of the sets.)");
1705
1706static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001707set_xor(PySetObject *so, PyObject *other)
1708{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001709 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001710 Py_INCREF(Py_NotImplemented);
1711 return Py_NotImplemented;
1712 }
1713 return set_symmetric_difference(so, other);
1714}
1715
1716static PyObject *
1717set_ixor(PySetObject *so, PyObject *other)
1718{
1719 PyObject *result;
1720
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001721 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001722 Py_INCREF(Py_NotImplemented);
1723 return Py_NotImplemented;
1724 }
1725 result = set_symmetric_difference_update(so, other);
1726 if (result == NULL)
1727 return NULL;
1728 Py_DECREF(result);
1729 Py_INCREF(so);
1730 return (PyObject *)so;
1731}
1732
1733static PyObject *
1734set_issubset(PySetObject *so, PyObject *other)
1735{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001736 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001737 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001738
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001739 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001740 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001741 tmp = make_new_set(&PySet_Type, other);
1742 if (tmp == NULL)
1743 return NULL;
1744 result = set_issubset(so, tmp);
1745 Py_DECREF(tmp);
1746 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001747 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001748 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001749 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001750
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001751 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001752 int rv = set_contains_entry((PySetObject *)other, entry);
1753 if (rv == -1)
1754 return NULL;
1755 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001756 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001757 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001758 Py_RETURN_TRUE;
1759}
1760
1761PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1762
1763static PyObject *
1764set_issuperset(PySetObject *so, PyObject *other)
1765{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001766 PyObject *tmp, *result;
1767
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001768 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001769 tmp = make_new_set(&PySet_Type, other);
1770 if (tmp == NULL)
1771 return NULL;
1772 result = set_issuperset(so, tmp);
1773 Py_DECREF(tmp);
1774 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001775 }
1776 return set_issubset((PySetObject *)other, (PyObject *)so);
1777}
1778
1779PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1780
Raymond Hettingera690a992003-11-16 16:17:49 +00001781static PyObject *
1782set_richcompare(PySetObject *v, PyObject *w, int op)
1783{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001784 PyObject *r1, *r2;
1785
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001786 if(!PyAnySet_Check(w)) {
1787 if (op == Py_EQ)
1788 Py_RETURN_FALSE;
1789 if (op == Py_NE)
1790 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001791 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1792 return NULL;
1793 }
1794 switch (op) {
1795 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001796 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001797 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001798 if (v->hash != -1 &&
1799 ((PySetObject *)w)->hash != -1 &&
1800 v->hash != ((PySetObject *)w)->hash)
1801 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001802 return set_issubset(v, w);
1803 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001804 r1 = set_richcompare(v, w, Py_EQ);
1805 if (r1 == NULL)
1806 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001807 r2 = PyBool_FromLong(PyObject_Not(r1));
1808 Py_DECREF(r1);
1809 return r2;
1810 case Py_LE:
1811 return set_issubset(v, w);
1812 case Py_GE:
1813 return set_issuperset(v, w);
1814 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001815 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001816 Py_RETURN_FALSE;
1817 return set_issubset(v, w);
1818 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001819 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001820 Py_RETURN_FALSE;
1821 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001822 }
1823 Py_INCREF(Py_NotImplemented);
1824 return Py_NotImplemented;
1825}
1826
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001827static int
Georg Brandl347b3002006-03-30 11:57:00 +00001828set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001829{
1830 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1831 return -1;
1832}
1833
Raymond Hettingera690a992003-11-16 16:17:49 +00001834static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001835set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001836{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001837 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001838 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001839 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001840}
1841
1842PyDoc_STRVAR(add_doc,
1843"Add an element to a set.\n\
1844\n\
1845This has no effect if the element is already present.");
1846
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001847static int
1848set_contains(PySetObject *so, PyObject *key)
1849{
1850 PyObject *tmpkey;
1851 int rv;
1852
1853 rv = set_contains_key(so, key);
1854 if (rv == -1) {
Raymond Hettingerc5a1cc52008-05-08 04:35:20 +00001855 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001856 return -1;
1857 PyErr_Clear();
1858 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1859 if (tmpkey == NULL)
1860 return -1;
1861 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1862 rv = set_contains(so, tmpkey);
1863 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1864 Py_DECREF(tmpkey);
1865 }
1866 return rv;
1867}
1868
1869static PyObject *
1870set_direct_contains(PySetObject *so, PyObject *key)
1871{
1872 long result;
1873
1874 result = set_contains(so, key);
1875 if (result == -1)
1876 return NULL;
1877 return PyBool_FromLong(result);
1878}
1879
1880PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1881
Raymond Hettingera690a992003-11-16 16:17:49 +00001882static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001883set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001884{
Amaury Forgeot d'Arcd78b9dc2008-10-07 20:32:10 +00001885 PyObject *tmpkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001886 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001887
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001888 rv = set_discard_key(so, key);
1889 if (rv == -1) {
Raymond Hettingerc5a1cc52008-05-08 04:35:20 +00001890 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001891 return NULL;
1892 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001893 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1894 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001895 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001896 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Amaury Forgeot d'Arcd78b9dc2008-10-07 20:32:10 +00001897 rv = set_discard_key(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001898 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001899 Py_DECREF(tmpkey);
Amaury Forgeot d'Arcd78b9dc2008-10-07 20:32:10 +00001900 if (rv == -1)
1901 return NULL;
1902 }
1903
1904 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +00001905 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001906 return NULL;
1907 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001908 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001909}
1910
1911PyDoc_STRVAR(remove_doc,
1912"Remove an element from a set; it must be a member.\n\
1913\n\
1914If the element is not a member, raise a KeyError.");
1915
1916static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001917set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001918{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001919 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001920 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001921
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001922 rv = set_discard_key(so, key);
1923 if (rv == -1) {
Raymond Hettingerc5a1cc52008-05-08 04:35:20 +00001924 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001925 return NULL;
1926 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001927 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1928 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001929 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001930 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001931 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001932 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001933 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001934 return result;
1935 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001936 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001937}
1938
1939PyDoc_STRVAR(discard_doc,
1940"Remove an element from a set if it is a member.\n\
1941\n\
1942If the element is not a member, do nothing.");
1943
1944static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001945set_reduce(PySetObject *so)
1946{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001947 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001948
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001949 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001950 if (keys == NULL)
1951 goto done;
1952 args = PyTuple_Pack(1, keys);
1953 if (args == NULL)
1954 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001955 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1956 if (dict == NULL) {
1957 PyErr_Clear();
1958 dict = Py_None;
1959 Py_INCREF(dict);
1960 }
Christian Heimese93237d2007-12-19 02:37:44 +00001961 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001962done:
1963 Py_XDECREF(args);
1964 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001965 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001966 return result;
1967}
1968
1969PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1970
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00001971static PyObject *
1972set_sizeof(PySetObject *so)
1973{
1974 Py_ssize_t res;
1975
1976 res = sizeof(PySetObject);
1977 if (so->table != so->smalltable)
1978 res = res + (so->mask + 1) * sizeof(setentry);
1979 return PyInt_FromSsize_t(res);
1980}
1981
1982PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001983static int
1984set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1985{
1986 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001987
1988 if (!PyAnySet_Check(self))
1989 return -1;
Christian Heimese93237d2007-12-19 02:37:44 +00001990 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001991 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001992 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001993 self->hash = -1;
1994 if (iterable == NULL)
1995 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001996 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001997}
1998
Raymond Hettingera690a992003-11-16 16:17:49 +00001999static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002000 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00002001 0, /* sq_concat */
2002 0, /* sq_repeat */
2003 0, /* sq_item */
2004 0, /* sq_slice */
2005 0, /* sq_ass_item */
2006 0, /* sq_ass_slice */
2007 (objobjproc)set_contains, /* sq_contains */
2008};
2009
2010/* set object ********************************************************/
2011
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002012#ifdef Py_DEBUG
2013static PyObject *test_c_api(PySetObject *so);
2014
2015PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2016All is well if assertions don't fail.");
2017#endif
2018
Raymond Hettingera690a992003-11-16 16:17:49 +00002019static PyMethodDef set_methods[] = {
2020 {"add", (PyCFunction)set_add, METH_O,
2021 add_doc},
2022 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2023 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00002024 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002025 contains_doc},
Raymond Hettingera37430a2008-02-12 19:05:36 +00002026 {"copy", (PyCFunction)set_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002027 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002028 {"discard", (PyCFunction)set_discard, METH_O,
2029 discard_doc},
Raymond Hettinger4267be62008-06-11 10:30:54 +00002030 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002031 difference_doc},
Raymond Hettinger4267be62008-06-11 10:30:54 +00002032 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002033 difference_update_doc},
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00002034 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002035 intersection_doc},
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00002036 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002037 intersection_update_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00002038 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2039 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002040 {"issubset", (PyCFunction)set_issubset, METH_O,
2041 issubset_doc},
2042 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2043 issuperset_doc},
2044 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2045 pop_doc},
2046 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2047 reduce_doc},
2048 {"remove", (PyCFunction)set_remove, METH_O,
2049 remove_doc},
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00002050 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2051 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002052 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2053 symmetric_difference_doc},
2054 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2055 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002056#ifdef Py_DEBUG
2057 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2058 test_c_api_doc},
2059#endif
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00002060 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002061 union_doc},
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00002062 {"update", (PyCFunction)set_update, METH_VARARGS,
Raymond Hettingera38123e2003-11-24 22:18:49 +00002063 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002064 {NULL, NULL} /* sentinel */
2065};
2066
2067static PyNumberMethods set_as_number = {
2068 0, /*nb_add*/
2069 (binaryfunc)set_sub, /*nb_subtract*/
2070 0, /*nb_multiply*/
2071 0, /*nb_divide*/
2072 0, /*nb_remainder*/
2073 0, /*nb_divmod*/
2074 0, /*nb_power*/
2075 0, /*nb_negative*/
2076 0, /*nb_positive*/
2077 0, /*nb_absolute*/
2078 0, /*nb_nonzero*/
2079 0, /*nb_invert*/
2080 0, /*nb_lshift*/
2081 0, /*nb_rshift*/
2082 (binaryfunc)set_and, /*nb_and*/
2083 (binaryfunc)set_xor, /*nb_xor*/
2084 (binaryfunc)set_or, /*nb_or*/
2085 0, /*nb_coerce*/
2086 0, /*nb_int*/
2087 0, /*nb_long*/
2088 0, /*nb_float*/
2089 0, /*nb_oct*/
2090 0, /*nb_hex*/
2091 0, /*nb_inplace_add*/
2092 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2093 0, /*nb_inplace_multiply*/
2094 0, /*nb_inplace_divide*/
2095 0, /*nb_inplace_remainder*/
2096 0, /*nb_inplace_power*/
2097 0, /*nb_inplace_lshift*/
2098 0, /*nb_inplace_rshift*/
2099 (binaryfunc)set_iand, /*nb_inplace_and*/
2100 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2101 (binaryfunc)set_ior, /*nb_inplace_or*/
2102};
2103
2104PyDoc_STRVAR(set_doc,
2105"set(iterable) --> set object\n\
2106\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002107Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002108
2109PyTypeObject PySet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002110 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002111 "set", /* tp_name */
2112 sizeof(PySetObject), /* tp_basicsize */
2113 0, /* tp_itemsize */
2114 /* methods */
2115 (destructor)set_dealloc, /* tp_dealloc */
2116 (printfunc)set_tp_print, /* tp_print */
2117 0, /* tp_getattr */
2118 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002119 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002120 (reprfunc)set_repr, /* tp_repr */
2121 &set_as_number, /* tp_as_number */
2122 &set_as_sequence, /* tp_as_sequence */
2123 0, /* tp_as_mapping */
Nick Coghlan53663a62008-07-15 14:27:37 +00002124 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00002125 0, /* tp_call */
2126 0, /* tp_str */
2127 PyObject_GenericGetAttr, /* tp_getattro */
2128 0, /* tp_setattro */
2129 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002130 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002131 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002132 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002133 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002134 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002135 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002136 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002137 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002138 0, /* tp_iternext */
2139 set_methods, /* tp_methods */
2140 0, /* tp_members */
2141 0, /* tp_getset */
2142 0, /* tp_base */
2143 0, /* tp_dict */
2144 0, /* tp_descr_get */
2145 0, /* tp_descr_set */
2146 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002147 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002148 PyType_GenericAlloc, /* tp_alloc */
2149 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002150 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002151};
2152
2153/* frozenset object ********************************************************/
2154
2155
2156static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002157 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002158 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002159 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002160 copy_doc},
Raymond Hettinger4267be62008-06-11 10:30:54 +00002161 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002162 difference_doc},
Raymond Hettinger5c4d3d02008-06-09 13:07:27 +00002163 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002164 intersection_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00002165 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2166 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002167 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002168 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002169 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002170 issuperset_doc},
2171 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2172 reduce_doc},
Robert Schuppenies9be2ec12008-07-10 15:24:04 +00002173 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2174 sizeof_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00002175 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2176 symmetric_difference_doc},
Raymond Hettingeree4bcad2008-06-09 08:33:37 +00002177 {"union", (PyCFunction)set_union, METH_VARARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002178 union_doc},
2179 {NULL, NULL} /* sentinel */
2180};
2181
2182static PyNumberMethods frozenset_as_number = {
2183 0, /*nb_add*/
2184 (binaryfunc)set_sub, /*nb_subtract*/
2185 0, /*nb_multiply*/
2186 0, /*nb_divide*/
2187 0, /*nb_remainder*/
2188 0, /*nb_divmod*/
2189 0, /*nb_power*/
2190 0, /*nb_negative*/
2191 0, /*nb_positive*/
2192 0, /*nb_absolute*/
2193 0, /*nb_nonzero*/
2194 0, /*nb_invert*/
2195 0, /*nb_lshift*/
2196 0, /*nb_rshift*/
2197 (binaryfunc)set_and, /*nb_and*/
2198 (binaryfunc)set_xor, /*nb_xor*/
2199 (binaryfunc)set_or, /*nb_or*/
2200};
2201
2202PyDoc_STRVAR(frozenset_doc,
2203"frozenset(iterable) --> frozenset object\n\
2204\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002205Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002206
2207PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002208 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002209 "frozenset", /* tp_name */
2210 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002211 0, /* tp_itemsize */
2212 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002213 (destructor)set_dealloc, /* tp_dealloc */
2214 (printfunc)set_tp_print, /* tp_print */
2215 0, /* tp_getattr */
2216 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002217 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002218 (reprfunc)set_repr, /* tp_repr */
2219 &frozenset_as_number, /* tp_as_number */
2220 &set_as_sequence, /* tp_as_sequence */
2221 0, /* tp_as_mapping */
2222 frozenset_hash, /* tp_hash */
2223 0, /* tp_call */
2224 0, /* tp_str */
2225 PyObject_GenericGetAttr, /* tp_getattro */
2226 0, /* tp_setattro */
2227 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002228 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002229 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002230 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002231 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002232 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002233 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002234 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002235 (getiterfunc)set_iter, /* tp_iter */
2236 0, /* tp_iternext */
2237 frozenset_methods, /* tp_methods */
2238 0, /* tp_members */
2239 0, /* tp_getset */
2240 0, /* tp_base */
2241 0, /* tp_dict */
2242 0, /* tp_descr_get */
2243 0, /* tp_descr_set */
2244 0, /* tp_dictoffset */
2245 0, /* tp_init */
2246 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002247 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002248 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002249};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002250
2251
2252/***** C API functions *************************************************/
2253
2254PyObject *
2255PySet_New(PyObject *iterable)
2256{
2257 return make_new_set(&PySet_Type, iterable);
2258}
2259
2260PyObject *
2261PyFrozenSet_New(PyObject *iterable)
2262{
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002263 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002264}
2265
Neal Norwitz8c49c822006-03-04 18:41:19 +00002266Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002267PySet_Size(PyObject *anyset)
2268{
2269 if (!PyAnySet_Check(anyset)) {
2270 PyErr_BadInternalCall();
2271 return -1;
2272 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002273 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002274}
2275
2276int
Barry Warsaw176014f2006-03-30 22:45:35 +00002277PySet_Clear(PyObject *set)
2278{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002279 if (!PySet_Check(set)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002280 PyErr_BadInternalCall();
2281 return -1;
2282 }
2283 return set_clear_internal((PySetObject *)set);
2284}
2285
2286int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002287PySet_Contains(PyObject *anyset, PyObject *key)
2288{
2289 if (!PyAnySet_Check(anyset)) {
2290 PyErr_BadInternalCall();
2291 return -1;
2292 }
2293 return set_contains_key((PySetObject *)anyset, key);
2294}
2295
2296int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002297PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002298{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002299 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002300 PyErr_BadInternalCall();
2301 return -1;
2302 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002303 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002304}
2305
2306int
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002307PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002308{
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +00002309 if (!PySet_Check(anyset) &&
2310 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerdee3f652008-01-26 09:31:11 +00002311 PyErr_BadInternalCall();
2312 return -1;
2313 }
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002314 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002315}
2316
Barry Warsaw176014f2006-03-30 22:45:35 +00002317int
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002318_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Barry Warsaw176014f2006-03-30 22:45:35 +00002319{
2320 setentry *entry_ptr;
2321
2322 if (!PyAnySet_Check(set)) {
2323 PyErr_BadInternalCall();
2324 return -1;
2325 }
2326 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2327 return 0;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002328 *key = entry_ptr->key;
2329 return 1;
2330}
2331
2332int
2333_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2334{
2335 setentry *entry;
2336
2337 if (!PyAnySet_Check(set)) {
2338 PyErr_BadInternalCall();
2339 return -1;
2340 }
2341 if (set_next((PySetObject *)set, pos, &entry) == 0)
2342 return 0;
2343 *key = entry->key;
2344 *hash = entry->hash;
Barry Warsaw176014f2006-03-30 22:45:35 +00002345 return 1;
2346}
2347
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002348PyObject *
2349PySet_Pop(PyObject *set)
2350{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002351 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002352 PyErr_BadInternalCall();
2353 return NULL;
2354 }
2355 return set_pop((PySetObject *)set);
2356}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002357
Barry Warsaw176014f2006-03-30 22:45:35 +00002358int
2359_PySet_Update(PyObject *set, PyObject *iterable)
2360{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002361 if (!PySet_Check(set)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002362 PyErr_BadInternalCall();
2363 return -1;
2364 }
2365 return set_update_internal((PySetObject *)set, iterable);
2366}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002367
2368#ifdef Py_DEBUG
2369
2370/* Test code to be called with any three element set.
2371 Returns True and original set is restored. */
2372
2373#define assertRaises(call_return_value, exception) \
2374 do { \
2375 assert(call_return_value); \
2376 assert(PyErr_ExceptionMatches(exception)); \
2377 PyErr_Clear(); \
2378 } while(0)
2379
2380static PyObject *
2381test_c_api(PySetObject *so)
2382{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002383 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002384 char *s;
2385 Py_ssize_t i;
Guido van Rossum360496d2007-05-10 17:20:15 +00002386 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Barry Warsaw176014f2006-03-30 22:45:35 +00002387 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002388
2389 /* Verify preconditions and exercise type/size checks */
2390 assert(PyAnySet_Check(ob));
2391 assert(PyAnySet_CheckExact(ob));
2392 assert(!PyFrozenSet_CheckExact(ob));
2393 assert(PySet_Size(ob) == 3);
2394 assert(PySet_GET_SIZE(ob) == 3);
2395
2396 /* Raise TypeError for non-iterable constructor arguments */
2397 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2398 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2399
2400 /* Raise TypeError for unhashable key */
2401 dup = PySet_New(ob);
2402 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2403 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2404 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2405
2406 /* Exercise successful pop, contains, add, and discard */
2407 elem = PySet_Pop(ob);
2408 assert(PySet_Contains(ob, elem) == 0);
2409 assert(PySet_GET_SIZE(ob) == 2);
2410 assert(PySet_Add(ob, elem) == 0);
2411 assert(PySet_Contains(ob, elem) == 1);
2412 assert(PySet_GET_SIZE(ob) == 3);
2413 assert(PySet_Discard(ob, elem) == 1);
2414 assert(PySet_GET_SIZE(ob) == 2);
2415 assert(PySet_Discard(ob, elem) == 0);
2416 assert(PySet_GET_SIZE(ob) == 2);
2417
Barry Warsaw176014f2006-03-30 22:45:35 +00002418 /* Exercise clear */
2419 dup2 = PySet_New(dup);
2420 assert(PySet_Clear(dup2) == 0);
2421 assert(PySet_Size(dup2) == 0);
2422 Py_DECREF(dup2);
2423
2424 /* Raise SystemError on clear or update of frozen set */
2425 f = PyFrozenSet_New(dup);
2426 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2427 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +00002428 assert(PySet_Add(f, elem) == 0);
2429 Py_INCREF(f);
2430 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2431 Py_DECREF(f);
Barry Warsaw176014f2006-03-30 22:45:35 +00002432 Py_DECREF(f);
2433
2434 /* Exercise direct iteration */
2435 i = 0, count = 0;
Guido van Rossum360496d2007-05-10 17:20:15 +00002436 while (_PySet_Next((PyObject *)dup, &i, &x)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002437 s = PyString_AsString(x);
Barry Warsaw176014f2006-03-30 22:45:35 +00002438 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2439 count++;
2440 }
2441 assert(count == 3);
2442
2443 /* Exercise updates */
2444 dup2 = PySet_New(NULL);
2445 assert(_PySet_Update(dup2, dup) == 0);
2446 assert(PySet_Size(dup2) == 3);
2447 assert(_PySet_Update(dup2, dup) == 0);
2448 assert(PySet_Size(dup2) == 3);
2449 Py_DECREF(dup2);
2450
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002451 /* Raise SystemError when self argument is not a set or frozenset. */
2452 t = PyTuple_New(0);
2453 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2454 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2455 Py_DECREF(t);
2456
2457 /* Raise SystemError when self argument is not a set. */
2458 f = PyFrozenSet_New(dup);
2459 assert(PySet_Size(f) == 3);
2460 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002461 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2462 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2463 Py_DECREF(f);
2464
2465 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002466 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2467 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002468 assert(PySet_GET_SIZE(ob) == 0);
2469 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2470
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002471 /* Restore the set from the copy using the PyNumber API */
2472 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2473 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002474
2475 /* Verify constructors accept NULL arguments */
2476 f = PySet_New(NULL);
2477 assert(f != NULL);
2478 assert(PySet_GET_SIZE(f) == 0);
2479 Py_DECREF(f);
2480 f = PyFrozenSet_New(NULL);
2481 assert(f != NULL);
2482 assert(PyFrozenSet_CheckExact(f));
2483 assert(PySet_GET_SIZE(f) == 0);
2484 Py_DECREF(f);
2485
2486 Py_DECREF(elem);
2487 Py_DECREF(dup);
2488 Py_RETURN_TRUE;
2489}
2490
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002491#undef assertRaises
2492
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002493#endif