blob: 5e8e05dd897f3fc3570ff52d5a66b6a88ff77305 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Raymond Hettingera9d99362005-08-05 00:01:15 +00002/* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00005
Martin v. Löwis68192102007-07-21 06:55:02 +00006 Copyright (c) 2003-2007 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00007 All rights reserved.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00008*/
9
Raymond Hettingera690a992003-11-16 16:17:49 +000010#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000011#include "structmember.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000012
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
19 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
25}
26
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000027/* This must be >= 1. */
28#define PERTURB_SHIFT 5
29
30/* Object used as dummy key to fill deleted entries */
Raymond Hettingera9d99362005-08-05 00:01:15 +000031static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000032
Armin Rigoe1709372006-04-12 17:06:05 +000033#ifdef Py_REF_DEBUG
34PyObject *
35_PySet_Dummy(void)
36{
37 return dummy;
38}
39#endif
40
Raymond Hettingerbc841a12005-08-07 13:02:53 +000041#define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
44 (so)->hash = -1; \
45 } while(0)
46
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000047#define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000050 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051 } while(0)
52
Raymond Hettingerbc841a12005-08-07 13:02:53 +000053/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes5b970ad2008-02-06 13:33:44 +000054#ifndef PySet_MAXFREELIST
55#define PySet_MAXFREELIST 80
56#endif
57static PySetObject *free_list[PySet_MAXFREELIST];
58static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000059
60/*
61The basic lookup function used by all operations.
62This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
63Open addressing is preferred over chaining since the link overhead for
64chaining would be substantial (100% with typical malloc overhead).
65
66The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000067probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000068
69All arithmetic on hash should ignore overflow.
70
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000071Unlike the dictionary implementation, the lookkey functions can return
72NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073*/
74
75static setentry *
76set_lookkey(PySetObject *so, PyObject *key, register long hash)
77{
Martin v. Löwis18e16552006-02-15 17:27:45 +000078 register Py_ssize_t i;
79 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000080 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +000081 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000082 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000083 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000084 register int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000085 PyObject *startkey;
86
87 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +000088 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000089 if (entry->key == NULL || entry->key == key)
90 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000091
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000092 if (entry->key == dummy)
93 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000094 else {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000095 if (entry->hash == hash) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +000096 startkey = entry->key;
Raymond Hettingerd99bee72008-05-30 06:49:47 +000097 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000098 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Raymond Hettingerd99bee72008-05-30 06:49:47 +000099 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000100 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000101 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000102 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000103 if (cmp > 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000104 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000105 }
106 else {
107 /* The compare did major nasty stuff to the
108 * set: start over.
109 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000110 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000111 }
112 }
113 freeslot = NULL;
114 }
115
116 /* In the loop, key == dummy is by far (factor of 100s) the
117 least likely outcome, so test for that last. */
118 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
119 i = (i << 2) + i + perturb + 1;
Raymond Hettingera580c472005-08-05 17:19:54 +0000120 entry = &table[i & mask];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000121 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000122 if (freeslot != NULL)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000123 entry = freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000124 break;
125 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000126 if (entry->key == key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000127 break;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000128 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000129 startkey = entry->key;
Raymond Hettingerd99bee72008-05-30 06:49:47 +0000130 Py_INCREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000131 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Raymond Hettingerd99bee72008-05-30 06:49:47 +0000132 Py_DECREF(startkey);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000133 if (cmp < 0)
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000134 return NULL;
Raymond Hettingera580c472005-08-05 17:19:54 +0000135 if (table == so->table && entry->key == startkey) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000136 if (cmp > 0)
137 break;
138 }
139 else {
140 /* The compare did major nasty stuff to the
141 * set: start over.
142 */
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000143 return set_lookkey(so, key, hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000144 }
145 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000146 else if (entry->key == dummy && freeslot == NULL)
147 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000148 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000149 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000150}
151
152/*
153 * Hacked up version of set_lookkey which can assume keys are always strings;
Christian Heimes593daf52008-05-26 12:51:38 +0000154 * This means we can always use _PyBytes_Eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000155 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000156 */
157static setentry *
158set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
159{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000160 register Py_ssize_t i;
161 register size_t perturb;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000162 register setentry *freeslot;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000163 register size_t mask = so->mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000164 setentry *table = so->table;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000165 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000166
167 /* Make sure this function doesn't have to handle non-string keys,
168 including subclasses of str; e.g., one reason to subclass
169 strings is to override __eq__, and for speed we don't cater to
170 that here. */
Christian Heimes593daf52008-05-26 12:51:38 +0000171 if (!PyBytes_CheckExact(key)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000172 so->lookup = set_lookkey;
173 return set_lookkey(so, key, hash);
174 }
175 i = hash & mask;
Raymond Hettingera580c472005-08-05 17:19:54 +0000176 entry = &table[i];
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000177 if (entry->key == NULL || entry->key == key)
178 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000179 if (entry->key == dummy)
180 freeslot = entry;
181 else {
Christian Heimes593daf52008-05-26 12:51:38 +0000182 if (entry->hash == hash && _PyBytes_Eq(entry->key, key))
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000183 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000184 freeslot = NULL;
185 }
186
187 /* In the loop, key == dummy is by far (factor of 100s) the
188 least likely outcome, so test for that last. */
189 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
190 i = (i << 2) + i + perturb + 1;
191 entry = &table[i & mask];
192 if (entry->key == NULL)
193 return freeslot == NULL ? entry : freeslot;
194 if (entry->key == key
195 || (entry->hash == hash
196 && entry->key != dummy
Christian Heimes593daf52008-05-26 12:51:38 +0000197 && _PyBytes_Eq(entry->key, key)))
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000198 return entry;
199 if (entry->key == dummy && freeslot == NULL)
200 freeslot = entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000201 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000202 assert(0); /* NOT REACHED */
203 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000204}
205
206/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000207Internal routine to insert a new key into the table.
Raymond Hettinger0c850862006-12-08 04:24:33 +0000208Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000209Eats a reference to key.
210*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000211static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000212set_insert_key(register PySetObject *so, PyObject *key, long hash)
213{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000214 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000215 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
216
217 assert(so->lookup != NULL);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000218 entry = so->lookup(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000219 if (entry == NULL)
220 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000221 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000222 /* UNUSED */
223 so->fill++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000224 entry->key = key;
225 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000226 so->used++;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000227 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000228 /* DUMMY */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000229 entry->key = key;
230 entry->hash = hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000231 so->used++;
232 Py_DECREF(dummy);
233 } else {
234 /* ACTIVE */
235 Py_DECREF(key);
236 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000237 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000238}
239
240/*
Raymond Hettinger0c850862006-12-08 04:24:33 +0000241Internal routine used by set_table_resize() to insert an item which is
242known to be absent from the set. This routine also assumes that
243the set contains no deleted entries. Besides the performance benefit,
244using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
245Note that no refcounts are changed by this routine; if needed, the caller
246is responsible for incref'ing `key`.
247*/
248static void
249set_insert_clean(register PySetObject *so, PyObject *key, long hash)
250{
251 register size_t i;
252 register size_t perturb;
253 register size_t mask = (size_t)so->mask;
254 setentry *table = so->table;
255 register setentry *entry;
256
257 i = hash & mask;
258 entry = &table[i];
259 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
260 i = (i << 2) + i + perturb + 1;
261 entry = &table[i & mask];
262 }
263 so->fill++;
264 entry->key = key;
265 entry->hash = hash;
266 so->used++;
267}
268
269/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000271keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272actually be smaller than the old one.
273*/
274static int
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000275set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000276{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000277 Py_ssize_t newsize;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000278 setentry *oldtable, *newtable, *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000279 Py_ssize_t i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000280 int is_oldtable_malloced;
281 setentry small_copy[PySet_MINSIZE];
282
283 assert(minused >= 0);
284
285 /* Find the smallest table size > minused. */
286 for (newsize = PySet_MINSIZE;
287 newsize <= minused && newsize > 0;
288 newsize <<= 1)
289 ;
290 if (newsize <= 0) {
291 PyErr_NoMemory();
292 return -1;
293 }
294
295 /* Get space for a new table. */
296 oldtable = so->table;
297 assert(oldtable != NULL);
298 is_oldtable_malloced = oldtable != so->smalltable;
299
300 if (newsize == PySet_MINSIZE) {
301 /* A large table is shrinking, or we can't get any smaller. */
302 newtable = so->smalltable;
303 if (newtable == oldtable) {
304 if (so->fill == so->used) {
305 /* No dummies, so no point doing anything. */
306 return 0;
307 }
308 /* We're not going to resize it, but rebuild the
309 table anyway to purge old dummy entries.
310 Subtle: This is *necessary* if fill==size,
311 as set_lookkey needs at least one virgin slot to
312 terminate failing searches. If fill < size, it's
313 merely desirable, as dummies slow searches. */
314 assert(so->fill > so->used);
315 memcpy(small_copy, oldtable, sizeof(small_copy));
316 oldtable = small_copy;
317 }
318 }
319 else {
320 newtable = PyMem_NEW(setentry, newsize);
321 if (newtable == NULL) {
322 PyErr_NoMemory();
323 return -1;
324 }
325 }
326
327 /* Make the set empty, using the new table. */
328 assert(newtable != oldtable);
329 so->table = newtable;
330 so->mask = newsize - 1;
331 memset(newtable, 0, sizeof(setentry) * newsize);
332 so->used = 0;
333 i = so->fill;
334 so->fill = 0;
335
336 /* Copy the data over; this is refcount-neutral for active entries;
337 dummy entries aren't copied over, of course */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000338 for (entry = oldtable; i > 0; entry++) {
339 if (entry->key == NULL) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000340 /* UNUSED */
341 ;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000342 } else if (entry->key == dummy) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000343 /* DUMMY */
344 --i;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000345 assert(entry->key == dummy);
346 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000347 } else {
348 /* ACTIVE */
349 --i;
Raymond Hettinger0c850862006-12-08 04:24:33 +0000350 set_insert_clean(so, entry->key, entry->hash);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000351 }
352 }
353
354 if (is_oldtable_malloced)
355 PyMem_DEL(oldtable);
356 return 0;
357}
358
Raymond Hettingerc991db22005-08-11 07:58:45 +0000359/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
360
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000361static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000362set_add_entry(register PySetObject *so, setentry *entry)
363{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000364 register Py_ssize_t n_used;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000365
366 assert(so->fill <= so->mask); /* at least one empty slot */
367 n_used = so->used;
368 Py_INCREF(entry->key);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000369 if (set_insert_key(so, entry->key, entry->hash) == -1) {
370 Py_DECREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000371 return -1;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +0000372 }
Raymond Hettingerc991db22005-08-11 07:58:45 +0000373 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
374 return 0;
375 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
376}
377
378static int
379set_add_key(register PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000380{
381 register long hash;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000382 register Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000383
Christian Heimes593daf52008-05-26 12:51:38 +0000384 if (!PyBytes_CheckExact(key) ||
385 (hash = ((PyBytesObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386 hash = PyObject_Hash(key);
387 if (hash == -1)
388 return -1;
389 }
390 assert(so->fill <= so->mask); /* at least one empty slot */
391 n_used = so->used;
392 Py_INCREF(key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000393 if (set_insert_key(so, key, hash) == -1) {
394 Py_DECREF(key);
395 return -1;
396 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
398 return 0;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000399 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000400}
401
402#define DISCARD_NOTFOUND 0
403#define DISCARD_FOUND 1
404
405static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000406set_discard_entry(PySetObject *so, setentry *oldentry)
407{ register setentry *entry;
408 PyObject *old_key;
409
410 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000411 if (entry == NULL)
412 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000413 if (entry->key == NULL || entry->key == dummy)
414 return DISCARD_NOTFOUND;
415 old_key = entry->key;
416 Py_INCREF(dummy);
417 entry->key = dummy;
418 so->used--;
419 Py_DECREF(old_key);
420 return DISCARD_FOUND;
421}
422
423static int
424set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000425{
426 register long hash;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000427 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000428 PyObject *old_key;
429
430 assert (PyAnySet_Check(so));
Christian Heimes593daf52008-05-26 12:51:38 +0000431 if (!PyBytes_CheckExact(key) ||
432 (hash = ((PyBytesObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000433 hash = PyObject_Hash(key);
434 if (hash == -1)
435 return -1;
436 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000437 entry = (so->lookup)(so, key, hash);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000438 if (entry == NULL)
439 return -1;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000440 if (entry->key == NULL || entry->key == dummy)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000441 return DISCARD_NOTFOUND;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000442 old_key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443 Py_INCREF(dummy);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000444 entry->key = dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445 so->used--;
446 Py_DECREF(old_key);
447 return DISCARD_FOUND;
448}
449
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000450static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451set_clear_internal(PySetObject *so)
452{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000453 setentry *entry, *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454 int table_is_malloced;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000455 Py_ssize_t fill;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456 setentry small_copy[PySet_MINSIZE];
457#ifdef Py_DEBUG
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000458 Py_ssize_t i, n;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000459 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000460
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000461 n = so->mask + 1;
462 i = 0;
463#endif
464
465 table = so->table;
466 assert(table != NULL);
467 table_is_malloced = table != so->smalltable;
468
469 /* This is delicate. During the process of clearing the set,
470 * decrefs can cause the set to mutate. To avoid fatal confusion
471 * (voice of experience), we have to make the set empty before
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000472 * clearing the slots, and never refer to anything via so->ref while
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000473 * clearing.
474 */
475 fill = so->fill;
476 if (table_is_malloced)
477 EMPTY_TO_MINSIZE(so);
478
479 else if (fill > 0) {
480 /* It's a small table with something that needs to be cleared.
481 * Afraid the only safe way is to copy the set entries into
482 * another small table first.
483 */
484 memcpy(small_copy, table, sizeof(small_copy));
485 table = small_copy;
486 EMPTY_TO_MINSIZE(so);
487 }
488 /* else it's a small table that's already empty */
489
490 /* Now we can finally clear things. If C had refcounts, we could
491 * assert that the refcount on table is 1 now, i.e. that this function
492 * has unique access to it, so decref side-effects can't alter it.
493 */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000494 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000495#ifdef Py_DEBUG
496 assert(i < n);
497 ++i;
498#endif
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000499 if (entry->key) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000500 --fill;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000501 Py_DECREF(entry->key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502 }
503#ifdef Py_DEBUG
504 else
Raymond Hettinger334b5b22006-03-26 03:11:29 +0000505 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000506#endif
507 }
508
509 if (table_is_malloced)
510 PyMem_DEL(table);
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000511 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512}
513
514/*
515 * Iterate over a set table. Use like so:
516 *
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000517 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000518 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000519 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000520 * while (set_next(yourset, &pos, &entry)) {
521 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000522 * }
523 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000524 * CAUTION: In general, it isn't safe to use set_next in a loop that
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000525 * mutates the table.
526 */
527static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000528set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000529{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000530 Py_ssize_t i;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000531 Py_ssize_t mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000532 register setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000533
534 assert (PyAnySet_Check(so));
Raymond Hettingerc991db22005-08-11 07:58:45 +0000535 i = *pos_ptr;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000536 assert(i >= 0);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000537 table = so->table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000538 mask = so->mask;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000539 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540 i++;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000541 *pos_ptr = i+1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000542 if (i > mask)
543 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000544 assert(table[i].key != NULL);
545 *entry_ptr = &table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000546 return 1;
547}
548
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000549static void
550set_dealloc(PySetObject *so)
551{
552 register setentry *entry;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000553 Py_ssize_t fill = so->fill;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000554 PyObject_GC_UnTrack(so);
555 Py_TRASHCAN_SAFE_BEGIN(so)
556 if (so->weakreflist != NULL)
557 PyObject_ClearWeakRefs((PyObject *) so);
558
559 for (entry = so->table; fill > 0; entry++) {
560 if (entry->key) {
561 --fill;
562 Py_DECREF(entry->key);
563 }
564 }
565 if (so->table != so->smalltable)
566 PyMem_DEL(so->table);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000567 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
568 free_list[numfree++] = so;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000569 else
Christian Heimese93237d2007-12-19 02:37:44 +0000570 Py_TYPE(so)->tp_free(so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000571 Py_TRASHCAN_SAFE_END(so)
572}
573
574static int
575set_tp_print(PySetObject *so, FILE *fp, int flags)
576{
577 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000578 Py_ssize_t pos=0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000579 char *emit = ""; /* No separator emitted on first pass */
580 char *separator = ", ";
Raymond Hettinger53999102006-12-30 04:01:17 +0000581 int status = Py_ReprEnter((PyObject*)so);
582
583 if (status != 0) {
584 if (status < 0)
585 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000586 Py_BEGIN_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000587 fprintf(fp, "%s(...)", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000588 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000589 return 0;
590 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591
Brett Cannon01531592007-09-17 03:28:34 +0000592 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000593 fprintf(fp, "%s([", so->ob_type->tp_name);
Brett Cannon01531592007-09-17 03:28:34 +0000594 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000595 while (set_next(so, &pos, &entry)) {
Brett Cannon01531592007-09-17 03:28:34 +0000596 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000597 fputs(emit, fp);
Brett Cannon01531592007-09-17 03:28:34 +0000598 Py_END_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000599 emit = separator;
Raymond Hettinger53999102006-12-30 04:01:17 +0000600 if (PyObject_Print(entry->key, fp, 0) != 0) {
601 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000602 return -1;
Raymond Hettinger53999102006-12-30 04:01:17 +0000603 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000604 }
Brett Cannon01531592007-09-17 03:28:34 +0000605 Py_BEGIN_ALLOW_THREADS
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000606 fputs("])", fp);
Brett Cannon01531592007-09-17 03:28:34 +0000607 Py_END_ALLOW_THREADS
Raymond Hettinger53999102006-12-30 04:01:17 +0000608 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000609 return 0;
610}
611
612static PyObject *
613set_repr(PySetObject *so)
614{
Raymond Hettinger53999102006-12-30 04:01:17 +0000615 PyObject *keys, *result=NULL, *listrepr;
616 int status = Py_ReprEnter((PyObject*)so);
617
618 if (status != 0) {
619 if (status < 0)
620 return NULL;
Christian Heimes593daf52008-05-26 12:51:38 +0000621 return PyBytes_FromFormat("%s(...)", so->ob_type->tp_name);
Raymond Hettinger53999102006-12-30 04:01:17 +0000622 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000623
624 keys = PySequence_List((PyObject *)so);
625 if (keys == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000626 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000627 listrepr = PyObject_Repr(keys);
628 Py_DECREF(keys);
629 if (listrepr == NULL)
Raymond Hettinger53999102006-12-30 04:01:17 +0000630 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000631
Christian Heimes593daf52008-05-26 12:51:38 +0000632 result = PyBytes_FromFormat("%s(%s)", so->ob_type->tp_name,
633 PyBytes_AS_STRING(listrepr));
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000634 Py_DECREF(listrepr);
Raymond Hettinger53999102006-12-30 04:01:17 +0000635done:
636 Py_ReprLeave((PyObject*)so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000637 return result;
638}
639
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000641set_len(PyObject *so)
642{
643 return ((PySetObject *)so)->used;
644}
645
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000646static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000647set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000648{
Raymond Hettingerd7946662005-08-01 21:39:29 +0000649 PySetObject *other;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000650 register Py_ssize_t i;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000651 register setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000652
653 assert (PyAnySet_Check(so));
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000654 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000655
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000656 other = (PySetObject*)otherset;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000657 if (other == so || other->used == 0)
658 /* a.update(a) or a.update({}); nothing to do */
659 return 0;
660 /* Do one big resize at the start, rather than
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000661 * incrementally resizing as we insert new keys. Expect
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000662 * that there will be no (or few) overlapping keys.
663 */
664 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
665 if (set_table_resize(so, (so->used + other->used)*2) != 0)
666 return -1;
667 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000668 for (i = 0; i <= other->mask; i++) {
669 entry = &other->table[i];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000670 if (entry->key != NULL &&
671 entry->key != dummy) {
672 Py_INCREF(entry->key);
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000673 if (set_insert_key(so, entry->key, entry->hash) == -1) {
674 Py_DECREF(entry->key);
675 return -1;
676 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000677 }
678 }
679 return 0;
680}
681
682static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000683set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000684{
685 long hash;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000686 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000687
Christian Heimes593daf52008-05-26 12:51:38 +0000688 if (!PyBytes_CheckExact(key) ||
689 (hash = ((PyBytesObject *) key)->ob_shash) == -1) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000690 hash = PyObject_Hash(key);
691 if (hash == -1)
692 return -1;
693 }
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000694 entry = (so->lookup)(so, key, hash);
695 if (entry == NULL)
696 return -1;
697 key = entry->key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000698 return key != NULL && key != dummy;
699}
700
Raymond Hettingerc991db22005-08-11 07:58:45 +0000701static int
702set_contains_entry(PySetObject *so, setentry *entry)
703{
704 PyObject *key;
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000705 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000706
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000707 lu_entry = (so->lookup)(so, entry->key, entry->hash);
708 if (lu_entry == NULL)
709 return -1;
710 key = lu_entry->key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000711 return key != NULL && key != dummy;
712}
713
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000714static PyObject *
715set_pop(PySetObject *so)
716{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000717 register Py_ssize_t i = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000718 register setentry *entry;
719 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000720
721 assert (PyAnySet_Check(so));
722 if (so->used == 0) {
723 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
724 return NULL;
725 }
726
727 /* Set entry to "the first" unused or dummy set entry. We abuse
728 * the hash field of slot 0 to hold a search finger:
729 * If slot 0 has a value, use slot 0.
730 * Else slot 0 is being used to hold a search finger,
731 * and we use its hash value as the first index to look.
732 */
733 entry = &so->table[0];
734 if (entry->key == NULL || entry->key == dummy) {
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000735 i = entry->hash;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000736 /* The hash field may be a real hash value, or it may be a
737 * legit search finger, or it may be a once-legit search
738 * finger that's out of bounds now because it wrapped around
739 * or the table shrunk -- simply make sure it's in bounds now.
740 */
741 if (i > so->mask || i < 1)
742 i = 1; /* skip slot 0 */
743 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
744 i++;
745 if (i > so->mask)
746 i = 1;
747 }
748 }
749 key = entry->key;
750 Py_INCREF(dummy);
751 entry->key = dummy;
752 so->used--;
753 so->table[0].hash = i + 1; /* next place to start */
754 return key;
755}
756
757PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
758
759static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000760set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000761{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000762 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000763 setentry *entry;
764
765 while (set_next(so, &pos, &entry))
766 Py_VISIT(entry->key);
767 return 0;
768}
769
770static long
771frozenset_hash(PyObject *self)
772{
773 PySetObject *so = (PySetObject *)self;
774 long h, hash = 1927868237L;
775 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000776 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000777
778 if (so->hash != -1)
779 return so->hash;
780
781 hash *= PySet_GET_SIZE(self) + 1;
782 while (set_next(so, &pos, &entry)) {
783 /* Work to increase the bit dispersion for closely spaced hash
784 values. The is important because some use cases have many
785 combinations of a small number of elements with nearby
786 hashes so that many distinct combinations collapse to only
787 a handful of distinct hash values. */
788 h = entry->hash;
789 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
790 }
791 hash = hash * 69069L + 907133923L;
792 if (hash == -1)
793 hash = 590923713L;
794 so->hash = hash;
795 return hash;
796}
797
Raymond Hettingera9d99362005-08-05 00:01:15 +0000798/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000800typedef struct {
801 PyObject_HEAD
802 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000803 Py_ssize_t si_used;
804 Py_ssize_t si_pos;
805 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000806} setiterobject;
807
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000808static void
809setiter_dealloc(setiterobject *si)
810{
811 Py_XDECREF(si->si_set);
812 PyObject_Del(si);
813}
814
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000815static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000816setiter_len(setiterobject *si)
817{
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000818 Py_ssize_t len = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000819 if (si->si_set != NULL && si->si_used == si->si_set->used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000820 len = si->len;
821 return PyInt_FromLong(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822}
823
Armin Rigof5b3e362006-02-11 21:32:43 +0000824PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000825
826static PyMethodDef setiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000827 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000828 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829};
830
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000831static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832{
833 PyObject *key;
Neal Norwitz0f2783c2006-06-19 05:40:44 +0000834 register Py_ssize_t i, mask;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000835 register setentry *entry;
836 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000837
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000838 if (so == NULL)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000839 return NULL;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000840 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000841
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000842 if (si->si_used != so->used) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000843 PyErr_SetString(PyExc_RuntimeError,
844 "Set changed size during iteration");
845 si->si_used = -1; /* Make this state sticky */
846 return NULL;
847 }
848
849 i = si->si_pos;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000850 assert(i>=0);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000851 entry = so->table;
852 mask = so->mask;
853 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000854 i++;
855 si->si_pos = i+1;
856 if (i > mask)
857 goto fail;
858 si->len--;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000859 key = entry[i].key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000860 Py_INCREF(key);
861 return key;
862
863fail:
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000864 Py_DECREF(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000865 si->si_set = NULL;
866 return NULL;
867}
868
Hye-Shik Change2956762005-08-01 05:26:41 +0000869static PyTypeObject PySetIter_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000870 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000871 "setiterator", /* tp_name */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872 sizeof(setiterobject), /* tp_basicsize */
873 0, /* tp_itemsize */
874 /* methods */
875 (destructor)setiter_dealloc, /* tp_dealloc */
876 0, /* tp_print */
877 0, /* tp_getattr */
878 0, /* tp_setattr */
879 0, /* tp_compare */
880 0, /* tp_repr */
881 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000882 0, /* tp_as_sequence */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000883 0, /* tp_as_mapping */
884 0, /* tp_hash */
885 0, /* tp_call */
886 0, /* tp_str */
887 PyObject_GenericGetAttr, /* tp_getattro */
888 0, /* tp_setattro */
889 0, /* tp_as_buffer */
890 Py_TPFLAGS_DEFAULT, /* tp_flags */
891 0, /* tp_doc */
892 0, /* tp_traverse */
893 0, /* tp_clear */
894 0, /* tp_richcompare */
895 0, /* tp_weaklistoffset */
896 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000897 (iternextfunc)setiter_iternext, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000898 setiter_methods, /* tp_methods */
899 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000900};
901
Martin v. Löwis72d20672006-04-11 09:04:12 +0000902static PyObject *
903set_iter(PySetObject *so)
904{
905 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
906 if (si == NULL)
907 return NULL;
908 Py_INCREF(so);
909 si->si_set = so;
910 si->si_used = so->used;
911 si->si_pos = 0;
912 si->len = so->used;
913 return (PyObject *)si;
914}
915
Raymond Hettingerd7946662005-08-01 21:39:29 +0000916static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000917set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000918{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000919 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000920
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +0000921 if (PyAnySet_Check(other))
Raymond Hettingerc991db22005-08-11 07:58:45 +0000922 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000923
Raymond Hettingerdb67aef2007-02-01 21:02:59 +0000924 if (PyDict_CheckExact(other)) {
Neal Norwitz0c6e2f12006-01-08 06:13:44 +0000925 PyObject *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000926 Py_ssize_t pos = 0;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000927 long hash;
Raymond Hettinger15cade02007-02-19 20:44:04 +0000928 Py_ssize_t dictsize = PyDict_Size(other);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000929
Raymond Hettinger15cade02007-02-19 20:44:04 +0000930 /* Do one big resize at the start, rather than
931 * incrementally resizing as we insert new keys. Expect
932 * that there will be no (or few) overlapping keys.
933 */
934 if (dictsize == -1)
935 return -1;
936 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
937 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
938 return -1;
939 }
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000940 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
941 setentry an_entry;
942
943 an_entry.hash = hash;
944 an_entry.key = key;
945 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000946 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000947 }
Raymond Hettingerd7946662005-08-01 21:39:29 +0000948 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000949 }
950
Raymond Hettingera38123e2003-11-24 22:18:49 +0000951 it = PyObject_GetIter(other);
952 if (it == NULL)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000953 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000954
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000955 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerc991db22005-08-11 07:58:45 +0000956 if (set_add_key(so, key) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +0000957 Py_DECREF(it);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000958 Py_DECREF(key);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000959 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000960 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000961 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +0000962 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000963 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000964 if (PyErr_Occurred())
Raymond Hettingerd7946662005-08-01 21:39:29 +0000965 return -1;
966 return 0;
967}
968
969static PyObject *
970set_update(PySetObject *so, PyObject *other)
971{
972 if (set_update_internal(so, other) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +0000973 return NULL;
974 Py_RETURN_NONE;
975}
976
977PyDoc_STRVAR(update_doc,
978"Update a set with the union of itself and another.");
979
980static PyObject *
981make_new_set(PyTypeObject *type, PyObject *iterable)
982{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000983 register PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000984
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000985 if (dummy == NULL) { /* Auto-initialize dummy */
Christian Heimes593daf52008-05-26 12:51:38 +0000986 dummy = PyBytes_FromString("<dummy key>");
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000987 if (dummy == NULL)
988 return NULL;
989 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000990
991 /* create PySetObject structure */
Christian Heimes5b970ad2008-02-06 13:33:44 +0000992 if (numfree &&
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000993 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
Christian Heimes5b970ad2008-02-06 13:33:44 +0000994 so = free_list[--numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000995 assert (so != NULL && PyAnySet_CheckExact(so));
Christian Heimese93237d2007-12-19 02:37:44 +0000996 Py_TYPE(so) = type;
Raymond Hettingerbc841a12005-08-07 13:02:53 +0000997 _Py_NewReference((PyObject *)so);
998 EMPTY_TO_MINSIZE(so);
999 PyObject_GC_Track(so);
1000 } else {
1001 so = (PySetObject *)type->tp_alloc(type, 0);
1002 if (so == NULL)
1003 return NULL;
1004 /* tp_alloc has already zeroed the structure */
1005 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1006 INIT_NONZERO_SET_SLOTS(so);
1007 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001008
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001009 so->lookup = set_lookkey_string;
Raymond Hettinger691d8052004-05-30 07:26:47 +00001010 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001011
Raymond Hettingera38123e2003-11-24 22:18:49 +00001012 if (iterable != NULL) {
Raymond Hettingerd7946662005-08-01 21:39:29 +00001013 if (set_update_internal(so, iterable) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +00001014 Py_DECREF(so);
1015 return NULL;
1016 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001017 }
1018
Raymond Hettingera690a992003-11-16 16:17:49 +00001019 return (PyObject *)so;
1020}
1021
Raymond Hettingerd7946662005-08-01 21:39:29 +00001022/* The empty frozenset is a singleton */
1023static PyObject *emptyfrozenset = NULL;
1024
Raymond Hettingera690a992003-11-16 16:17:49 +00001025static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001026frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001027{
Raymond Hettingerd7946662005-08-01 21:39:29 +00001028 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001029
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001030 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001031 return NULL;
1032
Raymond Hettingera690a992003-11-16 16:17:49 +00001033 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1034 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001035
1036 if (type != &PyFrozenSet_Type)
1037 return make_new_set(type, iterable);
1038
1039 if (iterable != NULL) {
1040 /* frozenset(f) is idempotent */
1041 if (PyFrozenSet_CheckExact(iterable)) {
1042 Py_INCREF(iterable);
1043 return iterable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001044 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001045 result = make_new_set(type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001046 if (result == NULL || PySet_GET_SIZE(result))
Raymond Hettingerd7946662005-08-01 21:39:29 +00001047 return result;
1048 Py_DECREF(result);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001049 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001050 /* The empty frozenset is a singleton */
1051 if (emptyfrozenset == NULL)
1052 emptyfrozenset = make_new_set(type, NULL);
1053 Py_XINCREF(emptyfrozenset);
1054 return emptyfrozenset;
1055}
1056
1057void
1058PySet_Fini(void)
1059{
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001060 PySetObject *so;
1061
Christian Heimes5b970ad2008-02-06 13:33:44 +00001062 while (numfree) {
1063 numfree--;
1064 so = free_list[numfree];
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001065 PyObject_GC_Del(so);
1066 }
Martin v. Löwised8f7832006-04-15 12:47:23 +00001067 Py_CLEAR(dummy);
1068 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001069}
1070
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001071static PyObject *
1072set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1073{
Raymond Hettinger9fdfadb2007-01-11 18:22:55 +00001074 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001075 return NULL;
1076
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001077 return make_new_set(type, NULL);
1078}
1079
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001080/* set_swap_bodies() switches the contents of any two sets by moving their
1081 internal data pointers and, if needed, copying the internal smalltables.
1082 Semantically equivalent to:
1083
1084 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1085
1086 The function always succeeds and it leaves both objects in a stable state.
1087 Useful for creating temporary frozensets from sets for membership testing
1088 in __contains__(), discard(), and remove(). Also useful for operations
1089 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001090 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001091*/
1092
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001093static void
1094set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001095{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00001096 Py_ssize_t t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001097 setentry *u;
1098 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1099 setentry tab[PySet_MINSIZE];
1100 long h;
1101
1102 t = a->fill; a->fill = b->fill; b->fill = t;
1103 t = a->used; a->used = b->used; b->used = t;
1104 t = a->mask; a->mask = b->mask; b->mask = t;
1105
1106 u = a->table;
1107 if (a->table == a->smalltable)
1108 u = b->smalltable;
1109 a->table = b->table;
1110 if (b->table == b->smalltable)
1111 a->table = a->smalltable;
1112 b->table = u;
1113
1114 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1115
1116 if (a->table == a->smalltable || b->table == b->smalltable) {
1117 memcpy(tab, a->smalltable, sizeof(tab));
1118 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1119 memcpy(b->smalltable, tab, sizeof(tab));
1120 }
1121
Christian Heimese93237d2007-12-19 02:37:44 +00001122 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1123 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
Raymond Hettingera580c472005-08-05 17:19:54 +00001124 h = a->hash; a->hash = b->hash; b->hash = h;
1125 } else {
1126 a->hash = -1;
1127 b->hash = -1;
1128 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001129}
1130
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001131static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001132set_copy(PySetObject *so)
1133{
Christian Heimese93237d2007-12-19 02:37:44 +00001134 return make_new_set(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001135}
1136
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001137static PyObject *
1138frozenset_copy(PySetObject *so)
1139{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001140 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001141 Py_INCREF(so);
1142 return (PyObject *)so;
1143 }
1144 return set_copy(so);
1145}
1146
Raymond Hettingera690a992003-11-16 16:17:49 +00001147PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1148
1149static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001150set_clear(PySetObject *so)
1151{
1152 set_clear_internal(so);
1153 Py_RETURN_NONE;
1154}
1155
1156PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1157
1158static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001159set_union(PySetObject *so, PyObject *other)
1160{
1161 PySetObject *result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001162
1163 result = (PySetObject *)set_copy(so);
1164 if (result == NULL)
1165 return NULL;
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001166 if ((PyObject *)so == other)
1167 return (PyObject *)result;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001168 if (set_update_internal(result, other) == -1) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001169 Py_DECREF(result);
1170 return NULL;
1171 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001172 return (PyObject *)result;
1173}
1174
1175PyDoc_STRVAR(union_doc,
1176 "Return the union of two sets as a new set.\n\
1177\n\
1178(i.e. all elements that are in either set.)");
1179
1180static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001181set_or(PySetObject *so, PyObject *other)
1182{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001183 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001184 Py_INCREF(Py_NotImplemented);
1185 return Py_NotImplemented;
1186 }
1187 return set_union(so, other);
1188}
1189
1190static PyObject *
1191set_ior(PySetObject *so, PyObject *other)
1192{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001193 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001194 Py_INCREF(Py_NotImplemented);
1195 return Py_NotImplemented;
1196 }
Raymond Hettingerd7946662005-08-01 21:39:29 +00001197 if (set_update_internal(so, other) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001198 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001199 Py_INCREF(so);
1200 return (PyObject *)so;
1201}
1202
1203static PyObject *
1204set_intersection(PySetObject *so, PyObject *other)
1205{
1206 PySetObject *result;
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001207 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001208
Raymond Hettingerd8e13382005-08-17 12:27:17 +00001209 if ((PyObject *)so == other)
1210 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001211
Christian Heimese93237d2007-12-19 02:37:44 +00001212 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +00001213 if (result == NULL)
1214 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001215
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001216 if (PyAnySet_Check(other)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001217 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001218 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001219
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001220 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001221 tmp = (PyObject *)so;
1222 so = (PySetObject *)other;
1223 other = tmp;
1224 }
1225
Raymond Hettingerc991db22005-08-11 07:58:45 +00001226 while (set_next((PySetObject *)other, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001227 int rv = set_contains_entry(so, entry);
1228 if (rv == -1) {
1229 Py_DECREF(result);
1230 return NULL;
1231 }
1232 if (rv) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001233 if (set_add_entry(result, entry) == -1) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001234 Py_DECREF(result);
1235 return NULL;
1236 }
1237 }
1238 }
1239 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001240 }
1241
Raymond Hettingera690a992003-11-16 16:17:49 +00001242 it = PyObject_GetIter(other);
1243 if (it == NULL) {
1244 Py_DECREF(result);
1245 return NULL;
1246 }
1247
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001248 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001249 int rv;
1250 setentry entry;
1251 long hash = PyObject_Hash(key);
1252
1253 if (hash == -1) {
1254 Py_DECREF(it);
1255 Py_DECREF(result);
1256 Py_DECREF(key);
1257 return NULL;
1258 }
1259 entry.hash = hash;
1260 entry.key = key;
1261 rv = set_contains_entry(so, &entry);
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001262 if (rv == -1) {
1263 Py_DECREF(it);
1264 Py_DECREF(result);
1265 Py_DECREF(key);
1266 return NULL;
1267 }
1268 if (rv) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001269 if (set_add_entry(result, &entry) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001270 Py_DECREF(it);
1271 Py_DECREF(result);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001272 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001273 return NULL;
1274 }
1275 }
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001276 Py_DECREF(key);
Raymond Hettingera690a992003-11-16 16:17:49 +00001277 }
1278 Py_DECREF(it);
1279 if (PyErr_Occurred()) {
1280 Py_DECREF(result);
1281 return NULL;
1282 }
1283 return (PyObject *)result;
1284}
1285
1286PyDoc_STRVAR(intersection_doc,
1287"Return the intersection of two sets as a new set.\n\
1288\n\
1289(i.e. all elements that are in both sets.)");
1290
1291static PyObject *
1292set_intersection_update(PySetObject *so, PyObject *other)
1293{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001294 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001295
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001296 tmp = set_intersection(so, other);
1297 if (tmp == NULL)
Raymond Hettingera690a992003-11-16 16:17:49 +00001298 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001299 set_swap_bodies(so, (PySetObject *)tmp);
Raymond Hettingera690a992003-11-16 16:17:49 +00001300 Py_DECREF(tmp);
1301 Py_RETURN_NONE;
1302}
1303
1304PyDoc_STRVAR(intersection_update_doc,
1305"Update a set with the intersection of itself and another.");
1306
1307static PyObject *
1308set_and(PySetObject *so, PyObject *other)
1309{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001310 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001311 Py_INCREF(Py_NotImplemented);
1312 return Py_NotImplemented;
1313 }
1314 return set_intersection(so, other);
1315}
1316
1317static PyObject *
1318set_iand(PySetObject *so, PyObject *other)
1319{
1320 PyObject *result;
1321
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001322 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001323 Py_INCREF(Py_NotImplemented);
1324 return Py_NotImplemented;
1325 }
1326 result = set_intersection_update(so, other);
1327 if (result == NULL)
1328 return NULL;
1329 Py_DECREF(result);
1330 Py_INCREF(so);
1331 return (PyObject *)so;
1332}
1333
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001334static PyObject *
1335set_isdisjoint(PySetObject *so, PyObject *other)
1336{
1337 PyObject *key, *it, *tmp;
1338
1339 if ((PyObject *)so == other) {
1340 if (PySet_GET_SIZE(so) == 0)
1341 Py_RETURN_TRUE;
1342 else
1343 Py_RETURN_FALSE;
1344 }
1345
1346 if (PyAnySet_CheckExact(other)) {
1347 Py_ssize_t pos = 0;
1348 setentry *entry;
1349
1350 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1351 tmp = (PyObject *)so;
1352 so = (PySetObject *)other;
1353 other = tmp;
1354 }
1355 while (set_next((PySetObject *)other, &pos, &entry)) {
1356 int rv = set_contains_entry(so, entry);
1357 if (rv == -1)
1358 return NULL;
1359 if (rv)
1360 Py_RETURN_FALSE;
1361 }
1362 Py_RETURN_TRUE;
1363 }
1364
1365 it = PyObject_GetIter(other);
1366 if (it == NULL)
1367 return NULL;
1368
1369 while ((key = PyIter_Next(it)) != NULL) {
1370 int rv;
1371 setentry entry;
1372 long hash = PyObject_Hash(key);
1373
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001374 if (hash == -1) {
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001375 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001376 Py_DECREF(it);
1377 return NULL;
1378 }
1379 entry.hash = hash;
1380 entry.key = key;
1381 rv = set_contains_entry(so, &entry);
Raymond Hettingere8d58ba2007-11-08 18:47:51 +00001382 Py_DECREF(key);
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001383 if (rv == -1) {
1384 Py_DECREF(it);
1385 return NULL;
1386 }
1387 if (rv) {
1388 Py_DECREF(it);
1389 Py_RETURN_FALSE;
1390 }
1391 }
1392 Py_DECREF(it);
1393 if (PyErr_Occurred())
1394 return NULL;
1395 Py_RETURN_TRUE;
1396}
1397
1398PyDoc_STRVAR(isdisjoint_doc,
1399"Return True if two sets have a null intersection.");
1400
Neal Norwitz6576bd82005-11-13 18:41:28 +00001401static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001402set_difference_update_internal(PySetObject *so, PyObject *other)
1403{
1404 if ((PyObject *)so == other)
1405 return set_clear_internal(so);
1406
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001407 if (PyAnySet_Check(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001408 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001409 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001410
1411 while (set_next((PySetObject *)other, &pos, &entry))
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001412 if (set_discard_entry(so, entry) == -1)
1413 return -1;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001414 } else {
1415 PyObject *key, *it;
1416 it = PyObject_GetIter(other);
1417 if (it == NULL)
1418 return -1;
1419
1420 while ((key = PyIter_Next(it)) != NULL) {
1421 if (set_discard_key(so, key) == -1) {
1422 Py_DECREF(it);
1423 Py_DECREF(key);
1424 return -1;
1425 }
1426 Py_DECREF(key);
1427 }
1428 Py_DECREF(it);
1429 if (PyErr_Occurred())
1430 return -1;
1431 }
1432 /* If more than 1/5 are dummies, then resize them away. */
1433 if ((so->fill - so->used) * 5 < so->mask)
1434 return 0;
1435 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1436}
1437
Raymond Hettingera690a992003-11-16 16:17:49 +00001438static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001439set_difference_update(PySetObject *so, PyObject *other)
1440{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001441 if (set_difference_update_internal(so, other) != -1)
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001442 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001443 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001444}
1445
1446PyDoc_STRVAR(difference_update_doc,
1447"Remove all elements of another set from this set.");
1448
1449static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001450set_difference(PySetObject *so, PyObject *other)
1451{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001452 PyObject *result;
1453 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001454 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001455
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001456 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001457 result = set_copy(so);
1458 if (result == NULL)
Raymond Hettingerc991db22005-08-11 07:58:45 +00001459 return NULL;
1460 if (set_difference_update_internal((PySetObject *)result, other) != -1)
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001461 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001462 Py_DECREF(result);
1463 return NULL;
1464 }
1465
Christian Heimese93237d2007-12-19 02:37:44 +00001466 result = make_new_set(Py_TYPE(so), NULL);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001467 if (result == NULL)
1468 return NULL;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001469
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001470 if (PyDict_CheckExact(other)) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001471 while (set_next(so, &pos, &entry)) {
1472 setentry entrycopy;
1473 entrycopy.hash = entry->hash;
1474 entrycopy.key = entry->key;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001475 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001476 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1477 Py_DECREF(result);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001478 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001479 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001480 }
1481 }
1482 return result;
1483 }
1484
Raymond Hettingerc991db22005-08-11 07:58:45 +00001485 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001486 int rv = set_contains_entry((PySetObject *)other, entry);
1487 if (rv == -1) {
1488 Py_DECREF(result);
1489 return NULL;
1490 }
1491 if (!rv) {
1492 if (set_add_entry((PySetObject *)result, entry) == -1) {
1493 Py_DECREF(result);
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001494 return NULL;
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001495 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001496 }
1497 }
1498 return result;
1499}
1500
1501PyDoc_STRVAR(difference_doc,
1502"Return the difference of two sets as a new set.\n\
1503\n\
1504(i.e. all elements that are in this set but not the other.)");
1505static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001506set_sub(PySetObject *so, PyObject *other)
1507{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001508 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001509 Py_INCREF(Py_NotImplemented);
1510 return Py_NotImplemented;
1511 }
1512 return set_difference(so, other);
1513}
1514
1515static PyObject *
1516set_isub(PySetObject *so, PyObject *other)
1517{
1518 PyObject *result;
1519
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001520 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001521 Py_INCREF(Py_NotImplemented);
1522 return Py_NotImplemented;
1523 }
1524 result = set_difference_update(so, other);
1525 if (result == NULL)
1526 return NULL;
1527 Py_DECREF(result);
1528 Py_INCREF(so);
1529 return (PyObject *)so;
1530}
1531
1532static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001533set_symmetric_difference_update(PySetObject *so, PyObject *other)
1534{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001535 PySetObject *otherset;
1536 PyObject *key;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001537 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001538 setentry *entry;
1539
1540 if ((PyObject *)so == other)
1541 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001542
Raymond Hettingerdb67aef2007-02-01 21:02:59 +00001543 if (PyDict_CheckExact(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001544 PyObject *value;
1545 int rv;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00001546 long hash;
1547 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001548 setentry an_entry;
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001549
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001550 an_entry.hash = hash;
1551 an_entry.key = key;
1552 rv = set_discard_entry(so, &an_entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001553 if (rv == -1)
1554 return NULL;
1555 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerf31e1752006-12-08 03:17:18 +00001556 if (set_add_entry(so, &an_entry) == -1)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001557 return NULL;
1558 }
1559 }
1560 Py_RETURN_NONE;
1561 }
1562
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001563 if (PyAnySet_Check(other)) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001564 Py_INCREF(other);
1565 otherset = (PySetObject *)other;
1566 } else {
Christian Heimese93237d2007-12-19 02:37:44 +00001567 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001568 if (otherset == NULL)
1569 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001570 }
1571
Raymond Hettingerc991db22005-08-11 07:58:45 +00001572 while (set_next(otherset, &pos, &entry)) {
1573 int rv = set_discard_entry(so, entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001574 if (rv == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001575 Py_DECREF(otherset);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001576 return NULL;
1577 }
1578 if (rv == DISCARD_NOTFOUND) {
Raymond Hettingerc991db22005-08-11 07:58:45 +00001579 if (set_add_entry(so, entry) == -1) {
Neal Norwitz04e39ec2006-07-17 00:57:15 +00001580 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001581 return NULL;
1582 }
1583 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001584 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001585 Py_DECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001586 Py_RETURN_NONE;
1587}
1588
1589PyDoc_STRVAR(symmetric_difference_update_doc,
1590"Update a set with the symmetric difference of itself and another.");
1591
1592static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001593set_symmetric_difference(PySetObject *so, PyObject *other)
1594{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001595 PyObject *rv;
1596 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001597
Christian Heimese93237d2007-12-19 02:37:44 +00001598 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001599 if (otherset == NULL)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001600 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001601 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1602 if (rv == NULL)
1603 return NULL;
1604 Py_DECREF(rv);
1605 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001606}
1607
1608PyDoc_STRVAR(symmetric_difference_doc,
1609"Return the symmetric difference of two sets as a new set.\n\
1610\n\
1611(i.e. all elements that are in exactly one of the sets.)");
1612
1613static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001614set_xor(PySetObject *so, PyObject *other)
1615{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001616 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001617 Py_INCREF(Py_NotImplemented);
1618 return Py_NotImplemented;
1619 }
1620 return set_symmetric_difference(so, other);
1621}
1622
1623static PyObject *
1624set_ixor(PySetObject *so, PyObject *other)
1625{
1626 PyObject *result;
1627
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001628 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +00001629 Py_INCREF(Py_NotImplemented);
1630 return Py_NotImplemented;
1631 }
1632 result = set_symmetric_difference_update(so, other);
1633 if (result == NULL)
1634 return NULL;
1635 Py_DECREF(result);
1636 Py_INCREF(so);
1637 return (PyObject *)so;
1638}
1639
1640static PyObject *
1641set_issubset(PySetObject *so, PyObject *other)
1642{
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001643 setentry *entry;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001644 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001645
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001646 if (!PyAnySet_Check(other)) {
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001647 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001648 tmp = make_new_set(&PySet_Type, other);
1649 if (tmp == NULL)
1650 return NULL;
1651 result = set_issubset(so, tmp);
1652 Py_DECREF(tmp);
1653 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001654 }
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001655 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
Raymond Hettingera690a992003-11-16 16:17:49 +00001656 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001657
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001658 while (set_next(so, &pos, &entry)) {
Raymond Hettingerc563a1c2006-09-07 02:42:48 +00001659 int rv = set_contains_entry((PySetObject *)other, entry);
1660 if (rv == -1)
1661 return NULL;
1662 if (!rv)
Raymond Hettingera690a992003-11-16 16:17:49 +00001663 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001664 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001665 Py_RETURN_TRUE;
1666}
1667
1668PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1669
1670static PyObject *
1671set_issuperset(PySetObject *so, PyObject *other)
1672{
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001673 PyObject *tmp, *result;
1674
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001675 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001676 tmp = make_new_set(&PySet_Type, other);
1677 if (tmp == NULL)
1678 return NULL;
1679 result = set_issuperset(so, tmp);
1680 Py_DECREF(tmp);
1681 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001682 }
1683 return set_issubset((PySetObject *)other, (PyObject *)so);
1684}
1685
1686PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1687
Raymond Hettingera690a992003-11-16 16:17:49 +00001688static PyObject *
1689set_richcompare(PySetObject *v, PyObject *w, int op)
1690{
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001691 PyObject *r1, *r2;
1692
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001693 if(!PyAnySet_Check(w)) {
1694 if (op == Py_EQ)
1695 Py_RETURN_FALSE;
1696 if (op == Py_NE)
1697 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001698 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1699 return NULL;
1700 }
1701 switch (op) {
1702 case Py_EQ:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001703 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
Raymond Hettingera690a992003-11-16 16:17:49 +00001704 Py_RETURN_FALSE;
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001705 if (v->hash != -1 &&
1706 ((PySetObject *)w)->hash != -1 &&
1707 v->hash != ((PySetObject *)w)->hash)
1708 Py_RETURN_FALSE;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001709 return set_issubset(v, w);
1710 case Py_NE:
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00001711 r1 = set_richcompare(v, w, Py_EQ);
1712 if (r1 == NULL)
1713 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001714 r2 = PyBool_FromLong(PyObject_Not(r1));
1715 Py_DECREF(r1);
1716 return r2;
1717 case Py_LE:
1718 return set_issubset(v, w);
1719 case Py_GE:
1720 return set_issuperset(v, w);
1721 case Py_LT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001722 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001723 Py_RETURN_FALSE;
1724 return set_issubset(v, w);
1725 case Py_GT:
Raymond Hettingerbeb31012005-08-16 03:47:52 +00001726 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001727 Py_RETURN_FALSE;
1728 return set_issuperset(v, w);
Raymond Hettingera690a992003-11-16 16:17:49 +00001729 }
1730 Py_INCREF(Py_NotImplemented);
1731 return Py_NotImplemented;
1732}
1733
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001734static int
Georg Brandl347b3002006-03-30 11:57:00 +00001735set_nocmp(PyObject *self, PyObject *other)
Raymond Hettingered6c1ef2005-08-13 08:28:03 +00001736{
1737 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1738 return -1;
1739}
1740
Raymond Hettingera690a992003-11-16 16:17:49 +00001741static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001742set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001743{
Raymond Hettingerc991db22005-08-11 07:58:45 +00001744 if (set_add_key(so, key) == -1)
Raymond Hettingera690a992003-11-16 16:17:49 +00001745 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001746 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001747}
1748
1749PyDoc_STRVAR(add_doc,
1750"Add an element to a set.\n\
1751\n\
1752This has no effect if the element is already present.");
1753
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001754static int
1755set_contains(PySetObject *so, PyObject *key)
1756{
1757 PyObject *tmpkey;
1758 int rv;
1759
1760 rv = set_contains_key(so, key);
1761 if (rv == -1) {
Raymond Hettingerc5a1cc52008-05-08 04:35:20 +00001762 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001763 return -1;
1764 PyErr_Clear();
1765 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1766 if (tmpkey == NULL)
1767 return -1;
1768 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1769 rv = set_contains(so, tmpkey);
1770 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1771 Py_DECREF(tmpkey);
1772 }
1773 return rv;
1774}
1775
1776static PyObject *
1777set_direct_contains(PySetObject *so, PyObject *key)
1778{
1779 long result;
1780
1781 result = set_contains(so, key);
1782 if (result == -1)
1783 return NULL;
1784 return PyBool_FromLong(result);
1785}
1786
1787PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1788
Raymond Hettingera690a992003-11-16 16:17:49 +00001789static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001790set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001791{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001792 PyObject *tmpkey, *result;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001793 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001794
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001795 rv = set_discard_key(so, key);
1796 if (rv == -1) {
Raymond Hettingerc5a1cc52008-05-08 04:35:20 +00001797 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001798 return NULL;
1799 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001800 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1801 if (tmpkey == NULL)
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001802 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001803 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001804 result = set_remove(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001805 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001806 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001807 return result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001808 } else if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger9c14ffb2006-12-08 04:57:50 +00001809 set_key_error(key);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001810 return NULL;
1811 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001812 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001813}
1814
1815PyDoc_STRVAR(remove_doc,
1816"Remove an element from a set; it must be a member.\n\
1817\n\
1818If the element is not a member, raise a KeyError.");
1819
1820static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001821set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001822{
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001823 PyObject *tmpkey, *result;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001824 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001825
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001826 rv = set_discard_key(so, key);
1827 if (rv == -1) {
Raymond Hettingerc5a1cc52008-05-08 04:35:20 +00001828 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001829 return NULL;
1830 PyErr_Clear();
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001831 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1832 if (tmpkey == NULL)
Raymond Hettinger0deab622003-12-13 18:53:18 +00001833 return NULL;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001834 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001835 result = set_discard(so, tmpkey);
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001836 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001837 Py_DECREF(tmpkey);
Raymond Hettinger0deab622003-12-13 18:53:18 +00001838 return result;
1839 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +00001840 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001841}
1842
1843PyDoc_STRVAR(discard_doc,
1844"Remove an element from a set if it is a member.\n\
1845\n\
1846If the element is not a member, do nothing.");
1847
1848static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001849set_reduce(PySetObject *so)
1850{
Raymond Hettinger15056a52004-11-09 07:25:31 +00001851 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001852
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001853 keys = PySequence_List((PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001854 if (keys == NULL)
1855 goto done;
1856 args = PyTuple_Pack(1, keys);
1857 if (args == NULL)
1858 goto done;
Raymond Hettinger15056a52004-11-09 07:25:31 +00001859 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1860 if (dict == NULL) {
1861 PyErr_Clear();
1862 dict = Py_None;
1863 Py_INCREF(dict);
1864 }
Christian Heimese93237d2007-12-19 02:37:44 +00001865 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001866done:
1867 Py_XDECREF(args);
1868 Py_XDECREF(keys);
Raymond Hettinger15056a52004-11-09 07:25:31 +00001869 Py_XDECREF(dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001870 return result;
1871}
1872
1873PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1874
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001875static int
1876set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1877{
1878 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001879
1880 if (!PyAnySet_Check(self))
1881 return -1;
Christian Heimese93237d2007-12-19 02:37:44 +00001882 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001883 return -1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001884 set_clear_internal(self);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001885 self->hash = -1;
1886 if (iterable == NULL)
1887 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001888 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001889}
1890
Raymond Hettingera690a992003-11-16 16:17:49 +00001891static PySequenceMethods set_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001892 set_len, /* sq_length */
Raymond Hettingera690a992003-11-16 16:17:49 +00001893 0, /* sq_concat */
1894 0, /* sq_repeat */
1895 0, /* sq_item */
1896 0, /* sq_slice */
1897 0, /* sq_ass_item */
1898 0, /* sq_ass_slice */
1899 (objobjproc)set_contains, /* sq_contains */
1900};
1901
1902/* set object ********************************************************/
1903
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001904#ifdef Py_DEBUG
1905static PyObject *test_c_api(PySetObject *so);
1906
1907PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1908All is well if assertions don't fail.");
1909#endif
1910
Raymond Hettingera690a992003-11-16 16:17:49 +00001911static PyMethodDef set_methods[] = {
1912 {"add", (PyCFunction)set_add, METH_O,
1913 add_doc},
1914 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1915 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +00001916 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001917 contains_doc},
Raymond Hettingera37430a2008-02-12 19:05:36 +00001918 {"copy", (PyCFunction)set_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001919 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001920 {"discard", (PyCFunction)set_discard, METH_O,
1921 discard_doc},
1922 {"difference", (PyCFunction)set_difference, METH_O,
1923 difference_doc},
1924 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1925 difference_update_doc},
1926 {"intersection",(PyCFunction)set_intersection, METH_O,
1927 intersection_doc},
1928 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1929 intersection_update_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00001930 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1931 isdisjoint_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001932 {"issubset", (PyCFunction)set_issubset, METH_O,
1933 issubset_doc},
1934 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1935 issuperset_doc},
1936 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1937 pop_doc},
1938 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1939 reduce_doc},
1940 {"remove", (PyCFunction)set_remove, METH_O,
1941 remove_doc},
1942 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1943 symmetric_difference_doc},
1944 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1945 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001946#ifdef Py_DEBUG
1947 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1948 test_c_api_doc},
1949#endif
Raymond Hettingera690a992003-11-16 16:17:49 +00001950 {"union", (PyCFunction)set_union, METH_O,
1951 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001952 {"update", (PyCFunction)set_update, METH_O,
1953 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001954 {NULL, NULL} /* sentinel */
1955};
1956
1957static PyNumberMethods set_as_number = {
1958 0, /*nb_add*/
1959 (binaryfunc)set_sub, /*nb_subtract*/
1960 0, /*nb_multiply*/
1961 0, /*nb_divide*/
1962 0, /*nb_remainder*/
1963 0, /*nb_divmod*/
1964 0, /*nb_power*/
1965 0, /*nb_negative*/
1966 0, /*nb_positive*/
1967 0, /*nb_absolute*/
1968 0, /*nb_nonzero*/
1969 0, /*nb_invert*/
1970 0, /*nb_lshift*/
1971 0, /*nb_rshift*/
1972 (binaryfunc)set_and, /*nb_and*/
1973 (binaryfunc)set_xor, /*nb_xor*/
1974 (binaryfunc)set_or, /*nb_or*/
1975 0, /*nb_coerce*/
1976 0, /*nb_int*/
1977 0, /*nb_long*/
1978 0, /*nb_float*/
1979 0, /*nb_oct*/
1980 0, /*nb_hex*/
1981 0, /*nb_inplace_add*/
1982 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1983 0, /*nb_inplace_multiply*/
1984 0, /*nb_inplace_divide*/
1985 0, /*nb_inplace_remainder*/
1986 0, /*nb_inplace_power*/
1987 0, /*nb_inplace_lshift*/
1988 0, /*nb_inplace_rshift*/
1989 (binaryfunc)set_iand, /*nb_inplace_and*/
1990 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1991 (binaryfunc)set_ior, /*nb_inplace_or*/
1992};
1993
1994PyDoc_STRVAR(set_doc,
1995"set(iterable) --> set object\n\
1996\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00001997Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001998
1999PyTypeObject PySet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002000 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002001 "set", /* tp_name */
2002 sizeof(PySetObject), /* tp_basicsize */
2003 0, /* tp_itemsize */
2004 /* methods */
2005 (destructor)set_dealloc, /* tp_dealloc */
2006 (printfunc)set_tp_print, /* tp_print */
2007 0, /* tp_getattr */
2008 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002009 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002010 (reprfunc)set_repr, /* tp_repr */
2011 &set_as_number, /* tp_as_number */
2012 &set_as_sequence, /* tp_as_sequence */
2013 0, /* tp_as_mapping */
Guido van Rossum64c06e32007-11-22 00:55:51 +00002014 0, /* tp_hash */
Raymond Hettingera690a992003-11-16 16:17:49 +00002015 0, /* tp_call */
2016 0, /* tp_str */
2017 PyObject_GenericGetAttr, /* tp_getattro */
2018 0, /* tp_setattro */
2019 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002020 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002021 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002022 set_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002023 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002024 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002025 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002026 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002027 (getiterfunc)set_iter, /* tp_iter */
Raymond Hettingera690a992003-11-16 16:17:49 +00002028 0, /* tp_iternext */
2029 set_methods, /* tp_methods */
2030 0, /* tp_members */
2031 0, /* tp_getset */
2032 0, /* tp_base */
2033 0, /* tp_dict */
2034 0, /* tp_descr_get */
2035 0, /* tp_descr_set */
2036 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002037 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00002038 PyType_GenericAlloc, /* tp_alloc */
2039 set_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002040 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002041};
2042
2043/* frozenset object ********************************************************/
2044
2045
2046static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00002047 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002048 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002049 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00002050 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002051 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002052 difference_doc},
2053 {"intersection",(PyCFunction)set_intersection, METH_O,
2054 intersection_doc},
Raymond Hettinger1760c8a2007-11-08 02:52:43 +00002055 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2056 isdisjoint_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002057 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002058 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00002059 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00002060 issuperset_doc},
2061 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2062 reduce_doc},
2063 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2064 symmetric_difference_doc},
2065 {"union", (PyCFunction)set_union, METH_O,
2066 union_doc},
2067 {NULL, NULL} /* sentinel */
2068};
2069
2070static PyNumberMethods frozenset_as_number = {
2071 0, /*nb_add*/
2072 (binaryfunc)set_sub, /*nb_subtract*/
2073 0, /*nb_multiply*/
2074 0, /*nb_divide*/
2075 0, /*nb_remainder*/
2076 0, /*nb_divmod*/
2077 0, /*nb_power*/
2078 0, /*nb_negative*/
2079 0, /*nb_positive*/
2080 0, /*nb_absolute*/
2081 0, /*nb_nonzero*/
2082 0, /*nb_invert*/
2083 0, /*nb_lshift*/
2084 0, /*nb_rshift*/
2085 (binaryfunc)set_and, /*nb_and*/
2086 (binaryfunc)set_xor, /*nb_xor*/
2087 (binaryfunc)set_or, /*nb_or*/
2088};
2089
2090PyDoc_STRVAR(frozenset_doc,
2091"frozenset(iterable) --> frozenset object\n\
2092\n\
Andrew M. Kuchling52740be2006-07-29 15:10:32 +00002093Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002094
2095PyTypeObject PyFrozenSet_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002096 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettingera690a992003-11-16 16:17:49 +00002097 "frozenset", /* tp_name */
2098 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00002099 0, /* tp_itemsize */
2100 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00002101 (destructor)set_dealloc, /* tp_dealloc */
2102 (printfunc)set_tp_print, /* tp_print */
2103 0, /* tp_getattr */
2104 0, /* tp_setattr */
Georg Brandl347b3002006-03-30 11:57:00 +00002105 set_nocmp, /* tp_compare */
Raymond Hettingera690a992003-11-16 16:17:49 +00002106 (reprfunc)set_repr, /* tp_repr */
2107 &frozenset_as_number, /* tp_as_number */
2108 &set_as_sequence, /* tp_as_sequence */
2109 0, /* tp_as_mapping */
2110 frozenset_hash, /* tp_hash */
2111 0, /* tp_call */
2112 0, /* tp_str */
2113 PyObject_GenericGetAttr, /* tp_getattro */
2114 0, /* tp_setattro */
2115 0, /* tp_as_buffer */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002116 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00002117 Py_TPFLAGS_BASETYPE, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00002118 frozenset_doc, /* tp_doc */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002119 (traverseproc)set_traverse, /* tp_traverse */
Raymond Hettingerfe889f32005-08-06 05:43:39 +00002120 (inquiry)set_clear_internal, /* tp_clear */
Raymond Hettingera690a992003-11-16 16:17:49 +00002121 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00002122 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00002123 (getiterfunc)set_iter, /* tp_iter */
2124 0, /* tp_iternext */
2125 frozenset_methods, /* tp_methods */
2126 0, /* tp_members */
2127 0, /* tp_getset */
2128 0, /* tp_base */
2129 0, /* tp_dict */
2130 0, /* tp_descr_get */
2131 0, /* tp_descr_set */
2132 0, /* tp_dictoffset */
2133 0, /* tp_init */
2134 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002135 frozenset_new, /* tp_new */
Raymond Hettingerbb999b52005-06-18 21:00:26 +00002136 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002137};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002138
2139
2140/***** C API functions *************************************************/
2141
2142PyObject *
2143PySet_New(PyObject *iterable)
2144{
2145 return make_new_set(&PySet_Type, iterable);
2146}
2147
2148PyObject *
2149PyFrozenSet_New(PyObject *iterable)
2150{
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002151 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002152}
2153
Neal Norwitz8c49c822006-03-04 18:41:19 +00002154Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002155PySet_Size(PyObject *anyset)
2156{
2157 if (!PyAnySet_Check(anyset)) {
2158 PyErr_BadInternalCall();
2159 return -1;
2160 }
Raymond Hettinger9c1491f2005-08-24 00:24:40 +00002161 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002162}
2163
2164int
Barry Warsaw176014f2006-03-30 22:45:35 +00002165PySet_Clear(PyObject *set)
2166{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002167 if (!PySet_Check(set)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002168 PyErr_BadInternalCall();
2169 return -1;
2170 }
2171 return set_clear_internal((PySetObject *)set);
2172}
2173
2174int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002175PySet_Contains(PyObject *anyset, PyObject *key)
2176{
2177 if (!PyAnySet_Check(anyset)) {
2178 PyErr_BadInternalCall();
2179 return -1;
2180 }
2181 return set_contains_key((PySetObject *)anyset, key);
2182}
2183
2184int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002185PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002186{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002187 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002188 PyErr_BadInternalCall();
2189 return -1;
2190 }
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002191 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002192}
2193
2194int
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002195PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002196{
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +00002197 if (!PySet_Check(anyset) &&
2198 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
Raymond Hettingerdee3f652008-01-26 09:31:11 +00002199 PyErr_BadInternalCall();
2200 return -1;
2201 }
Raymond Hettingerecdcb582008-01-28 20:34:33 +00002202 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002203}
2204
Barry Warsaw176014f2006-03-30 22:45:35 +00002205int
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002206_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
Barry Warsaw176014f2006-03-30 22:45:35 +00002207{
2208 setentry *entry_ptr;
2209
2210 if (!PyAnySet_Check(set)) {
2211 PyErr_BadInternalCall();
2212 return -1;
2213 }
2214 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2215 return 0;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00002216 *key = entry_ptr->key;
2217 return 1;
2218}
2219
2220int
2221_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2222{
2223 setentry *entry;
2224
2225 if (!PyAnySet_Check(set)) {
2226 PyErr_BadInternalCall();
2227 return -1;
2228 }
2229 if (set_next((PySetObject *)set, pos, &entry) == 0)
2230 return 0;
2231 *key = entry->key;
2232 *hash = entry->hash;
Barry Warsaw176014f2006-03-30 22:45:35 +00002233 return 1;
2234}
2235
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002236PyObject *
2237PySet_Pop(PyObject *set)
2238{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002239 if (!PySet_Check(set)) {
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002240 PyErr_BadInternalCall();
2241 return NULL;
2242 }
2243 return set_pop((PySetObject *)set);
2244}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002245
Barry Warsaw176014f2006-03-30 22:45:35 +00002246int
2247_PySet_Update(PyObject *set, PyObject *iterable)
2248{
Raymond Hettinger7759a0c2008-01-28 21:47:42 +00002249 if (!PySet_Check(set)) {
Barry Warsaw176014f2006-03-30 22:45:35 +00002250 PyErr_BadInternalCall();
2251 return -1;
2252 }
2253 return set_update_internal((PySetObject *)set, iterable);
2254}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002255
2256#ifdef Py_DEBUG
2257
2258/* Test code to be called with any three element set.
2259 Returns True and original set is restored. */
2260
2261#define assertRaises(call_return_value, exception) \
2262 do { \
2263 assert(call_return_value); \
2264 assert(PyErr_ExceptionMatches(exception)); \
2265 PyErr_Clear(); \
2266 } while(0)
2267
2268static PyObject *
2269test_c_api(PySetObject *so)
2270{
Neal Norwitz0f2783c2006-06-19 05:40:44 +00002271 Py_ssize_t count;
Barry Warsaw176014f2006-03-30 22:45:35 +00002272 char *s;
2273 Py_ssize_t i;
Guido van Rossum360496d2007-05-10 17:20:15 +00002274 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
Barry Warsaw176014f2006-03-30 22:45:35 +00002275 PyObject *ob = (PyObject *)so;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002276
2277 /* Verify preconditions and exercise type/size checks */
2278 assert(PyAnySet_Check(ob));
2279 assert(PyAnySet_CheckExact(ob));
2280 assert(!PyFrozenSet_CheckExact(ob));
2281 assert(PySet_Size(ob) == 3);
2282 assert(PySet_GET_SIZE(ob) == 3);
2283
2284 /* Raise TypeError for non-iterable constructor arguments */
2285 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2286 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2287
2288 /* Raise TypeError for unhashable key */
2289 dup = PySet_New(ob);
2290 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2291 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2292 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2293
2294 /* Exercise successful pop, contains, add, and discard */
2295 elem = PySet_Pop(ob);
2296 assert(PySet_Contains(ob, elem) == 0);
2297 assert(PySet_GET_SIZE(ob) == 2);
2298 assert(PySet_Add(ob, elem) == 0);
2299 assert(PySet_Contains(ob, elem) == 1);
2300 assert(PySet_GET_SIZE(ob) == 3);
2301 assert(PySet_Discard(ob, elem) == 1);
2302 assert(PySet_GET_SIZE(ob) == 2);
2303 assert(PySet_Discard(ob, elem) == 0);
2304 assert(PySet_GET_SIZE(ob) == 2);
2305
Barry Warsaw176014f2006-03-30 22:45:35 +00002306 /* Exercise clear */
2307 dup2 = PySet_New(dup);
2308 assert(PySet_Clear(dup2) == 0);
2309 assert(PySet_Size(dup2) == 0);
2310 Py_DECREF(dup2);
2311
2312 /* Raise SystemError on clear or update of frozen set */
2313 f = PyFrozenSet_New(dup);
2314 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2315 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
Amaury Forgeot d'Arccab3d982008-02-03 22:51:43 +00002316 assert(PySet_Add(f, elem) == 0);
2317 Py_INCREF(f);
2318 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2319 Py_DECREF(f);
Barry Warsaw176014f2006-03-30 22:45:35 +00002320 Py_DECREF(f);
2321
2322 /* Exercise direct iteration */
2323 i = 0, count = 0;
Guido van Rossum360496d2007-05-10 17:20:15 +00002324 while (_PySet_Next((PyObject *)dup, &i, &x)) {
Christian Heimes593daf52008-05-26 12:51:38 +00002325 s = PyBytes_AsString(x);
Barry Warsaw176014f2006-03-30 22:45:35 +00002326 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2327 count++;
2328 }
2329 assert(count == 3);
2330
2331 /* Exercise updates */
2332 dup2 = PySet_New(NULL);
2333 assert(_PySet_Update(dup2, dup) == 0);
2334 assert(PySet_Size(dup2) == 3);
2335 assert(_PySet_Update(dup2, dup) == 0);
2336 assert(PySet_Size(dup2) == 3);
2337 Py_DECREF(dup2);
2338
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002339 /* Raise SystemError when self argument is not a set or frozenset. */
2340 t = PyTuple_New(0);
2341 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2342 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2343 Py_DECREF(t);
2344
2345 /* Raise SystemError when self argument is not a set. */
2346 f = PyFrozenSet_New(dup);
2347 assert(PySet_Size(f) == 3);
2348 assert(PyFrozenSet_CheckExact(f));
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002349 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2350 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2351 Py_DECREF(f);
2352
2353 /* Raise KeyError when popping from an empty set */
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002354 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2355 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002356 assert(PySet_GET_SIZE(ob) == 0);
2357 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2358
Raymond Hettingerd8e13382005-08-17 12:27:17 +00002359 /* Restore the set from the copy using the PyNumber API */
2360 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2361 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002362
2363 /* Verify constructors accept NULL arguments */
2364 f = PySet_New(NULL);
2365 assert(f != NULL);
2366 assert(PySet_GET_SIZE(f) == 0);
2367 Py_DECREF(f);
2368 f = PyFrozenSet_New(NULL);
2369 assert(f != NULL);
2370 assert(PyFrozenSet_CheckExact(f));
2371 assert(PySet_GET_SIZE(f) == 0);
2372 Py_DECREF(f);
2373
2374 Py_DECREF(elem);
2375 Py_DECREF(dup);
2376 Py_RETURN_TRUE;
2377}
2378
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002379#undef assertRaises
2380
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002381#endif