blob: 704d7e2b2a76ec2db079eb275592a76f69886937 [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;
Yury Selivanov7aa53412015-05-30 10:57:56 -040055 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;
Raymond Hettinger438f9132015-02-09 06:48:29 -060087 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070088 }
Yury Selivanov7aa53412015-05-30 10:57:56 -040089 if (entry->hash == -1 && freeslot == NULL)
90 freeslot = entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070091
Raymond Hettingerc658d852015-02-02 08:35:00 -080092 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080093 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080094 entry++;
Yury Selivanov7aa53412015-05-30 10:57:56 -040095 if (entry->key == NULL)
96 goto found_null;
Raymond Hettingerc658d852015-02-02 08:35:00 -080097 if (entry->hash == hash) {
98 PyObject *startkey = entry->key;
99 assert(startkey != dummy);
100 if (startkey == key)
101 return entry;
102 if (PyUnicode_CheckExact(startkey)
103 && PyUnicode_CheckExact(key)
104 && unicode_eq(startkey, key))
105 return entry;
106 Py_INCREF(startkey);
107 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
108 Py_DECREF(startkey);
109 if (cmp < 0)
110 return NULL;
111 if (table != so->table || entry->key != startkey)
112 return set_lookkey(so, key, hash);
113 if (cmp > 0)
114 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600115 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800116 }
Raymond Hettinger438f9132015-02-09 06:48:29 -0600117 if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800118 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700121
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700122 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800123 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700124
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800125 entry = &table[i];
Yury Selivanov7aa53412015-05-30 10:57:56 -0400126 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700127 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700129 found_null:
Yury Selivanov7aa53412015-05-30 10:57:56 -0400130 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000131}
132
133/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000134Internal routine used by set_table_resize() to insert an item which is
135known to be absent from the set. This routine also assumes that
136the set contains no deleted entries. Besides the performance benefit,
137using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
138Note that no refcounts are changed by this routine; if needed, the caller
139is responsible for incref'ing `key`.
140*/
141static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200142set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200145 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700146 size_t perturb = hash;
147 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800148 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700149 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700151 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800152 entry = &table[i];
153 if (entry->key == NULL)
154 goto found_null;
155 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800156 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800157 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800158 if (entry->key == NULL)
159 goto found_null;
160 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700161 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700162 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800163 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700165 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 entry->key = key;
167 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700168 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000170}
171
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700172/* ======== End logic for probing the hash table ========================== */
173/* ======================================================================== */
174
Yury Selivanov7aa53412015-05-30 10:57:56 -0400175
176/*
177Internal routine to insert a new key into the table.
178Used by the public insert routine.
179Eats a reference to key.
180*/
181static int
182set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
183{
184 setentry *entry;
185
186 entry = set_lookkey(so, key, hash);
187 if (entry == NULL)
188 return -1;
189 if (entry->key == NULL) {
190 /* UNUSED */
191 entry->key = key;
192 entry->hash = hash;
193 so->fill++;
194 so->used++;
195 } else if (entry->key == dummy) {
196 /* DUMMY */
197 entry->key = key;
198 entry->hash = hash;
199 so->used++;
200 } else {
201 /* ACTIVE */
202 Py_DECREF(key);
203 }
204 return 0;
205}
206
Thomas Wouters89f507f2006-12-13 04:49:30 +0000207/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000210actually be smaller than the old one.
211*/
212static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000213set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_ssize_t newsize;
216 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800217 Py_ssize_t oldfill = so->fill;
218 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 int is_oldtable_malloced;
220 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100225 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 for (newsize = PySet_MINSIZE;
227 newsize <= minused && newsize > 0;
228 newsize <<= 1)
229 ;
230 if (newsize <= 0) {
231 PyErr_NoMemory();
232 return -1;
233 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 /* Get space for a new table. */
236 oldtable = so->table;
237 assert(oldtable != NULL);
238 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (newsize == PySet_MINSIZE) {
241 /* A large table is shrinking, or we can't get any smaller. */
242 newtable = so->smalltable;
243 if (newtable == oldtable) {
244 if (so->fill == so->used) {
245 /* No dummies, so no point doing anything. */
246 return 0;
247 }
248 /* We're not going to resize it, but rebuild the
249 table anyway to purge old dummy entries.
250 Subtle: This is *necessary* if fill==size,
251 as set_lookkey needs at least one virgin slot to
252 terminate failing searches. If fill < size, it's
253 merely desirable, as dummies slow searches. */
254 assert(so->fill > so->used);
255 memcpy(small_copy, oldtable, sizeof(small_copy));
256 oldtable = small_copy;
257 }
258 }
259 else {
260 newtable = PyMem_NEW(setentry, newsize);
261 if (newtable == NULL) {
262 PyErr_NoMemory();
263 return -1;
264 }
265 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 /* Make the set empty, using the new table. */
268 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800271 so->used = 0;
272 so->mask = newsize - 1;
273 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 /* Copy the data over; this is refcount-neutral for active entries;
276 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800277 if (oldfill == oldused) {
278 for (entry = oldtable; oldused > 0; entry++) {
279 if (entry->key != NULL) {
280 oldused--;
281 set_insert_clean(so, entry->key, entry->hash);
282 }
283 }
284 } else {
285 for (entry = oldtable; oldused > 0; entry++) {
286 if (entry->key != NULL && entry->key != dummy) {
287 oldused--;
288 set_insert_clean(so, entry->key, entry->hash);
289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 }
291 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 if (is_oldtable_malloced)
294 PyMem_DEL(oldtable);
295 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296}
297
Raymond Hettingerc991db22005-08-11 07:58:45 +0000298/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
299
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000300static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200301set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000302{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200303 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000304 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100305 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 assert(so->fill <= so->mask); /* at least one empty slot */
308 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000309 Py_INCREF(key);
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700310 if (set_insert_key(so, key, hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000311 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 return -1;
313 }
314 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
315 return 0;
316 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000317}
318
319static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200320set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000321{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800322 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200323 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200326 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 hash = PyObject_Hash(key);
328 if (hash == -1)
329 return -1;
330 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800331 entry.key = key;
332 entry.hash = hash;
333 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334}
335
336#define DISCARD_NOTFOUND 0
337#define DISCARD_FOUND 1
338
339static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000340set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800341{
342 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000344
Raymond Hettinger93035c42015-01-25 16:12:49 -0800345 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 if (entry == NULL)
347 return -1;
Yury Selivanov7aa53412015-05-30 10:57:56 -0400348 if (entry->key == NULL || entry->key == dummy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return DISCARD_NOTFOUND;
350 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800352 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 so->used--;
354 Py_DECREF(old_key);
355 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356}
357
358static int
359set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800361 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200362 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200367 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 hash = PyObject_Hash(key);
369 if (hash == -1)
370 return -1;
371 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800372 entry.key = key;
373 entry.hash = hash;
374 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000375}
376
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700377static void
378set_empty_to_minsize(PySetObject *so)
379{
380 memset(so->smalltable, 0, sizeof(so->smalltable));
381 so->fill = 0;
382 so->used = 0;
383 so->mask = PySet_MINSIZE - 1;
384 so->table = so->smalltable;
385 so->hash = -1;
386}
387
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000388static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000389set_clear_internal(PySetObject *so)
390{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800391 setentry *entry;
392 setentry *table = so->table;
393 Py_ssize_t fill = so->fill;
394 Py_ssize_t used = so->used;
395 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000397
Raymond Hettinger583cd032013-09-07 22:06:35 -0700398 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 /* This is delicate. During the process of clearing the set,
402 * decrefs can cause the set to mutate. To avoid fatal confusion
403 * (voice of experience), we have to make the set empty before
404 * clearing the slots, and never refer to anything via so->ref while
405 * clearing.
406 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700408 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 else if (fill > 0) {
411 /* It's a small table with something that needs to be cleared.
412 * Afraid the only safe way is to copy the set entries into
413 * another small table first.
414 */
415 memcpy(small_copy, table, sizeof(small_copy));
416 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700417 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 }
419 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 /* Now we can finally clear things. If C had refcounts, we could
422 * assert that the refcount on table is 1 now, i.e. that this function
423 * has unique access to it, so decref side-effects can't alter it.
424 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800425 for (entry = table; used > 0; entry++) {
426 if (entry->key && entry->key != dummy) {
427 used--;
428 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (table_is_malloced)
433 PyMem_DEL(table);
434 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000435}
436
437/*
438 * Iterate over a set table. Use like so:
439 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000440 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000441 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000442 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000443 * while (set_next(yourset, &pos, &entry)) {
444 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445 * }
446 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000447 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449 */
450static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 Py_ssize_t i;
454 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200455 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 assert (PyAnySet_Check(so));
458 i = *pos_ptr;
459 assert(i >= 0);
460 table = so->table;
461 mask = so->mask;
462 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
463 i++;
464 *pos_ptr = i+1;
465 if (i > mask)
466 return 0;
467 assert(table[i].key != NULL);
468 *entry_ptr = &table[i];
469 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470}
471
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000472static void
473set_dealloc(PySetObject *so)
474{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200475 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800476 Py_ssize_t used = so->used;
477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 PyObject_GC_UnTrack(so);
479 Py_TRASHCAN_SAFE_BEGIN(so)
480 if (so->weakreflist != NULL)
481 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000482
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800483 for (entry = so->table; used > 0; entry++) {
484 if (entry->key && entry->key != dummy) {
485 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700486 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 }
488 }
489 if (so->table != so->smalltable)
490 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700491 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000493}
494
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000495static PyObject *
496set_repr(PySetObject *so)
497{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200498 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (status != 0) {
502 if (status < 0)
503 return NULL;
504 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
505 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 /* shortcut for the empty set */
508 if (!so->used) {
509 Py_ReprLeave((PyObject*)so);
510 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
511 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 keys = PySequence_List((PyObject *)so);
514 if (keys == NULL)
515 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000516
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200517 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 listrepr = PyObject_Repr(keys);
519 Py_DECREF(keys);
520 if (listrepr == NULL)
521 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200522 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200524 if (tmp == NULL)
525 goto done;
526 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200527
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200528 if (Py_TYPE(so) != &PySet_Type)
529 result = PyUnicode_FromFormat("%s({%U})",
530 Py_TYPE(so)->tp_name,
531 listrepr);
532 else
533 result = PyUnicode_FromFormat("{%U}", listrepr);
534 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000535done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_ReprLeave((PyObject*)so);
537 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000538}
539
Martin v. Löwis18e16552006-02-15 17:27:45 +0000540static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000541set_len(PyObject *so)
542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000544}
545
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000546static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000547set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000550 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200551 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700552 setentry *so_entry;
553 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 assert (PyAnySet_Check(so));
556 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 other = (PySetObject*)otherset;
559 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700560 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 return 0;
562 /* Do one big resize at the start, rather than
563 * incrementally resizing as we insert new keys. Expect
564 * that there will be no (or few) overlapping keys.
565 */
566 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
567 if (set_table_resize(so, (so->used + other->used)*2) != 0)
568 return -1;
569 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700570 so_entry = so->table;
571 other_entry = other->table;
572
573 /* If our table is empty, and both tables have the same size, and
574 there are no dummies to eliminate, then just copy the pointers. */
575 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
576 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
577 key = other_entry->key;
578 if (key != NULL) {
579 assert(so_entry->key == NULL);
580 Py_INCREF(key);
581 so_entry->key = key;
582 so_entry->hash = other_entry->hash;
583 }
584 }
585 so->fill = other->fill;
586 so->used = other->used;
587 return 0;
588 }
589
590 /* If our table is empty, we can use set_insert_clean() */
591 if (so->fill == 0) {
592 for (i = 0; i <= other->mask; i++, other_entry++) {
593 key = other_entry->key;
594 if (key != NULL && key != dummy) {
595 Py_INCREF(key);
596 set_insert_clean(so, key, other_entry->hash);
597 }
598 }
599 return 0;
600 }
601
602 /* We can't assure there are no duplicates, so do normal insertions */
Raymond Hettingera3626bc2015-07-15 23:50:14 -0700603 for (i = 0; i <= other->mask; i++) {
604 other_entry = &other->table[i];
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700605 key = other_entry->key;
606 if (key != NULL && key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000607 Py_INCREF(key);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700608 if (set_insert_key(so, key, other_entry->hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000609 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 return -1;
611 }
612 }
613 }
614 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000615}
616
617static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000618set_contains_entry(PySetObject *so, setentry *entry)
619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 PyObject *key;
621 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000622
Raymond Hettinger93035c42015-01-25 16:12:49 -0800623 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 if (lu_entry == NULL)
625 return -1;
626 key = lu_entry->key;
Yury Selivanov7aa53412015-05-30 10:57:56 -0400627 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000628}
629
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800630static int
631set_contains_key(PySetObject *so, PyObject *key)
632{
633 setentry entry;
634 Py_hash_t hash;
635
636 if (!PyUnicode_CheckExact(key) ||
637 (hash = ((PyASCIIObject *) key)->hash) == -1) {
638 hash = PyObject_Hash(key);
639 if (hash == -1)
640 return -1;
641 }
642 entry.key = key;
643 entry.hash = hash;
644 return set_contains_entry(so, &entry);
645}
646
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000647static PyObject *
648set_pop(PySetObject *so)
649{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800650 /* Make sure the search finger is in bounds */
651 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200652 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 assert (PyAnySet_Check(so));
656 if (so->used == 0) {
657 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
658 return NULL;
659 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000660
Raymond Hettinger1202a472015-01-18 13:12:42 -0800661 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
662 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800663 if (i > so->mask)
664 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 }
666 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800668 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800670 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000672}
673
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000674PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
675Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000676
677static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000678set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 Py_ssize_t pos = 0;
681 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 while (set_next(so, &pos, &entry))
684 Py_VISIT(entry->key);
685 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000686}
687
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000688static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000689frozenset_hash(PyObject *self)
690{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800691 /* Most of the constants in this hash algorithm are randomly choosen
692 large primes with "interesting bit patterns" and that passed
693 tests for good collision statistics on a variety of problematic
694 datasets such as:
695
696 ps = []
697 for r in range(21):
698 ps += itertools.combinations(range(20), r)
699 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
700
701 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800703 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 setentry *entry;
705 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 if (so->hash != -1)
708 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000709
Mark Dickinson57e683e2011-09-24 18:18:40 +0100710 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 while (set_next(so, &pos, &entry)) {
712 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800713 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 combinations of a small number of elements with nearby
715 hashes so that many distinct combinations collapse to only
716 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000717 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800718 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800720 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500721 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800722 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200723 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800724 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 so->hash = hash;
726 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000727}
728
Raymond Hettingera9d99362005-08-05 00:01:15 +0000729/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000730
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000731typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 PyObject_HEAD
733 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
734 Py_ssize_t si_used;
735 Py_ssize_t si_pos;
736 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000737} setiterobject;
738
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000739static void
740setiter_dealloc(setiterobject *si)
741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 Py_XDECREF(si->si_set);
743 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000744}
745
746static int
747setiter_traverse(setiterobject *si, visitproc visit, void *arg)
748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 Py_VISIT(si->si_set);
750 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000751}
752
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000753static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000754setiter_len(setiterobject *si)
755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 Py_ssize_t len = 0;
757 if (si->si_set != NULL && si->si_used == si->si_set->used)
758 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000759 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000760}
761
Armin Rigof5b3e362006-02-11 21:32:43 +0000762PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000763
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000764static PyObject *setiter_iternext(setiterobject *si);
765
766static PyObject *
767setiter_reduce(setiterobject *si)
768{
769 PyObject *list;
770 setiterobject tmp;
771
772 list = PyList_New(0);
773 if (!list)
774 return NULL;
775
Ezio Melotti0e1af282012-09-28 16:43:40 +0300776 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000777 tmp = *si;
778 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300779
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000780 /* iterate the temporary into a list */
781 for(;;) {
782 PyObject *element = setiter_iternext(&tmp);
783 if (element) {
784 if (PyList_Append(list, element)) {
785 Py_DECREF(element);
786 Py_DECREF(list);
787 Py_XDECREF(tmp.si_set);
788 return NULL;
789 }
790 Py_DECREF(element);
791 } else
792 break;
793 }
794 Py_XDECREF(tmp.si_set);
795 /* check for error */
796 if (tmp.si_set != NULL) {
797 /* we have an error */
798 Py_DECREF(list);
799 return NULL;
800 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200801 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000802}
803
804PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
805
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000806static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000808 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000810};
811
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000812static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200815 Py_ssize_t i, mask;
816 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 if (so == NULL)
820 return NULL;
821 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 if (si->si_used != so->used) {
824 PyErr_SetString(PyExc_RuntimeError,
825 "Set changed size during iteration");
826 si->si_used = -1; /* Make this state sticky */
827 return NULL;
828 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 i = si->si_pos;
831 assert(i>=0);
832 entry = so->table;
833 mask = so->mask;
834 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
835 i++;
836 si->si_pos = i+1;
837 if (i > mask)
838 goto fail;
839 si->len--;
840 key = entry[i].key;
841 Py_INCREF(key);
842 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000843
844fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 Py_DECREF(so);
846 si->si_set = NULL;
847 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000848}
849
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000850PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 PyVarObject_HEAD_INIT(&PyType_Type, 0)
852 "set_iterator", /* tp_name */
853 sizeof(setiterobject), /* tp_basicsize */
854 0, /* tp_itemsize */
855 /* methods */
856 (destructor)setiter_dealloc, /* tp_dealloc */
857 0, /* tp_print */
858 0, /* tp_getattr */
859 0, /* tp_setattr */
860 0, /* tp_reserved */
861 0, /* tp_repr */
862 0, /* tp_as_number */
863 0, /* tp_as_sequence */
864 0, /* tp_as_mapping */
865 0, /* tp_hash */
866 0, /* tp_call */
867 0, /* tp_str */
868 PyObject_GenericGetAttr, /* tp_getattro */
869 0, /* tp_setattro */
870 0, /* tp_as_buffer */
871 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
872 0, /* tp_doc */
873 (traverseproc)setiter_traverse, /* tp_traverse */
874 0, /* tp_clear */
875 0, /* tp_richcompare */
876 0, /* tp_weaklistoffset */
877 PyObject_SelfIter, /* tp_iter */
878 (iternextfunc)setiter_iternext, /* tp_iternext */
879 setiter_methods, /* tp_methods */
880 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000881};
882
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000883static PyObject *
884set_iter(PySetObject *so)
885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
887 if (si == NULL)
888 return NULL;
889 Py_INCREF(so);
890 si->si_set = so;
891 si->si_used = so->used;
892 si->si_pos = 0;
893 si->len = so->used;
894 _PyObject_GC_TRACK(si);
895 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000896}
897
Raymond Hettingerd7946662005-08-01 21:39:29 +0000898static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000899set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (PyAnySet_Check(other))
904 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 if (PyDict_CheckExact(other)) {
907 PyObject *value;
908 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000909 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 /* Do one big resize at the start, rather than
913 * incrementally resizing as we insert new keys. Expect
914 * that there will be no (or few) overlapping keys.
915 */
916 if (dictsize == -1)
917 return -1;
918 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
919 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
920 return -1;
921 }
922 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
923 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 an_entry.hash = hash;
926 an_entry.key = key;
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700927 if (set_add_entry(so, &an_entry))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 return -1;
929 }
930 return 0;
931 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 it = PyObject_GetIter(other);
934 if (it == NULL)
935 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700938 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 Py_DECREF(it);
940 Py_DECREF(key);
941 return -1;
942 }
943 Py_DECREF(key);
944 }
945 Py_DECREF(it);
946 if (PyErr_Occurred())
947 return -1;
948 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000949}
950
951static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000952set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
957 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700958 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 return NULL;
960 }
961 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000962}
963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000965"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000966
Raymond Hettinger426d9952014-05-18 21:40:20 +0100967/* XXX Todo:
968 If aligned memory allocations become available, make the
969 set object 64 byte aligned so that most of the fields
970 can be retrieved or updated in a single cache line.
971*/
972
Raymond Hettingera38123e2003-11-24 22:18:49 +0000973static PyObject *
974make_new_set(PyTypeObject *type, PyObject *iterable)
975{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200976 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700979 so = (PySetObject *)type->tp_alloc(type, 0);
980 if (so == NULL)
981 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000982
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700983 so->fill = 0;
984 so->used = 0;
985 so->mask = PySet_MINSIZE - 1;
986 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700987 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800988 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700992 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 Py_DECREF(so);
994 return NULL;
995 }
996 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +0000999}
1000
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001001static PyObject *
1002make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1005 if (PyType_IsSubtype(type, &PySet_Type))
1006 type = &PySet_Type;
1007 else
1008 type = &PyFrozenSet_Type;
1009 }
1010 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001011}
1012
Raymond Hettingerd7946662005-08-01 21:39:29 +00001013/* The empty frozenset is a singleton */
1014static PyObject *emptyfrozenset = NULL;
1015
Raymond Hettingera690a992003-11-16 16:17:49 +00001016static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001017frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1022 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1025 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 if (type != &PyFrozenSet_Type)
1028 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (iterable != NULL) {
1031 /* frozenset(f) is idempotent */
1032 if (PyFrozenSet_CheckExact(iterable)) {
1033 Py_INCREF(iterable);
1034 return iterable;
1035 }
1036 result = make_new_set(type, iterable);
1037 if (result == NULL || PySet_GET_SIZE(result))
1038 return result;
1039 Py_DECREF(result);
1040 }
1041 /* The empty frozenset is a singleton */
1042 if (emptyfrozenset == NULL)
1043 emptyfrozenset = make_new_set(type, NULL);
1044 Py_XINCREF(emptyfrozenset);
1045 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001046}
1047
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001048int
1049PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001050{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001051 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001052}
1053
1054void
1055PySet_Fini(void)
1056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001058}
1059
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001060static PyObject *
1061set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1064 return NULL;
1065
1066 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001067}
1068
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001069/* set_swap_bodies() switches the contents of any two sets by moving their
1070 internal data pointers and, if needed, copying the internal smalltables.
1071 Semantically equivalent to:
1072
1073 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1074
1075 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001076 Useful for operations that update in-place (by allowing an intermediate
1077 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001078*/
1079
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001080static void
1081set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 Py_ssize_t t;
1084 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001086 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 t = a->fill; a->fill = b->fill; b->fill = t;
1089 t = a->used; a->used = b->used; b->used = t;
1090 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 u = a->table;
1093 if (a->table == a->smalltable)
1094 u = b->smalltable;
1095 a->table = b->table;
1096 if (b->table == b->smalltable)
1097 a->table = a->smalltable;
1098 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (a->table == a->smalltable || b->table == b->smalltable) {
1101 memcpy(tab, a->smalltable, sizeof(tab));
1102 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1103 memcpy(b->smalltable, tab, sizeof(tab));
1104 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1107 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1108 h = a->hash; a->hash = b->hash; b->hash = h;
1109 } else {
1110 a->hash = -1;
1111 b->hash = -1;
1112 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001113}
1114
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001115static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001116set_copy(PySetObject *so)
1117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001119}
1120
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001121static PyObject *
1122frozenset_copy(PySetObject *so)
1123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (PyFrozenSet_CheckExact(so)) {
1125 Py_INCREF(so);
1126 return (PyObject *)so;
1127 }
1128 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001129}
1130
Raymond Hettingera690a992003-11-16 16:17:49 +00001131PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1132
1133static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001134set_clear(PySetObject *so)
1135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 set_clear_internal(so);
1137 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001138}
1139
1140PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1141
1142static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001143set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 PySetObject *result;
1146 PyObject *other;
1147 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 result = (PySetObject *)set_copy(so);
1150 if (result == NULL)
1151 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1154 other = PyTuple_GET_ITEM(args, i);
1155 if ((PyObject *)so == other)
1156 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001157 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 Py_DECREF(result);
1159 return NULL;
1160 }
1161 }
1162 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001163}
1164
1165PyDoc_STRVAR(union_doc,
1166 "Return the union of sets as a new set.\n\
1167\n\
1168(i.e. all elements that are in either set.)");
1169
1170static PyObject *
1171set_or(PySetObject *so, PyObject *other)
1172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001174
Brian Curtindfc80e32011-08-10 20:28:54 -05001175 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1176 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 result = (PySetObject *)set_copy(so);
1179 if (result == NULL)
1180 return NULL;
1181 if ((PyObject *)so == other)
1182 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001183 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 Py_DECREF(result);
1185 return NULL;
1186 }
1187 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001188}
1189
Raymond Hettingera690a992003-11-16 16:17:49 +00001190static PyObject *
1191set_ior(PySetObject *so, PyObject *other)
1192{
Brian Curtindfc80e32011-08-10 20:28:54 -05001193 if (!PyAnySet_Check(other))
1194 Py_RETURN_NOTIMPLEMENTED;
1195
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001196 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 return NULL;
1198 Py_INCREF(so);
1199 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001200}
1201
1202static PyObject *
1203set_intersection(PySetObject *so, PyObject *other)
1204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 PySetObject *result;
1206 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if ((PyObject *)so == other)
1209 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1212 if (result == NULL)
1213 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (PyAnySet_Check(other)) {
1216 Py_ssize_t pos = 0;
1217 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1220 tmp = (PyObject *)so;
1221 so = (PySetObject *)other;
1222 other = tmp;
1223 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 while (set_next((PySetObject *)other, &pos, &entry)) {
1226 int rv = set_contains_entry(so, entry);
1227 if (rv == -1) {
1228 Py_DECREF(result);
1229 return NULL;
1230 }
1231 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001232 if (set_add_entry(result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 Py_DECREF(result);
1234 return NULL;
1235 }
1236 }
1237 }
1238 return (PyObject *)result;
1239 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 it = PyObject_GetIter(other);
1242 if (it == NULL) {
1243 Py_DECREF(result);
1244 return NULL;
1245 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 while ((key = PyIter_Next(it)) != NULL) {
1248 int rv;
1249 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001250 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (hash == -1) {
1253 Py_DECREF(it);
1254 Py_DECREF(result);
1255 Py_DECREF(key);
1256 return NULL;
1257 }
1258 entry.hash = hash;
1259 entry.key = key;
1260 rv = set_contains_entry(so, &entry);
1261 if (rv == -1) {
1262 Py_DECREF(it);
1263 Py_DECREF(result);
1264 Py_DECREF(key);
1265 return NULL;
1266 }
1267 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001268 if (set_add_entry(result, &entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 Py_DECREF(it);
1270 Py_DECREF(result);
1271 Py_DECREF(key);
1272 return NULL;
1273 }
1274 }
1275 Py_DECREF(key);
1276 }
1277 Py_DECREF(it);
1278 if (PyErr_Occurred()) {
1279 Py_DECREF(result);
1280 return NULL;
1281 }
1282 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001283}
1284
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001285static PyObject *
1286set_intersection_multi(PySetObject *so, PyObject *args)
1287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 Py_ssize_t i;
1289 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (PyTuple_GET_SIZE(args) == 0)
1292 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 Py_INCREF(so);
1295 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1296 PyObject *other = PyTuple_GET_ITEM(args, i);
1297 PyObject *newresult = set_intersection((PySetObject *)result, other);
1298 if (newresult == NULL) {
1299 Py_DECREF(result);
1300 return NULL;
1301 }
1302 Py_DECREF(result);
1303 result = newresult;
1304 }
1305 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001306}
1307
Raymond Hettingera690a992003-11-16 16:17:49 +00001308PyDoc_STRVAR(intersection_doc,
1309"Return the intersection of two sets as a new set.\n\
1310\n\
1311(i.e. all elements that are in both sets.)");
1312
1313static PyObject *
1314set_intersection_update(PySetObject *so, PyObject *other)
1315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 tmp = set_intersection(so, other);
1319 if (tmp == NULL)
1320 return NULL;
1321 set_swap_bodies(so, (PySetObject *)tmp);
1322 Py_DECREF(tmp);
1323 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001324}
1325
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001326static PyObject *
1327set_intersection_update_multi(PySetObject *so, PyObject *args)
1328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 tmp = set_intersection_multi(so, args);
1332 if (tmp == NULL)
1333 return NULL;
1334 set_swap_bodies(so, (PySetObject *)tmp);
1335 Py_DECREF(tmp);
1336 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001337}
1338
Raymond Hettingera690a992003-11-16 16:17:49 +00001339PyDoc_STRVAR(intersection_update_doc,
1340"Update a set with the intersection of itself and another.");
1341
1342static PyObject *
1343set_and(PySetObject *so, PyObject *other)
1344{
Brian Curtindfc80e32011-08-10 20:28:54 -05001345 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1346 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001348}
1349
1350static PyObject *
1351set_iand(PySetObject *so, PyObject *other)
1352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001354
Brian Curtindfc80e32011-08-10 20:28:54 -05001355 if (!PyAnySet_Check(other))
1356 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 result = set_intersection_update(so, other);
1358 if (result == NULL)
1359 return NULL;
1360 Py_DECREF(result);
1361 Py_INCREF(so);
1362 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001363}
1364
Guido van Rossum58da9312007-11-10 23:39:45 +00001365static PyObject *
1366set_isdisjoint(PySetObject *so, PyObject *other)
1367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if ((PyObject *)so == other) {
1371 if (PySet_GET_SIZE(so) == 0)
1372 Py_RETURN_TRUE;
1373 else
1374 Py_RETURN_FALSE;
1375 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (PyAnySet_CheckExact(other)) {
1378 Py_ssize_t pos = 0;
1379 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1382 tmp = (PyObject *)so;
1383 so = (PySetObject *)other;
1384 other = tmp;
1385 }
1386 while (set_next((PySetObject *)other, &pos, &entry)) {
1387 int rv = set_contains_entry(so, entry);
1388 if (rv == -1)
1389 return NULL;
1390 if (rv)
1391 Py_RETURN_FALSE;
1392 }
1393 Py_RETURN_TRUE;
1394 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 it = PyObject_GetIter(other);
1397 if (it == NULL)
1398 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 while ((key = PyIter_Next(it)) != NULL) {
1401 int rv;
1402 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001403 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 if (hash == -1) {
1406 Py_DECREF(key);
1407 Py_DECREF(it);
1408 return NULL;
1409 }
1410 entry.hash = hash;
1411 entry.key = key;
1412 rv = set_contains_entry(so, &entry);
1413 Py_DECREF(key);
1414 if (rv == -1) {
1415 Py_DECREF(it);
1416 return NULL;
1417 }
1418 if (rv) {
1419 Py_DECREF(it);
1420 Py_RETURN_FALSE;
1421 }
1422 }
1423 Py_DECREF(it);
1424 if (PyErr_Occurred())
1425 return NULL;
1426 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001427}
1428
1429PyDoc_STRVAR(isdisjoint_doc,
1430"Return True if two sets have a null intersection.");
1431
Neal Norwitz6576bd82005-11-13 18:41:28 +00001432static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001433set_difference_update_internal(PySetObject *so, PyObject *other)
1434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if ((PyObject *)so == other)
1436 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (PyAnySet_Check(other)) {
1439 setentry *entry;
1440 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 while (set_next((PySetObject *)other, &pos, &entry))
1443 if (set_discard_entry(so, entry) == -1)
1444 return -1;
1445 } else {
1446 PyObject *key, *it;
1447 it = PyObject_GetIter(other);
1448 if (it == NULL)
1449 return -1;
1450
1451 while ((key = PyIter_Next(it)) != NULL) {
1452 if (set_discard_key(so, key) == -1) {
1453 Py_DECREF(it);
1454 Py_DECREF(key);
1455 return -1;
1456 }
1457 Py_DECREF(key);
1458 }
1459 Py_DECREF(it);
1460 if (PyErr_Occurred())
1461 return -1;
1462 }
1463 /* If more than 1/5 are dummies, then resize them away. */
1464 if ((so->fill - so->used) * 5 < so->mask)
1465 return 0;
1466 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001467}
1468
Raymond Hettingera690a992003-11-16 16:17:49 +00001469static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001470set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1475 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001476 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 return NULL;
1478 }
1479 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001480}
1481
1482PyDoc_STRVAR(difference_update_doc,
1483"Remove all elements of another set from this set.");
1484
1485static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001486set_copy_and_difference(PySetObject *so, PyObject *other)
1487{
1488 PyObject *result;
1489
1490 result = set_copy(so);
1491 if (result == NULL)
1492 return NULL;
1493 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1494 return result;
1495 Py_DECREF(result);
1496 return NULL;
1497}
1498
1499static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001500set_difference(PySetObject *so, PyObject *other)
1501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 PyObject *result;
1503 setentry *entry;
1504 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001507 return set_copy_and_difference(so, other);
1508 }
1509
1510 /* If len(so) much more than len(other), it's more efficient to simply copy
1511 * so and then iterate other looking for common elements. */
1512 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1513 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 result = make_new_set_basetype(Py_TYPE(so), NULL);
1517 if (result == NULL)
1518 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (PyDict_CheckExact(other)) {
1521 while (set_next(so, &pos, &entry)) {
1522 setentry entrycopy;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001523 int rv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 entrycopy.hash = entry->hash;
1525 entrycopy.key = entry->key;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001526 rv = _PyDict_Contains(other, entry->key, entry->hash);
1527 if (rv < 0) {
1528 Py_DECREF(result);
1529 return NULL;
1530 }
1531 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001532 if (set_add_entry((PySetObject *)result, &entrycopy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 Py_DECREF(result);
1534 return NULL;
1535 }
1536 }
1537 }
1538 return result;
1539 }
1540
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001541 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 while (set_next(so, &pos, &entry)) {
1543 int rv = set_contains_entry((PySetObject *)other, entry);
1544 if (rv == -1) {
1545 Py_DECREF(result);
1546 return NULL;
1547 }
1548 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001549 if (set_add_entry((PySetObject *)result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 Py_DECREF(result);
1551 return NULL;
1552 }
1553 }
1554 }
1555 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001556}
1557
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001558static PyObject *
1559set_difference_multi(PySetObject *so, PyObject *args)
1560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 Py_ssize_t i;
1562 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (PyTuple_GET_SIZE(args) == 0)
1565 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 other = PyTuple_GET_ITEM(args, 0);
1568 result = set_difference(so, other);
1569 if (result == NULL)
1570 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1573 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001574 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 Py_DECREF(result);
1576 return NULL;
1577 }
1578 }
1579 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001580}
1581
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001582PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001583"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001584\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001585(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001586static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001587set_sub(PySetObject *so, PyObject *other)
1588{
Brian Curtindfc80e32011-08-10 20:28:54 -05001589 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1590 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001592}
1593
1594static PyObject *
1595set_isub(PySetObject *so, PyObject *other)
1596{
Brian Curtindfc80e32011-08-10 20:28:54 -05001597 if (!PyAnySet_Check(other))
1598 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001599 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 return NULL;
1601 Py_INCREF(so);
1602 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001603}
1604
1605static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001606set_symmetric_difference_update(PySetObject *so, PyObject *other)
1607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 PySetObject *otherset;
1609 PyObject *key;
1610 Py_ssize_t pos = 0;
1611 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if ((PyObject *)so == other)
1614 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 if (PyDict_CheckExact(other)) {
1617 PyObject *value;
1618 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001619 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1621 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001622
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001623 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 an_entry.hash = hash;
1625 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001628 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001629 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001632 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001633 if (set_add_entry(so, &an_entry)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001634 Py_DECREF(key);
1635 return NULL;
1636 }
1637 }
Georg Brandl2d444492010-09-03 10:52:55 +00001638 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 }
1640 Py_RETURN_NONE;
1641 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 if (PyAnySet_Check(other)) {
1644 Py_INCREF(other);
1645 otherset = (PySetObject *)other;
1646 } else {
1647 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1648 if (otherset == NULL)
1649 return NULL;
1650 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 while (set_next(otherset, &pos, &entry)) {
1653 int rv = set_discard_entry(so, entry);
1654 if (rv == -1) {
1655 Py_DECREF(otherset);
1656 return NULL;
1657 }
1658 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001659 if (set_add_entry(so, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 Py_DECREF(otherset);
1661 return NULL;
1662 }
1663 }
1664 }
1665 Py_DECREF(otherset);
1666 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001667}
1668
1669PyDoc_STRVAR(symmetric_difference_update_doc,
1670"Update a set with the symmetric difference of itself and another.");
1671
1672static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001673set_symmetric_difference(PySetObject *so, PyObject *other)
1674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 PyObject *rv;
1676 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1679 if (otherset == NULL)
1680 return NULL;
1681 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1682 if (rv == NULL)
1683 return NULL;
1684 Py_DECREF(rv);
1685 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001686}
1687
1688PyDoc_STRVAR(symmetric_difference_doc,
1689"Return the symmetric difference of two sets as a new set.\n\
1690\n\
1691(i.e. all elements that are in exactly one of the sets.)");
1692
1693static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001694set_xor(PySetObject *so, PyObject *other)
1695{
Brian Curtindfc80e32011-08-10 20:28:54 -05001696 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1697 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001699}
1700
1701static PyObject *
1702set_ixor(PySetObject *so, PyObject *other)
1703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001705
Brian Curtindfc80e32011-08-10 20:28:54 -05001706 if (!PyAnySet_Check(other))
1707 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 result = set_symmetric_difference_update(so, other);
1709 if (result == NULL)
1710 return NULL;
1711 Py_DECREF(result);
1712 Py_INCREF(so);
1713 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001714}
1715
1716static PyObject *
1717set_issubset(PySetObject *so, PyObject *other)
1718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 setentry *entry;
1720 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 if (!PyAnySet_Check(other)) {
1723 PyObject *tmp, *result;
1724 tmp = make_new_set(&PySet_Type, other);
1725 if (tmp == NULL)
1726 return NULL;
1727 result = set_issubset(so, tmp);
1728 Py_DECREF(tmp);
1729 return result;
1730 }
1731 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1732 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 while (set_next(so, &pos, &entry)) {
1735 int rv = set_contains_entry((PySetObject *)other, entry);
1736 if (rv == -1)
1737 return NULL;
1738 if (!rv)
1739 Py_RETURN_FALSE;
1740 }
1741 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001742}
1743
1744PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1745
1746static PyObject *
1747set_issuperset(PySetObject *so, PyObject *other)
1748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 if (!PyAnySet_Check(other)) {
1752 tmp = make_new_set(&PySet_Type, other);
1753 if (tmp == NULL)
1754 return NULL;
1755 result = set_issuperset(so, tmp);
1756 Py_DECREF(tmp);
1757 return result;
1758 }
1759 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001760}
1761
1762PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1763
Raymond Hettingera690a992003-11-16 16:17:49 +00001764static PyObject *
1765set_richcompare(PySetObject *v, PyObject *w, int op)
1766{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001767 PyObject *r1;
1768 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001769
Brian Curtindfc80e32011-08-10 20:28:54 -05001770 if(!PyAnySet_Check(w))
1771 Py_RETURN_NOTIMPLEMENTED;
1772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 switch (op) {
1774 case Py_EQ:
1775 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1776 Py_RETURN_FALSE;
1777 if (v->hash != -1 &&
1778 ((PySetObject *)w)->hash != -1 &&
1779 v->hash != ((PySetObject *)w)->hash)
1780 Py_RETURN_FALSE;
1781 return set_issubset(v, w);
1782 case Py_NE:
1783 r1 = set_richcompare(v, w, Py_EQ);
1784 if (r1 == NULL)
1785 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001786 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001788 if (r2 < 0)
1789 return NULL;
1790 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 case Py_LE:
1792 return set_issubset(v, w);
1793 case Py_GE:
1794 return set_issuperset(v, w);
1795 case Py_LT:
1796 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1797 Py_RETURN_FALSE;
1798 return set_issubset(v, w);
1799 case Py_GT:
1800 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1801 Py_RETURN_FALSE;
1802 return set_issuperset(v, w);
1803 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001804 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001805}
1806
Raymond Hettingera690a992003-11-16 16:17:49 +00001807static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001808set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001809{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001810 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 return NULL;
1812 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001813}
1814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001816"Add an element to a set.\n\
1817\n\
1818This has no effect if the element is already present.");
1819
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001820static int
1821set_contains(PySetObject *so, PyObject *key)
1822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 PyObject *tmpkey;
1824 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 rv = set_contains_key(so, key);
1827 if (rv == -1) {
1828 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1829 return -1;
1830 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001831 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 if (tmpkey == NULL)
1833 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001834 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 Py_DECREF(tmpkey);
1836 }
1837 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001838}
1839
1840static PyObject *
1841set_direct_contains(PySetObject *so, PyObject *key)
1842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 result = set_contains(so, key);
1846 if (result == -1)
1847 return NULL;
1848 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001849}
1850
1851PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1852
Raymond Hettingera690a992003-11-16 16:17:49 +00001853static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001854set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 PyObject *tmpkey;
1857 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 rv = set_discard_key(so, key);
1860 if (rv == -1) {
1861 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1862 return NULL;
1863 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001864 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 if (tmpkey == NULL)
1866 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 Py_DECREF(tmpkey);
1869 if (rv == -1)
1870 return NULL;
1871 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001874 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 return NULL;
1876 }
1877 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001878}
1879
1880PyDoc_STRVAR(remove_doc,
1881"Remove an element from a set; it must be a member.\n\
1882\n\
1883If the element is not a member, raise a KeyError.");
1884
1885static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001886set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001887{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001888 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 rv = set_discard_key(so, key);
1892 if (rv == -1) {
1893 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1894 return NULL;
1895 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001896 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 if (tmpkey == NULL)
1898 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001899 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001901 if (rv == -1)
1902 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 }
1904 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001905}
1906
1907PyDoc_STRVAR(discard_doc,
1908"Remove an element from a set if it is a member.\n\
1909\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001911
1912static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001913set_reduce(PySetObject *so)
1914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001916 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 keys = PySequence_List((PyObject *)so);
1919 if (keys == NULL)
1920 goto done;
1921 args = PyTuple_Pack(1, keys);
1922 if (args == NULL)
1923 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001924 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 if (dict == NULL) {
1926 PyErr_Clear();
1927 dict = Py_None;
1928 Py_INCREF(dict);
1929 }
1930 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001931done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 Py_XDECREF(args);
1933 Py_XDECREF(keys);
1934 Py_XDECREF(dict);
1935 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001936}
1937
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001938static PyObject *
1939set_sizeof(PySetObject *so)
1940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 res = sizeof(PySetObject);
1944 if (so->table != so->smalltable)
1945 res = res + (so->mask + 1) * sizeof(setentry);
1946 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001947}
1948
1949PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001950static int
1951set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (!PyAnySet_Check(self))
1956 return -1;
1957 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1958 return -1;
1959 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1960 return -1;
1961 set_clear_internal(self);
1962 self->hash = -1;
1963 if (iterable == NULL)
1964 return 0;
1965 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001966}
1967
Raymond Hettingera690a992003-11-16 16:17:49 +00001968static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 set_len, /* sq_length */
1970 0, /* sq_concat */
1971 0, /* sq_repeat */
1972 0, /* sq_item */
1973 0, /* sq_slice */
1974 0, /* sq_ass_item */
1975 0, /* sq_ass_slice */
1976 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001977};
1978
1979/* set object ********************************************************/
1980
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001981#ifdef Py_DEBUG
1982static PyObject *test_c_api(PySetObject *so);
1983
1984PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1985All is well if assertions don't fail.");
1986#endif
1987
Raymond Hettingera690a992003-11-16 16:17:49 +00001988static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 {"add", (PyCFunction)set_add, METH_O,
1990 add_doc},
1991 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1992 clear_doc},
1993 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1994 contains_doc},
1995 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1996 copy_doc},
1997 {"discard", (PyCFunction)set_discard, METH_O,
1998 discard_doc},
1999 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2000 difference_doc},
2001 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2002 difference_update_doc},
2003 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2004 intersection_doc},
2005 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2006 intersection_update_doc},
2007 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2008 isdisjoint_doc},
2009 {"issubset", (PyCFunction)set_issubset, METH_O,
2010 issubset_doc},
2011 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2012 issuperset_doc},
2013 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2014 pop_doc},
2015 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2016 reduce_doc},
2017 {"remove", (PyCFunction)set_remove, METH_O,
2018 remove_doc},
2019 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2020 sizeof_doc},
2021 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2022 symmetric_difference_doc},
2023 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2024 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002025#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2027 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002028#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 {"union", (PyCFunction)set_union, METH_VARARGS,
2030 union_doc},
2031 {"update", (PyCFunction)set_update, METH_VARARGS,
2032 update_doc},
2033 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002034};
2035
2036static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 0, /*nb_add*/
2038 (binaryfunc)set_sub, /*nb_subtract*/
2039 0, /*nb_multiply*/
2040 0, /*nb_remainder*/
2041 0, /*nb_divmod*/
2042 0, /*nb_power*/
2043 0, /*nb_negative*/
2044 0, /*nb_positive*/
2045 0, /*nb_absolute*/
2046 0, /*nb_bool*/
2047 0, /*nb_invert*/
2048 0, /*nb_lshift*/
2049 0, /*nb_rshift*/
2050 (binaryfunc)set_and, /*nb_and*/
2051 (binaryfunc)set_xor, /*nb_xor*/
2052 (binaryfunc)set_or, /*nb_or*/
2053 0, /*nb_int*/
2054 0, /*nb_reserved*/
2055 0, /*nb_float*/
2056 0, /*nb_inplace_add*/
2057 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2058 0, /*nb_inplace_multiply*/
2059 0, /*nb_inplace_remainder*/
2060 0, /*nb_inplace_power*/
2061 0, /*nb_inplace_lshift*/
2062 0, /*nb_inplace_rshift*/
2063 (binaryfunc)set_iand, /*nb_inplace_and*/
2064 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2065 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002066};
2067
2068PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002069"set() -> new empty set object\n\
2070set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002071\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002072Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002073
2074PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2076 "set", /* tp_name */
2077 sizeof(PySetObject), /* tp_basicsize */
2078 0, /* tp_itemsize */
2079 /* methods */
2080 (destructor)set_dealloc, /* tp_dealloc */
2081 0, /* tp_print */
2082 0, /* tp_getattr */
2083 0, /* tp_setattr */
2084 0, /* tp_reserved */
2085 (reprfunc)set_repr, /* tp_repr */
2086 &set_as_number, /* tp_as_number */
2087 &set_as_sequence, /* tp_as_sequence */
2088 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002089 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 0, /* tp_call */
2091 0, /* tp_str */
2092 PyObject_GenericGetAttr, /* tp_getattro */
2093 0, /* tp_setattro */
2094 0, /* tp_as_buffer */
2095 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2096 Py_TPFLAGS_BASETYPE, /* tp_flags */
2097 set_doc, /* tp_doc */
2098 (traverseproc)set_traverse, /* tp_traverse */
2099 (inquiry)set_clear_internal, /* tp_clear */
2100 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002101 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2102 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 0, /* tp_iternext */
2104 set_methods, /* tp_methods */
2105 0, /* tp_members */
2106 0, /* tp_getset */
2107 0, /* tp_base */
2108 0, /* tp_dict */
2109 0, /* tp_descr_get */
2110 0, /* tp_descr_set */
2111 0, /* tp_dictoffset */
2112 (initproc)set_init, /* tp_init */
2113 PyType_GenericAlloc, /* tp_alloc */
2114 set_new, /* tp_new */
2115 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002116};
2117
2118/* frozenset object ********************************************************/
2119
2120
2121static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2123 contains_doc},
2124 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2125 copy_doc},
2126 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2127 difference_doc},
2128 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2129 intersection_doc},
2130 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2131 isdisjoint_doc},
2132 {"issubset", (PyCFunction)set_issubset, METH_O,
2133 issubset_doc},
2134 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2135 issuperset_doc},
2136 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2137 reduce_doc},
2138 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2139 sizeof_doc},
2140 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2141 symmetric_difference_doc},
2142 {"union", (PyCFunction)set_union, METH_VARARGS,
2143 union_doc},
2144 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002145};
2146
2147static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 0, /*nb_add*/
2149 (binaryfunc)set_sub, /*nb_subtract*/
2150 0, /*nb_multiply*/
2151 0, /*nb_remainder*/
2152 0, /*nb_divmod*/
2153 0, /*nb_power*/
2154 0, /*nb_negative*/
2155 0, /*nb_positive*/
2156 0, /*nb_absolute*/
2157 0, /*nb_bool*/
2158 0, /*nb_invert*/
2159 0, /*nb_lshift*/
2160 0, /*nb_rshift*/
2161 (binaryfunc)set_and, /*nb_and*/
2162 (binaryfunc)set_xor, /*nb_xor*/
2163 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002164};
2165
2166PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002167"frozenset() -> empty frozenset object\n\
2168frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002169\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002170Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002171
2172PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2174 "frozenset", /* tp_name */
2175 sizeof(PySetObject), /* tp_basicsize */
2176 0, /* tp_itemsize */
2177 /* methods */
2178 (destructor)set_dealloc, /* tp_dealloc */
2179 0, /* tp_print */
2180 0, /* tp_getattr */
2181 0, /* tp_setattr */
2182 0, /* tp_reserved */
2183 (reprfunc)set_repr, /* tp_repr */
2184 &frozenset_as_number, /* tp_as_number */
2185 &set_as_sequence, /* tp_as_sequence */
2186 0, /* tp_as_mapping */
2187 frozenset_hash, /* tp_hash */
2188 0, /* tp_call */
2189 0, /* tp_str */
2190 PyObject_GenericGetAttr, /* tp_getattro */
2191 0, /* tp_setattro */
2192 0, /* tp_as_buffer */
2193 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2194 Py_TPFLAGS_BASETYPE, /* tp_flags */
2195 frozenset_doc, /* tp_doc */
2196 (traverseproc)set_traverse, /* tp_traverse */
2197 (inquiry)set_clear_internal, /* tp_clear */
2198 (richcmpfunc)set_richcompare, /* tp_richcompare */
2199 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2200 (getiterfunc)set_iter, /* tp_iter */
2201 0, /* tp_iternext */
2202 frozenset_methods, /* tp_methods */
2203 0, /* tp_members */
2204 0, /* tp_getset */
2205 0, /* tp_base */
2206 0, /* tp_dict */
2207 0, /* tp_descr_get */
2208 0, /* tp_descr_set */
2209 0, /* tp_dictoffset */
2210 0, /* tp_init */
2211 PyType_GenericAlloc, /* tp_alloc */
2212 frozenset_new, /* tp_new */
2213 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002214};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002215
2216
2217/***** C API functions *************************************************/
2218
2219PyObject *
2220PySet_New(PyObject *iterable)
2221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002223}
2224
2225PyObject *
2226PyFrozenSet_New(PyObject *iterable)
2227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002229}
2230
Neal Norwitz8c49c822006-03-04 18:41:19 +00002231Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002232PySet_Size(PyObject *anyset)
2233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 if (!PyAnySet_Check(anyset)) {
2235 PyErr_BadInternalCall();
2236 return -1;
2237 }
2238 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002239}
2240
2241int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002242PySet_Clear(PyObject *set)
2243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 if (!PySet_Check(set)) {
2245 PyErr_BadInternalCall();
2246 return -1;
2247 }
2248 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002249}
2250
2251int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002252PySet_Contains(PyObject *anyset, PyObject *key)
2253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 if (!PyAnySet_Check(anyset)) {
2255 PyErr_BadInternalCall();
2256 return -1;
2257 }
2258 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002259}
2260
2261int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002262PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 if (!PySet_Check(set)) {
2265 PyErr_BadInternalCall();
2266 return -1;
2267 }
2268 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002269}
2270
2271int
Christian Heimesfd66e512008-01-29 12:18:50 +00002272PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 if (!PySet_Check(anyset) &&
2275 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2276 PyErr_BadInternalCall();
2277 return -1;
2278 }
2279 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002280}
2281
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002282int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002283_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 if (!PyAnySet_Check(set)) {
2288 PyErr_BadInternalCall();
2289 return -1;
2290 }
2291 if (set_next((PySetObject *)set, pos, &entry) == 0)
2292 return 0;
2293 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002294 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002296}
2297
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002298PyObject *
2299PySet_Pop(PyObject *set)
2300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (!PySet_Check(set)) {
2302 PyErr_BadInternalCall();
2303 return NULL;
2304 }
2305 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002306}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002307
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002308int
2309_PySet_Update(PyObject *set, PyObject *iterable)
2310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 if (!PySet_Check(set)) {
2312 PyErr_BadInternalCall();
2313 return -1;
2314 }
2315 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002316}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002317
Raymond Hettingere259f132013-12-15 11:56:14 -08002318/* Exported for the gdb plugin's benefit. */
2319PyObject *_PySet_Dummy = dummy;
2320
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002321#ifdef Py_DEBUG
2322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002324 Returns True and original set is restored. */
2325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326#define assertRaises(call_return_value, exception) \
2327 do { \
2328 assert(call_return_value); \
2329 assert(PyErr_ExceptionMatches(exception)); \
2330 PyErr_Clear(); \
2331 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002332
2333static PyObject *
2334test_c_api(PySetObject *so)
2335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 Py_ssize_t count;
2337 char *s;
2338 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002339 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002341 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 /* Verify preconditions */
2345 assert(PyAnySet_Check(ob));
2346 assert(PyAnySet_CheckExact(ob));
2347 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 /* so.clear(); so |= set("abc"); */
2350 str = PyUnicode_FromString("abc");
2351 if (str == NULL)
2352 return NULL;
2353 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002354 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 Py_DECREF(str);
2356 return NULL;
2357 }
2358 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 /* Exercise type/size checks */
2361 assert(PySet_Size(ob) == 3);
2362 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* Raise TypeError for non-iterable constructor arguments */
2365 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2366 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 /* Raise TypeError for unhashable key */
2369 dup = PySet_New(ob);
2370 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2371 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2372 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 /* Exercise successful pop, contains, add, and discard */
2375 elem = PySet_Pop(ob);
2376 assert(PySet_Contains(ob, elem) == 0);
2377 assert(PySet_GET_SIZE(ob) == 2);
2378 assert(PySet_Add(ob, elem) == 0);
2379 assert(PySet_Contains(ob, elem) == 1);
2380 assert(PySet_GET_SIZE(ob) == 3);
2381 assert(PySet_Discard(ob, elem) == 1);
2382 assert(PySet_GET_SIZE(ob) == 2);
2383 assert(PySet_Discard(ob, elem) == 0);
2384 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 /* Exercise clear */
2387 dup2 = PySet_New(dup);
2388 assert(PySet_Clear(dup2) == 0);
2389 assert(PySet_Size(dup2) == 0);
2390 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* Raise SystemError on clear or update of frozen set */
2393 f = PyFrozenSet_New(dup);
2394 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2395 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2396 assert(PySet_Add(f, elem) == 0);
2397 Py_INCREF(f);
2398 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2399 Py_DECREF(f);
2400 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 /* Exercise direct iteration */
2403 i = 0, count = 0;
2404 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2405 s = _PyUnicode_AsString(x);
2406 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2407 count++;
2408 }
2409 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 /* Exercise updates */
2412 dup2 = PySet_New(NULL);
2413 assert(_PySet_Update(dup2, dup) == 0);
2414 assert(PySet_Size(dup2) == 3);
2415 assert(_PySet_Update(dup2, dup) == 0);
2416 assert(PySet_Size(dup2) == 3);
2417 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* Raise SystemError when self argument is not a set or frozenset. */
2420 t = PyTuple_New(0);
2421 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2422 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2423 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 /* Raise SystemError when self argument is not a set. */
2426 f = PyFrozenSet_New(dup);
2427 assert(PySet_Size(f) == 3);
2428 assert(PyFrozenSet_CheckExact(f));
2429 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2430 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2431 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 /* Raise KeyError when popping from an empty set */
2434 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2435 Py_DECREF(ob);
2436 assert(PySet_GET_SIZE(ob) == 0);
2437 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* Restore the set from the copy using the PyNumber API */
2440 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2441 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 /* Verify constructors accept NULL arguments */
2444 f = PySet_New(NULL);
2445 assert(f != NULL);
2446 assert(PySet_GET_SIZE(f) == 0);
2447 Py_DECREF(f);
2448 f = PyFrozenSet_New(NULL);
2449 assert(f != NULL);
2450 assert(PyFrozenSet_CheckExact(f));
2451 assert(PySet_GET_SIZE(f) == 0);
2452 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 Py_DECREF(elem);
2455 Py_DECREF(dup);
2456 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002457}
2458
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002459#undef assertRaises
2460
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002461#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002462
2463/***** Dummy Struct *************************************************/
2464
2465static PyObject *
2466dummy_repr(PyObject *op)
2467{
2468 return PyUnicode_FromString("<dummy key>");
2469}
2470
2471static void
2472dummy_dealloc(PyObject* ignore)
2473{
2474 Py_FatalError("deallocating <dummy key>");
2475}
2476
2477static PyTypeObject _PySetDummy_Type = {
2478 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2479 "<dummy key> type",
2480 0,
2481 0,
2482 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2483 0, /*tp_print*/
2484 0, /*tp_getattr*/
2485 0, /*tp_setattr*/
2486 0, /*tp_reserved*/
2487 dummy_repr, /*tp_repr*/
2488 0, /*tp_as_number*/
2489 0, /*tp_as_sequence*/
2490 0, /*tp_as_mapping*/
2491 0, /*tp_hash */
2492 0, /*tp_call */
2493 0, /*tp_str */
2494 0, /*tp_getattro */
2495 0, /*tp_setattro */
2496 0, /*tp_as_buffer */
2497 Py_TPFLAGS_DEFAULT, /*tp_flags */
2498};
2499
2500static PyObject _dummy_struct = {
2501 _PyObject_EXTRA_INIT
2502 2, &_PySetDummy_Type
2503};
2504