blob: 94789c6645bcaa76130ff90d194bb4d1afd98878 [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 Hettingera9d99362005-08-05 00:01:15 +000032static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#ifdef Py_REF_DEBUG
35PyObject *
36_PySet_Dummy(void)
37{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039}
40#endif
41
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042#define INIT_NONZERO_SET_SLOTS(so) do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE - 1; \
45 (so)->hash = -1; \
Raymond Hettingerbc841a12005-08-07 13:02:53 +000046 } while(0)
47
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define EMPTY_TO_MINSIZE(so) do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
50 (so)->used = (so)->fill = 0; \
51 INIT_NONZERO_SET_SLOTS(so); \
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000052 } while(0)
53
Raymond Hettingerbc841a12005-08-07 13:02:53 +000054/* Reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +000055#ifndef PySet_MAXFREELIST
56#define PySet_MAXFREELIST 80
57#endif
58static PySetObject *free_list[PySet_MAXFREELIST];
59static int numfree = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000060
Christian Heimes0ded5b52007-12-10 15:50:56 +000061
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062/*
63The basic lookup function used by all operations.
64This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65Open addressing is preferred over chaining since the link overhead for
66chaining would be substantial (100% with typical malloc overhead).
67
68The initial probe index is computed as hash mod the table size. Subsequent
Raymond Hettingerbc841a12005-08-07 13:02:53 +000069probe indices are computed as explained in Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000070
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070071To improve cache locality, each probe is done in pairs.
72After the probe is examined, an adjacent entry is then examined as well.
73The likelihood is that an adjacent entry is in the same cache line and
74can be examined more cheaply than another probe elsewhere in memory.
75
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000076All arithmetic on hash should ignore overflow.
77
Raymond Hettinger9bda1d62005-09-16 07:14:21 +000078Unlike the dictionary implementation, the lookkey functions can return
79NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000080*/
81
82static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020083set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000084{
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070085 size_t i, j; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020086 size_t perturb;
87 setentry *freeslot;
88 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020090 setentry *entry;
91 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 PyObject *startkey;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000093
Mark Dickinson57e683e2011-09-24 18:18:40 +010094 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 entry = &table[i];
96 if (entry->key == NULL || entry->key == key)
97 return entry;
Raymond Hettingerae9e6162013-08-20 22:28:24 -070098 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger237b34b2013-08-17 02:31:53 -070099 startkey = entry->key;
100 Py_INCREF(startkey);
101 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
102 Py_DECREF(startkey);
103 if (cmp < 0)
104 return NULL;
105 if (table == so->table && entry->key == startkey) {
106 if (cmp > 0)
107 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 }
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700109 else {
110 /* Start over if the compare altered the set */
111 return set_lookkey(so, key, hash);
112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 }
Raymond Hettinger237b34b2013-08-17 02:31:53 -0700114 freeslot = (entry->key == dummy) ? entry : NULL;
115
116 /* In the loop, key == dummy is by far (factor of 100s)
117 the least likely outcome, so test for that last. */
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700118 j = i;
119 perturb = hash;
120 while (1) {
121 j ^= 1;
122 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 if (entry->key == NULL) {
124 if (freeslot != NULL)
125 entry = freeslot;
126 break;
127 }
128 if (entry->key == key)
129 break;
Raymond Hettingerae9e6162013-08-20 22:28:24 -0700130 if (entry->hash == hash && entry->key != dummy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 startkey = entry->key;
132 Py_INCREF(startkey);
133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
134 Py_DECREF(startkey);
135 if (cmp < 0)
136 return NULL;
137 if (table == so->table && entry->key == startkey) {
138 if (cmp > 0)
139 break;
140 }
141 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 return set_lookkey(so, key, hash);
143 }
144 }
Raymond Hettinger07351a02013-08-17 02:39:46 -0700145 if (entry->key == dummy && freeslot == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 freeslot = entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700147
148 i = i * 5 + perturb + 1;
149 j = i & mask;
150 perturb >>= PERTURB_SHIFT;
151
152 entry = &table[j];
153 if (entry->key == NULL) {
154 if (freeslot != NULL)
155 entry = freeslot;
156 break;
157 }
158 if (entry->key == key)
159 break;
Raymond Hettingerae9e6162013-08-20 22:28:24 -0700160 if (entry->hash == hash && entry->key != dummy) {
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700161 startkey = entry->key;
162 Py_INCREF(startkey);
163 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
164 Py_DECREF(startkey);
165 if (cmp < 0)
166 return NULL;
167 if (table == so->table && entry->key == startkey) {
168 if (cmp > 0)
169 break;
170 }
171 else {
172 return set_lookkey(so, key, hash);
173 }
174 }
175 if (entry->key == dummy && freeslot == NULL)
176 freeslot = entry;
177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 }
179 return entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000180}
181
182/*
Christian Heimes0ded5b52007-12-10 15:50:56 +0000183 * Hacked up version of set_lookkey which can assume keys are always unicode;
184 * This means we can always use unicode_eq directly and not have to check to
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000185 * see if the comparison altered the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000186 */
187static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200188set_lookkey_unicode(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000189{
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700190 size_t i, j; /* Unsigned for defined overflow behavior. */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200191 size_t perturb;
192 setentry *freeslot;
193 size_t mask = so->mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200195 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 /* Make sure this function doesn't have to handle non-unicode keys,
198 including subclasses of str; e.g., one reason to subclass
199 strings is to override __eq__, and for speed we don't cater to
200 that here. */
201 if (!PyUnicode_CheckExact(key)) {
202 so->lookup = set_lookkey;
203 return set_lookkey(so, key, hash);
204 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700205
Mark Dickinson57e683e2011-09-24 18:18:40 +0100206 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 entry = &table[i];
208 if (entry->key == NULL || entry->key == key)
209 return entry;
210 if (entry->key == dummy)
211 freeslot = entry;
212 else {
213 if (entry->hash == hash && unicode_eq(entry->key, key))
214 return entry;
215 freeslot = NULL;
216 }
Raymond Hettingered6c1ef2005-08-13 08:28:03 +0000217
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700218 j = i;
219 perturb = hash;
220 while (1) {
221 j ^= 1;
222 entry = &table[j];
223 if (entry->key == NULL)
224 return freeslot == NULL ? entry : freeslot;
225 if (entry->key == key
226 || (entry->hash == hash
227 && entry->key != dummy
228 && unicode_eq(entry->key, key)))
229 return entry;
230 if (entry->key == dummy && freeslot == NULL)
231 freeslot = entry;
232
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700233 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700234 j = i & mask;
235 perturb >>= PERTURB_SHIFT;
236
237 entry = &table[j];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (entry->key == NULL)
239 return freeslot == NULL ? entry : freeslot;
240 if (entry->key == key
241 || (entry->hash == hash
242 && entry->key != dummy
243 && unicode_eq(entry->key, key)))
244 return entry;
245 if (entry->key == dummy && freeslot == NULL)
246 freeslot = entry;
247 }
248 assert(0); /* NOT REACHED */
249 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000250}
251
252/*
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000253Internal routine to insert a new key into the table.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000254Used by the public insert routine.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000255Eats a reference to key.
256*/
Raymond Hettinger9bda1d62005-09-16 07:14:21 +0000257static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200258set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000259{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200260 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 assert(so->lookup != NULL);
263 entry = so->lookup(so, key, hash);
264 if (entry == NULL)
265 return -1;
266 if (entry->key == NULL) {
267 /* UNUSED */
268 so->fill++;
269 entry->key = key;
270 entry->hash = hash;
271 so->used++;
272 } else if (entry->key == dummy) {
273 /* DUMMY */
274 entry->key = key;
275 entry->hash = hash;
276 so->used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 } else {
278 /* ACTIVE */
279 Py_DECREF(key);
280 }
281 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000282}
283
284/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000285Internal routine used by set_table_resize() to insert an item which is
286known to be absent from the set. This routine also assumes that
287the set contains no deleted entries. Besides the performance benefit,
288using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
289Note that no refcounts are changed by this routine; if needed, the caller
290is responsible for incref'ing `key`.
291*/
292static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200293set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200296 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700297 size_t perturb = hash;
298 size_t mask = (size_t)so->mask;
299 size_t i, j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000300
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700301 i = j = (size_t)hash & mask;
302 while (1) {
303 entry = &table[j];
304 if (entry->key == NULL)
305 break;
306 entry = &table[j ^ 1];
307 if (entry->key == NULL)
308 break;
Raymond Hettingerc629d4c2013-08-05 22:24:50 -0700309 i = i * 5 + perturb + 1;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700310 j = i & mask;
311 perturb >>= PERTURB_SHIFT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 }
313 so->fill++;
314 entry->key = key;
315 entry->hash = hash;
316 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000317}
318
319/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000320Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000321keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000322actually be smaller than the old one.
323*/
324static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000325set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_ssize_t newsize;
328 setentry *oldtable, *newtable, *entry;
329 Py_ssize_t i;
330 int is_oldtable_malloced;
331 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700332 PyObject *dummy_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* Find the smallest table size > minused. */
337 for (newsize = PySet_MINSIZE;
338 newsize <= minused && newsize > 0;
339 newsize <<= 1)
340 ;
341 if (newsize <= 0) {
342 PyErr_NoMemory();
343 return -1;
344 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 /* Get space for a new table. */
347 oldtable = so->table;
348 assert(oldtable != NULL);
349 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (newsize == PySet_MINSIZE) {
352 /* A large table is shrinking, or we can't get any smaller. */
353 newtable = so->smalltable;
354 if (newtable == oldtable) {
355 if (so->fill == so->used) {
356 /* No dummies, so no point doing anything. */
357 return 0;
358 }
359 /* We're not going to resize it, but rebuild the
360 table anyway to purge old dummy entries.
361 Subtle: This is *necessary* if fill==size,
362 as set_lookkey needs at least one virgin slot to
363 terminate failing searches. If fill < size, it's
364 merely desirable, as dummies slow searches. */
365 assert(so->fill > so->used);
366 memcpy(small_copy, oldtable, sizeof(small_copy));
367 oldtable = small_copy;
368 }
369 }
370 else {
371 newtable = PyMem_NEW(setentry, newsize);
372 if (newtable == NULL) {
373 PyErr_NoMemory();
374 return -1;
375 }
376 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 /* Make the set empty, using the new table. */
379 assert(newtable != oldtable);
380 so->table = newtable;
381 so->mask = newsize - 1;
382 memset(newtable, 0, sizeof(setentry) * newsize);
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700383 i = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 so->used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 so->fill = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 /* Copy the data over; this is refcount-neutral for active entries;
388 dummy entries aren't copied over, of course */
Raymond Hettinger8ad39192013-08-15 02:18:55 -0700389 dummy_entry = dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 for (entry = oldtable; i > 0; entry++) {
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700391 if (entry->key != NULL && entry->key != dummy_entry) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 /* ACTIVE */
393 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000394 set_insert_clean(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 }
396 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (is_oldtable_malloced)
399 PyMem_DEL(oldtable);
400 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000401}
402
Raymond Hettingerc991db22005-08-11 07:58:45 +0000403/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
404
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000405static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200406set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000407{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200408 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000409 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100410 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 assert(so->fill <= so->mask); /* at least one empty slot */
413 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000414 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100415 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000416 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return -1;
418 }
419 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
420 return 0;
421 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000422}
423
424static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200425set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000426{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200427 Py_hash_t hash;
428 Py_ssize_t n_used;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200431 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 hash = PyObject_Hash(key);
433 if (hash == -1)
434 return -1;
435 }
436 assert(so->fill <= so->mask); /* at least one empty slot */
437 n_used = so->used;
438 Py_INCREF(key);
439 if (set_insert_key(so, key, hash) == -1) {
440 Py_DECREF(key);
441 return -1;
442 }
443 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
444 return 0;
445 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000446}
447
448#define DISCARD_NOTFOUND 0
449#define DISCARD_FOUND 1
450
451static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000452set_discard_entry(PySetObject *so, setentry *oldentry)
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200453{ setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000455
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000456 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (entry == NULL)
458 return -1;
459 if (entry->key == NULL || entry->key == dummy)
460 return DISCARD_NOTFOUND;
461 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 entry->key = dummy;
463 so->used--;
464 Py_DECREF(old_key);
465 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000466}
467
468static int
469set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200471 Py_hash_t hash;
472 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 PyObject *old_key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200478 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 hash = PyObject_Hash(key);
480 if (hash == -1)
481 return -1;
482 }
483 entry = (so->lookup)(so, key, hash);
484 if (entry == NULL)
485 return -1;
486 if (entry->key == NULL || entry->key == dummy)
487 return DISCARD_NOTFOUND;
488 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 entry->key = dummy;
490 so->used--;
491 Py_DECREF(old_key);
492 return DISCARD_FOUND;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000493}
494
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000495static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000496set_clear_internal(PySetObject *so)
497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 setentry *entry, *table;
499 int table_is_malloced;
500 Py_ssize_t fill;
501 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000502#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 Py_ssize_t i, n;
504 assert (PyAnySet_Check(so));
Raymond Hettingera580c472005-08-05 17:19:54 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 n = so->mask + 1;
507 i = 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508#endif
509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 table = so->table;
511 assert(table != NULL);
512 table_is_malloced = table != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 /* This is delicate. During the process of clearing the set,
515 * decrefs can cause the set to mutate. To avoid fatal confusion
516 * (voice of experience), we have to make the set empty before
517 * clearing the slots, and never refer to anything via so->ref while
518 * clearing.
519 */
520 fill = so->fill;
521 if (table_is_malloced)
522 EMPTY_TO_MINSIZE(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 else if (fill > 0) {
525 /* It's a small table with something that needs to be cleared.
526 * Afraid the only safe way is to copy the set entries into
527 * another small table first.
528 */
529 memcpy(small_copy, table, sizeof(small_copy));
530 table = small_copy;
531 EMPTY_TO_MINSIZE(so);
532 }
533 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 /* Now we can finally clear things. If C had refcounts, we could
536 * assert that the refcount on table is 1 now, i.e. that this function
537 * has unique access to it, so decref side-effects can't alter it.
538 */
539 for (entry = table; fill > 0; ++entry) {
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000540#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 assert(i < n);
542 ++i;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000543#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (entry->key) {
545 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700546 if (entry->key != dummy)
547 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000549#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 else
551 assert(entry->key == NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000552#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 if (table_is_malloced)
556 PyMem_DEL(table);
557 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000558}
559
560/*
561 * Iterate over a set table. Use like so:
562 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000563 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000564 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000565 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000566 * while (set_next(yourset, &pos, &entry)) {
567 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000568 * }
569 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000570 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000572 */
573static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000574set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 Py_ssize_t i;
577 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200578 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 assert (PyAnySet_Check(so));
581 i = *pos_ptr;
582 assert(i >= 0);
583 table = so->table;
584 mask = so->mask;
585 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
586 i++;
587 *pos_ptr = i+1;
588 if (i > mask)
589 return 0;
590 assert(table[i].key != NULL);
591 *entry_ptr = &table[i];
592 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000593}
594
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000595static void
596set_dealloc(PySetObject *so)
597{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200598 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 Py_ssize_t fill = so->fill;
600 PyObject_GC_UnTrack(so);
601 Py_TRASHCAN_SAFE_BEGIN(so)
602 if (so->weakreflist != NULL)
603 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 for (entry = so->table; fill > 0; entry++) {
606 if (entry->key) {
607 --fill;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700608 if (entry->key != dummy)
609 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 }
611 }
612 if (so->table != so->smalltable)
613 PyMem_DEL(so->table);
614 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
615 free_list[numfree++] = so;
616 else
617 Py_TYPE(so)->tp_free(so);
618 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000619}
620
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000621static PyObject *
622set_repr(PySetObject *so)
623{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200624 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if (status != 0) {
628 if (status < 0)
629 return NULL;
630 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
631 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 /* shortcut for the empty set */
634 if (!so->used) {
635 Py_ReprLeave((PyObject*)so);
636 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
637 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 keys = PySequence_List((PyObject *)so);
640 if (keys == NULL)
641 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000642
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200643 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 listrepr = PyObject_Repr(keys);
645 Py_DECREF(keys);
646 if (listrepr == NULL)
647 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200648 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200650 if (tmp == NULL)
651 goto done;
652 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200653
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654 if (Py_TYPE(so) != &PySet_Type)
655 result = PyUnicode_FromFormat("%s({%U})",
656 Py_TYPE(so)->tp_name,
657 listrepr);
658 else
659 result = PyUnicode_FromFormat("{%U}", listrepr);
660 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000661done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 Py_ReprLeave((PyObject*)so);
663 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000664}
665
Martin v. Löwis18e16552006-02-15 17:27:45 +0000666static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000667set_len(PyObject *so)
668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000670}
671
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000672static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000673set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000676 PyObject *key;
Raymond Hettinger5bb1b1d2013-08-21 01:34:18 -0700677 PyObject *dummy_entry;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100678 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200679 Py_ssize_t i;
680 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 assert (PyAnySet_Check(so));
683 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 other = (PySetObject*)otherset;
686 if (other == so || other->used == 0)
687 /* a.update(a) or a.update({}); nothing to do */
688 return 0;
689 /* Do one big resize at the start, rather than
690 * incrementally resizing as we insert new keys. Expect
691 * that there will be no (or few) overlapping keys.
692 */
693 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
694 if (set_table_resize(so, (so->used + other->used)*2) != 0)
695 return -1;
696 }
Raymond Hettinger5bb1b1d2013-08-21 01:34:18 -0700697 dummy_entry = dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 for (i = 0; i <= other->mask; i++) {
699 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000700 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100701 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000702 if (key != NULL &&
Raymond Hettinger5bb1b1d2013-08-21 01:34:18 -0700703 key != dummy_entry) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000704 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100705 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000706 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 return -1;
708 }
709 }
710 }
711 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000712}
713
714static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000715set_contains_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000716{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000717 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200721 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 hash = PyObject_Hash(key);
723 if (hash == -1)
724 return -1;
725 }
726 entry = (so->lookup)(so, key, hash);
727 if (entry == NULL)
728 return -1;
729 key = entry->key;
730 return key != NULL && key != dummy;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000731}
732
Raymond Hettingerc991db22005-08-11 07:58:45 +0000733static int
734set_contains_entry(PySetObject *so, setentry *entry)
735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 PyObject *key;
737 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000738
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000739 lu_entry = (so->lookup)(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 if (lu_entry == NULL)
741 return -1;
742 key = lu_entry->key;
743 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000744}
745
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000746static PyObject *
747set_pop(PySetObject *so)
748{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200749 Py_ssize_t i = 0;
750 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 assert (PyAnySet_Check(so));
754 if (so->used == 0) {
755 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
756 return NULL;
757 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* Set entry to "the first" unused or dummy set entry. We abuse
760 * the hash field of slot 0 to hold a search finger:
761 * If slot 0 has a value, use slot 0.
762 * Else slot 0 is being used to hold a search finger,
763 * and we use its hash value as the first index to look.
764 */
765 entry = &so->table[0];
766 if (entry->key == NULL || entry->key == dummy) {
767 i = entry->hash;
768 /* The hash field may be a real hash value, or it may be a
769 * legit search finger, or it may be a once-legit search
770 * finger that's out of bounds now because it wrapped around
771 * or the table shrunk -- simply make sure it's in bounds now.
772 */
773 if (i > so->mask || i < 1)
774 i = 1; /* skip slot 0 */
775 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
776 i++;
777 if (i > so->mask)
778 i = 1;
779 }
780 }
781 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 entry->key = dummy;
783 so->used--;
784 so->table[0].hash = i + 1; /* next place to start */
785 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000786}
787
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000788PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
789Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000790
791static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000792set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 Py_ssize_t pos = 0;
795 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 while (set_next(so, &pos, &entry))
798 Py_VISIT(entry->key);
799 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000800}
801
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000802static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000803frozenset_hash(PyObject *self)
804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800806 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 setentry *entry;
808 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 if (so->hash != -1)
811 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000812
Mark Dickinson57e683e2011-09-24 18:18:40 +0100813 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 while (set_next(so, &pos, &entry)) {
815 /* Work to increase the bit dispersion for closely spaced hash
816 values. The is important because some use cases have many
817 combinations of a small number of elements with nearby
818 hashes so that many distinct combinations collapse to only
819 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000820 h = entry->hash;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800821 hash ^= (h ^ (h << 16) ^ 89869747UL) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 }
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800823 hash = hash * 69069U + 907133923UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 if (hash == -1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800825 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 so->hash = hash;
827 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000828}
829
Raymond Hettingera9d99362005-08-05 00:01:15 +0000830/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000831
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000832typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyObject_HEAD
834 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
835 Py_ssize_t si_used;
836 Py_ssize_t si_pos;
837 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000838} setiterobject;
839
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000840static void
841setiter_dealloc(setiterobject *si)
842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 Py_XDECREF(si->si_set);
844 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000845}
846
847static int
848setiter_traverse(setiterobject *si, visitproc visit, void *arg)
849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 Py_VISIT(si->si_set);
851 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000852}
853
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000854static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000855setiter_len(setiterobject *si)
856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 Py_ssize_t len = 0;
858 if (si->si_set != NULL && si->si_used == si->si_set->used)
859 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000860 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000861}
862
Armin Rigof5b3e362006-02-11 21:32:43 +0000863PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000864
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000865static PyObject *setiter_iternext(setiterobject *si);
866
867static PyObject *
868setiter_reduce(setiterobject *si)
869{
870 PyObject *list;
871 setiterobject tmp;
872
873 list = PyList_New(0);
874 if (!list)
875 return NULL;
876
Ezio Melotti0e1af282012-09-28 16:43:40 +0300877 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000878 tmp = *si;
879 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300880
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000881 /* iterate the temporary into a list */
882 for(;;) {
883 PyObject *element = setiter_iternext(&tmp);
884 if (element) {
885 if (PyList_Append(list, element)) {
886 Py_DECREF(element);
887 Py_DECREF(list);
888 Py_XDECREF(tmp.si_set);
889 return NULL;
890 }
891 Py_DECREF(element);
892 } else
893 break;
894 }
895 Py_XDECREF(tmp.si_set);
896 /* check for error */
897 if (tmp.si_set != NULL) {
898 /* we have an error */
899 Py_DECREF(list);
900 return NULL;
901 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200902 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000903}
904
905PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
906
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000907static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000909 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000911};
912
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000913static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200916 Py_ssize_t i, mask;
917 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (so == NULL)
921 return NULL;
922 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 if (si->si_used != so->used) {
925 PyErr_SetString(PyExc_RuntimeError,
926 "Set changed size during iteration");
927 si->si_used = -1; /* Make this state sticky */
928 return NULL;
929 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 i = si->si_pos;
932 assert(i>=0);
933 entry = so->table;
934 mask = so->mask;
935 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
936 i++;
937 si->si_pos = i+1;
938 if (i > mask)
939 goto fail;
940 si->len--;
941 key = entry[i].key;
942 Py_INCREF(key);
943 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000944
945fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 Py_DECREF(so);
947 si->si_set = NULL;
948 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000949}
950
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000951PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 PyVarObject_HEAD_INIT(&PyType_Type, 0)
953 "set_iterator", /* tp_name */
954 sizeof(setiterobject), /* tp_basicsize */
955 0, /* tp_itemsize */
956 /* methods */
957 (destructor)setiter_dealloc, /* tp_dealloc */
958 0, /* tp_print */
959 0, /* tp_getattr */
960 0, /* tp_setattr */
961 0, /* tp_reserved */
962 0, /* tp_repr */
963 0, /* tp_as_number */
964 0, /* tp_as_sequence */
965 0, /* tp_as_mapping */
966 0, /* tp_hash */
967 0, /* tp_call */
968 0, /* tp_str */
969 PyObject_GenericGetAttr, /* tp_getattro */
970 0, /* tp_setattro */
971 0, /* tp_as_buffer */
972 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
973 0, /* tp_doc */
974 (traverseproc)setiter_traverse, /* tp_traverse */
975 0, /* tp_clear */
976 0, /* tp_richcompare */
977 0, /* tp_weaklistoffset */
978 PyObject_SelfIter, /* tp_iter */
979 (iternextfunc)setiter_iternext, /* tp_iternext */
980 setiter_methods, /* tp_methods */
981 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000982};
983
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000984static PyObject *
985set_iter(PySetObject *so)
986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
988 if (si == NULL)
989 return NULL;
990 Py_INCREF(so);
991 si->si_set = so;
992 si->si_used = so->used;
993 si->si_pos = 0;
994 si->len = so->used;
995 _PyObject_GC_TRACK(si);
996 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000997}
998
Raymond Hettingerd7946662005-08-01 21:39:29 +0000999static int
Raymond Hettingerd7946662005-08-01 21:39:29 +00001000set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +00001001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 if (PyAnySet_Check(other))
1005 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 if (PyDict_CheckExact(other)) {
1008 PyObject *value;
1009 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001010 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 /* Do one big resize at the start, rather than
1014 * incrementally resizing as we insert new keys. Expect
1015 * that there will be no (or few) overlapping keys.
1016 */
1017 if (dictsize == -1)
1018 return -1;
1019 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
1020 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
1021 return -1;
1022 }
1023 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1024 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 an_entry.hash = hash;
1027 an_entry.key = key;
1028 if (set_add_entry(so, &an_entry) == -1)
1029 return -1;
1030 }
1031 return 0;
1032 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 it = PyObject_GetIter(other);
1035 if (it == NULL)
1036 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 while ((key = PyIter_Next(it)) != NULL) {
1039 if (set_add_key(so, key) == -1) {
1040 Py_DECREF(it);
1041 Py_DECREF(key);
1042 return -1;
1043 }
1044 Py_DECREF(key);
1045 }
1046 Py_DECREF(it);
1047 if (PyErr_Occurred())
1048 return -1;
1049 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001050}
1051
1052static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001053set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1058 PyObject *other = PyTuple_GET_ITEM(args, i);
1059 if (set_update_internal(so, other) == -1)
1060 return NULL;
1061 }
1062 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001063}
1064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001066"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001067
1068static PyObject *
1069make_new_set(PyTypeObject *type, PyObject *iterable)
1070{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001071 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (dummy == NULL) { /* Auto-initialize dummy */
Raymond Hettingerae9e6162013-08-20 22:28:24 -07001074 dummy = PyUnicode_FromString("<dummy key>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (dummy == NULL)
1076 return NULL;
1077 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 /* create PySetObject structure */
1080 if (numfree &&
1081 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1082 so = free_list[--numfree];
1083 assert (so != NULL && PyAnySet_CheckExact(so));
1084 Py_TYPE(so) = type;
1085 _Py_NewReference((PyObject *)so);
1086 EMPTY_TO_MINSIZE(so);
1087 PyObject_GC_Track(so);
1088 } else {
1089 so = (PySetObject *)type->tp_alloc(type, 0);
1090 if (so == NULL)
1091 return NULL;
1092 /* tp_alloc has already zeroed the structure */
1093 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1094 INIT_NONZERO_SET_SLOTS(so);
1095 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 so->lookup = set_lookkey_unicode;
1098 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (iterable != NULL) {
1101 if (set_update_internal(so, iterable) == -1) {
1102 Py_DECREF(so);
1103 return NULL;
1104 }
1105 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001108}
1109
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001110static PyObject *
1111make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1114 if (PyType_IsSubtype(type, &PySet_Type))
1115 type = &PySet_Type;
1116 else
1117 type = &PyFrozenSet_Type;
1118 }
1119 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001120}
1121
Raymond Hettingerd7946662005-08-01 21:39:29 +00001122/* The empty frozenset is a singleton */
1123static PyObject *emptyfrozenset = NULL;
1124
Raymond Hettingera690a992003-11-16 16:17:49 +00001125static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001126frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1131 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1134 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 if (type != &PyFrozenSet_Type)
1137 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (iterable != NULL) {
1140 /* frozenset(f) is idempotent */
1141 if (PyFrozenSet_CheckExact(iterable)) {
1142 Py_INCREF(iterable);
1143 return iterable;
1144 }
1145 result = make_new_set(type, iterable);
1146 if (result == NULL || PySet_GET_SIZE(result))
1147 return result;
1148 Py_DECREF(result);
1149 }
1150 /* The empty frozenset is a singleton */
1151 if (emptyfrozenset == NULL)
1152 emptyfrozenset = make_new_set(type, NULL);
1153 Py_XINCREF(emptyfrozenset);
1154 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001155}
1156
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001157int
1158PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001159{
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001160 int freelist_size = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 PySetObject *so;
Raymond Hettingerbc841a12005-08-07 13:02:53 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 while (numfree) {
1164 numfree--;
1165 so = free_list[numfree];
1166 PyObject_GC_Del(so);
1167 }
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001168 return freelist_size;
1169}
1170
1171void
1172PySet_Fini(void)
1173{
1174 PySet_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 Py_CLEAR(dummy);
1176 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001177}
1178
David Malcolm49526f42012-06-22 14:55:41 -04001179/* Print summary info about the state of the optimized allocator */
1180void
1181_PySet_DebugMallocStats(FILE *out)
1182{
1183 _PyDebugAllocatorStats(out,
1184 "free PySetObject",
1185 numfree, sizeof(PySetObject));
1186}
1187
1188
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001189static PyObject *
1190set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1193 return NULL;
1194
1195 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001196}
1197
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001198/* set_swap_bodies() switches the contents of any two sets by moving their
1199 internal data pointers and, if needed, copying the internal smalltables.
1200 Semantically equivalent to:
1201
1202 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1203
1204 The function always succeeds and it leaves both objects in a stable state.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 Useful for creating temporary frozensets from sets for membership testing
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001206 in __contains__(), discard(), and remove(). Also useful for operations
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 that update in-place (by allowing an intermediate result to be swapped
Raymond Hettinger9dcb17c2005-07-31 13:09:28 +00001208 into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001209*/
1210
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001211static void
1212set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 Py_ssize_t t;
1215 setentry *u;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001216 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001218 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 t = a->fill; a->fill = b->fill; b->fill = t;
1221 t = a->used; a->used = b->used; b->used = t;
1222 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 u = a->table;
1225 if (a->table == a->smalltable)
1226 u = b->smalltable;
1227 a->table = b->table;
1228 if (b->table == b->smalltable)
1229 a->table = a->smalltable;
1230 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 if (a->table == a->smalltable || b->table == b->smalltable) {
1235 memcpy(tab, a->smalltable, sizeof(tab));
1236 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1237 memcpy(b->smalltable, tab, sizeof(tab));
1238 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1241 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1242 h = a->hash; a->hash = b->hash; b->hash = h;
1243 } else {
1244 a->hash = -1;
1245 b->hash = -1;
1246 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001247}
1248
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001249static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001250set_copy(PySetObject *so)
1251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001253}
1254
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001255static PyObject *
1256frozenset_copy(PySetObject *so)
1257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (PyFrozenSet_CheckExact(so)) {
1259 Py_INCREF(so);
1260 return (PyObject *)so;
1261 }
1262 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001263}
1264
Raymond Hettingera690a992003-11-16 16:17:49 +00001265PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1266
1267static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001268set_clear(PySetObject *so)
1269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 set_clear_internal(so);
1271 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001272}
1273
1274PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1275
1276static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001277set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 PySetObject *result;
1280 PyObject *other;
1281 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 result = (PySetObject *)set_copy(so);
1284 if (result == NULL)
1285 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1288 other = PyTuple_GET_ITEM(args, i);
1289 if ((PyObject *)so == other)
1290 continue;
1291 if (set_update_internal(result, other) == -1) {
1292 Py_DECREF(result);
1293 return NULL;
1294 }
1295 }
1296 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001297}
1298
1299PyDoc_STRVAR(union_doc,
1300 "Return the union of sets as a new set.\n\
1301\n\
1302(i.e. all elements that are in either set.)");
1303
1304static PyObject *
1305set_or(PySetObject *so, PyObject *other)
1306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001308
Brian Curtindfc80e32011-08-10 20:28:54 -05001309 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1310 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 result = (PySetObject *)set_copy(so);
1313 if (result == NULL)
1314 return NULL;
1315 if ((PyObject *)so == other)
1316 return (PyObject *)result;
1317 if (set_update_internal(result, other) == -1) {
1318 Py_DECREF(result);
1319 return NULL;
1320 }
1321 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001322}
1323
Raymond Hettingera690a992003-11-16 16:17:49 +00001324static PyObject *
1325set_ior(PySetObject *so, PyObject *other)
1326{
Brian Curtindfc80e32011-08-10 20:28:54 -05001327 if (!PyAnySet_Check(other))
1328 Py_RETURN_NOTIMPLEMENTED;
1329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 if (set_update_internal(so, other) == -1)
1331 return NULL;
1332 Py_INCREF(so);
1333 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001334}
1335
1336static PyObject *
1337set_intersection(PySetObject *so, PyObject *other)
1338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 PySetObject *result;
1340 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if ((PyObject *)so == other)
1343 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1346 if (result == NULL)
1347 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 if (PyAnySet_Check(other)) {
1350 Py_ssize_t pos = 0;
1351 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1354 tmp = (PyObject *)so;
1355 so = (PySetObject *)other;
1356 other = tmp;
1357 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 while (set_next((PySetObject *)other, &pos, &entry)) {
1360 int rv = set_contains_entry(so, entry);
1361 if (rv == -1) {
1362 Py_DECREF(result);
1363 return NULL;
1364 }
1365 if (rv) {
1366 if (set_add_entry(result, entry) == -1) {
1367 Py_DECREF(result);
1368 return NULL;
1369 }
1370 }
1371 }
1372 return (PyObject *)result;
1373 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 it = PyObject_GetIter(other);
1376 if (it == NULL) {
1377 Py_DECREF(result);
1378 return NULL;
1379 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 while ((key = PyIter_Next(it)) != NULL) {
1382 int rv;
1383 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001384 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if (hash == -1) {
1387 Py_DECREF(it);
1388 Py_DECREF(result);
1389 Py_DECREF(key);
1390 return NULL;
1391 }
1392 entry.hash = hash;
1393 entry.key = key;
1394 rv = set_contains_entry(so, &entry);
1395 if (rv == -1) {
1396 Py_DECREF(it);
1397 Py_DECREF(result);
1398 Py_DECREF(key);
1399 return NULL;
1400 }
1401 if (rv) {
1402 if (set_add_entry(result, &entry) == -1) {
1403 Py_DECREF(it);
1404 Py_DECREF(result);
1405 Py_DECREF(key);
1406 return NULL;
1407 }
1408 }
1409 Py_DECREF(key);
1410 }
1411 Py_DECREF(it);
1412 if (PyErr_Occurred()) {
1413 Py_DECREF(result);
1414 return NULL;
1415 }
1416 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001417}
1418
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001419static PyObject *
1420set_intersection_multi(PySetObject *so, PyObject *args)
1421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 Py_ssize_t i;
1423 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 if (PyTuple_GET_SIZE(args) == 0)
1426 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 Py_INCREF(so);
1429 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1430 PyObject *other = PyTuple_GET_ITEM(args, i);
1431 PyObject *newresult = set_intersection((PySetObject *)result, other);
1432 if (newresult == NULL) {
1433 Py_DECREF(result);
1434 return NULL;
1435 }
1436 Py_DECREF(result);
1437 result = newresult;
1438 }
1439 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001440}
1441
Raymond Hettingera690a992003-11-16 16:17:49 +00001442PyDoc_STRVAR(intersection_doc,
1443"Return the intersection of two sets as a new set.\n\
1444\n\
1445(i.e. all elements that are in both sets.)");
1446
1447static PyObject *
1448set_intersection_update(PySetObject *so, PyObject *other)
1449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 tmp = set_intersection(so, other);
1453 if (tmp == NULL)
1454 return NULL;
1455 set_swap_bodies(so, (PySetObject *)tmp);
1456 Py_DECREF(tmp);
1457 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001458}
1459
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001460static PyObject *
1461set_intersection_update_multi(PySetObject *so, PyObject *args)
1462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 tmp = set_intersection_multi(so, args);
1466 if (tmp == NULL)
1467 return NULL;
1468 set_swap_bodies(so, (PySetObject *)tmp);
1469 Py_DECREF(tmp);
1470 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001471}
1472
Raymond Hettingera690a992003-11-16 16:17:49 +00001473PyDoc_STRVAR(intersection_update_doc,
1474"Update a set with the intersection of itself and another.");
1475
1476static PyObject *
1477set_and(PySetObject *so, PyObject *other)
1478{
Brian Curtindfc80e32011-08-10 20:28:54 -05001479 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1480 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001482}
1483
1484static PyObject *
1485set_iand(PySetObject *so, PyObject *other)
1486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001488
Brian Curtindfc80e32011-08-10 20:28:54 -05001489 if (!PyAnySet_Check(other))
1490 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 result = set_intersection_update(so, other);
1492 if (result == NULL)
1493 return NULL;
1494 Py_DECREF(result);
1495 Py_INCREF(so);
1496 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001497}
1498
Guido van Rossum58da9312007-11-10 23:39:45 +00001499static PyObject *
1500set_isdisjoint(PySetObject *so, PyObject *other)
1501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if ((PyObject *)so == other) {
1505 if (PySet_GET_SIZE(so) == 0)
1506 Py_RETURN_TRUE;
1507 else
1508 Py_RETURN_FALSE;
1509 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 if (PyAnySet_CheckExact(other)) {
1512 Py_ssize_t pos = 0;
1513 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1516 tmp = (PyObject *)so;
1517 so = (PySetObject *)other;
1518 other = tmp;
1519 }
1520 while (set_next((PySetObject *)other, &pos, &entry)) {
1521 int rv = set_contains_entry(so, entry);
1522 if (rv == -1)
1523 return NULL;
1524 if (rv)
1525 Py_RETURN_FALSE;
1526 }
1527 Py_RETURN_TRUE;
1528 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 it = PyObject_GetIter(other);
1531 if (it == NULL)
1532 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 while ((key = PyIter_Next(it)) != NULL) {
1535 int rv;
1536 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001537 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 if (hash == -1) {
1540 Py_DECREF(key);
1541 Py_DECREF(it);
1542 return NULL;
1543 }
1544 entry.hash = hash;
1545 entry.key = key;
1546 rv = set_contains_entry(so, &entry);
1547 Py_DECREF(key);
1548 if (rv == -1) {
1549 Py_DECREF(it);
1550 return NULL;
1551 }
1552 if (rv) {
1553 Py_DECREF(it);
1554 Py_RETURN_FALSE;
1555 }
1556 }
1557 Py_DECREF(it);
1558 if (PyErr_Occurred())
1559 return NULL;
1560 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001561}
1562
1563PyDoc_STRVAR(isdisjoint_doc,
1564"Return True if two sets have a null intersection.");
1565
Neal Norwitz6576bd82005-11-13 18:41:28 +00001566static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001567set_difference_update_internal(PySetObject *so, PyObject *other)
1568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 if ((PyObject *)so == other)
1570 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 if (PyAnySet_Check(other)) {
1573 setentry *entry;
1574 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 while (set_next((PySetObject *)other, &pos, &entry))
1577 if (set_discard_entry(so, entry) == -1)
1578 return -1;
1579 } else {
1580 PyObject *key, *it;
1581 it = PyObject_GetIter(other);
1582 if (it == NULL)
1583 return -1;
1584
1585 while ((key = PyIter_Next(it)) != NULL) {
1586 if (set_discard_key(so, key) == -1) {
1587 Py_DECREF(it);
1588 Py_DECREF(key);
1589 return -1;
1590 }
1591 Py_DECREF(key);
1592 }
1593 Py_DECREF(it);
1594 if (PyErr_Occurred())
1595 return -1;
1596 }
1597 /* If more than 1/5 are dummies, then resize them away. */
1598 if ((so->fill - so->used) * 5 < so->mask)
1599 return 0;
1600 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001601}
1602
Raymond Hettingera690a992003-11-16 16:17:49 +00001603static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001604set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1609 PyObject *other = PyTuple_GET_ITEM(args, i);
1610 if (set_difference_update_internal(so, other) == -1)
1611 return NULL;
1612 }
1613 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001614}
1615
1616PyDoc_STRVAR(difference_update_doc,
1617"Remove all elements of another set from this set.");
1618
1619static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001620set_copy_and_difference(PySetObject *so, PyObject *other)
1621{
1622 PyObject *result;
1623
1624 result = set_copy(so);
1625 if (result == NULL)
1626 return NULL;
1627 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1628 return result;
1629 Py_DECREF(result);
1630 return NULL;
1631}
1632
1633static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001634set_difference(PySetObject *so, PyObject *other)
1635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 PyObject *result;
1637 setentry *entry;
1638 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001641 return set_copy_and_difference(so, other);
1642 }
1643
1644 /* If len(so) much more than len(other), it's more efficient to simply copy
1645 * so and then iterate other looking for common elements. */
1646 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1647 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 result = make_new_set_basetype(Py_TYPE(so), NULL);
1651 if (result == NULL)
1652 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 if (PyDict_CheckExact(other)) {
1655 while (set_next(so, &pos, &entry)) {
1656 setentry entrycopy;
1657 entrycopy.hash = entry->hash;
1658 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001659 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1661 Py_DECREF(result);
1662 return NULL;
1663 }
1664 }
1665 }
1666 return result;
1667 }
1668
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001669 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 while (set_next(so, &pos, &entry)) {
1671 int rv = set_contains_entry((PySetObject *)other, entry);
1672 if (rv == -1) {
1673 Py_DECREF(result);
1674 return NULL;
1675 }
1676 if (!rv) {
1677 if (set_add_entry((PySetObject *)result, entry) == -1) {
1678 Py_DECREF(result);
1679 return NULL;
1680 }
1681 }
1682 }
1683 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001684}
1685
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001686static PyObject *
1687set_difference_multi(PySetObject *so, PyObject *args)
1688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 Py_ssize_t i;
1690 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 if (PyTuple_GET_SIZE(args) == 0)
1693 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 other = PyTuple_GET_ITEM(args, 0);
1696 result = set_difference(so, other);
1697 if (result == NULL)
1698 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1701 other = PyTuple_GET_ITEM(args, i);
1702 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1703 Py_DECREF(result);
1704 return NULL;
1705 }
1706 }
1707 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001708}
1709
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001710PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001711"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001712\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001713(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001714static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001715set_sub(PySetObject *so, PyObject *other)
1716{
Brian Curtindfc80e32011-08-10 20:28:54 -05001717 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1718 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001720}
1721
1722static PyObject *
1723set_isub(PySetObject *so, PyObject *other)
1724{
Brian Curtindfc80e32011-08-10 20:28:54 -05001725 if (!PyAnySet_Check(other))
1726 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 if (set_difference_update_internal(so, other) == -1)
1728 return NULL;
1729 Py_INCREF(so);
1730 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001731}
1732
1733static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001734set_symmetric_difference_update(PySetObject *so, PyObject *other)
1735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 PySetObject *otherset;
1737 PyObject *key;
1738 Py_ssize_t pos = 0;
1739 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 if ((PyObject *)so == other)
1742 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 if (PyDict_CheckExact(other)) {
1745 PyObject *value;
1746 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001747 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1749 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001750
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001751 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 an_entry.hash = hash;
1753 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001756 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001757 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001760 if (rv == DISCARD_NOTFOUND) {
1761 if (set_add_entry(so, &an_entry) == -1) {
1762 Py_DECREF(key);
1763 return NULL;
1764 }
1765 }
Georg Brandl2d444492010-09-03 10:52:55 +00001766 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 }
1768 Py_RETURN_NONE;
1769 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (PyAnySet_Check(other)) {
1772 Py_INCREF(other);
1773 otherset = (PySetObject *)other;
1774 } else {
1775 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1776 if (otherset == NULL)
1777 return NULL;
1778 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 while (set_next(otherset, &pos, &entry)) {
1781 int rv = set_discard_entry(so, entry);
1782 if (rv == -1) {
1783 Py_DECREF(otherset);
1784 return NULL;
1785 }
1786 if (rv == DISCARD_NOTFOUND) {
1787 if (set_add_entry(so, entry) == -1) {
1788 Py_DECREF(otherset);
1789 return NULL;
1790 }
1791 }
1792 }
1793 Py_DECREF(otherset);
1794 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001795}
1796
1797PyDoc_STRVAR(symmetric_difference_update_doc,
1798"Update a set with the symmetric difference of itself and another.");
1799
1800static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001801set_symmetric_difference(PySetObject *so, PyObject *other)
1802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 PyObject *rv;
1804 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1807 if (otherset == NULL)
1808 return NULL;
1809 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1810 if (rv == NULL)
1811 return NULL;
1812 Py_DECREF(rv);
1813 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001814}
1815
1816PyDoc_STRVAR(symmetric_difference_doc,
1817"Return the symmetric difference of two sets as a new set.\n\
1818\n\
1819(i.e. all elements that are in exactly one of the sets.)");
1820
1821static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001822set_xor(PySetObject *so, PyObject *other)
1823{
Brian Curtindfc80e32011-08-10 20:28:54 -05001824 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1825 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001827}
1828
1829static PyObject *
1830set_ixor(PySetObject *so, PyObject *other)
1831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001833
Brian Curtindfc80e32011-08-10 20:28:54 -05001834 if (!PyAnySet_Check(other))
1835 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 result = set_symmetric_difference_update(so, other);
1837 if (result == NULL)
1838 return NULL;
1839 Py_DECREF(result);
1840 Py_INCREF(so);
1841 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001842}
1843
1844static PyObject *
1845set_issubset(PySetObject *so, PyObject *other)
1846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 setentry *entry;
1848 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 if (!PyAnySet_Check(other)) {
1851 PyObject *tmp, *result;
1852 tmp = make_new_set(&PySet_Type, other);
1853 if (tmp == NULL)
1854 return NULL;
1855 result = set_issubset(so, tmp);
1856 Py_DECREF(tmp);
1857 return result;
1858 }
1859 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1860 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 while (set_next(so, &pos, &entry)) {
1863 int rv = set_contains_entry((PySetObject *)other, entry);
1864 if (rv == -1)
1865 return NULL;
1866 if (!rv)
1867 Py_RETURN_FALSE;
1868 }
1869 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001870}
1871
1872PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1873
1874static PyObject *
1875set_issuperset(PySetObject *so, PyObject *other)
1876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 if (!PyAnySet_Check(other)) {
1880 tmp = make_new_set(&PySet_Type, other);
1881 if (tmp == NULL)
1882 return NULL;
1883 result = set_issuperset(so, tmp);
1884 Py_DECREF(tmp);
1885 return result;
1886 }
1887 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001888}
1889
1890PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1891
Raymond Hettingera690a992003-11-16 16:17:49 +00001892static PyObject *
1893set_richcompare(PySetObject *v, PyObject *w, int op)
1894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001896
Brian Curtindfc80e32011-08-10 20:28:54 -05001897 if(!PyAnySet_Check(w))
1898 Py_RETURN_NOTIMPLEMENTED;
1899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 switch (op) {
1901 case Py_EQ:
1902 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1903 Py_RETURN_FALSE;
1904 if (v->hash != -1 &&
1905 ((PySetObject *)w)->hash != -1 &&
1906 v->hash != ((PySetObject *)w)->hash)
1907 Py_RETURN_FALSE;
1908 return set_issubset(v, w);
1909 case Py_NE:
1910 r1 = set_richcompare(v, w, Py_EQ);
1911 if (r1 == NULL)
1912 return NULL;
1913 r2 = PyBool_FromLong(PyObject_Not(r1));
1914 Py_DECREF(r1);
1915 return r2;
1916 case Py_LE:
1917 return set_issubset(v, w);
1918 case Py_GE:
1919 return set_issuperset(v, w);
1920 case Py_LT:
1921 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1922 Py_RETURN_FALSE;
1923 return set_issubset(v, w);
1924 case Py_GT:
1925 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1926 Py_RETURN_FALSE;
1927 return set_issuperset(v, w);
1928 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001929 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001930}
1931
Raymond Hettingera690a992003-11-16 16:17:49 +00001932static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001933set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 if (set_add_key(so, key) == -1)
1936 return NULL;
1937 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001938}
1939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001941"Add an element to a set.\n\
1942\n\
1943This has no effect if the element is already present.");
1944
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001945static int
1946set_contains(PySetObject *so, PyObject *key)
1947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 PyObject *tmpkey;
1949 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 rv = set_contains_key(so, key);
1952 if (rv == -1) {
1953 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1954 return -1;
1955 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001956 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 if (tmpkey == NULL)
1958 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001959 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 Py_DECREF(tmpkey);
1961 }
1962 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001963}
1964
1965static PyObject *
1966set_direct_contains(PySetObject *so, PyObject *key)
1967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 result = set_contains(so, key);
1971 if (result == -1)
1972 return NULL;
1973 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001974}
1975
1976PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1977
Raymond Hettingera690a992003-11-16 16:17:49 +00001978static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001979set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 PyObject *tmpkey;
1982 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 rv = set_discard_key(so, key);
1985 if (rv == -1) {
1986 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1987 return NULL;
1988 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001989 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 if (tmpkey == NULL)
1991 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 Py_DECREF(tmpkey);
1994 if (rv == -1)
1995 return NULL;
1996 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 if (rv == DISCARD_NOTFOUND) {
1999 set_key_error(key);
2000 return NULL;
2001 }
2002 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00002003}
2004
2005PyDoc_STRVAR(remove_doc,
2006"Remove an element from a set; it must be a member.\n\
2007\n\
2008If the element is not a member, raise a KeyError.");
2009
2010static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00002011set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00002012{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04002013 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00002015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 rv = set_discard_key(so, key);
2017 if (rv == -1) {
2018 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
2019 return NULL;
2020 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00002021 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 if (tmpkey == NULL)
2023 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002024 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02002026 if (rv == -1)
2027 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 }
2029 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00002030}
2031
2032PyDoc_STRVAR(discard_doc,
2033"Remove an element from a set if it is a member.\n\
2034\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002036
2037static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00002038set_reduce(PySetObject *so)
2039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002041 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 keys = PySequence_List((PyObject *)so);
2044 if (keys == NULL)
2045 goto done;
2046 args = PyTuple_Pack(1, keys);
2047 if (args == NULL)
2048 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002049 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 if (dict == NULL) {
2051 PyErr_Clear();
2052 dict = Py_None;
2053 Py_INCREF(dict);
2054 }
2055 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00002056done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 Py_XDECREF(args);
2058 Py_XDECREF(keys);
2059 Py_XDECREF(dict);
2060 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00002061}
2062
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002063static PyObject *
2064set_sizeof(PySetObject *so)
2065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 res = sizeof(PySetObject);
2069 if (so->table != so->smalltable)
2070 res = res + (so->mask + 1) * sizeof(setentry);
2071 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002072}
2073
2074PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002075static int
2076set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 if (!PyAnySet_Check(self))
2081 return -1;
2082 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2083 return -1;
2084 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2085 return -1;
2086 set_clear_internal(self);
2087 self->hash = -1;
2088 if (iterable == NULL)
2089 return 0;
2090 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002091}
2092
Raymond Hettingera690a992003-11-16 16:17:49 +00002093static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 set_len, /* sq_length */
2095 0, /* sq_concat */
2096 0, /* sq_repeat */
2097 0, /* sq_item */
2098 0, /* sq_slice */
2099 0, /* sq_ass_item */
2100 0, /* sq_ass_slice */
2101 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002102};
2103
2104/* set object ********************************************************/
2105
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002106#ifdef Py_DEBUG
2107static PyObject *test_c_api(PySetObject *so);
2108
2109PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2110All is well if assertions don't fail.");
2111#endif
2112
Raymond Hettingera690a992003-11-16 16:17:49 +00002113static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 {"add", (PyCFunction)set_add, METH_O,
2115 add_doc},
2116 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2117 clear_doc},
2118 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2119 contains_doc},
2120 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2121 copy_doc},
2122 {"discard", (PyCFunction)set_discard, METH_O,
2123 discard_doc},
2124 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2125 difference_doc},
2126 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2127 difference_update_doc},
2128 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2129 intersection_doc},
2130 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2131 intersection_update_doc},
2132 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2133 isdisjoint_doc},
2134 {"issubset", (PyCFunction)set_issubset, METH_O,
2135 issubset_doc},
2136 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2137 issuperset_doc},
2138 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2139 pop_doc},
2140 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2141 reduce_doc},
2142 {"remove", (PyCFunction)set_remove, METH_O,
2143 remove_doc},
2144 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2145 sizeof_doc},
2146 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2147 symmetric_difference_doc},
2148 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2149 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002150#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2152 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002153#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 {"union", (PyCFunction)set_union, METH_VARARGS,
2155 union_doc},
2156 {"update", (PyCFunction)set_update, METH_VARARGS,
2157 update_doc},
2158 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002159};
2160
2161static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 0, /*nb_add*/
2163 (binaryfunc)set_sub, /*nb_subtract*/
2164 0, /*nb_multiply*/
2165 0, /*nb_remainder*/
2166 0, /*nb_divmod*/
2167 0, /*nb_power*/
2168 0, /*nb_negative*/
2169 0, /*nb_positive*/
2170 0, /*nb_absolute*/
2171 0, /*nb_bool*/
2172 0, /*nb_invert*/
2173 0, /*nb_lshift*/
2174 0, /*nb_rshift*/
2175 (binaryfunc)set_and, /*nb_and*/
2176 (binaryfunc)set_xor, /*nb_xor*/
2177 (binaryfunc)set_or, /*nb_or*/
2178 0, /*nb_int*/
2179 0, /*nb_reserved*/
2180 0, /*nb_float*/
2181 0, /*nb_inplace_add*/
2182 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2183 0, /*nb_inplace_multiply*/
2184 0, /*nb_inplace_remainder*/
2185 0, /*nb_inplace_power*/
2186 0, /*nb_inplace_lshift*/
2187 0, /*nb_inplace_rshift*/
2188 (binaryfunc)set_iand, /*nb_inplace_and*/
2189 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2190 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002191};
2192
2193PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002194"set() -> new empty set object\n\
2195set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002196\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002197Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002198
2199PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2201 "set", /* tp_name */
2202 sizeof(PySetObject), /* tp_basicsize */
2203 0, /* tp_itemsize */
2204 /* methods */
2205 (destructor)set_dealloc, /* tp_dealloc */
2206 0, /* tp_print */
2207 0, /* tp_getattr */
2208 0, /* tp_setattr */
2209 0, /* tp_reserved */
2210 (reprfunc)set_repr, /* tp_repr */
2211 &set_as_number, /* tp_as_number */
2212 &set_as_sequence, /* tp_as_sequence */
2213 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002214 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 0, /* tp_call */
2216 0, /* tp_str */
2217 PyObject_GenericGetAttr, /* tp_getattro */
2218 0, /* tp_setattro */
2219 0, /* tp_as_buffer */
2220 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2221 Py_TPFLAGS_BASETYPE, /* tp_flags */
2222 set_doc, /* tp_doc */
2223 (traverseproc)set_traverse, /* tp_traverse */
2224 (inquiry)set_clear_internal, /* tp_clear */
2225 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002226 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2227 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 0, /* tp_iternext */
2229 set_methods, /* tp_methods */
2230 0, /* tp_members */
2231 0, /* tp_getset */
2232 0, /* tp_base */
2233 0, /* tp_dict */
2234 0, /* tp_descr_get */
2235 0, /* tp_descr_set */
2236 0, /* tp_dictoffset */
2237 (initproc)set_init, /* tp_init */
2238 PyType_GenericAlloc, /* tp_alloc */
2239 set_new, /* tp_new */
2240 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002241};
2242
2243/* frozenset object ********************************************************/
2244
2245
2246static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2248 contains_doc},
2249 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2250 copy_doc},
2251 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2252 difference_doc},
2253 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2254 intersection_doc},
2255 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2256 isdisjoint_doc},
2257 {"issubset", (PyCFunction)set_issubset, METH_O,
2258 issubset_doc},
2259 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2260 issuperset_doc},
2261 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2262 reduce_doc},
2263 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2264 sizeof_doc},
2265 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2266 symmetric_difference_doc},
2267 {"union", (PyCFunction)set_union, METH_VARARGS,
2268 union_doc},
2269 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002270};
2271
2272static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 0, /*nb_add*/
2274 (binaryfunc)set_sub, /*nb_subtract*/
2275 0, /*nb_multiply*/
2276 0, /*nb_remainder*/
2277 0, /*nb_divmod*/
2278 0, /*nb_power*/
2279 0, /*nb_negative*/
2280 0, /*nb_positive*/
2281 0, /*nb_absolute*/
2282 0, /*nb_bool*/
2283 0, /*nb_invert*/
2284 0, /*nb_lshift*/
2285 0, /*nb_rshift*/
2286 (binaryfunc)set_and, /*nb_and*/
2287 (binaryfunc)set_xor, /*nb_xor*/
2288 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002289};
2290
2291PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002292"frozenset() -> empty frozenset object\n\
2293frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002294\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002295Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002296
2297PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2299 "frozenset", /* tp_name */
2300 sizeof(PySetObject), /* tp_basicsize */
2301 0, /* tp_itemsize */
2302 /* methods */
2303 (destructor)set_dealloc, /* tp_dealloc */
2304 0, /* tp_print */
2305 0, /* tp_getattr */
2306 0, /* tp_setattr */
2307 0, /* tp_reserved */
2308 (reprfunc)set_repr, /* tp_repr */
2309 &frozenset_as_number, /* tp_as_number */
2310 &set_as_sequence, /* tp_as_sequence */
2311 0, /* tp_as_mapping */
2312 frozenset_hash, /* tp_hash */
2313 0, /* tp_call */
2314 0, /* tp_str */
2315 PyObject_GenericGetAttr, /* tp_getattro */
2316 0, /* tp_setattro */
2317 0, /* tp_as_buffer */
2318 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2319 Py_TPFLAGS_BASETYPE, /* tp_flags */
2320 frozenset_doc, /* tp_doc */
2321 (traverseproc)set_traverse, /* tp_traverse */
2322 (inquiry)set_clear_internal, /* tp_clear */
2323 (richcmpfunc)set_richcompare, /* tp_richcompare */
2324 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2325 (getiterfunc)set_iter, /* tp_iter */
2326 0, /* tp_iternext */
2327 frozenset_methods, /* tp_methods */
2328 0, /* tp_members */
2329 0, /* tp_getset */
2330 0, /* tp_base */
2331 0, /* tp_dict */
2332 0, /* tp_descr_get */
2333 0, /* tp_descr_set */
2334 0, /* tp_dictoffset */
2335 0, /* tp_init */
2336 PyType_GenericAlloc, /* tp_alloc */
2337 frozenset_new, /* tp_new */
2338 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002339};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002340
2341
2342/***** C API functions *************************************************/
2343
2344PyObject *
2345PySet_New(PyObject *iterable)
2346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002348}
2349
2350PyObject *
2351PyFrozenSet_New(PyObject *iterable)
2352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002354}
2355
Neal Norwitz8c49c822006-03-04 18:41:19 +00002356Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002357PySet_Size(PyObject *anyset)
2358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 if (!PyAnySet_Check(anyset)) {
2360 PyErr_BadInternalCall();
2361 return -1;
2362 }
2363 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002364}
2365
2366int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002367PySet_Clear(PyObject *set)
2368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 if (!PySet_Check(set)) {
2370 PyErr_BadInternalCall();
2371 return -1;
2372 }
2373 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002374}
2375
2376int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002377PySet_Contains(PyObject *anyset, PyObject *key)
2378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 if (!PyAnySet_Check(anyset)) {
2380 PyErr_BadInternalCall();
2381 return -1;
2382 }
2383 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002384}
2385
2386int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002387PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 if (!PySet_Check(set)) {
2390 PyErr_BadInternalCall();
2391 return -1;
2392 }
2393 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002394}
2395
2396int
Christian Heimesfd66e512008-01-29 12:18:50 +00002397PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 if (!PySet_Check(anyset) &&
2400 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2401 PyErr_BadInternalCall();
2402 return -1;
2403 }
2404 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002405}
2406
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002407int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002408_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 if (!PyAnySet_Check(set)) {
2413 PyErr_BadInternalCall();
2414 return -1;
2415 }
2416 if (set_next((PySetObject *)set, pos, &entry) == 0)
2417 return 0;
2418 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002419 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002421}
2422
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002423PyObject *
2424PySet_Pop(PyObject *set)
2425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 if (!PySet_Check(set)) {
2427 PyErr_BadInternalCall();
2428 return NULL;
2429 }
2430 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002431}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002432
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002433int
2434_PySet_Update(PyObject *set, PyObject *iterable)
2435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 if (!PySet_Check(set)) {
2437 PyErr_BadInternalCall();
2438 return -1;
2439 }
2440 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002441}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002442
2443#ifdef Py_DEBUG
2444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002446 Returns True and original set is restored. */
2447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448#define assertRaises(call_return_value, exception) \
2449 do { \
2450 assert(call_return_value); \
2451 assert(PyErr_ExceptionMatches(exception)); \
2452 PyErr_Clear(); \
2453 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002454
2455static PyObject *
2456test_c_api(PySetObject *so)
2457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 Py_ssize_t count;
2459 char *s;
2460 Py_ssize_t i;
2461 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2462 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002463 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 /* Verify preconditions */
2467 assert(PyAnySet_Check(ob));
2468 assert(PyAnySet_CheckExact(ob));
2469 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 /* so.clear(); so |= set("abc"); */
2472 str = PyUnicode_FromString("abc");
2473 if (str == NULL)
2474 return NULL;
2475 set_clear_internal(so);
2476 if (set_update_internal(so, str) == -1) {
2477 Py_DECREF(str);
2478 return NULL;
2479 }
2480 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* Exercise type/size checks */
2483 assert(PySet_Size(ob) == 3);
2484 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* Raise TypeError for non-iterable constructor arguments */
2487 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2488 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 /* Raise TypeError for unhashable key */
2491 dup = PySet_New(ob);
2492 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2493 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2494 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 /* Exercise successful pop, contains, add, and discard */
2497 elem = PySet_Pop(ob);
2498 assert(PySet_Contains(ob, elem) == 0);
2499 assert(PySet_GET_SIZE(ob) == 2);
2500 assert(PySet_Add(ob, elem) == 0);
2501 assert(PySet_Contains(ob, elem) == 1);
2502 assert(PySet_GET_SIZE(ob) == 3);
2503 assert(PySet_Discard(ob, elem) == 1);
2504 assert(PySet_GET_SIZE(ob) == 2);
2505 assert(PySet_Discard(ob, elem) == 0);
2506 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 /* Exercise clear */
2509 dup2 = PySet_New(dup);
2510 assert(PySet_Clear(dup2) == 0);
2511 assert(PySet_Size(dup2) == 0);
2512 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 /* Raise SystemError on clear or update of frozen set */
2515 f = PyFrozenSet_New(dup);
2516 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2517 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2518 assert(PySet_Add(f, elem) == 0);
2519 Py_INCREF(f);
2520 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2521 Py_DECREF(f);
2522 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 /* Exercise direct iteration */
2525 i = 0, count = 0;
2526 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2527 s = _PyUnicode_AsString(x);
2528 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2529 count++;
2530 }
2531 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 /* Exercise updates */
2534 dup2 = PySet_New(NULL);
2535 assert(_PySet_Update(dup2, dup) == 0);
2536 assert(PySet_Size(dup2) == 3);
2537 assert(_PySet_Update(dup2, dup) == 0);
2538 assert(PySet_Size(dup2) == 3);
2539 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 /* Raise SystemError when self argument is not a set or frozenset. */
2542 t = PyTuple_New(0);
2543 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2544 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2545 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 /* Raise SystemError when self argument is not a set. */
2548 f = PyFrozenSet_New(dup);
2549 assert(PySet_Size(f) == 3);
2550 assert(PyFrozenSet_CheckExact(f));
2551 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2552 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2553 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 /* Raise KeyError when popping from an empty set */
2556 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2557 Py_DECREF(ob);
2558 assert(PySet_GET_SIZE(ob) == 0);
2559 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 /* Restore the set from the copy using the PyNumber API */
2562 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2563 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 /* Verify constructors accept NULL arguments */
2566 f = PySet_New(NULL);
2567 assert(f != NULL);
2568 assert(PySet_GET_SIZE(f) == 0);
2569 Py_DECREF(f);
2570 f = PyFrozenSet_New(NULL);
2571 assert(f != NULL);
2572 assert(PyFrozenSet_CheckExact(f));
2573 assert(PySet_GET_SIZE(f) == 0);
2574 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 Py_DECREF(elem);
2577 Py_DECREF(dup);
2578 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002579}
2580
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002581#undef assertRaises
2582
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002583#endif