blob: ff832d55bb50f1e1306b096e97161ebafe5104a7 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettingerbd9b2002015-01-18 16:10:30 -08007 Copyright (c) 2003-2015 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
Raymond Hettinger4d45c102015-01-25 16:27:40 -080026 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070027 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070055 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t perturb = hash;
58 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080059 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070060 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070061 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080063 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070064 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070066
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070067 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080068 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070069 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080070 /* startkey cannot be a dummy because the dummy hash field is -1 */
71 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080072 if (startkey == key)
73 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080074 if (PyUnicode_CheckExact(startkey)
75 && PyUnicode_CheckExact(key)
76 && unicode_eq(startkey, key))
77 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 Py_INCREF(startkey);
79 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
80 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010081 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010083 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010085 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070086 return entry;
87 }
Raymond Hettingerf8d1a312015-01-26 22:06:43 -080088 if (entry->key == dummy && freeslot == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -070089 freeslot = entry;
90
Raymond Hettingerc658d852015-02-02 08:35:00 -080091 if (i + LINEAR_PROBES <= mask) {
92 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
93 entry++;
94 if (entry->key == NULL)
95 goto found_null;
96 if (entry->hash == hash) {
97 PyObject *startkey = entry->key;
98 assert(startkey != dummy);
99 if (startkey == key)
100 return entry;
101 if (PyUnicode_CheckExact(startkey)
102 && PyUnicode_CheckExact(key)
103 && unicode_eq(startkey, key))
104 return entry;
105 Py_INCREF(startkey);
106 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
107 Py_DECREF(startkey);
108 if (cmp < 0)
109 return NULL;
110 if (table != so->table || entry->key != startkey)
111 return set_lookkey(so, key, hash);
112 if (cmp > 0)
113 return entry;
114 }
115 if (entry->key == dummy && freeslot == NULL)
116 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700117 }
Raymond Hettingerc658d852015-02-02 08:35:00 -0800118 } else {
119 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
120 entry = &table[(i + j) & mask];
121 if (entry->key == NULL)
122 goto found_null;
123 if (entry->hash == hash) {
124 PyObject *startkey = entry->key;
125 assert(startkey != dummy);
126 if (startkey == key)
127 return entry;
128 if (PyUnicode_CheckExact(startkey)
129 && PyUnicode_CheckExact(key)
130 && unicode_eq(startkey, key))
131 return entry;
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 return set_lookkey(so, key, hash);
139 if (cmp > 0)
140 return entry;
141 }
142 if (entry->key == dummy && freeslot == NULL)
143 freeslot = entry;
144 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700146
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700147 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800148 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700149
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800150 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700151 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700152 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700154 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700155 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000156}
157
158/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000159Internal routine used by set_table_resize() to insert an item which is
160known to be absent from the set. This routine also assumes that
161the set contains no deleted entries. Besides the performance benefit,
162using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
163Note that no refcounts are changed by this routine; if needed, the caller
164is responsible for incref'ing `key`.
165*/
166static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200167set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200170 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700171 size_t perturb = hash;
172 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800173 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700174 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000175
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700176 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800177 entry = &table[i];
178 if (entry->key == NULL)
179 goto found_null;
180 if (i + LINEAR_PROBES <= mask) {
181 for (j = 1; j <= LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800182 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800183 if (entry->key == NULL)
184 goto found_null;
185 }
186 } else {
187 for (j = 1; j <= LINEAR_PROBES; j++) {
188 entry = &table[(i + j) & mask];
189 if (entry->key == NULL)
190 goto found_null;
191 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700192 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700193 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800194 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700196 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 entry->key = key;
198 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700199 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000201}
202
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700203/* ======== End logic for probing the hash table ========================== */
204/* ======================================================================== */
205
206
207/*
208Internal routine to insert a new key into the table.
209Used by the public insert routine.
210Eats a reference to key.
211*/
212static int
213set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
214{
215 setentry *entry;
216
Raymond Hettinger93035c42015-01-25 16:12:49 -0800217 entry = set_lookkey(so, key, hash);
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700218 if (entry == NULL)
219 return -1;
220 if (entry->key == NULL) {
221 /* UNUSED */
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700222 entry->key = key;
223 entry->hash = hash;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800224 so->fill++;
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700225 so->used++;
226 } else if (entry->key == dummy) {
227 /* DUMMY */
228 entry->key = key;
229 entry->hash = hash;
230 so->used++;
231 } else {
232 /* ACTIVE */
233 Py_DECREF(key);
234 }
235 return 0;
236}
237
Thomas Wouters89f507f2006-12-13 04:49:30 +0000238/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000239Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000240keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000241actually be smaller than the old one.
242*/
243static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000244set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 Py_ssize_t newsize;
247 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800248 Py_ssize_t oldfill = so->fill;
249 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 int is_oldtable_malloced;
251 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100256 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 for (newsize = PySet_MINSIZE;
258 newsize <= minused && newsize > 0;
259 newsize <<= 1)
260 ;
261 if (newsize <= 0) {
262 PyErr_NoMemory();
263 return -1;
264 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 /* Get space for a new table. */
267 oldtable = so->table;
268 assert(oldtable != NULL);
269 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 if (newsize == PySet_MINSIZE) {
272 /* A large table is shrinking, or we can't get any smaller. */
273 newtable = so->smalltable;
274 if (newtable == oldtable) {
275 if (so->fill == so->used) {
276 /* No dummies, so no point doing anything. */
277 return 0;
278 }
279 /* We're not going to resize it, but rebuild the
280 table anyway to purge old dummy entries.
281 Subtle: This is *necessary* if fill==size,
282 as set_lookkey needs at least one virgin slot to
283 terminate failing searches. If fill < size, it's
284 merely desirable, as dummies slow searches. */
285 assert(so->fill > so->used);
286 memcpy(small_copy, oldtable, sizeof(small_copy));
287 oldtable = small_copy;
288 }
289 }
290 else {
291 newtable = PyMem_NEW(setentry, newsize);
292 if (newtable == NULL) {
293 PyErr_NoMemory();
294 return -1;
295 }
296 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 /* Make the set empty, using the new table. */
299 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800302 so->used = 0;
303 so->mask = newsize - 1;
304 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 /* Copy the data over; this is refcount-neutral for active entries;
307 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800308 if (oldfill == oldused) {
309 for (entry = oldtable; oldused > 0; entry++) {
310 if (entry->key != NULL) {
311 oldused--;
312 set_insert_clean(so, entry->key, entry->hash);
313 }
314 }
315 } else {
316 for (entry = oldtable; oldused > 0; entry++) {
317 if (entry->key != NULL && entry->key != dummy) {
318 oldused--;
319 set_insert_clean(so, entry->key, entry->hash);
320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 }
322 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 if (is_oldtable_malloced)
325 PyMem_DEL(oldtable);
326 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000327}
328
Raymond Hettingerc991db22005-08-11 07:58:45 +0000329/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
330
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000331static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200332set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000333{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200334 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000335 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100336 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 assert(so->fill <= so->mask); /* at least one empty slot */
339 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000340 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100341 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000342 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 return -1;
344 }
345 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
346 return 0;
347 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000348}
349
350static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200351set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000352{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800353 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200354 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200357 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 hash = PyObject_Hash(key);
359 if (hash == -1)
360 return -1;
361 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800362 entry.key = key;
363 entry.hash = hash;
364 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000365}
366
367#define DISCARD_NOTFOUND 0
368#define DISCARD_FOUND 1
369
370static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000371set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800372{
373 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000375
Raymond Hettinger93035c42015-01-25 16:12:49 -0800376 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (entry == NULL)
378 return -1;
379 if (entry->key == NULL || entry->key == dummy)
380 return DISCARD_NOTFOUND;
381 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800383 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 so->used--;
385 Py_DECREF(old_key);
386 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000387}
388
389static int
390set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000391{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800392 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200393 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200398 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 hash = PyObject_Hash(key);
400 if (hash == -1)
401 return -1;
402 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800403 entry.key = key;
404 entry.hash = hash;
405 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000406}
407
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700408static void
409set_empty_to_minsize(PySetObject *so)
410{
411 memset(so->smalltable, 0, sizeof(so->smalltable));
412 so->fill = 0;
413 so->used = 0;
414 so->mask = PySet_MINSIZE - 1;
415 so->table = so->smalltable;
416 so->hash = -1;
417}
418
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000419static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000420set_clear_internal(PySetObject *so)
421{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800422 setentry *entry;
423 setentry *table = so->table;
424 Py_ssize_t fill = so->fill;
425 Py_ssize_t used = so->used;
426 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000428
Raymond Hettinger583cd032013-09-07 22:06:35 -0700429 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 /* This is delicate. During the process of clearing the set,
433 * decrefs can cause the set to mutate. To avoid fatal confusion
434 * (voice of experience), we have to make the set empty before
435 * clearing the slots, and never refer to anything via so->ref while
436 * clearing.
437 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700439 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 else if (fill > 0) {
442 /* It's a small table with something that needs to be cleared.
443 * Afraid the only safe way is to copy the set entries into
444 * another small table first.
445 */
446 memcpy(small_copy, table, sizeof(small_copy));
447 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700448 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 }
450 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 /* Now we can finally clear things. If C had refcounts, we could
453 * assert that the refcount on table is 1 now, i.e. that this function
454 * has unique access to it, so decref side-effects can't alter it.
455 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800456 for (entry = table; used > 0; entry++) {
457 if (entry->key && entry->key != dummy) {
458 used--;
459 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (table_is_malloced)
464 PyMem_DEL(table);
465 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000466}
467
468/*
469 * Iterate over a set table. Use like so:
470 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000471 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000472 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000473 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000474 * while (set_next(yourset, &pos, &entry)) {
475 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476 * }
477 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000478 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000480 */
481static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 Py_ssize_t i;
485 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200486 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 assert (PyAnySet_Check(so));
489 i = *pos_ptr;
490 assert(i >= 0);
491 table = so->table;
492 mask = so->mask;
493 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
494 i++;
495 *pos_ptr = i+1;
496 if (i > mask)
497 return 0;
498 assert(table[i].key != NULL);
499 *entry_ptr = &table[i];
500 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000501}
502
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000503static void
504set_dealloc(PySetObject *so)
505{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200506 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800507 Py_ssize_t used = so->used;
508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 PyObject_GC_UnTrack(so);
510 Py_TRASHCAN_SAFE_BEGIN(so)
511 if (so->weakreflist != NULL)
512 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000513
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800514 for (entry = so->table; used > 0; entry++) {
515 if (entry->key && entry->key != dummy) {
516 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700517 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 }
519 }
520 if (so->table != so->smalltable)
521 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700522 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000524}
525
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000526static PyObject *
527set_repr(PySetObject *so)
528{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200529 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 if (status != 0) {
533 if (status < 0)
534 return NULL;
535 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
536 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 /* shortcut for the empty set */
539 if (!so->used) {
540 Py_ReprLeave((PyObject*)so);
541 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
542 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 keys = PySequence_List((PyObject *)so);
545 if (keys == NULL)
546 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000547
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200548 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 listrepr = PyObject_Repr(keys);
550 Py_DECREF(keys);
551 if (listrepr == NULL)
552 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200553 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200555 if (tmp == NULL)
556 goto done;
557 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200558
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200559 if (Py_TYPE(so) != &PySet_Type)
560 result = PyUnicode_FromFormat("%s({%U})",
561 Py_TYPE(so)->tp_name,
562 listrepr);
563 else
564 result = PyUnicode_FromFormat("{%U}", listrepr);
565 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000566done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 Py_ReprLeave((PyObject*)so);
568 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000569}
570
Martin v. Löwis18e16552006-02-15 17:27:45 +0000571static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000572set_len(PyObject *so)
573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000575}
576
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000577static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000578set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000581 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100582 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200583 Py_ssize_t i;
584 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 assert (PyAnySet_Check(so));
587 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 other = (PySetObject*)otherset;
590 if (other == so || other->used == 0)
591 /* a.update(a) or a.update({}); nothing to do */
592 return 0;
593 /* Do one big resize at the start, rather than
594 * incrementally resizing as we insert new keys. Expect
595 * that there will be no (or few) overlapping keys.
596 */
597 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
598 if (set_table_resize(so, (so->used + other->used)*2) != 0)
599 return -1;
600 }
601 for (i = 0; i <= other->mask; i++) {
602 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000603 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100604 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000605 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500606 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000607 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100608 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000609 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 return -1;
611 }
612 }
613 }
614 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000615}
616
617static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000618set_contains_entry(PySetObject *so, setentry *entry)
619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 PyObject *key;
621 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000622
Raymond Hettinger93035c42015-01-25 16:12:49 -0800623 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 if (lu_entry == NULL)
625 return -1;
626 key = lu_entry->key;
627 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000628}
629
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800630static int
631set_contains_key(PySetObject *so, PyObject *key)
632{
633 setentry entry;
634 Py_hash_t hash;
635
636 if (!PyUnicode_CheckExact(key) ||
637 (hash = ((PyASCIIObject *) key)->hash) == -1) {
638 hash = PyObject_Hash(key);
639 if (hash == -1)
640 return -1;
641 }
642 entry.key = key;
643 entry.hash = hash;
644 return set_contains_entry(so, &entry);
645}
646
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000647static PyObject *
648set_pop(PySetObject *so)
649{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800650 /* Make sure the search finger is in bounds */
651 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200652 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 assert (PyAnySet_Check(so));
656 if (so->used == 0) {
657 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
658 return NULL;
659 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000660
Raymond Hettinger1202a472015-01-18 13:12:42 -0800661 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
662 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800663 if (i > so->mask)
664 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 }
666 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800668 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800670 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000672}
673
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000674PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
675Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000676
677static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000678set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 Py_ssize_t pos = 0;
681 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 while (set_next(so, &pos, &entry))
684 Py_VISIT(entry->key);
685 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000686}
687
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000688static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000689frozenset_hash(PyObject *self)
690{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800691 /* Most of the constants in this hash algorithm are randomly choosen
692 large primes with "interesting bit patterns" and that passed
693 tests for good collision statistics on a variety of problematic
694 datasets such as:
695
696 ps = []
697 for r in range(21):
698 ps += itertools.combinations(range(20), r)
699 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
700
701 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800703 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 setentry *entry;
705 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 if (so->hash != -1)
708 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000709
Mark Dickinson57e683e2011-09-24 18:18:40 +0100710 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 while (set_next(so, &pos, &entry)) {
712 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800713 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 combinations of a small number of elements with nearby
715 hashes so that many distinct combinations collapse to only
716 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000717 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800718 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800720 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500721 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800722 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200723 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800724 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 so->hash = hash;
726 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000727}
728
Raymond Hettingera9d99362005-08-05 00:01:15 +0000729/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000730
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000731typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 PyObject_HEAD
733 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
734 Py_ssize_t si_used;
735 Py_ssize_t si_pos;
736 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000737} setiterobject;
738
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000739static void
740setiter_dealloc(setiterobject *si)
741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 Py_XDECREF(si->si_set);
743 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000744}
745
746static int
747setiter_traverse(setiterobject *si, visitproc visit, void *arg)
748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 Py_VISIT(si->si_set);
750 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000751}
752
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000753static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000754setiter_len(setiterobject *si)
755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 Py_ssize_t len = 0;
757 if (si->si_set != NULL && si->si_used == si->si_set->used)
758 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000759 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000760}
761
Armin Rigof5b3e362006-02-11 21:32:43 +0000762PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000763
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000764static PyObject *setiter_iternext(setiterobject *si);
765
766static PyObject *
767setiter_reduce(setiterobject *si)
768{
769 PyObject *list;
770 setiterobject tmp;
771
772 list = PyList_New(0);
773 if (!list)
774 return NULL;
775
Ezio Melotti0e1af282012-09-28 16:43:40 +0300776 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000777 tmp = *si;
778 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300779
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000780 /* iterate the temporary into a list */
781 for(;;) {
782 PyObject *element = setiter_iternext(&tmp);
783 if (element) {
784 if (PyList_Append(list, element)) {
785 Py_DECREF(element);
786 Py_DECREF(list);
787 Py_XDECREF(tmp.si_set);
788 return NULL;
789 }
790 Py_DECREF(element);
791 } else
792 break;
793 }
794 Py_XDECREF(tmp.si_set);
795 /* check for error */
796 if (tmp.si_set != NULL) {
797 /* we have an error */
798 Py_DECREF(list);
799 return NULL;
800 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200801 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000802}
803
804PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
805
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000806static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000808 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810};
811
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000812static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200815 Py_ssize_t i, mask;
816 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 if (so == NULL)
820 return NULL;
821 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 if (si->si_used != so->used) {
824 PyErr_SetString(PyExc_RuntimeError,
825 "Set changed size during iteration");
826 si->si_used = -1; /* Make this state sticky */
827 return NULL;
828 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 i = si->si_pos;
831 assert(i>=0);
832 entry = so->table;
833 mask = so->mask;
834 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
835 i++;
836 si->si_pos = i+1;
837 if (i > mask)
838 goto fail;
839 si->len--;
840 key = entry[i].key;
841 Py_INCREF(key);
842 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000843
844fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 Py_DECREF(so);
846 si->si_set = NULL;
847 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848}
849
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000850PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 PyVarObject_HEAD_INIT(&PyType_Type, 0)
852 "set_iterator", /* tp_name */
853 sizeof(setiterobject), /* tp_basicsize */
854 0, /* tp_itemsize */
855 /* methods */
856 (destructor)setiter_dealloc, /* tp_dealloc */
857 0, /* tp_print */
858 0, /* tp_getattr */
859 0, /* tp_setattr */
860 0, /* tp_reserved */
861 0, /* tp_repr */
862 0, /* tp_as_number */
863 0, /* tp_as_sequence */
864 0, /* tp_as_mapping */
865 0, /* tp_hash */
866 0, /* tp_call */
867 0, /* tp_str */
868 PyObject_GenericGetAttr, /* tp_getattro */
869 0, /* tp_setattro */
870 0, /* tp_as_buffer */
871 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
872 0, /* tp_doc */
873 (traverseproc)setiter_traverse, /* tp_traverse */
874 0, /* tp_clear */
875 0, /* tp_richcompare */
876 0, /* tp_weaklistoffset */
877 PyObject_SelfIter, /* tp_iter */
878 (iternextfunc)setiter_iternext, /* tp_iternext */
879 setiter_methods, /* tp_methods */
880 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000881};
882
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883static PyObject *
884set_iter(PySetObject *so)
885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
887 if (si == NULL)
888 return NULL;
889 Py_INCREF(so);
890 si->si_set = so;
891 si->si_used = so->used;
892 si->si_pos = 0;
893 si->len = so->used;
894 _PyObject_GC_TRACK(si);
895 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000896}
897
Raymond Hettingerd7946662005-08-01 21:39:29 +0000898static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000899set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (PyAnySet_Check(other))
904 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 if (PyDict_CheckExact(other)) {
907 PyObject *value;
908 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000909 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 /* Do one big resize at the start, rather than
913 * incrementally resizing as we insert new keys. Expect
914 * that there will be no (or few) overlapping keys.
915 */
916 if (dictsize == -1)
917 return -1;
918 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
919 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
920 return -1;
921 }
922 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
923 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 an_entry.hash = hash;
926 an_entry.key = key;
927 if (set_add_entry(so, &an_entry) == -1)
928 return -1;
929 }
930 return 0;
931 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 it = PyObject_GetIter(other);
934 if (it == NULL)
935 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 while ((key = PyIter_Next(it)) != NULL) {
938 if (set_add_key(so, key) == -1) {
939 Py_DECREF(it);
940 Py_DECREF(key);
941 return -1;
942 }
943 Py_DECREF(key);
944 }
945 Py_DECREF(it);
946 if (PyErr_Occurred())
947 return -1;
948 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949}
950
951static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000952set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
957 PyObject *other = PyTuple_GET_ITEM(args, i);
958 if (set_update_internal(so, other) == -1)
959 return NULL;
960 }
961 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000962}
963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000965"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000966
Raymond Hettinger426d9952014-05-18 21:40:20 +0100967/* XXX Todo:
968 If aligned memory allocations become available, make the
969 set object 64 byte aligned so that most of the fields
970 can be retrieved or updated in a single cache line.
971*/
972
Raymond Hettingera38123e2003-11-24 22:18:49 +0000973static PyObject *
974make_new_set(PyTypeObject *type, PyObject *iterable)
975{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200976 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700979 so = (PySetObject *)type->tp_alloc(type, 0);
980 if (so == NULL)
981 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000982
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700983 so->fill = 0;
984 so->used = 0;
985 so->mask = PySet_MINSIZE - 1;
986 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700987 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800988 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 if (iterable != NULL) {
992 if (set_update_internal(so, iterable) == -1) {
993 Py_DECREF(so);
994 return NULL;
995 }
996 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +0000999}
1000
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001001static PyObject *
1002make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1005 if (PyType_IsSubtype(type, &PySet_Type))
1006 type = &PySet_Type;
1007 else
1008 type = &PyFrozenSet_Type;
1009 }
1010 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001011}
1012
Raymond Hettingerd7946662005-08-01 21:39:29 +00001013/* The empty frozenset is a singleton */
1014static PyObject *emptyfrozenset = NULL;
1015
Raymond Hettingera690a992003-11-16 16:17:49 +00001016static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001017frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1022 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1025 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 if (type != &PyFrozenSet_Type)
1028 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (iterable != NULL) {
1031 /* frozenset(f) is idempotent */
1032 if (PyFrozenSet_CheckExact(iterable)) {
1033 Py_INCREF(iterable);
1034 return iterable;
1035 }
1036 result = make_new_set(type, iterable);
1037 if (result == NULL || PySet_GET_SIZE(result))
1038 return result;
1039 Py_DECREF(result);
1040 }
1041 /* The empty frozenset is a singleton */
1042 if (emptyfrozenset == NULL)
1043 emptyfrozenset = make_new_set(type, NULL);
1044 Py_XINCREF(emptyfrozenset);
1045 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001046}
1047
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001048int
1049PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001050{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001051 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001052}
1053
1054void
1055PySet_Fini(void)
1056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001058}
1059
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001060static PyObject *
1061set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1064 return NULL;
1065
1066 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001067}
1068
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001069/* set_swap_bodies() switches the contents of any two sets by moving their
1070 internal data pointers and, if needed, copying the internal smalltables.
1071 Semantically equivalent to:
1072
1073 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1074
1075 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001076 Useful for operations that update in-place (by allowing an intermediate
1077 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001078*/
1079
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001080static void
1081set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 Py_ssize_t t;
1084 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001086 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 t = a->fill; a->fill = b->fill; b->fill = t;
1089 t = a->used; a->used = b->used; b->used = t;
1090 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 u = a->table;
1093 if (a->table == a->smalltable)
1094 u = b->smalltable;
1095 a->table = b->table;
1096 if (b->table == b->smalltable)
1097 a->table = a->smalltable;
1098 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (a->table == a->smalltable || b->table == b->smalltable) {
1101 memcpy(tab, a->smalltable, sizeof(tab));
1102 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1103 memcpy(b->smalltable, tab, sizeof(tab));
1104 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1107 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1108 h = a->hash; a->hash = b->hash; b->hash = h;
1109 } else {
1110 a->hash = -1;
1111 b->hash = -1;
1112 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001113}
1114
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001115static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001116set_copy(PySetObject *so)
1117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001119}
1120
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001121static PyObject *
1122frozenset_copy(PySetObject *so)
1123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (PyFrozenSet_CheckExact(so)) {
1125 Py_INCREF(so);
1126 return (PyObject *)so;
1127 }
1128 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001129}
1130
Raymond Hettingera690a992003-11-16 16:17:49 +00001131PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1132
1133static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001134set_clear(PySetObject *so)
1135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 set_clear_internal(so);
1137 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001138}
1139
1140PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1141
1142static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001143set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 PySetObject *result;
1146 PyObject *other;
1147 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 result = (PySetObject *)set_copy(so);
1150 if (result == NULL)
1151 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1154 other = PyTuple_GET_ITEM(args, i);
1155 if ((PyObject *)so == other)
1156 continue;
1157 if (set_update_internal(result, other) == -1) {
1158 Py_DECREF(result);
1159 return NULL;
1160 }
1161 }
1162 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001163}
1164
1165PyDoc_STRVAR(union_doc,
1166 "Return the union of sets as a new set.\n\
1167\n\
1168(i.e. all elements that are in either set.)");
1169
1170static PyObject *
1171set_or(PySetObject *so, PyObject *other)
1172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001174
Brian Curtindfc80e32011-08-10 20:28:54 -05001175 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1176 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 result = (PySetObject *)set_copy(so);
1179 if (result == NULL)
1180 return NULL;
1181 if ((PyObject *)so == other)
1182 return (PyObject *)result;
1183 if (set_update_internal(result, other) == -1) {
1184 Py_DECREF(result);
1185 return NULL;
1186 }
1187 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001188}
1189
Raymond Hettingera690a992003-11-16 16:17:49 +00001190static PyObject *
1191set_ior(PySetObject *so, PyObject *other)
1192{
Brian Curtindfc80e32011-08-10 20:28:54 -05001193 if (!PyAnySet_Check(other))
1194 Py_RETURN_NOTIMPLEMENTED;
1195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 if (set_update_internal(so, other) == -1)
1197 return NULL;
1198 Py_INCREF(so);
1199 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001200}
1201
1202static PyObject *
1203set_intersection(PySetObject *so, PyObject *other)
1204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 PySetObject *result;
1206 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if ((PyObject *)so == other)
1209 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1212 if (result == NULL)
1213 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (PyAnySet_Check(other)) {
1216 Py_ssize_t pos = 0;
1217 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1220 tmp = (PyObject *)so;
1221 so = (PySetObject *)other;
1222 other = tmp;
1223 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 while (set_next((PySetObject *)other, &pos, &entry)) {
1226 int rv = set_contains_entry(so, entry);
1227 if (rv == -1) {
1228 Py_DECREF(result);
1229 return NULL;
1230 }
1231 if (rv) {
1232 if (set_add_entry(result, entry) == -1) {
1233 Py_DECREF(result);
1234 return NULL;
1235 }
1236 }
1237 }
1238 return (PyObject *)result;
1239 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 it = PyObject_GetIter(other);
1242 if (it == NULL) {
1243 Py_DECREF(result);
1244 return NULL;
1245 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 while ((key = PyIter_Next(it)) != NULL) {
1248 int rv;
1249 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001250 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (hash == -1) {
1253 Py_DECREF(it);
1254 Py_DECREF(result);
1255 Py_DECREF(key);
1256 return NULL;
1257 }
1258 entry.hash = hash;
1259 entry.key = key;
1260 rv = set_contains_entry(so, &entry);
1261 if (rv == -1) {
1262 Py_DECREF(it);
1263 Py_DECREF(result);
1264 Py_DECREF(key);
1265 return NULL;
1266 }
1267 if (rv) {
1268 if (set_add_entry(result, &entry) == -1) {
1269 Py_DECREF(it);
1270 Py_DECREF(result);
1271 Py_DECREF(key);
1272 return NULL;
1273 }
1274 }
1275 Py_DECREF(key);
1276 }
1277 Py_DECREF(it);
1278 if (PyErr_Occurred()) {
1279 Py_DECREF(result);
1280 return NULL;
1281 }
1282 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001283}
1284
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001285static PyObject *
1286set_intersection_multi(PySetObject *so, PyObject *args)
1287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 Py_ssize_t i;
1289 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (PyTuple_GET_SIZE(args) == 0)
1292 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 Py_INCREF(so);
1295 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1296 PyObject *other = PyTuple_GET_ITEM(args, i);
1297 PyObject *newresult = set_intersection((PySetObject *)result, other);
1298 if (newresult == NULL) {
1299 Py_DECREF(result);
1300 return NULL;
1301 }
1302 Py_DECREF(result);
1303 result = newresult;
1304 }
1305 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001306}
1307
Raymond Hettingera690a992003-11-16 16:17:49 +00001308PyDoc_STRVAR(intersection_doc,
1309"Return the intersection of two sets as a new set.\n\
1310\n\
1311(i.e. all elements that are in both sets.)");
1312
1313static PyObject *
1314set_intersection_update(PySetObject *so, PyObject *other)
1315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 tmp = set_intersection(so, other);
1319 if (tmp == NULL)
1320 return NULL;
1321 set_swap_bodies(so, (PySetObject *)tmp);
1322 Py_DECREF(tmp);
1323 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001324}
1325
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001326static PyObject *
1327set_intersection_update_multi(PySetObject *so, PyObject *args)
1328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 tmp = set_intersection_multi(so, args);
1332 if (tmp == NULL)
1333 return NULL;
1334 set_swap_bodies(so, (PySetObject *)tmp);
1335 Py_DECREF(tmp);
1336 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001337}
1338
Raymond Hettingera690a992003-11-16 16:17:49 +00001339PyDoc_STRVAR(intersection_update_doc,
1340"Update a set with the intersection of itself and another.");
1341
1342static PyObject *
1343set_and(PySetObject *so, PyObject *other)
1344{
Brian Curtindfc80e32011-08-10 20:28:54 -05001345 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1346 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001348}
1349
1350static PyObject *
1351set_iand(PySetObject *so, PyObject *other)
1352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001354
Brian Curtindfc80e32011-08-10 20:28:54 -05001355 if (!PyAnySet_Check(other))
1356 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 result = set_intersection_update(so, other);
1358 if (result == NULL)
1359 return NULL;
1360 Py_DECREF(result);
1361 Py_INCREF(so);
1362 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001363}
1364
Guido van Rossum58da9312007-11-10 23:39:45 +00001365static PyObject *
1366set_isdisjoint(PySetObject *so, PyObject *other)
1367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if ((PyObject *)so == other) {
1371 if (PySet_GET_SIZE(so) == 0)
1372 Py_RETURN_TRUE;
1373 else
1374 Py_RETURN_FALSE;
1375 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (PyAnySet_CheckExact(other)) {
1378 Py_ssize_t pos = 0;
1379 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1382 tmp = (PyObject *)so;
1383 so = (PySetObject *)other;
1384 other = tmp;
1385 }
1386 while (set_next((PySetObject *)other, &pos, &entry)) {
1387 int rv = set_contains_entry(so, entry);
1388 if (rv == -1)
1389 return NULL;
1390 if (rv)
1391 Py_RETURN_FALSE;
1392 }
1393 Py_RETURN_TRUE;
1394 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 it = PyObject_GetIter(other);
1397 if (it == NULL)
1398 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 while ((key = PyIter_Next(it)) != NULL) {
1401 int rv;
1402 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001403 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 if (hash == -1) {
1406 Py_DECREF(key);
1407 Py_DECREF(it);
1408 return NULL;
1409 }
1410 entry.hash = hash;
1411 entry.key = key;
1412 rv = set_contains_entry(so, &entry);
1413 Py_DECREF(key);
1414 if (rv == -1) {
1415 Py_DECREF(it);
1416 return NULL;
1417 }
1418 if (rv) {
1419 Py_DECREF(it);
1420 Py_RETURN_FALSE;
1421 }
1422 }
1423 Py_DECREF(it);
1424 if (PyErr_Occurred())
1425 return NULL;
1426 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001427}
1428
1429PyDoc_STRVAR(isdisjoint_doc,
1430"Return True if two sets have a null intersection.");
1431
Neal Norwitz6576bd82005-11-13 18:41:28 +00001432static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001433set_difference_update_internal(PySetObject *so, PyObject *other)
1434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if ((PyObject *)so == other)
1436 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (PyAnySet_Check(other)) {
1439 setentry *entry;
1440 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 while (set_next((PySetObject *)other, &pos, &entry))
1443 if (set_discard_entry(so, entry) == -1)
1444 return -1;
1445 } else {
1446 PyObject *key, *it;
1447 it = PyObject_GetIter(other);
1448 if (it == NULL)
1449 return -1;
1450
1451 while ((key = PyIter_Next(it)) != NULL) {
1452 if (set_discard_key(so, key) == -1) {
1453 Py_DECREF(it);
1454 Py_DECREF(key);
1455 return -1;
1456 }
1457 Py_DECREF(key);
1458 }
1459 Py_DECREF(it);
1460 if (PyErr_Occurred())
1461 return -1;
1462 }
1463 /* If more than 1/5 are dummies, then resize them away. */
1464 if ((so->fill - so->used) * 5 < so->mask)
1465 return 0;
1466 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001467}
1468
Raymond Hettingera690a992003-11-16 16:17:49 +00001469static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001470set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1475 PyObject *other = PyTuple_GET_ITEM(args, i);
1476 if (set_difference_update_internal(so, other) == -1)
1477 return NULL;
1478 }
1479 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001480}
1481
1482PyDoc_STRVAR(difference_update_doc,
1483"Remove all elements of another set from this set.");
1484
1485static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001486set_copy_and_difference(PySetObject *so, PyObject *other)
1487{
1488 PyObject *result;
1489
1490 result = set_copy(so);
1491 if (result == NULL)
1492 return NULL;
1493 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1494 return result;
1495 Py_DECREF(result);
1496 return NULL;
1497}
1498
1499static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001500set_difference(PySetObject *so, PyObject *other)
1501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 PyObject *result;
1503 setentry *entry;
1504 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001507 return set_copy_and_difference(so, other);
1508 }
1509
1510 /* If len(so) much more than len(other), it's more efficient to simply copy
1511 * so and then iterate other looking for common elements. */
1512 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1513 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 result = make_new_set_basetype(Py_TYPE(so), NULL);
1517 if (result == NULL)
1518 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (PyDict_CheckExact(other)) {
1521 while (set_next(so, &pos, &entry)) {
1522 setentry entrycopy;
1523 entrycopy.hash = entry->hash;
1524 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001525 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1527 Py_DECREF(result);
1528 return NULL;
1529 }
1530 }
1531 }
1532 return result;
1533 }
1534
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001535 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 while (set_next(so, &pos, &entry)) {
1537 int rv = set_contains_entry((PySetObject *)other, entry);
1538 if (rv == -1) {
1539 Py_DECREF(result);
1540 return NULL;
1541 }
1542 if (!rv) {
1543 if (set_add_entry((PySetObject *)result, entry) == -1) {
1544 Py_DECREF(result);
1545 return NULL;
1546 }
1547 }
1548 }
1549 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001550}
1551
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001552static PyObject *
1553set_difference_multi(PySetObject *so, PyObject *args)
1554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 Py_ssize_t i;
1556 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 if (PyTuple_GET_SIZE(args) == 0)
1559 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 other = PyTuple_GET_ITEM(args, 0);
1562 result = set_difference(so, other);
1563 if (result == NULL)
1564 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1567 other = PyTuple_GET_ITEM(args, i);
1568 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1569 Py_DECREF(result);
1570 return NULL;
1571 }
1572 }
1573 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001574}
1575
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001576PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001577"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001578\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001579(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001580static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001581set_sub(PySetObject *so, PyObject *other)
1582{
Brian Curtindfc80e32011-08-10 20:28:54 -05001583 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1584 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001586}
1587
1588static PyObject *
1589set_isub(PySetObject *so, PyObject *other)
1590{
Brian Curtindfc80e32011-08-10 20:28:54 -05001591 if (!PyAnySet_Check(other))
1592 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 if (set_difference_update_internal(so, other) == -1)
1594 return NULL;
1595 Py_INCREF(so);
1596 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001597}
1598
1599static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001600set_symmetric_difference_update(PySetObject *so, PyObject *other)
1601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 PySetObject *otherset;
1603 PyObject *key;
1604 Py_ssize_t pos = 0;
1605 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 if ((PyObject *)so == other)
1608 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if (PyDict_CheckExact(other)) {
1611 PyObject *value;
1612 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001613 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1615 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001616
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001617 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 an_entry.hash = hash;
1619 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001622 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001623 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001626 if (rv == DISCARD_NOTFOUND) {
1627 if (set_add_entry(so, &an_entry) == -1) {
1628 Py_DECREF(key);
1629 return NULL;
1630 }
1631 }
Georg Brandl2d444492010-09-03 10:52:55 +00001632 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 }
1634 Py_RETURN_NONE;
1635 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 if (PyAnySet_Check(other)) {
1638 Py_INCREF(other);
1639 otherset = (PySetObject *)other;
1640 } else {
1641 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1642 if (otherset == NULL)
1643 return NULL;
1644 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 while (set_next(otherset, &pos, &entry)) {
1647 int rv = set_discard_entry(so, entry);
1648 if (rv == -1) {
1649 Py_DECREF(otherset);
1650 return NULL;
1651 }
1652 if (rv == DISCARD_NOTFOUND) {
1653 if (set_add_entry(so, entry) == -1) {
1654 Py_DECREF(otherset);
1655 return NULL;
1656 }
1657 }
1658 }
1659 Py_DECREF(otherset);
1660 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001661}
1662
1663PyDoc_STRVAR(symmetric_difference_update_doc,
1664"Update a set with the symmetric difference of itself and another.");
1665
1666static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001667set_symmetric_difference(PySetObject *so, PyObject *other)
1668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 PyObject *rv;
1670 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1673 if (otherset == NULL)
1674 return NULL;
1675 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1676 if (rv == NULL)
1677 return NULL;
1678 Py_DECREF(rv);
1679 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001680}
1681
1682PyDoc_STRVAR(symmetric_difference_doc,
1683"Return the symmetric difference of two sets as a new set.\n\
1684\n\
1685(i.e. all elements that are in exactly one of the sets.)");
1686
1687static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001688set_xor(PySetObject *so, PyObject *other)
1689{
Brian Curtindfc80e32011-08-10 20:28:54 -05001690 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1691 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001693}
1694
1695static PyObject *
1696set_ixor(PySetObject *so, PyObject *other)
1697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001699
Brian Curtindfc80e32011-08-10 20:28:54 -05001700 if (!PyAnySet_Check(other))
1701 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 result = set_symmetric_difference_update(so, other);
1703 if (result == NULL)
1704 return NULL;
1705 Py_DECREF(result);
1706 Py_INCREF(so);
1707 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001708}
1709
1710static PyObject *
1711set_issubset(PySetObject *so, PyObject *other)
1712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 setentry *entry;
1714 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 if (!PyAnySet_Check(other)) {
1717 PyObject *tmp, *result;
1718 tmp = make_new_set(&PySet_Type, other);
1719 if (tmp == NULL)
1720 return NULL;
1721 result = set_issubset(so, tmp);
1722 Py_DECREF(tmp);
1723 return result;
1724 }
1725 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1726 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 while (set_next(so, &pos, &entry)) {
1729 int rv = set_contains_entry((PySetObject *)other, entry);
1730 if (rv == -1)
1731 return NULL;
1732 if (!rv)
1733 Py_RETURN_FALSE;
1734 }
1735 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001736}
1737
1738PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1739
1740static PyObject *
1741set_issuperset(PySetObject *so, PyObject *other)
1742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 if (!PyAnySet_Check(other)) {
1746 tmp = make_new_set(&PySet_Type, other);
1747 if (tmp == NULL)
1748 return NULL;
1749 result = set_issuperset(so, tmp);
1750 Py_DECREF(tmp);
1751 return result;
1752 }
1753 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001754}
1755
1756PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1757
Raymond Hettingera690a992003-11-16 16:17:49 +00001758static PyObject *
1759set_richcompare(PySetObject *v, PyObject *w, int op)
1760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001762
Brian Curtindfc80e32011-08-10 20:28:54 -05001763 if(!PyAnySet_Check(w))
1764 Py_RETURN_NOTIMPLEMENTED;
1765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 switch (op) {
1767 case Py_EQ:
1768 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1769 Py_RETURN_FALSE;
1770 if (v->hash != -1 &&
1771 ((PySetObject *)w)->hash != -1 &&
1772 v->hash != ((PySetObject *)w)->hash)
1773 Py_RETURN_FALSE;
1774 return set_issubset(v, w);
1775 case Py_NE:
1776 r1 = set_richcompare(v, w, Py_EQ);
1777 if (r1 == NULL)
1778 return NULL;
1779 r2 = PyBool_FromLong(PyObject_Not(r1));
1780 Py_DECREF(r1);
1781 return r2;
1782 case Py_LE:
1783 return set_issubset(v, w);
1784 case Py_GE:
1785 return set_issuperset(v, w);
1786 case Py_LT:
1787 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1788 Py_RETURN_FALSE;
1789 return set_issubset(v, w);
1790 case Py_GT:
1791 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1792 Py_RETURN_FALSE;
1793 return set_issuperset(v, w);
1794 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001795 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001796}
1797
Raymond Hettingera690a992003-11-16 16:17:49 +00001798static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001799set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 if (set_add_key(so, key) == -1)
1802 return NULL;
1803 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001804}
1805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001807"Add an element to a set.\n\
1808\n\
1809This has no effect if the element is already present.");
1810
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001811static int
1812set_contains(PySetObject *so, PyObject *key)
1813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 PyObject *tmpkey;
1815 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 rv = set_contains_key(so, key);
1818 if (rv == -1) {
1819 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1820 return -1;
1821 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001822 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 if (tmpkey == NULL)
1824 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001825 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 Py_DECREF(tmpkey);
1827 }
1828 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001829}
1830
1831static PyObject *
1832set_direct_contains(PySetObject *so, PyObject *key)
1833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 result = set_contains(so, key);
1837 if (result == -1)
1838 return NULL;
1839 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001840}
1841
1842PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1843
Raymond Hettingera690a992003-11-16 16:17:49 +00001844static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001845set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 PyObject *tmpkey;
1848 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 rv = set_discard_key(so, key);
1851 if (rv == -1) {
1852 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1853 return NULL;
1854 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001855 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 if (tmpkey == NULL)
1857 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 Py_DECREF(tmpkey);
1860 if (rv == -1)
1861 return NULL;
1862 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001865 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 return NULL;
1867 }
1868 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001869}
1870
1871PyDoc_STRVAR(remove_doc,
1872"Remove an element from a set; it must be a member.\n\
1873\n\
1874If the element is not a member, raise a KeyError.");
1875
1876static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001877set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001878{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001879 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 rv = set_discard_key(so, key);
1883 if (rv == -1) {
1884 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1885 return NULL;
1886 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001887 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 if (tmpkey == NULL)
1889 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001890 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001892 if (rv == -1)
1893 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 }
1895 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001896}
1897
1898PyDoc_STRVAR(discard_doc,
1899"Remove an element from a set if it is a member.\n\
1900\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001902
1903static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001904set_reduce(PySetObject *so)
1905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001907 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 keys = PySequence_List((PyObject *)so);
1910 if (keys == NULL)
1911 goto done;
1912 args = PyTuple_Pack(1, keys);
1913 if (args == NULL)
1914 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001915 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 if (dict == NULL) {
1917 PyErr_Clear();
1918 dict = Py_None;
1919 Py_INCREF(dict);
1920 }
1921 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001922done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 Py_XDECREF(args);
1924 Py_XDECREF(keys);
1925 Py_XDECREF(dict);
1926 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001927}
1928
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001929static PyObject *
1930set_sizeof(PySetObject *so)
1931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 res = sizeof(PySetObject);
1935 if (so->table != so->smalltable)
1936 res = res + (so->mask + 1) * sizeof(setentry);
1937 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001938}
1939
1940PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001941static int
1942set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 if (!PyAnySet_Check(self))
1947 return -1;
1948 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1949 return -1;
1950 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1951 return -1;
1952 set_clear_internal(self);
1953 self->hash = -1;
1954 if (iterable == NULL)
1955 return 0;
1956 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001957}
1958
Raymond Hettingera690a992003-11-16 16:17:49 +00001959static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 set_len, /* sq_length */
1961 0, /* sq_concat */
1962 0, /* sq_repeat */
1963 0, /* sq_item */
1964 0, /* sq_slice */
1965 0, /* sq_ass_item */
1966 0, /* sq_ass_slice */
1967 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001968};
1969
1970/* set object ********************************************************/
1971
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001972#ifdef Py_DEBUG
1973static PyObject *test_c_api(PySetObject *so);
1974
1975PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1976All is well if assertions don't fail.");
1977#endif
1978
Raymond Hettingera690a992003-11-16 16:17:49 +00001979static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 {"add", (PyCFunction)set_add, METH_O,
1981 add_doc},
1982 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1983 clear_doc},
1984 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1985 contains_doc},
1986 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1987 copy_doc},
1988 {"discard", (PyCFunction)set_discard, METH_O,
1989 discard_doc},
1990 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
1991 difference_doc},
1992 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
1993 difference_update_doc},
1994 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
1995 intersection_doc},
1996 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
1997 intersection_update_doc},
1998 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1999 isdisjoint_doc},
2000 {"issubset", (PyCFunction)set_issubset, METH_O,
2001 issubset_doc},
2002 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2003 issuperset_doc},
2004 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2005 pop_doc},
2006 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2007 reduce_doc},
2008 {"remove", (PyCFunction)set_remove, METH_O,
2009 remove_doc},
2010 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2011 sizeof_doc},
2012 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2013 symmetric_difference_doc},
2014 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2015 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002016#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2018 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002019#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 {"union", (PyCFunction)set_union, METH_VARARGS,
2021 union_doc},
2022 {"update", (PyCFunction)set_update, METH_VARARGS,
2023 update_doc},
2024 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002025};
2026
2027static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 0, /*nb_add*/
2029 (binaryfunc)set_sub, /*nb_subtract*/
2030 0, /*nb_multiply*/
2031 0, /*nb_remainder*/
2032 0, /*nb_divmod*/
2033 0, /*nb_power*/
2034 0, /*nb_negative*/
2035 0, /*nb_positive*/
2036 0, /*nb_absolute*/
2037 0, /*nb_bool*/
2038 0, /*nb_invert*/
2039 0, /*nb_lshift*/
2040 0, /*nb_rshift*/
2041 (binaryfunc)set_and, /*nb_and*/
2042 (binaryfunc)set_xor, /*nb_xor*/
2043 (binaryfunc)set_or, /*nb_or*/
2044 0, /*nb_int*/
2045 0, /*nb_reserved*/
2046 0, /*nb_float*/
2047 0, /*nb_inplace_add*/
2048 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2049 0, /*nb_inplace_multiply*/
2050 0, /*nb_inplace_remainder*/
2051 0, /*nb_inplace_power*/
2052 0, /*nb_inplace_lshift*/
2053 0, /*nb_inplace_rshift*/
2054 (binaryfunc)set_iand, /*nb_inplace_and*/
2055 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2056 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002057};
2058
2059PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002060"set() -> new empty set object\n\
2061set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002062\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002063Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002064
2065PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2067 "set", /* tp_name */
2068 sizeof(PySetObject), /* tp_basicsize */
2069 0, /* tp_itemsize */
2070 /* methods */
2071 (destructor)set_dealloc, /* tp_dealloc */
2072 0, /* tp_print */
2073 0, /* tp_getattr */
2074 0, /* tp_setattr */
2075 0, /* tp_reserved */
2076 (reprfunc)set_repr, /* tp_repr */
2077 &set_as_number, /* tp_as_number */
2078 &set_as_sequence, /* tp_as_sequence */
2079 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002080 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 0, /* tp_call */
2082 0, /* tp_str */
2083 PyObject_GenericGetAttr, /* tp_getattro */
2084 0, /* tp_setattro */
2085 0, /* tp_as_buffer */
2086 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2087 Py_TPFLAGS_BASETYPE, /* tp_flags */
2088 set_doc, /* tp_doc */
2089 (traverseproc)set_traverse, /* tp_traverse */
2090 (inquiry)set_clear_internal, /* tp_clear */
2091 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002092 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2093 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 0, /* tp_iternext */
2095 set_methods, /* tp_methods */
2096 0, /* tp_members */
2097 0, /* tp_getset */
2098 0, /* tp_base */
2099 0, /* tp_dict */
2100 0, /* tp_descr_get */
2101 0, /* tp_descr_set */
2102 0, /* tp_dictoffset */
2103 (initproc)set_init, /* tp_init */
2104 PyType_GenericAlloc, /* tp_alloc */
2105 set_new, /* tp_new */
2106 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002107};
2108
2109/* frozenset object ********************************************************/
2110
2111
2112static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2114 contains_doc},
2115 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2116 copy_doc},
2117 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2118 difference_doc},
2119 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2120 intersection_doc},
2121 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2122 isdisjoint_doc},
2123 {"issubset", (PyCFunction)set_issubset, METH_O,
2124 issubset_doc},
2125 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2126 issuperset_doc},
2127 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2128 reduce_doc},
2129 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2130 sizeof_doc},
2131 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2132 symmetric_difference_doc},
2133 {"union", (PyCFunction)set_union, METH_VARARGS,
2134 union_doc},
2135 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002136};
2137
2138static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 0, /*nb_add*/
2140 (binaryfunc)set_sub, /*nb_subtract*/
2141 0, /*nb_multiply*/
2142 0, /*nb_remainder*/
2143 0, /*nb_divmod*/
2144 0, /*nb_power*/
2145 0, /*nb_negative*/
2146 0, /*nb_positive*/
2147 0, /*nb_absolute*/
2148 0, /*nb_bool*/
2149 0, /*nb_invert*/
2150 0, /*nb_lshift*/
2151 0, /*nb_rshift*/
2152 (binaryfunc)set_and, /*nb_and*/
2153 (binaryfunc)set_xor, /*nb_xor*/
2154 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002155};
2156
2157PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002158"frozenset() -> empty frozenset object\n\
2159frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002160\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002161Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002162
2163PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2165 "frozenset", /* tp_name */
2166 sizeof(PySetObject), /* tp_basicsize */
2167 0, /* tp_itemsize */
2168 /* methods */
2169 (destructor)set_dealloc, /* tp_dealloc */
2170 0, /* tp_print */
2171 0, /* tp_getattr */
2172 0, /* tp_setattr */
2173 0, /* tp_reserved */
2174 (reprfunc)set_repr, /* tp_repr */
2175 &frozenset_as_number, /* tp_as_number */
2176 &set_as_sequence, /* tp_as_sequence */
2177 0, /* tp_as_mapping */
2178 frozenset_hash, /* tp_hash */
2179 0, /* tp_call */
2180 0, /* tp_str */
2181 PyObject_GenericGetAttr, /* tp_getattro */
2182 0, /* tp_setattro */
2183 0, /* tp_as_buffer */
2184 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2185 Py_TPFLAGS_BASETYPE, /* tp_flags */
2186 frozenset_doc, /* tp_doc */
2187 (traverseproc)set_traverse, /* tp_traverse */
2188 (inquiry)set_clear_internal, /* tp_clear */
2189 (richcmpfunc)set_richcompare, /* tp_richcompare */
2190 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2191 (getiterfunc)set_iter, /* tp_iter */
2192 0, /* tp_iternext */
2193 frozenset_methods, /* tp_methods */
2194 0, /* tp_members */
2195 0, /* tp_getset */
2196 0, /* tp_base */
2197 0, /* tp_dict */
2198 0, /* tp_descr_get */
2199 0, /* tp_descr_set */
2200 0, /* tp_dictoffset */
2201 0, /* tp_init */
2202 PyType_GenericAlloc, /* tp_alloc */
2203 frozenset_new, /* tp_new */
2204 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002205};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002206
2207
2208/***** C API functions *************************************************/
2209
2210PyObject *
2211PySet_New(PyObject *iterable)
2212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002214}
2215
2216PyObject *
2217PyFrozenSet_New(PyObject *iterable)
2218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002220}
2221
Neal Norwitz8c49c822006-03-04 18:41:19 +00002222Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002223PySet_Size(PyObject *anyset)
2224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 if (!PyAnySet_Check(anyset)) {
2226 PyErr_BadInternalCall();
2227 return -1;
2228 }
2229 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002230}
2231
2232int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002233PySet_Clear(PyObject *set)
2234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 if (!PySet_Check(set)) {
2236 PyErr_BadInternalCall();
2237 return -1;
2238 }
2239 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002240}
2241
2242int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002243PySet_Contains(PyObject *anyset, PyObject *key)
2244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 if (!PyAnySet_Check(anyset)) {
2246 PyErr_BadInternalCall();
2247 return -1;
2248 }
2249 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002250}
2251
2252int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002253PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 if (!PySet_Check(set)) {
2256 PyErr_BadInternalCall();
2257 return -1;
2258 }
2259 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002260}
2261
2262int
Christian Heimesfd66e512008-01-29 12:18:50 +00002263PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 if (!PySet_Check(anyset) &&
2266 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2267 PyErr_BadInternalCall();
2268 return -1;
2269 }
2270 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002271}
2272
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002273int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002274_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 if (!PyAnySet_Check(set)) {
2279 PyErr_BadInternalCall();
2280 return -1;
2281 }
2282 if (set_next((PySetObject *)set, pos, &entry) == 0)
2283 return 0;
2284 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002285 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002287}
2288
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002289PyObject *
2290PySet_Pop(PyObject *set)
2291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 if (!PySet_Check(set)) {
2293 PyErr_BadInternalCall();
2294 return NULL;
2295 }
2296 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002297}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002298
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002299int
2300_PySet_Update(PyObject *set, PyObject *iterable)
2301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 if (!PySet_Check(set)) {
2303 PyErr_BadInternalCall();
2304 return -1;
2305 }
2306 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002307}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002308
Raymond Hettingere259f132013-12-15 11:56:14 -08002309/* Exported for the gdb plugin's benefit. */
2310PyObject *_PySet_Dummy = dummy;
2311
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002312#ifdef Py_DEBUG
2313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002315 Returns True and original set is restored. */
2316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317#define assertRaises(call_return_value, exception) \
2318 do { \
2319 assert(call_return_value); \
2320 assert(PyErr_ExceptionMatches(exception)); \
2321 PyErr_Clear(); \
2322 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002323
2324static PyObject *
2325test_c_api(PySetObject *so)
2326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 Py_ssize_t count;
2328 char *s;
2329 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002330 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002332 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 /* Verify preconditions */
2336 assert(PyAnySet_Check(ob));
2337 assert(PyAnySet_CheckExact(ob));
2338 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 /* so.clear(); so |= set("abc"); */
2341 str = PyUnicode_FromString("abc");
2342 if (str == NULL)
2343 return NULL;
2344 set_clear_internal(so);
2345 if (set_update_internal(so, str) == -1) {
2346 Py_DECREF(str);
2347 return NULL;
2348 }
2349 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 /* Exercise type/size checks */
2352 assert(PySet_Size(ob) == 3);
2353 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 /* Raise TypeError for non-iterable constructor arguments */
2356 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2357 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 /* Raise TypeError for unhashable key */
2360 dup = PySet_New(ob);
2361 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2362 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2363 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* Exercise successful pop, contains, add, and discard */
2366 elem = PySet_Pop(ob);
2367 assert(PySet_Contains(ob, elem) == 0);
2368 assert(PySet_GET_SIZE(ob) == 2);
2369 assert(PySet_Add(ob, elem) == 0);
2370 assert(PySet_Contains(ob, elem) == 1);
2371 assert(PySet_GET_SIZE(ob) == 3);
2372 assert(PySet_Discard(ob, elem) == 1);
2373 assert(PySet_GET_SIZE(ob) == 2);
2374 assert(PySet_Discard(ob, elem) == 0);
2375 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 /* Exercise clear */
2378 dup2 = PySet_New(dup);
2379 assert(PySet_Clear(dup2) == 0);
2380 assert(PySet_Size(dup2) == 0);
2381 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 /* Raise SystemError on clear or update of frozen set */
2384 f = PyFrozenSet_New(dup);
2385 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2386 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2387 assert(PySet_Add(f, elem) == 0);
2388 Py_INCREF(f);
2389 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2390 Py_DECREF(f);
2391 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* Exercise direct iteration */
2394 i = 0, count = 0;
2395 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2396 s = _PyUnicode_AsString(x);
2397 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2398 count++;
2399 }
2400 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 /* Exercise updates */
2403 dup2 = PySet_New(NULL);
2404 assert(_PySet_Update(dup2, dup) == 0);
2405 assert(PySet_Size(dup2) == 3);
2406 assert(_PySet_Update(dup2, dup) == 0);
2407 assert(PySet_Size(dup2) == 3);
2408 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 /* Raise SystemError when self argument is not a set or frozenset. */
2411 t = PyTuple_New(0);
2412 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2413 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2414 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* Raise SystemError when self argument is not a set. */
2417 f = PyFrozenSet_New(dup);
2418 assert(PySet_Size(f) == 3);
2419 assert(PyFrozenSet_CheckExact(f));
2420 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2421 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2422 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* Raise KeyError when popping from an empty set */
2425 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2426 Py_DECREF(ob);
2427 assert(PySet_GET_SIZE(ob) == 0);
2428 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 /* Restore the set from the copy using the PyNumber API */
2431 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2432 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 /* Verify constructors accept NULL arguments */
2435 f = PySet_New(NULL);
2436 assert(f != NULL);
2437 assert(PySet_GET_SIZE(f) == 0);
2438 Py_DECREF(f);
2439 f = PyFrozenSet_New(NULL);
2440 assert(f != NULL);
2441 assert(PyFrozenSet_CheckExact(f));
2442 assert(PySet_GET_SIZE(f) == 0);
2443 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 Py_DECREF(elem);
2446 Py_DECREF(dup);
2447 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002448}
2449
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002450#undef assertRaises
2451
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002452#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002453
2454/***** Dummy Struct *************************************************/
2455
2456static PyObject *
2457dummy_repr(PyObject *op)
2458{
2459 return PyUnicode_FromString("<dummy key>");
2460}
2461
2462static void
2463dummy_dealloc(PyObject* ignore)
2464{
2465 Py_FatalError("deallocating <dummy key>");
2466}
2467
2468static PyTypeObject _PySetDummy_Type = {
2469 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2470 "<dummy key> type",
2471 0,
2472 0,
2473 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2474 0, /*tp_print*/
2475 0, /*tp_getattr*/
2476 0, /*tp_setattr*/
2477 0, /*tp_reserved*/
2478 dummy_repr, /*tp_repr*/
2479 0, /*tp_as_number*/
2480 0, /*tp_as_sequence*/
2481 0, /*tp_as_mapping*/
2482 0, /*tp_hash */
2483 0, /*tp_call */
2484 0, /*tp_str */
2485 0, /*tp_getattro */
2486 0, /*tp_setattro */
2487 0, /*tp_as_buffer */
2488 Py_TPFLAGS_DEFAULT, /*tp_flags */
2489};
2490
2491static PyObject _dummy_struct = {
2492 _PyObject_EXTRA_INIT
2493 2, &_PySetDummy_Type
2494};
2495