blob: 1b9d70de9b8b44941656a02281a5e6cb214801d5 [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 Norwitza5ccda92006-10-28 21:16:54 +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);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +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;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +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)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +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) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001167 int rv;
1168 setentry entry;
1169 long hash = PyObject_Hash(key);
1170
1171 if (hash == -1) {
1172 Py_DECREF(it);
1173 Py_DECREF(result);
1174 Py_DECREF(key);
1175 return NULL;
1176 }
1177 entry.hash = hash;
1178 entry.key = key;
1179 rv = set_contains_entry(so, &entry);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001180 if (rv == -1) {
1181 Py_DECREF(it);
1182 Py_DECREF(result);
1183 Py_DECREF(key);
1184 return NULL;
1185 }
1186 if (rv) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001187 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001188 Py_DECREF(it);
1189 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001190 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001191 return NULL;
1192 }
1193 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001194 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001195 }
1196 Py_DECREF(it);
1197 if (PyErr_Occurred()) {
1198 Py_DECREF(result);
1199 return NULL;
1200 }
1201 return (PyObject *)result;
1202}
1203
1204PyDoc_STRVAR(intersection_doc,
1205"Return the intersection of two sets as a new set.\n\
1206\n\
1207(i.e. all elements that are in both sets.)");
1208
1209static PyObject *
1210set_intersection_update(PySetObject *so, PyObject *other)
1211{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001212 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001213
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001214 tmp = set_intersection(so, other);
1215 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001216 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001217 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001218 Py_DECREF(tmp);
1219 Py_RETURN_NONE;
1220}
1221
1222PyDoc_STRVAR(intersection_update_doc,
1223"Update a set with the intersection of itself and another.");
1224
1225static PyObject *
1226set_and(PySetObject *so, PyObject *other)
1227{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001228 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001229 Py_INCREF(Py_NotImplemented);
1230 return Py_NotImplemented;
1231 }
1232 return set_intersection(so, other);
1233}
1234
1235static PyObject *
1236set_iand(PySetObject *so, PyObject *other)
1237{
1238 PyObject *result;
1239
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001240 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001241 Py_INCREF(Py_NotImplemented);
1242 return Py_NotImplemented;
1243 }
1244 result = set_intersection_update(so, other);
1245 if (result == NULL)
1246 return NULL;
1247 Py_DECREF(result);
1248 Py_INCREF(so);
1249 return (PyObject *)so;
1250}
1251
Neal Norwitz6576bd82005-11-13 18:41:28 +00001252static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001253set_difference_update_internal(PySetObject *so, PyObject *other)
1254{
1255 if ((PyObject *)so == other)
1256 return set_clear_internal(so);
1257
1258 if (PyAnySet_Check(other)) {
1259 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001260 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001261
1262 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001263 if (set_discard_entry(so, entry) == -1)
1264 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001265 } else {
1266 PyObject *key, *it;
1267 it = PyObject_GetIter(other);
1268 if (it == NULL)
1269 return -1;
1270
1271 while ((key = PyIter_Next(it)) != NULL) {
1272 if (set_discard_key(so, key) == -1) {
1273 Py_DECREF(it);
1274 Py_DECREF(key);
1275 return -1;
1276 }
1277 Py_DECREF(key);
1278 }
1279 Py_DECREF(it);
1280 if (PyErr_Occurred())
1281 return -1;
1282 }
1283 /* If more than 1/5 are dummies, then resize them away. */
1284 if ((so->fill - so->used) * 5 < so->mask)
1285 return 0;
1286 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1287}
1288
Raymond Hettingera690a992003-11-16 16:17:49 +00001289static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001290set_difference_update(PySetObject *so, PyObject *other)
1291{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001292 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001293 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001294 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001295}
1296
1297PyDoc_STRVAR(difference_update_doc,
1298"Remove all elements of another set from this set.");
1299
1300static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001301set_difference(PySetObject *so, PyObject *other)
1302{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001303 PyObject *result;
1304 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001305 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001306
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001307 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001308 result = set_copy(so);
1309 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001310 return NULL;
1311 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001312 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001313 Py_DECREF(result);
1314 return NULL;
1315 }
1316
1317 result = make_new_set(so->ob_type, NULL);
1318 if (result == NULL)
1319 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001320
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001321 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001322 while (set_next(so, &pos, &entry)) {
1323 setentry entrycopy;
1324 entrycopy.hash = entry->hash;
1325 entrycopy.key = entry->key;
1326 if (!PyDict_Contains(other, entry->key)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001327 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1328 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001329 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001330 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001331 }
1332 }
1333 return result;
1334 }
1335
Raymond Hettingerc991db22005-08-11 07:58:45 +00001336 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001337 int rv = set_contains_entry((PySetObject *)other, entry);
1338 if (rv == -1) {
1339 Py_DECREF(result);
1340 return NULL;
1341 }
1342 if (!rv) {
1343 if (set_add_entry((PySetObject *)result, entry) == -1) {
1344 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001345 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001346 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001347 }
1348 }
1349 return result;
1350}
1351
1352PyDoc_STRVAR(difference_doc,
1353"Return the difference of two sets as a new set.\n\
1354\n\
1355(i.e. all elements that are in this set but not the other.)");
1356static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001357set_sub(PySetObject *so, PyObject *other)
1358{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001359 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001360 Py_INCREF(Py_NotImplemented);
1361 return Py_NotImplemented;
1362 }
1363 return set_difference(so, other);
1364}
1365
1366static PyObject *
1367set_isub(PySetObject *so, PyObject *other)
1368{
1369 PyObject *result;
1370
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001371 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001372 Py_INCREF(Py_NotImplemented);
1373 return Py_NotImplemented;
1374 }
1375 result = set_difference_update(so, other);
1376 if (result == NULL)
1377 return NULL;
1378 Py_DECREF(result);
1379 Py_INCREF(so);
1380 return (PyObject *)so;
1381}
1382
1383static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001384set_symmetric_difference_update(PySetObject *so, PyObject *other)
1385{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001386 PySetObject *otherset;
1387 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001388 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001389 setentry *entry;
1390
1391 if ((PyObject *)so == other)
1392 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001393
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001394 if (PyDict_Check(other)) {
1395 PyObject *value;
1396 int rv;
1397 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001398 setentry an_entry;
1399 long hash = PyObject_Hash(key);
1400
1401 if (hash == -1)
1402 return NULL;
1403 an_entry.hash = hash;
1404 an_entry.key = key;
1405 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001406 if (rv == -1)
1407 return NULL;
1408 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001409 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001410 return NULL;
1411 }
1412 }
1413 Py_RETURN_NONE;
1414 }
1415
1416 if (PyAnySet_Check(other)) {
1417 Py_INCREF(other);
1418 otherset = (PySetObject *)other;
1419 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001420 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1421 if (otherset == NULL)
1422 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001423 }
1424
Raymond Hettingerc991db22005-08-11 07:58:45 +00001425 while (set_next(otherset, &pos, &entry)) {
1426 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001427 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001428 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001429 return NULL;
1430 }
1431 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001432 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001433 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001434 return NULL;
1435 }
1436 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001437 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001438 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001439 Py_RETURN_NONE;
1440}
1441
1442PyDoc_STRVAR(symmetric_difference_update_doc,
1443"Update a set with the symmetric difference of itself and another.");
1444
1445static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001446set_symmetric_difference(PySetObject *so, PyObject *other)
1447{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001448 PyObject *rv;
1449 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001450
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001451 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1452 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001453 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001454 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1455 if (rv == NULL)
1456 return NULL;
1457 Py_DECREF(rv);
1458 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001459}
1460
1461PyDoc_STRVAR(symmetric_difference_doc,
1462"Return the symmetric difference of two sets as a new set.\n\
1463\n\
1464(i.e. all elements that are in exactly one of the sets.)");
1465
1466static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001467set_xor(PySetObject *so, PyObject *other)
1468{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001469 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001470 Py_INCREF(Py_NotImplemented);
1471 return Py_NotImplemented;
1472 }
1473 return set_symmetric_difference(so, other);
1474}
1475
1476static PyObject *
1477set_ixor(PySetObject *so, PyObject *other)
1478{
1479 PyObject *result;
1480
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001481 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001482 Py_INCREF(Py_NotImplemented);
1483 return Py_NotImplemented;
1484 }
1485 result = set_symmetric_difference_update(so, other);
1486 if (result == NULL)
1487 return NULL;
1488 Py_DECREF(result);
1489 Py_INCREF(so);
1490 return (PyObject *)so;
1491}
1492
1493static PyObject *
1494set_issubset(PySetObject *so, PyObject *other)
1495{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001496 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001497 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001498
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001499 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001500 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001501 tmp = make_new_set(&PySet_Type, other);
1502 if (tmp == NULL)
1503 return NULL;
1504 result = set_issubset(so, tmp);
1505 Py_DECREF(tmp);
1506 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001507 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001508 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001509 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001510
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001511 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001512 int rv = set_contains_entry((PySetObject *)other, entry);
1513 if (rv == -1)
1514 return NULL;
1515 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001516 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001517 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001518 Py_RETURN_TRUE;
1519}
1520
1521PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1522
1523static PyObject *
1524set_issuperset(PySetObject *so, PyObject *other)
1525{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001526 PyObject *tmp, *result;
1527
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001528 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001529 tmp = make_new_set(&PySet_Type, other);
1530 if (tmp == NULL)
1531 return NULL;
1532 result = set_issuperset(so, tmp);
1533 Py_DECREF(tmp);
1534 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001535 }
1536 return set_issubset((PySetObject *)other, (PyObject *)so);
1537}
1538
1539PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1540
Raymond Hettingera690a992003-11-16 16:17:49 +00001541static PyObject *
1542set_richcompare(PySetObject *v, PyObject *w, int op)
1543{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001544 PyObject *r1, *r2;
1545
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001546 if(!PyAnySet_Check(w)) {
1547 if (op == Py_EQ)
1548 Py_RETURN_FALSE;
1549 if (op == Py_NE)
1550 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001551 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1552 return NULL;
1553 }
1554 switch (op) {
1555 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001556 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001557 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001558 if (v->hash != -1 &&
1559 ((PySetObject *)w)->hash != -1 &&
1560 v->hash != ((PySetObject *)w)->hash)
1561 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001562 return set_issubset(v, w);
1563 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001564 r1 = set_richcompare(v, w, Py_EQ);
1565 if (r1 == NULL)
1566 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001567 r2 = PyBool_FromLong(PyObject_Not(r1));
1568 Py_DECREF(r1);
1569 return r2;
1570 case Py_LE:
1571 return set_issubset(v, w);
1572 case Py_GE:
1573 return set_issuperset(v, w);
1574 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001575 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001576 Py_RETURN_FALSE;
1577 return set_issubset(v, w);
1578 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001579 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001580 Py_RETURN_FALSE;
1581 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001582 }
1583 Py_INCREF(Py_NotImplemented);
1584 return Py_NotImplemented;
1585}
1586
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001587static int
Georg Brandl347b3002006-03-30 11:57:00 +00001588set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001589{
1590 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1591 return -1;
1592}
1593
Raymond Hettingera690a992003-11-16 16:17:49 +00001594static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001595set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001596{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001597 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001598 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001599 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001600}
1601
1602PyDoc_STRVAR(add_doc,
1603"Add an element to a set.\n\
1604\n\
1605This has no effect if the element is already present.");
1606
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001607static int
1608set_contains(PySetObject *so, PyObject *key)
1609{
1610 PyObject *tmpkey;
1611 int rv;
1612
1613 rv = set_contains_key(so, key);
1614 if (rv == -1) {
1615 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1616 return -1;
1617 PyErr_Clear();
1618 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1619 if (tmpkey == NULL)
1620 return -1;
1621 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1622 rv = set_contains(so, tmpkey);
1623 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1624 Py_DECREF(tmpkey);
1625 }
1626 return rv;
1627}
1628
1629static PyObject *
1630set_direct_contains(PySetObject *so, PyObject *key)
1631{
1632 long result;
1633
1634 result = set_contains(so, key);
1635 if (result == -1)
1636 return NULL;
1637 return PyBool_FromLong(result);
1638}
1639
1640PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1641
Raymond Hettingera690a992003-11-16 16:17:49 +00001642static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001643set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001644{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001645 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001646 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001647
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001648 rv = set_discard_key(so, key);
1649 if (rv == -1) {
1650 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1651 return NULL;
1652 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001653 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1654 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001655 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001656 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001657 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001658 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001659 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001660 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001661 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001662 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001663 return NULL;
1664 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001665 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001666}
1667
1668PyDoc_STRVAR(remove_doc,
1669"Remove an element from a set; it must be a member.\n\
1670\n\
1671If the element is not a member, raise a KeyError.");
1672
1673static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001674set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001675{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001676 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001677 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001678
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001679 rv = set_discard_key(so, key);
1680 if (rv == -1) {
1681 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1682 return NULL;
1683 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001684 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1685 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001686 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001687 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001688 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001689 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001690 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001691 return result;
1692 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001693 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001694}
1695
1696PyDoc_STRVAR(discard_doc,
1697"Remove an element from a set if it is a member.\n\
1698\n\
1699If the element is not a member, do nothing.");
1700
1701static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001702set_reduce(PySetObject *so)
1703{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001704 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001705
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001706 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001707 if (keys == NULL)
1708 goto done;
1709 args = PyTuple_Pack(1, keys);
1710 if (args == NULL)
1711 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001712 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1713 if (dict == NULL) {
1714 PyErr_Clear();
1715 dict = Py_None;
1716 Py_INCREF(dict);
1717 }
1718 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001719done:
1720 Py_XDECREF(args);
1721 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001722 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001723 return result;
1724}
1725
1726PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1727
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001728static int
1729set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1730{
1731 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001732
1733 if (!PyAnySet_Check(self))
1734 return -1;
1735 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1736 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001737 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001738 self->hash = -1;
1739 if (iterable == NULL)
1740 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001741 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001742}
1743
Raymond Hettingera690a992003-11-16 16:17:49 +00001744static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001745 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001746 0, /* sq_concat */
1747 0, /* sq_repeat */
1748 0, /* sq_item */
1749 0, /* sq_slice */
1750 0, /* sq_ass_item */
1751 0, /* sq_ass_slice */
1752 (objobjproc)set_contains, /* sq_contains */
1753};
1754
1755/* set object ********************************************************/
1756
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001757#ifdef Py_DEBUG
1758static PyObject *test_c_api(PySetObject *so);
1759
1760PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1761All is well if assertions don't fail.");
1762#endif
1763
Raymond Hettingera690a992003-11-16 16:17:49 +00001764static PyMethodDef set_methods[] = {
1765 {"add", (PyCFunction)set_add, METH_O,
1766 add_doc},
1767 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1768 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001769 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001770 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001771 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1772 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001773 {"discard", (PyCFunction)set_discard, METH_O,
1774 discard_doc},
1775 {"difference", (PyCFunction)set_difference, METH_O,
1776 difference_doc},
1777 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1778 difference_update_doc},
1779 {"intersection",(PyCFunction)set_intersection, METH_O,
1780 intersection_doc},
1781 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1782 intersection_update_doc},
1783 {"issubset", (PyCFunction)set_issubset, METH_O,
1784 issubset_doc},
1785 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1786 issuperset_doc},
1787 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1788 pop_doc},
1789 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1790 reduce_doc},
1791 {"remove", (PyCFunction)set_remove, METH_O,
1792 remove_doc},
1793 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1794 symmetric_difference_doc},
1795 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1796 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001797#ifdef Py_DEBUG
1798 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1799 test_c_api_doc},
1800#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001801 {"union", (PyCFunction)set_union, METH_O,
1802 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001803 {"update", (PyCFunction)set_update, METH_O,
1804 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001805 {NULL, NULL} /* sentinel */
1806};
1807
1808static PyNumberMethods set_as_number = {
1809 0, /*nb_add*/
1810 (binaryfunc)set_sub, /*nb_subtract*/
1811 0, /*nb_multiply*/
1812 0, /*nb_divide*/
1813 0, /*nb_remainder*/
1814 0, /*nb_divmod*/
1815 0, /*nb_power*/
1816 0, /*nb_negative*/
1817 0, /*nb_positive*/
1818 0, /*nb_absolute*/
1819 0, /*nb_nonzero*/
1820 0, /*nb_invert*/
1821 0, /*nb_lshift*/
1822 0, /*nb_rshift*/
1823 (binaryfunc)set_and, /*nb_and*/
1824 (binaryfunc)set_xor, /*nb_xor*/
1825 (binaryfunc)set_or, /*nb_or*/
1826 0, /*nb_coerce*/
1827 0, /*nb_int*/
1828 0, /*nb_long*/
1829 0, /*nb_float*/
1830 0, /*nb_oct*/
1831 0, /*nb_hex*/
1832 0, /*nb_inplace_add*/
1833 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1834 0, /*nb_inplace_multiply*/
1835 0, /*nb_inplace_divide*/
1836 0, /*nb_inplace_remainder*/
1837 0, /*nb_inplace_power*/
1838 0, /*nb_inplace_lshift*/
1839 0, /*nb_inplace_rshift*/
1840 (binaryfunc)set_iand, /*nb_inplace_and*/
1841 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1842 (binaryfunc)set_ior, /*nb_inplace_or*/
1843};
1844
1845PyDoc_STRVAR(set_doc,
1846"set(iterable) --> set object\n\
1847\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001848Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001849
1850PyTypeObject PySet_Type = {
1851 PyObject_HEAD_INIT(&PyType_Type)
1852 0, /* ob_size */
1853 "set", /* tp_name */
1854 sizeof(PySetObject), /* tp_basicsize */
1855 0, /* tp_itemsize */
1856 /* methods */
1857 (destructor)set_dealloc, /* tp_dealloc */
1858 (printfunc)set_tp_print, /* tp_print */
1859 0, /* tp_getattr */
1860 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001861 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001862 (reprfunc)set_repr, /* tp_repr */
1863 &set_as_number, /* tp_as_number */
1864 &set_as_sequence, /* tp_as_sequence */
1865 0, /* tp_as_mapping */
1866 set_nohash, /* tp_hash */
1867 0, /* tp_call */
1868 0, /* tp_str */
1869 PyObject_GenericGetAttr, /* tp_getattro */
1870 0, /* tp_setattro */
1871 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001872 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001873 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001874 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001875 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001876 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001877 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001878 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001879 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001880 0, /* tp_iternext */
1881 set_methods, /* tp_methods */
1882 0, /* tp_members */
1883 0, /* tp_getset */
1884 0, /* tp_base */
1885 0, /* tp_dict */
1886 0, /* tp_descr_get */
1887 0, /* tp_descr_set */
1888 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001889 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001890 PyType_GenericAlloc, /* tp_alloc */
1891 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001892 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001893};
1894
1895/* frozenset object ********************************************************/
1896
1897
1898static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001899 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001900 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001901 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001902 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001903 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001904 difference_doc},
1905 {"intersection",(PyCFunction)set_intersection, METH_O,
1906 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001907 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001908 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001909 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001910 issuperset_doc},
1911 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1912 reduce_doc},
1913 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1914 symmetric_difference_doc},
1915 {"union", (PyCFunction)set_union, METH_O,
1916 union_doc},
1917 {NULL, NULL} /* sentinel */
1918};
1919
1920static PyNumberMethods frozenset_as_number = {
1921 0, /*nb_add*/
1922 (binaryfunc)set_sub, /*nb_subtract*/
1923 0, /*nb_multiply*/
1924 0, /*nb_divide*/
1925 0, /*nb_remainder*/
1926 0, /*nb_divmod*/
1927 0, /*nb_power*/
1928 0, /*nb_negative*/
1929 0, /*nb_positive*/
1930 0, /*nb_absolute*/
1931 0, /*nb_nonzero*/
1932 0, /*nb_invert*/
1933 0, /*nb_lshift*/
1934 0, /*nb_rshift*/
1935 (binaryfunc)set_and, /*nb_and*/
1936 (binaryfunc)set_xor, /*nb_xor*/
1937 (binaryfunc)set_or, /*nb_or*/
1938};
1939
1940PyDoc_STRVAR(frozenset_doc,
1941"frozenset(iterable) --> frozenset object\n\
1942\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001943Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001944
1945PyTypeObject PyFrozenSet_Type = {
1946 PyObject_HEAD_INIT(&PyType_Type)
1947 0, /* ob_size */
1948 "frozenset", /* tp_name */
1949 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001950 0, /* tp_itemsize */
1951 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001952 (destructor)set_dealloc, /* tp_dealloc */
1953 (printfunc)set_tp_print, /* tp_print */
1954 0, /* tp_getattr */
1955 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001956 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001957 (reprfunc)set_repr, /* tp_repr */
1958 &frozenset_as_number, /* tp_as_number */
1959 &set_as_sequence, /* tp_as_sequence */
1960 0, /* tp_as_mapping */
1961 frozenset_hash, /* tp_hash */
1962 0, /* tp_call */
1963 0, /* tp_str */
1964 PyObject_GenericGetAttr, /* tp_getattro */
1965 0, /* tp_setattro */
1966 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001967 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001968 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001969 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001970 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001971 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001972 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001973 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001974 (getiterfunc)set_iter, /* tp_iter */
1975 0, /* tp_iternext */
1976 frozenset_methods, /* tp_methods */
1977 0, /* tp_members */
1978 0, /* tp_getset */
1979 0, /* tp_base */
1980 0, /* tp_dict */
1981 0, /* tp_descr_get */
1982 0, /* tp_descr_set */
1983 0, /* tp_dictoffset */
1984 0, /* tp_init */
1985 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001986 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001987 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001988};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001989
1990
1991/***** C API functions *************************************************/
1992
1993PyObject *
1994PySet_New(PyObject *iterable)
1995{
1996 return make_new_set(&PySet_Type, iterable);
1997}
1998
1999PyObject *
2000PyFrozenSet_New(PyObject *iterable)
2001{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002002 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002003
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002004 if (iterable == NULL)
2005 args = PyTuple_New(0);
2006 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002007 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002008 if (args == NULL)
2009 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002010 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002011 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002012 return result;
2013}
2014
Neal Norwitz8c49c822006-03-04 18:41:19 +00002015Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002016PySet_Size(PyObject *anyset)
2017{
2018 if (!PyAnySet_Check(anyset)) {
2019 PyErr_BadInternalCall();
2020 return -1;
2021 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002022 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002023}
2024
2025int
Barry Warsaw176014f2006-03-30 22:45:35 +00002026PySet_Clear(PyObject *set)
2027{
2028 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2029 PyErr_BadInternalCall();
2030 return -1;
2031 }
2032 return set_clear_internal((PySetObject *)set);
2033}
2034
2035int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002036PySet_Contains(PyObject *anyset, PyObject *key)
2037{
2038 if (!PyAnySet_Check(anyset)) {
2039 PyErr_BadInternalCall();
2040 return -1;
2041 }
2042 return set_contains_key((PySetObject *)anyset, key);
2043}
2044
2045int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002046PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002047{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002048 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002049 PyErr_BadInternalCall();
2050 return -1;
2051 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002052 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002053}
2054
2055int
2056PySet_Add(PyObject *set, PyObject *key)
2057{
2058 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2059 PyErr_BadInternalCall();
2060 return -1;
2061 }
2062 return set_add_key((PySetObject *)set, key);
2063}
2064
Barry Warsaw176014f2006-03-30 22:45:35 +00002065int
2066_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2067{
2068 setentry *entry_ptr;
2069
2070 if (!PyAnySet_Check(set)) {
2071 PyErr_BadInternalCall();
2072 return -1;
2073 }
2074 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2075 return 0;
2076 *entry = entry_ptr->key;
2077 return 1;
2078}
2079
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002080PyObject *
2081PySet_Pop(PyObject *set)
2082{
2083 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2084 PyErr_BadInternalCall();
2085 return NULL;
2086 }
2087 return set_pop((PySetObject *)set);
2088}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002089
Barry Warsaw176014f2006-03-30 22:45:35 +00002090int
2091_PySet_Update(PyObject *set, PyObject *iterable)
2092{
2093 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2094 PyErr_BadInternalCall();
2095 return -1;
2096 }
2097 return set_update_internal((PySetObject *)set, iterable);
2098}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002099
2100#ifdef Py_DEBUG
2101
2102/* Test code to be called with any three element set.
2103 Returns True and original set is restored. */
2104
2105#define assertRaises(call_return_value, exception) \
2106 do { \
2107 assert(call_return_value); \
2108 assert(PyErr_ExceptionMatches(exception)); \
2109 PyErr_Clear(); \
2110 } while(0)
2111
2112static PyObject *
2113test_c_api(PySetObject *so)
2114{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002115 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002116 char *s;
2117 Py_ssize_t i;
2118 PyObject *elem, *dup, *t, *f, *dup2;
2119 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002120
2121 /* Verify preconditions and exercise type/size checks */
2122 assert(PyAnySet_Check(ob));
2123 assert(PyAnySet_CheckExact(ob));
2124 assert(!PyFrozenSet_CheckExact(ob));
2125 assert(PySet_Size(ob) == 3);
2126 assert(PySet_GET_SIZE(ob) == 3);
2127
2128 /* Raise TypeError for non-iterable constructor arguments */
2129 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2130 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2131
2132 /* Raise TypeError for unhashable key */
2133 dup = PySet_New(ob);
2134 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2135 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2136 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2137
2138 /* Exercise successful pop, contains, add, and discard */
2139 elem = PySet_Pop(ob);
2140 assert(PySet_Contains(ob, elem) == 0);
2141 assert(PySet_GET_SIZE(ob) == 2);
2142 assert(PySet_Add(ob, elem) == 0);
2143 assert(PySet_Contains(ob, elem) == 1);
2144 assert(PySet_GET_SIZE(ob) == 3);
2145 assert(PySet_Discard(ob, elem) == 1);
2146 assert(PySet_GET_SIZE(ob) == 2);
2147 assert(PySet_Discard(ob, elem) == 0);
2148 assert(PySet_GET_SIZE(ob) == 2);
2149
Barry Warsaw176014f2006-03-30 22:45:35 +00002150 /* Exercise clear */
2151 dup2 = PySet_New(dup);
2152 assert(PySet_Clear(dup2) == 0);
2153 assert(PySet_Size(dup2) == 0);
2154 Py_DECREF(dup2);
2155
2156 /* Raise SystemError on clear or update of frozen set */
2157 f = PyFrozenSet_New(dup);
2158 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2159 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2160 Py_DECREF(f);
2161
2162 /* Exercise direct iteration */
2163 i = 0, count = 0;
2164 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2165 s = PyString_AsString(elem);
2166 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2167 count++;
2168 }
2169 assert(count == 3);
2170
2171 /* Exercise updates */
2172 dup2 = PySet_New(NULL);
2173 assert(_PySet_Update(dup2, dup) == 0);
2174 assert(PySet_Size(dup2) == 3);
2175 assert(_PySet_Update(dup2, dup) == 0);
2176 assert(PySet_Size(dup2) == 3);
2177 Py_DECREF(dup2);
2178
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002179 /* Raise SystemError when self argument is not a set or frozenset. */
2180 t = PyTuple_New(0);
2181 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2182 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2183 Py_DECREF(t);
2184
2185 /* Raise SystemError when self argument is not a set. */
2186 f = PyFrozenSet_New(dup);
2187 assert(PySet_Size(f) == 3);
2188 assert(PyFrozenSet_CheckExact(f));
2189 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2190 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2191 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2192 Py_DECREF(f);
2193
2194 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002195 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2196 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002197 assert(PySet_GET_SIZE(ob) == 0);
2198 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2199
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002200 /* Restore the set from the copy using the PyNumber API */
2201 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2202 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002203
2204 /* Verify constructors accept NULL arguments */
2205 f = PySet_New(NULL);
2206 assert(f != NULL);
2207 assert(PySet_GET_SIZE(f) == 0);
2208 Py_DECREF(f);
2209 f = PyFrozenSet_New(NULL);
2210 assert(f != NULL);
2211 assert(PyFrozenSet_CheckExact(f));
2212 assert(PySet_GET_SIZE(f) == 0);
2213 Py_DECREF(f);
2214
2215 Py_DECREF(elem);
2216 Py_DECREF(dup);
2217 Py_RETURN_TRUE;
2218}
2219
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002220#undef assertRaises
2221
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002222#endif