blob: 98969f5c81cc3c4f7ffaf6c12388235949b33d01 [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070083 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020084 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070085 size_t perturb = hash;
86 size_t mask = so->mask;
87 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior. */
88 size_t j = i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020089 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070092 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070094
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070095 while (1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (entry->key == key)
Raymond Hettingerafe89092013-08-28 20:59:31 -070097 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070098 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070099 PyObject *startkey = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 Py_INCREF(startkey);
101 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
102 Py_DECREF(startkey);
103 if (cmp < 0)
104 return NULL;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700105 if (table != so->table || entry->key != startkey)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 return set_lookkey(so, key, hash);
Raymond Hettingerafe89092013-08-28 20:59:31 -0700107 if (cmp > 0)
108 return entry;
109 }
110 if (entry->key == dummy && freeslot == NULL)
111 freeslot = entry;
112
113 entry = &table[j ^ 1];
114 if (entry->key == NULL)
115 break;
116 if (entry->key == key)
117 return entry;
118 if (entry->hash == hash && entry->key != dummy) {
119 PyObject *startkey = entry->key;
120 Py_INCREF(startkey);
121 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
122 Py_DECREF(startkey);
123 if (cmp < 0)
124 return NULL;
125 if (table != so->table || entry->key != startkey)
126 return set_lookkey(so, key, hash);
127 if (cmp > 0)
128 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 }
Raymond Hettinger07351a02013-08-17 02:39:46 -0700130 if (entry->key == dummy && freeslot == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 freeslot = entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700132
133 i = i * 5 + perturb + 1;
134 j = i & mask;
135 perturb >>= PERTURB_SHIFT;
136
137 entry = &table[j];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700138 if (entry->key == NULL)
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700139 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 }
Raymond Hettingerafe89092013-08-28 20:59:31 -0700141 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000142}
143
144/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000145 * Hacked up version of set_lookkey which can assume keys are always unicode;
146 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000147 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000148 */
149static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200150set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700153 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200154 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -0700155 size_t perturb = hash;
156 size_t mask = so->mask;
157 size_t i = (size_t)hash & mask;
158 size_t j = i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 /* Make sure this function doesn't have to handle non-unicode keys,
161 including subclasses of str; e.g., one reason to subclass
162 strings is to override __eq__, and for speed we don't cater to
163 that here. */
164 if (!PyUnicode_CheckExact(key)) {
165 so->lookup = set_lookkey;
166 return set_lookkey(so, key, hash);
167 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700170 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 return entry;
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000172
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700173 while (1) {
Raymond Hettingerafe89092013-08-28 20:59:31 -0700174 if (entry->key == key
175 || (entry->hash == hash
176 && entry->key != dummy
177 && unicode_eq(entry->key, key)))
178 return entry;
179 if (entry->key == dummy && freeslot == NULL)
180 freeslot = entry;
181
182 entry = &table[j ^ 1];
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700183 if (entry->key == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -0700184 break;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700185 if (entry->key == key
186 || (entry->hash == hash
187 && entry->key != dummy
188 && unicode_eq(entry->key, key)))
189 return entry;
190 if (entry->key == dummy && freeslot == NULL)
191 freeslot = entry;
192
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700193 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700194 j = i & mask;
195 perturb >>= PERTURB_SHIFT;
196
197 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 if (entry->key == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -0700199 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 }
Raymond Hettingerafe89092013-08-28 20:59:31 -0700201 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000202}
203
204/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000205Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000206Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000207Eats a reference to key.
208*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000209static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200210set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000211{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200212 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 assert(so->lookup != NULL);
215 entry = so->lookup(so, key, hash);
216 if (entry == NULL)
217 return -1;
218 if (entry->key == NULL) {
219 /* UNUSED */
220 so->fill++;
221 entry->key = key;
222 entry->hash = hash;
223 so->used++;
224 } else if (entry->key == dummy) {
225 /* DUMMY */
226 entry->key = key;
227 entry->hash = hash;
228 so->used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 } else {
230 /* ACTIVE */
231 Py_DECREF(key);
232 }
233 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234}
235
236/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000237Internal routine used by set_table_resize() to insert an item which is
238known to be absent from the set. This routine also assumes that
239the set contains no deleted entries. Besides the performance benefit,
240using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
241Note that no refcounts are changed by this routine; if needed, the caller
242is responsible for incref'ing `key`.
243*/
244static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200245set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200248 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700249 size_t perturb = hash;
250 size_t mask = (size_t)so->mask;
251 size_t i, j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700253 i = j = (size_t)hash & mask;
254 while (1) {
255 entry = &table[j];
256 if (entry->key == NULL)
257 break;
258 entry = &table[j ^ 1];
259 if (entry->key == NULL)
260 break;
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700261 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700262 j = i & mask;
263 perturb >>= PERTURB_SHIFT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 }
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000269}
270
271/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000273keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274actually be smaller than the old one.
275*/
276static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 Py_ssize_t newsize;
280 setentry *oldtable, *newtable, *entry;
281 Py_ssize_t i;
282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Find the smallest table size > minused. */
288 for (newsize = PySet_MINSIZE;
289 newsize <= minused && newsize > 0;
290 newsize <<= 1)
291 ;
292 if (newsize <= 0) {
293 PyErr_NoMemory();
294 return -1;
295 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL);
300 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (newsize == PySet_MINSIZE) {
303 /* A large table is shrinking, or we can't get any smaller. */
304 newtable = so->smalltable;
305 if (newtable == oldtable) {
306 if (so->fill == so->used) {
307 /* No dummies, so no point doing anything. */
308 return 0;
309 }
310 /* We're not going to resize it, but rebuild the
311 table anyway to purge old dummy entries.
312 Subtle: This is *necessary* if fill==size,
313 as set_lookkey needs at least one virgin slot to
314 terminate failing searches. If fill < size, it's
315 merely desirable, as dummies slow searches. */
316 assert(so->fill > so->used);
317 memcpy(small_copy, oldtable, sizeof(small_copy));
318 oldtable = small_copy;
319 }
320 }
321 else {
322 newtable = PyMem_NEW(setentry, newsize);
323 if (newtable == NULL) {
324 PyErr_NoMemory();
325 return -1;
326 }
327 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* Make the set empty, using the new table. */
330 assert(newtable != oldtable);
331 so->table = newtable;
332 so->mask = newsize - 1;
333 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700334 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
340 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500341 if (entry->key != NULL && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000343 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 }
345 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (is_oldtable_malloced)
348 PyMem_DEL(oldtable);
349 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000350}
351
Raymond Hettingerc991db22005-08-11 07:58:45 +0000352/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
353
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000354static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200355set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200357 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000358 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100359 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 assert(so->fill <= so->mask); /* at least one empty slot */
362 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000363 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100364 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000365 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return -1;
367 }
368 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
369 return 0;
370 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000371}
372
373static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200374set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000375{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200376 Py_hash_t hash;
377 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200380 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 hash = PyObject_Hash(key);
382 if (hash == -1)
383 return -1;
384 }
385 assert(so->fill <= so->mask); /* at least one empty slot */
386 n_used = so->used;
387 Py_INCREF(key);
388 if (set_insert_key(so, key, hash) == -1) {
389 Py_DECREF(key);
390 return -1;
391 }
392 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
393 return 0;
394 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000395}
396
397#define DISCARD_NOTFOUND 0
398#define DISCARD_FOUND 1
399
400static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000401set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200402{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000404
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000405 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 if (entry == NULL)
407 return -1;
408 if (entry->key == NULL || entry->key == dummy)
409 return DISCARD_NOTFOUND;
410 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 entry->key = dummy;
412 so->used--;
413 Py_DECREF(old_key);
414 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000415}
416
417static int
418set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000419{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200420 Py_hash_t hash;
421 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200427 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 hash = PyObject_Hash(key);
429 if (hash == -1)
430 return -1;
431 }
432 entry = (so->lookup)(so, key, hash);
433 if (entry == NULL)
434 return -1;
435 if (entry->key == NULL || entry->key == dummy)
436 return DISCARD_NOTFOUND;
437 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 entry->key = dummy;
439 so->used--;
440 Py_DECREF(old_key);
441 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000442}
443
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000444static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445set_clear_internal(PySetObject *so)
446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 setentry *entry, *table;
448 int table_is_malloced;
449 Py_ssize_t fill;
450 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 Py_ssize_t i, n;
453 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 n = so->mask + 1;
456 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000457#endif
458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 table = so->table;
460 assert(table != NULL);
461 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 /* This is delicate. During the process of clearing the set,
464 * decrefs can cause the set to mutate. To avoid fatal confusion
465 * (voice of experience), we have to make the set empty before
466 * clearing the slots, and never refer to anything via so->ref while
467 * clearing.
468 */
469 fill = so->fill;
470 if (table_is_malloced)
471 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 else if (fill > 0) {
474 /* It's a small table with something that needs to be cleared.
475 * Afraid the only safe way is to copy the set entries into
476 * another small table first.
477 */
478 memcpy(small_copy, table, sizeof(small_copy));
479 table = small_copy;
480 EMPTY_TO_MINSIZE(so);
481 }
482 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 /* Now we can finally clear things. If C had refcounts, we could
485 * assert that the refcount on table is 1 now, i.e. that this function
486 * has unique access to it, so decref side-effects can't alter it.
487 */
488 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000489#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 assert(i < n);
491 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000492#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 if (entry->key) {
494 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700495 if (entry->key != dummy)
496 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 else
500 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 if (table_is_malloced)
505 PyMem_DEL(table);
506 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000507}
508
509/*
510 * Iterate over a set table. Use like so:
511 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000513 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000514 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000515 * while (set_next(yourset, &pos, &entry)) {
516 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000517 * }
518 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000519 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000521 */
522static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000523set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 Py_ssize_t i;
526 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200527 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 assert (PyAnySet_Check(so));
530 i = *pos_ptr;
531 assert(i >= 0);
532 table = so->table;
533 mask = so->mask;
534 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
535 i++;
536 *pos_ptr = i+1;
537 if (i > mask)
538 return 0;
539 assert(table[i].key != NULL);
540 *entry_ptr = &table[i];
541 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000542}
543
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000544static void
545set_dealloc(PySetObject *so)
546{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200547 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 Py_ssize_t fill = so->fill;
549 PyObject_GC_UnTrack(so);
550 Py_TRASHCAN_SAFE_BEGIN(so)
551 if (so->weakreflist != NULL)
552 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 for (entry = so->table; fill > 0; entry++) {
555 if (entry->key) {
556 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700557 if (entry->key != dummy)
558 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 }
560 }
561 if (so->table != so->smalltable)
562 PyMem_DEL(so->table);
563 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
564 free_list[numfree++] = so;
565 else
566 Py_TYPE(so)->tp_free(so);
567 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000568}
569
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000570static PyObject *
571set_repr(PySetObject *so)
572{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200573 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (status != 0) {
577 if (status < 0)
578 return NULL;
579 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
580 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 /* shortcut for the empty set */
583 if (!so->used) {
584 Py_ReprLeave((PyObject*)so);
585 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
586 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 keys = PySequence_List((PyObject *)so);
589 if (keys == NULL)
590 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000591
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 listrepr = PyObject_Repr(keys);
594 Py_DECREF(keys);
595 if (listrepr == NULL)
596 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200597 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200599 if (tmp == NULL)
600 goto done;
601 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200602
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200603 if (Py_TYPE(so) != &PySet_Type)
604 result = PyUnicode_FromFormat("%s({%U})",
605 Py_TYPE(so)->tp_name,
606 listrepr);
607 else
608 result = PyUnicode_FromFormat("{%U}", listrepr);
609 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000610done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 Py_ReprLeave((PyObject*)so);
612 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000613}
614
Martin v. Löwis18e16552006-02-15 17:27:45 +0000615static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000616set_len(PyObject *so)
617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000619}
620
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000621static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000622set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000625 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100626 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200627 Py_ssize_t i;
628 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 assert (PyAnySet_Check(so));
631 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 other = (PySetObject*)otherset;
634 if (other == so || other->used == 0)
635 /* a.update(a) or a.update({}); nothing to do */
636 return 0;
637 /* Do one big resize at the start, rather than
638 * incrementally resizing as we insert new keys. Expect
639 * that there will be no (or few) overlapping keys.
640 */
641 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
642 if (set_table_resize(so, (so->used + other->used)*2) != 0)
643 return -1;
644 }
645 for (i = 0; i <= other->mask; i++) {
646 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000647 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100648 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000649 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500650 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000651 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100652 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000653 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 return -1;
655 }
656 }
657 }
658 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000659}
660
661static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000662set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000663{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000664 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200668 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 hash = PyObject_Hash(key);
670 if (hash == -1)
671 return -1;
672 }
673 entry = (so->lookup)(so, key, hash);
674 if (entry == NULL)
675 return -1;
676 key = entry->key;
677 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000678}
679
Raymond Hettingerc991db22005-08-11 07:58:45 +0000680static int
681set_contains_entry(PySetObject *so, setentry *entry)
682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 PyObject *key;
684 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000685
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000686 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 if (lu_entry == NULL)
688 return -1;
689 key = lu_entry->key;
690 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000691}
692
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000693static PyObject *
694set_pop(PySetObject *so)
695{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200696 Py_ssize_t i = 0;
697 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 assert (PyAnySet_Check(so));
701 if (so->used == 0) {
702 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
703 return NULL;
704 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 /* Set entry to "the first" unused or dummy set entry. We abuse
707 * the hash field of slot 0 to hold a search finger:
708 * If slot 0 has a value, use slot 0.
709 * Else slot 0 is being used to hold a search finger,
710 * and we use its hash value as the first index to look.
711 */
712 entry = &so->table[0];
713 if (entry->key == NULL || entry->key == dummy) {
714 i = entry->hash;
715 /* The hash field may be a real hash value, or it may be a
716 * legit search finger, or it may be a once-legit search
717 * finger that's out of bounds now because it wrapped around
718 * or the table shrunk -- simply make sure it's in bounds now.
719 */
720 if (i > so->mask || i < 1)
721 i = 1; /* skip slot 0 */
722 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
723 i++;
724 if (i > so->mask)
725 i = 1;
726 }
727 }
728 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 entry->key = dummy;
730 so->used--;
731 so->table[0].hash = i + 1; /* next place to start */
732 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000733}
734
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000735PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
736Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000737
738static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000739set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 Py_ssize_t pos = 0;
742 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 while (set_next(so, &pos, &entry))
745 Py_VISIT(entry->key);
746 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000747}
748
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000749static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000750frozenset_hash(PyObject *self)
751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800753 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 setentry *entry;
755 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (so->hash != -1)
758 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000759
Mark Dickinson57e683e2011-09-24 18:18:40 +0100760 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 while (set_next(so, &pos, &entry)) {
762 /* Work to increase the bit dispersion for closely spaced hash
763 values. The is important because some use cases have many
764 combinations of a small number of elements with nearby
765 hashes so that many distinct combinations collapse to only
766 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000767 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800768 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800770 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800772 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 so->hash = hash;
774 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000775}
776
Raymond Hettingera9d99362005-08-05 00:01:15 +0000777/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000778
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000779typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 PyObject_HEAD
781 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
782 Py_ssize_t si_used;
783 Py_ssize_t si_pos;
784 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000785} setiterobject;
786
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787static void
788setiter_dealloc(setiterobject *si)
789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 Py_XDECREF(si->si_set);
791 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000792}
793
794static int
795setiter_traverse(setiterobject *si, visitproc visit, void *arg)
796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 Py_VISIT(si->si_set);
798 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799}
800
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000801static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000802setiter_len(setiterobject *si)
803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 Py_ssize_t len = 0;
805 if (si->si_set != NULL && si->si_used == si->si_set->used)
806 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000807 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000808}
809
Armin Rigof5b3e362006-02-11 21:32:43 +0000810PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000811
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000812static PyObject *setiter_iternext(setiterobject *si);
813
814static PyObject *
815setiter_reduce(setiterobject *si)
816{
817 PyObject *list;
818 setiterobject tmp;
819
820 list = PyList_New(0);
821 if (!list)
822 return NULL;
823
Ezio Melotti0e1af282012-09-28 16:43:40 +0300824 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000825 tmp = *si;
826 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300827
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000828 /* iterate the temporary into a list */
829 for(;;) {
830 PyObject *element = setiter_iternext(&tmp);
831 if (element) {
832 if (PyList_Append(list, element)) {
833 Py_DECREF(element);
834 Py_DECREF(list);
835 Py_XDECREF(tmp.si_set);
836 return NULL;
837 }
838 Py_DECREF(element);
839 } else
840 break;
841 }
842 Py_XDECREF(tmp.si_set);
843 /* check for error */
844 if (tmp.si_set != NULL) {
845 /* we have an error */
846 Py_DECREF(list);
847 return NULL;
848 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200849 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000850}
851
852PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
853
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000854static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000856 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000858};
859
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000860static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200863 Py_ssize_t i, mask;
864 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (so == NULL)
868 return NULL;
869 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 if (si->si_used != so->used) {
872 PyErr_SetString(PyExc_RuntimeError,
873 "Set changed size during iteration");
874 si->si_used = -1; /* Make this state sticky */
875 return NULL;
876 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 i = si->si_pos;
879 assert(i>=0);
880 entry = so->table;
881 mask = so->mask;
882 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
883 i++;
884 si->si_pos = i+1;
885 if (i > mask)
886 goto fail;
887 si->len--;
888 key = entry[i].key;
889 Py_INCREF(key);
890 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891
892fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 Py_DECREF(so);
894 si->si_set = NULL;
895 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000896}
897
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000898PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 PyVarObject_HEAD_INIT(&PyType_Type, 0)
900 "set_iterator", /* tp_name */
901 sizeof(setiterobject), /* tp_basicsize */
902 0, /* tp_itemsize */
903 /* methods */
904 (destructor)setiter_dealloc, /* tp_dealloc */
905 0, /* tp_print */
906 0, /* tp_getattr */
907 0, /* tp_setattr */
908 0, /* tp_reserved */
909 0, /* tp_repr */
910 0, /* tp_as_number */
911 0, /* tp_as_sequence */
912 0, /* tp_as_mapping */
913 0, /* tp_hash */
914 0, /* tp_call */
915 0, /* tp_str */
916 PyObject_GenericGetAttr, /* tp_getattro */
917 0, /* tp_setattro */
918 0, /* tp_as_buffer */
919 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
920 0, /* tp_doc */
921 (traverseproc)setiter_traverse, /* tp_traverse */
922 0, /* tp_clear */
923 0, /* tp_richcompare */
924 0, /* tp_weaklistoffset */
925 PyObject_SelfIter, /* tp_iter */
926 (iternextfunc)setiter_iternext, /* tp_iternext */
927 setiter_methods, /* tp_methods */
928 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000929};
930
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000931static PyObject *
932set_iter(PySetObject *so)
933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
935 if (si == NULL)
936 return NULL;
937 Py_INCREF(so);
938 si->si_set = so;
939 si->si_used = so->used;
940 si->si_pos = 0;
941 si->len = so->used;
942 _PyObject_GC_TRACK(si);
943 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000944}
945
Raymond Hettingerd7946662005-08-01 21:39:29 +0000946static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000947set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 if (PyAnySet_Check(other))
952 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (PyDict_CheckExact(other)) {
955 PyObject *value;
956 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000957 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 /* Do one big resize at the start, rather than
961 * incrementally resizing as we insert new keys. Expect
962 * that there will be no (or few) overlapping keys.
963 */
964 if (dictsize == -1)
965 return -1;
966 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
967 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
968 return -1;
969 }
970 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
971 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 an_entry.hash = hash;
974 an_entry.key = key;
975 if (set_add_entry(so, &an_entry) == -1)
976 return -1;
977 }
978 return 0;
979 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 it = PyObject_GetIter(other);
982 if (it == NULL)
983 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 while ((key = PyIter_Next(it)) != NULL) {
986 if (set_add_key(so, key) == -1) {
987 Py_DECREF(it);
988 Py_DECREF(key);
989 return -1;
990 }
991 Py_DECREF(key);
992 }
993 Py_DECREF(it);
994 if (PyErr_Occurred())
995 return -1;
996 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000997}
998
999static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001000set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1005 PyObject *other = PyTuple_GET_ITEM(args, i);
1006 if (set_update_internal(so, other) == -1)
1007 return NULL;
1008 }
1009 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001010}
1011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001013"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001014
1015static PyObject *
1016make_new_set(PyTypeObject *type, PyObject *iterable)
1017{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001018 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 /* create PySetObject structure */
1021 if (numfree &&
1022 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1023 so = free_list[--numfree];
1024 assert (so != NULL && PyAnySet_CheckExact(so));
1025 Py_TYPE(so) = type;
1026 _Py_NewReference((PyObject *)so);
1027 EMPTY_TO_MINSIZE(so);
1028 PyObject_GC_Track(so);
1029 } else {
1030 so = (PySetObject *)type->tp_alloc(type, 0);
1031 if (so == NULL)
1032 return NULL;
1033 /* tp_alloc has already zeroed the structure */
1034 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1035 INIT_NONZERO_SET_SLOTS(so);
1036 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 so->lookup = set_lookkey_unicode;
1039 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (iterable != NULL) {
1042 if (set_update_internal(so, iterable) == -1) {
1043 Py_DECREF(so);
1044 return NULL;
1045 }
1046 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001049}
1050
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001051static PyObject *
1052make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1055 if (PyType_IsSubtype(type, &PySet_Type))
1056 type = &PySet_Type;
1057 else
1058 type = &PyFrozenSet_Type;
1059 }
1060 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001061}
1062
Raymond Hettingerd7946662005-08-01 21:39:29 +00001063/* The empty frozenset is a singleton */
1064static PyObject *emptyfrozenset = NULL;
1065
Raymond Hettingera690a992003-11-16 16:17:49 +00001066static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001067frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1072 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1075 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (type != &PyFrozenSet_Type)
1078 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (iterable != NULL) {
1081 /* frozenset(f) is idempotent */
1082 if (PyFrozenSet_CheckExact(iterable)) {
1083 Py_INCREF(iterable);
1084 return iterable;
1085 }
1086 result = make_new_set(type, iterable);
1087 if (result == NULL || PySet_GET_SIZE(result))
1088 return result;
1089 Py_DECREF(result);
1090 }
1091 /* The empty frozenset is a singleton */
1092 if (emptyfrozenset == NULL)
1093 emptyfrozenset = make_new_set(type, NULL);
1094 Py_XINCREF(emptyfrozenset);
1095 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001096}
1097
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001098int
1099PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001100{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001101 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 while (numfree) {
1105 numfree--;
1106 so = free_list[numfree];
1107 PyObject_GC_Del(so);
1108 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001109 return freelist_size;
1110}
1111
1112void
1113PySet_Fini(void)
1114{
1115 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001117}
1118
David Malcolm49526f42012-06-22 14:55:41 -04001119/* Print summary info about the state of the optimized allocator */
1120void
1121_PySet_DebugMallocStats(FILE *out)
1122{
1123 _PyDebugAllocatorStats(out,
1124 "free PySetObject",
1125 numfree, sizeof(PySetObject));
1126}
1127
1128
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001129static PyObject *
1130set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1133 return NULL;
1134
1135 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001136}
1137
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001138/* set_swap_bodies() switches the contents of any two sets by moving their
1139 internal data pointers and, if needed, copying the internal smalltables.
1140 Semantically equivalent to:
1141
1142 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1143
1144 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001146 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001148 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001149*/
1150
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001151static void
1152set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 Py_ssize_t t;
1155 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001156 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001158 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 t = a->fill; a->fill = b->fill; b->fill = t;
1161 t = a->used; a->used = b->used; b->used = t;
1162 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 u = a->table;
1165 if (a->table == a->smalltable)
1166 u = b->smalltable;
1167 a->table = b->table;
1168 if (b->table == b->smalltable)
1169 a->table = a->smalltable;
1170 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 if (a->table == a->smalltable || b->table == b->smalltable) {
1175 memcpy(tab, a->smalltable, sizeof(tab));
1176 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1177 memcpy(b->smalltable, tab, sizeof(tab));
1178 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1181 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1182 h = a->hash; a->hash = b->hash; b->hash = h;
1183 } else {
1184 a->hash = -1;
1185 b->hash = -1;
1186 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001187}
1188
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001189static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001190set_copy(PySetObject *so)
1191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001193}
1194
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001195static PyObject *
1196frozenset_copy(PySetObject *so)
1197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 if (PyFrozenSet_CheckExact(so)) {
1199 Py_INCREF(so);
1200 return (PyObject *)so;
1201 }
1202 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001203}
1204
Raymond Hettingera690a992003-11-16 16:17:49 +00001205PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1206
1207static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001208set_clear(PySetObject *so)
1209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 set_clear_internal(so);
1211 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001212}
1213
1214PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1215
1216static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001217set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 PySetObject *result;
1220 PyObject *other;
1221 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 result = (PySetObject *)set_copy(so);
1224 if (result == NULL)
1225 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1228 other = PyTuple_GET_ITEM(args, i);
1229 if ((PyObject *)so == other)
1230 continue;
1231 if (set_update_internal(result, other) == -1) {
1232 Py_DECREF(result);
1233 return NULL;
1234 }
1235 }
1236 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001237}
1238
1239PyDoc_STRVAR(union_doc,
1240 "Return the union of sets as a new set.\n\
1241\n\
1242(i.e. all elements that are in either set.)");
1243
1244static PyObject *
1245set_or(PySetObject *so, PyObject *other)
1246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001248
Brian Curtindfc80e32011-08-10 20:28:54 -05001249 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1250 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 result = (PySetObject *)set_copy(so);
1253 if (result == NULL)
1254 return NULL;
1255 if ((PyObject *)so == other)
1256 return (PyObject *)result;
1257 if (set_update_internal(result, other) == -1) {
1258 Py_DECREF(result);
1259 return NULL;
1260 }
1261 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001262}
1263
Raymond Hettingera690a992003-11-16 16:17:49 +00001264static PyObject *
1265set_ior(PySetObject *so, PyObject *other)
1266{
Brian Curtindfc80e32011-08-10 20:28:54 -05001267 if (!PyAnySet_Check(other))
1268 Py_RETURN_NOTIMPLEMENTED;
1269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (set_update_internal(so, other) == -1)
1271 return NULL;
1272 Py_INCREF(so);
1273 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001274}
1275
1276static PyObject *
1277set_intersection(PySetObject *so, PyObject *other)
1278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 PySetObject *result;
1280 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if ((PyObject *)so == other)
1283 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1286 if (result == NULL)
1287 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if (PyAnySet_Check(other)) {
1290 Py_ssize_t pos = 0;
1291 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1294 tmp = (PyObject *)so;
1295 so = (PySetObject *)other;
1296 other = tmp;
1297 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 while (set_next((PySetObject *)other, &pos, &entry)) {
1300 int rv = set_contains_entry(so, entry);
1301 if (rv == -1) {
1302 Py_DECREF(result);
1303 return NULL;
1304 }
1305 if (rv) {
1306 if (set_add_entry(result, entry) == -1) {
1307 Py_DECREF(result);
1308 return NULL;
1309 }
1310 }
1311 }
1312 return (PyObject *)result;
1313 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 it = PyObject_GetIter(other);
1316 if (it == NULL) {
1317 Py_DECREF(result);
1318 return NULL;
1319 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 while ((key = PyIter_Next(it)) != NULL) {
1322 int rv;
1323 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001324 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 if (hash == -1) {
1327 Py_DECREF(it);
1328 Py_DECREF(result);
1329 Py_DECREF(key);
1330 return NULL;
1331 }
1332 entry.hash = hash;
1333 entry.key = key;
1334 rv = set_contains_entry(so, &entry);
1335 if (rv == -1) {
1336 Py_DECREF(it);
1337 Py_DECREF(result);
1338 Py_DECREF(key);
1339 return NULL;
1340 }
1341 if (rv) {
1342 if (set_add_entry(result, &entry) == -1) {
1343 Py_DECREF(it);
1344 Py_DECREF(result);
1345 Py_DECREF(key);
1346 return NULL;
1347 }
1348 }
1349 Py_DECREF(key);
1350 }
1351 Py_DECREF(it);
1352 if (PyErr_Occurred()) {
1353 Py_DECREF(result);
1354 return NULL;
1355 }
1356 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001357}
1358
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001359static PyObject *
1360set_intersection_multi(PySetObject *so, PyObject *args)
1361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 Py_ssize_t i;
1363 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 if (PyTuple_GET_SIZE(args) == 0)
1366 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 Py_INCREF(so);
1369 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1370 PyObject *other = PyTuple_GET_ITEM(args, i);
1371 PyObject *newresult = set_intersection((PySetObject *)result, other);
1372 if (newresult == NULL) {
1373 Py_DECREF(result);
1374 return NULL;
1375 }
1376 Py_DECREF(result);
1377 result = newresult;
1378 }
1379 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001380}
1381
Raymond Hettingera690a992003-11-16 16:17:49 +00001382PyDoc_STRVAR(intersection_doc,
1383"Return the intersection of two sets as a new set.\n\
1384\n\
1385(i.e. all elements that are in both sets.)");
1386
1387static PyObject *
1388set_intersection_update(PySetObject *so, PyObject *other)
1389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 tmp = set_intersection(so, other);
1393 if (tmp == NULL)
1394 return NULL;
1395 set_swap_bodies(so, (PySetObject *)tmp);
1396 Py_DECREF(tmp);
1397 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001398}
1399
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001400static PyObject *
1401set_intersection_update_multi(PySetObject *so, PyObject *args)
1402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 tmp = set_intersection_multi(so, args);
1406 if (tmp == NULL)
1407 return NULL;
1408 set_swap_bodies(so, (PySetObject *)tmp);
1409 Py_DECREF(tmp);
1410 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001411}
1412
Raymond Hettingera690a992003-11-16 16:17:49 +00001413PyDoc_STRVAR(intersection_update_doc,
1414"Update a set with the intersection of itself and another.");
1415
1416static PyObject *
1417set_and(PySetObject *so, PyObject *other)
1418{
Brian Curtindfc80e32011-08-10 20:28:54 -05001419 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1420 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001422}
1423
1424static PyObject *
1425set_iand(PySetObject *so, PyObject *other)
1426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001428
Brian Curtindfc80e32011-08-10 20:28:54 -05001429 if (!PyAnySet_Check(other))
1430 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 result = set_intersection_update(so, other);
1432 if (result == NULL)
1433 return NULL;
1434 Py_DECREF(result);
1435 Py_INCREF(so);
1436 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001437}
1438
Guido van Rossum58da9312007-11-10 23:39:45 +00001439static PyObject *
1440set_isdisjoint(PySetObject *so, PyObject *other)
1441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 if ((PyObject *)so == other) {
1445 if (PySet_GET_SIZE(so) == 0)
1446 Py_RETURN_TRUE;
1447 else
1448 Py_RETURN_FALSE;
1449 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 if (PyAnySet_CheckExact(other)) {
1452 Py_ssize_t pos = 0;
1453 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1456 tmp = (PyObject *)so;
1457 so = (PySetObject *)other;
1458 other = tmp;
1459 }
1460 while (set_next((PySetObject *)other, &pos, &entry)) {
1461 int rv = set_contains_entry(so, entry);
1462 if (rv == -1)
1463 return NULL;
1464 if (rv)
1465 Py_RETURN_FALSE;
1466 }
1467 Py_RETURN_TRUE;
1468 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 it = PyObject_GetIter(other);
1471 if (it == NULL)
1472 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 while ((key = PyIter_Next(it)) != NULL) {
1475 int rv;
1476 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001477 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (hash == -1) {
1480 Py_DECREF(key);
1481 Py_DECREF(it);
1482 return NULL;
1483 }
1484 entry.hash = hash;
1485 entry.key = key;
1486 rv = set_contains_entry(so, &entry);
1487 Py_DECREF(key);
1488 if (rv == -1) {
1489 Py_DECREF(it);
1490 return NULL;
1491 }
1492 if (rv) {
1493 Py_DECREF(it);
1494 Py_RETURN_FALSE;
1495 }
1496 }
1497 Py_DECREF(it);
1498 if (PyErr_Occurred())
1499 return NULL;
1500 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001501}
1502
1503PyDoc_STRVAR(isdisjoint_doc,
1504"Return True if two sets have a null intersection.");
1505
Neal Norwitz6576bd82005-11-13 18:41:28 +00001506static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001507set_difference_update_internal(PySetObject *so, PyObject *other)
1508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 if ((PyObject *)so == other)
1510 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 if (PyAnySet_Check(other)) {
1513 setentry *entry;
1514 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 while (set_next((PySetObject *)other, &pos, &entry))
1517 if (set_discard_entry(so, entry) == -1)
1518 return -1;
1519 } else {
1520 PyObject *key, *it;
1521 it = PyObject_GetIter(other);
1522 if (it == NULL)
1523 return -1;
1524
1525 while ((key = PyIter_Next(it)) != NULL) {
1526 if (set_discard_key(so, key) == -1) {
1527 Py_DECREF(it);
1528 Py_DECREF(key);
1529 return -1;
1530 }
1531 Py_DECREF(key);
1532 }
1533 Py_DECREF(it);
1534 if (PyErr_Occurred())
1535 return -1;
1536 }
1537 /* If more than 1/5 are dummies, then resize them away. */
1538 if ((so->fill - so->used) * 5 < so->mask)
1539 return 0;
1540 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001541}
1542
Raymond Hettingera690a992003-11-16 16:17:49 +00001543static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001544set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1549 PyObject *other = PyTuple_GET_ITEM(args, i);
1550 if (set_difference_update_internal(so, other) == -1)
1551 return NULL;
1552 }
1553 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001554}
1555
1556PyDoc_STRVAR(difference_update_doc,
1557"Remove all elements of another set from this set.");
1558
1559static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001560set_copy_and_difference(PySetObject *so, PyObject *other)
1561{
1562 PyObject *result;
1563
1564 result = set_copy(so);
1565 if (result == NULL)
1566 return NULL;
1567 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1568 return result;
1569 Py_DECREF(result);
1570 return NULL;
1571}
1572
1573static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001574set_difference(PySetObject *so, PyObject *other)
1575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 PyObject *result;
1577 setentry *entry;
1578 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001581 return set_copy_and_difference(so, other);
1582 }
1583
1584 /* If len(so) much more than len(other), it's more efficient to simply copy
1585 * so and then iterate other looking for common elements. */
1586 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1587 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 result = make_new_set_basetype(Py_TYPE(so), NULL);
1591 if (result == NULL)
1592 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (PyDict_CheckExact(other)) {
1595 while (set_next(so, &pos, &entry)) {
1596 setentry entrycopy;
1597 entrycopy.hash = entry->hash;
1598 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001599 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1601 Py_DECREF(result);
1602 return NULL;
1603 }
1604 }
1605 }
1606 return result;
1607 }
1608
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001609 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 while (set_next(so, &pos, &entry)) {
1611 int rv = set_contains_entry((PySetObject *)other, entry);
1612 if (rv == -1) {
1613 Py_DECREF(result);
1614 return NULL;
1615 }
1616 if (!rv) {
1617 if (set_add_entry((PySetObject *)result, entry) == -1) {
1618 Py_DECREF(result);
1619 return NULL;
1620 }
1621 }
1622 }
1623 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001624}
1625
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001626static PyObject *
1627set_difference_multi(PySetObject *so, PyObject *args)
1628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 Py_ssize_t i;
1630 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 if (PyTuple_GET_SIZE(args) == 0)
1633 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 other = PyTuple_GET_ITEM(args, 0);
1636 result = set_difference(so, other);
1637 if (result == NULL)
1638 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1641 other = PyTuple_GET_ITEM(args, i);
1642 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1643 Py_DECREF(result);
1644 return NULL;
1645 }
1646 }
1647 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001648}
1649
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001650PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001651"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001652\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001653(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001654static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001655set_sub(PySetObject *so, PyObject *other)
1656{
Brian Curtindfc80e32011-08-10 20:28:54 -05001657 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1658 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001660}
1661
1662static PyObject *
1663set_isub(PySetObject *so, PyObject *other)
1664{
Brian Curtindfc80e32011-08-10 20:28:54 -05001665 if (!PyAnySet_Check(other))
1666 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 if (set_difference_update_internal(so, other) == -1)
1668 return NULL;
1669 Py_INCREF(so);
1670 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001671}
1672
1673static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001674set_symmetric_difference_update(PySetObject *so, PyObject *other)
1675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 PySetObject *otherset;
1677 PyObject *key;
1678 Py_ssize_t pos = 0;
1679 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 if ((PyObject *)so == other)
1682 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (PyDict_CheckExact(other)) {
1685 PyObject *value;
1686 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001687 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1689 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001690
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001691 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 an_entry.hash = hash;
1693 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001696 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001697 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001700 if (rv == DISCARD_NOTFOUND) {
1701 if (set_add_entry(so, &an_entry) == -1) {
1702 Py_DECREF(key);
1703 return NULL;
1704 }
1705 }
Georg Brandl2d444492010-09-03 10:52:55 +00001706 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 }
1708 Py_RETURN_NONE;
1709 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 if (PyAnySet_Check(other)) {
1712 Py_INCREF(other);
1713 otherset = (PySetObject *)other;
1714 } else {
1715 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1716 if (otherset == NULL)
1717 return NULL;
1718 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 while (set_next(otherset, &pos, &entry)) {
1721 int rv = set_discard_entry(so, entry);
1722 if (rv == -1) {
1723 Py_DECREF(otherset);
1724 return NULL;
1725 }
1726 if (rv == DISCARD_NOTFOUND) {
1727 if (set_add_entry(so, entry) == -1) {
1728 Py_DECREF(otherset);
1729 return NULL;
1730 }
1731 }
1732 }
1733 Py_DECREF(otherset);
1734 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001735}
1736
1737PyDoc_STRVAR(symmetric_difference_update_doc,
1738"Update a set with the symmetric difference of itself and another.");
1739
1740static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001741set_symmetric_difference(PySetObject *so, PyObject *other)
1742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 PyObject *rv;
1744 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1747 if (otherset == NULL)
1748 return NULL;
1749 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1750 if (rv == NULL)
1751 return NULL;
1752 Py_DECREF(rv);
1753 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001754}
1755
1756PyDoc_STRVAR(symmetric_difference_doc,
1757"Return the symmetric difference of two sets as a new set.\n\
1758\n\
1759(i.e. all elements that are in exactly one of the sets.)");
1760
1761static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001762set_xor(PySetObject *so, PyObject *other)
1763{
Brian Curtindfc80e32011-08-10 20:28:54 -05001764 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1765 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001767}
1768
1769static PyObject *
1770set_ixor(PySetObject *so, PyObject *other)
1771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001773
Brian Curtindfc80e32011-08-10 20:28:54 -05001774 if (!PyAnySet_Check(other))
1775 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 result = set_symmetric_difference_update(so, other);
1777 if (result == NULL)
1778 return NULL;
1779 Py_DECREF(result);
1780 Py_INCREF(so);
1781 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001782}
1783
1784static PyObject *
1785set_issubset(PySetObject *so, PyObject *other)
1786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 setentry *entry;
1788 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 if (!PyAnySet_Check(other)) {
1791 PyObject *tmp, *result;
1792 tmp = make_new_set(&PySet_Type, other);
1793 if (tmp == NULL)
1794 return NULL;
1795 result = set_issubset(so, tmp);
1796 Py_DECREF(tmp);
1797 return result;
1798 }
1799 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1800 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 while (set_next(so, &pos, &entry)) {
1803 int rv = set_contains_entry((PySetObject *)other, entry);
1804 if (rv == -1)
1805 return NULL;
1806 if (!rv)
1807 Py_RETURN_FALSE;
1808 }
1809 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001810}
1811
1812PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1813
1814static PyObject *
1815set_issuperset(PySetObject *so, PyObject *other)
1816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 if (!PyAnySet_Check(other)) {
1820 tmp = make_new_set(&PySet_Type, other);
1821 if (tmp == NULL)
1822 return NULL;
1823 result = set_issuperset(so, tmp);
1824 Py_DECREF(tmp);
1825 return result;
1826 }
1827 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001828}
1829
1830PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1831
Raymond Hettingera690a992003-11-16 16:17:49 +00001832static PyObject *
1833set_richcompare(PySetObject *v, PyObject *w, int op)
1834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001836
Brian Curtindfc80e32011-08-10 20:28:54 -05001837 if(!PyAnySet_Check(w))
1838 Py_RETURN_NOTIMPLEMENTED;
1839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 switch (op) {
1841 case Py_EQ:
1842 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1843 Py_RETURN_FALSE;
1844 if (v->hash != -1 &&
1845 ((PySetObject *)w)->hash != -1 &&
1846 v->hash != ((PySetObject *)w)->hash)
1847 Py_RETURN_FALSE;
1848 return set_issubset(v, w);
1849 case Py_NE:
1850 r1 = set_richcompare(v, w, Py_EQ);
1851 if (r1 == NULL)
1852 return NULL;
1853 r2 = PyBool_FromLong(PyObject_Not(r1));
1854 Py_DECREF(r1);
1855 return r2;
1856 case Py_LE:
1857 return set_issubset(v, w);
1858 case Py_GE:
1859 return set_issuperset(v, w);
1860 case Py_LT:
1861 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1862 Py_RETURN_FALSE;
1863 return set_issubset(v, w);
1864 case Py_GT:
1865 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1866 Py_RETURN_FALSE;
1867 return set_issuperset(v, w);
1868 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001869 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001870}
1871
Raymond Hettingera690a992003-11-16 16:17:49 +00001872static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001873set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 if (set_add_key(so, key) == -1)
1876 return NULL;
1877 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001878}
1879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001881"Add an element to a set.\n\
1882\n\
1883This has no effect if the element is already present.");
1884
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001885static int
1886set_contains(PySetObject *so, PyObject *key)
1887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 PyObject *tmpkey;
1889 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 rv = set_contains_key(so, key);
1892 if (rv == -1) {
1893 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1894 return -1;
1895 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001896 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 if (tmpkey == NULL)
1898 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001899 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 Py_DECREF(tmpkey);
1901 }
1902 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001903}
1904
1905static PyObject *
1906set_direct_contains(PySetObject *so, PyObject *key)
1907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 result = set_contains(so, key);
1911 if (result == -1)
1912 return NULL;
1913 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001914}
1915
1916PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1917
Raymond Hettingera690a992003-11-16 16:17:49 +00001918static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001919set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 PyObject *tmpkey;
1922 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 rv = set_discard_key(so, key);
1925 if (rv == -1) {
1926 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1927 return NULL;
1928 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001929 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 if (tmpkey == NULL)
1931 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 Py_DECREF(tmpkey);
1934 if (rv == -1)
1935 return NULL;
1936 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 if (rv == DISCARD_NOTFOUND) {
1939 set_key_error(key);
1940 return NULL;
1941 }
1942 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001943}
1944
1945PyDoc_STRVAR(remove_doc,
1946"Remove an element from a set; it must be a member.\n\
1947\n\
1948If the element is not a member, raise a KeyError.");
1949
1950static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001951set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001952{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001953 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 rv = set_discard_key(so, key);
1957 if (rv == -1) {
1958 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1959 return NULL;
1960 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001961 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 if (tmpkey == NULL)
1963 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001964 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001966 if (rv == -1)
1967 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 }
1969 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001970}
1971
1972PyDoc_STRVAR(discard_doc,
1973"Remove an element from a set if it is a member.\n\
1974\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001976
1977static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001978set_reduce(PySetObject *so)
1979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001981 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 keys = PySequence_List((PyObject *)so);
1984 if (keys == NULL)
1985 goto done;
1986 args = PyTuple_Pack(1, keys);
1987 if (args == NULL)
1988 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001989 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 if (dict == NULL) {
1991 PyErr_Clear();
1992 dict = Py_None;
1993 Py_INCREF(dict);
1994 }
1995 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001996done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 Py_XDECREF(args);
1998 Py_XDECREF(keys);
1999 Py_XDECREF(dict);
2000 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002001}
2002
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002003static PyObject *
2004set_sizeof(PySetObject *so)
2005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 res = sizeof(PySetObject);
2009 if (so->table != so->smalltable)
2010 res = res + (so->mask + 1) * sizeof(setentry);
2011 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002012}
2013
2014PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002015static int
2016set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 if (!PyAnySet_Check(self))
2021 return -1;
2022 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2023 return -1;
2024 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2025 return -1;
2026 set_clear_internal(self);
2027 self->hash = -1;
2028 if (iterable == NULL)
2029 return 0;
2030 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002031}
2032
Raymond Hettingera690a992003-11-16 16:17:49 +00002033static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 set_len, /* sq_length */
2035 0, /* sq_concat */
2036 0, /* sq_repeat */
2037 0, /* sq_item */
2038 0, /* sq_slice */
2039 0, /* sq_ass_item */
2040 0, /* sq_ass_slice */
2041 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002042};
2043
2044/* set object ********************************************************/
2045
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002046#ifdef Py_DEBUG
2047static PyObject *test_c_api(PySetObject *so);
2048
2049PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2050All is well if assertions don't fail.");
2051#endif
2052
Raymond Hettingera690a992003-11-16 16:17:49 +00002053static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 {"add", (PyCFunction)set_add, METH_O,
2055 add_doc},
2056 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2057 clear_doc},
2058 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2059 contains_doc},
2060 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2061 copy_doc},
2062 {"discard", (PyCFunction)set_discard, METH_O,
2063 discard_doc},
2064 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2065 difference_doc},
2066 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2067 difference_update_doc},
2068 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2069 intersection_doc},
2070 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2071 intersection_update_doc},
2072 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2073 isdisjoint_doc},
2074 {"issubset", (PyCFunction)set_issubset, METH_O,
2075 issubset_doc},
2076 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2077 issuperset_doc},
2078 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2079 pop_doc},
2080 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2081 reduce_doc},
2082 {"remove", (PyCFunction)set_remove, METH_O,
2083 remove_doc},
2084 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2085 sizeof_doc},
2086 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2087 symmetric_difference_doc},
2088 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2089 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002090#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2092 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002093#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 {"union", (PyCFunction)set_union, METH_VARARGS,
2095 union_doc},
2096 {"update", (PyCFunction)set_update, METH_VARARGS,
2097 update_doc},
2098 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002099};
2100
2101static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 0, /*nb_add*/
2103 (binaryfunc)set_sub, /*nb_subtract*/
2104 0, /*nb_multiply*/
2105 0, /*nb_remainder*/
2106 0, /*nb_divmod*/
2107 0, /*nb_power*/
2108 0, /*nb_negative*/
2109 0, /*nb_positive*/
2110 0, /*nb_absolute*/
2111 0, /*nb_bool*/
2112 0, /*nb_invert*/
2113 0, /*nb_lshift*/
2114 0, /*nb_rshift*/
2115 (binaryfunc)set_and, /*nb_and*/
2116 (binaryfunc)set_xor, /*nb_xor*/
2117 (binaryfunc)set_or, /*nb_or*/
2118 0, /*nb_int*/
2119 0, /*nb_reserved*/
2120 0, /*nb_float*/
2121 0, /*nb_inplace_add*/
2122 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2123 0, /*nb_inplace_multiply*/
2124 0, /*nb_inplace_remainder*/
2125 0, /*nb_inplace_power*/
2126 0, /*nb_inplace_lshift*/
2127 0, /*nb_inplace_rshift*/
2128 (binaryfunc)set_iand, /*nb_inplace_and*/
2129 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2130 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002131};
2132
2133PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002134"set() -> new empty set object\n\
2135set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002136\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002137Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002138
2139PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2141 "set", /* tp_name */
2142 sizeof(PySetObject), /* tp_basicsize */
2143 0, /* tp_itemsize */
2144 /* methods */
2145 (destructor)set_dealloc, /* tp_dealloc */
2146 0, /* tp_print */
2147 0, /* tp_getattr */
2148 0, /* tp_setattr */
2149 0, /* tp_reserved */
2150 (reprfunc)set_repr, /* tp_repr */
2151 &set_as_number, /* tp_as_number */
2152 &set_as_sequence, /* tp_as_sequence */
2153 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002154 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 0, /* tp_call */
2156 0, /* tp_str */
2157 PyObject_GenericGetAttr, /* tp_getattro */
2158 0, /* tp_setattro */
2159 0, /* tp_as_buffer */
2160 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2161 Py_TPFLAGS_BASETYPE, /* tp_flags */
2162 set_doc, /* tp_doc */
2163 (traverseproc)set_traverse, /* tp_traverse */
2164 (inquiry)set_clear_internal, /* tp_clear */
2165 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002166 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2167 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 0, /* tp_iternext */
2169 set_methods, /* tp_methods */
2170 0, /* tp_members */
2171 0, /* tp_getset */
2172 0, /* tp_base */
2173 0, /* tp_dict */
2174 0, /* tp_descr_get */
2175 0, /* tp_descr_set */
2176 0, /* tp_dictoffset */
2177 (initproc)set_init, /* tp_init */
2178 PyType_GenericAlloc, /* tp_alloc */
2179 set_new, /* tp_new */
2180 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002181};
2182
2183/* frozenset object ********************************************************/
2184
2185
2186static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2188 contains_doc},
2189 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2190 copy_doc},
2191 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2192 difference_doc},
2193 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2194 intersection_doc},
2195 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2196 isdisjoint_doc},
2197 {"issubset", (PyCFunction)set_issubset, METH_O,
2198 issubset_doc},
2199 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2200 issuperset_doc},
2201 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2202 reduce_doc},
2203 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2204 sizeof_doc},
2205 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2206 symmetric_difference_doc},
2207 {"union", (PyCFunction)set_union, METH_VARARGS,
2208 union_doc},
2209 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002210};
2211
2212static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 0, /*nb_add*/
2214 (binaryfunc)set_sub, /*nb_subtract*/
2215 0, /*nb_multiply*/
2216 0, /*nb_remainder*/
2217 0, /*nb_divmod*/
2218 0, /*nb_power*/
2219 0, /*nb_negative*/
2220 0, /*nb_positive*/
2221 0, /*nb_absolute*/
2222 0, /*nb_bool*/
2223 0, /*nb_invert*/
2224 0, /*nb_lshift*/
2225 0, /*nb_rshift*/
2226 (binaryfunc)set_and, /*nb_and*/
2227 (binaryfunc)set_xor, /*nb_xor*/
2228 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002229};
2230
2231PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002232"frozenset() -> empty frozenset object\n\
2233frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002234\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002235Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002236
2237PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2239 "frozenset", /* tp_name */
2240 sizeof(PySetObject), /* tp_basicsize */
2241 0, /* tp_itemsize */
2242 /* methods */
2243 (destructor)set_dealloc, /* tp_dealloc */
2244 0, /* tp_print */
2245 0, /* tp_getattr */
2246 0, /* tp_setattr */
2247 0, /* tp_reserved */
2248 (reprfunc)set_repr, /* tp_repr */
2249 &frozenset_as_number, /* tp_as_number */
2250 &set_as_sequence, /* tp_as_sequence */
2251 0, /* tp_as_mapping */
2252 frozenset_hash, /* tp_hash */
2253 0, /* tp_call */
2254 0, /* tp_str */
2255 PyObject_GenericGetAttr, /* tp_getattro */
2256 0, /* tp_setattro */
2257 0, /* tp_as_buffer */
2258 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2259 Py_TPFLAGS_BASETYPE, /* tp_flags */
2260 frozenset_doc, /* tp_doc */
2261 (traverseproc)set_traverse, /* tp_traverse */
2262 (inquiry)set_clear_internal, /* tp_clear */
2263 (richcmpfunc)set_richcompare, /* tp_richcompare */
2264 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2265 (getiterfunc)set_iter, /* tp_iter */
2266 0, /* tp_iternext */
2267 frozenset_methods, /* tp_methods */
2268 0, /* tp_members */
2269 0, /* tp_getset */
2270 0, /* tp_base */
2271 0, /* tp_dict */
2272 0, /* tp_descr_get */
2273 0, /* tp_descr_set */
2274 0, /* tp_dictoffset */
2275 0, /* tp_init */
2276 PyType_GenericAlloc, /* tp_alloc */
2277 frozenset_new, /* tp_new */
2278 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002279};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002280
2281
2282/***** C API functions *************************************************/
2283
2284PyObject *
2285PySet_New(PyObject *iterable)
2286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002288}
2289
2290PyObject *
2291PyFrozenSet_New(PyObject *iterable)
2292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002294}
2295
Neal Norwitz8c49c822006-03-04 18:41:19 +00002296Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002297PySet_Size(PyObject *anyset)
2298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 if (!PyAnySet_Check(anyset)) {
2300 PyErr_BadInternalCall();
2301 return -1;
2302 }
2303 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002304}
2305
2306int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002307PySet_Clear(PyObject *set)
2308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 if (!PySet_Check(set)) {
2310 PyErr_BadInternalCall();
2311 return -1;
2312 }
2313 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002314}
2315
2316int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002317PySet_Contains(PyObject *anyset, PyObject *key)
2318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 if (!PyAnySet_Check(anyset)) {
2320 PyErr_BadInternalCall();
2321 return -1;
2322 }
2323 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002324}
2325
2326int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002327PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 if (!PySet_Check(set)) {
2330 PyErr_BadInternalCall();
2331 return -1;
2332 }
2333 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002334}
2335
2336int
Christian Heimesfd66e512008-01-29 12:18:50 +00002337PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 if (!PySet_Check(anyset) &&
2340 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2341 PyErr_BadInternalCall();
2342 return -1;
2343 }
2344 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002345}
2346
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002347int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002348_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 if (!PyAnySet_Check(set)) {
2353 PyErr_BadInternalCall();
2354 return -1;
2355 }
2356 if (set_next((PySetObject *)set, pos, &entry) == 0)
2357 return 0;
2358 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002359 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002361}
2362
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002363PyObject *
2364PySet_Pop(PyObject *set)
2365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 if (!PySet_Check(set)) {
2367 PyErr_BadInternalCall();
2368 return NULL;
2369 }
2370 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002371}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002372
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002373int
2374_PySet_Update(PyObject *set, PyObject *iterable)
2375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 if (!PySet_Check(set)) {
2377 PyErr_BadInternalCall();
2378 return -1;
2379 }
2380 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002381}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002382
2383#ifdef Py_DEBUG
2384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002386 Returns True and original set is restored. */
2387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388#define assertRaises(call_return_value, exception) \
2389 do { \
2390 assert(call_return_value); \
2391 assert(PyErr_ExceptionMatches(exception)); \
2392 PyErr_Clear(); \
2393 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002394
2395static PyObject *
2396test_c_api(PySetObject *so)
2397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 Py_ssize_t count;
2399 char *s;
2400 Py_ssize_t i;
2401 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2402 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002403 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 /* Verify preconditions */
2407 assert(PyAnySet_Check(ob));
2408 assert(PyAnySet_CheckExact(ob));
2409 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 /* so.clear(); so |= set("abc"); */
2412 str = PyUnicode_FromString("abc");
2413 if (str == NULL)
2414 return NULL;
2415 set_clear_internal(so);
2416 if (set_update_internal(so, str) == -1) {
2417 Py_DECREF(str);
2418 return NULL;
2419 }
2420 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 /* Exercise type/size checks */
2423 assert(PySet_Size(ob) == 3);
2424 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 /* Raise TypeError for non-iterable constructor arguments */
2427 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2428 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 /* Raise TypeError for unhashable key */
2431 dup = PySet_New(ob);
2432 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2433 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2434 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 /* Exercise successful pop, contains, add, and discard */
2437 elem = PySet_Pop(ob);
2438 assert(PySet_Contains(ob, elem) == 0);
2439 assert(PySet_GET_SIZE(ob) == 2);
2440 assert(PySet_Add(ob, elem) == 0);
2441 assert(PySet_Contains(ob, elem) == 1);
2442 assert(PySet_GET_SIZE(ob) == 3);
2443 assert(PySet_Discard(ob, elem) == 1);
2444 assert(PySet_GET_SIZE(ob) == 2);
2445 assert(PySet_Discard(ob, elem) == 0);
2446 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 /* Exercise clear */
2449 dup2 = PySet_New(dup);
2450 assert(PySet_Clear(dup2) == 0);
2451 assert(PySet_Size(dup2) == 0);
2452 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 /* Raise SystemError on clear or update of frozen set */
2455 f = PyFrozenSet_New(dup);
2456 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2457 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2458 assert(PySet_Add(f, elem) == 0);
2459 Py_INCREF(f);
2460 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2461 Py_DECREF(f);
2462 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 /* Exercise direct iteration */
2465 i = 0, count = 0;
2466 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2467 s = _PyUnicode_AsString(x);
2468 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2469 count++;
2470 }
2471 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 /* Exercise updates */
2474 dup2 = PySet_New(NULL);
2475 assert(_PySet_Update(dup2, dup) == 0);
2476 assert(PySet_Size(dup2) == 3);
2477 assert(_PySet_Update(dup2, dup) == 0);
2478 assert(PySet_Size(dup2) == 3);
2479 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 /* Raise SystemError when self argument is not a set or frozenset. */
2482 t = PyTuple_New(0);
2483 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2484 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2485 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 /* Raise SystemError when self argument is not a set. */
2488 f = PyFrozenSet_New(dup);
2489 assert(PySet_Size(f) == 3);
2490 assert(PyFrozenSet_CheckExact(f));
2491 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2492 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2493 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 /* Raise KeyError when popping from an empty set */
2496 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2497 Py_DECREF(ob);
2498 assert(PySet_GET_SIZE(ob) == 0);
2499 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 /* Restore the set from the copy using the PyNumber API */
2502 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2503 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 /* Verify constructors accept NULL arguments */
2506 f = PySet_New(NULL);
2507 assert(f != NULL);
2508 assert(PySet_GET_SIZE(f) == 0);
2509 Py_DECREF(f);
2510 f = PyFrozenSet_New(NULL);
2511 assert(f != NULL);
2512 assert(PyFrozenSet_CheckExact(f));
2513 assert(PySet_GET_SIZE(f) == 0);
2514 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 Py_DECREF(elem);
2517 Py_DECREF(dup);
2518 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002519}
2520
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002521#undef assertRaises
2522
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002523#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002524
2525/***** Dummy Struct *************************************************/
2526
2527static PyObject *
2528dummy_repr(PyObject *op)
2529{
2530 return PyUnicode_FromString("<dummy key>");
2531}
2532
2533static void
2534dummy_dealloc(PyObject* ignore)
2535{
2536 Py_FatalError("deallocating <dummy key>");
2537}
2538
2539static PyTypeObject _PySetDummy_Type = {
2540 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2541 "<dummy key> type",
2542 0,
2543 0,
2544 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2545 0, /*tp_print*/
2546 0, /*tp_getattr*/
2547 0, /*tp_setattr*/
2548 0, /*tp_reserved*/
2549 dummy_repr, /*tp_repr*/
2550 0, /*tp_as_number*/
2551 0, /*tp_as_sequence*/
2552 0, /*tp_as_mapping*/
2553 0, /*tp_hash */
2554 0, /*tp_call */
2555 0, /*tp_str */
2556 0, /*tp_getattro */
2557 0, /*tp_setattro */
2558 0, /*tp_as_buffer */
2559 Py_TPFLAGS_DEFAULT, /*tp_flags */
2560};
2561
2562static PyObject _dummy_struct = {
2563 _PyObject_EXTRA_INIT
2564 2, &_PySetDummy_Type
2565};
2566