blob: e1aa4176baaef8abe5d9261c9964f721341b0b76 [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;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020055 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070056 size_t perturb = hash;
57 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080058 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070059 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070060 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000061
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080062 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070063 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070065
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070066 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080067 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070068 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080069 /* startkey cannot be a dummy because the dummy hash field is -1 */
70 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080071 if (startkey == key)
72 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080073 if (PyUnicode_CheckExact(startkey)
74 && PyUnicode_CheckExact(key)
75 && unicode_eq(startkey, key))
76 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 Py_INCREF(startkey);
78 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
79 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010080 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010082 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010084 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070085 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060086 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070087 }
Raymond Hettingerafe89092013-08-28 20:59:31 -070088
Raymond Hettingerc658d852015-02-02 08:35:00 -080089 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080090 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080091 entry++;
Raymond Hettinger8651a502015-05-27 10:37:20 -070092 if (entry->hash == 0 && entry->key == NULL)
93 return entry;
Raymond Hettingerc658d852015-02-02 08:35:00 -080094 if (entry->hash == hash) {
95 PyObject *startkey = entry->key;
96 assert(startkey != dummy);
97 if (startkey == key)
98 return entry;
99 if (PyUnicode_CheckExact(startkey)
100 && PyUnicode_CheckExact(key)
101 && unicode_eq(startkey, key))
102 return entry;
103 Py_INCREF(startkey);
104 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
105 Py_DECREF(startkey);
106 if (cmp < 0)
107 return NULL;
108 if (table != so->table || entry->key != startkey)
109 return set_lookkey(so, key, hash);
110 if (cmp > 0)
111 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600112 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800113 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700114 }
115 }
116
117 perturb >>= PERTURB_SHIFT;
118 i = (i * 5 + 1 + perturb) & mask;
119
120 entry = &table[i];
121 if (entry->hash == 0 && entry->key == NULL)
122 return entry;
123 }
124}
125
126/*
127Internal routine to insert a new key into the table.
128Used by the public insert routine.
129Eats a reference to key.
130*/
131static int
132set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
133{
134 setentry *table = so->table;
135 setentry *freeslot = NULL;
136 setentry *entry;
137 size_t perturb = hash;
138 size_t mask = so->mask;
139 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
140 size_t j;
141 int cmp;
142
143 entry = &table[i];
144 if (entry->key == NULL)
145 goto found_null;
146
147 while (1) {
148 if (entry->hash == hash) {
149 PyObject *startkey = entry->key;
150 /* startkey cannot be a dummy because the dummy hash field is -1 */
151 assert(startkey != dummy);
152 if (startkey == key)
153 goto found_active;
154 if (PyUnicode_CheckExact(startkey)
155 && PyUnicode_CheckExact(key)
156 && unicode_eq(startkey, key))
157 goto found_active;
158 Py_INCREF(startkey);
159 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
160 Py_DECREF(startkey);
161 if (cmp < 0) /* unlikely */
162 return -1;
163 if (table != so->table || entry->key != startkey) /* unlikely */
164 return set_insert_key(so, key, hash);
165 if (cmp > 0) /* likely */
166 goto found_active;
167 mask = so->mask; /* help avoid a register spill */
168 }
169 if (entry->hash == -1 && freeslot == NULL)
170 freeslot = entry;
171
172 if (i + LINEAR_PROBES <= mask) {
173 for (j = 0 ; j < LINEAR_PROBES ; j++) {
174 entry++;
175 if (entry->hash == 0 && entry->key == NULL)
176 goto found_null;
177 if (entry->hash == hash) {
178 PyObject *startkey = entry->key;
179 assert(startkey != dummy);
180 if (startkey == key)
181 goto found_active;
182 if (PyUnicode_CheckExact(startkey)
183 && PyUnicode_CheckExact(key)
184 && unicode_eq(startkey, key))
185 goto found_active;
186 Py_INCREF(startkey);
187 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
188 Py_DECREF(startkey);
189 if (cmp < 0)
190 return -1;
191 if (table != so->table || entry->key != startkey)
192 return set_insert_key(so, key, hash);
193 if (cmp > 0)
194 goto found_active;
195 mask = so->mask;
196 }
Raymond Hettinger438f9132015-02-09 06:48:29 -0600197 if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800198 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700201
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700202 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800203 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700204
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800205 entry = &table[i];
Raymond Hettinger8651a502015-05-27 10:37:20 -0700206 if (entry->hash == 0 && entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700207 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 }
Raymond Hettinger8651a502015-05-27 10:37:20 -0700209
Raymond Hettingera35adf52013-09-02 03:23:21 -0700210 found_null:
Raymond Hettinger8651a502015-05-27 10:37:20 -0700211 if (freeslot == NULL) {
212 /* UNUSED */
213 so->fill++;
214 } else {
215 /* DUMMY */
216 entry = freeslot;
217 }
218 so->used++;
219 entry->key = key;
220 entry->hash = hash;
221 return 0;
222
223 found_active:
224 Py_DECREF(key);
225 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000226}
227
228/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000229Internal routine used by set_table_resize() to insert an item which is
230known to be absent from the set. This routine also assumes that
231the set contains no deleted entries. Besides the performance benefit,
232using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
233Note that no refcounts are changed by this routine; if needed, the caller
234is responsible for incref'ing `key`.
235*/
236static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200237set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200240 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700241 size_t perturb = hash;
242 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800243 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700244 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000245
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700246 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800247 entry = &table[i];
248 if (entry->key == NULL)
249 goto found_null;
250 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800251 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800252 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800253 if (entry->key == NULL)
254 goto found_null;
255 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700256 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700257 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800258 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700260 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 entry->key = key;
262 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700263 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000265}
266
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700267/* ======== End logic for probing the hash table ========================== */
268/* ======================================================================== */
269
Thomas Wouters89f507f2006-12-13 04:49:30 +0000270/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000271Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000272keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000273actually be smaller than the old one.
274*/
275static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000276set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 Py_ssize_t newsize;
279 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800280 Py_ssize_t oldfill = so->fill;
281 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100288 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 for (newsize = PySet_MINSIZE;
290 newsize <= minused && newsize > 0;
291 newsize <<= 1)
292 ;
293 if (newsize <= 0) {
294 PyErr_NoMemory();
295 return -1;
296 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 /* Get space for a new table. */
299 oldtable = so->table;
300 assert(oldtable != NULL);
301 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (newsize == PySet_MINSIZE) {
304 /* A large table is shrinking, or we can't get any smaller. */
305 newtable = so->smalltable;
306 if (newtable == oldtable) {
307 if (so->fill == so->used) {
308 /* No dummies, so no point doing anything. */
309 return 0;
310 }
311 /* We're not going to resize it, but rebuild the
312 table anyway to purge old dummy entries.
313 Subtle: This is *necessary* if fill==size,
314 as set_lookkey needs at least one virgin slot to
315 terminate failing searches. If fill < size, it's
316 merely desirable, as dummies slow searches. */
317 assert(so->fill > so->used);
318 memcpy(small_copy, oldtable, sizeof(small_copy));
319 oldtable = small_copy;
320 }
321 }
322 else {
323 newtable = PyMem_NEW(setentry, newsize);
324 if (newtable == NULL) {
325 PyErr_NoMemory();
326 return -1;
327 }
328 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 /* Make the set empty, using the new table. */
331 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800334 so->used = 0;
335 so->mask = newsize - 1;
336 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800340 if (oldfill == oldused) {
341 for (entry = oldtable; oldused > 0; entry++) {
342 if (entry->key != NULL) {
343 oldused--;
344 set_insert_clean(so, entry->key, entry->hash);
345 }
346 }
347 } else {
348 for (entry = oldtable; oldused > 0; entry++) {
349 if (entry->key != NULL && entry->key != dummy) {
350 oldused--;
351 set_insert_clean(so, entry->key, entry->hash);
352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 }
354 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (is_oldtable_malloced)
357 PyMem_DEL(oldtable);
358 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000359}
360
Raymond Hettingerc991db22005-08-11 07:58:45 +0000361/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
362
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200364set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000365{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200366 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000367 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100368 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 assert(so->fill <= so->mask); /* at least one empty slot */
371 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000372 Py_INCREF(key);
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700373 if (set_insert_key(so, key, hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000374 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return -1;
376 }
377 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
378 return 0;
379 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000380}
381
382static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200383set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000384{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800385 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200386 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200389 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 hash = PyObject_Hash(key);
391 if (hash == -1)
392 return -1;
393 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800394 entry.key = key;
395 entry.hash = hash;
396 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000397}
398
399#define DISCARD_NOTFOUND 0
400#define DISCARD_FOUND 1
401
402static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000403set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800404{
405 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000407
Raymond Hettinger93035c42015-01-25 16:12:49 -0800408 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (entry == NULL)
410 return -1;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700411 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 return DISCARD_NOTFOUND;
413 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800415 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 so->used--;
417 Py_DECREF(old_key);
418 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000419}
420
421static int
422set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000423{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800424 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200425 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200430 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 hash = PyObject_Hash(key);
432 if (hash == -1)
433 return -1;
434 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800435 entry.key = key;
436 entry.hash = hash;
437 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000438}
439
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700440static void
441set_empty_to_minsize(PySetObject *so)
442{
443 memset(so->smalltable, 0, sizeof(so->smalltable));
444 so->fill = 0;
445 so->used = 0;
446 so->mask = PySet_MINSIZE - 1;
447 so->table = so->smalltable;
448 so->hash = -1;
449}
450
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000451static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000452set_clear_internal(PySetObject *so)
453{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800454 setentry *entry;
455 setentry *table = so->table;
456 Py_ssize_t fill = so->fill;
457 Py_ssize_t used = so->used;
458 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000460
Raymond Hettinger583cd032013-09-07 22:06:35 -0700461 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 /* This is delicate. During the process of clearing the set,
465 * decrefs can cause the set to mutate. To avoid fatal confusion
466 * (voice of experience), we have to make the set empty before
467 * clearing the slots, and never refer to anything via so->ref while
468 * clearing.
469 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700471 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 else if (fill > 0) {
474 /* It's a small table with something that needs to be cleared.
475 * Afraid the only safe way is to copy the set entries into
476 * another small table first.
477 */
478 memcpy(small_copy, table, sizeof(small_copy));
479 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700480 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 }
482 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 /* Now we can finally clear things. If C had refcounts, we could
485 * assert that the refcount on table is 1 now, i.e. that this function
486 * has unique access to it, so decref side-effects can't alter it.
487 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800488 for (entry = table; used > 0; entry++) {
489 if (entry->key && entry->key != dummy) {
490 used--;
491 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if (table_is_malloced)
496 PyMem_DEL(table);
497 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000498}
499
500/*
501 * Iterate over a set table. Use like so:
502 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000503 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000504 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000505 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000506 * while (set_next(yourset, &pos, &entry)) {
507 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000508 * }
509 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000510 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000512 */
513static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000514set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 Py_ssize_t i;
517 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200518 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 assert (PyAnySet_Check(so));
521 i = *pos_ptr;
522 assert(i >= 0);
523 table = so->table;
524 mask = so->mask;
525 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
526 i++;
527 *pos_ptr = i+1;
528 if (i > mask)
529 return 0;
530 assert(table[i].key != NULL);
531 *entry_ptr = &table[i];
532 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000533}
534
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000535static void
536set_dealloc(PySetObject *so)
537{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200538 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800539 Py_ssize_t used = so->used;
540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 PyObject_GC_UnTrack(so);
542 Py_TRASHCAN_SAFE_BEGIN(so)
543 if (so->weakreflist != NULL)
544 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000545
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800546 for (entry = so->table; used > 0; entry++) {
547 if (entry->key && entry->key != dummy) {
548 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700549 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 }
551 }
552 if (so->table != so->smalltable)
553 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700554 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000556}
557
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000558static PyObject *
559set_repr(PySetObject *so)
560{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200561 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 if (status != 0) {
565 if (status < 0)
566 return NULL;
567 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
568 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 /* shortcut for the empty set */
571 if (!so->used) {
572 Py_ReprLeave((PyObject*)so);
573 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
574 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 keys = PySequence_List((PyObject *)so);
577 if (keys == NULL)
578 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000579
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200580 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 listrepr = PyObject_Repr(keys);
582 Py_DECREF(keys);
583 if (listrepr == NULL)
584 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200585 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200587 if (tmp == NULL)
588 goto done;
589 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200590
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200591 if (Py_TYPE(so) != &PySet_Type)
592 result = PyUnicode_FromFormat("%s({%U})",
593 Py_TYPE(so)->tp_name,
594 listrepr);
595 else
596 result = PyUnicode_FromFormat("{%U}", listrepr);
597 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000598done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 Py_ReprLeave((PyObject*)so);
600 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000601}
602
Martin v. Löwis18e16552006-02-15 17:27:45 +0000603static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000604set_len(PyObject *so)
605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000607}
608
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000609static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000610set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000613 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200614 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700615 setentry *so_entry;
616 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 assert (PyAnySet_Check(so));
619 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 other = (PySetObject*)otherset;
622 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700623 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 return 0;
625 /* Do one big resize at the start, rather than
626 * incrementally resizing as we insert new keys. Expect
627 * that there will be no (or few) overlapping keys.
628 */
629 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
630 if (set_table_resize(so, (so->used + other->used)*2) != 0)
631 return -1;
632 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700633 so_entry = so->table;
634 other_entry = other->table;
635
636 /* If our table is empty, and both tables have the same size, and
637 there are no dummies to eliminate, then just copy the pointers. */
638 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
639 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
640 key = other_entry->key;
641 if (key != NULL) {
642 assert(so_entry->key == NULL);
643 Py_INCREF(key);
644 so_entry->key = key;
645 so_entry->hash = other_entry->hash;
646 }
647 }
648 so->fill = other->fill;
649 so->used = other->used;
650 return 0;
651 }
652
653 /* If our table is empty, we can use set_insert_clean() */
654 if (so->fill == 0) {
655 for (i = 0; i <= other->mask; i++, other_entry++) {
656 key = other_entry->key;
657 if (key != NULL && key != dummy) {
658 Py_INCREF(key);
659 set_insert_clean(so, key, other_entry->hash);
660 }
661 }
662 return 0;
663 }
664
665 /* We can't assure there are no duplicates, so do normal insertions */
666 for (i = 0; i <= other->mask; i++, other_entry++) {
667 key = other_entry->key;
668 if (key != NULL && key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000669 Py_INCREF(key);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700670 if (set_insert_key(so, key, other_entry->hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000671 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 return -1;
673 }
674 }
675 }
676 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000677}
678
679static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000680set_contains_entry(PySetObject *so, setentry *entry)
681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 PyObject *key;
683 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000684
Raymond Hettinger93035c42015-01-25 16:12:49 -0800685 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (lu_entry == NULL)
687 return -1;
688 key = lu_entry->key;
Raymond Hettinger8651a502015-05-27 10:37:20 -0700689 return key != NULL;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000690}
691
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800692static int
693set_contains_key(PySetObject *so, PyObject *key)
694{
695 setentry entry;
696 Py_hash_t hash;
697
698 if (!PyUnicode_CheckExact(key) ||
699 (hash = ((PyASCIIObject *) key)->hash) == -1) {
700 hash = PyObject_Hash(key);
701 if (hash == -1)
702 return -1;
703 }
704 entry.key = key;
705 entry.hash = hash;
706 return set_contains_entry(so, &entry);
707}
708
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000709static PyObject *
710set_pop(PySetObject *so)
711{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800712 /* Make sure the search finger is in bounds */
713 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200714 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 assert (PyAnySet_Check(so));
718 if (so->used == 0) {
719 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
720 return NULL;
721 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000722
Raymond Hettinger1202a472015-01-18 13:12:42 -0800723 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
724 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800725 if (i > so->mask)
726 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 }
728 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800730 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800732 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000734}
735
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000736PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
737Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000738
739static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000740set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 Py_ssize_t pos = 0;
743 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 while (set_next(so, &pos, &entry))
746 Py_VISIT(entry->key);
747 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000748}
749
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000750static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000751frozenset_hash(PyObject *self)
752{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800753 /* Most of the constants in this hash algorithm are randomly choosen
754 large primes with "interesting bit patterns" and that passed
755 tests for good collision statistics on a variety of problematic
756 datasets such as:
757
758 ps = []
759 for r in range(21):
760 ps += itertools.combinations(range(20), r)
761 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
762
763 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800765 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 setentry *entry;
767 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (so->hash != -1)
770 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000771
Mark Dickinson57e683e2011-09-24 18:18:40 +0100772 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 while (set_next(so, &pos, &entry)) {
774 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800775 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 combinations of a small number of elements with nearby
777 hashes so that many distinct combinations collapse to only
778 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000779 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800780 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800782 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500783 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800784 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200785 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800786 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 so->hash = hash;
788 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000789}
790
Raymond Hettingera9d99362005-08-05 00:01:15 +0000791/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000792
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 PyObject_HEAD
795 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
796 Py_ssize_t si_used;
797 Py_ssize_t si_pos;
798 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000799} setiterobject;
800
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000801static void
802setiter_dealloc(setiterobject *si)
803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 Py_XDECREF(si->si_set);
805 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000806}
807
808static int
809setiter_traverse(setiterobject *si, visitproc visit, void *arg)
810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 Py_VISIT(si->si_set);
812 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000813}
814
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000815static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000816setiter_len(setiterobject *si)
817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 Py_ssize_t len = 0;
819 if (si->si_set != NULL && si->si_used == si->si_set->used)
820 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000821 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000822}
823
Armin Rigof5b3e362006-02-11 21:32:43 +0000824PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000825
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000826static PyObject *setiter_iternext(setiterobject *si);
827
828static PyObject *
829setiter_reduce(setiterobject *si)
830{
831 PyObject *list;
832 setiterobject tmp;
833
834 list = PyList_New(0);
835 if (!list)
836 return NULL;
837
Ezio Melotti0e1af282012-09-28 16:43:40 +0300838 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000839 tmp = *si;
840 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300841
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000842 /* iterate the temporary into a list */
843 for(;;) {
844 PyObject *element = setiter_iternext(&tmp);
845 if (element) {
846 if (PyList_Append(list, element)) {
847 Py_DECREF(element);
848 Py_DECREF(list);
849 Py_XDECREF(tmp.si_set);
850 return NULL;
851 }
852 Py_DECREF(element);
853 } else
854 break;
855 }
856 Py_XDECREF(tmp.si_set);
857 /* check for error */
858 if (tmp.si_set != NULL) {
859 /* we have an error */
860 Py_DECREF(list);
861 return NULL;
862 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200863 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000864}
865
866PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
867
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000868static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000870 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000872};
873
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000874static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200877 Py_ssize_t i, mask;
878 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (so == NULL)
882 return NULL;
883 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 if (si->si_used != so->used) {
886 PyErr_SetString(PyExc_RuntimeError,
887 "Set changed size during iteration");
888 si->si_used = -1; /* Make this state sticky */
889 return NULL;
890 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 i = si->si_pos;
893 assert(i>=0);
894 entry = so->table;
895 mask = so->mask;
896 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
897 i++;
898 si->si_pos = i+1;
899 if (i > mask)
900 goto fail;
901 si->len--;
902 key = entry[i].key;
903 Py_INCREF(key);
904 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000905
906fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 Py_DECREF(so);
908 si->si_set = NULL;
909 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000910}
911
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000912PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyVarObject_HEAD_INIT(&PyType_Type, 0)
914 "set_iterator", /* tp_name */
915 sizeof(setiterobject), /* tp_basicsize */
916 0, /* tp_itemsize */
917 /* methods */
918 (destructor)setiter_dealloc, /* tp_dealloc */
919 0, /* tp_print */
920 0, /* tp_getattr */
921 0, /* tp_setattr */
922 0, /* tp_reserved */
923 0, /* tp_repr */
924 0, /* tp_as_number */
925 0, /* tp_as_sequence */
926 0, /* tp_as_mapping */
927 0, /* tp_hash */
928 0, /* tp_call */
929 0, /* tp_str */
930 PyObject_GenericGetAttr, /* tp_getattro */
931 0, /* tp_setattro */
932 0, /* tp_as_buffer */
933 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
934 0, /* tp_doc */
935 (traverseproc)setiter_traverse, /* tp_traverse */
936 0, /* tp_clear */
937 0, /* tp_richcompare */
938 0, /* tp_weaklistoffset */
939 PyObject_SelfIter, /* tp_iter */
940 (iternextfunc)setiter_iternext, /* tp_iternext */
941 setiter_methods, /* tp_methods */
942 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000943};
944
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000945static PyObject *
946set_iter(PySetObject *so)
947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
949 if (si == NULL)
950 return NULL;
951 Py_INCREF(so);
952 si->si_set = so;
953 si->si_used = so->used;
954 si->si_pos = 0;
955 si->len = so->used;
956 _PyObject_GC_TRACK(si);
957 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000958}
959
Raymond Hettingerd7946662005-08-01 21:39:29 +0000960static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000961set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 if (PyAnySet_Check(other))
966 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 if (PyDict_CheckExact(other)) {
969 PyObject *value;
970 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000971 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 /* Do one big resize at the start, rather than
975 * incrementally resizing as we insert new keys. Expect
976 * that there will be no (or few) overlapping keys.
977 */
978 if (dictsize == -1)
979 return -1;
980 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
981 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
982 return -1;
983 }
984 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
985 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 an_entry.hash = hash;
988 an_entry.key = key;
Raymond Hettinger5af9e132015-05-13 01:44:36 -0700989 if (set_add_entry(so, &an_entry))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 return -1;
991 }
992 return 0;
993 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 it = PyObject_GetIter(other);
996 if (it == NULL)
997 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 while ((key = PyIter_Next(it)) != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001000 if (set_add_key(so, key)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 Py_DECREF(it);
1002 Py_DECREF(key);
1003 return -1;
1004 }
1005 Py_DECREF(key);
1006 }
1007 Py_DECREF(it);
1008 if (PyErr_Occurred())
1009 return -1;
1010 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001011}
1012
1013static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001014set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1019 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001020 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 return NULL;
1022 }
1023 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001024}
1025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001027"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +00001028
Raymond Hettinger426d9952014-05-18 21:40:20 +01001029/* XXX Todo:
1030 If aligned memory allocations become available, make the
1031 set object 64 byte aligned so that most of the fields
1032 can be retrieved or updated in a single cache line.
1033*/
1034
Raymond Hettingera38123e2003-11-24 22:18:49 +00001035static PyObject *
1036make_new_set(PyTypeObject *type, PyObject *iterable)
1037{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001038 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001041 so = (PySetObject *)type->tp_alloc(type, 0);
1042 if (so == NULL)
1043 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001044
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001045 so->fill = 0;
1046 so->used = 0;
1047 so->mask = PySet_MINSIZE - 1;
1048 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -07001049 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -08001050 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (iterable != NULL) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001054 if (set_update_internal(so, iterable)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 Py_DECREF(so);
1056 return NULL;
1057 }
1058 }
Raymond Hettingera38123e2003-11-24 22:18:49 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001061}
1062
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001063static PyObject *
1064make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1067 if (PyType_IsSubtype(type, &PySet_Type))
1068 type = &PySet_Type;
1069 else
1070 type = &PyFrozenSet_Type;
1071 }
1072 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001073}
1074
Raymond Hettingerd7946662005-08-01 21:39:29 +00001075/* The empty frozenset is a singleton */
1076static PyObject *emptyfrozenset = NULL;
1077
Raymond Hettingera690a992003-11-16 16:17:49 +00001078static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001079frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1084 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1087 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 if (type != &PyFrozenSet_Type)
1090 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 if (iterable != NULL) {
1093 /* frozenset(f) is idempotent */
1094 if (PyFrozenSet_CheckExact(iterable)) {
1095 Py_INCREF(iterable);
1096 return iterable;
1097 }
1098 result = make_new_set(type, iterable);
1099 if (result == NULL || PySet_GET_SIZE(result))
1100 return result;
1101 Py_DECREF(result);
1102 }
1103 /* The empty frozenset is a singleton */
1104 if (emptyfrozenset == NULL)
1105 emptyfrozenset = make_new_set(type, NULL);
1106 Py_XINCREF(emptyfrozenset);
1107 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001108}
1109
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001110int
1111PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001112{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001113 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001114}
1115
1116void
1117PySet_Fini(void)
1118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001120}
1121
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001122static PyObject *
1123set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1126 return NULL;
1127
1128 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001129}
1130
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001131/* set_swap_bodies() switches the contents of any two sets by moving their
1132 internal data pointers and, if needed, copying the internal smalltables.
1133 Semantically equivalent to:
1134
1135 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1136
1137 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001138 Useful for operations that update in-place (by allowing an intermediate
1139 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001140*/
1141
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001142static void
1143set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 Py_ssize_t t;
1146 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001148 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 t = a->fill; a->fill = b->fill; b->fill = t;
1151 t = a->used; a->used = b->used; b->used = t;
1152 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 u = a->table;
1155 if (a->table == a->smalltable)
1156 u = b->smalltable;
1157 a->table = b->table;
1158 if (b->table == b->smalltable)
1159 a->table = a->smalltable;
1160 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (a->table == a->smalltable || b->table == b->smalltable) {
1163 memcpy(tab, a->smalltable, sizeof(tab));
1164 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1165 memcpy(b->smalltable, tab, sizeof(tab));
1166 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1169 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1170 h = a->hash; a->hash = b->hash; b->hash = h;
1171 } else {
1172 a->hash = -1;
1173 b->hash = -1;
1174 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001175}
1176
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001177static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001178set_copy(PySetObject *so)
1179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001181}
1182
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001183static PyObject *
1184frozenset_copy(PySetObject *so)
1185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (PyFrozenSet_CheckExact(so)) {
1187 Py_INCREF(so);
1188 return (PyObject *)so;
1189 }
1190 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001191}
1192
Raymond Hettingera690a992003-11-16 16:17:49 +00001193PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1194
1195static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001196set_clear(PySetObject *so)
1197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 set_clear_internal(so);
1199 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001200}
1201
1202PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1203
1204static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001205set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 PySetObject *result;
1208 PyObject *other;
1209 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 result = (PySetObject *)set_copy(so);
1212 if (result == NULL)
1213 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1216 other = PyTuple_GET_ITEM(args, i);
1217 if ((PyObject *)so == other)
1218 continue;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001219 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 Py_DECREF(result);
1221 return NULL;
1222 }
1223 }
1224 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001225}
1226
1227PyDoc_STRVAR(union_doc,
1228 "Return the union of sets as a new set.\n\
1229\n\
1230(i.e. all elements that are in either set.)");
1231
1232static PyObject *
1233set_or(PySetObject *so, PyObject *other)
1234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001236
Brian Curtindfc80e32011-08-10 20:28:54 -05001237 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1238 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 result = (PySetObject *)set_copy(so);
1241 if (result == NULL)
1242 return NULL;
1243 if ((PyObject *)so == other)
1244 return (PyObject *)result;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001245 if (set_update_internal(result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 Py_DECREF(result);
1247 return NULL;
1248 }
1249 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001250}
1251
Raymond Hettingera690a992003-11-16 16:17:49 +00001252static PyObject *
1253set_ior(PySetObject *so, PyObject *other)
1254{
Brian Curtindfc80e32011-08-10 20:28:54 -05001255 if (!PyAnySet_Check(other))
1256 Py_RETURN_NOTIMPLEMENTED;
1257
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001258 if (set_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 return NULL;
1260 Py_INCREF(so);
1261 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001262}
1263
1264static PyObject *
1265set_intersection(PySetObject *so, PyObject *other)
1266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 PySetObject *result;
1268 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if ((PyObject *)so == other)
1271 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1274 if (result == NULL)
1275 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (PyAnySet_Check(other)) {
1278 Py_ssize_t pos = 0;
1279 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1282 tmp = (PyObject *)so;
1283 so = (PySetObject *)other;
1284 other = tmp;
1285 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 while (set_next((PySetObject *)other, &pos, &entry)) {
1288 int rv = set_contains_entry(so, entry);
1289 if (rv == -1) {
1290 Py_DECREF(result);
1291 return NULL;
1292 }
1293 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001294 if (set_add_entry(result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 Py_DECREF(result);
1296 return NULL;
1297 }
1298 }
1299 }
1300 return (PyObject *)result;
1301 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 it = PyObject_GetIter(other);
1304 if (it == NULL) {
1305 Py_DECREF(result);
1306 return NULL;
1307 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 while ((key = PyIter_Next(it)) != NULL) {
1310 int rv;
1311 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001312 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (hash == -1) {
1315 Py_DECREF(it);
1316 Py_DECREF(result);
1317 Py_DECREF(key);
1318 return NULL;
1319 }
1320 entry.hash = hash;
1321 entry.key = key;
1322 rv = set_contains_entry(so, &entry);
1323 if (rv == -1) {
1324 Py_DECREF(it);
1325 Py_DECREF(result);
1326 Py_DECREF(key);
1327 return NULL;
1328 }
1329 if (rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001330 if (set_add_entry(result, &entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 Py_DECREF(it);
1332 Py_DECREF(result);
1333 Py_DECREF(key);
1334 return NULL;
1335 }
1336 }
1337 Py_DECREF(key);
1338 }
1339 Py_DECREF(it);
1340 if (PyErr_Occurred()) {
1341 Py_DECREF(result);
1342 return NULL;
1343 }
1344 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001345}
1346
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001347static PyObject *
1348set_intersection_multi(PySetObject *so, PyObject *args)
1349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 Py_ssize_t i;
1351 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 if (PyTuple_GET_SIZE(args) == 0)
1354 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 Py_INCREF(so);
1357 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1358 PyObject *other = PyTuple_GET_ITEM(args, i);
1359 PyObject *newresult = set_intersection((PySetObject *)result, other);
1360 if (newresult == NULL) {
1361 Py_DECREF(result);
1362 return NULL;
1363 }
1364 Py_DECREF(result);
1365 result = newresult;
1366 }
1367 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001368}
1369
Raymond Hettingera690a992003-11-16 16:17:49 +00001370PyDoc_STRVAR(intersection_doc,
1371"Return the intersection of two sets as a new set.\n\
1372\n\
1373(i.e. all elements that are in both sets.)");
1374
1375static PyObject *
1376set_intersection_update(PySetObject *so, PyObject *other)
1377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 tmp = set_intersection(so, other);
1381 if (tmp == NULL)
1382 return NULL;
1383 set_swap_bodies(so, (PySetObject *)tmp);
1384 Py_DECREF(tmp);
1385 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001386}
1387
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001388static PyObject *
1389set_intersection_update_multi(PySetObject *so, PyObject *args)
1390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 tmp = set_intersection_multi(so, args);
1394 if (tmp == NULL)
1395 return NULL;
1396 set_swap_bodies(so, (PySetObject *)tmp);
1397 Py_DECREF(tmp);
1398 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001399}
1400
Raymond Hettingera690a992003-11-16 16:17:49 +00001401PyDoc_STRVAR(intersection_update_doc,
1402"Update a set with the intersection of itself and another.");
1403
1404static PyObject *
1405set_and(PySetObject *so, PyObject *other)
1406{
Brian Curtindfc80e32011-08-10 20:28:54 -05001407 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1408 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001410}
1411
1412static PyObject *
1413set_iand(PySetObject *so, PyObject *other)
1414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001416
Brian Curtindfc80e32011-08-10 20:28:54 -05001417 if (!PyAnySet_Check(other))
1418 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 result = set_intersection_update(so, other);
1420 if (result == NULL)
1421 return NULL;
1422 Py_DECREF(result);
1423 Py_INCREF(so);
1424 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001425}
1426
Guido van Rossum58da9312007-11-10 23:39:45 +00001427static PyObject *
1428set_isdisjoint(PySetObject *so, PyObject *other)
1429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if ((PyObject *)so == other) {
1433 if (PySet_GET_SIZE(so) == 0)
1434 Py_RETURN_TRUE;
1435 else
1436 Py_RETURN_FALSE;
1437 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 if (PyAnySet_CheckExact(other)) {
1440 Py_ssize_t pos = 0;
1441 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1444 tmp = (PyObject *)so;
1445 so = (PySetObject *)other;
1446 other = tmp;
1447 }
1448 while (set_next((PySetObject *)other, &pos, &entry)) {
1449 int rv = set_contains_entry(so, entry);
1450 if (rv == -1)
1451 return NULL;
1452 if (rv)
1453 Py_RETURN_FALSE;
1454 }
1455 Py_RETURN_TRUE;
1456 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 it = PyObject_GetIter(other);
1459 if (it == NULL)
1460 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 while ((key = PyIter_Next(it)) != NULL) {
1463 int rv;
1464 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001465 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 if (hash == -1) {
1468 Py_DECREF(key);
1469 Py_DECREF(it);
1470 return NULL;
1471 }
1472 entry.hash = hash;
1473 entry.key = key;
1474 rv = set_contains_entry(so, &entry);
1475 Py_DECREF(key);
1476 if (rv == -1) {
1477 Py_DECREF(it);
1478 return NULL;
1479 }
1480 if (rv) {
1481 Py_DECREF(it);
1482 Py_RETURN_FALSE;
1483 }
1484 }
1485 Py_DECREF(it);
1486 if (PyErr_Occurred())
1487 return NULL;
1488 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001489}
1490
1491PyDoc_STRVAR(isdisjoint_doc,
1492"Return True if two sets have a null intersection.");
1493
Neal Norwitz6576bd82005-11-13 18:41:28 +00001494static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001495set_difference_update_internal(PySetObject *so, PyObject *other)
1496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if ((PyObject *)so == other)
1498 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 if (PyAnySet_Check(other)) {
1501 setentry *entry;
1502 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 while (set_next((PySetObject *)other, &pos, &entry))
1505 if (set_discard_entry(so, entry) == -1)
1506 return -1;
1507 } else {
1508 PyObject *key, *it;
1509 it = PyObject_GetIter(other);
1510 if (it == NULL)
1511 return -1;
1512
1513 while ((key = PyIter_Next(it)) != NULL) {
1514 if (set_discard_key(so, key) == -1) {
1515 Py_DECREF(it);
1516 Py_DECREF(key);
1517 return -1;
1518 }
1519 Py_DECREF(key);
1520 }
1521 Py_DECREF(it);
1522 if (PyErr_Occurred())
1523 return -1;
1524 }
1525 /* If more than 1/5 are dummies, then resize them away. */
1526 if ((so->fill - so->used) * 5 < so->mask)
1527 return 0;
1528 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001529}
1530
Raymond Hettingera690a992003-11-16 16:17:49 +00001531static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001532set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1537 PyObject *other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001538 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 return NULL;
1540 }
1541 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001542}
1543
1544PyDoc_STRVAR(difference_update_doc,
1545"Remove all elements of another set from this set.");
1546
1547static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001548set_copy_and_difference(PySetObject *so, PyObject *other)
1549{
1550 PyObject *result;
1551
1552 result = set_copy(so);
1553 if (result == NULL)
1554 return NULL;
1555 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1556 return result;
1557 Py_DECREF(result);
1558 return NULL;
1559}
1560
1561static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001562set_difference(PySetObject *so, PyObject *other)
1563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 PyObject *result;
1565 setentry *entry;
1566 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001569 return set_copy_and_difference(so, other);
1570 }
1571
1572 /* If len(so) much more than len(other), it's more efficient to simply copy
1573 * so and then iterate other looking for common elements. */
1574 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1575 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 result = make_new_set_basetype(Py_TYPE(so), NULL);
1579 if (result == NULL)
1580 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (PyDict_CheckExact(other)) {
1583 while (set_next(so, &pos, &entry)) {
1584 setentry entrycopy;
1585 entrycopy.hash = entry->hash;
1586 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001587 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001588 if (set_add_entry((PySetObject *)result, &entrycopy)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 Py_DECREF(result);
1590 return NULL;
1591 }
1592 }
1593 }
1594 return result;
1595 }
1596
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001597 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 while (set_next(so, &pos, &entry)) {
1599 int rv = set_contains_entry((PySetObject *)other, entry);
1600 if (rv == -1) {
1601 Py_DECREF(result);
1602 return NULL;
1603 }
1604 if (!rv) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001605 if (set_add_entry((PySetObject *)result, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 Py_DECREF(result);
1607 return NULL;
1608 }
1609 }
1610 }
1611 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001612}
1613
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001614static PyObject *
1615set_difference_multi(PySetObject *so, PyObject *args)
1616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 Py_ssize_t i;
1618 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 if (PyTuple_GET_SIZE(args) == 0)
1621 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 other = PyTuple_GET_ITEM(args, 0);
1624 result = set_difference(so, other);
1625 if (result == NULL)
1626 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1629 other = PyTuple_GET_ITEM(args, i);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001630 if (set_difference_update_internal((PySetObject *)result, other)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 Py_DECREF(result);
1632 return NULL;
1633 }
1634 }
1635 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001636}
1637
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001638PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001639"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001640\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001641(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001642static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001643set_sub(PySetObject *so, PyObject *other)
1644{
Brian Curtindfc80e32011-08-10 20:28:54 -05001645 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1646 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001648}
1649
1650static PyObject *
1651set_isub(PySetObject *so, PyObject *other)
1652{
Brian Curtindfc80e32011-08-10 20:28:54 -05001653 if (!PyAnySet_Check(other))
1654 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001655 if (set_difference_update_internal(so, other))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 return NULL;
1657 Py_INCREF(so);
1658 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001659}
1660
1661static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001662set_symmetric_difference_update(PySetObject *so, PyObject *other)
1663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 PySetObject *otherset;
1665 PyObject *key;
1666 Py_ssize_t pos = 0;
1667 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 if ((PyObject *)so == other)
1670 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 if (PyDict_CheckExact(other)) {
1673 PyObject *value;
1674 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001675 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1677 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001678
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001679 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 an_entry.hash = hash;
1681 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001684 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001685 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001688 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001689 if (set_add_entry(so, &an_entry)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001690 Py_DECREF(key);
1691 return NULL;
1692 }
1693 }
Georg Brandl2d444492010-09-03 10:52:55 +00001694 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 }
1696 Py_RETURN_NONE;
1697 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 if (PyAnySet_Check(other)) {
1700 Py_INCREF(other);
1701 otherset = (PySetObject *)other;
1702 } else {
1703 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1704 if (otherset == NULL)
1705 return NULL;
1706 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 while (set_next(otherset, &pos, &entry)) {
1709 int rv = set_discard_entry(so, entry);
1710 if (rv == -1) {
1711 Py_DECREF(otherset);
1712 return NULL;
1713 }
1714 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001715 if (set_add_entry(so, entry)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 Py_DECREF(otherset);
1717 return NULL;
1718 }
1719 }
1720 }
1721 Py_DECREF(otherset);
1722 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001723}
1724
1725PyDoc_STRVAR(symmetric_difference_update_doc,
1726"Update a set with the symmetric difference of itself and another.");
1727
1728static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001729set_symmetric_difference(PySetObject *so, PyObject *other)
1730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 PyObject *rv;
1732 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1735 if (otherset == NULL)
1736 return NULL;
1737 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1738 if (rv == NULL)
1739 return NULL;
1740 Py_DECREF(rv);
1741 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001742}
1743
1744PyDoc_STRVAR(symmetric_difference_doc,
1745"Return the symmetric difference of two sets as a new set.\n\
1746\n\
1747(i.e. all elements that are in exactly one of the sets.)");
1748
1749static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001750set_xor(PySetObject *so, PyObject *other)
1751{
Brian Curtindfc80e32011-08-10 20:28:54 -05001752 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1753 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001755}
1756
1757static PyObject *
1758set_ixor(PySetObject *so, PyObject *other)
1759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001761
Brian Curtindfc80e32011-08-10 20:28:54 -05001762 if (!PyAnySet_Check(other))
1763 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 result = set_symmetric_difference_update(so, other);
1765 if (result == NULL)
1766 return NULL;
1767 Py_DECREF(result);
1768 Py_INCREF(so);
1769 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001770}
1771
1772static PyObject *
1773set_issubset(PySetObject *so, PyObject *other)
1774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 setentry *entry;
1776 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 if (!PyAnySet_Check(other)) {
1779 PyObject *tmp, *result;
1780 tmp = make_new_set(&PySet_Type, other);
1781 if (tmp == NULL)
1782 return NULL;
1783 result = set_issubset(so, tmp);
1784 Py_DECREF(tmp);
1785 return result;
1786 }
1787 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1788 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 while (set_next(so, &pos, &entry)) {
1791 int rv = set_contains_entry((PySetObject *)other, entry);
1792 if (rv == -1)
1793 return NULL;
1794 if (!rv)
1795 Py_RETURN_FALSE;
1796 }
1797 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001798}
1799
1800PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1801
1802static PyObject *
1803set_issuperset(PySetObject *so, PyObject *other)
1804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 if (!PyAnySet_Check(other)) {
1808 tmp = make_new_set(&PySet_Type, other);
1809 if (tmp == NULL)
1810 return NULL;
1811 result = set_issuperset(so, tmp);
1812 Py_DECREF(tmp);
1813 return result;
1814 }
1815 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001816}
1817
1818PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1819
Raymond Hettingera690a992003-11-16 16:17:49 +00001820static PyObject *
1821set_richcompare(PySetObject *v, PyObject *w, int op)
1822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001824
Brian Curtindfc80e32011-08-10 20:28:54 -05001825 if(!PyAnySet_Check(w))
1826 Py_RETURN_NOTIMPLEMENTED;
1827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 switch (op) {
1829 case Py_EQ:
1830 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1831 Py_RETURN_FALSE;
1832 if (v->hash != -1 &&
1833 ((PySetObject *)w)->hash != -1 &&
1834 v->hash != ((PySetObject *)w)->hash)
1835 Py_RETURN_FALSE;
1836 return set_issubset(v, w);
1837 case Py_NE:
1838 r1 = set_richcompare(v, w, Py_EQ);
1839 if (r1 == NULL)
1840 return NULL;
1841 r2 = PyBool_FromLong(PyObject_Not(r1));
1842 Py_DECREF(r1);
1843 return r2;
1844 case Py_LE:
1845 return set_issubset(v, w);
1846 case Py_GE:
1847 return set_issuperset(v, w);
1848 case Py_LT:
1849 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1850 Py_RETURN_FALSE;
1851 return set_issubset(v, w);
1852 case Py_GT:
1853 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1854 Py_RETURN_FALSE;
1855 return set_issuperset(v, w);
1856 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001857 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001858}
1859
Raymond Hettingera690a992003-11-16 16:17:49 +00001860static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001861set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001862{
Raymond Hettinger5af9e132015-05-13 01:44:36 -07001863 if (set_add_key(so, key))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 return NULL;
1865 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001866}
1867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001869"Add an element to a set.\n\
1870\n\
1871This has no effect if the element is already present.");
1872
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001873static int
1874set_contains(PySetObject *so, PyObject *key)
1875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 PyObject *tmpkey;
1877 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 rv = set_contains_key(so, key);
1880 if (rv == -1) {
1881 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1882 return -1;
1883 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001884 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 if (tmpkey == NULL)
1886 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001887 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 Py_DECREF(tmpkey);
1889 }
1890 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001891}
1892
1893static PyObject *
1894set_direct_contains(PySetObject *so, PyObject *key)
1895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 result = set_contains(so, key);
1899 if (result == -1)
1900 return NULL;
1901 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001902}
1903
1904PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1905
Raymond Hettingera690a992003-11-16 16:17:49 +00001906static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001907set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 PyObject *tmpkey;
1910 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 rv = set_discard_key(so, key);
1913 if (rv == -1) {
1914 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1915 return NULL;
1916 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001917 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 if (tmpkey == NULL)
1919 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 Py_DECREF(tmpkey);
1922 if (rv == -1)
1923 return NULL;
1924 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001927 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 return NULL;
1929 }
1930 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001931}
1932
1933PyDoc_STRVAR(remove_doc,
1934"Remove an element from a set; it must be a member.\n\
1935\n\
1936If the element is not a member, raise a KeyError.");
1937
1938static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001939set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001940{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001941 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 rv = set_discard_key(so, key);
1945 if (rv == -1) {
1946 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1947 return NULL;
1948 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001949 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 if (tmpkey == NULL)
1951 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001952 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001954 if (rv == -1)
1955 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 }
1957 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001958}
1959
1960PyDoc_STRVAR(discard_doc,
1961"Remove an element from a set if it is a member.\n\
1962\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001964
1965static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001966set_reduce(PySetObject *so)
1967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001969 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 keys = PySequence_List((PyObject *)so);
1972 if (keys == NULL)
1973 goto done;
1974 args = PyTuple_Pack(1, keys);
1975 if (args == NULL)
1976 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001977 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 if (dict == NULL) {
1979 PyErr_Clear();
1980 dict = Py_None;
1981 Py_INCREF(dict);
1982 }
1983 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001984done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 Py_XDECREF(args);
1986 Py_XDECREF(keys);
1987 Py_XDECREF(dict);
1988 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001989}
1990
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001991static PyObject *
1992set_sizeof(PySetObject *so)
1993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 res = sizeof(PySetObject);
1997 if (so->table != so->smalltable)
1998 res = res + (so->mask + 1) * sizeof(setentry);
1999 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002000}
2001
2002PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002003static int
2004set_init(PySetObject *self, PyObject *args, PyObject *kwds)
2005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (!PyAnySet_Check(self))
2009 return -1;
2010 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
2011 return -1;
2012 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2013 return -1;
2014 set_clear_internal(self);
2015 self->hash = -1;
2016 if (iterable == NULL)
2017 return 0;
2018 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00002019}
2020
Raymond Hettingera690a992003-11-16 16:17:49 +00002021static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 set_len, /* sq_length */
2023 0, /* sq_concat */
2024 0, /* sq_repeat */
2025 0, /* sq_item */
2026 0, /* sq_slice */
2027 0, /* sq_ass_item */
2028 0, /* sq_ass_slice */
2029 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00002030};
2031
2032/* set object ********************************************************/
2033
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002034#ifdef Py_DEBUG
2035static PyObject *test_c_api(PySetObject *so);
2036
2037PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2038All is well if assertions don't fail.");
2039#endif
2040
Raymond Hettingera690a992003-11-16 16:17:49 +00002041static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 {"add", (PyCFunction)set_add, METH_O,
2043 add_doc},
2044 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2045 clear_doc},
2046 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2047 contains_doc},
2048 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2049 copy_doc},
2050 {"discard", (PyCFunction)set_discard, METH_O,
2051 discard_doc},
2052 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2053 difference_doc},
2054 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2055 difference_update_doc},
2056 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2057 intersection_doc},
2058 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2059 intersection_update_doc},
2060 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2061 isdisjoint_doc},
2062 {"issubset", (PyCFunction)set_issubset, METH_O,
2063 issubset_doc},
2064 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2065 issuperset_doc},
2066 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2067 pop_doc},
2068 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2069 reduce_doc},
2070 {"remove", (PyCFunction)set_remove, METH_O,
2071 remove_doc},
2072 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2073 sizeof_doc},
2074 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2075 symmetric_difference_doc},
2076 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2077 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002078#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2080 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002081#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 {"union", (PyCFunction)set_union, METH_VARARGS,
2083 union_doc},
2084 {"update", (PyCFunction)set_update, METH_VARARGS,
2085 update_doc},
2086 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002087};
2088
2089static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 0, /*nb_add*/
2091 (binaryfunc)set_sub, /*nb_subtract*/
2092 0, /*nb_multiply*/
2093 0, /*nb_remainder*/
2094 0, /*nb_divmod*/
2095 0, /*nb_power*/
2096 0, /*nb_negative*/
2097 0, /*nb_positive*/
2098 0, /*nb_absolute*/
2099 0, /*nb_bool*/
2100 0, /*nb_invert*/
2101 0, /*nb_lshift*/
2102 0, /*nb_rshift*/
2103 (binaryfunc)set_and, /*nb_and*/
2104 (binaryfunc)set_xor, /*nb_xor*/
2105 (binaryfunc)set_or, /*nb_or*/
2106 0, /*nb_int*/
2107 0, /*nb_reserved*/
2108 0, /*nb_float*/
2109 0, /*nb_inplace_add*/
2110 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2111 0, /*nb_inplace_multiply*/
2112 0, /*nb_inplace_remainder*/
2113 0, /*nb_inplace_power*/
2114 0, /*nb_inplace_lshift*/
2115 0, /*nb_inplace_rshift*/
2116 (binaryfunc)set_iand, /*nb_inplace_and*/
2117 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2118 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002119};
2120
2121PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002122"set() -> new empty set object\n\
2123set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002124\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002125Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002126
2127PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2129 "set", /* tp_name */
2130 sizeof(PySetObject), /* tp_basicsize */
2131 0, /* tp_itemsize */
2132 /* methods */
2133 (destructor)set_dealloc, /* tp_dealloc */
2134 0, /* tp_print */
2135 0, /* tp_getattr */
2136 0, /* tp_setattr */
2137 0, /* tp_reserved */
2138 (reprfunc)set_repr, /* tp_repr */
2139 &set_as_number, /* tp_as_number */
2140 &set_as_sequence, /* tp_as_sequence */
2141 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002142 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 0, /* tp_call */
2144 0, /* tp_str */
2145 PyObject_GenericGetAttr, /* tp_getattro */
2146 0, /* tp_setattro */
2147 0, /* tp_as_buffer */
2148 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2149 Py_TPFLAGS_BASETYPE, /* tp_flags */
2150 set_doc, /* tp_doc */
2151 (traverseproc)set_traverse, /* tp_traverse */
2152 (inquiry)set_clear_internal, /* tp_clear */
2153 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002154 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2155 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 0, /* tp_iternext */
2157 set_methods, /* tp_methods */
2158 0, /* tp_members */
2159 0, /* tp_getset */
2160 0, /* tp_base */
2161 0, /* tp_dict */
2162 0, /* tp_descr_get */
2163 0, /* tp_descr_set */
2164 0, /* tp_dictoffset */
2165 (initproc)set_init, /* tp_init */
2166 PyType_GenericAlloc, /* tp_alloc */
2167 set_new, /* tp_new */
2168 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002169};
2170
2171/* frozenset object ********************************************************/
2172
2173
2174static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2176 contains_doc},
2177 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2178 copy_doc},
2179 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2180 difference_doc},
2181 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2182 intersection_doc},
2183 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2184 isdisjoint_doc},
2185 {"issubset", (PyCFunction)set_issubset, METH_O,
2186 issubset_doc},
2187 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2188 issuperset_doc},
2189 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2190 reduce_doc},
2191 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2192 sizeof_doc},
2193 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2194 symmetric_difference_doc},
2195 {"union", (PyCFunction)set_union, METH_VARARGS,
2196 union_doc},
2197 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002198};
2199
2200static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 0, /*nb_add*/
2202 (binaryfunc)set_sub, /*nb_subtract*/
2203 0, /*nb_multiply*/
2204 0, /*nb_remainder*/
2205 0, /*nb_divmod*/
2206 0, /*nb_power*/
2207 0, /*nb_negative*/
2208 0, /*nb_positive*/
2209 0, /*nb_absolute*/
2210 0, /*nb_bool*/
2211 0, /*nb_invert*/
2212 0, /*nb_lshift*/
2213 0, /*nb_rshift*/
2214 (binaryfunc)set_and, /*nb_and*/
2215 (binaryfunc)set_xor, /*nb_xor*/
2216 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002217};
2218
2219PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002220"frozenset() -> empty frozenset object\n\
2221frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002222\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002223Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002224
2225PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2227 "frozenset", /* tp_name */
2228 sizeof(PySetObject), /* tp_basicsize */
2229 0, /* tp_itemsize */
2230 /* methods */
2231 (destructor)set_dealloc, /* tp_dealloc */
2232 0, /* tp_print */
2233 0, /* tp_getattr */
2234 0, /* tp_setattr */
2235 0, /* tp_reserved */
2236 (reprfunc)set_repr, /* tp_repr */
2237 &frozenset_as_number, /* tp_as_number */
2238 &set_as_sequence, /* tp_as_sequence */
2239 0, /* tp_as_mapping */
2240 frozenset_hash, /* tp_hash */
2241 0, /* tp_call */
2242 0, /* tp_str */
2243 PyObject_GenericGetAttr, /* tp_getattro */
2244 0, /* tp_setattro */
2245 0, /* tp_as_buffer */
2246 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2247 Py_TPFLAGS_BASETYPE, /* tp_flags */
2248 frozenset_doc, /* tp_doc */
2249 (traverseproc)set_traverse, /* tp_traverse */
2250 (inquiry)set_clear_internal, /* tp_clear */
2251 (richcmpfunc)set_richcompare, /* tp_richcompare */
2252 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2253 (getiterfunc)set_iter, /* tp_iter */
2254 0, /* tp_iternext */
2255 frozenset_methods, /* tp_methods */
2256 0, /* tp_members */
2257 0, /* tp_getset */
2258 0, /* tp_base */
2259 0, /* tp_dict */
2260 0, /* tp_descr_get */
2261 0, /* tp_descr_set */
2262 0, /* tp_dictoffset */
2263 0, /* tp_init */
2264 PyType_GenericAlloc, /* tp_alloc */
2265 frozenset_new, /* tp_new */
2266 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002267};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002268
2269
2270/***** C API functions *************************************************/
2271
2272PyObject *
2273PySet_New(PyObject *iterable)
2274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002276}
2277
2278PyObject *
2279PyFrozenSet_New(PyObject *iterable)
2280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002282}
2283
Neal Norwitz8c49c822006-03-04 18:41:19 +00002284Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002285PySet_Size(PyObject *anyset)
2286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 if (!PyAnySet_Check(anyset)) {
2288 PyErr_BadInternalCall();
2289 return -1;
2290 }
2291 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002292}
2293
2294int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002295PySet_Clear(PyObject *set)
2296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 if (!PySet_Check(set)) {
2298 PyErr_BadInternalCall();
2299 return -1;
2300 }
2301 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002302}
2303
2304int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002305PySet_Contains(PyObject *anyset, PyObject *key)
2306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (!PyAnySet_Check(anyset)) {
2308 PyErr_BadInternalCall();
2309 return -1;
2310 }
2311 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002312}
2313
2314int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002315PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!PySet_Check(set)) {
2318 PyErr_BadInternalCall();
2319 return -1;
2320 }
2321 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002322}
2323
2324int
Christian Heimesfd66e512008-01-29 12:18:50 +00002325PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 if (!PySet_Check(anyset) &&
2328 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2329 PyErr_BadInternalCall();
2330 return -1;
2331 }
2332 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002333}
2334
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002335int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002336_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 if (!PyAnySet_Check(set)) {
2341 PyErr_BadInternalCall();
2342 return -1;
2343 }
2344 if (set_next((PySetObject *)set, pos, &entry) == 0)
2345 return 0;
2346 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002347 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002349}
2350
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002351PyObject *
2352PySet_Pop(PyObject *set)
2353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (!PySet_Check(set)) {
2355 PyErr_BadInternalCall();
2356 return NULL;
2357 }
2358 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002359}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002360
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002361int
2362_PySet_Update(PyObject *set, PyObject *iterable)
2363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 if (!PySet_Check(set)) {
2365 PyErr_BadInternalCall();
2366 return -1;
2367 }
2368 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002369}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002370
Raymond Hettingere259f132013-12-15 11:56:14 -08002371/* Exported for the gdb plugin's benefit. */
2372PyObject *_PySet_Dummy = dummy;
2373
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002374#ifdef Py_DEBUG
2375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002377 Returns True and original set is restored. */
2378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379#define assertRaises(call_return_value, exception) \
2380 do { \
2381 assert(call_return_value); \
2382 assert(PyErr_ExceptionMatches(exception)); \
2383 PyErr_Clear(); \
2384 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002385
2386static PyObject *
2387test_c_api(PySetObject *so)
2388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 Py_ssize_t count;
2390 char *s;
2391 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002392 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002394 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 /* Verify preconditions */
2398 assert(PyAnySet_Check(ob));
2399 assert(PyAnySet_CheckExact(ob));
2400 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 /* so.clear(); so |= set("abc"); */
2403 str = PyUnicode_FromString("abc");
2404 if (str == NULL)
2405 return NULL;
2406 set_clear_internal(so);
Raymond Hettinger5af9e132015-05-13 01:44:36 -07002407 if (set_update_internal(so, str)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 Py_DECREF(str);
2409 return NULL;
2410 }
2411 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Exercise type/size checks */
2414 assert(PySet_Size(ob) == 3);
2415 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 /* Raise TypeError for non-iterable constructor arguments */
2418 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2419 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 /* Raise TypeError for unhashable key */
2422 dup = PySet_New(ob);
2423 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2424 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2425 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 /* Exercise successful pop, contains, add, and discard */
2428 elem = PySet_Pop(ob);
2429 assert(PySet_Contains(ob, elem) == 0);
2430 assert(PySet_GET_SIZE(ob) == 2);
2431 assert(PySet_Add(ob, elem) == 0);
2432 assert(PySet_Contains(ob, elem) == 1);
2433 assert(PySet_GET_SIZE(ob) == 3);
2434 assert(PySet_Discard(ob, elem) == 1);
2435 assert(PySet_GET_SIZE(ob) == 2);
2436 assert(PySet_Discard(ob, elem) == 0);
2437 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* Exercise clear */
2440 dup2 = PySet_New(dup);
2441 assert(PySet_Clear(dup2) == 0);
2442 assert(PySet_Size(dup2) == 0);
2443 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 /* Raise SystemError on clear or update of frozen set */
2446 f = PyFrozenSet_New(dup);
2447 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2448 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2449 assert(PySet_Add(f, elem) == 0);
2450 Py_INCREF(f);
2451 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2452 Py_DECREF(f);
2453 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 /* Exercise direct iteration */
2456 i = 0, count = 0;
2457 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2458 s = _PyUnicode_AsString(x);
2459 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2460 count++;
2461 }
2462 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 /* Exercise updates */
2465 dup2 = PySet_New(NULL);
2466 assert(_PySet_Update(dup2, dup) == 0);
2467 assert(PySet_Size(dup2) == 3);
2468 assert(_PySet_Update(dup2, dup) == 0);
2469 assert(PySet_Size(dup2) == 3);
2470 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 /* Raise SystemError when self argument is not a set or frozenset. */
2473 t = PyTuple_New(0);
2474 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2475 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2476 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 /* Raise SystemError when self argument is not a set. */
2479 f = PyFrozenSet_New(dup);
2480 assert(PySet_Size(f) == 3);
2481 assert(PyFrozenSet_CheckExact(f));
2482 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2483 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2484 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* Raise KeyError when popping from an empty set */
2487 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2488 Py_DECREF(ob);
2489 assert(PySet_GET_SIZE(ob) == 0);
2490 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 /* Restore the set from the copy using the PyNumber API */
2493 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2494 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 /* Verify constructors accept NULL arguments */
2497 f = PySet_New(NULL);
2498 assert(f != NULL);
2499 assert(PySet_GET_SIZE(f) == 0);
2500 Py_DECREF(f);
2501 f = PyFrozenSet_New(NULL);
2502 assert(f != NULL);
2503 assert(PyFrozenSet_CheckExact(f));
2504 assert(PySet_GET_SIZE(f) == 0);
2505 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 Py_DECREF(elem);
2508 Py_DECREF(dup);
2509 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002510}
2511
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002512#undef assertRaises
2513
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002514#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002515
2516/***** Dummy Struct *************************************************/
2517
2518static PyObject *
2519dummy_repr(PyObject *op)
2520{
2521 return PyUnicode_FromString("<dummy key>");
2522}
2523
2524static void
2525dummy_dealloc(PyObject* ignore)
2526{
2527 Py_FatalError("deallocating <dummy key>");
2528}
2529
2530static PyTypeObject _PySetDummy_Type = {
2531 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2532 "<dummy key> type",
2533 0,
2534 0,
2535 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2536 0, /*tp_print*/
2537 0, /*tp_getattr*/
2538 0, /*tp_setattr*/
2539 0, /*tp_reserved*/
2540 dummy_repr, /*tp_repr*/
2541 0, /*tp_as_number*/
2542 0, /*tp_as_sequence*/
2543 0, /*tp_as_mapping*/
2544 0, /*tp_hash */
2545 0, /*tp_call */
2546 0, /*tp_str */
2547 0, /*tp_getattro */
2548 0, /*tp_setattro */
2549 0, /*tp_as_buffer */
2550 Py_TPFLAGS_DEFAULT, /*tp_flags */
2551};
2552
2553static PyObject _dummy_struct = {
2554 _PyObject_EXTRA_INIT
2555 2, &_PySetDummy_Type
2556};
2557