blob: 3869b3b6be4eaa21a74cc9e713ec3369379bf1e7 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis68192102007-07-21 06:55:02 +00006 Copyright (c) 2003-2007 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
19 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
25}
26
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000027/* This must be >= 1. */
28#define PERTURB_SHIFT 5
29
30/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000031static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032
Armin Rigoe1709372006-04-12 17:06:05 +000033#ifdef Py_REF_DEBUG
34PyObject *
35_PySet_Dummy(void)
36{
37 return dummy;
38}
39#endif
40
Raymond Hettingerbc841a12005-08-07 13:02:53 +000041#define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
44 (so)->hash = -1; \
45 } while(0)
46
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047#define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000050 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051 } while(0)
52
Raymond Hettingerbc841a12005-08-07 13:02:53 +000053/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes5b970ad2008-02-06 13:33:44 +000054#ifndef PySet_MAXFREELIST
55#define PySet_MAXFREELIST 80
56#endif
57static PySetObject *free_list[PySet_MAXFREELIST];
58static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000059
60/*
61The basic lookup function used by all operations.
62This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
63Open addressing is preferred over chaining since the link overhead for
64chaining would be substantial (100% with typical malloc overhead).
65
66The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000067probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000068
69All arithmetic on hash should ignore overflow.
70
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000071Unlike the dictionary implementation, the lookkey functions can return
72NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073*/
74
75static setentry *
76set_lookkey(PySetObject *so, PyObject *key, register long hash)
77{
Martin v. Löwis18e16552006-02-15 17:27:45 +000078 register Py_ssize_t i;
79 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000080 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +000081 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000082 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000083 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000084 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000085 PyObject *startkey;
86
87 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000088 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000089 if (entry->key == NULL || entry->key == key)
90 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000091
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000092 if (entry->key == dummy)
93 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000094 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000095 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000096 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000097 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
98 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000099 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000100 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000101 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000102 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000103 }
104 else {
105 /* The compare did major nasty stuff to the
106 * set: start over.
107 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000108 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000109 }
110 }
111 freeslot = NULL;
112 }
113
114 /* In the loop, key == dummy is by far (factor of 100s) the
115 least likely outcome, so test for that last. */
116 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
117 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000118 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000119 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000120 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000121 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 break;
123 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000124 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000125 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000126 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000127 startkey = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000128 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
129 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000130 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000131 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000132 if (cmp > 0)
133 break;
134 }
135 else {
136 /* The compare did major nasty stuff to the
137 * set: start over.
138 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000139 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000140 }
141 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000142 else if (entry->key == dummy && freeslot == NULL)
143 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000144 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000145 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000146}
147
148/*
149 * Hacked up version of set_lookkey which can assume keys are always strings;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000150 * This means we can always use _PyString_Eq directly and not have to check to
151 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000152 */
153static setentry *
154set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
155{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156 register Py_ssize_t i;
157 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000158 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000159 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000160 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000161 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000162
163 /* Make sure this function doesn't have to handle non-string keys,
164 including subclasses of str; e.g., one reason to subclass
165 strings is to override __eq__, and for speed we don't cater to
166 that here. */
167 if (!PyString_CheckExact(key)) {
168 so->lookup = set_lookkey;
169 return set_lookkey(so, key, hash);
170 }
171 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000172 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000173 if (entry->key == NULL || entry->key == key)
174 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000175 if (entry->key == dummy)
176 freeslot = entry;
177 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000178 if (entry->hash == hash && _PyString_Eq(entry->key, key))
179 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000180 freeslot = NULL;
181 }
182
183 /* In the loop, key == dummy is by far (factor of 100s) the
184 least likely outcome, so test for that last. */
185 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
186 i = (i << 2) + i + perturb + 1;
187 entry = &table[i & mask];
188 if (entry->key == NULL)
189 return freeslot == NULL ? entry : freeslot;
190 if (entry->key == key
191 || (entry->hash == hash
192 && entry->key != dummy
193 && _PyString_Eq(entry->key, key)))
194 return entry;
195 if (entry->key == dummy && freeslot == NULL)
196 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000197 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000198 assert(0); /* NOT REACHED */
199 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000200}
201
202/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000203Internal routine to insert a new key into the table.
Raymond Hettinger0c850862006-12-08 04:24:33 +0000204Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000205Eats a reference to key.
206*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000207static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208set_insert_key(register PySetObject *so, PyObject *key, long hash)
209{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000210 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
212
213 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000214 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000215 if (entry == NULL)
216 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000217 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000218 /* UNUSED */
219 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000220 entry->key = key;
221 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000223 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000224 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000225 entry->key = key;
226 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000227 so->used++;
228 Py_DECREF(dummy);
229 } else {
230 /* ACTIVE */
231 Py_DECREF(key);
232 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000233 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234}
235
236/*
Raymond Hettinger0c850862006-12-08 04:24:33 +0000237Internal routine used by set_table_resize() to insert an item which is
238known to be absent from the set. This routine also assumes that
239the set contains no deleted entries. Besides the performance benefit,
240using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
241Note that no refcounts are changed by this routine; if needed, the caller
242is responsible for incref'ing `key`.
243*/
244static void
245set_insert_clean(register PySetObject *so, PyObject *key, long hash)
246{
247 register size_t i;
248 register size_t perturb;
249 register size_t mask = (size_t)so->mask;
250 setentry *table = so->table;
251 register setentry *entry;
252
253 i = hash & mask;
254 entry = &table[i];
255 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
256 i = (i << 2) + i + perturb + 1;
257 entry = &table[i & mask];
258 }
259 so->fill++;
260 entry->key = key;
261 entry->hash = hash;
262 so->used++;
263}
264
265/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000267keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000268actually be smaller than the old one.
269*/
270static int
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000271set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000273 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000274 setentry *oldtable, *newtable, *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000275 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000276 int is_oldtable_malloced;
277 setentry small_copy[PySet_MINSIZE];
278
279 assert(minused >= 0);
280
281 /* Find the smallest table size > minused. */
282 for (newsize = PySet_MINSIZE;
283 newsize <= minused && newsize > 0;
284 newsize <<= 1)
285 ;
286 if (newsize <= 0) {
287 PyErr_NoMemory();
288 return -1;
289 }
290
291 /* Get space for a new table. */
292 oldtable = so->table;
293 assert(oldtable != NULL);
294 is_oldtable_malloced = oldtable != so->smalltable;
295
296 if (newsize == PySet_MINSIZE) {
297 /* A large table is shrinking, or we can't get any smaller. */
298 newtable = so->smalltable;
299 if (newtable == oldtable) {
300 if (so->fill == so->used) {
301 /* No dummies, so no point doing anything. */
302 return 0;
303 }
304 /* We're not going to resize it, but rebuild the
305 table anyway to purge old dummy entries.
306 Subtle: This is *necessary* if fill==size,
307 as set_lookkey needs at least one virgin slot to
308 terminate failing searches. If fill < size, it's
309 merely desirable, as dummies slow searches. */
310 assert(so->fill > so->used);
311 memcpy(small_copy, oldtable, sizeof(small_copy));
312 oldtable = small_copy;
313 }
314 }
315 else {
316 newtable = PyMem_NEW(setentry, newsize);
317 if (newtable == NULL) {
318 PyErr_NoMemory();
319 return -1;
320 }
321 }
322
323 /* Make the set empty, using the new table. */
324 assert(newtable != oldtable);
325 so->table = newtable;
326 so->mask = newsize - 1;
327 memset(newtable, 0, sizeof(setentry) * newsize);
328 so->used = 0;
329 i = so->fill;
330 so->fill = 0;
331
332 /* Copy the data over; this is refcount-neutral for active entries;
333 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000334 for (entry = oldtable; i > 0; entry++) {
335 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000336 /* UNUSED */
337 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000338 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000339 /* DUMMY */
340 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000341 assert(entry->key == dummy);
342 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000343 } else {
344 /* ACTIVE */
345 --i;
Raymond Hettinger0c850862006-12-08 04:24:33 +0000346 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347 }
348 }
349
350 if (is_oldtable_malloced)
351 PyMem_DEL(oldtable);
352 return 0;
353}
354
Raymond Hettingerc991db22005-08-11 07:58:45 +0000355/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
356
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000357static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000358set_add_entry(register PySetObject *so, setentry *entry)
359{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000360 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361
362 assert(so->fill <= so->mask); /* at least one empty slot */
363 n_used = so->used;
364 Py_INCREF(entry->key);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000365 if (set_insert_key(so, entry->key, entry->hash) == -1) {
366 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000367 return -1;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000368 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000369 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
370 return 0;
371 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
372}
373
374static int
375set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000376{
377 register long hash;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000378 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000379
Raymond Hettingerc991db22005-08-11 07:58:45 +0000380 if (!PyString_CheckExact(key) ||
381 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382 hash = PyObject_Hash(key);
383 if (hash == -1)
384 return -1;
385 }
386 assert(so->fill <= so->mask); /* at least one empty slot */
387 n_used = so->used;
388 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000389 if (set_insert_key(so, key, hash) == -1) {
390 Py_DECREF(key);
391 return -1;
392 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000393 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
394 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000395 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000396}
397
398#define DISCARD_NOTFOUND 0
399#define DISCARD_FOUND 1
400
401static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000402set_discard_entry(PySetObject *so, setentry *oldentry)
403{ register setentry *entry;
404 PyObject *old_key;
405
406 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000407 if (entry == NULL)
408 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000409 if (entry->key == NULL || entry->key == dummy)
410 return DISCARD_NOTFOUND;
411 old_key = entry->key;
412 Py_INCREF(dummy);
413 entry->key = dummy;
414 so->used--;
415 Py_DECREF(old_key);
416 return DISCARD_FOUND;
417}
418
419static int
420set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000421{
422 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000423 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000424 PyObject *old_key;
425
426 assert (PyAnySet_Check(so));
427 if (!PyString_CheckExact(key) ||
428 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
429 hash = PyObject_Hash(key);
430 if (hash == -1)
431 return -1;
432 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000433 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000434 if (entry == NULL)
435 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000436 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000438 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000439 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000440 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000441 so->used--;
442 Py_DECREF(old_key);
443 return DISCARD_FOUND;
444}
445
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000446static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000447set_clear_internal(PySetObject *so)
448{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000449 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450 int table_is_malloced;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000451 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000452 setentry small_copy[PySet_MINSIZE];
453#ifdef Py_DEBUG
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000454 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000456
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457 n = so->mask + 1;
458 i = 0;
459#endif
460
461 table = so->table;
462 assert(table != NULL);
463 table_is_malloced = table != so->smalltable;
464
465 /* This is delicate. During the process of clearing the set,
466 * decrefs can cause the set to mutate. To avoid fatal confusion
467 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000468 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000469 * clearing.
470 */
471 fill = so->fill;
472 if (table_is_malloced)
473 EMPTY_TO_MINSIZE(so);
474
475 else if (fill > 0) {
476 /* It's a small table with something that needs to be cleared.
477 * Afraid the only safe way is to copy the set entries into
478 * another small table first.
479 */
480 memcpy(small_copy, table, sizeof(small_copy));
481 table = small_copy;
482 EMPTY_TO_MINSIZE(so);
483 }
484 /* else it's a small table that's already empty */
485
486 /* Now we can finally clear things. If C had refcounts, we could
487 * assert that the refcount on table is 1 now, i.e. that this function
488 * has unique access to it, so decref side-effects can't alter it.
489 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000490 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000491#ifdef Py_DEBUG
492 assert(i < n);
493 ++i;
494#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000495 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000497 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498 }
499#ifdef Py_DEBUG
500 else
Raymond Hettinger334b5b22006-03-26 03:11:29 +0000501 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502#endif
503 }
504
505 if (table_is_malloced)
506 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000507 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508}
509
510/*
511 * Iterate over a set table. Use like so:
512 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000513 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000514 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000515 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000516 * while (set_next(yourset, &pos, &entry)) {
517 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000518 * }
519 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000520 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000521 * mutates the table.
522 */
523static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000526 Py_ssize_t i;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000527 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000528 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529
530 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000531 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000532 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000533 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000536 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000537 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000538 if (i > mask)
539 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000540 assert(table[i].key != NULL);
541 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000542 return 1;
543}
544
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000545static void
546set_dealloc(PySetObject *so)
547{
548 register setentry *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000549 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000550 PyObject_GC_UnTrack(so);
551 Py_TRASHCAN_SAFE_BEGIN(so)
552 if (so->weakreflist != NULL)
553 PyObject_ClearWeakRefs((PyObject *) so);
554
555 for (entry = so->table; fill > 0; entry++) {
556 if (entry->key) {
557 --fill;
558 Py_DECREF(entry->key);
559 }
560 }
561 if (so->table != so->smalltable)
562 PyMem_DEL(so->table);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000563 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
564 free_list[numfree++] = so;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000565 else
Christian Heimese93237d2007-12-19 02:37:44 +0000566 Py_TYPE(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000567 Py_TRASHCAN_SAFE_END(so)
568}
569
570static int
571set_tp_print(PySetObject *so, FILE *fp, int flags)
572{
573 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000574 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000575 char *emit = ""; /* No separator emitted on first pass */
576 char *separator = ", ";
Raymond Hettinger53999102006-12-30 04:01:17 +0000577 int status = Py_ReprEnter((PyObject*)so);
578
579 if (status != 0) {
580 if (status < 0)
581 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000582 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000583 fprintf(fp, "%s(...)", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000584 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000585 return 0;
586 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000587
Brett Cannon01531592007-09-17 03:28:34 +0000588 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000589 fprintf(fp, "%s([", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000590 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591 while (set_next(so, &pos, &entry)) {
Brett Cannon01531592007-09-17 03:28:34 +0000592 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000593 fputs(emit, fp);
Brett Cannon01531592007-09-17 03:28:34 +0000594 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000595 emit = separator;
Raymond Hettinger53999102006-12-30 04:01:17 +0000596 if (PyObject_Print(entry->key, fp, 0) != 0) {
597 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000598 return -1;
Raymond Hettinger53999102006-12-30 04:01:17 +0000599 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000600 }
Brett Cannon01531592007-09-17 03:28:34 +0000601 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000602 fputs("])", fp);
Brett Cannon01531592007-09-17 03:28:34 +0000603 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000604 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000605 return 0;
606}
607
608static PyObject *
609set_repr(PySetObject *so)
610{
Raymond Hettinger53999102006-12-30 04:01:17 +0000611 PyObject *keys, *result=NULL, *listrepr;
612 int status = Py_ReprEnter((PyObject*)so);
613
614 if (status != 0) {
615 if (status < 0)
616 return NULL;
617 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
618 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000619
620 keys = PySequence_List((PyObject *)so);
621 if (keys == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000622 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000623 listrepr = PyObject_Repr(keys);
624 Py_DECREF(keys);
625 if (listrepr == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000626 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000627
628 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
629 PyString_AS_STRING(listrepr));
630 Py_DECREF(listrepr);
Raymond Hettinger53999102006-12-30 04:01:17 +0000631done:
632 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000633 return result;
634}
635
Martin v. Löwis18e16552006-02-15 17:27:45 +0000636static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000637set_len(PyObject *so)
638{
639 return ((PySetObject *)so)->used;
640}
641
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000642static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000643set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000644{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000645 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000646 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000647 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648
649 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000650 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000651
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000652 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000653 if (other == so || other->used == 0)
654 /* a.update(a) or a.update({}); nothing to do */
655 return 0;
656 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000657 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000658 * that there will be no (or few) overlapping keys.
659 */
660 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
661 if (set_table_resize(so, (so->used + other->used)*2) != 0)
662 return -1;
663 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000664 for (i = 0; i <= other->mask; i++) {
665 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000666 if (entry->key != NULL &&
667 entry->key != dummy) {
668 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000669 if (set_insert_key(so, entry->key, entry->hash) == -1) {
670 Py_DECREF(entry->key);
671 return -1;
672 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000673 }
674 }
675 return 0;
676}
677
678static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000679set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000680{
681 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000682 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000683
684 if (!PyString_CheckExact(key) ||
685 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
686 hash = PyObject_Hash(key);
687 if (hash == -1)
688 return -1;
689 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000690 entry = (so->lookup)(so, key, hash);
691 if (entry == NULL)
692 return -1;
693 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000694 return key != NULL && key != dummy;
695}
696
Raymond Hettingerc991db22005-08-11 07:58:45 +0000697static int
698set_contains_entry(PySetObject *so, setentry *entry)
699{
700 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000701 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000702
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000703 lu_entry = (so->lookup)(so, entry->key, entry->hash);
704 if (lu_entry == NULL)
705 return -1;
706 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000707 return key != NULL && key != dummy;
708}
709
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000710static PyObject *
711set_pop(PySetObject *so)
712{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000713 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000714 register setentry *entry;
715 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000716
717 assert (PyAnySet_Check(so));
718 if (so->used == 0) {
719 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
720 return NULL;
721 }
722
723 /* Set entry to "the first" unused or dummy set entry. We abuse
724 * the hash field of slot 0 to hold a search finger:
725 * If slot 0 has a value, use slot 0.
726 * Else slot 0 is being used to hold a search finger,
727 * and we use its hash value as the first index to look.
728 */
729 entry = &so->table[0];
730 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000731 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000732 /* The hash field may be a real hash value, or it may be a
733 * legit search finger, or it may be a once-legit search
734 * finger that's out of bounds now because it wrapped around
735 * or the table shrunk -- simply make sure it's in bounds now.
736 */
737 if (i > so->mask || i < 1)
738 i = 1; /* skip slot 0 */
739 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
740 i++;
741 if (i > so->mask)
742 i = 1;
743 }
744 }
745 key = entry->key;
746 Py_INCREF(dummy);
747 entry->key = dummy;
748 so->used--;
749 so->table[0].hash = i + 1; /* next place to start */
750 return key;
751}
752
753PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
754
755static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000756set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000757{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000758 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000759 setentry *entry;
760
761 while (set_next(so, &pos, &entry))
762 Py_VISIT(entry->key);
763 return 0;
764}
765
766static long
767frozenset_hash(PyObject *self)
768{
769 PySetObject *so = (PySetObject *)self;
770 long h, hash = 1927868237L;
771 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000772 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000773
774 if (so->hash != -1)
775 return so->hash;
776
777 hash *= PySet_GET_SIZE(self) + 1;
778 while (set_next(so, &pos, &entry)) {
779 /* Work to increase the bit dispersion for closely spaced hash
780 values. The is important because some use cases have many
781 combinations of a small number of elements with nearby
782 hashes so that many distinct combinations collapse to only
783 a handful of distinct hash values. */
784 h = entry->hash;
785 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
786 }
787 hash = hash * 69069L + 907133923L;
788 if (hash == -1)
789 hash = 590923713L;
790 so->hash = hash;
791 return hash;
792}
793
Raymond Hettingera9d99362005-08-05 00:01:15 +0000794/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000795
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796typedef struct {
797 PyObject_HEAD
798 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000799 Py_ssize_t si_used;
800 Py_ssize_t si_pos;
801 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802} setiterobject;
803
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804static void
805setiter_dealloc(setiterobject *si)
806{
807 Py_XDECREF(si->si_set);
808 PyObject_Del(si);
809}
810
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000811static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812setiter_len(setiterobject *si)
813{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000814 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000816 len = si->len;
817 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818}
819
Armin Rigof5b3e362006-02-11 21:32:43 +0000820PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000821
822static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000823 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000824 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000825};
826
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000827static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828{
829 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000830 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000831 register setentry *entry;
832 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000833
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000834 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000835 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000836 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000838 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839 PyErr_SetString(PyExc_RuntimeError,
840 "Set changed size during iteration");
841 si->si_used = -1; /* Make this state sticky */
842 return NULL;
843 }
844
845 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000846 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000847 entry = so->table;
848 mask = so->mask;
849 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850 i++;
851 si->si_pos = i+1;
852 if (i > mask)
853 goto fail;
854 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000855 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856 Py_INCREF(key);
857 return key;
858
859fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000860 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861 si->si_set = NULL;
862 return NULL;
863}
864
Hye-Shik Change2956762005-08-01 05:26:41 +0000865static PyTypeObject PySetIter_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000866 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000867 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000868 sizeof(setiterobject), /* tp_basicsize */
869 0, /* tp_itemsize */
870 /* methods */
871 (destructor)setiter_dealloc, /* tp_dealloc */
872 0, /* tp_print */
873 0, /* tp_getattr */
874 0, /* tp_setattr */
875 0, /* tp_compare */
876 0, /* tp_repr */
877 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000878 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000879 0, /* tp_as_mapping */
880 0, /* tp_hash */
881 0, /* tp_call */
882 0, /* tp_str */
883 PyObject_GenericGetAttr, /* tp_getattro */
884 0, /* tp_setattro */
885 0, /* tp_as_buffer */
886 Py_TPFLAGS_DEFAULT, /* tp_flags */
887 0, /* tp_doc */
888 0, /* tp_traverse */
889 0, /* tp_clear */
890 0, /* tp_richcompare */
891 0, /* tp_weaklistoffset */
892 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000893 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000894 setiter_methods, /* tp_methods */
895 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000896};
897
Martin v. Löwis72d20672006-04-11 09:04:12 +0000898static PyObject *
899set_iter(PySetObject *so)
900{
901 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
902 if (si == NULL)
903 return NULL;
904 Py_INCREF(so);
905 si->si_set = so;
906 si->si_used = so->used;
907 si->si_pos = 0;
908 si->len = so->used;
909 return (PyObject *)si;
910}
911
Raymond Hettingerd7946662005-08-01 21:39:29 +0000912static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000913set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000914{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000915 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000916
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +0000917 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000918 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000919
Raymond Hettingerdb67aef2007-02-01 21:02:59 +0000920 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000921 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000922 Py_ssize_t pos = 0;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000923 long hash;
Raymond Hettinger15cade02007-02-19 20:44:04 +0000924 Py_ssize_t dictsize = PyDict_Size(other);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000925
Raymond Hettinger15cade02007-02-19 20:44:04 +0000926 /* Do one big resize at the start, rather than
927 * incrementally resizing as we insert new keys. Expect
928 * that there will be no (or few) overlapping keys.
929 */
930 if (dictsize == -1)
931 return -1;
932 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
933 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
934 return -1;
935 }
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000936 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
937 setentry an_entry;
938
939 an_entry.hash = hash;
940 an_entry.key = key;
941 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000942 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000943 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000944 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000945 }
946
Raymond Hettingera38123e2003-11-24 22:18:49 +0000947 it = PyObject_GetIter(other);
948 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000950
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000951 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000952 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000953 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000954 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000955 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000956 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000957 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000958 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000959 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000960 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000961 return -1;
962 return 0;
963}
964
965static PyObject *
966set_update(PySetObject *so, PyObject *other)
967{
968 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000969 return NULL;
970 Py_RETURN_NONE;
971}
972
973PyDoc_STRVAR(update_doc,
974"Update a set with the union of itself and another.");
975
976static PyObject *
977make_new_set(PyTypeObject *type, PyObject *iterable)
978{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000979 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000980
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000981 if (dummy == NULL) { /* Auto-initialize dummy */
982 dummy = PyString_FromString("<dummy key>");
983 if (dummy == NULL)
984 return NULL;
985 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000986
987 /* create PySetObject structure */
Christian Heimes5b970ad2008-02-06 13:33:44 +0000988 if (numfree &&
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000989 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
Christian Heimes5b970ad2008-02-06 13:33:44 +0000990 so = free_list[--numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000991 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimese93237d2007-12-19 02:37:44 +0000992 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000993 _Py_NewReference((PyObject *)so);
994 EMPTY_TO_MINSIZE(so);
995 PyObject_GC_Track(so);
996 } else {
997 so = (PySetObject *)type->tp_alloc(type, 0);
998 if (so == NULL)
999 return NULL;
1000 /* tp_alloc has already zeroed the structure */
1001 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1002 INIT_NONZERO_SET_SLOTS(so);
1003 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001004
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001005 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001006 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001007
Raymond Hettingera38123e2003-11-24 22:18:49 +00001008 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001009 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001010 Py_DECREF(so);
1011 return NULL;
1012 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001013 }
1014
Raymond Hettingera690a992003-11-16 16:17:49 +00001015 return (PyObject *)so;
1016}
1017
Raymond Hettingerd7946662005-08-01 21:39:29 +00001018/* The empty frozenset is a singleton */
1019static PyObject *emptyfrozenset = NULL;
1020
Raymond Hettingera690a992003-11-16 16:17:49 +00001021static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001022frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001023{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001024 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001025
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001026 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001027 return NULL;
1028
Raymond Hettingera690a992003-11-16 16:17:49 +00001029 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1030 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001031
1032 if (type != &PyFrozenSet_Type)
1033 return make_new_set(type, iterable);
1034
1035 if (iterable != NULL) {
1036 /* frozenset(f) is idempotent */
1037 if (PyFrozenSet_CheckExact(iterable)) {
1038 Py_INCREF(iterable);
1039 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001040 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001041 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001042 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001043 return result;
1044 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001045 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001046 /* The empty frozenset is a singleton */
1047 if (emptyfrozenset == NULL)
1048 emptyfrozenset = make_new_set(type, NULL);
1049 Py_XINCREF(emptyfrozenset);
1050 return emptyfrozenset;
1051}
1052
1053void
1054PySet_Fini(void)
1055{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001056 PySetObject *so;
1057
Christian Heimes5b970ad2008-02-06 13:33:44 +00001058 while (numfree) {
1059 numfree--;
1060 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001061 PyObject_GC_Del(so);
1062 }
Martin v. Löwised8f7832006-04-15 12:47:23 +00001063 Py_CLEAR(dummy);
1064 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001065}
1066
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001067static PyObject *
1068set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1069{
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001070 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001071 return NULL;
1072
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001073 return make_new_set(type, NULL);
1074}
1075
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001076/* set_swap_bodies() switches the contents of any two sets by moving their
1077 internal data pointers and, if needed, copying the internal smalltables.
1078 Semantically equivalent to:
1079
1080 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1081
1082 The function always succeeds and it leaves both objects in a stable state.
1083 Useful for creating temporary frozensets from sets for membership testing
1084 in __contains__(), discard(), and remove(). Also useful for operations
1085 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001086 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001087*/
1088
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001089static void
1090set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001091{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001092 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001093 setentry *u;
1094 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1095 setentry tab[PySet_MINSIZE];
1096 long h;
1097
1098 t = a->fill; a->fill = b->fill; b->fill = t;
1099 t = a->used; a->used = b->used; b->used = t;
1100 t = a->mask; a->mask = b->mask; b->mask = t;
1101
1102 u = a->table;
1103 if (a->table == a->smalltable)
1104 u = b->smalltable;
1105 a->table = b->table;
1106 if (b->table == b->smalltable)
1107 a->table = a->smalltable;
1108 b->table = u;
1109
1110 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1111
1112 if (a->table == a->smalltable || b->table == b->smalltable) {
1113 memcpy(tab, a->smalltable, sizeof(tab));
1114 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1115 memcpy(b->smalltable, tab, sizeof(tab));
1116 }
1117
Christian Heimese93237d2007-12-19 02:37:44 +00001118 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1119 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001120 h = a->hash; a->hash = b->hash; b->hash = h;
1121 } else {
1122 a->hash = -1;
1123 b->hash = -1;
1124 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001125}
1126
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001127static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001128set_copy(PySetObject *so)
1129{
Christian Heimese93237d2007-12-19 02:37:44 +00001130 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001131}
1132
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001133static PyObject *
Raymond Hettinger17a74c32008-02-09 04:37:49 +00001134set_copy_method(PySetObject *so)
1135{
1136 if (Py_Py3kWarningFlag &&
1137 PyErr_Warn(PyExc_DeprecationWarning,
1138 "set.copy() not supported in 3.x") < 0)
1139 return NULL;
1140 return make_new_set(Py_TYPE(so), (PyObject *)so);
1141}
1142
1143static PyObject *
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001144frozenset_copy(PySetObject *so)
1145{
Raymond Hettinger17a74c32008-02-09 04:37:49 +00001146 if (Py_Py3kWarningFlag &&
1147 PyErr_Warn(PyExc_DeprecationWarning,
1148 "frozenset.copy() not supported in 3.x") < 0)
1149 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001150 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001151 Py_INCREF(so);
1152 return (PyObject *)so;
1153 }
1154 return set_copy(so);
1155}
1156
Raymond Hettingera690a992003-11-16 16:17:49 +00001157PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1158
1159static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001160set_clear(PySetObject *so)
1161{
1162 set_clear_internal(so);
1163 Py_RETURN_NONE;
1164}
1165
1166PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1167
1168static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001169set_union(PySetObject *so, PyObject *other)
1170{
1171 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001172
1173 result = (PySetObject *)set_copy(so);
1174 if (result == NULL)
1175 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001176 if ((PyObject *)so == other)
1177 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001178 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001179 Py_DECREF(result);
1180 return NULL;
1181 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001182 return (PyObject *)result;
1183}
1184
1185PyDoc_STRVAR(union_doc,
1186 "Return the union of two sets as a new set.\n\
1187\n\
1188(i.e. all elements that are in either set.)");
1189
1190static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001191set_or(PySetObject *so, PyObject *other)
1192{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001193 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001194 Py_INCREF(Py_NotImplemented);
1195 return Py_NotImplemented;
1196 }
1197 return set_union(so, other);
1198}
1199
1200static PyObject *
1201set_ior(PySetObject *so, PyObject *other)
1202{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001203 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001204 Py_INCREF(Py_NotImplemented);
1205 return Py_NotImplemented;
1206 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001207 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001208 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001209 Py_INCREF(so);
1210 return (PyObject *)so;
1211}
1212
1213static PyObject *
1214set_intersection(PySetObject *so, PyObject *other)
1215{
1216 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001217 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001218
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001219 if ((PyObject *)so == other)
1220 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001221
Christian Heimese93237d2007-12-19 02:37:44 +00001222 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001223 if (result == NULL)
1224 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001225
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001226 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001227 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001228 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001229
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001230 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001231 tmp = (PyObject *)so;
1232 so = (PySetObject *)other;
1233 other = tmp;
1234 }
1235
Raymond Hettingerc991db22005-08-11 07:58:45 +00001236 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001237 int rv = set_contains_entry(so, entry);
1238 if (rv == -1) {
1239 Py_DECREF(result);
1240 return NULL;
1241 }
1242 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001243 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001244 Py_DECREF(result);
1245 return NULL;
1246 }
1247 }
1248 }
1249 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001250 }
1251
Raymond Hettingera690a992003-11-16 16:17:49 +00001252 it = PyObject_GetIter(other);
1253 if (it == NULL) {
1254 Py_DECREF(result);
1255 return NULL;
1256 }
1257
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001258 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001259 int rv;
1260 setentry entry;
1261 long hash = PyObject_Hash(key);
1262
1263 if (hash == -1) {
1264 Py_DECREF(it);
1265 Py_DECREF(result);
1266 Py_DECREF(key);
1267 return NULL;
1268 }
1269 entry.hash = hash;
1270 entry.key = key;
1271 rv = set_contains_entry(so, &entry);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001272 if (rv == -1) {
1273 Py_DECREF(it);
1274 Py_DECREF(result);
1275 Py_DECREF(key);
1276 return NULL;
1277 }
1278 if (rv) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001279 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001280 Py_DECREF(it);
1281 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001282 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001283 return NULL;
1284 }
1285 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001286 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001287 }
1288 Py_DECREF(it);
1289 if (PyErr_Occurred()) {
1290 Py_DECREF(result);
1291 return NULL;
1292 }
1293 return (PyObject *)result;
1294}
1295
1296PyDoc_STRVAR(intersection_doc,
1297"Return the intersection of two sets as a new set.\n\
1298\n\
1299(i.e. all elements that are in both sets.)");
1300
1301static PyObject *
1302set_intersection_update(PySetObject *so, PyObject *other)
1303{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001304 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001305
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001306 tmp = set_intersection(so, other);
1307 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001308 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001309 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001310 Py_DECREF(tmp);
1311 Py_RETURN_NONE;
1312}
1313
1314PyDoc_STRVAR(intersection_update_doc,
1315"Update a set with the intersection of itself and another.");
1316
1317static PyObject *
1318set_and(PySetObject *so, PyObject *other)
1319{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001320 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001321 Py_INCREF(Py_NotImplemented);
1322 return Py_NotImplemented;
1323 }
1324 return set_intersection(so, other);
1325}
1326
1327static PyObject *
1328set_iand(PySetObject *so, PyObject *other)
1329{
1330 PyObject *result;
1331
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001332 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001333 Py_INCREF(Py_NotImplemented);
1334 return Py_NotImplemented;
1335 }
1336 result = set_intersection_update(so, other);
1337 if (result == NULL)
1338 return NULL;
1339 Py_DECREF(result);
1340 Py_INCREF(so);
1341 return (PyObject *)so;
1342}
1343
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001344static PyObject *
1345set_isdisjoint(PySetObject *so, PyObject *other)
1346{
1347 PyObject *key, *it, *tmp;
1348
1349 if ((PyObject *)so == other) {
1350 if (PySet_GET_SIZE(so) == 0)
1351 Py_RETURN_TRUE;
1352 else
1353 Py_RETURN_FALSE;
1354 }
1355
1356 if (PyAnySet_CheckExact(other)) {
1357 Py_ssize_t pos = 0;
1358 setentry *entry;
1359
1360 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1361 tmp = (PyObject *)so;
1362 so = (PySetObject *)other;
1363 other = tmp;
1364 }
1365 while (set_next((PySetObject *)other, &pos, &entry)) {
1366 int rv = set_contains_entry(so, entry);
1367 if (rv == -1)
1368 return NULL;
1369 if (rv)
1370 Py_RETURN_FALSE;
1371 }
1372 Py_RETURN_TRUE;
1373 }
1374
1375 it = PyObject_GetIter(other);
1376 if (it == NULL)
1377 return NULL;
1378
1379 while ((key = PyIter_Next(it)) != NULL) {
1380 int rv;
1381 setentry entry;
1382 long hash = PyObject_Hash(key);
1383
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001384 if (hash == -1) {
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001385 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001386 Py_DECREF(it);
1387 return NULL;
1388 }
1389 entry.hash = hash;
1390 entry.key = key;
1391 rv = set_contains_entry(so, &entry);
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001392 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001393 if (rv == -1) {
1394 Py_DECREF(it);
1395 return NULL;
1396 }
1397 if (rv) {
1398 Py_DECREF(it);
1399 Py_RETURN_FALSE;
1400 }
1401 }
1402 Py_DECREF(it);
1403 if (PyErr_Occurred())
1404 return NULL;
1405 Py_RETURN_TRUE;
1406}
1407
1408PyDoc_STRVAR(isdisjoint_doc,
1409"Return True if two sets have a null intersection.");
1410
Neal Norwitz6576bd82005-11-13 18:41:28 +00001411static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001412set_difference_update_internal(PySetObject *so, PyObject *other)
1413{
1414 if ((PyObject *)so == other)
1415 return set_clear_internal(so);
1416
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001417 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001418 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001419 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001420
1421 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001422 if (set_discard_entry(so, entry) == -1)
1423 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001424 } else {
1425 PyObject *key, *it;
1426 it = PyObject_GetIter(other);
1427 if (it == NULL)
1428 return -1;
1429
1430 while ((key = PyIter_Next(it)) != NULL) {
1431 if (set_discard_key(so, key) == -1) {
1432 Py_DECREF(it);
1433 Py_DECREF(key);
1434 return -1;
1435 }
1436 Py_DECREF(key);
1437 }
1438 Py_DECREF(it);
1439 if (PyErr_Occurred())
1440 return -1;
1441 }
1442 /* If more than 1/5 are dummies, then resize them away. */
1443 if ((so->fill - so->used) * 5 < so->mask)
1444 return 0;
1445 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1446}
1447
Raymond Hettingera690a992003-11-16 16:17:49 +00001448static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001449set_difference_update(PySetObject *so, PyObject *other)
1450{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001451 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001452 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001453 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001454}
1455
1456PyDoc_STRVAR(difference_update_doc,
1457"Remove all elements of another set from this set.");
1458
1459static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001460set_difference(PySetObject *so, PyObject *other)
1461{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001462 PyObject *result;
1463 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001464 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001465
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001466 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001467 result = set_copy(so);
1468 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001469 return NULL;
1470 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001471 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001472 Py_DECREF(result);
1473 return NULL;
1474 }
1475
Christian Heimese93237d2007-12-19 02:37:44 +00001476 result = make_new_set(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001477 if (result == NULL)
1478 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001479
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001480 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001481 while (set_next(so, &pos, &entry)) {
1482 setentry entrycopy;
1483 entrycopy.hash = entry->hash;
1484 entrycopy.key = entry->key;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001485 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001486 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1487 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001488 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001489 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001490 }
1491 }
1492 return result;
1493 }
1494
Raymond Hettingerc991db22005-08-11 07:58:45 +00001495 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001496 int rv = set_contains_entry((PySetObject *)other, entry);
1497 if (rv == -1) {
1498 Py_DECREF(result);
1499 return NULL;
1500 }
1501 if (!rv) {
1502 if (set_add_entry((PySetObject *)result, entry) == -1) {
1503 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001504 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001505 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001506 }
1507 }
1508 return result;
1509}
1510
1511PyDoc_STRVAR(difference_doc,
1512"Return the difference of two sets as a new set.\n\
1513\n\
1514(i.e. all elements that are in this set but not the other.)");
1515static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001516set_sub(PySetObject *so, PyObject *other)
1517{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001518 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001519 Py_INCREF(Py_NotImplemented);
1520 return Py_NotImplemented;
1521 }
1522 return set_difference(so, other);
1523}
1524
1525static PyObject *
1526set_isub(PySetObject *so, PyObject *other)
1527{
1528 PyObject *result;
1529
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001530 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001531 Py_INCREF(Py_NotImplemented);
1532 return Py_NotImplemented;
1533 }
1534 result = set_difference_update(so, other);
1535 if (result == NULL)
1536 return NULL;
1537 Py_DECREF(result);
1538 Py_INCREF(so);
1539 return (PyObject *)so;
1540}
1541
1542static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001543set_symmetric_difference_update(PySetObject *so, PyObject *other)
1544{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001545 PySetObject *otherset;
1546 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001547 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001548 setentry *entry;
1549
1550 if ((PyObject *)so == other)
1551 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001552
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001553 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001554 PyObject *value;
1555 int rv;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001556 long hash;
1557 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001558 setentry an_entry;
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001559
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001560 an_entry.hash = hash;
1561 an_entry.key = key;
1562 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001563 if (rv == -1)
1564 return NULL;
1565 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001566 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001567 return NULL;
1568 }
1569 }
1570 Py_RETURN_NONE;
1571 }
1572
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001573 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001574 Py_INCREF(other);
1575 otherset = (PySetObject *)other;
1576 } else {
Christian Heimese93237d2007-12-19 02:37:44 +00001577 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001578 if (otherset == NULL)
1579 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001580 }
1581
Raymond Hettingerc991db22005-08-11 07:58:45 +00001582 while (set_next(otherset, &pos, &entry)) {
1583 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001584 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001585 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001586 return NULL;
1587 }
1588 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001589 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001590 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001591 return NULL;
1592 }
1593 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001594 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001595 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001596 Py_RETURN_NONE;
1597}
1598
1599PyDoc_STRVAR(symmetric_difference_update_doc,
1600"Update a set with the symmetric difference of itself and another.");
1601
1602static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001603set_symmetric_difference(PySetObject *so, PyObject *other)
1604{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001605 PyObject *rv;
1606 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001607
Christian Heimese93237d2007-12-19 02:37:44 +00001608 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001609 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001610 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001611 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1612 if (rv == NULL)
1613 return NULL;
1614 Py_DECREF(rv);
1615 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001616}
1617
1618PyDoc_STRVAR(symmetric_difference_doc,
1619"Return the symmetric difference of two sets as a new set.\n\
1620\n\
1621(i.e. all elements that are in exactly one of the sets.)");
1622
1623static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001624set_xor(PySetObject *so, PyObject *other)
1625{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001626 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001627 Py_INCREF(Py_NotImplemented);
1628 return Py_NotImplemented;
1629 }
1630 return set_symmetric_difference(so, other);
1631}
1632
1633static PyObject *
1634set_ixor(PySetObject *so, PyObject *other)
1635{
1636 PyObject *result;
1637
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001638 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001639 Py_INCREF(Py_NotImplemented);
1640 return Py_NotImplemented;
1641 }
1642 result = set_symmetric_difference_update(so, other);
1643 if (result == NULL)
1644 return NULL;
1645 Py_DECREF(result);
1646 Py_INCREF(so);
1647 return (PyObject *)so;
1648}
1649
1650static PyObject *
1651set_issubset(PySetObject *so, PyObject *other)
1652{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001653 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001654 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001655
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001656 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001657 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001658 tmp = make_new_set(&PySet_Type, other);
1659 if (tmp == NULL)
1660 return NULL;
1661 result = set_issubset(so, tmp);
1662 Py_DECREF(tmp);
1663 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001664 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001665 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001666 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001667
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001668 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001669 int rv = set_contains_entry((PySetObject *)other, entry);
1670 if (rv == -1)
1671 return NULL;
1672 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001673 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001674 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001675 Py_RETURN_TRUE;
1676}
1677
1678PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1679
1680static PyObject *
1681set_issuperset(PySetObject *so, PyObject *other)
1682{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001683 PyObject *tmp, *result;
1684
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001685 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001686 tmp = make_new_set(&PySet_Type, other);
1687 if (tmp == NULL)
1688 return NULL;
1689 result = set_issuperset(so, tmp);
1690 Py_DECREF(tmp);
1691 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001692 }
1693 return set_issubset((PySetObject *)other, (PyObject *)so);
1694}
1695
1696PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1697
Raymond Hettingera690a992003-11-16 16:17:49 +00001698static PyObject *
1699set_richcompare(PySetObject *v, PyObject *w, int op)
1700{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001701 PyObject *r1, *r2;
1702
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001703 if(!PyAnySet_Check(w)) {
1704 if (op == Py_EQ)
1705 Py_RETURN_FALSE;
1706 if (op == Py_NE)
1707 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001708 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1709 return NULL;
1710 }
1711 switch (op) {
1712 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001713 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001714 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001715 if (v->hash != -1 &&
1716 ((PySetObject *)w)->hash != -1 &&
1717 v->hash != ((PySetObject *)w)->hash)
1718 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001719 return set_issubset(v, w);
1720 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001721 r1 = set_richcompare(v, w, Py_EQ);
1722 if (r1 == NULL)
1723 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001724 r2 = PyBool_FromLong(PyObject_Not(r1));
1725 Py_DECREF(r1);
1726 return r2;
1727 case Py_LE:
1728 return set_issubset(v, w);
1729 case Py_GE:
1730 return set_issuperset(v, w);
1731 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001732 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001733 Py_RETURN_FALSE;
1734 return set_issubset(v, w);
1735 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001736 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001737 Py_RETURN_FALSE;
1738 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001739 }
1740 Py_INCREF(Py_NotImplemented);
1741 return Py_NotImplemented;
1742}
1743
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001744static int
Georg Brandl347b3002006-03-30 11:57:00 +00001745set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001746{
1747 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1748 return -1;
1749}
1750
Raymond Hettingera690a992003-11-16 16:17:49 +00001751static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001752set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001753{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001754 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001755 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001756 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001757}
1758
1759PyDoc_STRVAR(add_doc,
1760"Add an element to a set.\n\
1761\n\
1762This has no effect if the element is already present.");
1763
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001764static int
1765set_contains(PySetObject *so, PyObject *key)
1766{
1767 PyObject *tmpkey;
1768 int rv;
1769
1770 rv = set_contains_key(so, key);
1771 if (rv == -1) {
1772 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1773 return -1;
1774 PyErr_Clear();
1775 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1776 if (tmpkey == NULL)
1777 return -1;
1778 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1779 rv = set_contains(so, tmpkey);
1780 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1781 Py_DECREF(tmpkey);
1782 }
1783 return rv;
1784}
1785
1786static PyObject *
1787set_direct_contains(PySetObject *so, PyObject *key)
1788{
1789 long result;
1790
1791 result = set_contains(so, key);
1792 if (result == -1)
1793 return NULL;
1794 return PyBool_FromLong(result);
1795}
1796
1797PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1798
Raymond Hettingera690a992003-11-16 16:17:49 +00001799static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001800set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001801{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001802 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001803 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001804
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001805 rv = set_discard_key(so, key);
1806 if (rv == -1) {
1807 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1808 return NULL;
1809 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001810 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1811 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001812 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001813 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001814 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001815 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001816 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001817 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001818 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +00001819 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001820 return NULL;
1821 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001822 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001823}
1824
1825PyDoc_STRVAR(remove_doc,
1826"Remove an element from a set; it must be a member.\n\
1827\n\
1828If the element is not a member, raise a KeyError.");
1829
1830static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001831set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001832{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001833 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001834 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001835
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001836 rv = set_discard_key(so, key);
1837 if (rv == -1) {
1838 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1839 return NULL;
1840 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001841 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1842 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001843 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001844 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001845 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001846 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001847 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001848 return result;
1849 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001850 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001851}
1852
1853PyDoc_STRVAR(discard_doc,
1854"Remove an element from a set if it is a member.\n\
1855\n\
1856If the element is not a member, do nothing.");
1857
1858static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001859set_reduce(PySetObject *so)
1860{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001861 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001862
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001863 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001864 if (keys == NULL)
1865 goto done;
1866 args = PyTuple_Pack(1, keys);
1867 if (args == NULL)
1868 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001869 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1870 if (dict == NULL) {
1871 PyErr_Clear();
1872 dict = Py_None;
1873 Py_INCREF(dict);
1874 }
Christian Heimese93237d2007-12-19 02:37:44 +00001875 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001876done:
1877 Py_XDECREF(args);
1878 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001879 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001880 return result;
1881}
1882
1883PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1884
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001885static int
1886set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1887{
1888 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001889
1890 if (!PyAnySet_Check(self))
1891 return -1;
Christian Heimese93237d2007-12-19 02:37:44 +00001892 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001893 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001894 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001895 self->hash = -1;
1896 if (iterable == NULL)
1897 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001898 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001899}
1900
Raymond Hettingera690a992003-11-16 16:17:49 +00001901static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001902 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001903 0, /* sq_concat */
1904 0, /* sq_repeat */
1905 0, /* sq_item */
1906 0, /* sq_slice */
1907 0, /* sq_ass_item */
1908 0, /* sq_ass_slice */
1909 (objobjproc)set_contains, /* sq_contains */
1910};
1911
1912/* set object ********************************************************/
1913
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001914#ifdef Py_DEBUG
1915static PyObject *test_c_api(PySetObject *so);
1916
1917PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1918All is well if assertions don't fail.");
1919#endif
1920
Raymond Hettingera690a992003-11-16 16:17:49 +00001921static PyMethodDef set_methods[] = {
1922 {"add", (PyCFunction)set_add, METH_O,
1923 add_doc},
1924 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1925 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001926 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001927 contains_doc},
Raymond Hettinger17a74c32008-02-09 04:37:49 +00001928 {"copy", (PyCFunction)set_copy_method, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001929 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001930 {"discard", (PyCFunction)set_discard, METH_O,
1931 discard_doc},
1932 {"difference", (PyCFunction)set_difference, METH_O,
1933 difference_doc},
1934 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1935 difference_update_doc},
1936 {"intersection",(PyCFunction)set_intersection, METH_O,
1937 intersection_doc},
1938 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1939 intersection_update_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001940 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1941 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001942 {"issubset", (PyCFunction)set_issubset, METH_O,
1943 issubset_doc},
1944 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1945 issuperset_doc},
1946 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1947 pop_doc},
1948 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1949 reduce_doc},
1950 {"remove", (PyCFunction)set_remove, METH_O,
1951 remove_doc},
1952 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1953 symmetric_difference_doc},
1954 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1955 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001956#ifdef Py_DEBUG
1957 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1958 test_c_api_doc},
1959#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001960 {"union", (PyCFunction)set_union, METH_O,
1961 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001962 {"update", (PyCFunction)set_update, METH_O,
1963 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001964 {NULL, NULL} /* sentinel */
1965};
1966
1967static PyNumberMethods set_as_number = {
1968 0, /*nb_add*/
1969 (binaryfunc)set_sub, /*nb_subtract*/
1970 0, /*nb_multiply*/
1971 0, /*nb_divide*/
1972 0, /*nb_remainder*/
1973 0, /*nb_divmod*/
1974 0, /*nb_power*/
1975 0, /*nb_negative*/
1976 0, /*nb_positive*/
1977 0, /*nb_absolute*/
1978 0, /*nb_nonzero*/
1979 0, /*nb_invert*/
1980 0, /*nb_lshift*/
1981 0, /*nb_rshift*/
1982 (binaryfunc)set_and, /*nb_and*/
1983 (binaryfunc)set_xor, /*nb_xor*/
1984 (binaryfunc)set_or, /*nb_or*/
1985 0, /*nb_coerce*/
1986 0, /*nb_int*/
1987 0, /*nb_long*/
1988 0, /*nb_float*/
1989 0, /*nb_oct*/
1990 0, /*nb_hex*/
1991 0, /*nb_inplace_add*/
1992 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1993 0, /*nb_inplace_multiply*/
1994 0, /*nb_inplace_divide*/
1995 0, /*nb_inplace_remainder*/
1996 0, /*nb_inplace_power*/
1997 0, /*nb_inplace_lshift*/
1998 0, /*nb_inplace_rshift*/
1999 (binaryfunc)set_iand, /*nb_inplace_and*/
2000 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2001 (binaryfunc)set_ior, /*nb_inplace_or*/
2002};
2003
2004PyDoc_STRVAR(set_doc,
2005"set(iterable) --> set object\n\
2006\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002007Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002008
2009PyTypeObject PySet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002010 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002011 "set", /* tp_name */
2012 sizeof(PySetObject), /* tp_basicsize */
2013 0, /* tp_itemsize */
2014 /* methods */
2015 (destructor)set_dealloc, /* tp_dealloc */
2016 (printfunc)set_tp_print, /* tp_print */
2017 0, /* tp_getattr */
2018 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002019 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002020 (reprfunc)set_repr, /* tp_repr */
2021 &set_as_number, /* tp_as_number */
2022 &set_as_sequence, /* tp_as_sequence */
2023 0, /* tp_as_mapping */
Guido van Rossum64c06e32007-11-22 00:55:51 +00002024 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00002025 0, /* tp_call */
2026 0, /* tp_str */
2027 PyObject_GenericGetAttr, /* tp_getattro */
2028 0, /* tp_setattro */
2029 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002030 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002031 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002032 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002033 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002034 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002035 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002036 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002037 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002038 0, /* tp_iternext */
2039 set_methods, /* tp_methods */
2040 0, /* tp_members */
2041 0, /* tp_getset */
2042 0, /* tp_base */
2043 0, /* tp_dict */
2044 0, /* tp_descr_get */
2045 0, /* tp_descr_set */
2046 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002047 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002048 PyType_GenericAlloc, /* tp_alloc */
2049 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002050 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002051};
2052
2053/* frozenset object ********************************************************/
2054
2055
2056static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002057 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002058 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002059 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002060 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002061 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002062 difference_doc},
2063 {"intersection",(PyCFunction)set_intersection, METH_O,
2064 intersection_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00002065 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2066 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002067 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002068 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002069 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002070 issuperset_doc},
2071 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2072 reduce_doc},
2073 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2074 symmetric_difference_doc},
2075 {"union", (PyCFunction)set_union, METH_O,
2076 union_doc},
2077 {NULL, NULL} /* sentinel */
2078};
2079
2080static PyNumberMethods frozenset_as_number = {
2081 0, /*nb_add*/
2082 (binaryfunc)set_sub, /*nb_subtract*/
2083 0, /*nb_multiply*/
2084 0, /*nb_divide*/
2085 0, /*nb_remainder*/
2086 0, /*nb_divmod*/
2087 0, /*nb_power*/
2088 0, /*nb_negative*/
2089 0, /*nb_positive*/
2090 0, /*nb_absolute*/
2091 0, /*nb_nonzero*/
2092 0, /*nb_invert*/
2093 0, /*nb_lshift*/
2094 0, /*nb_rshift*/
2095 (binaryfunc)set_and, /*nb_and*/
2096 (binaryfunc)set_xor, /*nb_xor*/
2097 (binaryfunc)set_or, /*nb_or*/
2098};
2099
2100PyDoc_STRVAR(frozenset_doc,
2101"frozenset(iterable) --> frozenset object\n\
2102\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002103Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002104
2105PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002106 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002107 "frozenset", /* tp_name */
2108 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002109 0, /* tp_itemsize */
2110 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002111 (destructor)set_dealloc, /* tp_dealloc */
2112 (printfunc)set_tp_print, /* tp_print */
2113 0, /* tp_getattr */
2114 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002115 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002116 (reprfunc)set_repr, /* tp_repr */
2117 &frozenset_as_number, /* tp_as_number */
2118 &set_as_sequence, /* tp_as_sequence */
2119 0, /* tp_as_mapping */
2120 frozenset_hash, /* tp_hash */
2121 0, /* tp_call */
2122 0, /* tp_str */
2123 PyObject_GenericGetAttr, /* tp_getattro */
2124 0, /* tp_setattro */
2125 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002126 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002127 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002128 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002129 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002130 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002131 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002132 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002133 (getiterfunc)set_iter, /* tp_iter */
2134 0, /* tp_iternext */
2135 frozenset_methods, /* tp_methods */
2136 0, /* tp_members */
2137 0, /* tp_getset */
2138 0, /* tp_base */
2139 0, /* tp_dict */
2140 0, /* tp_descr_get */
2141 0, /* tp_descr_set */
2142 0, /* tp_dictoffset */
2143 0, /* tp_init */
2144 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002145 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002146 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002147};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002148
2149
2150/***** C API functions *************************************************/
2151
2152PyObject *
2153PySet_New(PyObject *iterable)
2154{
2155 return make_new_set(&PySet_Type, iterable);
2156}
2157
2158PyObject *
2159PyFrozenSet_New(PyObject *iterable)
2160{
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002161 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002162}
2163
Neal Norwitz8c49c822006-03-04 18:41:19 +00002164Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002165PySet_Size(PyObject *anyset)
2166{
2167 if (!PyAnySet_Check(anyset)) {
2168 PyErr_BadInternalCall();
2169 return -1;
2170 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002171 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002172}
2173
2174int
Barry Warsaw176014f2006-03-30 22:45:35 +00002175PySet_Clear(PyObject *set)
2176{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002177 if (!PySet_Check(set)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002178 PyErr_BadInternalCall();
2179 return -1;
2180 }
2181 return set_clear_internal((PySetObject *)set);
2182}
2183
2184int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002185PySet_Contains(PyObject *anyset, PyObject *key)
2186{
2187 if (!PyAnySet_Check(anyset)) {
2188 PyErr_BadInternalCall();
2189 return -1;
2190 }
2191 return set_contains_key((PySetObject *)anyset, key);
2192}
2193
2194int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002195PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002196{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002197 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002198 PyErr_BadInternalCall();
2199 return -1;
2200 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002201 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002202}
2203
2204int
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002205PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002206{
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +00002207 if (!PySet_Check(anyset) &&
2208 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerdee3f652008-01-26 09:31:11 +00002209 PyErr_BadInternalCall();
2210 return -1;
2211 }
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002212 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002213}
2214
Barry Warsaw176014f2006-03-30 22:45:35 +00002215int
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002216_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Barry Warsaw176014f2006-03-30 22:45:35 +00002217{
2218 setentry *entry_ptr;
2219
2220 if (!PyAnySet_Check(set)) {
2221 PyErr_BadInternalCall();
2222 return -1;
2223 }
2224 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2225 return 0;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002226 *key = entry_ptr->key;
2227 return 1;
2228}
2229
2230int
2231_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2232{
2233 setentry *entry;
2234
2235 if (!PyAnySet_Check(set)) {
2236 PyErr_BadInternalCall();
2237 return -1;
2238 }
2239 if (set_next((PySetObject *)set, pos, &entry) == 0)
2240 return 0;
2241 *key = entry->key;
2242 *hash = entry->hash;
Barry Warsaw176014f2006-03-30 22:45:35 +00002243 return 1;
2244}
2245
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002246PyObject *
2247PySet_Pop(PyObject *set)
2248{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002249 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002250 PyErr_BadInternalCall();
2251 return NULL;
2252 }
2253 return set_pop((PySetObject *)set);
2254}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002255
Barry Warsaw176014f2006-03-30 22:45:35 +00002256int
2257_PySet_Update(PyObject *set, PyObject *iterable)
2258{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002259 if (!PySet_Check(set)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002260 PyErr_BadInternalCall();
2261 return -1;
2262 }
2263 return set_update_internal((PySetObject *)set, iterable);
2264}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002265
2266#ifdef Py_DEBUG
2267
2268/* Test code to be called with any three element set.
2269 Returns True and original set is restored. */
2270
2271#define assertRaises(call_return_value, exception) \
2272 do { \
2273 assert(call_return_value); \
2274 assert(PyErr_ExceptionMatches(exception)); \
2275 PyErr_Clear(); \
2276 } while(0)
2277
2278static PyObject *
2279test_c_api(PySetObject *so)
2280{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002281 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002282 char *s;
2283 Py_ssize_t i;
Guido van Rossum360496d2007-05-10 17:20:15 +00002284 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Barry Warsaw176014f2006-03-30 22:45:35 +00002285 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002286
2287 /* Verify preconditions and exercise type/size checks */
2288 assert(PyAnySet_Check(ob));
2289 assert(PyAnySet_CheckExact(ob));
2290 assert(!PyFrozenSet_CheckExact(ob));
2291 assert(PySet_Size(ob) == 3);
2292 assert(PySet_GET_SIZE(ob) == 3);
2293
2294 /* Raise TypeError for non-iterable constructor arguments */
2295 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2296 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2297
2298 /* Raise TypeError for unhashable key */
2299 dup = PySet_New(ob);
2300 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2301 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2302 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2303
2304 /* Exercise successful pop, contains, add, and discard */
2305 elem = PySet_Pop(ob);
2306 assert(PySet_Contains(ob, elem) == 0);
2307 assert(PySet_GET_SIZE(ob) == 2);
2308 assert(PySet_Add(ob, elem) == 0);
2309 assert(PySet_Contains(ob, elem) == 1);
2310 assert(PySet_GET_SIZE(ob) == 3);
2311 assert(PySet_Discard(ob, elem) == 1);
2312 assert(PySet_GET_SIZE(ob) == 2);
2313 assert(PySet_Discard(ob, elem) == 0);
2314 assert(PySet_GET_SIZE(ob) == 2);
2315
Barry Warsaw176014f2006-03-30 22:45:35 +00002316 /* Exercise clear */
2317 dup2 = PySet_New(dup);
2318 assert(PySet_Clear(dup2) == 0);
2319 assert(PySet_Size(dup2) == 0);
2320 Py_DECREF(dup2);
2321
2322 /* Raise SystemError on clear or update of frozen set */
2323 f = PyFrozenSet_New(dup);
2324 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2325 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +00002326 assert(PySet_Add(f, elem) == 0);
2327 Py_INCREF(f);
2328 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2329 Py_DECREF(f);
Barry Warsaw176014f2006-03-30 22:45:35 +00002330 Py_DECREF(f);
2331
2332 /* Exercise direct iteration */
2333 i = 0, count = 0;
Guido van Rossum360496d2007-05-10 17:20:15 +00002334 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2335 s = PyString_AsString(x);
Barry Warsaw176014f2006-03-30 22:45:35 +00002336 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2337 count++;
2338 }
2339 assert(count == 3);
2340
2341 /* Exercise updates */
2342 dup2 = PySet_New(NULL);
2343 assert(_PySet_Update(dup2, dup) == 0);
2344 assert(PySet_Size(dup2) == 3);
2345 assert(_PySet_Update(dup2, dup) == 0);
2346 assert(PySet_Size(dup2) == 3);
2347 Py_DECREF(dup2);
2348
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002349 /* Raise SystemError when self argument is not a set or frozenset. */
2350 t = PyTuple_New(0);
2351 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2352 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2353 Py_DECREF(t);
2354
2355 /* Raise SystemError when self argument is not a set. */
2356 f = PyFrozenSet_New(dup);
2357 assert(PySet_Size(f) == 3);
2358 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002359 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2360 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2361 Py_DECREF(f);
2362
2363 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002364 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2365 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002366 assert(PySet_GET_SIZE(ob) == 0);
2367 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2368
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002369 /* Restore the set from the copy using the PyNumber API */
2370 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2371 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002372
2373 /* Verify constructors accept NULL arguments */
2374 f = PySet_New(NULL);
2375 assert(f != NULL);
2376 assert(PySet_GET_SIZE(f) == 0);
2377 Py_DECREF(f);
2378 f = PyFrozenSet_New(NULL);
2379 assert(f != NULL);
2380 assert(PyFrozenSet_CheckExact(f));
2381 assert(PySet_GET_SIZE(f) == 0);
2382 Py_DECREF(f);
2383
2384 Py_DECREF(elem);
2385 Py_DECREF(dup);
2386 Py_RETURN_TRUE;
2387}
2388
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002389#undef assertRaises
2390
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002391#endif