blob: d962c1e44a0431ba4aeed2de1c2d882380d14941 [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 */
603 for (i = 0; i <= other->mask; i++, other_entry++) {
604 key = other_entry->key;
605 if (key != NULL && key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000606 Py_INCREF(key);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700607 if (set_insert_key(so, key, other_entry->hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000608 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 return -1;
610 }
611 }
612 }
613 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000614}
615
616static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000617set_contains_entry(PySetObject *so, setentry *entry)
618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PyObject *key;
620 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000621
Raymond Hettinger93035c42015-01-25 16:12:49 -0800622 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 if (lu_entry == NULL)
624 return -1;
625 key = lu_entry->key;
Yury Selivanov7aa53412015-05-30 10:57:56 -0400626 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000627}
628
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800629static int
630set_contains_key(PySetObject *so, PyObject *key)
631{
632 setentry entry;
633 Py_hash_t hash;
634
635 if (!PyUnicode_CheckExact(key) ||
636 (hash = ((PyASCIIObject *) key)->hash) == -1) {
637 hash = PyObject_Hash(key);
638 if (hash == -1)
639 return -1;
640 }
641 entry.key = key;
642 entry.hash = hash;
643 return set_contains_entry(so, &entry);
644}
645
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000646static PyObject *
647set_pop(PySetObject *so)
648{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800649 /* Make sure the search finger is in bounds */
650 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200651 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 assert (PyAnySet_Check(so));
655 if (so->used == 0) {
656 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
657 return NULL;
658 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000659
Raymond Hettinger1202a472015-01-18 13:12:42 -0800660 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
661 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800662 if (i > so->mask)
663 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 }
665 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800667 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800669 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000671}
672
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000673PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
674Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000675
676static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000677set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 Py_ssize_t pos = 0;
680 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 while (set_next(so, &pos, &entry))
683 Py_VISIT(entry->key);
684 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000685}
686
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000687static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000688frozenset_hash(PyObject *self)
689{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800690 /* Most of the constants in this hash algorithm are randomly choosen
691 large primes with "interesting bit patterns" and that passed
692 tests for good collision statistics on a variety of problematic
693 datasets such as:
694
695 ps = []
696 for r in range(21):
697 ps += itertools.combinations(range(20), r)
698 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
699
700 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800702 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 setentry *entry;
704 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (so->hash != -1)
707 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000708
Mark Dickinson57e683e2011-09-24 18:18:40 +0100709 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 while (set_next(so, &pos, &entry)) {
711 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800712 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 combinations of a small number of elements with nearby
714 hashes so that many distinct combinations collapse to only
715 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000716 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800717 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800719 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500720 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800721 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200722 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800723 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 so->hash = hash;
725 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000726}
727
Raymond Hettingera9d99362005-08-05 00:01:15 +0000728/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000729
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000730typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 PyObject_HEAD
732 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
733 Py_ssize_t si_used;
734 Py_ssize_t si_pos;
735 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000736} setiterobject;
737
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000738static void
739setiter_dealloc(setiterobject *si)
740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 Py_XDECREF(si->si_set);
742 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000743}
744
745static int
746setiter_traverse(setiterobject *si, visitproc visit, void *arg)
747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 Py_VISIT(si->si_set);
749 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000750}
751
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000752static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000753setiter_len(setiterobject *si)
754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 Py_ssize_t len = 0;
756 if (si->si_set != NULL && si->si_used == si->si_set->used)
757 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000758 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000759}
760
Armin Rigof5b3e362006-02-11 21:32:43 +0000761PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000762
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000763static PyObject *setiter_iternext(setiterobject *si);
764
765static PyObject *
766setiter_reduce(setiterobject *si)
767{
768 PyObject *list;
769 setiterobject tmp;
770
771 list = PyList_New(0);
772 if (!list)
773 return NULL;
774
Ezio Melotti0e1af282012-09-28 16:43:40 +0300775 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000776 tmp = *si;
777 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300778
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000779 /* iterate the temporary into a list */
780 for(;;) {
781 PyObject *element = setiter_iternext(&tmp);
782 if (element) {
783 if (PyList_Append(list, element)) {
784 Py_DECREF(element);
785 Py_DECREF(list);
786 Py_XDECREF(tmp.si_set);
787 return NULL;
788 }
789 Py_DECREF(element);
790 } else
791 break;
792 }
793 Py_XDECREF(tmp.si_set);
794 /* check for error */
795 if (tmp.si_set != NULL) {
796 /* we have an error */
797 Py_DECREF(list);
798 return NULL;
799 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200800 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000801}
802
803PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
804
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000805static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000807 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809};
810
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000811static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200814 Py_ssize_t i, mask;
815 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (so == NULL)
819 return NULL;
820 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 if (si->si_used != so->used) {
823 PyErr_SetString(PyExc_RuntimeError,
824 "Set changed size during iteration");
825 si->si_used = -1; /* Make this state sticky */
826 return NULL;
827 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 i = si->si_pos;
830 assert(i>=0);
831 entry = so->table;
832 mask = so->mask;
833 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
834 i++;
835 si->si_pos = i+1;
836 if (i > mask)
837 goto fail;
838 si->len--;
839 key = entry[i].key;
840 Py_INCREF(key);
841 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000842
843fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 Py_DECREF(so);
845 si->si_set = NULL;
846 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847}
848
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000849PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 PyVarObject_HEAD_INIT(&PyType_Type, 0)
851 "set_iterator", /* tp_name */
852 sizeof(setiterobject), /* tp_basicsize */
853 0, /* tp_itemsize */
854 /* methods */
855 (destructor)setiter_dealloc, /* tp_dealloc */
856 0, /* tp_print */
857 0, /* tp_getattr */
858 0, /* tp_setattr */
859 0, /* tp_reserved */
860 0, /* tp_repr */
861 0, /* tp_as_number */
862 0, /* tp_as_sequence */
863 0, /* tp_as_mapping */
864 0, /* tp_hash */
865 0, /* tp_call */
866 0, /* tp_str */
867 PyObject_GenericGetAttr, /* tp_getattro */
868 0, /* tp_setattro */
869 0, /* tp_as_buffer */
870 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
871 0, /* tp_doc */
872 (traverseproc)setiter_traverse, /* tp_traverse */
873 0, /* tp_clear */
874 0, /* tp_richcompare */
875 0, /* tp_weaklistoffset */
876 PyObject_SelfIter, /* tp_iter */
877 (iternextfunc)setiter_iternext, /* tp_iternext */
878 setiter_methods, /* tp_methods */
879 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880};
881
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882static PyObject *
883set_iter(PySetObject *so)
884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
886 if (si == NULL)
887 return NULL;
888 Py_INCREF(so);
889 si->si_set = so;
890 si->si_used = so->used;
891 si->si_pos = 0;
892 si->len = so->used;
893 _PyObject_GC_TRACK(si);
894 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000895}
896
Raymond Hettingerd7946662005-08-01 21:39:29 +0000897static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000898set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 if (PyAnySet_Check(other))
903 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 if (PyDict_CheckExact(other)) {
906 PyObject *value;
907 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000908 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 /* Do one big resize at the start, rather than
912 * incrementally resizing as we insert new keys. Expect
913 * that there will be no (or few) overlapping keys.
914 */
915 if (dictsize == -1)
916 return -1;
917 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
918 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
919 return -1;
920 }
921 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
922 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 an_entry.hash = hash;
925 an_entry.key = key;
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700926 if (set_add_entry(so, &an_entry))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 return -1;
928 }
929 return 0;
930 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 it = PyObject_GetIter(other);
933 if (it == NULL)
934 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700937 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 Py_DECREF(it);
939 Py_DECREF(key);
940 return -1;
941 }
942 Py_DECREF(key);
943 }
944 Py_DECREF(it);
945 if (PyErr_Occurred())
946 return -1;
947 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000948}
949
950static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000951set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
956 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700957 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 return NULL;
959 }
960 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000961}
962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000964"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000965
Raymond Hettinger426d9952014-05-18 21:40:20 +0100966/* XXX Todo:
967 If aligned memory allocations become available, make the
968 set object 64 byte aligned so that most of the fields
969 can be retrieved or updated in a single cache line.
970*/
971
Raymond Hettingera38123e2003-11-24 22:18:49 +0000972static PyObject *
973make_new_set(PyTypeObject *type, PyObject *iterable)
974{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200975 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700978 so = (PySetObject *)type->tp_alloc(type, 0);
979 if (so == NULL)
980 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000981
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700982 so->fill = 0;
983 so->used = 0;
984 so->mask = PySet_MINSIZE - 1;
985 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700986 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800987 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700991 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 Py_DECREF(so);
993 return NULL;
994 }
995 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +0000998}
999
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001000static PyObject *
1001make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1004 if (PyType_IsSubtype(type, &PySet_Type))
1005 type = &PySet_Type;
1006 else
1007 type = &PyFrozenSet_Type;
1008 }
1009 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001010}
1011
Raymond Hettingerd7946662005-08-01 21:39:29 +00001012/* The empty frozenset is a singleton */
1013static PyObject *emptyfrozenset = NULL;
1014
Raymond Hettingera690a992003-11-16 16:17:49 +00001015static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001016frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1021 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1024 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (type != &PyFrozenSet_Type)
1027 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (iterable != NULL) {
1030 /* frozenset(f) is idempotent */
1031 if (PyFrozenSet_CheckExact(iterable)) {
1032 Py_INCREF(iterable);
1033 return iterable;
1034 }
1035 result = make_new_set(type, iterable);
1036 if (result == NULL || PySet_GET_SIZE(result))
1037 return result;
1038 Py_DECREF(result);
1039 }
1040 /* The empty frozenset is a singleton */
1041 if (emptyfrozenset == NULL)
1042 emptyfrozenset = make_new_set(type, NULL);
1043 Py_XINCREF(emptyfrozenset);
1044 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001045}
1046
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001047int
1048PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001049{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001050 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001051}
1052
1053void
1054PySet_Fini(void)
1055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001057}
1058
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001059static PyObject *
1060set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1063 return NULL;
1064
1065 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001066}
1067
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001068/* set_swap_bodies() switches the contents of any two sets by moving their
1069 internal data pointers and, if needed, copying the internal smalltables.
1070 Semantically equivalent to:
1071
1072 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1073
1074 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001075 Useful for operations that update in-place (by allowing an intermediate
1076 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001077*/
1078
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001079static void
1080set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 Py_ssize_t t;
1083 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001085 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 t = a->fill; a->fill = b->fill; b->fill = t;
1088 t = a->used; a->used = b->used; b->used = t;
1089 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 u = a->table;
1092 if (a->table == a->smalltable)
1093 u = b->smalltable;
1094 a->table = b->table;
1095 if (b->table == b->smalltable)
1096 a->table = a->smalltable;
1097 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (a->table == a->smalltable || b->table == b->smalltable) {
1100 memcpy(tab, a->smalltable, sizeof(tab));
1101 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1102 memcpy(b->smalltable, tab, sizeof(tab));
1103 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1106 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1107 h = a->hash; a->hash = b->hash; b->hash = h;
1108 } else {
1109 a->hash = -1;
1110 b->hash = -1;
1111 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001112}
1113
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001114static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001115set_copy(PySetObject *so)
1116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001118}
1119
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001120static PyObject *
1121frozenset_copy(PySetObject *so)
1122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (PyFrozenSet_CheckExact(so)) {
1124 Py_INCREF(so);
1125 return (PyObject *)so;
1126 }
1127 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001128}
1129
Raymond Hettingera690a992003-11-16 16:17:49 +00001130PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1131
1132static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001133set_clear(PySetObject *so)
1134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 set_clear_internal(so);
1136 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001137}
1138
1139PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1140
1141static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001142set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 PySetObject *result;
1145 PyObject *other;
1146 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 result = (PySetObject *)set_copy(so);
1149 if (result == NULL)
1150 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1153 other = PyTuple_GET_ITEM(args, i);
1154 if ((PyObject *)so == other)
1155 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001156 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 Py_DECREF(result);
1158 return NULL;
1159 }
1160 }
1161 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001162}
1163
1164PyDoc_STRVAR(union_doc,
1165 "Return the union of sets as a new set.\n\
1166\n\
1167(i.e. all elements that are in either set.)");
1168
1169static PyObject *
1170set_or(PySetObject *so, PyObject *other)
1171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001173
Brian Curtindfc80e32011-08-10 20:28:54 -05001174 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1175 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 result = (PySetObject *)set_copy(so);
1178 if (result == NULL)
1179 return NULL;
1180 if ((PyObject *)so == other)
1181 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001182 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 Py_DECREF(result);
1184 return NULL;
1185 }
1186 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001187}
1188
Raymond Hettingera690a992003-11-16 16:17:49 +00001189static PyObject *
1190set_ior(PySetObject *so, PyObject *other)
1191{
Brian Curtindfc80e32011-08-10 20:28:54 -05001192 if (!PyAnySet_Check(other))
1193 Py_RETURN_NOTIMPLEMENTED;
1194
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001195 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 return NULL;
1197 Py_INCREF(so);
1198 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001199}
1200
1201static PyObject *
1202set_intersection(PySetObject *so, PyObject *other)
1203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 PySetObject *result;
1205 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 if ((PyObject *)so == other)
1208 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1211 if (result == NULL)
1212 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (PyAnySet_Check(other)) {
1215 Py_ssize_t pos = 0;
1216 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1219 tmp = (PyObject *)so;
1220 so = (PySetObject *)other;
1221 other = tmp;
1222 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 while (set_next((PySetObject *)other, &pos, &entry)) {
1225 int rv = set_contains_entry(so, entry);
1226 if (rv == -1) {
1227 Py_DECREF(result);
1228 return NULL;
1229 }
1230 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001231 if (set_add_entry(result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 Py_DECREF(result);
1233 return NULL;
1234 }
1235 }
1236 }
1237 return (PyObject *)result;
1238 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 it = PyObject_GetIter(other);
1241 if (it == NULL) {
1242 Py_DECREF(result);
1243 return NULL;
1244 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 while ((key = PyIter_Next(it)) != NULL) {
1247 int rv;
1248 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001249 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (hash == -1) {
1252 Py_DECREF(it);
1253 Py_DECREF(result);
1254 Py_DECREF(key);
1255 return NULL;
1256 }
1257 entry.hash = hash;
1258 entry.key = key;
1259 rv = set_contains_entry(so, &entry);
1260 if (rv == -1) {
1261 Py_DECREF(it);
1262 Py_DECREF(result);
1263 Py_DECREF(key);
1264 return NULL;
1265 }
1266 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001267 if (set_add_entry(result, &entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 Py_DECREF(it);
1269 Py_DECREF(result);
1270 Py_DECREF(key);
1271 return NULL;
1272 }
1273 }
1274 Py_DECREF(key);
1275 }
1276 Py_DECREF(it);
1277 if (PyErr_Occurred()) {
1278 Py_DECREF(result);
1279 return NULL;
1280 }
1281 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001282}
1283
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001284static PyObject *
1285set_intersection_multi(PySetObject *so, PyObject *args)
1286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 Py_ssize_t i;
1288 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (PyTuple_GET_SIZE(args) == 0)
1291 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 Py_INCREF(so);
1294 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1295 PyObject *other = PyTuple_GET_ITEM(args, i);
1296 PyObject *newresult = set_intersection((PySetObject *)result, other);
1297 if (newresult == NULL) {
1298 Py_DECREF(result);
1299 return NULL;
1300 }
1301 Py_DECREF(result);
1302 result = newresult;
1303 }
1304 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001305}
1306
Raymond Hettingera690a992003-11-16 16:17:49 +00001307PyDoc_STRVAR(intersection_doc,
1308"Return the intersection of two sets as a new set.\n\
1309\n\
1310(i.e. all elements that are in both sets.)");
1311
1312static PyObject *
1313set_intersection_update(PySetObject *so, PyObject *other)
1314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 tmp = set_intersection(so, other);
1318 if (tmp == NULL)
1319 return NULL;
1320 set_swap_bodies(so, (PySetObject *)tmp);
1321 Py_DECREF(tmp);
1322 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001323}
1324
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001325static PyObject *
1326set_intersection_update_multi(PySetObject *so, PyObject *args)
1327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 tmp = set_intersection_multi(so, args);
1331 if (tmp == NULL)
1332 return NULL;
1333 set_swap_bodies(so, (PySetObject *)tmp);
1334 Py_DECREF(tmp);
1335 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001336}
1337
Raymond Hettingera690a992003-11-16 16:17:49 +00001338PyDoc_STRVAR(intersection_update_doc,
1339"Update a set with the intersection of itself and another.");
1340
1341static PyObject *
1342set_and(PySetObject *so, PyObject *other)
1343{
Brian Curtindfc80e32011-08-10 20:28:54 -05001344 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1345 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001347}
1348
1349static PyObject *
1350set_iand(PySetObject *so, PyObject *other)
1351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001353
Brian Curtindfc80e32011-08-10 20:28:54 -05001354 if (!PyAnySet_Check(other))
1355 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 result = set_intersection_update(so, other);
1357 if (result == NULL)
1358 return NULL;
1359 Py_DECREF(result);
1360 Py_INCREF(so);
1361 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001362}
1363
Guido van Rossum58da9312007-11-10 23:39:45 +00001364static PyObject *
1365set_isdisjoint(PySetObject *so, PyObject *other)
1366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if ((PyObject *)so == other) {
1370 if (PySet_GET_SIZE(so) == 0)
1371 Py_RETURN_TRUE;
1372 else
1373 Py_RETURN_FALSE;
1374 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if (PyAnySet_CheckExact(other)) {
1377 Py_ssize_t pos = 0;
1378 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1381 tmp = (PyObject *)so;
1382 so = (PySetObject *)other;
1383 other = tmp;
1384 }
1385 while (set_next((PySetObject *)other, &pos, &entry)) {
1386 int rv = set_contains_entry(so, entry);
1387 if (rv == -1)
1388 return NULL;
1389 if (rv)
1390 Py_RETURN_FALSE;
1391 }
1392 Py_RETURN_TRUE;
1393 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 it = PyObject_GetIter(other);
1396 if (it == NULL)
1397 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 while ((key = PyIter_Next(it)) != NULL) {
1400 int rv;
1401 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001402 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 if (hash == -1) {
1405 Py_DECREF(key);
1406 Py_DECREF(it);
1407 return NULL;
1408 }
1409 entry.hash = hash;
1410 entry.key = key;
1411 rv = set_contains_entry(so, &entry);
1412 Py_DECREF(key);
1413 if (rv == -1) {
1414 Py_DECREF(it);
1415 return NULL;
1416 }
1417 if (rv) {
1418 Py_DECREF(it);
1419 Py_RETURN_FALSE;
1420 }
1421 }
1422 Py_DECREF(it);
1423 if (PyErr_Occurred())
1424 return NULL;
1425 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001426}
1427
1428PyDoc_STRVAR(isdisjoint_doc,
1429"Return True if two sets have a null intersection.");
1430
Neal Norwitz6576bd82005-11-13 18:41:28 +00001431static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001432set_difference_update_internal(PySetObject *so, PyObject *other)
1433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if ((PyObject *)so == other)
1435 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (PyAnySet_Check(other)) {
1438 setentry *entry;
1439 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 while (set_next((PySetObject *)other, &pos, &entry))
1442 if (set_discard_entry(so, entry) == -1)
1443 return -1;
1444 } else {
1445 PyObject *key, *it;
1446 it = PyObject_GetIter(other);
1447 if (it == NULL)
1448 return -1;
1449
1450 while ((key = PyIter_Next(it)) != NULL) {
1451 if (set_discard_key(so, key) == -1) {
1452 Py_DECREF(it);
1453 Py_DECREF(key);
1454 return -1;
1455 }
1456 Py_DECREF(key);
1457 }
1458 Py_DECREF(it);
1459 if (PyErr_Occurred())
1460 return -1;
1461 }
1462 /* If more than 1/5 are dummies, then resize them away. */
1463 if ((so->fill - so->used) * 5 < so->mask)
1464 return 0;
1465 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001466}
1467
Raymond Hettingera690a992003-11-16 16:17:49 +00001468static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001469set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1474 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001475 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 return NULL;
1477 }
1478 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001479}
1480
1481PyDoc_STRVAR(difference_update_doc,
1482"Remove all elements of another set from this set.");
1483
1484static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001485set_copy_and_difference(PySetObject *so, PyObject *other)
1486{
1487 PyObject *result;
1488
1489 result = set_copy(so);
1490 if (result == NULL)
1491 return NULL;
1492 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1493 return result;
1494 Py_DECREF(result);
1495 return NULL;
1496}
1497
1498static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001499set_difference(PySetObject *so, PyObject *other)
1500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 PyObject *result;
1502 setentry *entry;
1503 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001506 return set_copy_and_difference(so, other);
1507 }
1508
1509 /* If len(so) much more than len(other), it's more efficient to simply copy
1510 * so and then iterate other looking for common elements. */
1511 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1512 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 result = make_new_set_basetype(Py_TYPE(so), NULL);
1516 if (result == NULL)
1517 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 if (PyDict_CheckExact(other)) {
1520 while (set_next(so, &pos, &entry)) {
1521 setentry entrycopy;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001522 int rv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 entrycopy.hash = entry->hash;
1524 entrycopy.key = entry->key;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001525 rv = _PyDict_Contains(other, entry->key, entry->hash);
1526 if (rv < 0) {
1527 Py_DECREF(result);
1528 return NULL;
1529 }
1530 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001531 if (set_add_entry((PySetObject *)result, &entrycopy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 Py_DECREF(result);
1533 return NULL;
1534 }
1535 }
1536 }
1537 return result;
1538 }
1539
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001540 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 while (set_next(so, &pos, &entry)) {
1542 int rv = set_contains_entry((PySetObject *)other, entry);
1543 if (rv == -1) {
1544 Py_DECREF(result);
1545 return NULL;
1546 }
1547 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001548 if (set_add_entry((PySetObject *)result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 Py_DECREF(result);
1550 return NULL;
1551 }
1552 }
1553 }
1554 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001555}
1556
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001557static PyObject *
1558set_difference_multi(PySetObject *so, PyObject *args)
1559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 Py_ssize_t i;
1561 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if (PyTuple_GET_SIZE(args) == 0)
1564 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 other = PyTuple_GET_ITEM(args, 0);
1567 result = set_difference(so, other);
1568 if (result == NULL)
1569 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1572 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001573 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 Py_DECREF(result);
1575 return NULL;
1576 }
1577 }
1578 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001579}
1580
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001581PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001582"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001583\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001584(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001585static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001586set_sub(PySetObject *so, PyObject *other)
1587{
Brian Curtindfc80e32011-08-10 20:28:54 -05001588 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1589 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001591}
1592
1593static PyObject *
1594set_isub(PySetObject *so, PyObject *other)
1595{
Brian Curtindfc80e32011-08-10 20:28:54 -05001596 if (!PyAnySet_Check(other))
1597 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001598 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 return NULL;
1600 Py_INCREF(so);
1601 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001602}
1603
1604static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001605set_symmetric_difference_update(PySetObject *so, PyObject *other)
1606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 PySetObject *otherset;
1608 PyObject *key;
1609 Py_ssize_t pos = 0;
1610 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if ((PyObject *)so == other)
1613 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 if (PyDict_CheckExact(other)) {
1616 PyObject *value;
1617 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001618 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1620 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001621
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001622 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 an_entry.hash = hash;
1624 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001627 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001628 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001631 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001632 if (set_add_entry(so, &an_entry)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001633 Py_DECREF(key);
1634 return NULL;
1635 }
1636 }
Georg Brandl2d444492010-09-03 10:52:55 +00001637 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 }
1639 Py_RETURN_NONE;
1640 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 if (PyAnySet_Check(other)) {
1643 Py_INCREF(other);
1644 otherset = (PySetObject *)other;
1645 } else {
1646 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1647 if (otherset == NULL)
1648 return NULL;
1649 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 while (set_next(otherset, &pos, &entry)) {
1652 int rv = set_discard_entry(so, entry);
1653 if (rv == -1) {
1654 Py_DECREF(otherset);
1655 return NULL;
1656 }
1657 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001658 if (set_add_entry(so, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 Py_DECREF(otherset);
1660 return NULL;
1661 }
1662 }
1663 }
1664 Py_DECREF(otherset);
1665 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001666}
1667
1668PyDoc_STRVAR(symmetric_difference_update_doc,
1669"Update a set with the symmetric difference of itself and another.");
1670
1671static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001672set_symmetric_difference(PySetObject *so, PyObject *other)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 PyObject *rv;
1675 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1678 if (otherset == NULL)
1679 return NULL;
1680 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1681 if (rv == NULL)
1682 return NULL;
1683 Py_DECREF(rv);
1684 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001685}
1686
1687PyDoc_STRVAR(symmetric_difference_doc,
1688"Return the symmetric difference of two sets as a new set.\n\
1689\n\
1690(i.e. all elements that are in exactly one of the sets.)");
1691
1692static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001693set_xor(PySetObject *so, PyObject *other)
1694{
Brian Curtindfc80e32011-08-10 20:28:54 -05001695 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1696 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001698}
1699
1700static PyObject *
1701set_ixor(PySetObject *so, PyObject *other)
1702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001704
Brian Curtindfc80e32011-08-10 20:28:54 -05001705 if (!PyAnySet_Check(other))
1706 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 result = set_symmetric_difference_update(so, other);
1708 if (result == NULL)
1709 return NULL;
1710 Py_DECREF(result);
1711 Py_INCREF(so);
1712 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001713}
1714
1715static PyObject *
1716set_issubset(PySetObject *so, PyObject *other)
1717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 setentry *entry;
1719 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (!PyAnySet_Check(other)) {
1722 PyObject *tmp, *result;
1723 tmp = make_new_set(&PySet_Type, other);
1724 if (tmp == NULL)
1725 return NULL;
1726 result = set_issubset(so, tmp);
1727 Py_DECREF(tmp);
1728 return result;
1729 }
1730 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1731 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 while (set_next(so, &pos, &entry)) {
1734 int rv = set_contains_entry((PySetObject *)other, entry);
1735 if (rv == -1)
1736 return NULL;
1737 if (!rv)
1738 Py_RETURN_FALSE;
1739 }
1740 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001741}
1742
1743PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1744
1745static PyObject *
1746set_issuperset(PySetObject *so, PyObject *other)
1747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 if (!PyAnySet_Check(other)) {
1751 tmp = make_new_set(&PySet_Type, other);
1752 if (tmp == NULL)
1753 return NULL;
1754 result = set_issuperset(so, tmp);
1755 Py_DECREF(tmp);
1756 return result;
1757 }
1758 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001759}
1760
1761PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1762
Raymond Hettingera690a992003-11-16 16:17:49 +00001763static PyObject *
1764set_richcompare(PySetObject *v, PyObject *w, int op)
1765{
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001766 PyObject *r1;
1767 int r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001768
Brian Curtindfc80e32011-08-10 20:28:54 -05001769 if(!PyAnySet_Check(w))
1770 Py_RETURN_NOTIMPLEMENTED;
1771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 switch (op) {
1773 case Py_EQ:
1774 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1775 Py_RETURN_FALSE;
1776 if (v->hash != -1 &&
1777 ((PySetObject *)w)->hash != -1 &&
1778 v->hash != ((PySetObject *)w)->hash)
1779 Py_RETURN_FALSE;
1780 return set_issubset(v, w);
1781 case Py_NE:
1782 r1 = set_richcompare(v, w, Py_EQ);
1783 if (r1 == NULL)
1784 return NULL;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001785 r2 = PyObject_IsTrue(r1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 Py_DECREF(r1);
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001787 if (r2 < 0)
1788 return NULL;
1789 return PyBool_FromLong(!r2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 case Py_LE:
1791 return set_issubset(v, w);
1792 case Py_GE:
1793 return set_issuperset(v, w);
1794 case Py_LT:
1795 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1796 Py_RETURN_FALSE;
1797 return set_issubset(v, w);
1798 case Py_GT:
1799 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1800 Py_RETURN_FALSE;
1801 return set_issuperset(v, w);
1802 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001803 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001804}
1805
Raymond Hettingera690a992003-11-16 16:17:49 +00001806static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001807set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001808{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001809 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 return NULL;
1811 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001812}
1813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001815"Add an element to a set.\n\
1816\n\
1817This has no effect if the element is already present.");
1818
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001819static int
1820set_contains(PySetObject *so, PyObject *key)
1821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyObject *tmpkey;
1823 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 rv = set_contains_key(so, key);
1826 if (rv == -1) {
1827 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1828 return -1;
1829 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001830 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 if (tmpkey == NULL)
1832 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001833 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 Py_DECREF(tmpkey);
1835 }
1836 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001837}
1838
1839static PyObject *
1840set_direct_contains(PySetObject *so, PyObject *key)
1841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 result = set_contains(so, key);
1845 if (result == -1)
1846 return NULL;
1847 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001848}
1849
1850PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1851
Raymond Hettingera690a992003-11-16 16:17:49 +00001852static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001853set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 PyObject *tmpkey;
1856 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 rv = set_discard_key(so, key);
1859 if (rv == -1) {
1860 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1861 return NULL;
1862 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001863 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 if (tmpkey == NULL)
1865 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 Py_DECREF(tmpkey);
1868 if (rv == -1)
1869 return NULL;
1870 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001873 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 return NULL;
1875 }
1876 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001877}
1878
1879PyDoc_STRVAR(remove_doc,
1880"Remove an element from a set; it must be a member.\n\
1881\n\
1882If the element is not a member, raise a KeyError.");
1883
1884static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001885set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001886{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001887 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 rv = set_discard_key(so, key);
1891 if (rv == -1) {
1892 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1893 return NULL;
1894 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001895 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 if (tmpkey == NULL)
1897 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001898 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001900 if (rv == -1)
1901 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 }
1903 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001904}
1905
1906PyDoc_STRVAR(discard_doc,
1907"Remove an element from a set if it is a member.\n\
1908\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001910
1911static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001912set_reduce(PySetObject *so)
1913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001915 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 keys = PySequence_List((PyObject *)so);
1918 if (keys == NULL)
1919 goto done;
1920 args = PyTuple_Pack(1, keys);
1921 if (args == NULL)
1922 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001923 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 if (dict == NULL) {
1925 PyErr_Clear();
1926 dict = Py_None;
1927 Py_INCREF(dict);
1928 }
1929 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001930done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 Py_XDECREF(args);
1932 Py_XDECREF(keys);
1933 Py_XDECREF(dict);
1934 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001935}
1936
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001937static PyObject *
1938set_sizeof(PySetObject *so)
1939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 res = sizeof(PySetObject);
1943 if (so->table != so->smalltable)
1944 res = res + (so->mask + 1) * sizeof(setentry);
1945 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001946}
1947
1948PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001949static int
1950set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 if (!PyAnySet_Check(self))
1955 return -1;
1956 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1957 return -1;
1958 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1959 return -1;
1960 set_clear_internal(self);
1961 self->hash = -1;
1962 if (iterable == NULL)
1963 return 0;
1964 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001965}
1966
Raymond Hettingera690a992003-11-16 16:17:49 +00001967static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 set_len, /* sq_length */
1969 0, /* sq_concat */
1970 0, /* sq_repeat */
1971 0, /* sq_item */
1972 0, /* sq_slice */
1973 0, /* sq_ass_item */
1974 0, /* sq_ass_slice */
1975 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001976};
1977
1978/* set object ********************************************************/
1979
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001980#ifdef Py_DEBUG
1981static PyObject *test_c_api(PySetObject *so);
1982
1983PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1984All is well if assertions don't fail.");
1985#endif
1986
Raymond Hettingera690a992003-11-16 16:17:49 +00001987static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 {"add", (PyCFunction)set_add, METH_O,
1989 add_doc},
1990 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1991 clear_doc},
1992 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1993 contains_doc},
1994 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1995 copy_doc},
1996 {"discard", (PyCFunction)set_discard, METH_O,
1997 discard_doc},
1998 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
1999 difference_doc},
2000 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2001 difference_update_doc},
2002 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2003 intersection_doc},
2004 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2005 intersection_update_doc},
2006 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2007 isdisjoint_doc},
2008 {"issubset", (PyCFunction)set_issubset, METH_O,
2009 issubset_doc},
2010 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2011 issuperset_doc},
2012 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2013 pop_doc},
2014 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2015 reduce_doc},
2016 {"remove", (PyCFunction)set_remove, METH_O,
2017 remove_doc},
2018 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2019 sizeof_doc},
2020 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2021 symmetric_difference_doc},
2022 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2023 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002024#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2026 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002027#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 {"union", (PyCFunction)set_union, METH_VARARGS,
2029 union_doc},
2030 {"update", (PyCFunction)set_update, METH_VARARGS,
2031 update_doc},
2032 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002033};
2034
2035static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 0, /*nb_add*/
2037 (binaryfunc)set_sub, /*nb_subtract*/
2038 0, /*nb_multiply*/
2039 0, /*nb_remainder*/
2040 0, /*nb_divmod*/
2041 0, /*nb_power*/
2042 0, /*nb_negative*/
2043 0, /*nb_positive*/
2044 0, /*nb_absolute*/
2045 0, /*nb_bool*/
2046 0, /*nb_invert*/
2047 0, /*nb_lshift*/
2048 0, /*nb_rshift*/
2049 (binaryfunc)set_and, /*nb_and*/
2050 (binaryfunc)set_xor, /*nb_xor*/
2051 (binaryfunc)set_or, /*nb_or*/
2052 0, /*nb_int*/
2053 0, /*nb_reserved*/
2054 0, /*nb_float*/
2055 0, /*nb_inplace_add*/
2056 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2057 0, /*nb_inplace_multiply*/
2058 0, /*nb_inplace_remainder*/
2059 0, /*nb_inplace_power*/
2060 0, /*nb_inplace_lshift*/
2061 0, /*nb_inplace_rshift*/
2062 (binaryfunc)set_iand, /*nb_inplace_and*/
2063 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2064 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002065};
2066
2067PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002068"set() -> new empty set object\n\
2069set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002070\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002071Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002072
2073PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2075 "set", /* tp_name */
2076 sizeof(PySetObject), /* tp_basicsize */
2077 0, /* tp_itemsize */
2078 /* methods */
2079 (destructor)set_dealloc, /* tp_dealloc */
2080 0, /* tp_print */
2081 0, /* tp_getattr */
2082 0, /* tp_setattr */
2083 0, /* tp_reserved */
2084 (reprfunc)set_repr, /* tp_repr */
2085 &set_as_number, /* tp_as_number */
2086 &set_as_sequence, /* tp_as_sequence */
2087 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002088 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 0, /* tp_call */
2090 0, /* tp_str */
2091 PyObject_GenericGetAttr, /* tp_getattro */
2092 0, /* tp_setattro */
2093 0, /* tp_as_buffer */
2094 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2095 Py_TPFLAGS_BASETYPE, /* tp_flags */
2096 set_doc, /* tp_doc */
2097 (traverseproc)set_traverse, /* tp_traverse */
2098 (inquiry)set_clear_internal, /* tp_clear */
2099 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002100 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2101 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 0, /* tp_iternext */
2103 set_methods, /* tp_methods */
2104 0, /* tp_members */
2105 0, /* tp_getset */
2106 0, /* tp_base */
2107 0, /* tp_dict */
2108 0, /* tp_descr_get */
2109 0, /* tp_descr_set */
2110 0, /* tp_dictoffset */
2111 (initproc)set_init, /* tp_init */
2112 PyType_GenericAlloc, /* tp_alloc */
2113 set_new, /* tp_new */
2114 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002115};
2116
2117/* frozenset object ********************************************************/
2118
2119
2120static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2122 contains_doc},
2123 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2124 copy_doc},
2125 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2126 difference_doc},
2127 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2128 intersection_doc},
2129 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2130 isdisjoint_doc},
2131 {"issubset", (PyCFunction)set_issubset, METH_O,
2132 issubset_doc},
2133 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2134 issuperset_doc},
2135 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2136 reduce_doc},
2137 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2138 sizeof_doc},
2139 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2140 symmetric_difference_doc},
2141 {"union", (PyCFunction)set_union, METH_VARARGS,
2142 union_doc},
2143 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002144};
2145
2146static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 0, /*nb_add*/
2148 (binaryfunc)set_sub, /*nb_subtract*/
2149 0, /*nb_multiply*/
2150 0, /*nb_remainder*/
2151 0, /*nb_divmod*/
2152 0, /*nb_power*/
2153 0, /*nb_negative*/
2154 0, /*nb_positive*/
2155 0, /*nb_absolute*/
2156 0, /*nb_bool*/
2157 0, /*nb_invert*/
2158 0, /*nb_lshift*/
2159 0, /*nb_rshift*/
2160 (binaryfunc)set_and, /*nb_and*/
2161 (binaryfunc)set_xor, /*nb_xor*/
2162 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002163};
2164
2165PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002166"frozenset() -> empty frozenset object\n\
2167frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002168\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002169Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002170
2171PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2173 "frozenset", /* tp_name */
2174 sizeof(PySetObject), /* tp_basicsize */
2175 0, /* tp_itemsize */
2176 /* methods */
2177 (destructor)set_dealloc, /* tp_dealloc */
2178 0, /* tp_print */
2179 0, /* tp_getattr */
2180 0, /* tp_setattr */
2181 0, /* tp_reserved */
2182 (reprfunc)set_repr, /* tp_repr */
2183 &frozenset_as_number, /* tp_as_number */
2184 &set_as_sequence, /* tp_as_sequence */
2185 0, /* tp_as_mapping */
2186 frozenset_hash, /* tp_hash */
2187 0, /* tp_call */
2188 0, /* tp_str */
2189 PyObject_GenericGetAttr, /* tp_getattro */
2190 0, /* tp_setattro */
2191 0, /* tp_as_buffer */
2192 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2193 Py_TPFLAGS_BASETYPE, /* tp_flags */
2194 frozenset_doc, /* tp_doc */
2195 (traverseproc)set_traverse, /* tp_traverse */
2196 (inquiry)set_clear_internal, /* tp_clear */
2197 (richcmpfunc)set_richcompare, /* tp_richcompare */
2198 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2199 (getiterfunc)set_iter, /* tp_iter */
2200 0, /* tp_iternext */
2201 frozenset_methods, /* tp_methods */
2202 0, /* tp_members */
2203 0, /* tp_getset */
2204 0, /* tp_base */
2205 0, /* tp_dict */
2206 0, /* tp_descr_get */
2207 0, /* tp_descr_set */
2208 0, /* tp_dictoffset */
2209 0, /* tp_init */
2210 PyType_GenericAlloc, /* tp_alloc */
2211 frozenset_new, /* tp_new */
2212 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002213};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002214
2215
2216/***** C API functions *************************************************/
2217
2218PyObject *
2219PySet_New(PyObject *iterable)
2220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002222}
2223
2224PyObject *
2225PyFrozenSet_New(PyObject *iterable)
2226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002228}
2229
Neal Norwitz8c49c822006-03-04 18:41:19 +00002230Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002231PySet_Size(PyObject *anyset)
2232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 if (!PyAnySet_Check(anyset)) {
2234 PyErr_BadInternalCall();
2235 return -1;
2236 }
2237 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002238}
2239
2240int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002241PySet_Clear(PyObject *set)
2242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 if (!PySet_Check(set)) {
2244 PyErr_BadInternalCall();
2245 return -1;
2246 }
2247 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002248}
2249
2250int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002251PySet_Contains(PyObject *anyset, PyObject *key)
2252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 if (!PyAnySet_Check(anyset)) {
2254 PyErr_BadInternalCall();
2255 return -1;
2256 }
2257 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002258}
2259
2260int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002261PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 if (!PySet_Check(set)) {
2264 PyErr_BadInternalCall();
2265 return -1;
2266 }
2267 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002268}
2269
2270int
Christian Heimesfd66e512008-01-29 12:18:50 +00002271PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 if (!PySet_Check(anyset) &&
2274 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2275 PyErr_BadInternalCall();
2276 return -1;
2277 }
2278 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002279}
2280
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002281int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002282_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 if (!PyAnySet_Check(set)) {
2287 PyErr_BadInternalCall();
2288 return -1;
2289 }
2290 if (set_next((PySetObject *)set, pos, &entry) == 0)
2291 return 0;
2292 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002293 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002295}
2296
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002297PyObject *
2298PySet_Pop(PyObject *set)
2299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 if (!PySet_Check(set)) {
2301 PyErr_BadInternalCall();
2302 return NULL;
2303 }
2304 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002305}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002306
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002307int
2308_PySet_Update(PyObject *set, PyObject *iterable)
2309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (!PySet_Check(set)) {
2311 PyErr_BadInternalCall();
2312 return -1;
2313 }
2314 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002315}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002316
Raymond Hettingere259f132013-12-15 11:56:14 -08002317/* Exported for the gdb plugin's benefit. */
2318PyObject *_PySet_Dummy = dummy;
2319
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002320#ifdef Py_DEBUG
2321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002323 Returns True and original set is restored. */
2324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325#define assertRaises(call_return_value, exception) \
2326 do { \
2327 assert(call_return_value); \
2328 assert(PyErr_ExceptionMatches(exception)); \
2329 PyErr_Clear(); \
2330 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002331
2332static PyObject *
2333test_c_api(PySetObject *so)
2334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 Py_ssize_t count;
2336 char *s;
2337 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002338 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002340 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 /* Verify preconditions */
2344 assert(PyAnySet_Check(ob));
2345 assert(PyAnySet_CheckExact(ob));
2346 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 /* so.clear(); so |= set("abc"); */
2349 str = PyUnicode_FromString("abc");
2350 if (str == NULL)
2351 return NULL;
2352 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002353 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 Py_DECREF(str);
2355 return NULL;
2356 }
2357 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 /* Exercise type/size checks */
2360 assert(PySet_Size(ob) == 3);
2361 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 /* Raise TypeError for non-iterable constructor arguments */
2364 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2365 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 /* Raise TypeError for unhashable key */
2368 dup = PySet_New(ob);
2369 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2370 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2371 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 /* Exercise successful pop, contains, add, and discard */
2374 elem = PySet_Pop(ob);
2375 assert(PySet_Contains(ob, elem) == 0);
2376 assert(PySet_GET_SIZE(ob) == 2);
2377 assert(PySet_Add(ob, elem) == 0);
2378 assert(PySet_Contains(ob, elem) == 1);
2379 assert(PySet_GET_SIZE(ob) == 3);
2380 assert(PySet_Discard(ob, elem) == 1);
2381 assert(PySet_GET_SIZE(ob) == 2);
2382 assert(PySet_Discard(ob, elem) == 0);
2383 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 /* Exercise clear */
2386 dup2 = PySet_New(dup);
2387 assert(PySet_Clear(dup2) == 0);
2388 assert(PySet_Size(dup2) == 0);
2389 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* Raise SystemError on clear or update of frozen set */
2392 f = PyFrozenSet_New(dup);
2393 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2394 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2395 assert(PySet_Add(f, elem) == 0);
2396 Py_INCREF(f);
2397 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2398 Py_DECREF(f);
2399 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* Exercise direct iteration */
2402 i = 0, count = 0;
2403 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2404 s = _PyUnicode_AsString(x);
2405 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2406 count++;
2407 }
2408 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 /* Exercise updates */
2411 dup2 = PySet_New(NULL);
2412 assert(_PySet_Update(dup2, dup) == 0);
2413 assert(PySet_Size(dup2) == 3);
2414 assert(_PySet_Update(dup2, dup) == 0);
2415 assert(PySet_Size(dup2) == 3);
2416 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 /* Raise SystemError when self argument is not a set or frozenset. */
2419 t = PyTuple_New(0);
2420 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2421 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2422 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* Raise SystemError when self argument is not a set. */
2425 f = PyFrozenSet_New(dup);
2426 assert(PySet_Size(f) == 3);
2427 assert(PyFrozenSet_CheckExact(f));
2428 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2429 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2430 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 /* Raise KeyError when popping from an empty set */
2433 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2434 Py_DECREF(ob);
2435 assert(PySet_GET_SIZE(ob) == 0);
2436 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 /* Restore the set from the copy using the PyNumber API */
2439 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2440 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 /* Verify constructors accept NULL arguments */
2443 f = PySet_New(NULL);
2444 assert(f != NULL);
2445 assert(PySet_GET_SIZE(f) == 0);
2446 Py_DECREF(f);
2447 f = PyFrozenSet_New(NULL);
2448 assert(f != NULL);
2449 assert(PyFrozenSet_CheckExact(f));
2450 assert(PySet_GET_SIZE(f) == 0);
2451 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 Py_DECREF(elem);
2454 Py_DECREF(dup);
2455 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002456}
2457
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002458#undef assertRaises
2459
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002460#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002461
2462/***** Dummy Struct *************************************************/
2463
2464static PyObject *
2465dummy_repr(PyObject *op)
2466{
2467 return PyUnicode_FromString("<dummy key>");
2468}
2469
2470static void
2471dummy_dealloc(PyObject* ignore)
2472{
2473 Py_FatalError("deallocating <dummy key>");
2474}
2475
2476static PyTypeObject _PySetDummy_Type = {
2477 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2478 "<dummy key> type",
2479 0,
2480 0,
2481 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2482 0, /*tp_print*/
2483 0, /*tp_getattr*/
2484 0, /*tp_setattr*/
2485 0, /*tp_reserved*/
2486 dummy_repr, /*tp_repr*/
2487 0, /*tp_as_number*/
2488 0, /*tp_as_sequence*/
2489 0, /*tp_as_mapping*/
2490 0, /*tp_hash */
2491 0, /*tp_call */
2492 0, /*tp_str */
2493 0, /*tp_getattro */
2494 0, /*tp_setattro */
2495 0, /*tp_as_buffer */
2496 Py_TPFLAGS_DEFAULT, /*tp_flags */
2497};
2498
2499static PyObject _dummy_struct = {
2500 _PyObject_EXTRA_INIT
2501 2, &_PySetDummy_Type
2502};
2503