blob: 23ab95c715d620596d4fabc0f1cf893f80a6c366 [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) {
Raymond Hettinger82492822015-02-04 08:37:02 -080092 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080093 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 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700119
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700120 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800121 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700122
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800123 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700124 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700125 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700127 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700128 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000129}
130
131/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000132Internal routine used by set_table_resize() to insert an item which is
133known to be absent from the set. This routine also assumes that
134the set contains no deleted entries. Besides the performance benefit,
135using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
136Note that no refcounts are changed by this routine; if needed, the caller
137is responsible for incref'ing `key`.
138*/
139static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200140set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200143 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700144 size_t perturb = hash;
145 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800146 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700147 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000148
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700149 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800150 entry = &table[i];
151 if (entry->key == NULL)
152 goto found_null;
153 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800154 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800155 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800156 if (entry->key == NULL)
157 goto found_null;
158 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700159 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700160 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800161 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700163 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 entry->key = key;
165 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700166 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000168}
169
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700170/* ======== End logic for probing the hash table ========================== */
171/* ======================================================================== */
172
173
174/*
175Internal routine to insert a new key into the table.
176Used by the public insert routine.
177Eats a reference to key.
178*/
179static int
180set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
181{
182 setentry *entry;
183
Raymond Hettinger93035c42015-01-25 16:12:49 -0800184 entry = set_lookkey(so, key, hash);
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700185 if (entry == NULL)
186 return -1;
187 if (entry->key == NULL) {
188 /* UNUSED */
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700189 entry->key = key;
190 entry->hash = hash;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800191 so->fill++;
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700192 so->used++;
193 } else if (entry->key == dummy) {
194 /* DUMMY */
195 entry->key = key;
196 entry->hash = hash;
197 so->used++;
198 } else {
199 /* ACTIVE */
200 Py_DECREF(key);
201 }
202 return 0;
203}
204
Thomas Wouters89f507f2006-12-13 04:49:30 +0000205/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000206Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000207keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208actually be smaller than the old one.
209*/
210static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 Py_ssize_t newsize;
214 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800215 Py_ssize_t oldfill = so->fill;
216 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 int is_oldtable_malloced;
218 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100223 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 for (newsize = PySet_MINSIZE;
225 newsize <= minused && newsize > 0;
226 newsize <<= 1)
227 ;
228 if (newsize <= 0) {
229 PyErr_NoMemory();
230 return -1;
231 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 /* Get space for a new table. */
234 oldtable = so->table;
235 assert(oldtable != NULL);
236 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (newsize == PySet_MINSIZE) {
239 /* A large table is shrinking, or we can't get any smaller. */
240 newtable = so->smalltable;
241 if (newtable == oldtable) {
242 if (so->fill == so->used) {
243 /* No dummies, so no point doing anything. */
244 return 0;
245 }
246 /* We're not going to resize it, but rebuild the
247 table anyway to purge old dummy entries.
248 Subtle: This is *necessary* if fill==size,
249 as set_lookkey needs at least one virgin slot to
250 terminate failing searches. If fill < size, it's
251 merely desirable, as dummies slow searches. */
252 assert(so->fill > so->used);
253 memcpy(small_copy, oldtable, sizeof(small_copy));
254 oldtable = small_copy;
255 }
256 }
257 else {
258 newtable = PyMem_NEW(setentry, newsize);
259 if (newtable == NULL) {
260 PyErr_NoMemory();
261 return -1;
262 }
263 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 /* Make the set empty, using the new table. */
266 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800269 so->used = 0;
270 so->mask = newsize - 1;
271 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 /* Copy the data over; this is refcount-neutral for active entries;
274 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800275 if (oldfill == oldused) {
276 for (entry = oldtable; oldused > 0; entry++) {
277 if (entry->key != NULL) {
278 oldused--;
279 set_insert_clean(so, entry->key, entry->hash);
280 }
281 }
282 } else {
283 for (entry = oldtable; oldused > 0; entry++) {
284 if (entry->key != NULL && entry->key != dummy) {
285 oldused--;
286 set_insert_clean(so, entry->key, entry->hash);
287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
289 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 if (is_oldtable_malloced)
292 PyMem_DEL(oldtable);
293 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000294}
295
Raymond Hettingerc991db22005-08-11 07:58:45 +0000296/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
297
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000298static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200299set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000300{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200301 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000302 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100303 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 assert(so->fill <= so->mask); /* at least one empty slot */
306 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000307 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100308 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000309 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 return -1;
311 }
312 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
313 return 0;
314 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000315}
316
317static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200318set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000319{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800320 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200321 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200324 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 hash = PyObject_Hash(key);
326 if (hash == -1)
327 return -1;
328 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800329 entry.key = key;
330 entry.hash = hash;
331 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000332}
333
334#define DISCARD_NOTFOUND 0
335#define DISCARD_FOUND 1
336
337static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000338set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800339{
340 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000342
Raymond Hettinger93035c42015-01-25 16:12:49 -0800343 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 if (entry == NULL)
345 return -1;
346 if (entry->key == NULL || entry->key == dummy)
347 return DISCARD_NOTFOUND;
348 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800350 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 so->used--;
352 Py_DECREF(old_key);
353 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000354}
355
356static int
357set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000358{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800359 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200360 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200365 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 hash = PyObject_Hash(key);
367 if (hash == -1)
368 return -1;
369 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800370 entry.key = key;
371 entry.hash = hash;
372 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000373}
374
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700375static void
376set_empty_to_minsize(PySetObject *so)
377{
378 memset(so->smalltable, 0, sizeof(so->smalltable));
379 so->fill = 0;
380 so->used = 0;
381 so->mask = PySet_MINSIZE - 1;
382 so->table = so->smalltable;
383 so->hash = -1;
384}
385
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000386static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000387set_clear_internal(PySetObject *so)
388{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800389 setentry *entry;
390 setentry *table = so->table;
391 Py_ssize_t fill = so->fill;
392 Py_ssize_t used = so->used;
393 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000395
Raymond Hettinger583cd032013-09-07 22:06:35 -0700396 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 /* This is delicate. During the process of clearing the set,
400 * decrefs can cause the set to mutate. To avoid fatal confusion
401 * (voice of experience), we have to make the set empty before
402 * clearing the slots, and never refer to anything via so->ref while
403 * clearing.
404 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700406 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 else if (fill > 0) {
409 /* It's a small table with something that needs to be cleared.
410 * Afraid the only safe way is to copy the set entries into
411 * another small table first.
412 */
413 memcpy(small_copy, table, sizeof(small_copy));
414 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700415 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 }
417 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 /* Now we can finally clear things. If C had refcounts, we could
420 * assert that the refcount on table is 1 now, i.e. that this function
421 * has unique access to it, so decref side-effects can't alter it.
422 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800423 for (entry = table; used > 0; entry++) {
424 if (entry->key && entry->key != dummy) {
425 used--;
426 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (table_is_malloced)
431 PyMem_DEL(table);
432 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000433}
434
435/*
436 * Iterate over a set table. Use like so:
437 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000438 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000439 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000440 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000441 * while (set_next(yourset, &pos, &entry)) {
442 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000443 * }
444 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000445 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000447 */
448static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000449set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 Py_ssize_t i;
452 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200453 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 assert (PyAnySet_Check(so));
456 i = *pos_ptr;
457 assert(i >= 0);
458 table = so->table;
459 mask = so->mask;
460 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
461 i++;
462 *pos_ptr = i+1;
463 if (i > mask)
464 return 0;
465 assert(table[i].key != NULL);
466 *entry_ptr = &table[i];
467 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000468}
469
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000470static void
471set_dealloc(PySetObject *so)
472{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200473 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800474 Py_ssize_t used = so->used;
475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyObject_GC_UnTrack(so);
477 Py_TRASHCAN_SAFE_BEGIN(so)
478 if (so->weakreflist != NULL)
479 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000480
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800481 for (entry = so->table; used > 0; entry++) {
482 if (entry->key && entry->key != dummy) {
483 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700484 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 }
486 }
487 if (so->table != so->smalltable)
488 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700489 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000491}
492
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000493static PyObject *
494set_repr(PySetObject *so)
495{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200496 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (status != 0) {
500 if (status < 0)
501 return NULL;
502 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
503 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* shortcut for the empty set */
506 if (!so->used) {
507 Py_ReprLeave((PyObject*)so);
508 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
509 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 keys = PySequence_List((PyObject *)so);
512 if (keys == NULL)
513 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000514
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200515 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 listrepr = PyObject_Repr(keys);
517 Py_DECREF(keys);
518 if (listrepr == NULL)
519 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200520 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200522 if (tmp == NULL)
523 goto done;
524 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200525
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200526 if (Py_TYPE(so) != &PySet_Type)
527 result = PyUnicode_FromFormat("%s({%U})",
528 Py_TYPE(so)->tp_name,
529 listrepr);
530 else
531 result = PyUnicode_FromFormat("{%U}", listrepr);
532 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000533done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 Py_ReprLeave((PyObject*)so);
535 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000536}
537
Martin v. Löwis18e16552006-02-15 17:27:45 +0000538static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000539set_len(PyObject *so)
540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000542}
543
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000544static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000545set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000548 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100549 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200550 Py_ssize_t i;
551 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 assert (PyAnySet_Check(so));
554 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 other = (PySetObject*)otherset;
557 if (other == so || other->used == 0)
558 /* a.update(a) or a.update({}); nothing to do */
559 return 0;
560 /* Do one big resize at the start, rather than
561 * incrementally resizing as we insert new keys. Expect
562 * that there will be no (or few) overlapping keys.
563 */
564 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
565 if (set_table_resize(so, (so->used + other->used)*2) != 0)
566 return -1;
567 }
568 for (i = 0; i <= other->mask; i++) {
569 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000570 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100571 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000572 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500573 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000574 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100575 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000576 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 return -1;
578 }
579 }
580 }
581 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000582}
583
584static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000585set_contains_entry(PySetObject *so, setentry *entry)
586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 PyObject *key;
588 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000589
Raymond Hettinger93035c42015-01-25 16:12:49 -0800590 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 if (lu_entry == NULL)
592 return -1;
593 key = lu_entry->key;
594 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000595}
596
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800597static int
598set_contains_key(PySetObject *so, PyObject *key)
599{
600 setentry entry;
601 Py_hash_t hash;
602
603 if (!PyUnicode_CheckExact(key) ||
604 (hash = ((PyASCIIObject *) key)->hash) == -1) {
605 hash = PyObject_Hash(key);
606 if (hash == -1)
607 return -1;
608 }
609 entry.key = key;
610 entry.hash = hash;
611 return set_contains_entry(so, &entry);
612}
613
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000614static PyObject *
615set_pop(PySetObject *so)
616{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800617 /* Make sure the search finger is in bounds */
618 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200619 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 assert (PyAnySet_Check(so));
623 if (so->used == 0) {
624 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
625 return NULL;
626 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000627
Raymond Hettinger1202a472015-01-18 13:12:42 -0800628 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
629 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800630 if (i > so->mask)
631 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 }
633 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800635 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800637 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000639}
640
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000641PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
642Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000643
644static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000645set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 Py_ssize_t pos = 0;
648 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 while (set_next(so, &pos, &entry))
651 Py_VISIT(entry->key);
652 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000653}
654
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000655static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000656frozenset_hash(PyObject *self)
657{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800658 /* Most of the constants in this hash algorithm are randomly choosen
659 large primes with "interesting bit patterns" and that passed
660 tests for good collision statistics on a variety of problematic
661 datasets such as:
662
663 ps = []
664 for r in range(21):
665 ps += itertools.combinations(range(20), r)
666 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
667
668 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800670 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 setentry *entry;
672 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 if (so->hash != -1)
675 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000676
Mark Dickinson57e683e2011-09-24 18:18:40 +0100677 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 while (set_next(so, &pos, &entry)) {
679 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800680 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 combinations of a small number of elements with nearby
682 hashes so that many distinct combinations collapse to only
683 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000684 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800685 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800687 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500688 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800689 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200690 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800691 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 so->hash = hash;
693 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000694}
695
Raymond Hettingera9d99362005-08-05 00:01:15 +0000696/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000697
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000698typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 PyObject_HEAD
700 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
701 Py_ssize_t si_used;
702 Py_ssize_t si_pos;
703 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000704} setiterobject;
705
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000706static void
707setiter_dealloc(setiterobject *si)
708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 Py_XDECREF(si->si_set);
710 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000711}
712
713static int
714setiter_traverse(setiterobject *si, visitproc visit, void *arg)
715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 Py_VISIT(si->si_set);
717 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000718}
719
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000720static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000721setiter_len(setiterobject *si)
722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 Py_ssize_t len = 0;
724 if (si->si_set != NULL && si->si_used == si->si_set->used)
725 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000726 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000727}
728
Armin Rigof5b3e362006-02-11 21:32:43 +0000729PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000730
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000731static PyObject *setiter_iternext(setiterobject *si);
732
733static PyObject *
734setiter_reduce(setiterobject *si)
735{
736 PyObject *list;
737 setiterobject tmp;
738
739 list = PyList_New(0);
740 if (!list)
741 return NULL;
742
Ezio Melotti0e1af282012-09-28 16:43:40 +0300743 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000744 tmp = *si;
745 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300746
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000747 /* iterate the temporary into a list */
748 for(;;) {
749 PyObject *element = setiter_iternext(&tmp);
750 if (element) {
751 if (PyList_Append(list, element)) {
752 Py_DECREF(element);
753 Py_DECREF(list);
754 Py_XDECREF(tmp.si_set);
755 return NULL;
756 }
757 Py_DECREF(element);
758 } else
759 break;
760 }
761 Py_XDECREF(tmp.si_set);
762 /* check for error */
763 if (tmp.si_set != NULL) {
764 /* we have an error */
765 Py_DECREF(list);
766 return NULL;
767 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200768 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000769}
770
771PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
772
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000773static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000775 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000777};
778
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000779static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200782 Py_ssize_t i, mask;
783 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 if (so == NULL)
787 return NULL;
788 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 if (si->si_used != so->used) {
791 PyErr_SetString(PyExc_RuntimeError,
792 "Set changed size during iteration");
793 si->si_used = -1; /* Make this state sticky */
794 return NULL;
795 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 i = si->si_pos;
798 assert(i>=0);
799 entry = so->table;
800 mask = so->mask;
801 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
802 i++;
803 si->si_pos = i+1;
804 if (i > mask)
805 goto fail;
806 si->len--;
807 key = entry[i].key;
808 Py_INCREF(key);
809 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810
811fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 Py_DECREF(so);
813 si->si_set = NULL;
814 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000815}
816
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000817PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 PyVarObject_HEAD_INIT(&PyType_Type, 0)
819 "set_iterator", /* tp_name */
820 sizeof(setiterobject), /* tp_basicsize */
821 0, /* tp_itemsize */
822 /* methods */
823 (destructor)setiter_dealloc, /* tp_dealloc */
824 0, /* tp_print */
825 0, /* tp_getattr */
826 0, /* tp_setattr */
827 0, /* tp_reserved */
828 0, /* tp_repr */
829 0, /* tp_as_number */
830 0, /* tp_as_sequence */
831 0, /* tp_as_mapping */
832 0, /* tp_hash */
833 0, /* tp_call */
834 0, /* tp_str */
835 PyObject_GenericGetAttr, /* tp_getattro */
836 0, /* tp_setattro */
837 0, /* tp_as_buffer */
838 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
839 0, /* tp_doc */
840 (traverseproc)setiter_traverse, /* tp_traverse */
841 0, /* tp_clear */
842 0, /* tp_richcompare */
843 0, /* tp_weaklistoffset */
844 PyObject_SelfIter, /* tp_iter */
845 (iternextfunc)setiter_iternext, /* tp_iternext */
846 setiter_methods, /* tp_methods */
847 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848};
849
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000850static PyObject *
851set_iter(PySetObject *so)
852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
854 if (si == NULL)
855 return NULL;
856 Py_INCREF(so);
857 si->si_set = so;
858 si->si_used = so->used;
859 si->si_pos = 0;
860 si->len = so->used;
861 _PyObject_GC_TRACK(si);
862 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000863}
864
Raymond Hettingerd7946662005-08-01 21:39:29 +0000865static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000866set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 if (PyAnySet_Check(other))
871 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (PyDict_CheckExact(other)) {
874 PyObject *value;
875 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000876 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 /* Do one big resize at the start, rather than
880 * incrementally resizing as we insert new keys. Expect
881 * that there will be no (or few) overlapping keys.
882 */
883 if (dictsize == -1)
884 return -1;
885 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
886 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
887 return -1;
888 }
889 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
890 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 an_entry.hash = hash;
893 an_entry.key = key;
894 if (set_add_entry(so, &an_entry) == -1)
895 return -1;
896 }
897 return 0;
898 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 it = PyObject_GetIter(other);
901 if (it == NULL)
902 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 while ((key = PyIter_Next(it)) != NULL) {
905 if (set_add_key(so, key) == -1) {
906 Py_DECREF(it);
907 Py_DECREF(key);
908 return -1;
909 }
910 Py_DECREF(key);
911 }
912 Py_DECREF(it);
913 if (PyErr_Occurred())
914 return -1;
915 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000916}
917
918static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000919set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
924 PyObject *other = PyTuple_GET_ITEM(args, i);
925 if (set_update_internal(so, other) == -1)
926 return NULL;
927 }
928 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000929}
930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000932"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000933
Raymond Hettinger426d9952014-05-18 21:40:20 +0100934/* XXX Todo:
935 If aligned memory allocations become available, make the
936 set object 64 byte aligned so that most of the fields
937 can be retrieved or updated in a single cache line.
938*/
939
Raymond Hettingera38123e2003-11-24 22:18:49 +0000940static PyObject *
941make_new_set(PyTypeObject *type, PyObject *iterable)
942{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200943 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700946 so = (PySetObject *)type->tp_alloc(type, 0);
947 if (so == NULL)
948 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000949
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700950 so->fill = 0;
951 so->used = 0;
952 so->mask = PySet_MINSIZE - 1;
953 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700954 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800955 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (iterable != NULL) {
959 if (set_update_internal(so, iterable) == -1) {
960 Py_DECREF(so);
961 return NULL;
962 }
963 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +0000966}
967
Raymond Hettinger7d99f092008-11-16 11:44:54 +0000968static PyObject *
969make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
972 if (PyType_IsSubtype(type, &PySet_Type))
973 type = &PySet_Type;
974 else
975 type = &PyFrozenSet_Type;
976 }
977 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +0000978}
979
Raymond Hettingerd7946662005-08-01 21:39:29 +0000980/* The empty frozenset is a singleton */
981static PyObject *emptyfrozenset = NULL;
982
Raymond Hettingera690a992003-11-16 16:17:49 +0000983static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000984frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
989 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
992 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 if (type != &PyFrozenSet_Type)
995 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (iterable != NULL) {
998 /* frozenset(f) is idempotent */
999 if (PyFrozenSet_CheckExact(iterable)) {
1000 Py_INCREF(iterable);
1001 return iterable;
1002 }
1003 result = make_new_set(type, iterable);
1004 if (result == NULL || PySet_GET_SIZE(result))
1005 return result;
1006 Py_DECREF(result);
1007 }
1008 /* The empty frozenset is a singleton */
1009 if (emptyfrozenset == NULL)
1010 emptyfrozenset = make_new_set(type, NULL);
1011 Py_XINCREF(emptyfrozenset);
1012 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001013}
1014
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001015int
1016PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001017{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001018 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001019}
1020
1021void
1022PySet_Fini(void)
1023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001025}
1026
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001027static PyObject *
1028set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1031 return NULL;
1032
1033 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001034}
1035
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001036/* set_swap_bodies() switches the contents of any two sets by moving their
1037 internal data pointers and, if needed, copying the internal smalltables.
1038 Semantically equivalent to:
1039
1040 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1041
1042 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001043 Useful for operations that update in-place (by allowing an intermediate
1044 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001045*/
1046
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001047static void
1048set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 Py_ssize_t t;
1051 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001053 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 t = a->fill; a->fill = b->fill; b->fill = t;
1056 t = a->used; a->used = b->used; b->used = t;
1057 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 u = a->table;
1060 if (a->table == a->smalltable)
1061 u = b->smalltable;
1062 a->table = b->table;
1063 if (b->table == b->smalltable)
1064 a->table = a->smalltable;
1065 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (a->table == a->smalltable || b->table == b->smalltable) {
1068 memcpy(tab, a->smalltable, sizeof(tab));
1069 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1070 memcpy(b->smalltable, tab, sizeof(tab));
1071 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1074 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1075 h = a->hash; a->hash = b->hash; b->hash = h;
1076 } else {
1077 a->hash = -1;
1078 b->hash = -1;
1079 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001080}
1081
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001082static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001083set_copy(PySetObject *so)
1084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001086}
1087
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001088static PyObject *
1089frozenset_copy(PySetObject *so)
1090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (PyFrozenSet_CheckExact(so)) {
1092 Py_INCREF(so);
1093 return (PyObject *)so;
1094 }
1095 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001096}
1097
Raymond Hettingera690a992003-11-16 16:17:49 +00001098PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1099
1100static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001101set_clear(PySetObject *so)
1102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 set_clear_internal(so);
1104 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001105}
1106
1107PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1108
1109static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001110set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 PySetObject *result;
1113 PyObject *other;
1114 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 result = (PySetObject *)set_copy(so);
1117 if (result == NULL)
1118 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1121 other = PyTuple_GET_ITEM(args, i);
1122 if ((PyObject *)so == other)
1123 continue;
1124 if (set_update_internal(result, other) == -1) {
1125 Py_DECREF(result);
1126 return NULL;
1127 }
1128 }
1129 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001130}
1131
1132PyDoc_STRVAR(union_doc,
1133 "Return the union of sets as a new set.\n\
1134\n\
1135(i.e. all elements that are in either set.)");
1136
1137static PyObject *
1138set_or(PySetObject *so, PyObject *other)
1139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001141
Brian Curtindfc80e32011-08-10 20:28:54 -05001142 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1143 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 result = (PySetObject *)set_copy(so);
1146 if (result == NULL)
1147 return NULL;
1148 if ((PyObject *)so == other)
1149 return (PyObject *)result;
1150 if (set_update_internal(result, other) == -1) {
1151 Py_DECREF(result);
1152 return NULL;
1153 }
1154 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001155}
1156
Raymond Hettingera690a992003-11-16 16:17:49 +00001157static PyObject *
1158set_ior(PySetObject *so, PyObject *other)
1159{
Brian Curtindfc80e32011-08-10 20:28:54 -05001160 if (!PyAnySet_Check(other))
1161 Py_RETURN_NOTIMPLEMENTED;
1162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (set_update_internal(so, other) == -1)
1164 return NULL;
1165 Py_INCREF(so);
1166 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001167}
1168
1169static PyObject *
1170set_intersection(PySetObject *so, PyObject *other)
1171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 PySetObject *result;
1173 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 if ((PyObject *)so == other)
1176 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1179 if (result == NULL)
1180 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 if (PyAnySet_Check(other)) {
1183 Py_ssize_t pos = 0;
1184 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1187 tmp = (PyObject *)so;
1188 so = (PySetObject *)other;
1189 other = tmp;
1190 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 while (set_next((PySetObject *)other, &pos, &entry)) {
1193 int rv = set_contains_entry(so, entry);
1194 if (rv == -1) {
1195 Py_DECREF(result);
1196 return NULL;
1197 }
1198 if (rv) {
1199 if (set_add_entry(result, entry) == -1) {
1200 Py_DECREF(result);
1201 return NULL;
1202 }
1203 }
1204 }
1205 return (PyObject *)result;
1206 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 it = PyObject_GetIter(other);
1209 if (it == NULL) {
1210 Py_DECREF(result);
1211 return NULL;
1212 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 while ((key = PyIter_Next(it)) != NULL) {
1215 int rv;
1216 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001217 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (hash == -1) {
1220 Py_DECREF(it);
1221 Py_DECREF(result);
1222 Py_DECREF(key);
1223 return NULL;
1224 }
1225 entry.hash = hash;
1226 entry.key = key;
1227 rv = set_contains_entry(so, &entry);
1228 if (rv == -1) {
1229 Py_DECREF(it);
1230 Py_DECREF(result);
1231 Py_DECREF(key);
1232 return NULL;
1233 }
1234 if (rv) {
1235 if (set_add_entry(result, &entry) == -1) {
1236 Py_DECREF(it);
1237 Py_DECREF(result);
1238 Py_DECREF(key);
1239 return NULL;
1240 }
1241 }
1242 Py_DECREF(key);
1243 }
1244 Py_DECREF(it);
1245 if (PyErr_Occurred()) {
1246 Py_DECREF(result);
1247 return NULL;
1248 }
1249 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001250}
1251
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001252static PyObject *
1253set_intersection_multi(PySetObject *so, PyObject *args)
1254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 Py_ssize_t i;
1256 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (PyTuple_GET_SIZE(args) == 0)
1259 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 Py_INCREF(so);
1262 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1263 PyObject *other = PyTuple_GET_ITEM(args, i);
1264 PyObject *newresult = set_intersection((PySetObject *)result, other);
1265 if (newresult == NULL) {
1266 Py_DECREF(result);
1267 return NULL;
1268 }
1269 Py_DECREF(result);
1270 result = newresult;
1271 }
1272 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001273}
1274
Raymond Hettingera690a992003-11-16 16:17:49 +00001275PyDoc_STRVAR(intersection_doc,
1276"Return the intersection of two sets as a new set.\n\
1277\n\
1278(i.e. all elements that are in both sets.)");
1279
1280static PyObject *
1281set_intersection_update(PySetObject *so, PyObject *other)
1282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 tmp = set_intersection(so, other);
1286 if (tmp == NULL)
1287 return NULL;
1288 set_swap_bodies(so, (PySetObject *)tmp);
1289 Py_DECREF(tmp);
1290 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001291}
1292
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001293static PyObject *
1294set_intersection_update_multi(PySetObject *so, PyObject *args)
1295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 tmp = set_intersection_multi(so, args);
1299 if (tmp == NULL)
1300 return NULL;
1301 set_swap_bodies(so, (PySetObject *)tmp);
1302 Py_DECREF(tmp);
1303 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001304}
1305
Raymond Hettingera690a992003-11-16 16:17:49 +00001306PyDoc_STRVAR(intersection_update_doc,
1307"Update a set with the intersection of itself and another.");
1308
1309static PyObject *
1310set_and(PySetObject *so, PyObject *other)
1311{
Brian Curtindfc80e32011-08-10 20:28:54 -05001312 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1313 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001315}
1316
1317static PyObject *
1318set_iand(PySetObject *so, PyObject *other)
1319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001321
Brian Curtindfc80e32011-08-10 20:28:54 -05001322 if (!PyAnySet_Check(other))
1323 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 result = set_intersection_update(so, other);
1325 if (result == NULL)
1326 return NULL;
1327 Py_DECREF(result);
1328 Py_INCREF(so);
1329 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001330}
1331
Guido van Rossum58da9312007-11-10 23:39:45 +00001332static PyObject *
1333set_isdisjoint(PySetObject *so, PyObject *other)
1334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 if ((PyObject *)so == other) {
1338 if (PySet_GET_SIZE(so) == 0)
1339 Py_RETURN_TRUE;
1340 else
1341 Py_RETURN_FALSE;
1342 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (PyAnySet_CheckExact(other)) {
1345 Py_ssize_t pos = 0;
1346 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1349 tmp = (PyObject *)so;
1350 so = (PySetObject *)other;
1351 other = tmp;
1352 }
1353 while (set_next((PySetObject *)other, &pos, &entry)) {
1354 int rv = set_contains_entry(so, entry);
1355 if (rv == -1)
1356 return NULL;
1357 if (rv)
1358 Py_RETURN_FALSE;
1359 }
1360 Py_RETURN_TRUE;
1361 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 it = PyObject_GetIter(other);
1364 if (it == NULL)
1365 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 while ((key = PyIter_Next(it)) != NULL) {
1368 int rv;
1369 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001370 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 if (hash == -1) {
1373 Py_DECREF(key);
1374 Py_DECREF(it);
1375 return NULL;
1376 }
1377 entry.hash = hash;
1378 entry.key = key;
1379 rv = set_contains_entry(so, &entry);
1380 Py_DECREF(key);
1381 if (rv == -1) {
1382 Py_DECREF(it);
1383 return NULL;
1384 }
1385 if (rv) {
1386 Py_DECREF(it);
1387 Py_RETURN_FALSE;
1388 }
1389 }
1390 Py_DECREF(it);
1391 if (PyErr_Occurred())
1392 return NULL;
1393 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001394}
1395
1396PyDoc_STRVAR(isdisjoint_doc,
1397"Return True if two sets have a null intersection.");
1398
Neal Norwitz6576bd82005-11-13 18:41:28 +00001399static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001400set_difference_update_internal(PySetObject *so, PyObject *other)
1401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if ((PyObject *)so == other)
1403 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 if (PyAnySet_Check(other)) {
1406 setentry *entry;
1407 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 while (set_next((PySetObject *)other, &pos, &entry))
1410 if (set_discard_entry(so, entry) == -1)
1411 return -1;
1412 } else {
1413 PyObject *key, *it;
1414 it = PyObject_GetIter(other);
1415 if (it == NULL)
1416 return -1;
1417
1418 while ((key = PyIter_Next(it)) != NULL) {
1419 if (set_discard_key(so, key) == -1) {
1420 Py_DECREF(it);
1421 Py_DECREF(key);
1422 return -1;
1423 }
1424 Py_DECREF(key);
1425 }
1426 Py_DECREF(it);
1427 if (PyErr_Occurred())
1428 return -1;
1429 }
1430 /* If more than 1/5 are dummies, then resize them away. */
1431 if ((so->fill - so->used) * 5 < so->mask)
1432 return 0;
1433 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001434}
1435
Raymond Hettingera690a992003-11-16 16:17:49 +00001436static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001437set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1442 PyObject *other = PyTuple_GET_ITEM(args, i);
1443 if (set_difference_update_internal(so, other) == -1)
1444 return NULL;
1445 }
1446 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001447}
1448
1449PyDoc_STRVAR(difference_update_doc,
1450"Remove all elements of another set from this set.");
1451
1452static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001453set_copy_and_difference(PySetObject *so, PyObject *other)
1454{
1455 PyObject *result;
1456
1457 result = set_copy(so);
1458 if (result == NULL)
1459 return NULL;
1460 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1461 return result;
1462 Py_DECREF(result);
1463 return NULL;
1464}
1465
1466static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001467set_difference(PySetObject *so, PyObject *other)
1468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 PyObject *result;
1470 setentry *entry;
1471 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001474 return set_copy_and_difference(so, other);
1475 }
1476
1477 /* If len(so) much more than len(other), it's more efficient to simply copy
1478 * so and then iterate other looking for common elements. */
1479 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1480 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 result = make_new_set_basetype(Py_TYPE(so), NULL);
1484 if (result == NULL)
1485 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 if (PyDict_CheckExact(other)) {
1488 while (set_next(so, &pos, &entry)) {
1489 setentry entrycopy;
1490 entrycopy.hash = entry->hash;
1491 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001492 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1494 Py_DECREF(result);
1495 return NULL;
1496 }
1497 }
1498 }
1499 return result;
1500 }
1501
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001502 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 while (set_next(so, &pos, &entry)) {
1504 int rv = set_contains_entry((PySetObject *)other, entry);
1505 if (rv == -1) {
1506 Py_DECREF(result);
1507 return NULL;
1508 }
1509 if (!rv) {
1510 if (set_add_entry((PySetObject *)result, entry) == -1) {
1511 Py_DECREF(result);
1512 return NULL;
1513 }
1514 }
1515 }
1516 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001517}
1518
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001519static PyObject *
1520set_difference_multi(PySetObject *so, PyObject *args)
1521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 Py_ssize_t i;
1523 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (PyTuple_GET_SIZE(args) == 0)
1526 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 other = PyTuple_GET_ITEM(args, 0);
1529 result = set_difference(so, other);
1530 if (result == NULL)
1531 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1534 other = PyTuple_GET_ITEM(args, i);
1535 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1536 Py_DECREF(result);
1537 return NULL;
1538 }
1539 }
1540 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001541}
1542
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001543PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001544"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001545\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001546(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001547static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001548set_sub(PySetObject *so, PyObject *other)
1549{
Brian Curtindfc80e32011-08-10 20:28:54 -05001550 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1551 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001553}
1554
1555static PyObject *
1556set_isub(PySetObject *so, PyObject *other)
1557{
Brian Curtindfc80e32011-08-10 20:28:54 -05001558 if (!PyAnySet_Check(other))
1559 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 if (set_difference_update_internal(so, other) == -1)
1561 return NULL;
1562 Py_INCREF(so);
1563 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001564}
1565
1566static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001567set_symmetric_difference_update(PySetObject *so, PyObject *other)
1568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 PySetObject *otherset;
1570 PyObject *key;
1571 Py_ssize_t pos = 0;
1572 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 if ((PyObject *)so == other)
1575 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 if (PyDict_CheckExact(other)) {
1578 PyObject *value;
1579 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001580 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1582 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001583
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001584 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 an_entry.hash = hash;
1586 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001589 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001590 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001593 if (rv == DISCARD_NOTFOUND) {
1594 if (set_add_entry(so, &an_entry) == -1) {
1595 Py_DECREF(key);
1596 return NULL;
1597 }
1598 }
Georg Brandl2d444492010-09-03 10:52:55 +00001599 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 }
1601 Py_RETURN_NONE;
1602 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 if (PyAnySet_Check(other)) {
1605 Py_INCREF(other);
1606 otherset = (PySetObject *)other;
1607 } else {
1608 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1609 if (otherset == NULL)
1610 return NULL;
1611 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 while (set_next(otherset, &pos, &entry)) {
1614 int rv = set_discard_entry(so, entry);
1615 if (rv == -1) {
1616 Py_DECREF(otherset);
1617 return NULL;
1618 }
1619 if (rv == DISCARD_NOTFOUND) {
1620 if (set_add_entry(so, entry) == -1) {
1621 Py_DECREF(otherset);
1622 return NULL;
1623 }
1624 }
1625 }
1626 Py_DECREF(otherset);
1627 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001628}
1629
1630PyDoc_STRVAR(symmetric_difference_update_doc,
1631"Update a set with the symmetric difference of itself and another.");
1632
1633static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001634set_symmetric_difference(PySetObject *so, PyObject *other)
1635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 PyObject *rv;
1637 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1640 if (otherset == NULL)
1641 return NULL;
1642 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1643 if (rv == NULL)
1644 return NULL;
1645 Py_DECREF(rv);
1646 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001647}
1648
1649PyDoc_STRVAR(symmetric_difference_doc,
1650"Return the symmetric difference of two sets as a new set.\n\
1651\n\
1652(i.e. all elements that are in exactly one of the sets.)");
1653
1654static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001655set_xor(PySetObject *so, PyObject *other)
1656{
Brian Curtindfc80e32011-08-10 20:28:54 -05001657 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1658 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001660}
1661
1662static PyObject *
1663set_ixor(PySetObject *so, PyObject *other)
1664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001666
Brian Curtindfc80e32011-08-10 20:28:54 -05001667 if (!PyAnySet_Check(other))
1668 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 result = set_symmetric_difference_update(so, other);
1670 if (result == NULL)
1671 return NULL;
1672 Py_DECREF(result);
1673 Py_INCREF(so);
1674 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001675}
1676
1677static PyObject *
1678set_issubset(PySetObject *so, PyObject *other)
1679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 setentry *entry;
1681 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 if (!PyAnySet_Check(other)) {
1684 PyObject *tmp, *result;
1685 tmp = make_new_set(&PySet_Type, other);
1686 if (tmp == NULL)
1687 return NULL;
1688 result = set_issubset(so, tmp);
1689 Py_DECREF(tmp);
1690 return result;
1691 }
1692 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1693 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 while (set_next(so, &pos, &entry)) {
1696 int rv = set_contains_entry((PySetObject *)other, entry);
1697 if (rv == -1)
1698 return NULL;
1699 if (!rv)
1700 Py_RETURN_FALSE;
1701 }
1702 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001703}
1704
1705PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1706
1707static PyObject *
1708set_issuperset(PySetObject *so, PyObject *other)
1709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 if (!PyAnySet_Check(other)) {
1713 tmp = make_new_set(&PySet_Type, other);
1714 if (tmp == NULL)
1715 return NULL;
1716 result = set_issuperset(so, tmp);
1717 Py_DECREF(tmp);
1718 return result;
1719 }
1720 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001721}
1722
1723PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1724
Raymond Hettingera690a992003-11-16 16:17:49 +00001725static PyObject *
1726set_richcompare(PySetObject *v, PyObject *w, int op)
1727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001729
Brian Curtindfc80e32011-08-10 20:28:54 -05001730 if(!PyAnySet_Check(w))
1731 Py_RETURN_NOTIMPLEMENTED;
1732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 switch (op) {
1734 case Py_EQ:
1735 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1736 Py_RETURN_FALSE;
1737 if (v->hash != -1 &&
1738 ((PySetObject *)w)->hash != -1 &&
1739 v->hash != ((PySetObject *)w)->hash)
1740 Py_RETURN_FALSE;
1741 return set_issubset(v, w);
1742 case Py_NE:
1743 r1 = set_richcompare(v, w, Py_EQ);
1744 if (r1 == NULL)
1745 return NULL;
1746 r2 = PyBool_FromLong(PyObject_Not(r1));
1747 Py_DECREF(r1);
1748 return r2;
1749 case Py_LE:
1750 return set_issubset(v, w);
1751 case Py_GE:
1752 return set_issuperset(v, w);
1753 case Py_LT:
1754 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1755 Py_RETURN_FALSE;
1756 return set_issubset(v, w);
1757 case Py_GT:
1758 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1759 Py_RETURN_FALSE;
1760 return set_issuperset(v, w);
1761 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001762 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001763}
1764
Raymond Hettingera690a992003-11-16 16:17:49 +00001765static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001766set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 if (set_add_key(so, key) == -1)
1769 return NULL;
1770 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001771}
1772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001774"Add an element to a set.\n\
1775\n\
1776This has no effect if the element is already present.");
1777
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001778static int
1779set_contains(PySetObject *so, PyObject *key)
1780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 PyObject *tmpkey;
1782 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 rv = set_contains_key(so, key);
1785 if (rv == -1) {
1786 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1787 return -1;
1788 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001789 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 if (tmpkey == NULL)
1791 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001792 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 Py_DECREF(tmpkey);
1794 }
1795 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001796}
1797
1798static PyObject *
1799set_direct_contains(PySetObject *so, PyObject *key)
1800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 result = set_contains(so, key);
1804 if (result == -1)
1805 return NULL;
1806 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001807}
1808
1809PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1810
Raymond Hettingera690a992003-11-16 16:17:49 +00001811static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001812set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 PyObject *tmpkey;
1815 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 rv = set_discard_key(so, key);
1818 if (rv == -1) {
1819 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1820 return NULL;
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 NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 Py_DECREF(tmpkey);
1827 if (rv == -1)
1828 return NULL;
1829 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001832 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 return NULL;
1834 }
1835 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001836}
1837
1838PyDoc_STRVAR(remove_doc,
1839"Remove an element from a set; it must be a member.\n\
1840\n\
1841If the element is not a member, raise a KeyError.");
1842
1843static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001844set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001845{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001846 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 rv = set_discard_key(so, key);
1850 if (rv == -1) {
1851 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1852 return NULL;
1853 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001854 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 if (tmpkey == NULL)
1856 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001857 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001859 if (rv == -1)
1860 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 }
1862 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001863}
1864
1865PyDoc_STRVAR(discard_doc,
1866"Remove an element from a set if it is a member.\n\
1867\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001869
1870static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001871set_reduce(PySetObject *so)
1872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001874 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 keys = PySequence_List((PyObject *)so);
1877 if (keys == NULL)
1878 goto done;
1879 args = PyTuple_Pack(1, keys);
1880 if (args == NULL)
1881 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001882 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 if (dict == NULL) {
1884 PyErr_Clear();
1885 dict = Py_None;
1886 Py_INCREF(dict);
1887 }
1888 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001889done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 Py_XDECREF(args);
1891 Py_XDECREF(keys);
1892 Py_XDECREF(dict);
1893 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001894}
1895
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001896static PyObject *
1897set_sizeof(PySetObject *so)
1898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 res = sizeof(PySetObject);
1902 if (so->table != so->smalltable)
1903 res = res + (so->mask + 1) * sizeof(setentry);
1904 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001905}
1906
1907PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001908static int
1909set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 if (!PyAnySet_Check(self))
1914 return -1;
1915 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1916 return -1;
1917 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1918 return -1;
1919 set_clear_internal(self);
1920 self->hash = -1;
1921 if (iterable == NULL)
1922 return 0;
1923 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001924}
1925
Raymond Hettingera690a992003-11-16 16:17:49 +00001926static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 set_len, /* sq_length */
1928 0, /* sq_concat */
1929 0, /* sq_repeat */
1930 0, /* sq_item */
1931 0, /* sq_slice */
1932 0, /* sq_ass_item */
1933 0, /* sq_ass_slice */
1934 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001935};
1936
1937/* set object ********************************************************/
1938
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001939#ifdef Py_DEBUG
1940static PyObject *test_c_api(PySetObject *so);
1941
1942PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1943All is well if assertions don't fail.");
1944#endif
1945
Raymond Hettingera690a992003-11-16 16:17:49 +00001946static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 {"add", (PyCFunction)set_add, METH_O,
1948 add_doc},
1949 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1950 clear_doc},
1951 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1952 contains_doc},
1953 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1954 copy_doc},
1955 {"discard", (PyCFunction)set_discard, METH_O,
1956 discard_doc},
1957 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
1958 difference_doc},
1959 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
1960 difference_update_doc},
1961 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
1962 intersection_doc},
1963 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
1964 intersection_update_doc},
1965 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1966 isdisjoint_doc},
1967 {"issubset", (PyCFunction)set_issubset, METH_O,
1968 issubset_doc},
1969 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1970 issuperset_doc},
1971 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1972 pop_doc},
1973 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1974 reduce_doc},
1975 {"remove", (PyCFunction)set_remove, METH_O,
1976 remove_doc},
1977 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
1978 sizeof_doc},
1979 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1980 symmetric_difference_doc},
1981 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1982 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001983#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1985 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001986#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 {"union", (PyCFunction)set_union, METH_VARARGS,
1988 union_doc},
1989 {"update", (PyCFunction)set_update, METH_VARARGS,
1990 update_doc},
1991 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00001992};
1993
1994static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 0, /*nb_add*/
1996 (binaryfunc)set_sub, /*nb_subtract*/
1997 0, /*nb_multiply*/
1998 0, /*nb_remainder*/
1999 0, /*nb_divmod*/
2000 0, /*nb_power*/
2001 0, /*nb_negative*/
2002 0, /*nb_positive*/
2003 0, /*nb_absolute*/
2004 0, /*nb_bool*/
2005 0, /*nb_invert*/
2006 0, /*nb_lshift*/
2007 0, /*nb_rshift*/
2008 (binaryfunc)set_and, /*nb_and*/
2009 (binaryfunc)set_xor, /*nb_xor*/
2010 (binaryfunc)set_or, /*nb_or*/
2011 0, /*nb_int*/
2012 0, /*nb_reserved*/
2013 0, /*nb_float*/
2014 0, /*nb_inplace_add*/
2015 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2016 0, /*nb_inplace_multiply*/
2017 0, /*nb_inplace_remainder*/
2018 0, /*nb_inplace_power*/
2019 0, /*nb_inplace_lshift*/
2020 0, /*nb_inplace_rshift*/
2021 (binaryfunc)set_iand, /*nb_inplace_and*/
2022 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2023 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002024};
2025
2026PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002027"set() -> new empty set object\n\
2028set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002029\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002030Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002031
2032PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2034 "set", /* tp_name */
2035 sizeof(PySetObject), /* tp_basicsize */
2036 0, /* tp_itemsize */
2037 /* methods */
2038 (destructor)set_dealloc, /* tp_dealloc */
2039 0, /* tp_print */
2040 0, /* tp_getattr */
2041 0, /* tp_setattr */
2042 0, /* tp_reserved */
2043 (reprfunc)set_repr, /* tp_repr */
2044 &set_as_number, /* tp_as_number */
2045 &set_as_sequence, /* tp_as_sequence */
2046 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002047 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 0, /* tp_call */
2049 0, /* tp_str */
2050 PyObject_GenericGetAttr, /* tp_getattro */
2051 0, /* tp_setattro */
2052 0, /* tp_as_buffer */
2053 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2054 Py_TPFLAGS_BASETYPE, /* tp_flags */
2055 set_doc, /* tp_doc */
2056 (traverseproc)set_traverse, /* tp_traverse */
2057 (inquiry)set_clear_internal, /* tp_clear */
2058 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002059 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2060 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 0, /* tp_iternext */
2062 set_methods, /* tp_methods */
2063 0, /* tp_members */
2064 0, /* tp_getset */
2065 0, /* tp_base */
2066 0, /* tp_dict */
2067 0, /* tp_descr_get */
2068 0, /* tp_descr_set */
2069 0, /* tp_dictoffset */
2070 (initproc)set_init, /* tp_init */
2071 PyType_GenericAlloc, /* tp_alloc */
2072 set_new, /* tp_new */
2073 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002074};
2075
2076/* frozenset object ********************************************************/
2077
2078
2079static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2081 contains_doc},
2082 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2083 copy_doc},
2084 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2085 difference_doc},
2086 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2087 intersection_doc},
2088 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2089 isdisjoint_doc},
2090 {"issubset", (PyCFunction)set_issubset, METH_O,
2091 issubset_doc},
2092 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2093 issuperset_doc},
2094 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2095 reduce_doc},
2096 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2097 sizeof_doc},
2098 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2099 symmetric_difference_doc},
2100 {"union", (PyCFunction)set_union, METH_VARARGS,
2101 union_doc},
2102 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002103};
2104
2105static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 0, /*nb_add*/
2107 (binaryfunc)set_sub, /*nb_subtract*/
2108 0, /*nb_multiply*/
2109 0, /*nb_remainder*/
2110 0, /*nb_divmod*/
2111 0, /*nb_power*/
2112 0, /*nb_negative*/
2113 0, /*nb_positive*/
2114 0, /*nb_absolute*/
2115 0, /*nb_bool*/
2116 0, /*nb_invert*/
2117 0, /*nb_lshift*/
2118 0, /*nb_rshift*/
2119 (binaryfunc)set_and, /*nb_and*/
2120 (binaryfunc)set_xor, /*nb_xor*/
2121 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002122};
2123
2124PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002125"frozenset() -> empty frozenset object\n\
2126frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002127\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002128Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002129
2130PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2132 "frozenset", /* tp_name */
2133 sizeof(PySetObject), /* tp_basicsize */
2134 0, /* tp_itemsize */
2135 /* methods */
2136 (destructor)set_dealloc, /* tp_dealloc */
2137 0, /* tp_print */
2138 0, /* tp_getattr */
2139 0, /* tp_setattr */
2140 0, /* tp_reserved */
2141 (reprfunc)set_repr, /* tp_repr */
2142 &frozenset_as_number, /* tp_as_number */
2143 &set_as_sequence, /* tp_as_sequence */
2144 0, /* tp_as_mapping */
2145 frozenset_hash, /* tp_hash */
2146 0, /* tp_call */
2147 0, /* tp_str */
2148 PyObject_GenericGetAttr, /* tp_getattro */
2149 0, /* tp_setattro */
2150 0, /* tp_as_buffer */
2151 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2152 Py_TPFLAGS_BASETYPE, /* tp_flags */
2153 frozenset_doc, /* tp_doc */
2154 (traverseproc)set_traverse, /* tp_traverse */
2155 (inquiry)set_clear_internal, /* tp_clear */
2156 (richcmpfunc)set_richcompare, /* tp_richcompare */
2157 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2158 (getiterfunc)set_iter, /* tp_iter */
2159 0, /* tp_iternext */
2160 frozenset_methods, /* tp_methods */
2161 0, /* tp_members */
2162 0, /* tp_getset */
2163 0, /* tp_base */
2164 0, /* tp_dict */
2165 0, /* tp_descr_get */
2166 0, /* tp_descr_set */
2167 0, /* tp_dictoffset */
2168 0, /* tp_init */
2169 PyType_GenericAlloc, /* tp_alloc */
2170 frozenset_new, /* tp_new */
2171 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002172};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002173
2174
2175/***** C API functions *************************************************/
2176
2177PyObject *
2178PySet_New(PyObject *iterable)
2179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002181}
2182
2183PyObject *
2184PyFrozenSet_New(PyObject *iterable)
2185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002187}
2188
Neal Norwitz8c49c822006-03-04 18:41:19 +00002189Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002190PySet_Size(PyObject *anyset)
2191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 if (!PyAnySet_Check(anyset)) {
2193 PyErr_BadInternalCall();
2194 return -1;
2195 }
2196 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002197}
2198
2199int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002200PySet_Clear(PyObject *set)
2201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 if (!PySet_Check(set)) {
2203 PyErr_BadInternalCall();
2204 return -1;
2205 }
2206 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002207}
2208
2209int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002210PySet_Contains(PyObject *anyset, PyObject *key)
2211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 if (!PyAnySet_Check(anyset)) {
2213 PyErr_BadInternalCall();
2214 return -1;
2215 }
2216 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002217}
2218
2219int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002220PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 if (!PySet_Check(set)) {
2223 PyErr_BadInternalCall();
2224 return -1;
2225 }
2226 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002227}
2228
2229int
Christian Heimesfd66e512008-01-29 12:18:50 +00002230PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 if (!PySet_Check(anyset) &&
2233 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2234 PyErr_BadInternalCall();
2235 return -1;
2236 }
2237 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002238}
2239
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002240int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002241_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 if (!PyAnySet_Check(set)) {
2246 PyErr_BadInternalCall();
2247 return -1;
2248 }
2249 if (set_next((PySetObject *)set, pos, &entry) == 0)
2250 return 0;
2251 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002252 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002254}
2255
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002256PyObject *
2257PySet_Pop(PyObject *set)
2258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 if (!PySet_Check(set)) {
2260 PyErr_BadInternalCall();
2261 return NULL;
2262 }
2263 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002264}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002265
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002266int
2267_PySet_Update(PyObject *set, PyObject *iterable)
2268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 if (!PySet_Check(set)) {
2270 PyErr_BadInternalCall();
2271 return -1;
2272 }
2273 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002274}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002275
Raymond Hettingere259f132013-12-15 11:56:14 -08002276/* Exported for the gdb plugin's benefit. */
2277PyObject *_PySet_Dummy = dummy;
2278
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002279#ifdef Py_DEBUG
2280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002282 Returns True and original set is restored. */
2283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284#define assertRaises(call_return_value, exception) \
2285 do { \
2286 assert(call_return_value); \
2287 assert(PyErr_ExceptionMatches(exception)); \
2288 PyErr_Clear(); \
2289 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002290
2291static PyObject *
2292test_c_api(PySetObject *so)
2293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 Py_ssize_t count;
2295 char *s;
2296 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002297 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002299 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 /* Verify preconditions */
2303 assert(PyAnySet_Check(ob));
2304 assert(PyAnySet_CheckExact(ob));
2305 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 /* so.clear(); so |= set("abc"); */
2308 str = PyUnicode_FromString("abc");
2309 if (str == NULL)
2310 return NULL;
2311 set_clear_internal(so);
2312 if (set_update_internal(so, str) == -1) {
2313 Py_DECREF(str);
2314 return NULL;
2315 }
2316 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 /* Exercise type/size checks */
2319 assert(PySet_Size(ob) == 3);
2320 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 /* Raise TypeError for non-iterable constructor arguments */
2323 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2324 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 /* Raise TypeError for unhashable key */
2327 dup = PySet_New(ob);
2328 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2329 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2330 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 /* Exercise successful pop, contains, add, and discard */
2333 elem = PySet_Pop(ob);
2334 assert(PySet_Contains(ob, elem) == 0);
2335 assert(PySet_GET_SIZE(ob) == 2);
2336 assert(PySet_Add(ob, elem) == 0);
2337 assert(PySet_Contains(ob, elem) == 1);
2338 assert(PySet_GET_SIZE(ob) == 3);
2339 assert(PySet_Discard(ob, elem) == 1);
2340 assert(PySet_GET_SIZE(ob) == 2);
2341 assert(PySet_Discard(ob, elem) == 0);
2342 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 /* Exercise clear */
2345 dup2 = PySet_New(dup);
2346 assert(PySet_Clear(dup2) == 0);
2347 assert(PySet_Size(dup2) == 0);
2348 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 /* Raise SystemError on clear or update of frozen set */
2351 f = PyFrozenSet_New(dup);
2352 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2353 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2354 assert(PySet_Add(f, elem) == 0);
2355 Py_INCREF(f);
2356 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2357 Py_DECREF(f);
2358 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 /* Exercise direct iteration */
2361 i = 0, count = 0;
2362 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2363 s = _PyUnicode_AsString(x);
2364 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2365 count++;
2366 }
2367 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 /* Exercise updates */
2370 dup2 = PySet_New(NULL);
2371 assert(_PySet_Update(dup2, dup) == 0);
2372 assert(PySet_Size(dup2) == 3);
2373 assert(_PySet_Update(dup2, dup) == 0);
2374 assert(PySet_Size(dup2) == 3);
2375 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 /* Raise SystemError when self argument is not a set or frozenset. */
2378 t = PyTuple_New(0);
2379 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2380 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2381 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 /* Raise SystemError when self argument is not a set. */
2384 f = PyFrozenSet_New(dup);
2385 assert(PySet_Size(f) == 3);
2386 assert(PyFrozenSet_CheckExact(f));
2387 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2388 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2389 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* Raise KeyError when popping from an empty set */
2392 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2393 Py_DECREF(ob);
2394 assert(PySet_GET_SIZE(ob) == 0);
2395 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 /* Restore the set from the copy using the PyNumber API */
2398 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2399 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* Verify constructors accept NULL arguments */
2402 f = PySet_New(NULL);
2403 assert(f != NULL);
2404 assert(PySet_GET_SIZE(f) == 0);
2405 Py_DECREF(f);
2406 f = PyFrozenSet_New(NULL);
2407 assert(f != NULL);
2408 assert(PyFrozenSet_CheckExact(f));
2409 assert(PySet_GET_SIZE(f) == 0);
2410 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 Py_DECREF(elem);
2413 Py_DECREF(dup);
2414 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002415}
2416
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002417#undef assertRaises
2418
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002419#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002420
2421/***** Dummy Struct *************************************************/
2422
2423static PyObject *
2424dummy_repr(PyObject *op)
2425{
2426 return PyUnicode_FromString("<dummy key>");
2427}
2428
2429static void
2430dummy_dealloc(PyObject* ignore)
2431{
2432 Py_FatalError("deallocating <dummy key>");
2433}
2434
2435static PyTypeObject _PySetDummy_Type = {
2436 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2437 "<dummy key> type",
2438 0,
2439 0,
2440 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2441 0, /*tp_print*/
2442 0, /*tp_getattr*/
2443 0, /*tp_setattr*/
2444 0, /*tp_reserved*/
2445 dummy_repr, /*tp_repr*/
2446 0, /*tp_as_number*/
2447 0, /*tp_as_sequence*/
2448 0, /*tp_as_mapping*/
2449 0, /*tp_hash */
2450 0, /*tp_call */
2451 0, /*tp_str */
2452 0, /*tp_getattro */
2453 0, /*tp_setattro */
2454 0, /*tp_as_buffer */
2455 Py_TPFLAGS_DEFAULT, /*tp_flags */
2456};
2457
2458static PyObject _dummy_struct = {
2459 _PyObject_EXTRA_INIT
2460 2, &_PySetDummy_Type
2461};
2462