blob: 99db926a0deb8ab65b7d06025096bca97e8cba32 [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 Hettinger0c850862006-12-08 04:24:33 +0000188Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000189Eats 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/*
Raymond Hettinger0c850862006-12-08 04:24:33 +0000221Internal routine used by set_table_resize() to insert an item which is
222known to be absent from the set. This routine also assumes that
223the set contains no deleted entries. Besides the performance benefit,
224using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
225Note that no refcounts are changed by this routine; if needed, the caller
226is responsible for incref'ing `key`.
227*/
228static void
229set_insert_clean(register PySetObject *so, PyObject *key, long hash)
230{
231 register size_t i;
232 register size_t perturb;
233 register size_t mask = (size_t)so->mask;
234 setentry *table = so->table;
235 register setentry *entry;
236
237 i = hash & mask;
238 entry = &table[i];
239 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
240 i = (i << 2) + i + perturb + 1;
241 entry = &table[i & mask];
242 }
243 so->fill++;
244 entry->key = key;
245 entry->hash = hash;
246 so->used++;
247}
248
249/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000250Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000251keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000252actually be smaller than the old one.
253*/
254static int
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000255set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000256{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000257 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000258 setentry *oldtable, *newtable, *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000259 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000260 int is_oldtable_malloced;
261 setentry small_copy[PySet_MINSIZE];
262
263 assert(minused >= 0);
264
265 /* Find the smallest table size > minused. */
266 for (newsize = PySet_MINSIZE;
267 newsize <= minused && newsize > 0;
268 newsize <<= 1)
269 ;
270 if (newsize <= 0) {
271 PyErr_NoMemory();
272 return -1;
273 }
274
275 /* Get space for a new table. */
276 oldtable = so->table;
277 assert(oldtable != NULL);
278 is_oldtable_malloced = oldtable != so->smalltable;
279
280 if (newsize == PySet_MINSIZE) {
281 /* A large table is shrinking, or we can't get any smaller. */
282 newtable = so->smalltable;
283 if (newtable == oldtable) {
284 if (so->fill == so->used) {
285 /* No dummies, so no point doing anything. */
286 return 0;
287 }
288 /* We're not going to resize it, but rebuild the
289 table anyway to purge old dummy entries.
290 Subtle: This is *necessary* if fill==size,
291 as set_lookkey needs at least one virgin slot to
292 terminate failing searches. If fill < size, it's
293 merely desirable, as dummies slow searches. */
294 assert(so->fill > so->used);
295 memcpy(small_copy, oldtable, sizeof(small_copy));
296 oldtable = small_copy;
297 }
298 }
299 else {
300 newtable = PyMem_NEW(setentry, newsize);
301 if (newtable == NULL) {
302 PyErr_NoMemory();
303 return -1;
304 }
305 }
306
307 /* Make the set empty, using the new table. */
308 assert(newtable != oldtable);
309 so->table = newtable;
310 so->mask = newsize - 1;
311 memset(newtable, 0, sizeof(setentry) * newsize);
312 so->used = 0;
313 i = so->fill;
314 so->fill = 0;
315
316 /* Copy the data over; this is refcount-neutral for active entries;
317 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000318 for (entry = oldtable; i > 0; entry++) {
319 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000320 /* UNUSED */
321 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000322 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323 /* DUMMY */
324 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000325 assert(entry->key == dummy);
326 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000327 } else {
328 /* ACTIVE */
329 --i;
Raymond Hettinger0c850862006-12-08 04:24:33 +0000330 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331 }
332 }
333
334 if (is_oldtable_malloced)
335 PyMem_DEL(oldtable);
336 return 0;
337}
338
Raymond Hettingerc991db22005-08-11 07:58:45 +0000339/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
340
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000342set_add_entry(register PySetObject *so, setentry *entry)
343{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000344 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000345
346 assert(so->fill <= so->mask); /* at least one empty slot */
347 n_used = so->used;
348 Py_INCREF(entry->key);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000349 if (set_insert_key(so, entry->key, entry->hash) == -1) {
350 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000351 return -1;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000352 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000353 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
354 return 0;
355 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
356}
357
358static int
359set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360{
361 register long hash;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000362 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363
Raymond Hettingerc991db22005-08-11 07:58:45 +0000364 if (!PyString_CheckExact(key) ||
365 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000366 hash = PyObject_Hash(key);
367 if (hash == -1)
368 return -1;
369 }
370 assert(so->fill <= so->mask); /* at least one empty slot */
371 n_used = so->used;
372 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000373 if (set_insert_key(so, key, hash) == -1) {
374 Py_DECREF(key);
375 return -1;
376 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
378 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000379 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380}
381
382#define DISCARD_NOTFOUND 0
383#define DISCARD_FOUND 1
384
385static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000386set_discard_entry(PySetObject *so, setentry *oldentry)
387{ register setentry *entry;
388 PyObject *old_key;
389
390 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000391 if (entry == NULL)
392 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000393 if (entry->key == NULL || entry->key == dummy)
394 return DISCARD_NOTFOUND;
395 old_key = entry->key;
396 Py_INCREF(dummy);
397 entry->key = dummy;
398 so->used--;
399 Py_DECREF(old_key);
400 return DISCARD_FOUND;
401}
402
403static int
404set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405{
406 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000407 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000408 PyObject *old_key;
409
410 assert (PyAnySet_Check(so));
411 if (!PyString_CheckExact(key) ||
412 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
413 hash = PyObject_Hash(key);
414 if (hash == -1)
415 return -1;
416 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000417 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000418 if (entry == NULL)
419 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000420 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000422 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000423 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000424 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000425 so->used--;
426 Py_DECREF(old_key);
427 return DISCARD_FOUND;
428}
429
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000430static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431set_clear_internal(PySetObject *so)
432{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000433 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000434 int table_is_malloced;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000435 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000436 setentry small_copy[PySet_MINSIZE];
437#ifdef Py_DEBUG
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000438 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000440
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000441 n = so->mask + 1;
442 i = 0;
443#endif
444
445 table = so->table;
446 assert(table != NULL);
447 table_is_malloced = table != so->smalltable;
448
449 /* This is delicate. During the process of clearing the set,
450 * decrefs can cause the set to mutate. To avoid fatal confusion
451 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000452 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000453 * clearing.
454 */
455 fill = so->fill;
456 if (table_is_malloced)
457 EMPTY_TO_MINSIZE(so);
458
459 else if (fill > 0) {
460 /* It's a small table with something that needs to be cleared.
461 * Afraid the only safe way is to copy the set entries into
462 * another small table first.
463 */
464 memcpy(small_copy, table, sizeof(small_copy));
465 table = small_copy;
466 EMPTY_TO_MINSIZE(so);
467 }
468 /* else it's a small table that's already empty */
469
470 /* Now we can finally clear things. If C had refcounts, we could
471 * assert that the refcount on table is 1 now, i.e. that this function
472 * has unique access to it, so decref side-effects can't alter it.
473 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000474 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000475#ifdef Py_DEBUG
476 assert(i < n);
477 ++i;
478#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000479 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000481 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000482 }
483#ifdef Py_DEBUG
484 else
Raymond Hettinger334b5b22006-03-26 03:11:29 +0000485 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000486#endif
487 }
488
489 if (table_is_malloced)
490 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000491 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000492}
493
494/*
495 * Iterate over a set table. Use like so:
496 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000497 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000498 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000499 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000500 * while (set_next(yourset, &pos, &entry)) {
501 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502 * }
503 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000504 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000505 * mutates the table.
506 */
507static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000508set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000509{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000510 Py_ssize_t i;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000511 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000512 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513
514 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000515 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000516 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000517 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000518 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000519 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000520 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000521 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522 if (i > mask)
523 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000524 assert(table[i].key != NULL);
525 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000526 return 1;
527}
528
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000529static void
530set_dealloc(PySetObject *so)
531{
532 register setentry *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000533 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000534 PyObject_GC_UnTrack(so);
535 Py_TRASHCAN_SAFE_BEGIN(so)
536 if (so->weakreflist != NULL)
537 PyObject_ClearWeakRefs((PyObject *) so);
538
539 for (entry = so->table; fill > 0; entry++) {
540 if (entry->key) {
541 --fill;
542 Py_DECREF(entry->key);
543 }
544 }
545 if (so->table != so->smalltable)
546 PyMem_DEL(so->table);
547 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
548 free_sets[num_free_sets++] = so;
549 else
550 so->ob_type->tp_free(so);
551 Py_TRASHCAN_SAFE_END(so)
552}
553
554static int
555set_tp_print(PySetObject *so, FILE *fp, int flags)
556{
557 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000558 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000559 char *emit = ""; /* No separator emitted on first pass */
560 char *separator = ", ";
561
562 fprintf(fp, "%s([", so->ob_type->tp_name);
563 while (set_next(so, &pos, &entry)) {
564 fputs(emit, fp);
565 emit = separator;
566 if (PyObject_Print(entry->key, fp, 0) != 0)
567 return -1;
568 }
569 fputs("])", fp);
570 return 0;
571}
572
573static PyObject *
574set_repr(PySetObject *so)
575{
576 PyObject *keys, *result, *listrepr;
577
578 keys = PySequence_List((PyObject *)so);
579 if (keys == NULL)
580 return NULL;
581 listrepr = PyObject_Repr(keys);
582 Py_DECREF(keys);
583 if (listrepr == NULL)
584 return NULL;
585
586 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
587 PyString_AS_STRING(listrepr));
588 Py_DECREF(listrepr);
589 return result;
590}
591
Martin v. Löwis18e16552006-02-15 17:27:45 +0000592static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000593set_len(PyObject *so)
594{
595 return ((PySetObject *)so)->used;
596}
597
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000598static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000599set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000600{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000601 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000602 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000603 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000604
605 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000606 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000607
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000608 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000609 if (other == so || other->used == 0)
610 /* a.update(a) or a.update({}); nothing to do */
611 return 0;
612 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000613 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000614 * that there will be no (or few) overlapping keys.
615 */
616 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
617 if (set_table_resize(so, (so->used + other->used)*2) != 0)
618 return -1;
619 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000620 for (i = 0; i <= other->mask; i++) {
621 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000622 if (entry->key != NULL &&
623 entry->key != dummy) {
624 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000625 if (set_insert_key(so, entry->key, entry->hash) == -1) {
626 Py_DECREF(entry->key);
627 return -1;
628 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000629 }
630 }
631 return 0;
632}
633
634static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000635set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000636{
637 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000638 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000639
640 if (!PyString_CheckExact(key) ||
641 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
642 hash = PyObject_Hash(key);
643 if (hash == -1)
644 return -1;
645 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000646 entry = (so->lookup)(so, key, hash);
647 if (entry == NULL)
648 return -1;
649 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000650 return key != NULL && key != dummy;
651}
652
Raymond Hettingerc991db22005-08-11 07:58:45 +0000653static int
654set_contains_entry(PySetObject *so, setentry *entry)
655{
656 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000657 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000658
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000659 lu_entry = (so->lookup)(so, entry->key, entry->hash);
660 if (lu_entry == NULL)
661 return -1;
662 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000663 return key != NULL && key != dummy;
664}
665
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000666static PyObject *
667set_pop(PySetObject *so)
668{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000669 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000670 register setentry *entry;
671 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000672
673 assert (PyAnySet_Check(so));
674 if (so->used == 0) {
675 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
676 return NULL;
677 }
678
679 /* Set entry to "the first" unused or dummy set entry. We abuse
680 * the hash field of slot 0 to hold a search finger:
681 * If slot 0 has a value, use slot 0.
682 * Else slot 0 is being used to hold a search finger,
683 * and we use its hash value as the first index to look.
684 */
685 entry = &so->table[0];
686 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000687 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000688 /* The hash field may be a real hash value, or it may be a
689 * legit search finger, or it may be a once-legit search
690 * finger that's out of bounds now because it wrapped around
691 * or the table shrunk -- simply make sure it's in bounds now.
692 */
693 if (i > so->mask || i < 1)
694 i = 1; /* skip slot 0 */
695 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
696 i++;
697 if (i > so->mask)
698 i = 1;
699 }
700 }
701 key = entry->key;
702 Py_INCREF(dummy);
703 entry->key = dummy;
704 so->used--;
705 so->table[0].hash = i + 1; /* next place to start */
706 return key;
707}
708
709PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
710
711static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000712set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000713{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000714 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000715 setentry *entry;
716
717 while (set_next(so, &pos, &entry))
718 Py_VISIT(entry->key);
719 return 0;
720}
721
722static long
723frozenset_hash(PyObject *self)
724{
725 PySetObject *so = (PySetObject *)self;
726 long h, hash = 1927868237L;
727 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000728 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000729
730 if (so->hash != -1)
731 return so->hash;
732
733 hash *= PySet_GET_SIZE(self) + 1;
734 while (set_next(so, &pos, &entry)) {
735 /* Work to increase the bit dispersion for closely spaced hash
736 values. The is important because some use cases have many
737 combinations of a small number of elements with nearby
738 hashes so that many distinct combinations collapse to only
739 a handful of distinct hash values. */
740 h = entry->hash;
741 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
742 }
743 hash = hash * 69069L + 907133923L;
744 if (hash == -1)
745 hash = 590923713L;
746 so->hash = hash;
747 return hash;
748}
749
750static long
751set_nohash(PyObject *self)
752{
753 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
754 return -1;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000755}
756
Raymond Hettingera9d99362005-08-05 00:01:15 +0000757/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000758
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000759typedef struct {
760 PyObject_HEAD
761 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000762 Py_ssize_t si_used;
763 Py_ssize_t si_pos;
764 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000765} setiterobject;
766
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000767static void
768setiter_dealloc(setiterobject *si)
769{
770 Py_XDECREF(si->si_set);
771 PyObject_Del(si);
772}
773
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000774static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000775setiter_len(setiterobject *si)
776{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000777 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000778 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000779 len = si->len;
780 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000781}
782
Armin Rigof5b3e362006-02-11 21:32:43 +0000783PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000784
785static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000786 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000787 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788};
789
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000790static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791{
792 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000793 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000794 register setentry *entry;
795 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000797 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000798 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000799 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000801 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802 PyErr_SetString(PyExc_RuntimeError,
803 "Set changed size during iteration");
804 si->si_used = -1; /* Make this state sticky */
805 return NULL;
806 }
807
808 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000809 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000810 entry = so->table;
811 mask = so->mask;
812 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813 i++;
814 si->si_pos = i+1;
815 if (i > mask)
816 goto fail;
817 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000818 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000819 Py_INCREF(key);
820 return key;
821
822fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000823 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824 si->si_set = NULL;
825 return NULL;
826}
827
Hye-Shik Change2956762005-08-01 05:26:41 +0000828static PyTypeObject PySetIter_Type = {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829 PyObject_HEAD_INIT(&PyType_Type)
830 0, /* ob_size */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000831 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832 sizeof(setiterobject), /* tp_basicsize */
833 0, /* tp_itemsize */
834 /* methods */
835 (destructor)setiter_dealloc, /* tp_dealloc */
836 0, /* tp_print */
837 0, /* tp_getattr */
838 0, /* tp_setattr */
839 0, /* tp_compare */
840 0, /* tp_repr */
841 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000842 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000843 0, /* tp_as_mapping */
844 0, /* tp_hash */
845 0, /* tp_call */
846 0, /* tp_str */
847 PyObject_GenericGetAttr, /* tp_getattro */
848 0, /* tp_setattro */
849 0, /* tp_as_buffer */
850 Py_TPFLAGS_DEFAULT, /* tp_flags */
851 0, /* tp_doc */
852 0, /* tp_traverse */
853 0, /* tp_clear */
854 0, /* tp_richcompare */
855 0, /* tp_weaklistoffset */
856 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000857 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000858 setiter_methods, /* tp_methods */
859 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000860};
861
Martin v. Löwis72d20672006-04-11 09:04:12 +0000862static PyObject *
863set_iter(PySetObject *so)
864{
865 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
866 if (si == NULL)
867 return NULL;
868 Py_INCREF(so);
869 si->si_set = so;
870 si->si_used = so->used;
871 si->si_pos = 0;
872 si->len = so->used;
873 return (PyObject *)si;
874}
875
Raymond Hettingerd7946662005-08-01 21:39:29 +0000876static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000877set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000878{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000879 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000880
Raymond Hettingerd7946662005-08-01 21:39:29 +0000881 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000882 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000883
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000884 if (PyDict_Check(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000885 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000886 Py_ssize_t pos = 0;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000887 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000888 if (set_add_key(so, key) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000889 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000890 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000891 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000892 }
893
Raymond Hettingera38123e2003-11-24 22:18:49 +0000894 it = PyObject_GetIter(other);
895 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000896 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000897
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000898 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000899 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000900 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000901 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000902 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000903 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000904 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000905 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000906 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000907 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000908 return -1;
909 return 0;
910}
911
912static PyObject *
913set_update(PySetObject *so, PyObject *other)
914{
915 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000916 return NULL;
917 Py_RETURN_NONE;
918}
919
920PyDoc_STRVAR(update_doc,
921"Update a set with the union of itself and another.");
922
923static PyObject *
924make_new_set(PyTypeObject *type, PyObject *iterable)
925{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000926 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000927
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000928 if (dummy == NULL) { /* Auto-initialize dummy */
929 dummy = PyString_FromString("<dummy key>");
930 if (dummy == NULL)
931 return NULL;
932 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000933
934 /* create PySetObject structure */
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000935 if (num_free_sets &&
936 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
937 so = free_sets[--num_free_sets];
938 assert (so != NULL && PyAnySet_CheckExact(so));
939 so->ob_type = type;
940 _Py_NewReference((PyObject *)so);
941 EMPTY_TO_MINSIZE(so);
942 PyObject_GC_Track(so);
943 } else {
944 so = (PySetObject *)type->tp_alloc(type, 0);
945 if (so == NULL)
946 return NULL;
947 /* tp_alloc has already zeroed the structure */
948 assert(so->table == NULL && so->fill == 0 && so->used == 0);
949 INIT_NONZERO_SET_SLOTS(so);
950 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000951
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000952 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +0000953 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000954
Raymond Hettingera38123e2003-11-24 22:18:49 +0000955 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +0000956 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000957 Py_DECREF(so);
958 return NULL;
959 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000960 }
961
Raymond Hettingera690a992003-11-16 16:17:49 +0000962 return (PyObject *)so;
963}
964
Raymond Hettingerd7946662005-08-01 21:39:29 +0000965/* The empty frozenset is a singleton */
966static PyObject *emptyfrozenset = NULL;
967
Raymond Hettingera690a992003-11-16 16:17:49 +0000968static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000969frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000970{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000971 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000972
Georg Brandl02c42872005-08-26 06:42:30 +0000973 if (!_PyArg_NoKeywords("frozenset()", kwds))
974 return NULL;
975
Raymond Hettingera690a992003-11-16 16:17:49 +0000976 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
977 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000978
979 if (type != &PyFrozenSet_Type)
980 return make_new_set(type, iterable);
981
982 if (iterable != NULL) {
983 /* frozenset(f) is idempotent */
984 if (PyFrozenSet_CheckExact(iterable)) {
985 Py_INCREF(iterable);
986 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000987 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000988 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +0000989 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +0000990 return result;
991 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000992 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000993 /* The empty frozenset is a singleton */
994 if (emptyfrozenset == NULL)
995 emptyfrozenset = make_new_set(type, NULL);
996 Py_XINCREF(emptyfrozenset);
997 return emptyfrozenset;
998}
999
1000void
1001PySet_Fini(void)
1002{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001003 PySetObject *so;
1004
1005 while (num_free_sets) {
1006 num_free_sets--;
1007 so = free_sets[num_free_sets];
1008 PyObject_GC_Del(so);
1009 }
Martin v. Löwised8f7832006-04-15 12:47:23 +00001010 Py_CLEAR(dummy);
1011 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001012}
1013
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001014static PyObject *
1015set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1016{
Georg Brandl02c42872005-08-26 06:42:30 +00001017 if (!_PyArg_NoKeywords("set()", kwds))
1018 return NULL;
1019
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001020 return make_new_set(type, NULL);
1021}
1022
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001023/* set_swap_bodies() switches the contents of any two sets by moving their
1024 internal data pointers and, if needed, copying the internal smalltables.
1025 Semantically equivalent to:
1026
1027 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1028
1029 The function always succeeds and it leaves both objects in a stable state.
1030 Useful for creating temporary frozensets from sets for membership testing
1031 in __contains__(), discard(), and remove(). Also useful for operations
1032 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001033 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001034*/
1035
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001036static void
1037set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001038{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001039 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001040 setentry *u;
1041 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1042 setentry tab[PySet_MINSIZE];
1043 long h;
1044
1045 t = a->fill; a->fill = b->fill; b->fill = t;
1046 t = a->used; a->used = b->used; b->used = t;
1047 t = a->mask; a->mask = b->mask; b->mask = t;
1048
1049 u = a->table;
1050 if (a->table == a->smalltable)
1051 u = b->smalltable;
1052 a->table = b->table;
1053 if (b->table == b->smalltable)
1054 a->table = a->smalltable;
1055 b->table = u;
1056
1057 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1058
1059 if (a->table == a->smalltable || b->table == b->smalltable) {
1060 memcpy(tab, a->smalltable, sizeof(tab));
1061 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1062 memcpy(b->smalltable, tab, sizeof(tab));
1063 }
1064
Raymond Hettingera580c472005-08-05 17:19:54 +00001065 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
1066 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
1067 h = a->hash; a->hash = b->hash; b->hash = h;
1068 } else {
1069 a->hash = -1;
1070 b->hash = -1;
1071 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001072}
1073
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001074static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001075set_copy(PySetObject *so)
1076{
Raymond Hettingera38123e2003-11-24 22:18:49 +00001077 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001078}
1079
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001080static PyObject *
1081frozenset_copy(PySetObject *so)
1082{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001083 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001084 Py_INCREF(so);
1085 return (PyObject *)so;
1086 }
1087 return set_copy(so);
1088}
1089
Raymond Hettingera690a992003-11-16 16:17:49 +00001090PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1091
1092static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001093set_clear(PySetObject *so)
1094{
1095 set_clear_internal(so);
1096 Py_RETURN_NONE;
1097}
1098
1099PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1100
1101static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001102set_union(PySetObject *so, PyObject *other)
1103{
1104 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001105
1106 result = (PySetObject *)set_copy(so);
1107 if (result == NULL)
1108 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001109 if ((PyObject *)so == other)
1110 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001111 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001112 Py_DECREF(result);
1113 return NULL;
1114 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001115 return (PyObject *)result;
1116}
1117
1118PyDoc_STRVAR(union_doc,
1119 "Return the union of two sets as a new set.\n\
1120\n\
1121(i.e. all elements that are in either set.)");
1122
1123static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001124set_or(PySetObject *so, PyObject *other)
1125{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001126 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001127 Py_INCREF(Py_NotImplemented);
1128 return Py_NotImplemented;
1129 }
1130 return set_union(so, other);
1131}
1132
1133static PyObject *
1134set_ior(PySetObject *so, PyObject *other)
1135{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001136 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001137 Py_INCREF(Py_NotImplemented);
1138 return Py_NotImplemented;
1139 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001140 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001141 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001142 Py_INCREF(so);
1143 return (PyObject *)so;
1144}
1145
1146static PyObject *
1147set_intersection(PySetObject *so, PyObject *other)
1148{
1149 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001150 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001151
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001152 if ((PyObject *)so == other)
1153 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001154
Raymond Hettingera690a992003-11-16 16:17:49 +00001155 result = (PySetObject *)make_new_set(so->ob_type, NULL);
1156 if (result == NULL)
1157 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001158
Raymond Hettingerc991db22005-08-11 07:58:45 +00001159 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001160 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001161 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001162
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001163 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001164 tmp = (PyObject *)so;
1165 so = (PySetObject *)other;
1166 other = tmp;
1167 }
1168
Raymond Hettingerc991db22005-08-11 07:58:45 +00001169 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001170 int rv = set_contains_entry(so, entry);
1171 if (rv == -1) {
1172 Py_DECREF(result);
1173 return NULL;
1174 }
1175 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001176 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001177 Py_DECREF(result);
1178 return NULL;
1179 }
1180 }
1181 }
1182 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001183 }
1184
Raymond Hettingera690a992003-11-16 16:17:49 +00001185 it = PyObject_GetIter(other);
1186 if (it == NULL) {
1187 Py_DECREF(result);
1188 return NULL;
1189 }
1190
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001191 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001192 int rv;
1193 setentry entry;
1194 long hash = PyObject_Hash(key);
1195
1196 if (hash == -1) {
1197 Py_DECREF(it);
1198 Py_DECREF(result);
1199 Py_DECREF(key);
1200 return NULL;
1201 }
1202 entry.hash = hash;
1203 entry.key = key;
1204 rv = set_contains_entry(so, &entry);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001205 if (rv == -1) {
1206 Py_DECREF(it);
1207 Py_DECREF(result);
1208 Py_DECREF(key);
1209 return NULL;
1210 }
1211 if (rv) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001212 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001213 Py_DECREF(it);
1214 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001215 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001216 return NULL;
1217 }
1218 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001219 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001220 }
1221 Py_DECREF(it);
1222 if (PyErr_Occurred()) {
1223 Py_DECREF(result);
1224 return NULL;
1225 }
1226 return (PyObject *)result;
1227}
1228
1229PyDoc_STRVAR(intersection_doc,
1230"Return the intersection of two sets as a new set.\n\
1231\n\
1232(i.e. all elements that are in both sets.)");
1233
1234static PyObject *
1235set_intersection_update(PySetObject *so, PyObject *other)
1236{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001237 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001238
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001239 tmp = set_intersection(so, other);
1240 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001241 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001242 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001243 Py_DECREF(tmp);
1244 Py_RETURN_NONE;
1245}
1246
1247PyDoc_STRVAR(intersection_update_doc,
1248"Update a set with the intersection of itself and another.");
1249
1250static PyObject *
1251set_and(PySetObject *so, PyObject *other)
1252{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001253 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001254 Py_INCREF(Py_NotImplemented);
1255 return Py_NotImplemented;
1256 }
1257 return set_intersection(so, other);
1258}
1259
1260static PyObject *
1261set_iand(PySetObject *so, PyObject *other)
1262{
1263 PyObject *result;
1264
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001265 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001266 Py_INCREF(Py_NotImplemented);
1267 return Py_NotImplemented;
1268 }
1269 result = set_intersection_update(so, other);
1270 if (result == NULL)
1271 return NULL;
1272 Py_DECREF(result);
1273 Py_INCREF(so);
1274 return (PyObject *)so;
1275}
1276
Neal Norwitz6576bd82005-11-13 18:41:28 +00001277static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001278set_difference_update_internal(PySetObject *so, PyObject *other)
1279{
1280 if ((PyObject *)so == other)
1281 return set_clear_internal(so);
1282
1283 if (PyAnySet_Check(other)) {
1284 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001285 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001286
1287 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001288 if (set_discard_entry(so, entry) == -1)
1289 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001290 } else {
1291 PyObject *key, *it;
1292 it = PyObject_GetIter(other);
1293 if (it == NULL)
1294 return -1;
1295
1296 while ((key = PyIter_Next(it)) != NULL) {
1297 if (set_discard_key(so, key) == -1) {
1298 Py_DECREF(it);
1299 Py_DECREF(key);
1300 return -1;
1301 }
1302 Py_DECREF(key);
1303 }
1304 Py_DECREF(it);
1305 if (PyErr_Occurred())
1306 return -1;
1307 }
1308 /* If more than 1/5 are dummies, then resize them away. */
1309 if ((so->fill - so->used) * 5 < so->mask)
1310 return 0;
1311 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1312}
1313
Raymond Hettingera690a992003-11-16 16:17:49 +00001314static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001315set_difference_update(PySetObject *so, PyObject *other)
1316{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001317 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001318 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001319 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001320}
1321
1322PyDoc_STRVAR(difference_update_doc,
1323"Remove all elements of another set from this set.");
1324
1325static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001326set_difference(PySetObject *so, PyObject *other)
1327{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001328 PyObject *result;
1329 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001330 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001331
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001332 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001333 result = set_copy(so);
1334 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001335 return NULL;
1336 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001337 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001338 Py_DECREF(result);
1339 return NULL;
1340 }
1341
1342 result = make_new_set(so->ob_type, NULL);
1343 if (result == NULL)
1344 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001345
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001346 if (PyDict_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001347 while (set_next(so, &pos, &entry)) {
1348 setentry entrycopy;
1349 entrycopy.hash = entry->hash;
1350 entrycopy.key = entry->key;
1351 if (!PyDict_Contains(other, entry->key)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001352 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1353 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001354 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001355 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001356 }
1357 }
1358 return result;
1359 }
1360
Raymond Hettingerc991db22005-08-11 07:58:45 +00001361 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001362 int rv = set_contains_entry((PySetObject *)other, entry);
1363 if (rv == -1) {
1364 Py_DECREF(result);
1365 return NULL;
1366 }
1367 if (!rv) {
1368 if (set_add_entry((PySetObject *)result, entry) == -1) {
1369 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001370 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001371 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001372 }
1373 }
1374 return result;
1375}
1376
1377PyDoc_STRVAR(difference_doc,
1378"Return the difference of two sets as a new set.\n\
1379\n\
1380(i.e. all elements that are in this set but not the other.)");
1381static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001382set_sub(PySetObject *so, PyObject *other)
1383{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001384 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001385 Py_INCREF(Py_NotImplemented);
1386 return Py_NotImplemented;
1387 }
1388 return set_difference(so, other);
1389}
1390
1391static PyObject *
1392set_isub(PySetObject *so, PyObject *other)
1393{
1394 PyObject *result;
1395
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001396 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001397 Py_INCREF(Py_NotImplemented);
1398 return Py_NotImplemented;
1399 }
1400 result = set_difference_update(so, other);
1401 if (result == NULL)
1402 return NULL;
1403 Py_DECREF(result);
1404 Py_INCREF(so);
1405 return (PyObject *)so;
1406}
1407
1408static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001409set_symmetric_difference_update(PySetObject *so, PyObject *other)
1410{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001411 PySetObject *otherset;
1412 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001413 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001414 setentry *entry;
1415
1416 if ((PyObject *)so == other)
1417 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001418
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001419 if (PyDict_Check(other)) {
1420 PyObject *value;
1421 int rv;
1422 while (PyDict_Next(other, &pos, &key, &value)) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001423 setentry an_entry;
1424 long hash = PyObject_Hash(key);
1425
1426 if (hash == -1)
1427 return NULL;
1428 an_entry.hash = hash;
1429 an_entry.key = key;
1430 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001431 if (rv == -1)
1432 return NULL;
1433 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001434 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001435 return NULL;
1436 }
1437 }
1438 Py_RETURN_NONE;
1439 }
1440
1441 if (PyAnySet_Check(other)) {
1442 Py_INCREF(other);
1443 otherset = (PySetObject *)other;
1444 } else {
Raymond Hettingera690a992003-11-16 16:17:49 +00001445 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1446 if (otherset == NULL)
1447 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001448 }
1449
Raymond Hettingerc991db22005-08-11 07:58:45 +00001450 while (set_next(otherset, &pos, &entry)) {
1451 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001452 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001453 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001454 return NULL;
1455 }
1456 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001457 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001458 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001459 return NULL;
1460 }
1461 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001462 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001463 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001464 Py_RETURN_NONE;
1465}
1466
1467PyDoc_STRVAR(symmetric_difference_update_doc,
1468"Update a set with the symmetric difference of itself and another.");
1469
1470static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001471set_symmetric_difference(PySetObject *so, PyObject *other)
1472{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001473 PyObject *rv;
1474 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001475
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001476 otherset = (PySetObject *)make_new_set(so->ob_type, other);
1477 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001478 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001479 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1480 if (rv == NULL)
1481 return NULL;
1482 Py_DECREF(rv);
1483 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001484}
1485
1486PyDoc_STRVAR(symmetric_difference_doc,
1487"Return the symmetric difference of two sets as a new set.\n\
1488\n\
1489(i.e. all elements that are in exactly one of the sets.)");
1490
1491static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001492set_xor(PySetObject *so, PyObject *other)
1493{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001494 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001495 Py_INCREF(Py_NotImplemented);
1496 return Py_NotImplemented;
1497 }
1498 return set_symmetric_difference(so, other);
1499}
1500
1501static PyObject *
1502set_ixor(PySetObject *so, PyObject *other)
1503{
1504 PyObject *result;
1505
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001506 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001507 Py_INCREF(Py_NotImplemented);
1508 return Py_NotImplemented;
1509 }
1510 result = set_symmetric_difference_update(so, other);
1511 if (result == NULL)
1512 return NULL;
1513 Py_DECREF(result);
1514 Py_INCREF(so);
1515 return (PyObject *)so;
1516}
1517
1518static PyObject *
1519set_issubset(PySetObject *so, PyObject *other)
1520{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001521 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001522 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001523
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001524 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001525 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001526 tmp = make_new_set(&PySet_Type, other);
1527 if (tmp == NULL)
1528 return NULL;
1529 result = set_issubset(so, tmp);
1530 Py_DECREF(tmp);
1531 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001532 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001533 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001534 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001535
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001536 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001537 int rv = set_contains_entry((PySetObject *)other, entry);
1538 if (rv == -1)
1539 return NULL;
1540 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001541 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001542 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001543 Py_RETURN_TRUE;
1544}
1545
1546PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1547
1548static PyObject *
1549set_issuperset(PySetObject *so, PyObject *other)
1550{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001551 PyObject *tmp, *result;
1552
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001553 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001554 tmp = make_new_set(&PySet_Type, other);
1555 if (tmp == NULL)
1556 return NULL;
1557 result = set_issuperset(so, tmp);
1558 Py_DECREF(tmp);
1559 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001560 }
1561 return set_issubset((PySetObject *)other, (PyObject *)so);
1562}
1563
1564PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1565
Raymond Hettingera690a992003-11-16 16:17:49 +00001566static PyObject *
1567set_richcompare(PySetObject *v, PyObject *w, int op)
1568{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001569 PyObject *r1, *r2;
1570
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001571 if(!PyAnySet_Check(w)) {
1572 if (op == Py_EQ)
1573 Py_RETURN_FALSE;
1574 if (op == Py_NE)
1575 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001576 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1577 return NULL;
1578 }
1579 switch (op) {
1580 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001581 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001582 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001583 if (v->hash != -1 &&
1584 ((PySetObject *)w)->hash != -1 &&
1585 v->hash != ((PySetObject *)w)->hash)
1586 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001587 return set_issubset(v, w);
1588 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001589 r1 = set_richcompare(v, w, Py_EQ);
1590 if (r1 == NULL)
1591 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001592 r2 = PyBool_FromLong(PyObject_Not(r1));
1593 Py_DECREF(r1);
1594 return r2;
1595 case Py_LE:
1596 return set_issubset(v, w);
1597 case Py_GE:
1598 return set_issuperset(v, w);
1599 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001600 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001601 Py_RETURN_FALSE;
1602 return set_issubset(v, w);
1603 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001604 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001605 Py_RETURN_FALSE;
1606 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001607 }
1608 Py_INCREF(Py_NotImplemented);
1609 return Py_NotImplemented;
1610}
1611
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001612static int
Georg Brandl347b3002006-03-30 11:57:00 +00001613set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001614{
1615 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1616 return -1;
1617}
1618
Raymond Hettingera690a992003-11-16 16:17:49 +00001619static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001620set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001621{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001622 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001623 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001624 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001625}
1626
1627PyDoc_STRVAR(add_doc,
1628"Add an element to a set.\n\
1629\n\
1630This has no effect if the element is already present.");
1631
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001632static int
1633set_contains(PySetObject *so, PyObject *key)
1634{
1635 PyObject *tmpkey;
1636 int rv;
1637
1638 rv = set_contains_key(so, key);
1639 if (rv == -1) {
1640 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1641 return -1;
1642 PyErr_Clear();
1643 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1644 if (tmpkey == NULL)
1645 return -1;
1646 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1647 rv = set_contains(so, tmpkey);
1648 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1649 Py_DECREF(tmpkey);
1650 }
1651 return rv;
1652}
1653
1654static PyObject *
1655set_direct_contains(PySetObject *so, PyObject *key)
1656{
1657 long result;
1658
1659 result = set_contains(so, key);
1660 if (result == -1)
1661 return NULL;
1662 return PyBool_FromLong(result);
1663}
1664
1665PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1666
Raymond Hettingera690a992003-11-16 16:17:49 +00001667static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001668set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001669{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001670 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001671 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001672
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001673 rv = set_discard_key(so, key);
1674 if (rv == -1) {
1675 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1676 return NULL;
1677 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001678 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1679 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001680 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001681 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001682 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001683 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001684 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001685 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001686 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001687 PyErr_SetObject(PyExc_KeyError, key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001688 return NULL;
1689 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001690 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001691}
1692
1693PyDoc_STRVAR(remove_doc,
1694"Remove an element from a set; it must be a member.\n\
1695\n\
1696If the element is not a member, raise a KeyError.");
1697
1698static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001699set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001700{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001701 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001702 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001703
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001704 rv = set_discard_key(so, key);
1705 if (rv == -1) {
1706 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1707 return NULL;
1708 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001709 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1710 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001711 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001712 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001713 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001714 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001715 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001716 return result;
1717 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001718 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001719}
1720
1721PyDoc_STRVAR(discard_doc,
1722"Remove an element from a set if it is a member.\n\
1723\n\
1724If the element is not a member, do nothing.");
1725
1726static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001727set_reduce(PySetObject *so)
1728{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001729 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001730
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001731 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001732 if (keys == NULL)
1733 goto done;
1734 args = PyTuple_Pack(1, keys);
1735 if (args == NULL)
1736 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001737 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1738 if (dict == NULL) {
1739 PyErr_Clear();
1740 dict = Py_None;
1741 Py_INCREF(dict);
1742 }
1743 result = PyTuple_Pack(3, so->ob_type, args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001744done:
1745 Py_XDECREF(args);
1746 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001747 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001748 return result;
1749}
1750
1751PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1752
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001753static int
1754set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1755{
1756 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001757
1758 if (!PyAnySet_Check(self))
1759 return -1;
1760 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
1761 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001762 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001763 self->hash = -1;
1764 if (iterable == NULL)
1765 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001766 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001767}
1768
Raymond Hettingera690a992003-11-16 16:17:49 +00001769static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001770 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001771 0, /* sq_concat */
1772 0, /* sq_repeat */
1773 0, /* sq_item */
1774 0, /* sq_slice */
1775 0, /* sq_ass_item */
1776 0, /* sq_ass_slice */
1777 (objobjproc)set_contains, /* sq_contains */
1778};
1779
1780/* set object ********************************************************/
1781
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001782#ifdef Py_DEBUG
1783static PyObject *test_c_api(PySetObject *so);
1784
1785PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1786All is well if assertions don't fail.");
1787#endif
1788
Raymond Hettingera690a992003-11-16 16:17:49 +00001789static PyMethodDef set_methods[] = {
1790 {"add", (PyCFunction)set_add, METH_O,
1791 add_doc},
1792 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1793 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001794 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001795 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001796 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1797 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001798 {"discard", (PyCFunction)set_discard, METH_O,
1799 discard_doc},
1800 {"difference", (PyCFunction)set_difference, METH_O,
1801 difference_doc},
1802 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1803 difference_update_doc},
1804 {"intersection",(PyCFunction)set_intersection, METH_O,
1805 intersection_doc},
1806 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1807 intersection_update_doc},
1808 {"issubset", (PyCFunction)set_issubset, METH_O,
1809 issubset_doc},
1810 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1811 issuperset_doc},
1812 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1813 pop_doc},
1814 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1815 reduce_doc},
1816 {"remove", (PyCFunction)set_remove, METH_O,
1817 remove_doc},
1818 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1819 symmetric_difference_doc},
1820 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1821 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001822#ifdef Py_DEBUG
1823 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1824 test_c_api_doc},
1825#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001826 {"union", (PyCFunction)set_union, METH_O,
1827 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001828 {"update", (PyCFunction)set_update, METH_O,
1829 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001830 {NULL, NULL} /* sentinel */
1831};
1832
1833static PyNumberMethods set_as_number = {
1834 0, /*nb_add*/
1835 (binaryfunc)set_sub, /*nb_subtract*/
1836 0, /*nb_multiply*/
1837 0, /*nb_divide*/
1838 0, /*nb_remainder*/
1839 0, /*nb_divmod*/
1840 0, /*nb_power*/
1841 0, /*nb_negative*/
1842 0, /*nb_positive*/
1843 0, /*nb_absolute*/
1844 0, /*nb_nonzero*/
1845 0, /*nb_invert*/
1846 0, /*nb_lshift*/
1847 0, /*nb_rshift*/
1848 (binaryfunc)set_and, /*nb_and*/
1849 (binaryfunc)set_xor, /*nb_xor*/
1850 (binaryfunc)set_or, /*nb_or*/
1851 0, /*nb_coerce*/
1852 0, /*nb_int*/
1853 0, /*nb_long*/
1854 0, /*nb_float*/
1855 0, /*nb_oct*/
1856 0, /*nb_hex*/
1857 0, /*nb_inplace_add*/
1858 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1859 0, /*nb_inplace_multiply*/
1860 0, /*nb_inplace_divide*/
1861 0, /*nb_inplace_remainder*/
1862 0, /*nb_inplace_power*/
1863 0, /*nb_inplace_lshift*/
1864 0, /*nb_inplace_rshift*/
1865 (binaryfunc)set_iand, /*nb_inplace_and*/
1866 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1867 (binaryfunc)set_ior, /*nb_inplace_or*/
1868};
1869
1870PyDoc_STRVAR(set_doc,
1871"set(iterable) --> set object\n\
1872\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001873Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001874
1875PyTypeObject PySet_Type = {
1876 PyObject_HEAD_INIT(&PyType_Type)
1877 0, /* ob_size */
1878 "set", /* tp_name */
1879 sizeof(PySetObject), /* tp_basicsize */
1880 0, /* tp_itemsize */
1881 /* methods */
1882 (destructor)set_dealloc, /* tp_dealloc */
1883 (printfunc)set_tp_print, /* tp_print */
1884 0, /* tp_getattr */
1885 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001886 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001887 (reprfunc)set_repr, /* tp_repr */
1888 &set_as_number, /* tp_as_number */
1889 &set_as_sequence, /* tp_as_sequence */
1890 0, /* tp_as_mapping */
1891 set_nohash, /* tp_hash */
1892 0, /* tp_call */
1893 0, /* tp_str */
1894 PyObject_GenericGetAttr, /* tp_getattro */
1895 0, /* tp_setattro */
1896 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001897 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001898 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001899 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001900 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001901 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001902 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001903 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001904 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00001905 0, /* tp_iternext */
1906 set_methods, /* tp_methods */
1907 0, /* tp_members */
1908 0, /* tp_getset */
1909 0, /* tp_base */
1910 0, /* tp_dict */
1911 0, /* tp_descr_get */
1912 0, /* tp_descr_set */
1913 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001914 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001915 PyType_GenericAlloc, /* tp_alloc */
1916 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001917 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00001918};
1919
1920/* frozenset object ********************************************************/
1921
1922
1923static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001924 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001925 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001926 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001927 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001928 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001929 difference_doc},
1930 {"intersection",(PyCFunction)set_intersection, METH_O,
1931 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001932 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001933 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001934 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001935 issuperset_doc},
1936 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1937 reduce_doc},
1938 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1939 symmetric_difference_doc},
1940 {"union", (PyCFunction)set_union, METH_O,
1941 union_doc},
1942 {NULL, NULL} /* sentinel */
1943};
1944
1945static PyNumberMethods frozenset_as_number = {
1946 0, /*nb_add*/
1947 (binaryfunc)set_sub, /*nb_subtract*/
1948 0, /*nb_multiply*/
1949 0, /*nb_divide*/
1950 0, /*nb_remainder*/
1951 0, /*nb_divmod*/
1952 0, /*nb_power*/
1953 0, /*nb_negative*/
1954 0, /*nb_positive*/
1955 0, /*nb_absolute*/
1956 0, /*nb_nonzero*/
1957 0, /*nb_invert*/
1958 0, /*nb_lshift*/
1959 0, /*nb_rshift*/
1960 (binaryfunc)set_and, /*nb_and*/
1961 (binaryfunc)set_xor, /*nb_xor*/
1962 (binaryfunc)set_or, /*nb_or*/
1963};
1964
1965PyDoc_STRVAR(frozenset_doc,
1966"frozenset(iterable) --> frozenset object\n\
1967\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001968Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001969
1970PyTypeObject PyFrozenSet_Type = {
1971 PyObject_HEAD_INIT(&PyType_Type)
1972 0, /* ob_size */
1973 "frozenset", /* tp_name */
1974 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001975 0, /* tp_itemsize */
1976 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001977 (destructor)set_dealloc, /* tp_dealloc */
1978 (printfunc)set_tp_print, /* tp_print */
1979 0, /* tp_getattr */
1980 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00001981 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00001982 (reprfunc)set_repr, /* tp_repr */
1983 &frozenset_as_number, /* tp_as_number */
1984 &set_as_sequence, /* tp_as_sequence */
1985 0, /* tp_as_mapping */
1986 frozenset_hash, /* tp_hash */
1987 0, /* tp_call */
1988 0, /* tp_str */
1989 PyObject_GenericGetAttr, /* tp_getattro */
1990 0, /* tp_setattro */
1991 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001992 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001993 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001994 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00001995 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00001996 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00001997 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001998 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001999 (getiterfunc)set_iter, /* tp_iter */
2000 0, /* tp_iternext */
2001 frozenset_methods, /* tp_methods */
2002 0, /* tp_members */
2003 0, /* tp_getset */
2004 0, /* tp_base */
2005 0, /* tp_dict */
2006 0, /* tp_descr_get */
2007 0, /* tp_descr_set */
2008 0, /* tp_dictoffset */
2009 0, /* tp_init */
2010 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002011 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002012 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002013};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002014
2015
2016/***** C API functions *************************************************/
2017
2018PyObject *
2019PySet_New(PyObject *iterable)
2020{
2021 return make_new_set(&PySet_Type, iterable);
2022}
2023
2024PyObject *
2025PyFrozenSet_New(PyObject *iterable)
2026{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002027 PyObject *args, *result;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002028
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002029 if (iterable == NULL)
2030 args = PyTuple_New(0);
2031 else
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002032 args = PyTuple_Pack(1, iterable);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002033 if (args == NULL)
2034 return NULL;
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002035 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002036 Py_DECREF(args);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002037 return result;
2038}
2039
Neal Norwitz8c49c822006-03-04 18:41:19 +00002040Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002041PySet_Size(PyObject *anyset)
2042{
2043 if (!PyAnySet_Check(anyset)) {
2044 PyErr_BadInternalCall();
2045 return -1;
2046 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002047 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002048}
2049
2050int
Barry Warsaw176014f2006-03-30 22:45:35 +00002051PySet_Clear(PyObject *set)
2052{
2053 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2054 PyErr_BadInternalCall();
2055 return -1;
2056 }
2057 return set_clear_internal((PySetObject *)set);
2058}
2059
2060int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002061PySet_Contains(PyObject *anyset, PyObject *key)
2062{
2063 if (!PyAnySet_Check(anyset)) {
2064 PyErr_BadInternalCall();
2065 return -1;
2066 }
2067 return set_contains_key((PySetObject *)anyset, key);
2068}
2069
2070int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002071PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002072{
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002073 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002074 PyErr_BadInternalCall();
2075 return -1;
2076 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002077 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002078}
2079
2080int
2081PySet_Add(PyObject *set, PyObject *key)
2082{
2083 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2084 PyErr_BadInternalCall();
2085 return -1;
2086 }
2087 return set_add_key((PySetObject *)set, key);
2088}
2089
Barry Warsaw176014f2006-03-30 22:45:35 +00002090int
2091_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
2092{
2093 setentry *entry_ptr;
2094
2095 if (!PyAnySet_Check(set)) {
2096 PyErr_BadInternalCall();
2097 return -1;
2098 }
2099 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2100 return 0;
2101 *entry = entry_ptr->key;
2102 return 1;
2103}
2104
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002105PyObject *
2106PySet_Pop(PyObject *set)
2107{
2108 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2109 PyErr_BadInternalCall();
2110 return NULL;
2111 }
2112 return set_pop((PySetObject *)set);
2113}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002114
Barry Warsaw176014f2006-03-30 22:45:35 +00002115int
2116_PySet_Update(PyObject *set, PyObject *iterable)
2117{
2118 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
2119 PyErr_BadInternalCall();
2120 return -1;
2121 }
2122 return set_update_internal((PySetObject *)set, iterable);
2123}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002124
2125#ifdef Py_DEBUG
2126
2127/* Test code to be called with any three element set.
2128 Returns True and original set is restored. */
2129
2130#define assertRaises(call_return_value, exception) \
2131 do { \
2132 assert(call_return_value); \
2133 assert(PyErr_ExceptionMatches(exception)); \
2134 PyErr_Clear(); \
2135 } while(0)
2136
2137static PyObject *
2138test_c_api(PySetObject *so)
2139{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002140 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002141 char *s;
2142 Py_ssize_t i;
2143 PyObject *elem, *dup, *t, *f, *dup2;
2144 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002145
2146 /* Verify preconditions and exercise type/size checks */
2147 assert(PyAnySet_Check(ob));
2148 assert(PyAnySet_CheckExact(ob));
2149 assert(!PyFrozenSet_CheckExact(ob));
2150 assert(PySet_Size(ob) == 3);
2151 assert(PySet_GET_SIZE(ob) == 3);
2152
2153 /* Raise TypeError for non-iterable constructor arguments */
2154 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2155 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2156
2157 /* Raise TypeError for unhashable key */
2158 dup = PySet_New(ob);
2159 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2160 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2161 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2162
2163 /* Exercise successful pop, contains, add, and discard */
2164 elem = PySet_Pop(ob);
2165 assert(PySet_Contains(ob, elem) == 0);
2166 assert(PySet_GET_SIZE(ob) == 2);
2167 assert(PySet_Add(ob, elem) == 0);
2168 assert(PySet_Contains(ob, elem) == 1);
2169 assert(PySet_GET_SIZE(ob) == 3);
2170 assert(PySet_Discard(ob, elem) == 1);
2171 assert(PySet_GET_SIZE(ob) == 2);
2172 assert(PySet_Discard(ob, elem) == 0);
2173 assert(PySet_GET_SIZE(ob) == 2);
2174
Barry Warsaw176014f2006-03-30 22:45:35 +00002175 /* Exercise clear */
2176 dup2 = PySet_New(dup);
2177 assert(PySet_Clear(dup2) == 0);
2178 assert(PySet_Size(dup2) == 0);
2179 Py_DECREF(dup2);
2180
2181 /* Raise SystemError on clear or update of frozen set */
2182 f = PyFrozenSet_New(dup);
2183 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2184 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2185 Py_DECREF(f);
2186
2187 /* Exercise direct iteration */
2188 i = 0, count = 0;
2189 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
2190 s = PyString_AsString(elem);
2191 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2192 count++;
2193 }
2194 assert(count == 3);
2195
2196 /* Exercise updates */
2197 dup2 = PySet_New(NULL);
2198 assert(_PySet_Update(dup2, dup) == 0);
2199 assert(PySet_Size(dup2) == 3);
2200 assert(_PySet_Update(dup2, dup) == 0);
2201 assert(PySet_Size(dup2) == 3);
2202 Py_DECREF(dup2);
2203
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002204 /* Raise SystemError when self argument is not a set or frozenset. */
2205 t = PyTuple_New(0);
2206 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2207 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2208 Py_DECREF(t);
2209
2210 /* Raise SystemError when self argument is not a set. */
2211 f = PyFrozenSet_New(dup);
2212 assert(PySet_Size(f) == 3);
2213 assert(PyFrozenSet_CheckExact(f));
2214 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2215 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2216 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2217 Py_DECREF(f);
2218
2219 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002220 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2221 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002222 assert(PySet_GET_SIZE(ob) == 0);
2223 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2224
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002225 /* Restore the set from the copy using the PyNumber API */
2226 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2227 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002228
2229 /* Verify constructors accept NULL arguments */
2230 f = PySet_New(NULL);
2231 assert(f != NULL);
2232 assert(PySet_GET_SIZE(f) == 0);
2233 Py_DECREF(f);
2234 f = PyFrozenSet_New(NULL);
2235 assert(f != NULL);
2236 assert(PyFrozenSet_CheckExact(f));
2237 assert(PySet_GET_SIZE(f) == 0);
2238 Py_DECREF(f);
2239
2240 Py_DECREF(elem);
2241 Py_DECREF(dup);
2242 Py_RETURN_TRUE;
2243}
2244
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002245#undef assertRaises
2246
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002247#endif