blob: 8197cd914aded821e73292f74c9e79258df701f4 [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettingerbd9b2002015-01-18 16:10:30 -08007 Copyright (c) 2003-2015 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
Raymond Hettinger4d45c102015-01-25 16:27:40 -080026 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070027 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070055 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t perturb = hash;
58 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080059 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070060 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070061 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080063 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070064 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070066
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070067 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080068 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070069 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080070 /* startkey cannot be a dummy because the dummy hash field is -1 */
71 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080072 if (startkey == key)
73 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080074 if (PyUnicode_CheckExact(startkey)
75 && PyUnicode_CheckExact(key)
76 && unicode_eq(startkey, key))
77 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 Py_INCREF(startkey);
79 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
80 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010081 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010083 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010085 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070086 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060087 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070088 }
Raymond Hettinger438f9132015-02-09 06:48:29 -060089 if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -070090 freeslot = entry;
91
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++;
95 if (entry->key == NULL)
96 goto found_null;
97 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];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700126 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:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700130 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
175
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
Raymond Hettinger93035c42015-01-25 16:12:49 -0800186 entry = set_lookkey(so, key, hash);
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700187 if (entry == NULL)
188 return -1;
189 if (entry->key == NULL) {
190 /* UNUSED */
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700191 entry->key = key;
192 entry->hash = hash;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800193 so->fill++;
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700194 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);
Éric Araujo48049912011-03-23 02:08:07 +0100310 if (set_insert_key(so, key, hash) == -1) {
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;
348 if (entry->key == NULL || entry->key == dummy)
349 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;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100551 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200552 Py_ssize_t i;
553 setentry *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)
560 /* a.update(a) or a.update({}); nothing to do */
561 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 }
570 for (i = 0; i <= other->mask; i++) {
571 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000572 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100573 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000574 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500575 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000576 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100577 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000578 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 return -1;
580 }
581 }
582 }
583 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000584}
585
586static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000587set_contains_entry(PySetObject *so, setentry *entry)
588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 PyObject *key;
590 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000591
Raymond Hettinger93035c42015-01-25 16:12:49 -0800592 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (lu_entry == NULL)
594 return -1;
595 key = lu_entry->key;
596 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000597}
598
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800599static int
600set_contains_key(PySetObject *so, PyObject *key)
601{
602 setentry entry;
603 Py_hash_t hash;
604
605 if (!PyUnicode_CheckExact(key) ||
606 (hash = ((PyASCIIObject *) key)->hash) == -1) {
607 hash = PyObject_Hash(key);
608 if (hash == -1)
609 return -1;
610 }
611 entry.key = key;
612 entry.hash = hash;
613 return set_contains_entry(so, &entry);
614}
615
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000616static PyObject *
617set_pop(PySetObject *so)
618{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800619 /* Make sure the search finger is in bounds */
620 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200621 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 assert (PyAnySet_Check(so));
625 if (so->used == 0) {
626 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
627 return NULL;
628 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000629
Raymond Hettinger1202a472015-01-18 13:12:42 -0800630 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
631 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800632 if (i > so->mask)
633 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 }
635 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800637 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800639 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000641}
642
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000643PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
644Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000645
646static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000647set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 Py_ssize_t pos = 0;
650 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 while (set_next(so, &pos, &entry))
653 Py_VISIT(entry->key);
654 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000655}
656
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000657static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000658frozenset_hash(PyObject *self)
659{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800660 /* Most of the constants in this hash algorithm are randomly choosen
661 large primes with "interesting bit patterns" and that passed
662 tests for good collision statistics on a variety of problematic
663 datasets such as:
664
665 ps = []
666 for r in range(21):
667 ps += itertools.combinations(range(20), r)
668 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
669
670 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800672 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 setentry *entry;
674 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 if (so->hash != -1)
677 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000678
Mark Dickinson57e683e2011-09-24 18:18:40 +0100679 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 while (set_next(so, &pos, &entry)) {
681 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800682 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 combinations of a small number of elements with nearby
684 hashes so that many distinct combinations collapse to only
685 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000686 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800687 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800689 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500690 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800691 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200692 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800693 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 so->hash = hash;
695 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000696}
697
Raymond Hettingera9d99362005-08-05 00:01:15 +0000698/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000699
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000700typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 PyObject_HEAD
702 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
703 Py_ssize_t si_used;
704 Py_ssize_t si_pos;
705 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000706} setiterobject;
707
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000708static void
709setiter_dealloc(setiterobject *si)
710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 Py_XDECREF(si->si_set);
712 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000713}
714
715static int
716setiter_traverse(setiterobject *si, visitproc visit, void *arg)
717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 Py_VISIT(si->si_set);
719 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000720}
721
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000722static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000723setiter_len(setiterobject *si)
724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 Py_ssize_t len = 0;
726 if (si->si_set != NULL && si->si_used == si->si_set->used)
727 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000728 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000729}
730
Armin Rigof5b3e362006-02-11 21:32:43 +0000731PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000732
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000733static PyObject *setiter_iternext(setiterobject *si);
734
735static PyObject *
736setiter_reduce(setiterobject *si)
737{
738 PyObject *list;
739 setiterobject tmp;
740
741 list = PyList_New(0);
742 if (!list)
743 return NULL;
744
Ezio Melotti0e1af282012-09-28 16:43:40 +0300745 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000746 tmp = *si;
747 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300748
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000749 /* iterate the temporary into a list */
750 for(;;) {
751 PyObject *element = setiter_iternext(&tmp);
752 if (element) {
753 if (PyList_Append(list, element)) {
754 Py_DECREF(element);
755 Py_DECREF(list);
756 Py_XDECREF(tmp.si_set);
757 return NULL;
758 }
759 Py_DECREF(element);
760 } else
761 break;
762 }
763 Py_XDECREF(tmp.si_set);
764 /* check for error */
765 if (tmp.si_set != NULL) {
766 /* we have an error */
767 Py_DECREF(list);
768 return NULL;
769 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200770 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000771}
772
773PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
774
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000775static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000777 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000779};
780
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000781static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200784 Py_ssize_t i, mask;
785 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (so == NULL)
789 return NULL;
790 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 if (si->si_used != so->used) {
793 PyErr_SetString(PyExc_RuntimeError,
794 "Set changed size during iteration");
795 si->si_used = -1; /* Make this state sticky */
796 return NULL;
797 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 i = si->si_pos;
800 assert(i>=0);
801 entry = so->table;
802 mask = so->mask;
803 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
804 i++;
805 si->si_pos = i+1;
806 if (i > mask)
807 goto fail;
808 si->len--;
809 key = entry[i].key;
810 Py_INCREF(key);
811 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812
813fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 Py_DECREF(so);
815 si->si_set = NULL;
816 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000817}
818
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000819PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 PyVarObject_HEAD_INIT(&PyType_Type, 0)
821 "set_iterator", /* tp_name */
822 sizeof(setiterobject), /* tp_basicsize */
823 0, /* tp_itemsize */
824 /* methods */
825 (destructor)setiter_dealloc, /* tp_dealloc */
826 0, /* tp_print */
827 0, /* tp_getattr */
828 0, /* tp_setattr */
829 0, /* tp_reserved */
830 0, /* tp_repr */
831 0, /* tp_as_number */
832 0, /* tp_as_sequence */
833 0, /* tp_as_mapping */
834 0, /* tp_hash */
835 0, /* tp_call */
836 0, /* tp_str */
837 PyObject_GenericGetAttr, /* tp_getattro */
838 0, /* tp_setattro */
839 0, /* tp_as_buffer */
840 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
841 0, /* tp_doc */
842 (traverseproc)setiter_traverse, /* tp_traverse */
843 0, /* tp_clear */
844 0, /* tp_richcompare */
845 0, /* tp_weaklistoffset */
846 PyObject_SelfIter, /* tp_iter */
847 (iternextfunc)setiter_iternext, /* tp_iternext */
848 setiter_methods, /* tp_methods */
849 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000850};
851
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000852static PyObject *
853set_iter(PySetObject *so)
854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
856 if (si == NULL)
857 return NULL;
858 Py_INCREF(so);
859 si->si_set = so;
860 si->si_used = so->used;
861 si->si_pos = 0;
862 si->len = so->used;
863 _PyObject_GC_TRACK(si);
864 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865}
866
Raymond Hettingerd7946662005-08-01 21:39:29 +0000867static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000868set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 if (PyAnySet_Check(other))
873 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (PyDict_CheckExact(other)) {
876 PyObject *value;
877 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000878 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 /* Do one big resize at the start, rather than
882 * incrementally resizing as we insert new keys. Expect
883 * that there will be no (or few) overlapping keys.
884 */
885 if (dictsize == -1)
886 return -1;
887 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
888 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
889 return -1;
890 }
891 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
892 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 an_entry.hash = hash;
895 an_entry.key = key;
896 if (set_add_entry(so, &an_entry) == -1)
897 return -1;
898 }
899 return 0;
900 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 it = PyObject_GetIter(other);
903 if (it == NULL)
904 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 while ((key = PyIter_Next(it)) != NULL) {
907 if (set_add_key(so, key) == -1) {
908 Py_DECREF(it);
909 Py_DECREF(key);
910 return -1;
911 }
912 Py_DECREF(key);
913 }
914 Py_DECREF(it);
915 if (PyErr_Occurred())
916 return -1;
917 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000918}
919
920static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000921set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
926 PyObject *other = PyTuple_GET_ITEM(args, i);
927 if (set_update_internal(so, other) == -1)
928 return NULL;
929 }
930 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000931}
932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000934"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000935
Raymond Hettinger426d9952014-05-18 21:40:20 +0100936/* XXX Todo:
937 If aligned memory allocations become available, make the
938 set object 64 byte aligned so that most of the fields
939 can be retrieved or updated in a single cache line.
940*/
941
Raymond Hettingera38123e2003-11-24 22:18:49 +0000942static PyObject *
943make_new_set(PyTypeObject *type, PyObject *iterable)
944{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200945 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700948 so = (PySetObject *)type->tp_alloc(type, 0);
949 if (so == NULL)
950 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000951
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700952 so->fill = 0;
953 so->used = 0;
954 so->mask = PySet_MINSIZE - 1;
955 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700956 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800957 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 if (iterable != NULL) {
961 if (set_update_internal(so, iterable) == -1) {
962 Py_DECREF(so);
963 return NULL;
964 }
965 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +0000968}
969
Raymond Hettinger7d99f092008-11-16 11:44:54 +0000970static PyObject *
971make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
974 if (PyType_IsSubtype(type, &PySet_Type))
975 type = &PySet_Type;
976 else
977 type = &PyFrozenSet_Type;
978 }
979 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +0000980}
981
Raymond Hettingerd7946662005-08-01 21:39:29 +0000982/* The empty frozenset is a singleton */
983static PyObject *emptyfrozenset = NULL;
984
Raymond Hettingera690a992003-11-16 16:17:49 +0000985static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000986frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
991 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
994 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 if (type != &PyFrozenSet_Type)
997 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (iterable != NULL) {
1000 /* frozenset(f) is idempotent */
1001 if (PyFrozenSet_CheckExact(iterable)) {
1002 Py_INCREF(iterable);
1003 return iterable;
1004 }
1005 result = make_new_set(type, iterable);
1006 if (result == NULL || PySet_GET_SIZE(result))
1007 return result;
1008 Py_DECREF(result);
1009 }
1010 /* The empty frozenset is a singleton */
1011 if (emptyfrozenset == NULL)
1012 emptyfrozenset = make_new_set(type, NULL);
1013 Py_XINCREF(emptyfrozenset);
1014 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001015}
1016
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001017int
1018PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001019{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001020 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001021}
1022
1023void
1024PySet_Fini(void)
1025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001027}
1028
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001029static PyObject *
1030set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1033 return NULL;
1034
1035 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001036}
1037
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001038/* set_swap_bodies() switches the contents of any two sets by moving their
1039 internal data pointers and, if needed, copying the internal smalltables.
1040 Semantically equivalent to:
1041
1042 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1043
1044 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001045 Useful for operations that update in-place (by allowing an intermediate
1046 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001047*/
1048
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001049static void
1050set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 Py_ssize_t t;
1053 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001055 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 t = a->fill; a->fill = b->fill; b->fill = t;
1058 t = a->used; a->used = b->used; b->used = t;
1059 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 u = a->table;
1062 if (a->table == a->smalltable)
1063 u = b->smalltable;
1064 a->table = b->table;
1065 if (b->table == b->smalltable)
1066 a->table = a->smalltable;
1067 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 if (a->table == a->smalltable || b->table == b->smalltable) {
1070 memcpy(tab, a->smalltable, sizeof(tab));
1071 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1072 memcpy(b->smalltable, tab, sizeof(tab));
1073 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1076 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1077 h = a->hash; a->hash = b->hash; b->hash = h;
1078 } else {
1079 a->hash = -1;
1080 b->hash = -1;
1081 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001082}
1083
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001084static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001085set_copy(PySetObject *so)
1086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001088}
1089
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001090static PyObject *
1091frozenset_copy(PySetObject *so)
1092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (PyFrozenSet_CheckExact(so)) {
1094 Py_INCREF(so);
1095 return (PyObject *)so;
1096 }
1097 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001098}
1099
Raymond Hettingera690a992003-11-16 16:17:49 +00001100PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1101
1102static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001103set_clear(PySetObject *so)
1104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 set_clear_internal(so);
1106 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001107}
1108
1109PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1110
1111static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001112set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 PySetObject *result;
1115 PyObject *other;
1116 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 result = (PySetObject *)set_copy(so);
1119 if (result == NULL)
1120 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1123 other = PyTuple_GET_ITEM(args, i);
1124 if ((PyObject *)so == other)
1125 continue;
1126 if (set_update_internal(result, other) == -1) {
1127 Py_DECREF(result);
1128 return NULL;
1129 }
1130 }
1131 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001132}
1133
1134PyDoc_STRVAR(union_doc,
1135 "Return the union of sets as a new set.\n\
1136\n\
1137(i.e. all elements that are in either set.)");
1138
1139static PyObject *
1140set_or(PySetObject *so, PyObject *other)
1141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001143
Brian Curtindfc80e32011-08-10 20:28:54 -05001144 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1145 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 result = (PySetObject *)set_copy(so);
1148 if (result == NULL)
1149 return NULL;
1150 if ((PyObject *)so == other)
1151 return (PyObject *)result;
1152 if (set_update_internal(result, other) == -1) {
1153 Py_DECREF(result);
1154 return NULL;
1155 }
1156 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001157}
1158
Raymond Hettingera690a992003-11-16 16:17:49 +00001159static PyObject *
1160set_ior(PySetObject *so, PyObject *other)
1161{
Brian Curtindfc80e32011-08-10 20:28:54 -05001162 if (!PyAnySet_Check(other))
1163 Py_RETURN_NOTIMPLEMENTED;
1164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 if (set_update_internal(so, other) == -1)
1166 return NULL;
1167 Py_INCREF(so);
1168 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001169}
1170
1171static PyObject *
1172set_intersection(PySetObject *so, PyObject *other)
1173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 PySetObject *result;
1175 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 if ((PyObject *)so == other)
1178 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1181 if (result == NULL)
1182 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 if (PyAnySet_Check(other)) {
1185 Py_ssize_t pos = 0;
1186 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1189 tmp = (PyObject *)so;
1190 so = (PySetObject *)other;
1191 other = tmp;
1192 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 while (set_next((PySetObject *)other, &pos, &entry)) {
1195 int rv = set_contains_entry(so, entry);
1196 if (rv == -1) {
1197 Py_DECREF(result);
1198 return NULL;
1199 }
1200 if (rv) {
1201 if (set_add_entry(result, entry) == -1) {
1202 Py_DECREF(result);
1203 return NULL;
1204 }
1205 }
1206 }
1207 return (PyObject *)result;
1208 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 it = PyObject_GetIter(other);
1211 if (it == NULL) {
1212 Py_DECREF(result);
1213 return NULL;
1214 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 while ((key = PyIter_Next(it)) != NULL) {
1217 int rv;
1218 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001219 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 if (hash == -1) {
1222 Py_DECREF(it);
1223 Py_DECREF(result);
1224 Py_DECREF(key);
1225 return NULL;
1226 }
1227 entry.hash = hash;
1228 entry.key = key;
1229 rv = set_contains_entry(so, &entry);
1230 if (rv == -1) {
1231 Py_DECREF(it);
1232 Py_DECREF(result);
1233 Py_DECREF(key);
1234 return NULL;
1235 }
1236 if (rv) {
1237 if (set_add_entry(result, &entry) == -1) {
1238 Py_DECREF(it);
1239 Py_DECREF(result);
1240 Py_DECREF(key);
1241 return NULL;
1242 }
1243 }
1244 Py_DECREF(key);
1245 }
1246 Py_DECREF(it);
1247 if (PyErr_Occurred()) {
1248 Py_DECREF(result);
1249 return NULL;
1250 }
1251 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001252}
1253
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001254static PyObject *
1255set_intersection_multi(PySetObject *so, PyObject *args)
1256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 Py_ssize_t i;
1258 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (PyTuple_GET_SIZE(args) == 0)
1261 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 Py_INCREF(so);
1264 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1265 PyObject *other = PyTuple_GET_ITEM(args, i);
1266 PyObject *newresult = set_intersection((PySetObject *)result, other);
1267 if (newresult == NULL) {
1268 Py_DECREF(result);
1269 return NULL;
1270 }
1271 Py_DECREF(result);
1272 result = newresult;
1273 }
1274 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001275}
1276
Raymond Hettingera690a992003-11-16 16:17:49 +00001277PyDoc_STRVAR(intersection_doc,
1278"Return the intersection of two sets as a new set.\n\
1279\n\
1280(i.e. all elements that are in both sets.)");
1281
1282static PyObject *
1283set_intersection_update(PySetObject *so, PyObject *other)
1284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 tmp = set_intersection(so, other);
1288 if (tmp == NULL)
1289 return NULL;
1290 set_swap_bodies(so, (PySetObject *)tmp);
1291 Py_DECREF(tmp);
1292 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001293}
1294
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001295static PyObject *
1296set_intersection_update_multi(PySetObject *so, PyObject *args)
1297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 tmp = set_intersection_multi(so, args);
1301 if (tmp == NULL)
1302 return NULL;
1303 set_swap_bodies(so, (PySetObject *)tmp);
1304 Py_DECREF(tmp);
1305 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001306}
1307
Raymond Hettingera690a992003-11-16 16:17:49 +00001308PyDoc_STRVAR(intersection_update_doc,
1309"Update a set with the intersection of itself and another.");
1310
1311static PyObject *
1312set_and(PySetObject *so, PyObject *other)
1313{
Brian Curtindfc80e32011-08-10 20:28:54 -05001314 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1315 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001317}
1318
1319static PyObject *
1320set_iand(PySetObject *so, PyObject *other)
1321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001323
Brian Curtindfc80e32011-08-10 20:28:54 -05001324 if (!PyAnySet_Check(other))
1325 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 result = set_intersection_update(so, other);
1327 if (result == NULL)
1328 return NULL;
1329 Py_DECREF(result);
1330 Py_INCREF(so);
1331 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001332}
1333
Guido van Rossum58da9312007-11-10 23:39:45 +00001334static PyObject *
1335set_isdisjoint(PySetObject *so, PyObject *other)
1336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if ((PyObject *)so == other) {
1340 if (PySet_GET_SIZE(so) == 0)
1341 Py_RETURN_TRUE;
1342 else
1343 Py_RETURN_FALSE;
1344 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (PyAnySet_CheckExact(other)) {
1347 Py_ssize_t pos = 0;
1348 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1351 tmp = (PyObject *)so;
1352 so = (PySetObject *)other;
1353 other = tmp;
1354 }
1355 while (set_next((PySetObject *)other, &pos, &entry)) {
1356 int rv = set_contains_entry(so, entry);
1357 if (rv == -1)
1358 return NULL;
1359 if (rv)
1360 Py_RETURN_FALSE;
1361 }
1362 Py_RETURN_TRUE;
1363 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 it = PyObject_GetIter(other);
1366 if (it == NULL)
1367 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 while ((key = PyIter_Next(it)) != NULL) {
1370 int rv;
1371 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001372 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (hash == -1) {
1375 Py_DECREF(key);
1376 Py_DECREF(it);
1377 return NULL;
1378 }
1379 entry.hash = hash;
1380 entry.key = key;
1381 rv = set_contains_entry(so, &entry);
1382 Py_DECREF(key);
1383 if (rv == -1) {
1384 Py_DECREF(it);
1385 return NULL;
1386 }
1387 if (rv) {
1388 Py_DECREF(it);
1389 Py_RETURN_FALSE;
1390 }
1391 }
1392 Py_DECREF(it);
1393 if (PyErr_Occurred())
1394 return NULL;
1395 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001396}
1397
1398PyDoc_STRVAR(isdisjoint_doc,
1399"Return True if two sets have a null intersection.");
1400
Neal Norwitz6576bd82005-11-13 18:41:28 +00001401static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001402set_difference_update_internal(PySetObject *so, PyObject *other)
1403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 if ((PyObject *)so == other)
1405 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 if (PyAnySet_Check(other)) {
1408 setentry *entry;
1409 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 while (set_next((PySetObject *)other, &pos, &entry))
1412 if (set_discard_entry(so, entry) == -1)
1413 return -1;
1414 } else {
1415 PyObject *key, *it;
1416 it = PyObject_GetIter(other);
1417 if (it == NULL)
1418 return -1;
1419
1420 while ((key = PyIter_Next(it)) != NULL) {
1421 if (set_discard_key(so, key) == -1) {
1422 Py_DECREF(it);
1423 Py_DECREF(key);
1424 return -1;
1425 }
1426 Py_DECREF(key);
1427 }
1428 Py_DECREF(it);
1429 if (PyErr_Occurred())
1430 return -1;
1431 }
1432 /* If more than 1/5 are dummies, then resize them away. */
1433 if ((so->fill - so->used) * 5 < so->mask)
1434 return 0;
1435 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001436}
1437
Raymond Hettingera690a992003-11-16 16:17:49 +00001438static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001439set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1444 PyObject *other = PyTuple_GET_ITEM(args, i);
1445 if (set_difference_update_internal(so, other) == -1)
1446 return NULL;
1447 }
1448 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001449}
1450
1451PyDoc_STRVAR(difference_update_doc,
1452"Remove all elements of another set from this set.");
1453
1454static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001455set_copy_and_difference(PySetObject *so, PyObject *other)
1456{
1457 PyObject *result;
1458
1459 result = set_copy(so);
1460 if (result == NULL)
1461 return NULL;
1462 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1463 return result;
1464 Py_DECREF(result);
1465 return NULL;
1466}
1467
1468static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001469set_difference(PySetObject *so, PyObject *other)
1470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 PyObject *result;
1472 setentry *entry;
1473 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001476 return set_copy_and_difference(so, other);
1477 }
1478
1479 /* If len(so) much more than len(other), it's more efficient to simply copy
1480 * so and then iterate other looking for common elements. */
1481 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1482 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 result = make_new_set_basetype(Py_TYPE(so), NULL);
1486 if (result == NULL)
1487 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 if (PyDict_CheckExact(other)) {
1490 while (set_next(so, &pos, &entry)) {
1491 setentry entrycopy;
1492 entrycopy.hash = entry->hash;
1493 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001494 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1496 Py_DECREF(result);
1497 return NULL;
1498 }
1499 }
1500 }
1501 return result;
1502 }
1503
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001504 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 while (set_next(so, &pos, &entry)) {
1506 int rv = set_contains_entry((PySetObject *)other, entry);
1507 if (rv == -1) {
1508 Py_DECREF(result);
1509 return NULL;
1510 }
1511 if (!rv) {
1512 if (set_add_entry((PySetObject *)result, entry) == -1) {
1513 Py_DECREF(result);
1514 return NULL;
1515 }
1516 }
1517 }
1518 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001519}
1520
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001521static PyObject *
1522set_difference_multi(PySetObject *so, PyObject *args)
1523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 Py_ssize_t i;
1525 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 if (PyTuple_GET_SIZE(args) == 0)
1528 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 other = PyTuple_GET_ITEM(args, 0);
1531 result = set_difference(so, other);
1532 if (result == NULL)
1533 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1536 other = PyTuple_GET_ITEM(args, i);
1537 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1538 Py_DECREF(result);
1539 return NULL;
1540 }
1541 }
1542 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001543}
1544
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001545PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001546"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001547\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001548(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001549static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001550set_sub(PySetObject *so, PyObject *other)
1551{
Brian Curtindfc80e32011-08-10 20:28:54 -05001552 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1553 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001555}
1556
1557static PyObject *
1558set_isub(PySetObject *so, PyObject *other)
1559{
Brian Curtindfc80e32011-08-10 20:28:54 -05001560 if (!PyAnySet_Check(other))
1561 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (set_difference_update_internal(so, other) == -1)
1563 return NULL;
1564 Py_INCREF(so);
1565 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001566}
1567
1568static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001569set_symmetric_difference_update(PySetObject *so, PyObject *other)
1570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 PySetObject *otherset;
1572 PyObject *key;
1573 Py_ssize_t pos = 0;
1574 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 if ((PyObject *)so == other)
1577 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 if (PyDict_CheckExact(other)) {
1580 PyObject *value;
1581 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001582 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1584 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001585
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001586 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 an_entry.hash = hash;
1588 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001591 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001592 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001595 if (rv == DISCARD_NOTFOUND) {
1596 if (set_add_entry(so, &an_entry) == -1) {
1597 Py_DECREF(key);
1598 return NULL;
1599 }
1600 }
Georg Brandl2d444492010-09-03 10:52:55 +00001601 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 }
1603 Py_RETURN_NONE;
1604 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if (PyAnySet_Check(other)) {
1607 Py_INCREF(other);
1608 otherset = (PySetObject *)other;
1609 } else {
1610 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1611 if (otherset == NULL)
1612 return NULL;
1613 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 while (set_next(otherset, &pos, &entry)) {
1616 int rv = set_discard_entry(so, entry);
1617 if (rv == -1) {
1618 Py_DECREF(otherset);
1619 return NULL;
1620 }
1621 if (rv == DISCARD_NOTFOUND) {
1622 if (set_add_entry(so, entry) == -1) {
1623 Py_DECREF(otherset);
1624 return NULL;
1625 }
1626 }
1627 }
1628 Py_DECREF(otherset);
1629 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001630}
1631
1632PyDoc_STRVAR(symmetric_difference_update_doc,
1633"Update a set with the symmetric difference of itself and another.");
1634
1635static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001636set_symmetric_difference(PySetObject *so, PyObject *other)
1637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 PyObject *rv;
1639 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1642 if (otherset == NULL)
1643 return NULL;
1644 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1645 if (rv == NULL)
1646 return NULL;
1647 Py_DECREF(rv);
1648 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001649}
1650
1651PyDoc_STRVAR(symmetric_difference_doc,
1652"Return the symmetric difference of two sets as a new set.\n\
1653\n\
1654(i.e. all elements that are in exactly one of the sets.)");
1655
1656static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001657set_xor(PySetObject *so, PyObject *other)
1658{
Brian Curtindfc80e32011-08-10 20:28:54 -05001659 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1660 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001662}
1663
1664static PyObject *
1665set_ixor(PySetObject *so, PyObject *other)
1666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001668
Brian Curtindfc80e32011-08-10 20:28:54 -05001669 if (!PyAnySet_Check(other))
1670 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 result = set_symmetric_difference_update(so, other);
1672 if (result == NULL)
1673 return NULL;
1674 Py_DECREF(result);
1675 Py_INCREF(so);
1676 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001677}
1678
1679static PyObject *
1680set_issubset(PySetObject *so, PyObject *other)
1681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 setentry *entry;
1683 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 if (!PyAnySet_Check(other)) {
1686 PyObject *tmp, *result;
1687 tmp = make_new_set(&PySet_Type, other);
1688 if (tmp == NULL)
1689 return NULL;
1690 result = set_issubset(so, tmp);
1691 Py_DECREF(tmp);
1692 return result;
1693 }
1694 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1695 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 while (set_next(so, &pos, &entry)) {
1698 int rv = set_contains_entry((PySetObject *)other, entry);
1699 if (rv == -1)
1700 return NULL;
1701 if (!rv)
1702 Py_RETURN_FALSE;
1703 }
1704 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001705}
1706
1707PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1708
1709static PyObject *
1710set_issuperset(PySetObject *so, PyObject *other)
1711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 if (!PyAnySet_Check(other)) {
1715 tmp = make_new_set(&PySet_Type, other);
1716 if (tmp == NULL)
1717 return NULL;
1718 result = set_issuperset(so, tmp);
1719 Py_DECREF(tmp);
1720 return result;
1721 }
1722 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001723}
1724
1725PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1726
Raymond Hettingera690a992003-11-16 16:17:49 +00001727static PyObject *
1728set_richcompare(PySetObject *v, PyObject *w, int op)
1729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001731
Brian Curtindfc80e32011-08-10 20:28:54 -05001732 if(!PyAnySet_Check(w))
1733 Py_RETURN_NOTIMPLEMENTED;
1734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 switch (op) {
1736 case Py_EQ:
1737 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1738 Py_RETURN_FALSE;
1739 if (v->hash != -1 &&
1740 ((PySetObject *)w)->hash != -1 &&
1741 v->hash != ((PySetObject *)w)->hash)
1742 Py_RETURN_FALSE;
1743 return set_issubset(v, w);
1744 case Py_NE:
1745 r1 = set_richcompare(v, w, Py_EQ);
1746 if (r1 == NULL)
1747 return NULL;
1748 r2 = PyBool_FromLong(PyObject_Not(r1));
1749 Py_DECREF(r1);
1750 return r2;
1751 case Py_LE:
1752 return set_issubset(v, w);
1753 case Py_GE:
1754 return set_issuperset(v, w);
1755 case Py_LT:
1756 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1757 Py_RETURN_FALSE;
1758 return set_issubset(v, w);
1759 case Py_GT:
1760 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1761 Py_RETURN_FALSE;
1762 return set_issuperset(v, w);
1763 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001764 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001765}
1766
Raymond Hettingera690a992003-11-16 16:17:49 +00001767static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001768set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 if (set_add_key(so, key) == -1)
1771 return NULL;
1772 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001773}
1774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001776"Add an element to a set.\n\
1777\n\
1778This has no effect if the element is already present.");
1779
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001780static int
1781set_contains(PySetObject *so, PyObject *key)
1782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 PyObject *tmpkey;
1784 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 rv = set_contains_key(so, key);
1787 if (rv == -1) {
1788 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1789 return -1;
1790 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001791 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 if (tmpkey == NULL)
1793 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001794 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 Py_DECREF(tmpkey);
1796 }
1797 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001798}
1799
1800static PyObject *
1801set_direct_contains(PySetObject *so, PyObject *key)
1802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 result = set_contains(so, key);
1806 if (result == -1)
1807 return NULL;
1808 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001809}
1810
1811PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1812
Raymond Hettingera690a992003-11-16 16:17:49 +00001813static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001814set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 PyObject *tmpkey;
1817 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 rv = set_discard_key(so, key);
1820 if (rv == -1) {
1821 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1822 return NULL;
1823 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001824 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 if (tmpkey == NULL)
1826 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 Py_DECREF(tmpkey);
1829 if (rv == -1)
1830 return NULL;
1831 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001834 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 return NULL;
1836 }
1837 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001838}
1839
1840PyDoc_STRVAR(remove_doc,
1841"Remove an element from a set; it must be a member.\n\
1842\n\
1843If the element is not a member, raise a KeyError.");
1844
1845static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001846set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001847{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001848 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 rv = set_discard_key(so, key);
1852 if (rv == -1) {
1853 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1854 return NULL;
1855 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001856 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 if (tmpkey == NULL)
1858 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001859 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001861 if (rv == -1)
1862 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 }
1864 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001865}
1866
1867PyDoc_STRVAR(discard_doc,
1868"Remove an element from a set if it is a member.\n\
1869\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001871
1872static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001873set_reduce(PySetObject *so)
1874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001876 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 keys = PySequence_List((PyObject *)so);
1879 if (keys == NULL)
1880 goto done;
1881 args = PyTuple_Pack(1, keys);
1882 if (args == NULL)
1883 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001884 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 if (dict == NULL) {
1886 PyErr_Clear();
1887 dict = Py_None;
1888 Py_INCREF(dict);
1889 }
1890 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001891done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 Py_XDECREF(args);
1893 Py_XDECREF(keys);
1894 Py_XDECREF(dict);
1895 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001896}
1897
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001898static PyObject *
1899set_sizeof(PySetObject *so)
1900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 res = sizeof(PySetObject);
1904 if (so->table != so->smalltable)
1905 res = res + (so->mask + 1) * sizeof(setentry);
1906 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001907}
1908
1909PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001910static int
1911set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (!PyAnySet_Check(self))
1916 return -1;
1917 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1918 return -1;
1919 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1920 return -1;
1921 set_clear_internal(self);
1922 self->hash = -1;
1923 if (iterable == NULL)
1924 return 0;
1925 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001926}
1927
Raymond Hettingera690a992003-11-16 16:17:49 +00001928static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 set_len, /* sq_length */
1930 0, /* sq_concat */
1931 0, /* sq_repeat */
1932 0, /* sq_item */
1933 0, /* sq_slice */
1934 0, /* sq_ass_item */
1935 0, /* sq_ass_slice */
1936 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001937};
1938
1939/* set object ********************************************************/
1940
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001941#ifdef Py_DEBUG
1942static PyObject *test_c_api(PySetObject *so);
1943
1944PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1945All is well if assertions don't fail.");
1946#endif
1947
Raymond Hettingera690a992003-11-16 16:17:49 +00001948static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 {"add", (PyCFunction)set_add, METH_O,
1950 add_doc},
1951 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1952 clear_doc},
1953 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1954 contains_doc},
1955 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1956 copy_doc},
1957 {"discard", (PyCFunction)set_discard, METH_O,
1958 discard_doc},
1959 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
1960 difference_doc},
1961 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
1962 difference_update_doc},
1963 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
1964 intersection_doc},
1965 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
1966 intersection_update_doc},
1967 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1968 isdisjoint_doc},
1969 {"issubset", (PyCFunction)set_issubset, METH_O,
1970 issubset_doc},
1971 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1972 issuperset_doc},
1973 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1974 pop_doc},
1975 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1976 reduce_doc},
1977 {"remove", (PyCFunction)set_remove, METH_O,
1978 remove_doc},
1979 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
1980 sizeof_doc},
1981 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1982 symmetric_difference_doc},
1983 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1984 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001985#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1987 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001988#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 {"union", (PyCFunction)set_union, METH_VARARGS,
1990 union_doc},
1991 {"update", (PyCFunction)set_update, METH_VARARGS,
1992 update_doc},
1993 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00001994};
1995
1996static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 0, /*nb_add*/
1998 (binaryfunc)set_sub, /*nb_subtract*/
1999 0, /*nb_multiply*/
2000 0, /*nb_remainder*/
2001 0, /*nb_divmod*/
2002 0, /*nb_power*/
2003 0, /*nb_negative*/
2004 0, /*nb_positive*/
2005 0, /*nb_absolute*/
2006 0, /*nb_bool*/
2007 0, /*nb_invert*/
2008 0, /*nb_lshift*/
2009 0, /*nb_rshift*/
2010 (binaryfunc)set_and, /*nb_and*/
2011 (binaryfunc)set_xor, /*nb_xor*/
2012 (binaryfunc)set_or, /*nb_or*/
2013 0, /*nb_int*/
2014 0, /*nb_reserved*/
2015 0, /*nb_float*/
2016 0, /*nb_inplace_add*/
2017 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2018 0, /*nb_inplace_multiply*/
2019 0, /*nb_inplace_remainder*/
2020 0, /*nb_inplace_power*/
2021 0, /*nb_inplace_lshift*/
2022 0, /*nb_inplace_rshift*/
2023 (binaryfunc)set_iand, /*nb_inplace_and*/
2024 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2025 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002026};
2027
2028PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002029"set() -> new empty set object\n\
2030set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002031\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002032Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002033
2034PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2036 "set", /* tp_name */
2037 sizeof(PySetObject), /* tp_basicsize */
2038 0, /* tp_itemsize */
2039 /* methods */
2040 (destructor)set_dealloc, /* tp_dealloc */
2041 0, /* tp_print */
2042 0, /* tp_getattr */
2043 0, /* tp_setattr */
2044 0, /* tp_reserved */
2045 (reprfunc)set_repr, /* tp_repr */
2046 &set_as_number, /* tp_as_number */
2047 &set_as_sequence, /* tp_as_sequence */
2048 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002049 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 0, /* tp_call */
2051 0, /* tp_str */
2052 PyObject_GenericGetAttr, /* tp_getattro */
2053 0, /* tp_setattro */
2054 0, /* tp_as_buffer */
2055 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2056 Py_TPFLAGS_BASETYPE, /* tp_flags */
2057 set_doc, /* tp_doc */
2058 (traverseproc)set_traverse, /* tp_traverse */
2059 (inquiry)set_clear_internal, /* tp_clear */
2060 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002061 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2062 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 0, /* tp_iternext */
2064 set_methods, /* tp_methods */
2065 0, /* tp_members */
2066 0, /* tp_getset */
2067 0, /* tp_base */
2068 0, /* tp_dict */
2069 0, /* tp_descr_get */
2070 0, /* tp_descr_set */
2071 0, /* tp_dictoffset */
2072 (initproc)set_init, /* tp_init */
2073 PyType_GenericAlloc, /* tp_alloc */
2074 set_new, /* tp_new */
2075 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002076};
2077
2078/* frozenset object ********************************************************/
2079
2080
2081static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2083 contains_doc},
2084 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2085 copy_doc},
2086 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2087 difference_doc},
2088 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2089 intersection_doc},
2090 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2091 isdisjoint_doc},
2092 {"issubset", (PyCFunction)set_issubset, METH_O,
2093 issubset_doc},
2094 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2095 issuperset_doc},
2096 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2097 reduce_doc},
2098 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2099 sizeof_doc},
2100 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2101 symmetric_difference_doc},
2102 {"union", (PyCFunction)set_union, METH_VARARGS,
2103 union_doc},
2104 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002105};
2106
2107static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 0, /*nb_add*/
2109 (binaryfunc)set_sub, /*nb_subtract*/
2110 0, /*nb_multiply*/
2111 0, /*nb_remainder*/
2112 0, /*nb_divmod*/
2113 0, /*nb_power*/
2114 0, /*nb_negative*/
2115 0, /*nb_positive*/
2116 0, /*nb_absolute*/
2117 0, /*nb_bool*/
2118 0, /*nb_invert*/
2119 0, /*nb_lshift*/
2120 0, /*nb_rshift*/
2121 (binaryfunc)set_and, /*nb_and*/
2122 (binaryfunc)set_xor, /*nb_xor*/
2123 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002124};
2125
2126PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002127"frozenset() -> empty frozenset object\n\
2128frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002129\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002130Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002131
2132PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2134 "frozenset", /* tp_name */
2135 sizeof(PySetObject), /* tp_basicsize */
2136 0, /* tp_itemsize */
2137 /* methods */
2138 (destructor)set_dealloc, /* tp_dealloc */
2139 0, /* tp_print */
2140 0, /* tp_getattr */
2141 0, /* tp_setattr */
2142 0, /* tp_reserved */
2143 (reprfunc)set_repr, /* tp_repr */
2144 &frozenset_as_number, /* tp_as_number */
2145 &set_as_sequence, /* tp_as_sequence */
2146 0, /* tp_as_mapping */
2147 frozenset_hash, /* tp_hash */
2148 0, /* tp_call */
2149 0, /* tp_str */
2150 PyObject_GenericGetAttr, /* tp_getattro */
2151 0, /* tp_setattro */
2152 0, /* tp_as_buffer */
2153 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2154 Py_TPFLAGS_BASETYPE, /* tp_flags */
2155 frozenset_doc, /* tp_doc */
2156 (traverseproc)set_traverse, /* tp_traverse */
2157 (inquiry)set_clear_internal, /* tp_clear */
2158 (richcmpfunc)set_richcompare, /* tp_richcompare */
2159 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2160 (getiterfunc)set_iter, /* tp_iter */
2161 0, /* tp_iternext */
2162 frozenset_methods, /* tp_methods */
2163 0, /* tp_members */
2164 0, /* tp_getset */
2165 0, /* tp_base */
2166 0, /* tp_dict */
2167 0, /* tp_descr_get */
2168 0, /* tp_descr_set */
2169 0, /* tp_dictoffset */
2170 0, /* tp_init */
2171 PyType_GenericAlloc, /* tp_alloc */
2172 frozenset_new, /* tp_new */
2173 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002174};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002175
2176
2177/***** C API functions *************************************************/
2178
2179PyObject *
2180PySet_New(PyObject *iterable)
2181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002183}
2184
2185PyObject *
2186PyFrozenSet_New(PyObject *iterable)
2187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002189}
2190
Neal Norwitz8c49c822006-03-04 18:41:19 +00002191Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002192PySet_Size(PyObject *anyset)
2193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 if (!PyAnySet_Check(anyset)) {
2195 PyErr_BadInternalCall();
2196 return -1;
2197 }
2198 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002199}
2200
2201int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002202PySet_Clear(PyObject *set)
2203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 if (!PySet_Check(set)) {
2205 PyErr_BadInternalCall();
2206 return -1;
2207 }
2208 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002209}
2210
2211int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002212PySet_Contains(PyObject *anyset, PyObject *key)
2213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 if (!PyAnySet_Check(anyset)) {
2215 PyErr_BadInternalCall();
2216 return -1;
2217 }
2218 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002219}
2220
2221int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002222PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 if (!PySet_Check(set)) {
2225 PyErr_BadInternalCall();
2226 return -1;
2227 }
2228 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002229}
2230
2231int
Christian Heimesfd66e512008-01-29 12:18:50 +00002232PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 if (!PySet_Check(anyset) &&
2235 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2236 PyErr_BadInternalCall();
2237 return -1;
2238 }
2239 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002240}
2241
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002242int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002243_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 if (!PyAnySet_Check(set)) {
2248 PyErr_BadInternalCall();
2249 return -1;
2250 }
2251 if (set_next((PySetObject *)set, pos, &entry) == 0)
2252 return 0;
2253 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002254 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002256}
2257
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002258PyObject *
2259PySet_Pop(PyObject *set)
2260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 if (!PySet_Check(set)) {
2262 PyErr_BadInternalCall();
2263 return NULL;
2264 }
2265 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002266}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002267
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002268int
2269_PySet_Update(PyObject *set, PyObject *iterable)
2270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 if (!PySet_Check(set)) {
2272 PyErr_BadInternalCall();
2273 return -1;
2274 }
2275 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002276}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002277
Raymond Hettingere259f132013-12-15 11:56:14 -08002278/* Exported for the gdb plugin's benefit. */
2279PyObject *_PySet_Dummy = dummy;
2280
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002281#ifdef Py_DEBUG
2282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002284 Returns True and original set is restored. */
2285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286#define assertRaises(call_return_value, exception) \
2287 do { \
2288 assert(call_return_value); \
2289 assert(PyErr_ExceptionMatches(exception)); \
2290 PyErr_Clear(); \
2291 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002292
2293static PyObject *
2294test_c_api(PySetObject *so)
2295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 Py_ssize_t count;
2297 char *s;
2298 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002299 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002301 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 /* Verify preconditions */
2305 assert(PyAnySet_Check(ob));
2306 assert(PyAnySet_CheckExact(ob));
2307 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 /* so.clear(); so |= set("abc"); */
2310 str = PyUnicode_FromString("abc");
2311 if (str == NULL)
2312 return NULL;
2313 set_clear_internal(so);
2314 if (set_update_internal(so, str) == -1) {
2315 Py_DECREF(str);
2316 return NULL;
2317 }
2318 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 /* Exercise type/size checks */
2321 assert(PySet_Size(ob) == 3);
2322 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 /* Raise TypeError for non-iterable constructor arguments */
2325 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2326 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 /* Raise TypeError for unhashable key */
2329 dup = PySet_New(ob);
2330 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2331 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2332 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 /* Exercise successful pop, contains, add, and discard */
2335 elem = PySet_Pop(ob);
2336 assert(PySet_Contains(ob, elem) == 0);
2337 assert(PySet_GET_SIZE(ob) == 2);
2338 assert(PySet_Add(ob, elem) == 0);
2339 assert(PySet_Contains(ob, elem) == 1);
2340 assert(PySet_GET_SIZE(ob) == 3);
2341 assert(PySet_Discard(ob, elem) == 1);
2342 assert(PySet_GET_SIZE(ob) == 2);
2343 assert(PySet_Discard(ob, elem) == 0);
2344 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 /* Exercise clear */
2347 dup2 = PySet_New(dup);
2348 assert(PySet_Clear(dup2) == 0);
2349 assert(PySet_Size(dup2) == 0);
2350 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 /* Raise SystemError on clear or update of frozen set */
2353 f = PyFrozenSet_New(dup);
2354 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2355 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2356 assert(PySet_Add(f, elem) == 0);
2357 Py_INCREF(f);
2358 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2359 Py_DECREF(f);
2360 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 /* Exercise direct iteration */
2363 i = 0, count = 0;
2364 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2365 s = _PyUnicode_AsString(x);
2366 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2367 count++;
2368 }
2369 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 /* Exercise updates */
2372 dup2 = PySet_New(NULL);
2373 assert(_PySet_Update(dup2, dup) == 0);
2374 assert(PySet_Size(dup2) == 3);
2375 assert(_PySet_Update(dup2, dup) == 0);
2376 assert(PySet_Size(dup2) == 3);
2377 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 /* Raise SystemError when self argument is not a set or frozenset. */
2380 t = PyTuple_New(0);
2381 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2382 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2383 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 /* Raise SystemError when self argument is not a set. */
2386 f = PyFrozenSet_New(dup);
2387 assert(PySet_Size(f) == 3);
2388 assert(PyFrozenSet_CheckExact(f));
2389 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2390 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2391 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* Raise KeyError when popping from an empty set */
2394 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2395 Py_DECREF(ob);
2396 assert(PySet_GET_SIZE(ob) == 0);
2397 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 /* Restore the set from the copy using the PyNumber API */
2400 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2401 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 /* Verify constructors accept NULL arguments */
2404 f = PySet_New(NULL);
2405 assert(f != NULL);
2406 assert(PySet_GET_SIZE(f) == 0);
2407 Py_DECREF(f);
2408 f = PyFrozenSet_New(NULL);
2409 assert(f != NULL);
2410 assert(PyFrozenSet_CheckExact(f));
2411 assert(PySet_GET_SIZE(f) == 0);
2412 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 Py_DECREF(elem);
2415 Py_DECREF(dup);
2416 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002417}
2418
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002419#undef assertRaises
2420
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002421#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002422
2423/***** Dummy Struct *************************************************/
2424
2425static PyObject *
2426dummy_repr(PyObject *op)
2427{
2428 return PyUnicode_FromString("<dummy key>");
2429}
2430
2431static void
2432dummy_dealloc(PyObject* ignore)
2433{
2434 Py_FatalError("deallocating <dummy key>");
2435}
2436
2437static PyTypeObject _PySetDummy_Type = {
2438 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2439 "<dummy key> type",
2440 0,
2441 0,
2442 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2443 0, /*tp_print*/
2444 0, /*tp_getattr*/
2445 0, /*tp_setattr*/
2446 0, /*tp_reserved*/
2447 dummy_repr, /*tp_repr*/
2448 0, /*tp_as_number*/
2449 0, /*tp_as_sequence*/
2450 0, /*tp_as_mapping*/
2451 0, /*tp_hash */
2452 0, /*tp_call */
2453 0, /*tp_str */
2454 0, /*tp_getattro */
2455 0, /*tp_setattro */
2456 0, /*tp_as_buffer */
2457 Py_TPFLAGS_DEFAULT, /*tp_flags */
2458};
2459
2460static PyObject _dummy_struct = {
2461 _PyObject_EXTRA_INIT
2462 2, &_PySetDummy_Type
2463};
2464