blob: 9d72b33c0d32e066a844d9063bea2dca776209ea [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis72d20672006-04-11 09:04:12 +00006 Copyright (c) 2003-6 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
13/* This must be >= 1. */
14#define PERTURB_SHIFT 5
15
16/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000017static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000018
Armin Rigoe1709372006-04-12 17:06:05 +000019#ifdef Py_REF_DEBUG
20PyObject *
21_PySet_Dummy(void)
22{
23 return dummy;
24}
25#endif
26
Raymond Hettingerbc841a12005-08-07 13:02:53 +000027#define INIT_NONZERO_SET_SLOTS(so) do { \
28 (so)->table = (so)->smalltable; \
29 (so)->mask = PySet_MINSIZE - 1; \
30 (so)->hash = -1; \
31 } while(0)
32
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033#define EMPTY_TO_MINSIZE(so) do { \
34 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
35 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000036 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000037 } while(0)
38
Raymond Hettingerbc841a12005-08-07 13:02:53 +000039/* Reuse scheme to save calls to malloc, free, and memset */
40#define MAXFREESETS 80
41static PySetObject *free_sets[MAXFREESETS];
42static int num_free_sets = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000043
44/*
45The basic lookup function used by all operations.
46This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
47Open addressing is preferred over chaining since the link overhead for
48chaining would be substantial (100% with typical malloc overhead).
49
50The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000051probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052
53All arithmetic on hash should ignore overflow.
54
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000055Unlike the dictionary implementation, the lookkey functions can return
56NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057*/
58
59static setentry *
60set_lookkey(PySetObject *so, PyObject *key, register long hash)
61{
Martin v. Löwis18e16552006-02-15 17:27:45 +000062 register Py_ssize_t i;
63 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000064 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +000065 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000066 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000067 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000068 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000069 PyObject *startkey;
70
71 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000072 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000073 if (entry->key == NULL || entry->key == key)
74 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000075
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000076 if (entry->key == dummy)
77 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000078 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000079 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000080 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000081 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
82 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000083 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +000084 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000085 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000086 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000087 }
88 else {
89 /* The compare did major nasty stuff to the
90 * set: start over.
91 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000092 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093 }
94 }
95 freeslot = NULL;
96 }
97
98 /* In the loop, key == dummy is by far (factor of 100s) the
99 least likely outcome, so test for that last. */
100 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
101 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000102 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000103 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000104 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000105 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000106 break;
107 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000108 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000109 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000110 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000111 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000112 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
113 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000114 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000115 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000116 if (cmp > 0)
117 break;
118 }
119 else {
120 /* The compare did major nasty stuff to the
121 * set: start over.
122 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000123 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 }
125 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000126 else if (entry->key == dummy && freeslot == NULL)
127 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000128 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000129 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000130}
131
132/*
133 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000134 * This means we can always use _PyString_Eq directly and not have to check to
135 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000136 */
137static setentry *
138set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
139{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000140 register Py_ssize_t i;
141 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000142 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000143 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000144 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000145 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146
147 /* Make sure this function doesn't have to handle non-string keys,
148 including subclasses of str; e.g., one reason to subclass
149 strings is to override __eq__, and for speed we don't cater to
150 that here. */
151 if (!PyString_CheckExact(key)) {
152 so->lookup = set_lookkey;
153 return set_lookkey(so, key, hash);
154 }
155 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000156 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000157 if (entry->key == NULL || entry->key == key)
158 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000159 if (entry->key == dummy)
160 freeslot = entry;
161 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000162 if (entry->hash == hash && _PyString_Eq(entry->key, key))
163 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000164 freeslot = NULL;
165 }
166
167 /* In the loop, key == dummy is by far (factor of 100s) the
168 least likely outcome, so test for that last. */
169 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
170 i = (i << 2) + i + perturb + 1;
171 entry = &table[i & mask];
172 if (entry->key == NULL)
173 return freeslot == NULL ? entry : freeslot;
174 if (entry->key == key
175 || (entry->hash == hash
176 && entry->key != dummy
177 && _PyString_Eq(entry->key, key)))
178 return entry;
179 if (entry->key == dummy && freeslot == NULL)
180 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000181 }
Neal Norwitz7e3ec042006-10-28 21:37:16 +0000182 assert(0); /* NOT REACHED */
183 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000184}
185
186/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000187Internal routine to insert a new key into the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000188Used both by the internal resize routine and by the public insert routine.
189Eats a reference to key.
190*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000191static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000192set_insert_key(register PySetObject *so, PyObject *key, long hash)
193{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000194 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000195 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
196
197 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000198 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000199 if (entry == NULL)
200 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000201 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000202 /* UNUSED */
203 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000204 entry->key = key;
205 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000207 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209 entry->key = key;
210 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211 so->used++;
212 Py_DECREF(dummy);
213 } else {
214 /* ACTIVE */
215 Py_DECREF(key);
216 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000217 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000218}
219
220/*
221Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000222keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000223actually be smaller than the old one.
224*/
225static int
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000226set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000227{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000228 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000229 setentry *oldtable, *newtable, *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000230 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000231 int is_oldtable_malloced;
232 setentry small_copy[PySet_MINSIZE];
233
234 assert(minused >= 0);
235
236 /* Find the smallest table size > minused. */
237 for (newsize = PySet_MINSIZE;
238 newsize <= minused && newsize > 0;
239 newsize <<= 1)
240 ;
241 if (newsize <= 0) {
242 PyErr_NoMemory();
243 return -1;
244 }
245
246 /* Get space for a new table. */
247 oldtable = so->table;
248 assert(oldtable != NULL);
249 is_oldtable_malloced = oldtable != so->smalltable;
250
251 if (newsize == PySet_MINSIZE) {
252 /* A large table is shrinking, or we can't get any smaller. */
253 newtable = so->smalltable;
254 if (newtable == oldtable) {
255 if (so->fill == so->used) {
256 /* No dummies, so no point doing anything. */
257 return 0;
258 }
259 /* We're not going to resize it, but rebuild the
260 table anyway to purge old dummy entries.
261 Subtle: This is *necessary* if fill==size,
262 as set_lookkey needs at least one virgin slot to
263 terminate failing searches. If fill < size, it's
264 merely desirable, as dummies slow searches. */
265 assert(so->fill > so->used);
266 memcpy(small_copy, oldtable, sizeof(small_copy));
267 oldtable = small_copy;
268 }
269 }
270 else {
271 newtable = PyMem_NEW(setentry, newsize);
272 if (newtable == NULL) {
273 PyErr_NoMemory();
274 return -1;
275 }
276 }
277
278 /* Make the set empty, using the new table. */
279 assert(newtable != oldtable);
280 so->table = newtable;
281 so->mask = newsize - 1;
282 memset(newtable, 0, sizeof(setentry) * newsize);
283 so->used = 0;
284 i = so->fill;
285 so->fill = 0;
286
287 /* Copy the data over; this is refcount-neutral for active entries;
288 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000289 for (entry = oldtable; i > 0; entry++) {
290 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000291 /* UNUSED */
292 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000293 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000294 /* DUMMY */
295 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000296 assert(entry->key == dummy);
297 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000298 } else {
299 /* ACTIVE */
300 --i;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000301 if(set_insert_key(so, entry->key, entry->hash) == -1) {
302 if (is_oldtable_malloced)
303 PyMem_DEL(oldtable);
304 return -1;
305 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000306 }
307 }
308
309 if (is_oldtable_malloced)
310 PyMem_DEL(oldtable);
311 return 0;
312}
313
Raymond Hettingerc991db22005-08-11 07:58:45 +0000314/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
315
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000316static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000317set_add_entry(register PySetObject *so, setentry *entry)
318{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000319 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000320
321 assert(so->fill <= so->mask); /* at least one empty slot */
322 n_used = so->used;
323 Py_INCREF(entry->key);
Georg Brandl8de403a2006-09-08 06:02:26 +0000324 if (set_insert_key(so, entry->key, entry->hash) == -1) {
325 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000326 return -1;
Georg Brandl8de403a2006-09-08 06:02:26 +0000327 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000328 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
329 return 0;
330 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
331}
332
333static int
334set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000335{
336 register long hash;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000337 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000338
Raymond Hettingerc991db22005-08-11 07:58:45 +0000339 if (!PyString_CheckExact(key) ||
340 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341 hash = PyObject_Hash(key);
342 if (hash == -1)
343 return -1;
344 }
345 assert(so->fill <= so->mask); /* at least one empty slot */
346 n_used = so->used;
347 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000348 if (set_insert_key(so, key, hash) == -1) {
349 Py_DECREF(key);
350 return -1;
351 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000352 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
353 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000354 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355}
356
357#define DISCARD_NOTFOUND 0
358#define DISCARD_FOUND 1
359
360static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361set_discard_entry(PySetObject *so, setentry *oldentry)
362{ register setentry *entry;
363 PyObject *old_key;
364
365 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000366 if (entry == NULL)
367 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000368 if (entry->key == NULL || entry->key == dummy)
369 return DISCARD_NOTFOUND;
370 old_key = entry->key;
371 Py_INCREF(dummy);
372 entry->key = dummy;
373 so->used--;
374 Py_DECREF(old_key);
375 return DISCARD_FOUND;
376}
377
378static int
379set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380{
381 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000382 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000383 PyObject *old_key;
384
385 assert (PyAnySet_Check(so));
386 if (!PyString_CheckExact(key) ||
387 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
388 hash = PyObject_Hash(key);
389 if (hash == -1)
390 return -1;
391 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000392 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000393 if (entry == NULL)
394 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000395 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000396 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000397 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000398 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000399 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000400 so->used--;
401 Py_DECREF(old_key);
402 return DISCARD_FOUND;
403}
404
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000405static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000406set_clear_internal(PySetObject *so)
407{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000408 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000409 int table_is_malloced;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000410 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000411 setentry small_copy[PySet_MINSIZE];
412#ifdef Py_DEBUG
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000413 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000414 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000415
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000416 n = so->mask + 1;
417 i = 0;
418#endif
419
420 table = so->table;
421 assert(table != NULL);
422 table_is_malloced = table != so->smalltable;
423
424 /* This is delicate. During the process of clearing the set,
425 * decrefs can cause the set to mutate. To avoid fatal confusion
426 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000427 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000428 * clearing.
429 */
430 fill = so->fill;
431 if (table_is_malloced)
432 EMPTY_TO_MINSIZE(so);
433
434 else if (fill > 0) {
435 /* It's a small table with something that needs to be cleared.
436 * Afraid the only safe way is to copy the set entries into
437 * another small table first.
438 */
439 memcpy(small_copy, table, sizeof(small_copy));
440 table = small_copy;
441 EMPTY_TO_MINSIZE(so);
442 }
443 /* else it's a small table that's already empty */
444
445 /* Now we can finally clear things. If C had refcounts, we could
446 * assert that the refcount on table is 1 now, i.e. that this function
447 * has unique access to it, so decref side-effects can't alter it.
448 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000449 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450#ifdef Py_DEBUG
451 assert(i < n);
452 ++i;
453#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000454 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000456 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457 }
458#ifdef Py_DEBUG
459 else
Raymond Hettinger334b5b22006-03-26 03:11:29 +0000460 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000461#endif
462 }
463
464 if (table_is_malloced)
465 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000466 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000467}
468
469/*
470 * Iterate over a set table. Use like so:
471 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000472 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000473 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000474 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000475 * while (set_next(yourset, &pos, &entry)) {
476 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000477 * }
478 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000479 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480 * mutates the table.
481 */
482static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000483set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000484{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000485 Py_ssize_t i;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000486 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000487 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000488
489 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000490 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000491 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000492 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000494 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000496 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000497 if (i > mask)
498 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000499 assert(table[i].key != NULL);
500 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501 return 1;
502}
503
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000504static void
505set_dealloc(PySetObject *so)
506{
507 register setentry *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000508 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000509 PyObject_GC_UnTrack(so);
510 Py_TRASHCAN_SAFE_BEGIN(so)
511 if (so->weakreflist != NULL)
512 PyObject_ClearWeakRefs((PyObject *) so);
513
514 for (entry = so->table; fill > 0; entry++) {
515 if (entry->key) {
516 --fill;
517 Py_DECREF(entry->key);
518 }
519 }
520 if (so->table != so->smalltable)
521 PyMem_DEL(so->table);
522 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
523 free_sets[num_free_sets++] = so;
524 else
525 so->ob_type->tp_free(so);
526 Py_TRASHCAN_SAFE_END(so)
527}
528
529static int
530set_tp_print(PySetObject *so, FILE *fp, int flags)
531{
532 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000533 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000534 char *emit = ""; /* No separator emitted on first pass */
535 char *separator = ", ";
536
537 fprintf(fp, "%s([", so->ob_type->tp_name);
538 while (set_next(so, &pos, &entry)) {
539 fputs(emit, fp);
540 emit = separator;
541 if (PyObject_Print(entry->key, fp, 0) != 0)
542 return -1;
543 }
544 fputs("])", fp);
545 return 0;
546}
547
548static PyObject *
549set_repr(PySetObject *so)
550{
551 PyObject *keys, *result, *listrepr;
552
553 keys = PySequence_List((PyObject *)so);
554 if (keys == NULL)
555 return NULL;
556 listrepr = PyObject_Repr(keys);
557 Py_DECREF(keys);
558 if (listrepr == NULL)
559 return NULL;
560
561 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
562 PyString_AS_STRING(listrepr));
563 Py_DECREF(listrepr);
564 return result;
565}
566
Martin v. Löwis18e16552006-02-15 17:27:45 +0000567static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568set_len(PyObject *so)
569{
570 return ((PySetObject *)so)->used;
571}
572
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000573static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000574set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000575{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000576 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000577 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000578 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000579
580 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000581 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000582
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000583 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000584 if (other == so || other->used == 0)
585 /* a.update(a) or a.update({}); nothing to do */
586 return 0;
587 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000588 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000589 * that there will be no (or few) overlapping keys.
590 */
591 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
592 if (set_table_resize(so, (so->used + other->used)*2) != 0)
593 return -1;
594 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000595 for (i = 0; i <= other->mask; i++) {
596 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000597 if (entry->key != NULL &&
598 entry->key != dummy) {
599 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000600 if (set_insert_key(so, entry->key, entry->hash) == -1) {
601 Py_DECREF(entry->key);
602 return -1;
603 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000604 }
605 }
606 return 0;
607}
608
609static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000610set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000611{
612 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000613 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000614
615 if (!PyString_CheckExact(key) ||
616 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
617 hash = PyObject_Hash(key);
618 if (hash == -1)
619 return -1;
620 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000621 entry = (so->lookup)(so, key, hash);
622 if (entry == NULL)
623 return -1;
624 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000625 return key != NULL && key != dummy;
626}
627
Raymond Hettingerc991db22005-08-11 07:58:45 +0000628static int
629set_contains_entry(PySetObject *so, setentry *entry)
630{
631 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000632 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000633
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000634 lu_entry = (so->lookup)(so, entry->key, entry->hash);
635 if (lu_entry == NULL)
636 return -1;
637 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000638 return key != NULL && key != dummy;
639}
640
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000641static PyObject *
642set_pop(PySetObject *so)
643{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000644 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000645 register setentry *entry;
646 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000647
648 assert (PyAnySet_Check(so));
649 if (so->used == 0) {
650 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
651 return NULL;
652 }
653
654 /* Set entry to "the first" unused or dummy set entry. We abuse
655 * the hash field of slot 0 to hold a search finger:
656 * If slot 0 has a value, use slot 0.
657 * Else slot 0 is being used to hold a search finger,
658 * and we use its hash value as the first index to look.
659 */
660 entry = &so->table[0];
661 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000662 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000663 /* The hash field may be a real hash value, or it may be a
664 * legit search finger, or it may be a once-legit search
665 * finger that's out of bounds now because it wrapped around
666 * or the table shrunk -- simply make sure it's in bounds now.
667 */
668 if (i > so->mask || i < 1)
669 i = 1; /* skip slot 0 */
670 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
671 i++;
672 if (i > so->mask)
673 i = 1;
674 }
675 }
676 key = entry->key;
677 Py_INCREF(dummy);
678 entry->key = dummy;
679 so->used--;
680 so->table[0].hash = i + 1; /* next place to start */
681 return key;
682}
683
684PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
685
686static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000687set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000688{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000689 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000690 setentry *entry;
691
692 while (set_next(so, &pos, &entry))
693 Py_VISIT(entry->key);
694 return 0;
695}
696
697static long
698frozenset_hash(PyObject *self)
699{
700 PySetObject *so = (PySetObject *)self;
701 long h, hash = 1927868237L;
702 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000703 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000704
705 if (so->hash != -1)
706 return so->hash;
707
708 hash *= PySet_GET_SIZE(self) + 1;
709 while (set_next(so, &pos, &entry)) {
710 /* Work to increase the bit dispersion for closely spaced hash
711 values. The is important because some use cases have many
712 combinations of a small number of elements with nearby
713 hashes so that many distinct combinations collapse to only
714 a handful of distinct hash values. */
715 h = entry->hash;
716 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
717 }
718 hash = hash * 69069L + 907133923L;
719 if (hash == -1)
720 hash = 590923713L;
721 so->hash = hash;
722 return hash;
723}
724
725static long
726set_nohash(PyObject *self)
727{
728 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
729 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000730}
731
Raymond Hettingera9d99362005-08-05 00:01:15 +0000732/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000733
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000734typedef struct {
735 PyObject_HEAD
736 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000737 Py_ssize_t si_used;
738 Py_ssize_t si_pos;
739 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000740} setiterobject;
741
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000742static void
743setiter_dealloc(setiterobject *si)
744{
745 Py_XDECREF(si->si_set);
746 PyObject_Del(si);
747}
748
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000749static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000750setiter_len(setiterobject *si)
751{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000752 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000753 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000754 len = si->len;
755 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000756}
757
Armin Rigof5b3e362006-02-11 21:32:43 +0000758PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000759
760static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000761 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000762 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000763};
764
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000765static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000766{
767 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000768 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000769 register setentry *entry;
770 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000771
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000772 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000773 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000774 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000775
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000776 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777 PyErr_SetString(PyExc_RuntimeError,
778 "Set changed size during iteration");
779 si->si_used = -1; /* Make this state sticky */
780 return NULL;
781 }
782
783 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000784 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000785 entry = so->table;
786 mask = so->mask;
787 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788 i++;
789 si->si_pos = i+1;
790 if (i > mask)
791 goto fail;
792 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000793 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000794 Py_INCREF(key);
795 return key;
796
797fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000798 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799 si->si_set = NULL;
800 return NULL;
801}
802
Hye-Shik Change2956762005-08-01 05:26:41 +0000803static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804 PyObject_HEAD_INIT(&PyType_Type)
805 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000806 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000807 sizeof(setiterobject), /* tp_basicsize */
808 0, /* tp_itemsize */
809 /* methods */
810 (destructor)setiter_dealloc, /* tp_dealloc */
811 0, /* tp_print */
812 0, /* tp_getattr */
813 0, /* tp_setattr */
814 0, /* tp_compare */
815 0, /* tp_repr */
816 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000817 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818 0, /* tp_as_mapping */
819 0, /* tp_hash */
820 0, /* tp_call */
821 0, /* tp_str */
822 PyObject_GenericGetAttr, /* tp_getattro */
823 0, /* tp_setattro */
824 0, /* tp_as_buffer */
825 Py_TPFLAGS_DEFAULT, /* tp_flags */
826 0, /* tp_doc */
827 0, /* tp_traverse */
828 0, /* tp_clear */
829 0, /* tp_richcompare */
830 0, /* tp_weaklistoffset */
831 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000832 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000833 setiter_methods, /* tp_methods */
834 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835};
836
Martin v. Löwis72d20672006-04-11 09:04:12 +0000837static PyObject *
838set_iter(PySetObject *so)
839{
840 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
841 if (si == NULL)
842 return NULL;
843 Py_INCREF(so);
844 si->si_set = so;
845 si->si_used = so->used;
846 si->si_pos = 0;
847 si->len = so->used;
848 return (PyObject *)si;
849}
850
Raymond Hettingerd7946662005-08-01 21:39:29 +0000851static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000852set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000853{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000854 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000855
Raymond Hettingerd7946662005-08-01 21:39:29 +0000856 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000857 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000858
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000859 if (PyDict_Check(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000860 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000861 Py_ssize_t pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000862 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000863 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000864 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000865 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000866 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000867 }
868
Raymond Hettingera38123e2003-11-24 22:18:49 +0000869 it = PyObject_GetIter(other);
870 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000871 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000872
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000873 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000874 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000875 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000876 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000877 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000878 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000879 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000880 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000881 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000882 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000883 return -1;
884 return 0;
885}
886
887static PyObject *
888set_update(PySetObject *so, PyObject *other)
889{
890 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000891 return NULL;
892 Py_RETURN_NONE;
893}
894
895PyDoc_STRVAR(update_doc,
896"Update a set with the union of itself and another.");
897
898static PyObject *
899make_new_set(PyTypeObject *type, PyObject *iterable)
900{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000901 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000902
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000903 if (dummy == NULL) { /* Auto-initialize dummy */
904 dummy = PyString_FromString("<dummy key>");
905 if (dummy == NULL)
906 return NULL;
907 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000908
909 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000910 if (num_free_sets &&
911 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
912 so = free_sets[--num_free_sets];
913 assert (so != NULL && PyAnySet_CheckExact(so));
914 so->ob_type = type;
915 _Py_NewReference((PyObject *)so);
916 EMPTY_TO_MINSIZE(so);
917 PyObject_GC_Track(so);
918 } else {
919 so = (PySetObject *)type->tp_alloc(type, 0);
920 if (so == NULL)
921 return NULL;
922 /* tp_alloc has already zeroed the structure */
923 assert(so->table == NULL && so->fill == 0 && so->used == 0);
924 INIT_NONZERO_SET_SLOTS(so);
925 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000926
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000927 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000928 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000929
Raymond Hettingera38123e2003-11-24 22:18:49 +0000930 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000931 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000932 Py_DECREF(so);
933 return NULL;
934 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000935 }
936
Raymond Hettingera690a992003-11-16 16:17:49 +0000937 return (PyObject *)so;
938}
939
Raymond Hettingerd7946662005-08-01 21:39:29 +0000940/* The empty frozenset is a singleton */
941static PyObject *emptyfrozenset = NULL;
942
Raymond Hettingera690a992003-11-16 16:17:49 +0000943static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000944frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000945{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000946 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000947
Georg Brandl02c42872005-08-26 06:42:30 +0000948 if (!_PyArg_NoKeywords("frozenset()", kwds))
949 return NULL;
950
Raymond Hettingera690a992003-11-16 16:17:49 +0000951 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
952 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000953
954 if (type != &PyFrozenSet_Type)
955 return make_new_set(type, iterable);
956
957 if (iterable != NULL) {
958 /* frozenset(f) is idempotent */
959 if (PyFrozenSet_CheckExact(iterable)) {
960 Py_INCREF(iterable);
961 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000962 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000963 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000964 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000965 return result;
966 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000967 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000968 /* The empty frozenset is a singleton */
969 if (emptyfrozenset == NULL)
970 emptyfrozenset = make_new_set(type, NULL);
971 Py_XINCREF(emptyfrozenset);
972 return emptyfrozenset;
973}
974
975void
976PySet_Fini(void)
977{
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000978 PySetObject *so;
979
980 while (num_free_sets) {
981 num_free_sets--;
982 so = free_sets[num_free_sets];
983 PyObject_GC_Del(so);
984 }
Martin v. Löwised8f7832006-04-15 12:47:23 +0000985 Py_CLEAR(dummy);
986 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000987}
988
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000989static PyObject *
990set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
991{
Georg Brandl02c42872005-08-26 06:42:30 +0000992 if (!_PyArg_NoKeywords("set()", kwds))
993 return NULL;
994
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000995 return make_new_set(type, NULL);
996}
997
Raymond Hettinger934d63e2005-07-31 01:33:10 +0000998/* set_swap_bodies() switches the contents of any two sets by moving their
999 internal data pointers and, if needed, copying the internal smalltables.
1000 Semantically equivalent to:
1001
1002 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1003
1004 The function always succeeds and it leaves both objects in a stable state.
1005 Useful for creating temporary frozensets from sets for membership testing
1006 in __contains__(), discard(), and remove(). Also useful for operations
1007 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001008 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001009*/
1010
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001011static void
1012set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001013{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001014 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001015 setentry *u;
1016 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1017 setentry tab[PySet_MINSIZE];
1018 long h;
1019
1020 t = a->fill; a->fill = b->fill; b->fill = t;
1021 t = a->used; a->used = b->used; b->used = t;
1022 t = a->mask; a->mask = b->mask; b->mask = t;
1023
1024 u = a->table;
1025 if (a->table == a->smalltable)
1026 u = b->smalltable;
1027 a->table = b->table;
1028 if (b->table == b->smalltable)
1029 a->table = a->smalltable;
1030 b->table = u;
1031
1032 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1033
1034 if (a->table == a->smalltable || b->table == b->smalltable) {
1035 memcpy(tab, a->smalltable, sizeof(tab));
1036 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1037 memcpy(b->smalltable, tab, sizeof(tab));
1038 }
1039
Raymond Hettingera580c472005-08-05 17:19:54 +00001040 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1041 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1042 h = a->hash; a->hash = b->hash; b->hash = h;
1043 } else {
1044 a->hash = -1;
1045 b->hash = -1;
1046 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001047}
1048
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001049static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001050set_copy(PySetObject *so)
1051{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001052 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001053}
1054
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001055static PyObject *
1056frozenset_copy(PySetObject *so)
1057{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001058 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001059 Py_INCREF(so);
1060 return (PyObject *)so;
1061 }
1062 return set_copy(so);
1063}
1064
Raymond Hettingera690a992003-11-16 16:17:49 +00001065PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1066
1067static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001068set_clear(PySetObject *so)
1069{
1070 set_clear_internal(so);
1071 Py_RETURN_NONE;
1072}
1073
1074PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1075
1076static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001077set_union(PySetObject *so, PyObject *other)
1078{
1079 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001080
1081 result = (PySetObject *)set_copy(so);
1082 if (result == NULL)
1083 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001084 if ((PyObject *)so == other)
1085 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001086 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001087 Py_DECREF(result);
1088 return NULL;
1089 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001090 return (PyObject *)result;
1091}
1092
1093PyDoc_STRVAR(union_doc,
1094 "Return the union of two sets as a new set.\n\
1095\n\
1096(i.e. all elements that are in either set.)");
1097
1098static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001099set_or(PySetObject *so, PyObject *other)
1100{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001101 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001102 Py_INCREF(Py_NotImplemented);
1103 return Py_NotImplemented;
1104 }
1105 return set_union(so, other);
1106}
1107
1108static PyObject *
1109set_ior(PySetObject *so, PyObject *other)
1110{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001111 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001112 Py_INCREF(Py_NotImplemented);
1113 return Py_NotImplemented;
1114 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001115 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001116 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001117 Py_INCREF(so);
1118 return (PyObject *)so;
1119}
1120
1121static PyObject *
1122set_intersection(PySetObject *so, PyObject *other)
1123{
1124 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001125 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001126
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001127 if ((PyObject *)so == other)
1128 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001129
Raymond Hettingera690a992003-11-16 16:17:49 +00001130 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1131 if (result == NULL)
1132 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001133
Raymond Hettingerc991db22005-08-11 07:58:45 +00001134 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001135 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001136 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001137
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001138 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001139 tmp = (PyObject *)so;
1140 so = (PySetObject *)other;
1141 other = tmp;
1142 }
1143
Raymond Hettingerc991db22005-08-11 07:58:45 +00001144 while (set_next((PySetObject *)other, &pos, &entry)) {
Georg Brandl8de403a2006-09-08 06:02:26 +00001145 int rv = set_contains_entry(so, entry);
1146 if (rv == -1) {
1147 Py_DECREF(result);
1148 return NULL;
1149 }
1150 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001151 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001152 Py_DECREF(result);
1153 return NULL;
1154 }
1155 }
1156 }
1157 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001158 }
1159
Raymond Hettingera690a992003-11-16 16:17:49 +00001160 it = PyObject_GetIter(other);
1161 if (it == NULL) {
1162 Py_DECREF(result);
1163 return NULL;
1164 }
1165
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001166 while ((key = PyIter_Next(it)) != NULL) {
Georg Brandl8de403a2006-09-08 06:02:26 +00001167 int rv = set_contains_key(so, key);
1168 if (rv == -1) {
1169 Py_DECREF(it);
1170 Py_DECREF(result);
1171 Py_DECREF(key);
1172 return NULL;
1173 }
1174 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001175 if (set_add_key(result, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001176 Py_DECREF(it);
1177 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001178 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001179 return NULL;
1180 }
1181 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001182 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001183 }
1184 Py_DECREF(it);
1185 if (PyErr_Occurred()) {
1186 Py_DECREF(result);
1187 return NULL;
1188 }
1189 return (PyObject *)result;
1190}
1191
1192PyDoc_STRVAR(intersection_doc,
1193"Return the intersection of two sets as a new set.\n\
1194\n\
1195(i.e. all elements that are in both sets.)");
1196
1197static PyObject *
1198set_intersection_update(PySetObject *so, PyObject *other)
1199{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001200 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001201
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001202 tmp = set_intersection(so, other);
1203 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001204 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001205 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001206 Py_DECREF(tmp);
1207 Py_RETURN_NONE;
1208}
1209
1210PyDoc_STRVAR(intersection_update_doc,
1211"Update a set with the intersection of itself and another.");
1212
1213static PyObject *
1214set_and(PySetObject *so, PyObject *other)
1215{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001216 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001217 Py_INCREF(Py_NotImplemented);
1218 return Py_NotImplemented;
1219 }
1220 return set_intersection(so, other);
1221}
1222
1223static PyObject *
1224set_iand(PySetObject *so, PyObject *other)
1225{
1226 PyObject *result;
1227
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001228 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001229 Py_INCREF(Py_NotImplemented);
1230 return Py_NotImplemented;
1231 }
1232 result = set_intersection_update(so, other);
1233 if (result == NULL)
1234 return NULL;
1235 Py_DECREF(result);
1236 Py_INCREF(so);
1237 return (PyObject *)so;
1238}
1239
Neal Norwitz6576bd82005-11-13 18:41:28 +00001240static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001241set_difference_update_internal(PySetObject *so, PyObject *other)
1242{
1243 if ((PyObject *)so == other)
1244 return set_clear_internal(so);
1245
1246 if (PyAnySet_Check(other)) {
1247 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001248 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001249
1250 while (set_next((PySetObject *)other, &pos, &entry))
Georg Brandl8de403a2006-09-08 06:02:26 +00001251 if (set_discard_entry(so, entry) == -1)
1252 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001253 } else {
1254 PyObject *key, *it;
1255 it = PyObject_GetIter(other);
1256 if (it == NULL)
1257 return -1;
1258
1259 while ((key = PyIter_Next(it)) != NULL) {
1260 if (set_discard_key(so, key) == -1) {
1261 Py_DECREF(it);
1262 Py_DECREF(key);
1263 return -1;
1264 }
1265 Py_DECREF(key);
1266 }
1267 Py_DECREF(it);
1268 if (PyErr_Occurred())
1269 return -1;
1270 }
1271 /* If more than 1/5 are dummies, then resize them away. */
1272 if ((so->fill - so->used) * 5 < so->mask)
1273 return 0;
1274 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1275}
1276
Raymond Hettingera690a992003-11-16 16:17:49 +00001277static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001278set_difference_update(PySetObject *so, PyObject *other)
1279{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001280 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001281 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001282 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001283}
1284
1285PyDoc_STRVAR(difference_update_doc,
1286"Remove all elements of another set from this set.");
1287
1288static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001289set_difference(PySetObject *so, PyObject *other)
1290{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001291 PyObject *result;
1292 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001293 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001294
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001295 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001296 result = set_copy(so);
1297 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001298 return NULL;
1299 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001300 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001301 Py_DECREF(result);
1302 return NULL;
1303 }
1304
1305 result = make_new_set(so->ob_type, NULL);
1306 if (result == NULL)
1307 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001308
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001309 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001310 while (set_next(so, &pos, &entry)) {
1311 setentry entrycopy;
1312 entrycopy.hash = entry->hash;
1313 entrycopy.key = entry->key;
1314 if (!PyDict_Contains(other, entry->key)) {
Georg Brandl8de403a2006-09-08 06:02:26 +00001315 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1316 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001317 return NULL;
Georg Brandl8de403a2006-09-08 06:02:26 +00001318 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001319 }
1320 }
1321 return result;
1322 }
1323
Raymond Hettingerc991db22005-08-11 07:58:45 +00001324 while (set_next(so, &pos, &entry)) {
Georg Brandl8de403a2006-09-08 06:02:26 +00001325 int rv = set_contains_entry((PySetObject *)other, entry);
1326 if (rv == -1) {
1327 Py_DECREF(result);
1328 return NULL;
1329 }
1330 if (!rv) {
1331 if (set_add_entry((PySetObject *)result, entry) == -1) {
1332 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001333 return NULL;
Georg Brandl8de403a2006-09-08 06:02:26 +00001334 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001335 }
1336 }
1337 return result;
1338}
1339
1340PyDoc_STRVAR(difference_doc,
1341"Return the difference of two sets as a new set.\n\
1342\n\
1343(i.e. all elements that are in this set but not the other.)");
1344static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001345set_sub(PySetObject *so, PyObject *other)
1346{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001347 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001348 Py_INCREF(Py_NotImplemented);
1349 return Py_NotImplemented;
1350 }
1351 return set_difference(so, other);
1352}
1353
1354static PyObject *
1355set_isub(PySetObject *so, PyObject *other)
1356{
1357 PyObject *result;
1358
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001359 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001360 Py_INCREF(Py_NotImplemented);
1361 return Py_NotImplemented;
1362 }
1363 result = set_difference_update(so, other);
1364 if (result == NULL)
1365 return NULL;
1366 Py_DECREF(result);
1367 Py_INCREF(so);
1368 return (PyObject *)so;
1369}
1370
1371static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001372set_symmetric_difference_update(PySetObject *so, PyObject *other)
1373{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001374 PySetObject *otherset;
1375 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001376 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001377 setentry *entry;
1378
1379 if ((PyObject *)so == other)
1380 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001381
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001382 if (PyDict_Check(other)) {
1383 PyObject *value;
1384 int rv;
1385 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001386 rv = set_discard_key(so, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001387 if (rv == -1)
1388 return NULL;
1389 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001390 if (set_add_key(so, key) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001391 return NULL;
1392 }
1393 }
1394 Py_RETURN_NONE;
1395 }
1396
1397 if (PyAnySet_Check(other)) {
1398 Py_INCREF(other);
1399 otherset = (PySetObject *)other;
1400 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001401 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1402 if (otherset == NULL)
1403 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001404 }
1405
Raymond Hettingerc991db22005-08-11 07:58:45 +00001406 while (set_next(otherset, &pos, &entry)) {
1407 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001408 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001409 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001410 return NULL;
1411 }
1412 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001413 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001414 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001415 return NULL;
1416 }
1417 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001418 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001419 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001420 Py_RETURN_NONE;
1421}
1422
1423PyDoc_STRVAR(symmetric_difference_update_doc,
1424"Update a set with the symmetric difference of itself and another.");
1425
1426static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001427set_symmetric_difference(PySetObject *so, PyObject *other)
1428{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001429 PyObject *rv;
1430 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001431
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001432 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1433 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001434 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001435 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1436 if (rv == NULL)
1437 return NULL;
1438 Py_DECREF(rv);
1439 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001440}
1441
1442PyDoc_STRVAR(symmetric_difference_doc,
1443"Return the symmetric difference of two sets as a new set.\n\
1444\n\
1445(i.e. all elements that are in exactly one of the sets.)");
1446
1447static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001448set_xor(PySetObject *so, PyObject *other)
1449{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001450 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001451 Py_INCREF(Py_NotImplemented);
1452 return Py_NotImplemented;
1453 }
1454 return set_symmetric_difference(so, other);
1455}
1456
1457static PyObject *
1458set_ixor(PySetObject *so, PyObject *other)
1459{
1460 PyObject *result;
1461
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001462 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001463 Py_INCREF(Py_NotImplemented);
1464 return Py_NotImplemented;
1465 }
1466 result = set_symmetric_difference_update(so, other);
1467 if (result == NULL)
1468 return NULL;
1469 Py_DECREF(result);
1470 Py_INCREF(so);
1471 return (PyObject *)so;
1472}
1473
1474static PyObject *
1475set_issubset(PySetObject *so, PyObject *other)
1476{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001477 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001478 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001479
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001480 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001481 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001482 tmp = make_new_set(&PySet_Type, other);
1483 if (tmp == NULL)
1484 return NULL;
1485 result = set_issubset(so, tmp);
1486 Py_DECREF(tmp);
1487 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001488 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001489 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001490 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001491
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001492 while (set_next(so, &pos, &entry)) {
Georg Brandl8de403a2006-09-08 06:02:26 +00001493 int rv = set_contains_entry((PySetObject *)other, entry);
1494 if (rv == -1)
1495 return NULL;
1496 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001497 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001498 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001499 Py_RETURN_TRUE;
1500}
1501
1502PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1503
1504static PyObject *
1505set_issuperset(PySetObject *so, PyObject *other)
1506{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001507 PyObject *tmp, *result;
1508
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001509 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001510 tmp = make_new_set(&PySet_Type, other);
1511 if (tmp == NULL)
1512 return NULL;
1513 result = set_issuperset(so, tmp);
1514 Py_DECREF(tmp);
1515 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001516 }
1517 return set_issubset((PySetObject *)other, (PyObject *)so);
1518}
1519
1520PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1521
Raymond Hettingera690a992003-11-16 16:17:49 +00001522static PyObject *
1523set_richcompare(PySetObject *v, PyObject *w, int op)
1524{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001525 PyObject *r1, *r2;
1526
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001527 if(!PyAnySet_Check(w)) {
1528 if (op == Py_EQ)
1529 Py_RETURN_FALSE;
1530 if (op == Py_NE)
1531 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001532 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1533 return NULL;
1534 }
1535 switch (op) {
1536 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001537 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001538 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001539 if (v->hash != -1 &&
1540 ((PySetObject *)w)->hash != -1 &&
1541 v->hash != ((PySetObject *)w)->hash)
1542 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001543 return set_issubset(v, w);
1544 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001545 r1 = set_richcompare(v, w, Py_EQ);
1546 if (r1 == NULL)
1547 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001548 r2 = PyBool_FromLong(PyObject_Not(r1));
1549 Py_DECREF(r1);
1550 return r2;
1551 case Py_LE:
1552 return set_issubset(v, w);
1553 case Py_GE:
1554 return set_issuperset(v, w);
1555 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001556 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001557 Py_RETURN_FALSE;
1558 return set_issubset(v, w);
1559 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001560 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001561 Py_RETURN_FALSE;
1562 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001563 }
1564 Py_INCREF(Py_NotImplemented);
1565 return Py_NotImplemented;
1566}
1567
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001568static int
Georg Brandl347b3002006-03-30 11:57:00 +00001569set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001570{
1571 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1572 return -1;
1573}
1574
Raymond Hettingera690a992003-11-16 16:17:49 +00001575static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001576set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001577{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001578 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001579 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001580 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001581}
1582
1583PyDoc_STRVAR(add_doc,
1584"Add an element to a set.\n\
1585\n\
1586This has no effect if the element is already present.");
1587
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001588static int
1589set_contains(PySetObject *so, PyObject *key)
1590{
1591 PyObject *tmpkey;
1592 int rv;
1593
1594 rv = set_contains_key(so, key);
1595 if (rv == -1) {
1596 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1597 return -1;
1598 PyErr_Clear();
1599 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1600 if (tmpkey == NULL)
1601 return -1;
1602 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1603 rv = set_contains(so, tmpkey);
1604 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1605 Py_DECREF(tmpkey);
1606 }
1607 return rv;
1608}
1609
1610static PyObject *
1611set_direct_contains(PySetObject *so, PyObject *key)
1612{
1613 long result;
1614
1615 result = set_contains(so, key);
1616 if (result == -1)
1617 return NULL;
1618 return PyBool_FromLong(result);
1619}
1620
1621PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1622
Raymond Hettingera690a992003-11-16 16:17:49 +00001623static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001624set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001625{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001626 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001627 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001628
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001629 rv = set_discard_key(so, key);
1630 if (rv == -1) {
1631 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1632 return NULL;
1633 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001634 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1635 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001636 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001637 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001638 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001639 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001640 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001641 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001642 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001643 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001644 return NULL;
1645 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001646 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001647}
1648
1649PyDoc_STRVAR(remove_doc,
1650"Remove an element from a set; it must be a member.\n\
1651\n\
1652If the element is not a member, raise a KeyError.");
1653
1654static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001655set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001656{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001657 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001658 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001659
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001660 rv = set_discard_key(so, key);
1661 if (rv == -1) {
1662 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1663 return NULL;
1664 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001665 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1666 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001667 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001668 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001669 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001670 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001671 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001672 return result;
1673 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001674 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001675}
1676
1677PyDoc_STRVAR(discard_doc,
1678"Remove an element from a set if it is a member.\n\
1679\n\
1680If the element is not a member, do nothing.");
1681
1682static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001683set_reduce(PySetObject *so)
1684{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001685 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001686
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001687 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001688 if (keys == NULL)
1689 goto done;
1690 args = PyTuple_Pack(1, keys);
1691 if (args == NULL)
1692 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001693 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1694 if (dict == NULL) {
1695 PyErr_Clear();
1696 dict = Py_None;
1697 Py_INCREF(dict);
1698 }
1699 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001700done:
1701 Py_XDECREF(args);
1702 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001703 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001704 return result;
1705}
1706
1707PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1708
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001709static int
1710set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1711{
1712 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001713
1714 if (!PyAnySet_Check(self))
1715 return -1;
1716 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1717 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001718 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001719 self->hash = -1;
1720 if (iterable == NULL)
1721 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001722 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001723}
1724
Raymond Hettingera690a992003-11-16 16:17:49 +00001725static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001726 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001727 0, /* sq_concat */
1728 0, /* sq_repeat */
1729 0, /* sq_item */
1730 0, /* sq_slice */
1731 0, /* sq_ass_item */
1732 0, /* sq_ass_slice */
1733 (objobjproc)set_contains, /* sq_contains */
1734};
1735
1736/* set object ********************************************************/
1737
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001738#ifdef Py_DEBUG
1739static PyObject *test_c_api(PySetObject *so);
1740
1741PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1742All is well if assertions don't fail.");
1743#endif
1744
Raymond Hettingera690a992003-11-16 16:17:49 +00001745static PyMethodDef set_methods[] = {
1746 {"add", (PyCFunction)set_add, METH_O,
1747 add_doc},
1748 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1749 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001750 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001751 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001752 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1753 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001754 {"discard", (PyCFunction)set_discard, METH_O,
1755 discard_doc},
1756 {"difference", (PyCFunction)set_difference, METH_O,
1757 difference_doc},
1758 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1759 difference_update_doc},
1760 {"intersection",(PyCFunction)set_intersection, METH_O,
1761 intersection_doc},
1762 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1763 intersection_update_doc},
1764 {"issubset", (PyCFunction)set_issubset, METH_O,
1765 issubset_doc},
1766 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1767 issuperset_doc},
1768 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1769 pop_doc},
1770 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1771 reduce_doc},
1772 {"remove", (PyCFunction)set_remove, METH_O,
1773 remove_doc},
1774 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1775 symmetric_difference_doc},
1776 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1777 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001778#ifdef Py_DEBUG
1779 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1780 test_c_api_doc},
1781#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001782 {"union", (PyCFunction)set_union, METH_O,
1783 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001784 {"update", (PyCFunction)set_update, METH_O,
1785 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001786 {NULL, NULL} /* sentinel */
1787};
1788
1789static PyNumberMethods set_as_number = {
1790 0, /*nb_add*/
1791 (binaryfunc)set_sub, /*nb_subtract*/
1792 0, /*nb_multiply*/
1793 0, /*nb_divide*/
1794 0, /*nb_remainder*/
1795 0, /*nb_divmod*/
1796 0, /*nb_power*/
1797 0, /*nb_negative*/
1798 0, /*nb_positive*/
1799 0, /*nb_absolute*/
1800 0, /*nb_nonzero*/
1801 0, /*nb_invert*/
1802 0, /*nb_lshift*/
1803 0, /*nb_rshift*/
1804 (binaryfunc)set_and, /*nb_and*/
1805 (binaryfunc)set_xor, /*nb_xor*/
1806 (binaryfunc)set_or, /*nb_or*/
1807 0, /*nb_coerce*/
1808 0, /*nb_int*/
1809 0, /*nb_long*/
1810 0, /*nb_float*/
1811 0, /*nb_oct*/
1812 0, /*nb_hex*/
1813 0, /*nb_inplace_add*/
1814 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1815 0, /*nb_inplace_multiply*/
1816 0, /*nb_inplace_divide*/
1817 0, /*nb_inplace_remainder*/
1818 0, /*nb_inplace_power*/
1819 0, /*nb_inplace_lshift*/
1820 0, /*nb_inplace_rshift*/
1821 (binaryfunc)set_iand, /*nb_inplace_and*/
1822 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1823 (binaryfunc)set_ior, /*nb_inplace_or*/
1824};
1825
1826PyDoc_STRVAR(set_doc,
1827"set(iterable) --> set object\n\
1828\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001829Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001830
1831PyTypeObject PySet_Type = {
1832 PyObject_HEAD_INIT(&PyType_Type)
1833 0, /* ob_size */
1834 "set", /* tp_name */
1835 sizeof(PySetObject), /* tp_basicsize */
1836 0, /* tp_itemsize */
1837 /* methods */
1838 (destructor)set_dealloc, /* tp_dealloc */
1839 (printfunc)set_tp_print, /* tp_print */
1840 0, /* tp_getattr */
1841 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001842 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001843 (reprfunc)set_repr, /* tp_repr */
1844 &set_as_number, /* tp_as_number */
1845 &set_as_sequence, /* tp_as_sequence */
1846 0, /* tp_as_mapping */
1847 set_nohash, /* tp_hash */
1848 0, /* tp_call */
1849 0, /* tp_str */
1850 PyObject_GenericGetAttr, /* tp_getattro */
1851 0, /* tp_setattro */
1852 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001853 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001854 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001855 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001856 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001857 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001858 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001859 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001860 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001861 0, /* tp_iternext */
1862 set_methods, /* tp_methods */
1863 0, /* tp_members */
1864 0, /* tp_getset */
1865 0, /* tp_base */
1866 0, /* tp_dict */
1867 0, /* tp_descr_get */
1868 0, /* tp_descr_set */
1869 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001870 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001871 PyType_GenericAlloc, /* tp_alloc */
1872 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001873 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001874};
1875
1876/* frozenset object ********************************************************/
1877
1878
1879static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001880 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001881 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001882 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001883 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001884 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001885 difference_doc},
1886 {"intersection",(PyCFunction)set_intersection, METH_O,
1887 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001888 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001889 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001890 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001891 issuperset_doc},
1892 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1893 reduce_doc},
1894 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1895 symmetric_difference_doc},
1896 {"union", (PyCFunction)set_union, METH_O,
1897 union_doc},
1898 {NULL, NULL} /* sentinel */
1899};
1900
1901static PyNumberMethods frozenset_as_number = {
1902 0, /*nb_add*/
1903 (binaryfunc)set_sub, /*nb_subtract*/
1904 0, /*nb_multiply*/
1905 0, /*nb_divide*/
1906 0, /*nb_remainder*/
1907 0, /*nb_divmod*/
1908 0, /*nb_power*/
1909 0, /*nb_negative*/
1910 0, /*nb_positive*/
1911 0, /*nb_absolute*/
1912 0, /*nb_nonzero*/
1913 0, /*nb_invert*/
1914 0, /*nb_lshift*/
1915 0, /*nb_rshift*/
1916 (binaryfunc)set_and, /*nb_and*/
1917 (binaryfunc)set_xor, /*nb_xor*/
1918 (binaryfunc)set_or, /*nb_or*/
1919};
1920
1921PyDoc_STRVAR(frozenset_doc,
1922"frozenset(iterable) --> frozenset object\n\
1923\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001924Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001925
1926PyTypeObject PyFrozenSet_Type = {
1927 PyObject_HEAD_INIT(&PyType_Type)
1928 0, /* ob_size */
1929 "frozenset", /* tp_name */
1930 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001931 0, /* tp_itemsize */
1932 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001933 (destructor)set_dealloc, /* tp_dealloc */
1934 (printfunc)set_tp_print, /* tp_print */
1935 0, /* tp_getattr */
1936 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001937 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001938 (reprfunc)set_repr, /* tp_repr */
1939 &frozenset_as_number, /* tp_as_number */
1940 &set_as_sequence, /* tp_as_sequence */
1941 0, /* tp_as_mapping */
1942 frozenset_hash, /* tp_hash */
1943 0, /* tp_call */
1944 0, /* tp_str */
1945 PyObject_GenericGetAttr, /* tp_getattro */
1946 0, /* tp_setattro */
1947 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001948 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001949 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001950 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001951 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001952 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001953 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001954 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001955 (getiterfunc)set_iter, /* tp_iter */
1956 0, /* tp_iternext */
1957 frozenset_methods, /* tp_methods */
1958 0, /* tp_members */
1959 0, /* tp_getset */
1960 0, /* tp_base */
1961 0, /* tp_dict */
1962 0, /* tp_descr_get */
1963 0, /* tp_descr_set */
1964 0, /* tp_dictoffset */
1965 0, /* tp_init */
1966 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001967 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001968 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001969};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001970
1971
1972/***** C API functions *************************************************/
1973
1974PyObject *
1975PySet_New(PyObject *iterable)
1976{
1977 return make_new_set(&PySet_Type, iterable);
1978}
1979
1980PyObject *
1981PyFrozenSet_New(PyObject *iterable)
1982{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001983 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001984
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001985 if (iterable == NULL)
1986 args = PyTuple_New(0);
1987 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001988 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001989 if (args == NULL)
1990 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001991 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001992 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001993 return result;
1994}
1995
Neal Norwitz8c49c822006-03-04 18:41:19 +00001996Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001997PySet_Size(PyObject *anyset)
1998{
1999 if (!PyAnySet_Check(anyset)) {
2000 PyErr_BadInternalCall();
2001 return -1;
2002 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002003 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002004}
2005
2006int
Barry Warsaw176014f2006-03-30 22:45:35 +00002007PySet_Clear(PyObject *set)
2008{
2009 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2010 PyErr_BadInternalCall();
2011 return -1;
2012 }
2013 return set_clear_internal((PySetObject *)set);
2014}
2015
2016int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002017PySet_Contains(PyObject *anyset, PyObject *key)
2018{
2019 if (!PyAnySet_Check(anyset)) {
2020 PyErr_BadInternalCall();
2021 return -1;
2022 }
2023 return set_contains_key((PySetObject *)anyset, key);
2024}
2025
2026int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002027PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002028{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002029 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002030 PyErr_BadInternalCall();
2031 return -1;
2032 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002033 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002034}
2035
2036int
2037PySet_Add(PyObject *set, PyObject *key)
2038{
2039 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2040 PyErr_BadInternalCall();
2041 return -1;
2042 }
2043 return set_add_key((PySetObject *)set, key);
2044}
2045
Barry Warsaw176014f2006-03-30 22:45:35 +00002046int
2047_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2048{
2049 setentry *entry_ptr;
2050
2051 if (!PyAnySet_Check(set)) {
2052 PyErr_BadInternalCall();
2053 return -1;
2054 }
2055 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2056 return 0;
2057 *entry = entry_ptr->key;
2058 return 1;
2059}
2060
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002061PyObject *
2062PySet_Pop(PyObject *set)
2063{
2064 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2065 PyErr_BadInternalCall();
2066 return NULL;
2067 }
2068 return set_pop((PySetObject *)set);
2069}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002070
Barry Warsaw176014f2006-03-30 22:45:35 +00002071int
2072_PySet_Update(PyObject *set, PyObject *iterable)
2073{
2074 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2075 PyErr_BadInternalCall();
2076 return -1;
2077 }
2078 return set_update_internal((PySetObject *)set, iterable);
2079}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002080
2081#ifdef Py_DEBUG
2082
2083/* Test code to be called with any three element set.
2084 Returns True and original set is restored. */
2085
2086#define assertRaises(call_return_value, exception) \
2087 do { \
2088 assert(call_return_value); \
2089 assert(PyErr_ExceptionMatches(exception)); \
2090 PyErr_Clear(); \
2091 } while(0)
2092
2093static PyObject *
2094test_c_api(PySetObject *so)
2095{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002096 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002097 char *s;
2098 Py_ssize_t i;
2099 PyObject *elem, *dup, *t, *f, *dup2;
2100 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002101
2102 /* Verify preconditions and exercise type/size checks */
2103 assert(PyAnySet_Check(ob));
2104 assert(PyAnySet_CheckExact(ob));
2105 assert(!PyFrozenSet_CheckExact(ob));
2106 assert(PySet_Size(ob) == 3);
2107 assert(PySet_GET_SIZE(ob) == 3);
2108
2109 /* Raise TypeError for non-iterable constructor arguments */
2110 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2111 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2112
2113 /* Raise TypeError for unhashable key */
2114 dup = PySet_New(ob);
2115 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2116 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2117 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2118
2119 /* Exercise successful pop, contains, add, and discard */
2120 elem = PySet_Pop(ob);
2121 assert(PySet_Contains(ob, elem) == 0);
2122 assert(PySet_GET_SIZE(ob) == 2);
2123 assert(PySet_Add(ob, elem) == 0);
2124 assert(PySet_Contains(ob, elem) == 1);
2125 assert(PySet_GET_SIZE(ob) == 3);
2126 assert(PySet_Discard(ob, elem) == 1);
2127 assert(PySet_GET_SIZE(ob) == 2);
2128 assert(PySet_Discard(ob, elem) == 0);
2129 assert(PySet_GET_SIZE(ob) == 2);
2130
Barry Warsaw176014f2006-03-30 22:45:35 +00002131 /* Exercise clear */
2132 dup2 = PySet_New(dup);
2133 assert(PySet_Clear(dup2) == 0);
2134 assert(PySet_Size(dup2) == 0);
2135 Py_DECREF(dup2);
2136
2137 /* Raise SystemError on clear or update of frozen set */
2138 f = PyFrozenSet_New(dup);
2139 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2140 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2141 Py_DECREF(f);
2142
2143 /* Exercise direct iteration */
2144 i = 0, count = 0;
2145 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2146 s = PyString_AsString(elem);
2147 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2148 count++;
2149 }
2150 assert(count == 3);
2151
2152 /* Exercise updates */
2153 dup2 = PySet_New(NULL);
2154 assert(_PySet_Update(dup2, dup) == 0);
2155 assert(PySet_Size(dup2) == 3);
2156 assert(_PySet_Update(dup2, dup) == 0);
2157 assert(PySet_Size(dup2) == 3);
2158 Py_DECREF(dup2);
2159
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002160 /* Raise SystemError when self argument is not a set or frozenset. */
2161 t = PyTuple_New(0);
2162 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2163 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2164 Py_DECREF(t);
2165
2166 /* Raise SystemError when self argument is not a set. */
2167 f = PyFrozenSet_New(dup);
2168 assert(PySet_Size(f) == 3);
2169 assert(PyFrozenSet_CheckExact(f));
2170 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2171 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2172 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2173 Py_DECREF(f);
2174
2175 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002176 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2177 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002178 assert(PySet_GET_SIZE(ob) == 0);
2179 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2180
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002181 /* Restore the set from the copy using the PyNumber API */
2182 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2183 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002184
2185 /* Verify constructors accept NULL arguments */
2186 f = PySet_New(NULL);
2187 assert(f != NULL);
2188 assert(PySet_GET_SIZE(f) == 0);
2189 Py_DECREF(f);
2190 f = PyFrozenSet_New(NULL);
2191 assert(f != NULL);
2192 assert(PyFrozenSet_CheckExact(f));
2193 assert(PySet_GET_SIZE(f) == 0);
2194 Py_DECREF(f);
2195
2196 Py_DECREF(elem);
2197 Py_DECREF(dup);
2198 Py_RETURN_TRUE;
2199}
2200
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002201#undef assertRaises
2202
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002203#endif