blob: 1ad78c4deb3711257ac5c63fbad334e2f07c6711 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingera9d99362005-08-05 00:01:15 +00003 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
Raymond Hettinger2212e522008-11-30 23:43:36 +00006 Copyright (c) 2003-2008 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"
Christian Heimes0ded5b52007-12-10 15:50:56 +000012#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
Thomas Wouters89f507f2006-12-13 04:49:30 +000026}
27
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028/* This must be >= 1. */
29#define PERTURB_SHIFT 5
30
31/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050032static PyObject _dummy_struct;
33
34#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000035
Antoine Pitrou9d952542013-08-24 21:07:07 +020036/* Exported for the gdb plugin's benefit. */
37PyObject *_PySet_Dummy = dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039#define INIT_NONZERO_SET_SLOTS(so) do { \
40 (so)->table = (so)->smalltable; \
41 (so)->mask = PySet_MINSIZE - 1; \
42 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000043 } while(0)
44
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045#define EMPTY_TO_MINSIZE(so) do { \
46 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
47 (so)->used = (so)->fill = 0; \
48 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000049 } while(0)
50
Raymond Hettingerbc841a12005-08-07 13:02:53 +000051/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000052#ifndef PySet_MAXFREELIST
53#define PySet_MAXFREELIST 80
54#endif
55static PySetObject *free_list[PySet_MAXFREELIST];
56static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000057
Christian Heimes0ded5b52007-12-10 15:50:56 +000058
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000059/*
60The basic lookup function used by all operations.
61This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
62Open addressing is preferred over chaining since the link overhead for
63chaining would be substantial (100% with typical malloc overhead).
64
65The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000066probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000067
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070068To improve cache locality, each probe is done in pairs.
69After the probe is examined, an adjacent entry is then examined as well.
70The likelihood is that an adjacent entry is in the same cache line and
71can be examined more cheaply than another probe elsewhere in memory.
72
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000073All arithmetic on hash should ignore overflow.
74
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000075Unlike the dictionary implementation, the lookkey functions can return
76NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000077*/
78
79static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020080set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000081{
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070082 size_t i, j; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020083 size_t perturb;
84 setentry *freeslot;
85 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020087 setentry *entry;
88 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000090
Mark Dickinson57e683e2011-09-24 18:18:40 +010091 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 entry = &table[i];
93 if (entry->key == NULL || entry->key == key)
94 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070095 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger237b34b2013-08-17 02:31:53 -070096 startkey = entry->key;
97 Py_INCREF(startkey);
98 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
99 Py_DECREF(startkey);
100 if (cmp < 0)
101 return NULL;
102 if (table == so->table && entry->key == startkey) {
103 if (cmp > 0)
104 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700106 else {
107 /* Start over if the compare altered the set */
108 return set_lookkey(so, key, hash);
109 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 }
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700111 freeslot = (entry->key == dummy) ? entry : NULL;
112
113 /* In the loop, key == dummy is by far (factor of 100s)
114 the least likely outcome, so test for that last. */
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700115 j = i;
116 perturb = hash;
117 while (1) {
118 j ^= 1;
119 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 if (entry->key == NULL) {
121 if (freeslot != NULL)
122 entry = freeslot;
123 break;
124 }
125 if (entry->key == key)
126 break;
Raymond Hettingerae9e6162013-08-20 22:28:24 -0700127 if (entry->hash == hash && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 startkey = entry->key;
129 Py_INCREF(startkey);
130 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
131 Py_DECREF(startkey);
132 if (cmp < 0)
133 return NULL;
134 if (table == so->table && entry->key == startkey) {
135 if (cmp > 0)
136 break;
137 }
138 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return set_lookkey(so, key, hash);
140 }
141 }
Raymond Hettinger07351a02013-08-17 02:39:46 -0700142 if (entry->key == dummy && freeslot == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 freeslot = entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700144
145 i = i * 5 + perturb + 1;
146 j = i & mask;
147 perturb >>= PERTURB_SHIFT;
148
149 entry = &table[j];
150 if (entry->key == NULL) {
151 if (freeslot != NULL)
152 entry = freeslot;
153 break;
154 }
155 if (entry->key == key)
156 break;
Raymond Hettingerae9e6162013-08-20 22:28:24 -0700157 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700158 startkey = entry->key;
159 Py_INCREF(startkey);
160 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
161 Py_DECREF(startkey);
162 if (cmp < 0)
163 return NULL;
164 if (table == so->table && entry->key == startkey) {
165 if (cmp > 0)
166 break;
167 }
168 else {
169 return set_lookkey(so, key, hash);
170 }
171 }
172 if (entry->key == dummy && freeslot == NULL)
173 freeslot = entry;
174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 }
176 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000177}
178
179/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000180 * Hacked up version of set_lookkey which can assume keys are always unicode;
181 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000182 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000183 */
184static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200185set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000186{
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700187 size_t i, j; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200188 size_t perturb;
189 setentry *freeslot;
190 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200192 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 /* Make sure this function doesn't have to handle non-unicode keys,
195 including subclasses of str; e.g., one reason to subclass
196 strings is to override __eq__, and for speed we don't cater to
197 that here. */
198 if (!PyUnicode_CheckExact(key)) {
199 so->lookup = set_lookkey;
200 return set_lookkey(so, key, hash);
201 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700202
Mark Dickinson57e683e2011-09-24 18:18:40 +0100203 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 entry = &table[i];
205 if (entry->key == NULL || entry->key == key)
206 return entry;
207 if (entry->key == dummy)
208 freeslot = entry;
209 else {
210 if (entry->hash == hash && unicode_eq(entry->key, key))
211 return entry;
212 freeslot = NULL;
213 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000214
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700215 j = i;
216 perturb = hash;
217 while (1) {
218 j ^= 1;
219 entry = &table[j];
220 if (entry->key == NULL)
221 return freeslot == NULL ? entry : freeslot;
222 if (entry->key == key
223 || (entry->hash == hash
224 && entry->key != dummy
225 && unicode_eq(entry->key, key)))
226 return entry;
227 if (entry->key == dummy && freeslot == NULL)
228 freeslot = entry;
229
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700230 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700231 j = i & mask;
232 perturb >>= PERTURB_SHIFT;
233
234 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 if (entry->key == NULL)
236 return freeslot == NULL ? entry : freeslot;
237 if (entry->key == key
238 || (entry->hash == hash
239 && entry->key != dummy
240 && unicode_eq(entry->key, key)))
241 return entry;
242 if (entry->key == dummy && freeslot == NULL)
243 freeslot = entry;
244 }
245 assert(0); /* NOT REACHED */
246 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000247}
248
249/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000250Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000251Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000252Eats a reference to key.
253*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000254static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200255set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000256{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200257 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 assert(so->lookup != NULL);
260 entry = so->lookup(so, key, hash);
261 if (entry == NULL)
262 return -1;
263 if (entry->key == NULL) {
264 /* UNUSED */
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
269 } else if (entry->key == dummy) {
270 /* DUMMY */
271 entry->key = key;
272 entry->hash = hash;
273 so->used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 } else {
275 /* ACTIVE */
276 Py_DECREF(key);
277 }
278 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000279}
280
281/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000282Internal routine used by set_table_resize() to insert an item which is
283known to be absent from the set. This routine also assumes that
284the set contains no deleted entries. Besides the performance benefit,
285using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
286Note that no refcounts are changed by this routine; if needed, the caller
287is responsible for incref'ing `key`.
288*/
289static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200290set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200293 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700294 size_t perturb = hash;
295 size_t mask = (size_t)so->mask;
296 size_t i, j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000297
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700298 i = j = (size_t)hash & mask;
299 while (1) {
300 entry = &table[j];
301 if (entry->key == NULL)
302 break;
303 entry = &table[j ^ 1];
304 if (entry->key == NULL)
305 break;
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700306 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700307 j = i & mask;
308 perturb >>= PERTURB_SHIFT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 }
310 so->fill++;
311 entry->key = key;
312 entry->hash = hash;
313 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000314}
315
316/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000317Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000318keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000319actually be smaller than the old one.
320*/
321static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000322set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 Py_ssize_t newsize;
325 setentry *oldtable, *newtable, *entry;
326 Py_ssize_t i;
327 int is_oldtable_malloced;
328 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 /* Find the smallest table size > minused. */
333 for (newsize = PySet_MINSIZE;
334 newsize <= minused && newsize > 0;
335 newsize <<= 1)
336 ;
337 if (newsize <= 0) {
338 PyErr_NoMemory();
339 return -1;
340 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 /* Get space for a new table. */
343 oldtable = so->table;
344 assert(oldtable != NULL);
345 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (newsize == PySet_MINSIZE) {
348 /* A large table is shrinking, or we can't get any smaller. */
349 newtable = so->smalltable;
350 if (newtable == oldtable) {
351 if (so->fill == so->used) {
352 /* No dummies, so no point doing anything. */
353 return 0;
354 }
355 /* We're not going to resize it, but rebuild the
356 table anyway to purge old dummy entries.
357 Subtle: This is *necessary* if fill==size,
358 as set_lookkey needs at least one virgin slot to
359 terminate failing searches. If fill < size, it's
360 merely desirable, as dummies slow searches. */
361 assert(so->fill > so->used);
362 memcpy(small_copy, oldtable, sizeof(small_copy));
363 oldtable = small_copy;
364 }
365 }
366 else {
367 newtable = PyMem_NEW(setentry, newsize);
368 if (newtable == NULL) {
369 PyErr_NoMemory();
370 return -1;
371 }
372 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 /* Make the set empty, using the new table. */
375 assert(newtable != oldtable);
376 so->table = newtable;
377 so->mask = newsize - 1;
378 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700379 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 /* Copy the data over; this is refcount-neutral for active entries;
384 dummy entries aren't copied over, of course */
385 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500386 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000388 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 }
390 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (is_oldtable_malloced)
393 PyMem_DEL(oldtable);
394 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000395}
396
Raymond Hettingerc991db22005-08-11 07:58:45 +0000397/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
398
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000399static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200400set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000401{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200402 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000403 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100404 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 assert(so->fill <= so->mask); /* at least one empty slot */
407 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000408 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100409 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000410 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 return -1;
412 }
413 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
414 return 0;
415 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000416}
417
418static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200419set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000420{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200421 Py_hash_t hash;
422 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200425 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 hash = PyObject_Hash(key);
427 if (hash == -1)
428 return -1;
429 }
430 assert(so->fill <= so->mask); /* at least one empty slot */
431 n_used = so->used;
432 Py_INCREF(key);
433 if (set_insert_key(so, key, hash) == -1) {
434 Py_DECREF(key);
435 return -1;
436 }
437 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
438 return 0;
439 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000440}
441
442#define DISCARD_NOTFOUND 0
443#define DISCARD_FOUND 1
444
445static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000446set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200447{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000449
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000450 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (entry == NULL)
452 return -1;
453 if (entry->key == NULL || entry->key == dummy)
454 return DISCARD_NOTFOUND;
455 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 entry->key = dummy;
457 so->used--;
458 Py_DECREF(old_key);
459 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000460}
461
462static int
463set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000464{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200465 Py_hash_t hash;
466 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200472 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 hash = PyObject_Hash(key);
474 if (hash == -1)
475 return -1;
476 }
477 entry = (so->lookup)(so, key, hash);
478 if (entry == NULL)
479 return -1;
480 if (entry->key == NULL || entry->key == dummy)
481 return DISCARD_NOTFOUND;
482 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 entry->key = dummy;
484 so->used--;
485 Py_DECREF(old_key);
486 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487}
488
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000489static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000490set_clear_internal(PySetObject *so)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 setentry *entry, *table;
493 int table_is_malloced;
494 Py_ssize_t fill;
495 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 Py_ssize_t i, n;
498 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 n = so->mask + 1;
501 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502#endif
503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 table = so->table;
505 assert(table != NULL);
506 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 /* This is delicate. During the process of clearing the set,
509 * decrefs can cause the set to mutate. To avoid fatal confusion
510 * (voice of experience), we have to make the set empty before
511 * clearing the slots, and never refer to anything via so->ref while
512 * clearing.
513 */
514 fill = so->fill;
515 if (table_is_malloced)
516 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 else if (fill > 0) {
519 /* It's a small table with something that needs to be cleared.
520 * Afraid the only safe way is to copy the set entries into
521 * another small table first.
522 */
523 memcpy(small_copy, table, sizeof(small_copy));
524 table = small_copy;
525 EMPTY_TO_MINSIZE(so);
526 }
527 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 /* Now we can finally clear things. If C had refcounts, we could
530 * assert that the refcount on table is 1 now, i.e. that this function
531 * has unique access to it, so decref side-effects can't alter it.
532 */
533 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 assert(i < n);
536 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000537#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (entry->key) {
539 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700540 if (entry->key != dummy)
541 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 else
545 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000546#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (table_is_malloced)
550 PyMem_DEL(table);
551 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000552}
553
554/*
555 * Iterate over a set table. Use like so:
556 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000557 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000558 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000559 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000560 * while (set_next(yourset, &pos, &entry)) {
561 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000562 * }
563 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000564 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000566 */
567static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000568set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 Py_ssize_t i;
571 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200572 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 assert (PyAnySet_Check(so));
575 i = *pos_ptr;
576 assert(i >= 0);
577 table = so->table;
578 mask = so->mask;
579 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
580 i++;
581 *pos_ptr = i+1;
582 if (i > mask)
583 return 0;
584 assert(table[i].key != NULL);
585 *entry_ptr = &table[i];
586 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000587}
588
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000589static void
590set_dealloc(PySetObject *so)
591{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200592 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 Py_ssize_t fill = so->fill;
594 PyObject_GC_UnTrack(so);
595 Py_TRASHCAN_SAFE_BEGIN(so)
596 if (so->weakreflist != NULL)
597 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 for (entry = so->table; fill > 0; entry++) {
600 if (entry->key) {
601 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700602 if (entry->key != dummy)
603 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 }
605 }
606 if (so->table != so->smalltable)
607 PyMem_DEL(so->table);
608 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
609 free_list[numfree++] = so;
610 else
611 Py_TYPE(so)->tp_free(so);
612 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000613}
614
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000615static PyObject *
616set_repr(PySetObject *so)
617{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200618 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (status != 0) {
622 if (status < 0)
623 return NULL;
624 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
625 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* shortcut for the empty set */
628 if (!so->used) {
629 Py_ReprLeave((PyObject*)so);
630 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
631 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 keys = PySequence_List((PyObject *)so);
634 if (keys == NULL)
635 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000636
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200637 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 listrepr = PyObject_Repr(keys);
639 Py_DECREF(keys);
640 if (listrepr == NULL)
641 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200642 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200644 if (tmp == NULL)
645 goto done;
646 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200647
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200648 if (Py_TYPE(so) != &PySet_Type)
649 result = PyUnicode_FromFormat("%s({%U})",
650 Py_TYPE(so)->tp_name,
651 listrepr);
652 else
653 result = PyUnicode_FromFormat("{%U}", listrepr);
654 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000655done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 Py_ReprLeave((PyObject*)so);
657 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000658}
659
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000661set_len(PyObject *so)
662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000664}
665
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000666static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000667set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000670 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100671 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200672 Py_ssize_t i;
673 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 assert (PyAnySet_Check(so));
676 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 other = (PySetObject*)otherset;
679 if (other == so || other->used == 0)
680 /* a.update(a) or a.update({}); nothing to do */
681 return 0;
682 /* Do one big resize at the start, rather than
683 * incrementally resizing as we insert new keys. Expect
684 * that there will be no (or few) overlapping keys.
685 */
686 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
687 if (set_table_resize(so, (so->used + other->used)*2) != 0)
688 return -1;
689 }
690 for (i = 0; i <= other->mask; i++) {
691 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000692 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100693 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000694 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500695 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000696 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100697 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000698 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 return -1;
700 }
701 }
702 }
703 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000704}
705
706static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000707set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000708{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000709 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200713 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 hash = PyObject_Hash(key);
715 if (hash == -1)
716 return -1;
717 }
718 entry = (so->lookup)(so, key, hash);
719 if (entry == NULL)
720 return -1;
721 key = entry->key;
722 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000723}
724
Raymond Hettingerc991db22005-08-11 07:58:45 +0000725static int
726set_contains_entry(PySetObject *so, setentry *entry)
727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 PyObject *key;
729 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000730
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000731 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (lu_entry == NULL)
733 return -1;
734 key = lu_entry->key;
735 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000736}
737
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000738static PyObject *
739set_pop(PySetObject *so)
740{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200741 Py_ssize_t i = 0;
742 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 assert (PyAnySet_Check(so));
746 if (so->used == 0) {
747 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
748 return NULL;
749 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 /* Set entry to "the first" unused or dummy set entry. We abuse
752 * the hash field of slot 0 to hold a search finger:
753 * If slot 0 has a value, use slot 0.
754 * Else slot 0 is being used to hold a search finger,
755 * and we use its hash value as the first index to look.
756 */
757 entry = &so->table[0];
758 if (entry->key == NULL || entry->key == dummy) {
759 i = entry->hash;
760 /* The hash field may be a real hash value, or it may be a
761 * legit search finger, or it may be a once-legit search
762 * finger that's out of bounds now because it wrapped around
763 * or the table shrunk -- simply make sure it's in bounds now.
764 */
765 if (i > so->mask || i < 1)
766 i = 1; /* skip slot 0 */
767 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
768 i++;
769 if (i > so->mask)
770 i = 1;
771 }
772 }
773 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 entry->key = dummy;
775 so->used--;
776 so->table[0].hash = i + 1; /* next place to start */
777 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000778}
779
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000780PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
781Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000782
783static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000784set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 Py_ssize_t pos = 0;
787 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 while (set_next(so, &pos, &entry))
790 Py_VISIT(entry->key);
791 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000792}
793
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000794static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000795frozenset_hash(PyObject *self)
796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800798 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 setentry *entry;
800 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 if (so->hash != -1)
803 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000804
Mark Dickinson57e683e2011-09-24 18:18:40 +0100805 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 while (set_next(so, &pos, &entry)) {
807 /* Work to increase the bit dispersion for closely spaced hash
808 values. The is important because some use cases have many
809 combinations of a small number of elements with nearby
810 hashes so that many distinct combinations collapse to only
811 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000812 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800813 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800815 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800817 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 so->hash = hash;
819 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000820}
821
Raymond Hettingera9d99362005-08-05 00:01:15 +0000822/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000823
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000824typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 PyObject_HEAD
826 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
827 Py_ssize_t si_used;
828 Py_ssize_t si_pos;
829 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000830} setiterobject;
831
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832static void
833setiter_dealloc(setiterobject *si)
834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 Py_XDECREF(si->si_set);
836 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000837}
838
839static int
840setiter_traverse(setiterobject *si, visitproc visit, void *arg)
841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 Py_VISIT(si->si_set);
843 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000844}
845
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000846static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847setiter_len(setiterobject *si)
848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 Py_ssize_t len = 0;
850 if (si->si_set != NULL && si->si_used == si->si_set->used)
851 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000852 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000853}
854
Armin Rigof5b3e362006-02-11 21:32:43 +0000855PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000856
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000857static PyObject *setiter_iternext(setiterobject *si);
858
859static PyObject *
860setiter_reduce(setiterobject *si)
861{
862 PyObject *list;
863 setiterobject tmp;
864
865 list = PyList_New(0);
866 if (!list)
867 return NULL;
868
Ezio Melotti0e1af282012-09-28 16:43:40 +0300869 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000870 tmp = *si;
871 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300872
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000873 /* iterate the temporary into a list */
874 for(;;) {
875 PyObject *element = setiter_iternext(&tmp);
876 if (element) {
877 if (PyList_Append(list, element)) {
878 Py_DECREF(element);
879 Py_DECREF(list);
880 Py_XDECREF(tmp.si_set);
881 return NULL;
882 }
883 Py_DECREF(element);
884 } else
885 break;
886 }
887 Py_XDECREF(tmp.si_set);
888 /* check for error */
889 if (tmp.si_set != NULL) {
890 /* we have an error */
891 Py_DECREF(list);
892 return NULL;
893 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200894 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000895}
896
897PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
898
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000899static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000901 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000903};
904
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000905static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200908 Py_ssize_t i, mask;
909 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 if (so == NULL)
913 return NULL;
914 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 if (si->si_used != so->used) {
917 PyErr_SetString(PyExc_RuntimeError,
918 "Set changed size during iteration");
919 si->si_used = -1; /* Make this state sticky */
920 return NULL;
921 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 i = si->si_pos;
924 assert(i>=0);
925 entry = so->table;
926 mask = so->mask;
927 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
928 i++;
929 si->si_pos = i+1;
930 if (i > mask)
931 goto fail;
932 si->len--;
933 key = entry[i].key;
934 Py_INCREF(key);
935 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000936
937fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 Py_DECREF(so);
939 si->si_set = NULL;
940 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000941}
942
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000943PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyVarObject_HEAD_INIT(&PyType_Type, 0)
945 "set_iterator", /* tp_name */
946 sizeof(setiterobject), /* tp_basicsize */
947 0, /* tp_itemsize */
948 /* methods */
949 (destructor)setiter_dealloc, /* tp_dealloc */
950 0, /* tp_print */
951 0, /* tp_getattr */
952 0, /* tp_setattr */
953 0, /* tp_reserved */
954 0, /* tp_repr */
955 0, /* tp_as_number */
956 0, /* tp_as_sequence */
957 0, /* tp_as_mapping */
958 0, /* tp_hash */
959 0, /* tp_call */
960 0, /* tp_str */
961 PyObject_GenericGetAttr, /* tp_getattro */
962 0, /* tp_setattro */
963 0, /* tp_as_buffer */
964 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
965 0, /* tp_doc */
966 (traverseproc)setiter_traverse, /* tp_traverse */
967 0, /* tp_clear */
968 0, /* tp_richcompare */
969 0, /* tp_weaklistoffset */
970 PyObject_SelfIter, /* tp_iter */
971 (iternextfunc)setiter_iternext, /* tp_iternext */
972 setiter_methods, /* tp_methods */
973 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000974};
975
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000976static PyObject *
977set_iter(PySetObject *so)
978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
980 if (si == NULL)
981 return NULL;
982 Py_INCREF(so);
983 si->si_set = so;
984 si->si_used = so->used;
985 si->si_pos = 0;
986 si->len = so->used;
987 _PyObject_GC_TRACK(si);
988 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000989}
990
Raymond Hettingerd7946662005-08-01 21:39:29 +0000991static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000992set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 if (PyAnySet_Check(other))
997 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (PyDict_CheckExact(other)) {
1000 PyObject *value;
1001 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001002 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* Do one big resize at the start, rather than
1006 * incrementally resizing as we insert new keys. Expect
1007 * that there will be no (or few) overlapping keys.
1008 */
1009 if (dictsize == -1)
1010 return -1;
1011 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
1012 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
1013 return -1;
1014 }
1015 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1016 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 an_entry.hash = hash;
1019 an_entry.key = key;
1020 if (set_add_entry(so, &an_entry) == -1)
1021 return -1;
1022 }
1023 return 0;
1024 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 it = PyObject_GetIter(other);
1027 if (it == NULL)
1028 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 while ((key = PyIter_Next(it)) != NULL) {
1031 if (set_add_key(so, key) == -1) {
1032 Py_DECREF(it);
1033 Py_DECREF(key);
1034 return -1;
1035 }
1036 Py_DECREF(key);
1037 }
1038 Py_DECREF(it);
1039 if (PyErr_Occurred())
1040 return -1;
1041 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001042}
1043
1044static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001045set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1050 PyObject *other = PyTuple_GET_ITEM(args, i);
1051 if (set_update_internal(so, other) == -1)
1052 return NULL;
1053 }
1054 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001055}
1056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001058"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001059
1060static PyObject *
1061make_new_set(PyTypeObject *type, PyObject *iterable)
1062{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001063 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 /* create PySetObject structure */
1066 if (numfree &&
1067 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1068 so = free_list[--numfree];
1069 assert (so != NULL && PyAnySet_CheckExact(so));
1070 Py_TYPE(so) = type;
1071 _Py_NewReference((PyObject *)so);
1072 EMPTY_TO_MINSIZE(so);
1073 PyObject_GC_Track(so);
1074 } else {
1075 so = (PySetObject *)type->tp_alloc(type, 0);
1076 if (so == NULL)
1077 return NULL;
1078 /* tp_alloc has already zeroed the structure */
1079 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1080 INIT_NONZERO_SET_SLOTS(so);
1081 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 so->lookup = set_lookkey_unicode;
1084 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 if (iterable != NULL) {
1087 if (set_update_internal(so, iterable) == -1) {
1088 Py_DECREF(so);
1089 return NULL;
1090 }
1091 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001094}
1095
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001096static PyObject *
1097make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1100 if (PyType_IsSubtype(type, &PySet_Type))
1101 type = &PySet_Type;
1102 else
1103 type = &PyFrozenSet_Type;
1104 }
1105 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001106}
1107
Raymond Hettingerd7946662005-08-01 21:39:29 +00001108/* The empty frozenset is a singleton */
1109static PyObject *emptyfrozenset = NULL;
1110
Raymond Hettingera690a992003-11-16 16:17:49 +00001111static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001112frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1117 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1120 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (type != &PyFrozenSet_Type)
1123 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (iterable != NULL) {
1126 /* frozenset(f) is idempotent */
1127 if (PyFrozenSet_CheckExact(iterable)) {
1128 Py_INCREF(iterable);
1129 return iterable;
1130 }
1131 result = make_new_set(type, iterable);
1132 if (result == NULL || PySet_GET_SIZE(result))
1133 return result;
1134 Py_DECREF(result);
1135 }
1136 /* The empty frozenset is a singleton */
1137 if (emptyfrozenset == NULL)
1138 emptyfrozenset = make_new_set(type, NULL);
1139 Py_XINCREF(emptyfrozenset);
1140 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001141}
1142
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001143int
1144PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001145{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001146 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 while (numfree) {
1150 numfree--;
1151 so = free_list[numfree];
1152 PyObject_GC_Del(so);
1153 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001154 return freelist_size;
1155}
1156
1157void
1158PySet_Fini(void)
1159{
1160 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001162}
1163
David Malcolm49526f42012-06-22 14:55:41 -04001164/* Print summary info about the state of the optimized allocator */
1165void
1166_PySet_DebugMallocStats(FILE *out)
1167{
1168 _PyDebugAllocatorStats(out,
1169 "free PySetObject",
1170 numfree, sizeof(PySetObject));
1171}
1172
1173
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001174static PyObject *
1175set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1178 return NULL;
1179
1180 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001181}
1182
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001183/* set_swap_bodies() switches the contents of any two sets by moving their
1184 internal data pointers and, if needed, copying the internal smalltables.
1185 Semantically equivalent to:
1186
1187 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1188
1189 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001191 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001193 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001194*/
1195
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001196static void
1197set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 Py_ssize_t t;
1200 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001201 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001203 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 t = a->fill; a->fill = b->fill; b->fill = t;
1206 t = a->used; a->used = b->used; b->used = t;
1207 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 u = a->table;
1210 if (a->table == a->smalltable)
1211 u = b->smalltable;
1212 a->table = b->table;
1213 if (b->table == b->smalltable)
1214 a->table = a->smalltable;
1215 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (a->table == a->smalltable || b->table == b->smalltable) {
1220 memcpy(tab, a->smalltable, sizeof(tab));
1221 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1222 memcpy(b->smalltable, tab, sizeof(tab));
1223 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1226 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1227 h = a->hash; a->hash = b->hash; b->hash = h;
1228 } else {
1229 a->hash = -1;
1230 b->hash = -1;
1231 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001232}
1233
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001234static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001235set_copy(PySetObject *so)
1236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001238}
1239
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001240static PyObject *
1241frozenset_copy(PySetObject *so)
1242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 if (PyFrozenSet_CheckExact(so)) {
1244 Py_INCREF(so);
1245 return (PyObject *)so;
1246 }
1247 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001248}
1249
Raymond Hettingera690a992003-11-16 16:17:49 +00001250PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1251
1252static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001253set_clear(PySetObject *so)
1254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 set_clear_internal(so);
1256 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001257}
1258
1259PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1260
1261static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001262set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 PySetObject *result;
1265 PyObject *other;
1266 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 result = (PySetObject *)set_copy(so);
1269 if (result == NULL)
1270 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1273 other = PyTuple_GET_ITEM(args, i);
1274 if ((PyObject *)so == other)
1275 continue;
1276 if (set_update_internal(result, other) == -1) {
1277 Py_DECREF(result);
1278 return NULL;
1279 }
1280 }
1281 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001282}
1283
1284PyDoc_STRVAR(union_doc,
1285 "Return the union of sets as a new set.\n\
1286\n\
1287(i.e. all elements that are in either set.)");
1288
1289static PyObject *
1290set_or(PySetObject *so, PyObject *other)
1291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001293
Brian Curtindfc80e32011-08-10 20:28:54 -05001294 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1295 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 result = (PySetObject *)set_copy(so);
1298 if (result == NULL)
1299 return NULL;
1300 if ((PyObject *)so == other)
1301 return (PyObject *)result;
1302 if (set_update_internal(result, other) == -1) {
1303 Py_DECREF(result);
1304 return NULL;
1305 }
1306 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001307}
1308
Raymond Hettingera690a992003-11-16 16:17:49 +00001309static PyObject *
1310set_ior(PySetObject *so, PyObject *other)
1311{
Brian Curtindfc80e32011-08-10 20:28:54 -05001312 if (!PyAnySet_Check(other))
1313 Py_RETURN_NOTIMPLEMENTED;
1314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 if (set_update_internal(so, other) == -1)
1316 return NULL;
1317 Py_INCREF(so);
1318 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001319}
1320
1321static PyObject *
1322set_intersection(PySetObject *so, PyObject *other)
1323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 PySetObject *result;
1325 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 if ((PyObject *)so == other)
1328 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1331 if (result == NULL)
1332 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (PyAnySet_Check(other)) {
1335 Py_ssize_t pos = 0;
1336 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1339 tmp = (PyObject *)so;
1340 so = (PySetObject *)other;
1341 other = tmp;
1342 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 while (set_next((PySetObject *)other, &pos, &entry)) {
1345 int rv = set_contains_entry(so, entry);
1346 if (rv == -1) {
1347 Py_DECREF(result);
1348 return NULL;
1349 }
1350 if (rv) {
1351 if (set_add_entry(result, entry) == -1) {
1352 Py_DECREF(result);
1353 return NULL;
1354 }
1355 }
1356 }
1357 return (PyObject *)result;
1358 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 it = PyObject_GetIter(other);
1361 if (it == NULL) {
1362 Py_DECREF(result);
1363 return NULL;
1364 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 while ((key = PyIter_Next(it)) != NULL) {
1367 int rv;
1368 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001369 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 if (hash == -1) {
1372 Py_DECREF(it);
1373 Py_DECREF(result);
1374 Py_DECREF(key);
1375 return NULL;
1376 }
1377 entry.hash = hash;
1378 entry.key = key;
1379 rv = set_contains_entry(so, &entry);
1380 if (rv == -1) {
1381 Py_DECREF(it);
1382 Py_DECREF(result);
1383 Py_DECREF(key);
1384 return NULL;
1385 }
1386 if (rv) {
1387 if (set_add_entry(result, &entry) == -1) {
1388 Py_DECREF(it);
1389 Py_DECREF(result);
1390 Py_DECREF(key);
1391 return NULL;
1392 }
1393 }
1394 Py_DECREF(key);
1395 }
1396 Py_DECREF(it);
1397 if (PyErr_Occurred()) {
1398 Py_DECREF(result);
1399 return NULL;
1400 }
1401 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001402}
1403
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001404static PyObject *
1405set_intersection_multi(PySetObject *so, PyObject *args)
1406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 Py_ssize_t i;
1408 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if (PyTuple_GET_SIZE(args) == 0)
1411 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 Py_INCREF(so);
1414 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1415 PyObject *other = PyTuple_GET_ITEM(args, i);
1416 PyObject *newresult = set_intersection((PySetObject *)result, other);
1417 if (newresult == NULL) {
1418 Py_DECREF(result);
1419 return NULL;
1420 }
1421 Py_DECREF(result);
1422 result = newresult;
1423 }
1424 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001425}
1426
Raymond Hettingera690a992003-11-16 16:17:49 +00001427PyDoc_STRVAR(intersection_doc,
1428"Return the intersection of two sets as a new set.\n\
1429\n\
1430(i.e. all elements that are in both sets.)");
1431
1432static PyObject *
1433set_intersection_update(PySetObject *so, PyObject *other)
1434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 tmp = set_intersection(so, other);
1438 if (tmp == NULL)
1439 return NULL;
1440 set_swap_bodies(so, (PySetObject *)tmp);
1441 Py_DECREF(tmp);
1442 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001443}
1444
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001445static PyObject *
1446set_intersection_update_multi(PySetObject *so, PyObject *args)
1447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 tmp = set_intersection_multi(so, args);
1451 if (tmp == NULL)
1452 return NULL;
1453 set_swap_bodies(so, (PySetObject *)tmp);
1454 Py_DECREF(tmp);
1455 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001456}
1457
Raymond Hettingera690a992003-11-16 16:17:49 +00001458PyDoc_STRVAR(intersection_update_doc,
1459"Update a set with the intersection of itself and another.");
1460
1461static PyObject *
1462set_and(PySetObject *so, PyObject *other)
1463{
Brian Curtindfc80e32011-08-10 20:28:54 -05001464 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1465 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001467}
1468
1469static PyObject *
1470set_iand(PySetObject *so, PyObject *other)
1471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001473
Brian Curtindfc80e32011-08-10 20:28:54 -05001474 if (!PyAnySet_Check(other))
1475 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 result = set_intersection_update(so, other);
1477 if (result == NULL)
1478 return NULL;
1479 Py_DECREF(result);
1480 Py_INCREF(so);
1481 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001482}
1483
Guido van Rossum58da9312007-11-10 23:39:45 +00001484static PyObject *
1485set_isdisjoint(PySetObject *so, PyObject *other)
1486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 if ((PyObject *)so == other) {
1490 if (PySet_GET_SIZE(so) == 0)
1491 Py_RETURN_TRUE;
1492 else
1493 Py_RETURN_FALSE;
1494 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 if (PyAnySet_CheckExact(other)) {
1497 Py_ssize_t pos = 0;
1498 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1501 tmp = (PyObject *)so;
1502 so = (PySetObject *)other;
1503 other = tmp;
1504 }
1505 while (set_next((PySetObject *)other, &pos, &entry)) {
1506 int rv = set_contains_entry(so, entry);
1507 if (rv == -1)
1508 return NULL;
1509 if (rv)
1510 Py_RETURN_FALSE;
1511 }
1512 Py_RETURN_TRUE;
1513 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 it = PyObject_GetIter(other);
1516 if (it == NULL)
1517 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 while ((key = PyIter_Next(it)) != NULL) {
1520 int rv;
1521 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001522 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 if (hash == -1) {
1525 Py_DECREF(key);
1526 Py_DECREF(it);
1527 return NULL;
1528 }
1529 entry.hash = hash;
1530 entry.key = key;
1531 rv = set_contains_entry(so, &entry);
1532 Py_DECREF(key);
1533 if (rv == -1) {
1534 Py_DECREF(it);
1535 return NULL;
1536 }
1537 if (rv) {
1538 Py_DECREF(it);
1539 Py_RETURN_FALSE;
1540 }
1541 }
1542 Py_DECREF(it);
1543 if (PyErr_Occurred())
1544 return NULL;
1545 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001546}
1547
1548PyDoc_STRVAR(isdisjoint_doc,
1549"Return True if two sets have a null intersection.");
1550
Neal Norwitz6576bd82005-11-13 18:41:28 +00001551static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001552set_difference_update_internal(PySetObject *so, PyObject *other)
1553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 if ((PyObject *)so == other)
1555 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (PyAnySet_Check(other)) {
1558 setentry *entry;
1559 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 while (set_next((PySetObject *)other, &pos, &entry))
1562 if (set_discard_entry(so, entry) == -1)
1563 return -1;
1564 } else {
1565 PyObject *key, *it;
1566 it = PyObject_GetIter(other);
1567 if (it == NULL)
1568 return -1;
1569
1570 while ((key = PyIter_Next(it)) != NULL) {
1571 if (set_discard_key(so, key) == -1) {
1572 Py_DECREF(it);
1573 Py_DECREF(key);
1574 return -1;
1575 }
1576 Py_DECREF(key);
1577 }
1578 Py_DECREF(it);
1579 if (PyErr_Occurred())
1580 return -1;
1581 }
1582 /* If more than 1/5 are dummies, then resize them away. */
1583 if ((so->fill - so->used) * 5 < so->mask)
1584 return 0;
1585 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001586}
1587
Raymond Hettingera690a992003-11-16 16:17:49 +00001588static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001589set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1594 PyObject *other = PyTuple_GET_ITEM(args, i);
1595 if (set_difference_update_internal(so, other) == -1)
1596 return NULL;
1597 }
1598 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001599}
1600
1601PyDoc_STRVAR(difference_update_doc,
1602"Remove all elements of another set from this set.");
1603
1604static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001605set_copy_and_difference(PySetObject *so, PyObject *other)
1606{
1607 PyObject *result;
1608
1609 result = set_copy(so);
1610 if (result == NULL)
1611 return NULL;
1612 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1613 return result;
1614 Py_DECREF(result);
1615 return NULL;
1616}
1617
1618static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001619set_difference(PySetObject *so, PyObject *other)
1620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 PyObject *result;
1622 setentry *entry;
1623 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001626 return set_copy_and_difference(so, other);
1627 }
1628
1629 /* If len(so) much more than len(other), it's more efficient to simply copy
1630 * so and then iterate other looking for common elements. */
1631 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1632 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 result = make_new_set_basetype(Py_TYPE(so), NULL);
1636 if (result == NULL)
1637 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 if (PyDict_CheckExact(other)) {
1640 while (set_next(so, &pos, &entry)) {
1641 setentry entrycopy;
1642 entrycopy.hash = entry->hash;
1643 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001644 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1646 Py_DECREF(result);
1647 return NULL;
1648 }
1649 }
1650 }
1651 return result;
1652 }
1653
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001654 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 while (set_next(so, &pos, &entry)) {
1656 int rv = set_contains_entry((PySetObject *)other, entry);
1657 if (rv == -1) {
1658 Py_DECREF(result);
1659 return NULL;
1660 }
1661 if (!rv) {
1662 if (set_add_entry((PySetObject *)result, entry) == -1) {
1663 Py_DECREF(result);
1664 return NULL;
1665 }
1666 }
1667 }
1668 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001669}
1670
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001671static PyObject *
1672set_difference_multi(PySetObject *so, PyObject *args)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 Py_ssize_t i;
1675 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 if (PyTuple_GET_SIZE(args) == 0)
1678 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 other = PyTuple_GET_ITEM(args, 0);
1681 result = set_difference(so, other);
1682 if (result == NULL)
1683 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1686 other = PyTuple_GET_ITEM(args, i);
1687 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1688 Py_DECREF(result);
1689 return NULL;
1690 }
1691 }
1692 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001693}
1694
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001695PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001696"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001697\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001698(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001699static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001700set_sub(PySetObject *so, PyObject *other)
1701{
Brian Curtindfc80e32011-08-10 20:28:54 -05001702 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1703 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001705}
1706
1707static PyObject *
1708set_isub(PySetObject *so, PyObject *other)
1709{
Brian Curtindfc80e32011-08-10 20:28:54 -05001710 if (!PyAnySet_Check(other))
1711 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 if (set_difference_update_internal(so, other) == -1)
1713 return NULL;
1714 Py_INCREF(so);
1715 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001716}
1717
1718static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001719set_symmetric_difference_update(PySetObject *so, PyObject *other)
1720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 PySetObject *otherset;
1722 PyObject *key;
1723 Py_ssize_t pos = 0;
1724 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 if ((PyObject *)so == other)
1727 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 if (PyDict_CheckExact(other)) {
1730 PyObject *value;
1731 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001732 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1734 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001735
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001736 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 an_entry.hash = hash;
1738 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001741 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001742 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001745 if (rv == DISCARD_NOTFOUND) {
1746 if (set_add_entry(so, &an_entry) == -1) {
1747 Py_DECREF(key);
1748 return NULL;
1749 }
1750 }
Georg Brandl2d444492010-09-03 10:52:55 +00001751 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 }
1753 Py_RETURN_NONE;
1754 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 if (PyAnySet_Check(other)) {
1757 Py_INCREF(other);
1758 otherset = (PySetObject *)other;
1759 } else {
1760 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1761 if (otherset == NULL)
1762 return NULL;
1763 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 while (set_next(otherset, &pos, &entry)) {
1766 int rv = set_discard_entry(so, entry);
1767 if (rv == -1) {
1768 Py_DECREF(otherset);
1769 return NULL;
1770 }
1771 if (rv == DISCARD_NOTFOUND) {
1772 if (set_add_entry(so, entry) == -1) {
1773 Py_DECREF(otherset);
1774 return NULL;
1775 }
1776 }
1777 }
1778 Py_DECREF(otherset);
1779 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001780}
1781
1782PyDoc_STRVAR(symmetric_difference_update_doc,
1783"Update a set with the symmetric difference of itself and another.");
1784
1785static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001786set_symmetric_difference(PySetObject *so, PyObject *other)
1787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 PyObject *rv;
1789 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1792 if (otherset == NULL)
1793 return NULL;
1794 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1795 if (rv == NULL)
1796 return NULL;
1797 Py_DECREF(rv);
1798 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001799}
1800
1801PyDoc_STRVAR(symmetric_difference_doc,
1802"Return the symmetric difference of two sets as a new set.\n\
1803\n\
1804(i.e. all elements that are in exactly one of the sets.)");
1805
1806static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001807set_xor(PySetObject *so, PyObject *other)
1808{
Brian Curtindfc80e32011-08-10 20:28:54 -05001809 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1810 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001812}
1813
1814static PyObject *
1815set_ixor(PySetObject *so, PyObject *other)
1816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001818
Brian Curtindfc80e32011-08-10 20:28:54 -05001819 if (!PyAnySet_Check(other))
1820 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 result = set_symmetric_difference_update(so, other);
1822 if (result == NULL)
1823 return NULL;
1824 Py_DECREF(result);
1825 Py_INCREF(so);
1826 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001827}
1828
1829static PyObject *
1830set_issubset(PySetObject *so, PyObject *other)
1831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 setentry *entry;
1833 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 if (!PyAnySet_Check(other)) {
1836 PyObject *tmp, *result;
1837 tmp = make_new_set(&PySet_Type, other);
1838 if (tmp == NULL)
1839 return NULL;
1840 result = set_issubset(so, tmp);
1841 Py_DECREF(tmp);
1842 return result;
1843 }
1844 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1845 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 while (set_next(so, &pos, &entry)) {
1848 int rv = set_contains_entry((PySetObject *)other, entry);
1849 if (rv == -1)
1850 return NULL;
1851 if (!rv)
1852 Py_RETURN_FALSE;
1853 }
1854 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001855}
1856
1857PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1858
1859static PyObject *
1860set_issuperset(PySetObject *so, PyObject *other)
1861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 if (!PyAnySet_Check(other)) {
1865 tmp = make_new_set(&PySet_Type, other);
1866 if (tmp == NULL)
1867 return NULL;
1868 result = set_issuperset(so, tmp);
1869 Py_DECREF(tmp);
1870 return result;
1871 }
1872 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001873}
1874
1875PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1876
Raymond Hettingera690a992003-11-16 16:17:49 +00001877static PyObject *
1878set_richcompare(PySetObject *v, PyObject *w, int op)
1879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001881
Brian Curtindfc80e32011-08-10 20:28:54 -05001882 if(!PyAnySet_Check(w))
1883 Py_RETURN_NOTIMPLEMENTED;
1884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 switch (op) {
1886 case Py_EQ:
1887 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1888 Py_RETURN_FALSE;
1889 if (v->hash != -1 &&
1890 ((PySetObject *)w)->hash != -1 &&
1891 v->hash != ((PySetObject *)w)->hash)
1892 Py_RETURN_FALSE;
1893 return set_issubset(v, w);
1894 case Py_NE:
1895 r1 = set_richcompare(v, w, Py_EQ);
1896 if (r1 == NULL)
1897 return NULL;
1898 r2 = PyBool_FromLong(PyObject_Not(r1));
1899 Py_DECREF(r1);
1900 return r2;
1901 case Py_LE:
1902 return set_issubset(v, w);
1903 case Py_GE:
1904 return set_issuperset(v, w);
1905 case Py_LT:
1906 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1907 Py_RETURN_FALSE;
1908 return set_issubset(v, w);
1909 case Py_GT:
1910 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1911 Py_RETURN_FALSE;
1912 return set_issuperset(v, w);
1913 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001914 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001915}
1916
Raymond Hettingera690a992003-11-16 16:17:49 +00001917static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001918set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 if (set_add_key(so, key) == -1)
1921 return NULL;
1922 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001923}
1924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001926"Add an element to a set.\n\
1927\n\
1928This has no effect if the element is already present.");
1929
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001930static int
1931set_contains(PySetObject *so, PyObject *key)
1932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 PyObject *tmpkey;
1934 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 rv = set_contains_key(so, key);
1937 if (rv == -1) {
1938 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1939 return -1;
1940 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001941 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 if (tmpkey == NULL)
1943 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001944 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 Py_DECREF(tmpkey);
1946 }
1947 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001948}
1949
1950static PyObject *
1951set_direct_contains(PySetObject *so, PyObject *key)
1952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 result = set_contains(so, key);
1956 if (result == -1)
1957 return NULL;
1958 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001959}
1960
1961PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1962
Raymond Hettingera690a992003-11-16 16:17:49 +00001963static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001964set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 PyObject *tmpkey;
1967 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 rv = set_discard_key(so, key);
1970 if (rv == -1) {
1971 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1972 return NULL;
1973 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001974 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 if (tmpkey == NULL)
1976 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 Py_DECREF(tmpkey);
1979 if (rv == -1)
1980 return NULL;
1981 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 if (rv == DISCARD_NOTFOUND) {
1984 set_key_error(key);
1985 return NULL;
1986 }
1987 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001988}
1989
1990PyDoc_STRVAR(remove_doc,
1991"Remove an element from a set; it must be a member.\n\
1992\n\
1993If the element is not a member, raise a KeyError.");
1994
1995static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001996set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001997{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001998 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00002000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 rv = set_discard_key(so, key);
2002 if (rv == -1) {
2003 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
2004 return NULL;
2005 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00002006 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 if (tmpkey == NULL)
2008 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002009 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002011 if (rv == -1)
2012 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 }
2014 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00002015}
2016
2017PyDoc_STRVAR(discard_doc,
2018"Remove an element from a set if it is a member.\n\
2019\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002021
2022static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00002023set_reduce(PySetObject *so)
2024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002026 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 keys = PySequence_List((PyObject *)so);
2029 if (keys == NULL)
2030 goto done;
2031 args = PyTuple_Pack(1, keys);
2032 if (args == NULL)
2033 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002034 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 if (dict == NULL) {
2036 PyErr_Clear();
2037 dict = Py_None;
2038 Py_INCREF(dict);
2039 }
2040 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002041done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 Py_XDECREF(args);
2043 Py_XDECREF(keys);
2044 Py_XDECREF(dict);
2045 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002046}
2047
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002048static PyObject *
2049set_sizeof(PySetObject *so)
2050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 res = sizeof(PySetObject);
2054 if (so->table != so->smalltable)
2055 res = res + (so->mask + 1) * sizeof(setentry);
2056 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002057}
2058
2059PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002060static int
2061set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 if (!PyAnySet_Check(self))
2066 return -1;
2067 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2068 return -1;
2069 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2070 return -1;
2071 set_clear_internal(self);
2072 self->hash = -1;
2073 if (iterable == NULL)
2074 return 0;
2075 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002076}
2077
Raymond Hettingera690a992003-11-16 16:17:49 +00002078static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 set_len, /* sq_length */
2080 0, /* sq_concat */
2081 0, /* sq_repeat */
2082 0, /* sq_item */
2083 0, /* sq_slice */
2084 0, /* sq_ass_item */
2085 0, /* sq_ass_slice */
2086 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002087};
2088
2089/* set object ********************************************************/
2090
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002091#ifdef Py_DEBUG
2092static PyObject *test_c_api(PySetObject *so);
2093
2094PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2095All is well if assertions don't fail.");
2096#endif
2097
Raymond Hettingera690a992003-11-16 16:17:49 +00002098static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 {"add", (PyCFunction)set_add, METH_O,
2100 add_doc},
2101 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2102 clear_doc},
2103 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2104 contains_doc},
2105 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2106 copy_doc},
2107 {"discard", (PyCFunction)set_discard, METH_O,
2108 discard_doc},
2109 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2110 difference_doc},
2111 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2112 difference_update_doc},
2113 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2114 intersection_doc},
2115 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2116 intersection_update_doc},
2117 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2118 isdisjoint_doc},
2119 {"issubset", (PyCFunction)set_issubset, METH_O,
2120 issubset_doc},
2121 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2122 issuperset_doc},
2123 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2124 pop_doc},
2125 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2126 reduce_doc},
2127 {"remove", (PyCFunction)set_remove, METH_O,
2128 remove_doc},
2129 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2130 sizeof_doc},
2131 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2132 symmetric_difference_doc},
2133 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2134 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002135#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2137 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002138#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 {"union", (PyCFunction)set_union, METH_VARARGS,
2140 union_doc},
2141 {"update", (PyCFunction)set_update, METH_VARARGS,
2142 update_doc},
2143 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002144};
2145
2146static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 0, /*nb_add*/
2148 (binaryfunc)set_sub, /*nb_subtract*/
2149 0, /*nb_multiply*/
2150 0, /*nb_remainder*/
2151 0, /*nb_divmod*/
2152 0, /*nb_power*/
2153 0, /*nb_negative*/
2154 0, /*nb_positive*/
2155 0, /*nb_absolute*/
2156 0, /*nb_bool*/
2157 0, /*nb_invert*/
2158 0, /*nb_lshift*/
2159 0, /*nb_rshift*/
2160 (binaryfunc)set_and, /*nb_and*/
2161 (binaryfunc)set_xor, /*nb_xor*/
2162 (binaryfunc)set_or, /*nb_or*/
2163 0, /*nb_int*/
2164 0, /*nb_reserved*/
2165 0, /*nb_float*/
2166 0, /*nb_inplace_add*/
2167 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2168 0, /*nb_inplace_multiply*/
2169 0, /*nb_inplace_remainder*/
2170 0, /*nb_inplace_power*/
2171 0, /*nb_inplace_lshift*/
2172 0, /*nb_inplace_rshift*/
2173 (binaryfunc)set_iand, /*nb_inplace_and*/
2174 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2175 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002176};
2177
2178PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002179"set() -> new empty set object\n\
2180set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002181\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002182Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002183
2184PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2186 "set", /* tp_name */
2187 sizeof(PySetObject), /* tp_basicsize */
2188 0, /* tp_itemsize */
2189 /* methods */
2190 (destructor)set_dealloc, /* tp_dealloc */
2191 0, /* tp_print */
2192 0, /* tp_getattr */
2193 0, /* tp_setattr */
2194 0, /* tp_reserved */
2195 (reprfunc)set_repr, /* tp_repr */
2196 &set_as_number, /* tp_as_number */
2197 &set_as_sequence, /* tp_as_sequence */
2198 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002199 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 0, /* tp_call */
2201 0, /* tp_str */
2202 PyObject_GenericGetAttr, /* tp_getattro */
2203 0, /* tp_setattro */
2204 0, /* tp_as_buffer */
2205 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2206 Py_TPFLAGS_BASETYPE, /* tp_flags */
2207 set_doc, /* tp_doc */
2208 (traverseproc)set_traverse, /* tp_traverse */
2209 (inquiry)set_clear_internal, /* tp_clear */
2210 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002211 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2212 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 0, /* tp_iternext */
2214 set_methods, /* tp_methods */
2215 0, /* tp_members */
2216 0, /* tp_getset */
2217 0, /* tp_base */
2218 0, /* tp_dict */
2219 0, /* tp_descr_get */
2220 0, /* tp_descr_set */
2221 0, /* tp_dictoffset */
2222 (initproc)set_init, /* tp_init */
2223 PyType_GenericAlloc, /* tp_alloc */
2224 set_new, /* tp_new */
2225 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002226};
2227
2228/* frozenset object ********************************************************/
2229
2230
2231static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2233 contains_doc},
2234 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2235 copy_doc},
2236 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2237 difference_doc},
2238 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2239 intersection_doc},
2240 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2241 isdisjoint_doc},
2242 {"issubset", (PyCFunction)set_issubset, METH_O,
2243 issubset_doc},
2244 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2245 issuperset_doc},
2246 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2247 reduce_doc},
2248 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2249 sizeof_doc},
2250 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2251 symmetric_difference_doc},
2252 {"union", (PyCFunction)set_union, METH_VARARGS,
2253 union_doc},
2254 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002255};
2256
2257static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 0, /*nb_add*/
2259 (binaryfunc)set_sub, /*nb_subtract*/
2260 0, /*nb_multiply*/
2261 0, /*nb_remainder*/
2262 0, /*nb_divmod*/
2263 0, /*nb_power*/
2264 0, /*nb_negative*/
2265 0, /*nb_positive*/
2266 0, /*nb_absolute*/
2267 0, /*nb_bool*/
2268 0, /*nb_invert*/
2269 0, /*nb_lshift*/
2270 0, /*nb_rshift*/
2271 (binaryfunc)set_and, /*nb_and*/
2272 (binaryfunc)set_xor, /*nb_xor*/
2273 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002274};
2275
2276PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002277"frozenset() -> empty frozenset object\n\
2278frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002279\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002280Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002281
2282PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2284 "frozenset", /* tp_name */
2285 sizeof(PySetObject), /* tp_basicsize */
2286 0, /* tp_itemsize */
2287 /* methods */
2288 (destructor)set_dealloc, /* tp_dealloc */
2289 0, /* tp_print */
2290 0, /* tp_getattr */
2291 0, /* tp_setattr */
2292 0, /* tp_reserved */
2293 (reprfunc)set_repr, /* tp_repr */
2294 &frozenset_as_number, /* tp_as_number */
2295 &set_as_sequence, /* tp_as_sequence */
2296 0, /* tp_as_mapping */
2297 frozenset_hash, /* tp_hash */
2298 0, /* tp_call */
2299 0, /* tp_str */
2300 PyObject_GenericGetAttr, /* tp_getattro */
2301 0, /* tp_setattro */
2302 0, /* tp_as_buffer */
2303 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2304 Py_TPFLAGS_BASETYPE, /* tp_flags */
2305 frozenset_doc, /* tp_doc */
2306 (traverseproc)set_traverse, /* tp_traverse */
2307 (inquiry)set_clear_internal, /* tp_clear */
2308 (richcmpfunc)set_richcompare, /* tp_richcompare */
2309 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2310 (getiterfunc)set_iter, /* tp_iter */
2311 0, /* tp_iternext */
2312 frozenset_methods, /* tp_methods */
2313 0, /* tp_members */
2314 0, /* tp_getset */
2315 0, /* tp_base */
2316 0, /* tp_dict */
2317 0, /* tp_descr_get */
2318 0, /* tp_descr_set */
2319 0, /* tp_dictoffset */
2320 0, /* tp_init */
2321 PyType_GenericAlloc, /* tp_alloc */
2322 frozenset_new, /* tp_new */
2323 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002324};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002325
2326
2327/***** C API functions *************************************************/
2328
2329PyObject *
2330PySet_New(PyObject *iterable)
2331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002333}
2334
2335PyObject *
2336PyFrozenSet_New(PyObject *iterable)
2337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002339}
2340
Neal Norwitz8c49c822006-03-04 18:41:19 +00002341Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002342PySet_Size(PyObject *anyset)
2343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 if (!PyAnySet_Check(anyset)) {
2345 PyErr_BadInternalCall();
2346 return -1;
2347 }
2348 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002349}
2350
2351int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002352PySet_Clear(PyObject *set)
2353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (!PySet_Check(set)) {
2355 PyErr_BadInternalCall();
2356 return -1;
2357 }
2358 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002359}
2360
2361int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002362PySet_Contains(PyObject *anyset, PyObject *key)
2363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 if (!PyAnySet_Check(anyset)) {
2365 PyErr_BadInternalCall();
2366 return -1;
2367 }
2368 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002369}
2370
2371int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002372PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 if (!PySet_Check(set)) {
2375 PyErr_BadInternalCall();
2376 return -1;
2377 }
2378 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002379}
2380
2381int
Christian Heimesfd66e512008-01-29 12:18:50 +00002382PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 if (!PySet_Check(anyset) &&
2385 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2386 PyErr_BadInternalCall();
2387 return -1;
2388 }
2389 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002390}
2391
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002392int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002393_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 if (!PyAnySet_Check(set)) {
2398 PyErr_BadInternalCall();
2399 return -1;
2400 }
2401 if (set_next((PySetObject *)set, pos, &entry) == 0)
2402 return 0;
2403 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002404 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002406}
2407
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002408PyObject *
2409PySet_Pop(PyObject *set)
2410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 if (!PySet_Check(set)) {
2412 PyErr_BadInternalCall();
2413 return NULL;
2414 }
2415 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002416}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002417
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002418int
2419_PySet_Update(PyObject *set, PyObject *iterable)
2420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 if (!PySet_Check(set)) {
2422 PyErr_BadInternalCall();
2423 return -1;
2424 }
2425 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002426}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002427
2428#ifdef Py_DEBUG
2429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002431 Returns True and original set is restored. */
2432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433#define assertRaises(call_return_value, exception) \
2434 do { \
2435 assert(call_return_value); \
2436 assert(PyErr_ExceptionMatches(exception)); \
2437 PyErr_Clear(); \
2438 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002439
2440static PyObject *
2441test_c_api(PySetObject *so)
2442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 Py_ssize_t count;
2444 char *s;
2445 Py_ssize_t i;
2446 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2447 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002448 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 /* Verify preconditions */
2452 assert(PyAnySet_Check(ob));
2453 assert(PyAnySet_CheckExact(ob));
2454 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 /* so.clear(); so |= set("abc"); */
2457 str = PyUnicode_FromString("abc");
2458 if (str == NULL)
2459 return NULL;
2460 set_clear_internal(so);
2461 if (set_update_internal(so, str) == -1) {
2462 Py_DECREF(str);
2463 return NULL;
2464 }
2465 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 /* Exercise type/size checks */
2468 assert(PySet_Size(ob) == 3);
2469 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 /* Raise TypeError for non-iterable constructor arguments */
2472 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2473 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 /* Raise TypeError for unhashable key */
2476 dup = PySet_New(ob);
2477 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2478 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2479 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 /* Exercise successful pop, contains, add, and discard */
2482 elem = PySet_Pop(ob);
2483 assert(PySet_Contains(ob, elem) == 0);
2484 assert(PySet_GET_SIZE(ob) == 2);
2485 assert(PySet_Add(ob, elem) == 0);
2486 assert(PySet_Contains(ob, elem) == 1);
2487 assert(PySet_GET_SIZE(ob) == 3);
2488 assert(PySet_Discard(ob, elem) == 1);
2489 assert(PySet_GET_SIZE(ob) == 2);
2490 assert(PySet_Discard(ob, elem) == 0);
2491 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 /* Exercise clear */
2494 dup2 = PySet_New(dup);
2495 assert(PySet_Clear(dup2) == 0);
2496 assert(PySet_Size(dup2) == 0);
2497 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 /* Raise SystemError on clear or update of frozen set */
2500 f = PyFrozenSet_New(dup);
2501 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2502 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2503 assert(PySet_Add(f, elem) == 0);
2504 Py_INCREF(f);
2505 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2506 Py_DECREF(f);
2507 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 /* Exercise direct iteration */
2510 i = 0, count = 0;
2511 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2512 s = _PyUnicode_AsString(x);
2513 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2514 count++;
2515 }
2516 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 /* Exercise updates */
2519 dup2 = PySet_New(NULL);
2520 assert(_PySet_Update(dup2, dup) == 0);
2521 assert(PySet_Size(dup2) == 3);
2522 assert(_PySet_Update(dup2, dup) == 0);
2523 assert(PySet_Size(dup2) == 3);
2524 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 /* Raise SystemError when self argument is not a set or frozenset. */
2527 t = PyTuple_New(0);
2528 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2529 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2530 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 /* Raise SystemError when self argument is not a set. */
2533 f = PyFrozenSet_New(dup);
2534 assert(PySet_Size(f) == 3);
2535 assert(PyFrozenSet_CheckExact(f));
2536 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2537 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2538 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 /* Raise KeyError when popping from an empty set */
2541 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2542 Py_DECREF(ob);
2543 assert(PySet_GET_SIZE(ob) == 0);
2544 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 /* Restore the set from the copy using the PyNumber API */
2547 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2548 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 /* Verify constructors accept NULL arguments */
2551 f = PySet_New(NULL);
2552 assert(f != NULL);
2553 assert(PySet_GET_SIZE(f) == 0);
2554 Py_DECREF(f);
2555 f = PyFrozenSet_New(NULL);
2556 assert(f != NULL);
2557 assert(PyFrozenSet_CheckExact(f));
2558 assert(PySet_GET_SIZE(f) == 0);
2559 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 Py_DECREF(elem);
2562 Py_DECREF(dup);
2563 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002564}
2565
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002566#undef assertRaises
2567
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002568#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002569
2570/***** Dummy Struct *************************************************/
2571
2572static PyObject *
2573dummy_repr(PyObject *op)
2574{
2575 return PyUnicode_FromString("<dummy key>");
2576}
2577
2578static void
2579dummy_dealloc(PyObject* ignore)
2580{
2581 Py_FatalError("deallocating <dummy key>");
2582}
2583
2584static PyTypeObject _PySetDummy_Type = {
2585 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2586 "<dummy key> type",
2587 0,
2588 0,
2589 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2590 0, /*tp_print*/
2591 0, /*tp_getattr*/
2592 0, /*tp_setattr*/
2593 0, /*tp_reserved*/
2594 dummy_repr, /*tp_repr*/
2595 0, /*tp_as_number*/
2596 0, /*tp_as_sequence*/
2597 0, /*tp_as_mapping*/
2598 0, /*tp_hash */
2599 0, /*tp_call */
2600 0, /*tp_str */
2601 0, /*tp_getattro */
2602 0, /*tp_setattro */
2603 0, /*tp_as_buffer */
2604 Py_TPFLAGS_DEFAULT, /*tp_flags */
2605};
2606
2607static PyObject _dummy_struct = {
2608 _PyObject_EXTRA_INIT
2609 2, &_PySetDummy_Type
2610};
2611