blob: 2227128e048427e58e6196069bb1c1bc34f16f6e [file] [log] [blame]
Raymond Hettingerc991db22005-08-11 07:58:45 +00001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002/* set object implementation
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07003
Raymond Hettingera9d99362005-08-05 00:01:15 +00004 Written and maintained by Raymond D. Hettinger <python@rcn.com>
5 Derived from Lib/sets.py and Objects/dictobject.c.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00006
Raymond Hettingerbd9b2002015-01-18 16:10:30 -08007 Copyright (c) 2003-2015 Python Software Foundation.
Raymond Hettingera9d99362005-08-05 00:01:15 +00008 All rights reserved.
Raymond Hettingerae7b00e2013-09-07 15:05:00 -07009
10 The basic lookup function used by all operations.
11 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
12
13 The initial probe index is computed as hash mod the table size.
14 Subsequent probe indices are computed as explained in Objects/dictobject.c.
15
16 To improve cache locality, each probe inspects a series of consecutive
17 nearby entries before moving on to probes elsewhere in memory. This leaves
18 us with a hybrid of linear probing and open addressing. The linear probing
19 reduces the cost of hash collisions because consecutive memory accesses
20 tend to be much cheaper than scattered probes. After LINEAR_PROBES steps,
21 we then use open addressing with the upper bits from the hash value. This
22 helps break-up long chains of collisions.
23
24 All arithmetic on hash should ignore overflow.
25
Raymond Hettinger4d45c102015-01-25 16:27:40 -080026 Unlike the dictionary implementation, the lookkey function can return
Raymond Hettingerae7b00e2013-09-07 15:05:00 -070027 NULL if the rich comparison returns an error.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000028*/
29
Raymond Hettingera690a992003-11-16 16:17:49 +000030#include "Python.h"
Raymond Hettingera9d99362005-08-05 00:01:15 +000031#include "structmember.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000032#include "stringlib/eq.h"
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000033
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000034/* Object used as dummy key to fill deleted entries */
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -050035static PyObject _dummy_struct;
36
37#define dummy (&_dummy_struct)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000038
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000039
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -070040/* ======================================================================== */
41/* ======= Begin logic for probing the hash table ========================= */
42
Raymond Hettinger710a67e2013-09-21 20:17:31 -070043/* Set this to zero to turn-off linear probing */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070044#ifndef LINEAR_PROBES
Raymond Hettinger742d8712013-09-08 00:25:57 -070045#define LINEAR_PROBES 9
Raymond Hettinger8408dc52013-09-15 14:57:15 -070046#endif
Raymond Hettinger742d8712013-09-08 00:25:57 -070047
48/* This must be >= 1 */
49#define PERTURB_SHIFT 5
50
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000051static setentry *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020052set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 setentry *table = so->table;
Raymond Hettingerafe89092013-08-28 20:59:31 -070055 setentry *freeslot = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 setentry *entry;
Raymond Hettingerafe89092013-08-28 20:59:31 -070057 size_t perturb = hash;
58 size_t mask = so->mask;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080059 size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
Raymond Hettinger8408dc52013-09-15 14:57:15 -070060 size_t j;
Raymond Hettinger4ef05282013-09-21 15:39:49 -070061 int cmp;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +000062
Raymond Hettinger59ecabd2015-01-31 02:45:12 -080063 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -070064 if (entry->key == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return entry;
Raymond Hettinger237b34b2013-08-17 02:31:53 -070066
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -070067 while (1) {
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080068 if (entry->hash == hash) {
Raymond Hettingerafe89092013-08-28 20:59:31 -070069 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080070 /* startkey cannot be a dummy because the dummy hash field is -1 */
71 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -080072 if (startkey == key)
73 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -080074 if (PyUnicode_CheckExact(startkey)
75 && PyUnicode_CheckExact(key)
76 && unicode_eq(startkey, key))
77 return entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 Py_INCREF(startkey);
79 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
80 Py_DECREF(startkey);
Raymond Hettinger426d9952014-05-18 21:40:20 +010081 if (cmp < 0) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 return NULL;
Raymond Hettinger426d9952014-05-18 21:40:20 +010083 if (table != so->table || entry->key != startkey) /* unlikely */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 return set_lookkey(so, key, hash);
Raymond Hettinger426d9952014-05-18 21:40:20 +010085 if (cmp > 0) /* likely */
Raymond Hettingerafe89092013-08-28 20:59:31 -070086 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -060087 mask = so->mask; /* help avoid a register spill */
Raymond Hettingerafe89092013-08-28 20:59:31 -070088 }
Raymond Hettinger438f9132015-02-09 06:48:29 -060089 if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerafe89092013-08-28 20:59:31 -070090 freeslot = entry;
91
Raymond Hettingerc658d852015-02-02 08:35:00 -080092 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -080093 for (j = 0 ; j < LINEAR_PROBES ; j++) {
Raymond Hettingerc658d852015-02-02 08:35:00 -080094 entry++;
95 if (entry->key == NULL)
96 goto found_null;
97 if (entry->hash == hash) {
98 PyObject *startkey = entry->key;
99 assert(startkey != dummy);
100 if (startkey == key)
101 return entry;
102 if (PyUnicode_CheckExact(startkey)
103 && PyUnicode_CheckExact(key)
104 && unicode_eq(startkey, key))
105 return entry;
106 Py_INCREF(startkey);
107 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
108 Py_DECREF(startkey);
109 if (cmp < 0)
110 return NULL;
111 if (table != so->table || entry->key != startkey)
112 return set_lookkey(so, key, hash);
113 if (cmp > 0)
114 return entry;
Raymond Hettinger438f9132015-02-09 06:48:29 -0600115 mask = so->mask;
Raymond Hettingerc658d852015-02-02 08:35:00 -0800116 }
Raymond Hettinger438f9132015-02-09 06:48:29 -0600117 if (entry->hash == -1 && freeslot == NULL)
Raymond Hettingerc658d852015-02-02 08:35:00 -0800118 freeslot = entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700121
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700122 perturb >>= PERTURB_SHIFT;
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800123 i = (i * 5 + 1 + perturb) & mask;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700124
Raymond Hettinger59ecabd2015-01-31 02:45:12 -0800125 entry = &table[i];
Raymond Hettingerafe89092013-08-28 20:59:31 -0700126 if (entry->key == NULL)
Raymond Hettinger8408dc52013-09-15 14:57:15 -0700127 goto found_null;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700129 found_null:
Raymond Hettingerafe89092013-08-28 20:59:31 -0700130 return freeslot == NULL ? entry : freeslot;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000131}
132
133/*
Thomas Wouters89f507f2006-12-13 04:49:30 +0000134Internal routine used by set_table_resize() to insert an item which is
135known to be absent from the set. This routine also assumes that
136the set contains no deleted entries. Besides the performance benefit,
137using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
138Note that no refcounts are changed by this routine; if needed, the caller
139is responsible for incref'ing `key`.
140*/
141static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200142set_insert_clean(PySetObject *so, PyObject *key, Py_hash_t hash)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 setentry *table = so->table;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200145 setentry *entry;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700146 size_t perturb = hash;
147 size_t mask = (size_t)so->mask;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800148 size_t i = (size_t)hash & mask;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700149 size_t j;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700151 while (1) {
Raymond Hettinger3037e842015-01-26 21:33:48 -0800152 entry = &table[i];
153 if (entry->key == NULL)
154 goto found_null;
155 if (i + LINEAR_PROBES <= mask) {
Raymond Hettinger82492822015-02-04 08:37:02 -0800156 for (j = 0; j < LINEAR_PROBES; j++) {
Raymond Hettinger9edd7532015-01-30 20:09:23 -0800157 entry++;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800158 if (entry->key == NULL)
159 goto found_null;
160 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700161 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700162 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800163 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700165 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 entry->key = key;
167 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700168 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000170}
171
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700172/* ======== End logic for probing the hash table ========================== */
173/* ======================================================================== */
174
175
176/*
177Internal routine to insert a new key into the table.
178Used by the public insert routine.
179Eats a reference to key.
180*/
181static int
182set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
183{
184 setentry *entry;
185
Raymond Hettinger93035c42015-01-25 16:12:49 -0800186 entry = set_lookkey(so, key, hash);
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700187 if (entry == NULL)
188 return -1;
189 if (entry->key == NULL) {
190 /* UNUSED */
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700191 entry->key = key;
192 entry->hash = hash;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800193 so->fill++;
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700194 so->used++;
195 } else if (entry->key == dummy) {
196 /* DUMMY */
197 entry->key = key;
198 entry->hash = hash;
199 so->used++;
200 } else {
201 /* ACTIVE */
202 Py_DECREF(key);
203 }
204 return 0;
205}
206
Thomas Wouters89f507f2006-12-13 04:49:30 +0000207/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000208Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000209keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000210actually be smaller than the old one.
211*/
212static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000213set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_ssize_t newsize;
216 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800217 Py_ssize_t oldfill = so->fill;
218 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 int is_oldtable_malloced;
220 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100225 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 for (newsize = PySet_MINSIZE;
227 newsize <= minused && newsize > 0;
228 newsize <<= 1)
229 ;
230 if (newsize <= 0) {
231 PyErr_NoMemory();
232 return -1;
233 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 /* Get space for a new table. */
236 oldtable = so->table;
237 assert(oldtable != NULL);
238 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (newsize == PySet_MINSIZE) {
241 /* A large table is shrinking, or we can't get any smaller. */
242 newtable = so->smalltable;
243 if (newtable == oldtable) {
244 if (so->fill == so->used) {
245 /* No dummies, so no point doing anything. */
246 return 0;
247 }
248 /* We're not going to resize it, but rebuild the
249 table anyway to purge old dummy entries.
250 Subtle: This is *necessary* if fill==size,
251 as set_lookkey needs at least one virgin slot to
252 terminate failing searches. If fill < size, it's
253 merely desirable, as dummies slow searches. */
254 assert(so->fill > so->used);
255 memcpy(small_copy, oldtable, sizeof(small_copy));
256 oldtable = small_copy;
257 }
258 }
259 else {
260 newtable = PyMem_NEW(setentry, newsize);
261 if (newtable == NULL) {
262 PyErr_NoMemory();
263 return -1;
264 }
265 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 /* Make the set empty, using the new table. */
268 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800271 so->used = 0;
272 so->mask = newsize - 1;
273 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 /* Copy the data over; this is refcount-neutral for active entries;
276 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800277 if (oldfill == oldused) {
278 for (entry = oldtable; oldused > 0; entry++) {
279 if (entry->key != NULL) {
280 oldused--;
281 set_insert_clean(so, entry->key, entry->hash);
282 }
283 }
284 } else {
285 for (entry = oldtable; oldused > 0; entry++) {
286 if (entry->key != NULL && entry->key != dummy) {
287 oldused--;
288 set_insert_clean(so, entry->key, entry->hash);
289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 }
291 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 if (is_oldtable_malloced)
294 PyMem_DEL(oldtable);
295 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000296}
297
Raymond Hettingerc991db22005-08-11 07:58:45 +0000298/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
299
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000300static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200301set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000302{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200303 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000304 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100305 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 assert(so->fill <= so->mask); /* at least one empty slot */
308 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000309 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100310 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000311 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 return -1;
313 }
314 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
315 return 0;
316 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000317}
318
319static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200320set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000321{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800322 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200323 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200326 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 hash = PyObject_Hash(key);
328 if (hash == -1)
329 return -1;
330 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800331 entry.key = key;
332 entry.hash = hash;
333 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000334}
335
336#define DISCARD_NOTFOUND 0
337#define DISCARD_FOUND 1
338
339static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000340set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800341{
342 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000344
Raymond Hettinger93035c42015-01-25 16:12:49 -0800345 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 if (entry == NULL)
347 return -1;
348 if (entry->key == NULL || entry->key == dummy)
349 return DISCARD_NOTFOUND;
350 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800352 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 so->used--;
354 Py_DECREF(old_key);
355 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000356}
357
358static int
359set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000360{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800361 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200362 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200367 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 hash = PyObject_Hash(key);
369 if (hash == -1)
370 return -1;
371 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800372 entry.key = key;
373 entry.hash = hash;
374 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000375}
376
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700377static void
378set_empty_to_minsize(PySetObject *so)
379{
380 memset(so->smalltable, 0, sizeof(so->smalltable));
381 so->fill = 0;
382 so->used = 0;
383 so->mask = PySet_MINSIZE - 1;
384 so->table = so->smalltable;
385 so->hash = -1;
386}
387
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000388static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000389set_clear_internal(PySetObject *so)
390{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800391 setentry *entry;
392 setentry *table = so->table;
393 Py_ssize_t fill = so->fill;
394 Py_ssize_t used = so->used;
395 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000397
Raymond Hettinger583cd032013-09-07 22:06:35 -0700398 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 /* This is delicate. During the process of clearing the set,
402 * decrefs can cause the set to mutate. To avoid fatal confusion
403 * (voice of experience), we have to make the set empty before
404 * clearing the slots, and never refer to anything via so->ref while
405 * clearing.
406 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700408 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 else if (fill > 0) {
411 /* It's a small table with something that needs to be cleared.
412 * Afraid the only safe way is to copy the set entries into
413 * another small table first.
414 */
415 memcpy(small_copy, table, sizeof(small_copy));
416 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700417 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 }
419 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 /* Now we can finally clear things. If C had refcounts, we could
422 * assert that the refcount on table is 1 now, i.e. that this function
423 * has unique access to it, so decref side-effects can't alter it.
424 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800425 for (entry = table; used > 0; entry++) {
426 if (entry->key && entry->key != dummy) {
427 used--;
428 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (table_is_malloced)
433 PyMem_DEL(table);
434 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000435}
436
437/*
438 * Iterate over a set table. Use like so:
439 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000440 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000441 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000442 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000443 * while (set_next(yourset, &pos, &entry)) {
444 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000445 * }
446 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000447 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000449 */
450static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 Py_ssize_t i;
454 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200455 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 assert (PyAnySet_Check(so));
458 i = *pos_ptr;
459 assert(i >= 0);
460 table = so->table;
461 mask = so->mask;
462 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
463 i++;
464 *pos_ptr = i+1;
465 if (i > mask)
466 return 0;
467 assert(table[i].key != NULL);
468 *entry_ptr = &table[i];
469 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000470}
471
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000472static void
473set_dealloc(PySetObject *so)
474{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200475 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800476 Py_ssize_t used = so->used;
477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 PyObject_GC_UnTrack(so);
479 Py_TRASHCAN_SAFE_BEGIN(so)
480 if (so->weakreflist != NULL)
481 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000482
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800483 for (entry = so->table; used > 0; entry++) {
484 if (entry->key && entry->key != dummy) {
485 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700486 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 }
488 }
489 if (so->table != so->smalltable)
490 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700491 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000493}
494
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000495static PyObject *
496set_repr(PySetObject *so)
497{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200498 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (status != 0) {
502 if (status < 0)
503 return NULL;
504 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
505 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 /* shortcut for the empty set */
508 if (!so->used) {
509 Py_ReprLeave((PyObject*)so);
510 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
511 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 keys = PySequence_List((PyObject *)so);
514 if (keys == NULL)
515 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000516
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200517 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 listrepr = PyObject_Repr(keys);
519 Py_DECREF(keys);
520 if (listrepr == NULL)
521 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200522 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200524 if (tmp == NULL)
525 goto done;
526 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200527
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200528 if (Py_TYPE(so) != &PySet_Type)
529 result = PyUnicode_FromFormat("%s({%U})",
530 Py_TYPE(so)->tp_name,
531 listrepr);
532 else
533 result = PyUnicode_FromFormat("{%U}", listrepr);
534 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000535done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_ReprLeave((PyObject*)so);
537 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000538}
539
Martin v. Löwis18e16552006-02-15 17:27:45 +0000540static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000541set_len(PyObject *so)
542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000544}
545
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000546static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000547set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000550 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200551 Py_ssize_t i;
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700552 setentry *so_entry;
553 setentry *other_entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 assert (PyAnySet_Check(so));
556 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 other = (PySetObject*)otherset;
559 if (other == so || other->used == 0)
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700560 /* a.update(a) or a.update(set()); nothing to do */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 return 0;
562 /* Do one big resize at the start, rather than
563 * incrementally resizing as we insert new keys. Expect
564 * that there will be no (or few) overlapping keys.
565 */
566 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
567 if (set_table_resize(so, (so->used + other->used)*2) != 0)
568 return -1;
569 }
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700570 so_entry = so->table;
571 other_entry = other->table;
572
573 /* If our table is empty, and both tables have the same size, and
574 there are no dummies to eliminate, then just copy the pointers. */
575 if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
576 for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
577 key = other_entry->key;
578 if (key != NULL) {
579 assert(so_entry->key == NULL);
580 Py_INCREF(key);
581 so_entry->key = key;
582 so_entry->hash = other_entry->hash;
583 }
584 }
585 so->fill = other->fill;
586 so->used = other->used;
587 return 0;
588 }
589
590 /* If our table is empty, we can use set_insert_clean() */
591 if (so->fill == 0) {
592 for (i = 0; i <= other->mask; i++, other_entry++) {
593 key = other_entry->key;
594 if (key != NULL && key != dummy) {
595 Py_INCREF(key);
596 set_insert_clean(so, key, other_entry->hash);
597 }
598 }
599 return 0;
600 }
601
602 /* We can't assure there are no duplicates, so do normal insertions */
603 for (i = 0; i <= other->mask; i++, other_entry++) {
604 key = other_entry->key;
605 if (key != NULL && key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000606 Py_INCREF(key);
Raymond Hettinger1bd8d752015-05-13 01:26:14 -0700607 if (set_insert_key(so, key, other_entry->hash)) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000608 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 return -1;
610 }
611 }
612 }
613 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000614}
615
616static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000617set_contains_entry(PySetObject *so, setentry *entry)
618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PyObject *key;
620 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000621
Raymond Hettinger93035c42015-01-25 16:12:49 -0800622 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 if (lu_entry == NULL)
624 return -1;
625 key = lu_entry->key;
626 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000627}
628
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800629static int
630set_contains_key(PySetObject *so, PyObject *key)
631{
632 setentry entry;
633 Py_hash_t hash;
634
635 if (!PyUnicode_CheckExact(key) ||
636 (hash = ((PyASCIIObject *) key)->hash) == -1) {
637 hash = PyObject_Hash(key);
638 if (hash == -1)
639 return -1;
640 }
641 entry.key = key;
642 entry.hash = hash;
643 return set_contains_entry(so, &entry);
644}
645
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000646static PyObject *
647set_pop(PySetObject *so)
648{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800649 /* Make sure the search finger is in bounds */
650 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200651 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 assert (PyAnySet_Check(so));
655 if (so->used == 0) {
656 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
657 return NULL;
658 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000659
Raymond Hettinger1202a472015-01-18 13:12:42 -0800660 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
661 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800662 if (i > so->mask)
663 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 }
665 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800667 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800669 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000671}
672
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000673PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
674Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000675
676static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000677set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 Py_ssize_t pos = 0;
680 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 while (set_next(so, &pos, &entry))
683 Py_VISIT(entry->key);
684 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000685}
686
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000687static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000688frozenset_hash(PyObject *self)
689{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800690 /* Most of the constants in this hash algorithm are randomly choosen
691 large primes with "interesting bit patterns" and that passed
692 tests for good collision statistics on a variety of problematic
693 datasets such as:
694
695 ps = []
696 for r in range(21):
697 ps += itertools.combinations(range(20), r)
698 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
699
700 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800702 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 setentry *entry;
704 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (so->hash != -1)
707 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000708
Mark Dickinson57e683e2011-09-24 18:18:40 +0100709 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 while (set_next(so, &pos, &entry)) {
711 /* Work to increase the bit dispersion for closely spaced hash
Raymond Hettinger06a1c8d2015-01-30 18:02:15 -0800712 values. This is important because some use cases have many
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 combinations of a small number of elements with nearby
714 hashes so that many distinct combinations collapse to only
715 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000716 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800717 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800719 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500720 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800721 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200722 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800723 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 so->hash = hash;
725 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000726}
727
Raymond Hettingera9d99362005-08-05 00:01:15 +0000728/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000729
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000730typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 PyObject_HEAD
732 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
733 Py_ssize_t si_used;
734 Py_ssize_t si_pos;
735 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000736} setiterobject;
737
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000738static void
739setiter_dealloc(setiterobject *si)
740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 Py_XDECREF(si->si_set);
742 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000743}
744
745static int
746setiter_traverse(setiterobject *si, visitproc visit, void *arg)
747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 Py_VISIT(si->si_set);
749 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000750}
751
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000752static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000753setiter_len(setiterobject *si)
754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 Py_ssize_t len = 0;
756 if (si->si_set != NULL && si->si_used == si->si_set->used)
757 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000758 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000759}
760
Armin Rigof5b3e362006-02-11 21:32:43 +0000761PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000762
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000763static PyObject *setiter_iternext(setiterobject *si);
764
765static PyObject *
766setiter_reduce(setiterobject *si)
767{
768 PyObject *list;
769 setiterobject tmp;
770
771 list = PyList_New(0);
772 if (!list)
773 return NULL;
774
Ezio Melotti0e1af282012-09-28 16:43:40 +0300775 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000776 tmp = *si;
777 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300778
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000779 /* iterate the temporary into a list */
780 for(;;) {
781 PyObject *element = setiter_iternext(&tmp);
782 if (element) {
783 if (PyList_Append(list, element)) {
784 Py_DECREF(element);
785 Py_DECREF(list);
786 Py_XDECREF(tmp.si_set);
787 return NULL;
788 }
789 Py_DECREF(element);
790 } else
791 break;
792 }
793 Py_XDECREF(tmp.si_set);
794 /* check for error */
795 if (tmp.si_set != NULL) {
796 /* we have an error */
797 Py_DECREF(list);
798 return NULL;
799 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200800 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000801}
802
803PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
804
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000805static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000807 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000809};
810
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000811static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200814 Py_ssize_t i, mask;
815 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (so == NULL)
819 return NULL;
820 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 if (si->si_used != so->used) {
823 PyErr_SetString(PyExc_RuntimeError,
824 "Set changed size during iteration");
825 si->si_used = -1; /* Make this state sticky */
826 return NULL;
827 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 i = si->si_pos;
830 assert(i>=0);
831 entry = so->table;
832 mask = so->mask;
833 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
834 i++;
835 si->si_pos = i+1;
836 if (i > mask)
837 goto fail;
838 si->len--;
839 key = entry[i].key;
840 Py_INCREF(key);
841 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000842
843fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 Py_DECREF(so);
845 si->si_set = NULL;
846 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000847}
848
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000849PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 PyVarObject_HEAD_INIT(&PyType_Type, 0)
851 "set_iterator", /* tp_name */
852 sizeof(setiterobject), /* tp_basicsize */
853 0, /* tp_itemsize */
854 /* methods */
855 (destructor)setiter_dealloc, /* tp_dealloc */
856 0, /* tp_print */
857 0, /* tp_getattr */
858 0, /* tp_setattr */
859 0, /* tp_reserved */
860 0, /* tp_repr */
861 0, /* tp_as_number */
862 0, /* tp_as_sequence */
863 0, /* tp_as_mapping */
864 0, /* tp_hash */
865 0, /* tp_call */
866 0, /* tp_str */
867 PyObject_GenericGetAttr, /* tp_getattro */
868 0, /* tp_setattro */
869 0, /* tp_as_buffer */
870 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
871 0, /* tp_doc */
872 (traverseproc)setiter_traverse, /* tp_traverse */
873 0, /* tp_clear */
874 0, /* tp_richcompare */
875 0, /* tp_weaklistoffset */
876 PyObject_SelfIter, /* tp_iter */
877 (iternextfunc)setiter_iternext, /* tp_iternext */
878 setiter_methods, /* tp_methods */
879 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000880};
881
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882static PyObject *
883set_iter(PySetObject *so)
884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
886 if (si == NULL)
887 return NULL;
888 Py_INCREF(so);
889 si->si_set = so;
890 si->si_used = so->used;
891 si->si_pos = 0;
892 si->len = so->used;
893 _PyObject_GC_TRACK(si);
894 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000895}
896
Raymond Hettingerd7946662005-08-01 21:39:29 +0000897static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000898set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 if (PyAnySet_Check(other))
903 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 if (PyDict_CheckExact(other)) {
906 PyObject *value;
907 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000908 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 /* Do one big resize at the start, rather than
912 * incrementally resizing as we insert new keys. Expect
913 * that there will be no (or few) overlapping keys.
914 */
915 if (dictsize == -1)
916 return -1;
917 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
918 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
919 return -1;
920 }
921 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
922 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 an_entry.hash = hash;
925 an_entry.key = key;
926 if (set_add_entry(so, &an_entry) == -1)
927 return -1;
928 }
929 return 0;
930 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 it = PyObject_GetIter(other);
933 if (it == NULL)
934 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 while ((key = PyIter_Next(it)) != NULL) {
937 if (set_add_key(so, key) == -1) {
938 Py_DECREF(it);
939 Py_DECREF(key);
940 return -1;
941 }
942 Py_DECREF(key);
943 }
944 Py_DECREF(it);
945 if (PyErr_Occurred())
946 return -1;
947 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000948}
949
950static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000951set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
956 PyObject *other = PyTuple_GET_ITEM(args, i);
957 if (set_update_internal(so, other) == -1)
958 return NULL;
959 }
960 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000961}
962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000964"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000965
Raymond Hettinger426d9952014-05-18 21:40:20 +0100966/* XXX Todo:
967 If aligned memory allocations become available, make the
968 set object 64 byte aligned so that most of the fields
969 can be retrieved or updated in a single cache line.
970*/
971
Raymond Hettingera38123e2003-11-24 22:18:49 +0000972static PyObject *
973make_new_set(PyTypeObject *type, PyObject *iterable)
974{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200975 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700978 so = (PySetObject *)type->tp_alloc(type, 0);
979 if (so == NULL)
980 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000981
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700982 so->fill = 0;
983 so->used = 0;
984 so->mask = PySet_MINSIZE - 1;
985 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700986 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800987 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (iterable != NULL) {
991 if (set_update_internal(so, iterable) == -1) {
992 Py_DECREF(so);
993 return NULL;
994 }
995 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +0000998}
999
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001000static PyObject *
1001make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1004 if (PyType_IsSubtype(type, &PySet_Type))
1005 type = &PySet_Type;
1006 else
1007 type = &PyFrozenSet_Type;
1008 }
1009 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +00001010}
1011
Raymond Hettingerd7946662005-08-01 21:39:29 +00001012/* The empty frozenset is a singleton */
1013static PyObject *emptyfrozenset = NULL;
1014
Raymond Hettingera690a992003-11-16 16:17:49 +00001015static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001016frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +00001017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1021 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1024 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (type != &PyFrozenSet_Type)
1027 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (iterable != NULL) {
1030 /* frozenset(f) is idempotent */
1031 if (PyFrozenSet_CheckExact(iterable)) {
1032 Py_INCREF(iterable);
1033 return iterable;
1034 }
1035 result = make_new_set(type, iterable);
1036 if (result == NULL || PySet_GET_SIZE(result))
1037 return result;
1038 Py_DECREF(result);
1039 }
1040 /* The empty frozenset is a singleton */
1041 if (emptyfrozenset == NULL)
1042 emptyfrozenset = make_new_set(type, NULL);
1043 Py_XINCREF(emptyfrozenset);
1044 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001045}
1046
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001047int
1048PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001049{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001050 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001051}
1052
1053void
1054PySet_Fini(void)
1055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001057}
1058
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001059static PyObject *
1060set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1063 return NULL;
1064
1065 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001066}
1067
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001068/* set_swap_bodies() switches the contents of any two sets by moving their
1069 internal data pointers and, if needed, copying the internal smalltables.
1070 Semantically equivalent to:
1071
1072 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1073
1074 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001075 Useful for operations that update in-place (by allowing an intermediate
1076 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001077*/
1078
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001079static void
1080set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 Py_ssize_t t;
1083 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001085 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 t = a->fill; a->fill = b->fill; b->fill = t;
1088 t = a->used; a->used = b->used; b->used = t;
1089 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 u = a->table;
1092 if (a->table == a->smalltable)
1093 u = b->smalltable;
1094 a->table = b->table;
1095 if (b->table == b->smalltable)
1096 a->table = a->smalltable;
1097 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (a->table == a->smalltable || b->table == b->smalltable) {
1100 memcpy(tab, a->smalltable, sizeof(tab));
1101 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1102 memcpy(b->smalltable, tab, sizeof(tab));
1103 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1106 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1107 h = a->hash; a->hash = b->hash; b->hash = h;
1108 } else {
1109 a->hash = -1;
1110 b->hash = -1;
1111 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001112}
1113
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001114static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001115set_copy(PySetObject *so)
1116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001118}
1119
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001120static PyObject *
1121frozenset_copy(PySetObject *so)
1122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (PyFrozenSet_CheckExact(so)) {
1124 Py_INCREF(so);
1125 return (PyObject *)so;
1126 }
1127 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001128}
1129
Raymond Hettingera690a992003-11-16 16:17:49 +00001130PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1131
1132static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001133set_clear(PySetObject *so)
1134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 set_clear_internal(so);
1136 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001137}
1138
1139PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1140
1141static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001142set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 PySetObject *result;
1145 PyObject *other;
1146 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 result = (PySetObject *)set_copy(so);
1149 if (result == NULL)
1150 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1153 other = PyTuple_GET_ITEM(args, i);
1154 if ((PyObject *)so == other)
1155 continue;
1156 if (set_update_internal(result, other) == -1) {
1157 Py_DECREF(result);
1158 return NULL;
1159 }
1160 }
1161 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001162}
1163
1164PyDoc_STRVAR(union_doc,
1165 "Return the union of sets as a new set.\n\
1166\n\
1167(i.e. all elements that are in either set.)");
1168
1169static PyObject *
1170set_or(PySetObject *so, PyObject *other)
1171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001173
Brian Curtindfc80e32011-08-10 20:28:54 -05001174 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1175 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 result = (PySetObject *)set_copy(so);
1178 if (result == NULL)
1179 return NULL;
1180 if ((PyObject *)so == other)
1181 return (PyObject *)result;
1182 if (set_update_internal(result, other) == -1) {
1183 Py_DECREF(result);
1184 return NULL;
1185 }
1186 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001187}
1188
Raymond Hettingera690a992003-11-16 16:17:49 +00001189static PyObject *
1190set_ior(PySetObject *so, PyObject *other)
1191{
Brian Curtindfc80e32011-08-10 20:28:54 -05001192 if (!PyAnySet_Check(other))
1193 Py_RETURN_NOTIMPLEMENTED;
1194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 if (set_update_internal(so, other) == -1)
1196 return NULL;
1197 Py_INCREF(so);
1198 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001199}
1200
1201static PyObject *
1202set_intersection(PySetObject *so, PyObject *other)
1203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 PySetObject *result;
1205 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 if ((PyObject *)so == other)
1208 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1211 if (result == NULL)
1212 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (PyAnySet_Check(other)) {
1215 Py_ssize_t pos = 0;
1216 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1219 tmp = (PyObject *)so;
1220 so = (PySetObject *)other;
1221 other = tmp;
1222 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 while (set_next((PySetObject *)other, &pos, &entry)) {
1225 int rv = set_contains_entry(so, entry);
1226 if (rv == -1) {
1227 Py_DECREF(result);
1228 return NULL;
1229 }
1230 if (rv) {
1231 if (set_add_entry(result, entry) == -1) {
1232 Py_DECREF(result);
1233 return NULL;
1234 }
1235 }
1236 }
1237 return (PyObject *)result;
1238 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 it = PyObject_GetIter(other);
1241 if (it == NULL) {
1242 Py_DECREF(result);
1243 return NULL;
1244 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 while ((key = PyIter_Next(it)) != NULL) {
1247 int rv;
1248 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001249 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (hash == -1) {
1252 Py_DECREF(it);
1253 Py_DECREF(result);
1254 Py_DECREF(key);
1255 return NULL;
1256 }
1257 entry.hash = hash;
1258 entry.key = key;
1259 rv = set_contains_entry(so, &entry);
1260 if (rv == -1) {
1261 Py_DECREF(it);
1262 Py_DECREF(result);
1263 Py_DECREF(key);
1264 return NULL;
1265 }
1266 if (rv) {
1267 if (set_add_entry(result, &entry) == -1) {
1268 Py_DECREF(it);
1269 Py_DECREF(result);
1270 Py_DECREF(key);
1271 return NULL;
1272 }
1273 }
1274 Py_DECREF(key);
1275 }
1276 Py_DECREF(it);
1277 if (PyErr_Occurred()) {
1278 Py_DECREF(result);
1279 return NULL;
1280 }
1281 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001282}
1283
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001284static PyObject *
1285set_intersection_multi(PySetObject *so, PyObject *args)
1286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 Py_ssize_t i;
1288 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (PyTuple_GET_SIZE(args) == 0)
1291 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 Py_INCREF(so);
1294 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1295 PyObject *other = PyTuple_GET_ITEM(args, i);
1296 PyObject *newresult = set_intersection((PySetObject *)result, other);
1297 if (newresult == NULL) {
1298 Py_DECREF(result);
1299 return NULL;
1300 }
1301 Py_DECREF(result);
1302 result = newresult;
1303 }
1304 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001305}
1306
Raymond Hettingera690a992003-11-16 16:17:49 +00001307PyDoc_STRVAR(intersection_doc,
1308"Return the intersection of two sets as a new set.\n\
1309\n\
1310(i.e. all elements that are in both sets.)");
1311
1312static PyObject *
1313set_intersection_update(PySetObject *so, PyObject *other)
1314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 tmp = set_intersection(so, other);
1318 if (tmp == NULL)
1319 return NULL;
1320 set_swap_bodies(so, (PySetObject *)tmp);
1321 Py_DECREF(tmp);
1322 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001323}
1324
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001325static PyObject *
1326set_intersection_update_multi(PySetObject *so, PyObject *args)
1327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 tmp = set_intersection_multi(so, args);
1331 if (tmp == NULL)
1332 return NULL;
1333 set_swap_bodies(so, (PySetObject *)tmp);
1334 Py_DECREF(tmp);
1335 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001336}
1337
Raymond Hettingera690a992003-11-16 16:17:49 +00001338PyDoc_STRVAR(intersection_update_doc,
1339"Update a set with the intersection of itself and another.");
1340
1341static PyObject *
1342set_and(PySetObject *so, PyObject *other)
1343{
Brian Curtindfc80e32011-08-10 20:28:54 -05001344 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1345 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001347}
1348
1349static PyObject *
1350set_iand(PySetObject *so, PyObject *other)
1351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001353
Brian Curtindfc80e32011-08-10 20:28:54 -05001354 if (!PyAnySet_Check(other))
1355 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 result = set_intersection_update(so, other);
1357 if (result == NULL)
1358 return NULL;
1359 Py_DECREF(result);
1360 Py_INCREF(so);
1361 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001362}
1363
Guido van Rossum58da9312007-11-10 23:39:45 +00001364static PyObject *
1365set_isdisjoint(PySetObject *so, PyObject *other)
1366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if ((PyObject *)so == other) {
1370 if (PySet_GET_SIZE(so) == 0)
1371 Py_RETURN_TRUE;
1372 else
1373 Py_RETURN_FALSE;
1374 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if (PyAnySet_CheckExact(other)) {
1377 Py_ssize_t pos = 0;
1378 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1381 tmp = (PyObject *)so;
1382 so = (PySetObject *)other;
1383 other = tmp;
1384 }
1385 while (set_next((PySetObject *)other, &pos, &entry)) {
1386 int rv = set_contains_entry(so, entry);
1387 if (rv == -1)
1388 return NULL;
1389 if (rv)
1390 Py_RETURN_FALSE;
1391 }
1392 Py_RETURN_TRUE;
1393 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 it = PyObject_GetIter(other);
1396 if (it == NULL)
1397 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 while ((key = PyIter_Next(it)) != NULL) {
1400 int rv;
1401 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001402 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 if (hash == -1) {
1405 Py_DECREF(key);
1406 Py_DECREF(it);
1407 return NULL;
1408 }
1409 entry.hash = hash;
1410 entry.key = key;
1411 rv = set_contains_entry(so, &entry);
1412 Py_DECREF(key);
1413 if (rv == -1) {
1414 Py_DECREF(it);
1415 return NULL;
1416 }
1417 if (rv) {
1418 Py_DECREF(it);
1419 Py_RETURN_FALSE;
1420 }
1421 }
1422 Py_DECREF(it);
1423 if (PyErr_Occurred())
1424 return NULL;
1425 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001426}
1427
1428PyDoc_STRVAR(isdisjoint_doc,
1429"Return True if two sets have a null intersection.");
1430
Neal Norwitz6576bd82005-11-13 18:41:28 +00001431static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001432set_difference_update_internal(PySetObject *so, PyObject *other)
1433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if ((PyObject *)so == other)
1435 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (PyAnySet_Check(other)) {
1438 setentry *entry;
1439 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 while (set_next((PySetObject *)other, &pos, &entry))
1442 if (set_discard_entry(so, entry) == -1)
1443 return -1;
1444 } else {
1445 PyObject *key, *it;
1446 it = PyObject_GetIter(other);
1447 if (it == NULL)
1448 return -1;
1449
1450 while ((key = PyIter_Next(it)) != NULL) {
1451 if (set_discard_key(so, key) == -1) {
1452 Py_DECREF(it);
1453 Py_DECREF(key);
1454 return -1;
1455 }
1456 Py_DECREF(key);
1457 }
1458 Py_DECREF(it);
1459 if (PyErr_Occurred())
1460 return -1;
1461 }
1462 /* If more than 1/5 are dummies, then resize them away. */
1463 if ((so->fill - so->used) * 5 < so->mask)
1464 return 0;
1465 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001466}
1467
Raymond Hettingera690a992003-11-16 16:17:49 +00001468static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001469set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1474 PyObject *other = PyTuple_GET_ITEM(args, i);
1475 if (set_difference_update_internal(so, other) == -1)
1476 return NULL;
1477 }
1478 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001479}
1480
1481PyDoc_STRVAR(difference_update_doc,
1482"Remove all elements of another set from this set.");
1483
1484static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001485set_copy_and_difference(PySetObject *so, PyObject *other)
1486{
1487 PyObject *result;
1488
1489 result = set_copy(so);
1490 if (result == NULL)
1491 return NULL;
1492 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1493 return result;
1494 Py_DECREF(result);
1495 return NULL;
1496}
1497
1498static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001499set_difference(PySetObject *so, PyObject *other)
1500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 PyObject *result;
1502 setentry *entry;
1503 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001506 return set_copy_and_difference(so, other);
1507 }
1508
1509 /* If len(so) much more than len(other), it's more efficient to simply copy
1510 * so and then iterate other looking for common elements. */
1511 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1512 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 result = make_new_set_basetype(Py_TYPE(so), NULL);
1516 if (result == NULL)
1517 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 if (PyDict_CheckExact(other)) {
1520 while (set_next(so, &pos, &entry)) {
1521 setentry entrycopy;
1522 entrycopy.hash = entry->hash;
1523 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001524 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1526 Py_DECREF(result);
1527 return NULL;
1528 }
1529 }
1530 }
1531 return result;
1532 }
1533
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001534 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 while (set_next(so, &pos, &entry)) {
1536 int rv = set_contains_entry((PySetObject *)other, entry);
1537 if (rv == -1) {
1538 Py_DECREF(result);
1539 return NULL;
1540 }
1541 if (!rv) {
1542 if (set_add_entry((PySetObject *)result, entry) == -1) {
1543 Py_DECREF(result);
1544 return NULL;
1545 }
1546 }
1547 }
1548 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001549}
1550
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001551static PyObject *
1552set_difference_multi(PySetObject *so, PyObject *args)
1553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 Py_ssize_t i;
1555 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (PyTuple_GET_SIZE(args) == 0)
1558 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 other = PyTuple_GET_ITEM(args, 0);
1561 result = set_difference(so, other);
1562 if (result == NULL)
1563 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1566 other = PyTuple_GET_ITEM(args, i);
1567 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1568 Py_DECREF(result);
1569 return NULL;
1570 }
1571 }
1572 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001573}
1574
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001575PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001576"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001577\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001578(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001579static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001580set_sub(PySetObject *so, PyObject *other)
1581{
Brian Curtindfc80e32011-08-10 20:28:54 -05001582 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1583 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001585}
1586
1587static PyObject *
1588set_isub(PySetObject *so, PyObject *other)
1589{
Brian Curtindfc80e32011-08-10 20:28:54 -05001590 if (!PyAnySet_Check(other))
1591 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 if (set_difference_update_internal(so, other) == -1)
1593 return NULL;
1594 Py_INCREF(so);
1595 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001596}
1597
1598static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001599set_symmetric_difference_update(PySetObject *so, PyObject *other)
1600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 PySetObject *otherset;
1602 PyObject *key;
1603 Py_ssize_t pos = 0;
1604 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if ((PyObject *)so == other)
1607 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 if (PyDict_CheckExact(other)) {
1610 PyObject *value;
1611 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001612 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1614 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001615
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001616 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 an_entry.hash = hash;
1618 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001621 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001622 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001625 if (rv == DISCARD_NOTFOUND) {
1626 if (set_add_entry(so, &an_entry) == -1) {
1627 Py_DECREF(key);
1628 return NULL;
1629 }
1630 }
Georg Brandl2d444492010-09-03 10:52:55 +00001631 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 }
1633 Py_RETURN_NONE;
1634 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 if (PyAnySet_Check(other)) {
1637 Py_INCREF(other);
1638 otherset = (PySetObject *)other;
1639 } else {
1640 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1641 if (otherset == NULL)
1642 return NULL;
1643 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 while (set_next(otherset, &pos, &entry)) {
1646 int rv = set_discard_entry(so, entry);
1647 if (rv == -1) {
1648 Py_DECREF(otherset);
1649 return NULL;
1650 }
1651 if (rv == DISCARD_NOTFOUND) {
1652 if (set_add_entry(so, entry) == -1) {
1653 Py_DECREF(otherset);
1654 return NULL;
1655 }
1656 }
1657 }
1658 Py_DECREF(otherset);
1659 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001660}
1661
1662PyDoc_STRVAR(symmetric_difference_update_doc,
1663"Update a set with the symmetric difference of itself and another.");
1664
1665static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001666set_symmetric_difference(PySetObject *so, PyObject *other)
1667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 PyObject *rv;
1669 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1672 if (otherset == NULL)
1673 return NULL;
1674 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1675 if (rv == NULL)
1676 return NULL;
1677 Py_DECREF(rv);
1678 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001679}
1680
1681PyDoc_STRVAR(symmetric_difference_doc,
1682"Return the symmetric difference of two sets as a new set.\n\
1683\n\
1684(i.e. all elements that are in exactly one of the sets.)");
1685
1686static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001687set_xor(PySetObject *so, PyObject *other)
1688{
Brian Curtindfc80e32011-08-10 20:28:54 -05001689 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1690 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001692}
1693
1694static PyObject *
1695set_ixor(PySetObject *so, PyObject *other)
1696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001698
Brian Curtindfc80e32011-08-10 20:28:54 -05001699 if (!PyAnySet_Check(other))
1700 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 result = set_symmetric_difference_update(so, other);
1702 if (result == NULL)
1703 return NULL;
1704 Py_DECREF(result);
1705 Py_INCREF(so);
1706 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001707}
1708
1709static PyObject *
1710set_issubset(PySetObject *so, PyObject *other)
1711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 setentry *entry;
1713 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (!PyAnySet_Check(other)) {
1716 PyObject *tmp, *result;
1717 tmp = make_new_set(&PySet_Type, other);
1718 if (tmp == NULL)
1719 return NULL;
1720 result = set_issubset(so, tmp);
1721 Py_DECREF(tmp);
1722 return result;
1723 }
1724 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1725 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 while (set_next(so, &pos, &entry)) {
1728 int rv = set_contains_entry((PySetObject *)other, entry);
1729 if (rv == -1)
1730 return NULL;
1731 if (!rv)
1732 Py_RETURN_FALSE;
1733 }
1734 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001735}
1736
1737PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1738
1739static PyObject *
1740set_issuperset(PySetObject *so, PyObject *other)
1741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 if (!PyAnySet_Check(other)) {
1745 tmp = make_new_set(&PySet_Type, other);
1746 if (tmp == NULL)
1747 return NULL;
1748 result = set_issuperset(so, tmp);
1749 Py_DECREF(tmp);
1750 return result;
1751 }
1752 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001753}
1754
1755PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1756
Raymond Hettingera690a992003-11-16 16:17:49 +00001757static PyObject *
1758set_richcompare(PySetObject *v, PyObject *w, int op)
1759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001761
Brian Curtindfc80e32011-08-10 20:28:54 -05001762 if(!PyAnySet_Check(w))
1763 Py_RETURN_NOTIMPLEMENTED;
1764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 switch (op) {
1766 case Py_EQ:
1767 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1768 Py_RETURN_FALSE;
1769 if (v->hash != -1 &&
1770 ((PySetObject *)w)->hash != -1 &&
1771 v->hash != ((PySetObject *)w)->hash)
1772 Py_RETURN_FALSE;
1773 return set_issubset(v, w);
1774 case Py_NE:
1775 r1 = set_richcompare(v, w, Py_EQ);
1776 if (r1 == NULL)
1777 return NULL;
1778 r2 = PyBool_FromLong(PyObject_Not(r1));
1779 Py_DECREF(r1);
1780 return r2;
1781 case Py_LE:
1782 return set_issubset(v, w);
1783 case Py_GE:
1784 return set_issuperset(v, w);
1785 case Py_LT:
1786 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1787 Py_RETURN_FALSE;
1788 return set_issubset(v, w);
1789 case Py_GT:
1790 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1791 Py_RETURN_FALSE;
1792 return set_issuperset(v, w);
1793 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001794 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001795}
1796
Raymond Hettingera690a992003-11-16 16:17:49 +00001797static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001798set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 if (set_add_key(so, key) == -1)
1801 return NULL;
1802 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001803}
1804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001806"Add an element to a set.\n\
1807\n\
1808This has no effect if the element is already present.");
1809
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001810static int
1811set_contains(PySetObject *so, PyObject *key)
1812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 PyObject *tmpkey;
1814 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 rv = set_contains_key(so, key);
1817 if (rv == -1) {
1818 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1819 return -1;
1820 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001821 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 if (tmpkey == NULL)
1823 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001824 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 Py_DECREF(tmpkey);
1826 }
1827 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001828}
1829
1830static PyObject *
1831set_direct_contains(PySetObject *so, PyObject *key)
1832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 result = set_contains(so, key);
1836 if (result == -1)
1837 return NULL;
1838 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001839}
1840
1841PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1842
Raymond Hettingera690a992003-11-16 16:17:49 +00001843static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001844set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 PyObject *tmpkey;
1847 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 rv = set_discard_key(so, key);
1850 if (rv == -1) {
1851 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1852 return NULL;
1853 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001854 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 if (tmpkey == NULL)
1856 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 Py_DECREF(tmpkey);
1859 if (rv == -1)
1860 return NULL;
1861 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001864 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 return NULL;
1866 }
1867 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001868}
1869
1870PyDoc_STRVAR(remove_doc,
1871"Remove an element from a set; it must be a member.\n\
1872\n\
1873If the element is not a member, raise a KeyError.");
1874
1875static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001876set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001877{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001878 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 rv = set_discard_key(so, key);
1882 if (rv == -1) {
1883 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1884 return NULL;
1885 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001886 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 if (tmpkey == NULL)
1888 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001889 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001891 if (rv == -1)
1892 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 }
1894 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001895}
1896
1897PyDoc_STRVAR(discard_doc,
1898"Remove an element from a set if it is a member.\n\
1899\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001901
1902static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001903set_reduce(PySetObject *so)
1904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001906 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 keys = PySequence_List((PyObject *)so);
1909 if (keys == NULL)
1910 goto done;
1911 args = PyTuple_Pack(1, keys);
1912 if (args == NULL)
1913 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001914 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (dict == NULL) {
1916 PyErr_Clear();
1917 dict = Py_None;
1918 Py_INCREF(dict);
1919 }
1920 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001921done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 Py_XDECREF(args);
1923 Py_XDECREF(keys);
1924 Py_XDECREF(dict);
1925 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001926}
1927
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001928static PyObject *
1929set_sizeof(PySetObject *so)
1930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 res = sizeof(PySetObject);
1934 if (so->table != so->smalltable)
1935 res = res + (so->mask + 1) * sizeof(setentry);
1936 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001937}
1938
1939PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001940static int
1941set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 if (!PyAnySet_Check(self))
1946 return -1;
1947 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1948 return -1;
1949 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1950 return -1;
1951 set_clear_internal(self);
1952 self->hash = -1;
1953 if (iterable == NULL)
1954 return 0;
1955 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001956}
1957
Raymond Hettingera690a992003-11-16 16:17:49 +00001958static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 set_len, /* sq_length */
1960 0, /* sq_concat */
1961 0, /* sq_repeat */
1962 0, /* sq_item */
1963 0, /* sq_slice */
1964 0, /* sq_ass_item */
1965 0, /* sq_ass_slice */
1966 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001967};
1968
1969/* set object ********************************************************/
1970
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001971#ifdef Py_DEBUG
1972static PyObject *test_c_api(PySetObject *so);
1973
1974PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1975All is well if assertions don't fail.");
1976#endif
1977
Raymond Hettingera690a992003-11-16 16:17:49 +00001978static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 {"add", (PyCFunction)set_add, METH_O,
1980 add_doc},
1981 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1982 clear_doc},
1983 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1984 contains_doc},
1985 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1986 copy_doc},
1987 {"discard", (PyCFunction)set_discard, METH_O,
1988 discard_doc},
1989 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
1990 difference_doc},
1991 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
1992 difference_update_doc},
1993 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
1994 intersection_doc},
1995 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
1996 intersection_update_doc},
1997 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1998 isdisjoint_doc},
1999 {"issubset", (PyCFunction)set_issubset, METH_O,
2000 issubset_doc},
2001 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2002 issuperset_doc},
2003 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2004 pop_doc},
2005 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2006 reduce_doc},
2007 {"remove", (PyCFunction)set_remove, METH_O,
2008 remove_doc},
2009 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2010 sizeof_doc},
2011 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2012 symmetric_difference_doc},
2013 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2014 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002015#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2017 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002018#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 {"union", (PyCFunction)set_union, METH_VARARGS,
2020 union_doc},
2021 {"update", (PyCFunction)set_update, METH_VARARGS,
2022 update_doc},
2023 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002024};
2025
2026static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 0, /*nb_add*/
2028 (binaryfunc)set_sub, /*nb_subtract*/
2029 0, /*nb_multiply*/
2030 0, /*nb_remainder*/
2031 0, /*nb_divmod*/
2032 0, /*nb_power*/
2033 0, /*nb_negative*/
2034 0, /*nb_positive*/
2035 0, /*nb_absolute*/
2036 0, /*nb_bool*/
2037 0, /*nb_invert*/
2038 0, /*nb_lshift*/
2039 0, /*nb_rshift*/
2040 (binaryfunc)set_and, /*nb_and*/
2041 (binaryfunc)set_xor, /*nb_xor*/
2042 (binaryfunc)set_or, /*nb_or*/
2043 0, /*nb_int*/
2044 0, /*nb_reserved*/
2045 0, /*nb_float*/
2046 0, /*nb_inplace_add*/
2047 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2048 0, /*nb_inplace_multiply*/
2049 0, /*nb_inplace_remainder*/
2050 0, /*nb_inplace_power*/
2051 0, /*nb_inplace_lshift*/
2052 0, /*nb_inplace_rshift*/
2053 (binaryfunc)set_iand, /*nb_inplace_and*/
2054 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2055 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002056};
2057
2058PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002059"set() -> new empty set object\n\
2060set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002061\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002062Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002063
2064PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2066 "set", /* tp_name */
2067 sizeof(PySetObject), /* tp_basicsize */
2068 0, /* tp_itemsize */
2069 /* methods */
2070 (destructor)set_dealloc, /* tp_dealloc */
2071 0, /* tp_print */
2072 0, /* tp_getattr */
2073 0, /* tp_setattr */
2074 0, /* tp_reserved */
2075 (reprfunc)set_repr, /* tp_repr */
2076 &set_as_number, /* tp_as_number */
2077 &set_as_sequence, /* tp_as_sequence */
2078 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002079 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 0, /* tp_call */
2081 0, /* tp_str */
2082 PyObject_GenericGetAttr, /* tp_getattro */
2083 0, /* tp_setattro */
2084 0, /* tp_as_buffer */
2085 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2086 Py_TPFLAGS_BASETYPE, /* tp_flags */
2087 set_doc, /* tp_doc */
2088 (traverseproc)set_traverse, /* tp_traverse */
2089 (inquiry)set_clear_internal, /* tp_clear */
2090 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002091 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2092 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 0, /* tp_iternext */
2094 set_methods, /* tp_methods */
2095 0, /* tp_members */
2096 0, /* tp_getset */
2097 0, /* tp_base */
2098 0, /* tp_dict */
2099 0, /* tp_descr_get */
2100 0, /* tp_descr_set */
2101 0, /* tp_dictoffset */
2102 (initproc)set_init, /* tp_init */
2103 PyType_GenericAlloc, /* tp_alloc */
2104 set_new, /* tp_new */
2105 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002106};
2107
2108/* frozenset object ********************************************************/
2109
2110
2111static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2113 contains_doc},
2114 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2115 copy_doc},
2116 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2117 difference_doc},
2118 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2119 intersection_doc},
2120 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2121 isdisjoint_doc},
2122 {"issubset", (PyCFunction)set_issubset, METH_O,
2123 issubset_doc},
2124 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2125 issuperset_doc},
2126 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2127 reduce_doc},
2128 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2129 sizeof_doc},
2130 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2131 symmetric_difference_doc},
2132 {"union", (PyCFunction)set_union, METH_VARARGS,
2133 union_doc},
2134 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002135};
2136
2137static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 0, /*nb_add*/
2139 (binaryfunc)set_sub, /*nb_subtract*/
2140 0, /*nb_multiply*/
2141 0, /*nb_remainder*/
2142 0, /*nb_divmod*/
2143 0, /*nb_power*/
2144 0, /*nb_negative*/
2145 0, /*nb_positive*/
2146 0, /*nb_absolute*/
2147 0, /*nb_bool*/
2148 0, /*nb_invert*/
2149 0, /*nb_lshift*/
2150 0, /*nb_rshift*/
2151 (binaryfunc)set_and, /*nb_and*/
2152 (binaryfunc)set_xor, /*nb_xor*/
2153 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002154};
2155
2156PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002157"frozenset() -> empty frozenset object\n\
2158frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002159\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002160Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002161
2162PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2164 "frozenset", /* tp_name */
2165 sizeof(PySetObject), /* tp_basicsize */
2166 0, /* tp_itemsize */
2167 /* methods */
2168 (destructor)set_dealloc, /* tp_dealloc */
2169 0, /* tp_print */
2170 0, /* tp_getattr */
2171 0, /* tp_setattr */
2172 0, /* tp_reserved */
2173 (reprfunc)set_repr, /* tp_repr */
2174 &frozenset_as_number, /* tp_as_number */
2175 &set_as_sequence, /* tp_as_sequence */
2176 0, /* tp_as_mapping */
2177 frozenset_hash, /* tp_hash */
2178 0, /* tp_call */
2179 0, /* tp_str */
2180 PyObject_GenericGetAttr, /* tp_getattro */
2181 0, /* tp_setattro */
2182 0, /* tp_as_buffer */
2183 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2184 Py_TPFLAGS_BASETYPE, /* tp_flags */
2185 frozenset_doc, /* tp_doc */
2186 (traverseproc)set_traverse, /* tp_traverse */
2187 (inquiry)set_clear_internal, /* tp_clear */
2188 (richcmpfunc)set_richcompare, /* tp_richcompare */
2189 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2190 (getiterfunc)set_iter, /* tp_iter */
2191 0, /* tp_iternext */
2192 frozenset_methods, /* tp_methods */
2193 0, /* tp_members */
2194 0, /* tp_getset */
2195 0, /* tp_base */
2196 0, /* tp_dict */
2197 0, /* tp_descr_get */
2198 0, /* tp_descr_set */
2199 0, /* tp_dictoffset */
2200 0, /* tp_init */
2201 PyType_GenericAlloc, /* tp_alloc */
2202 frozenset_new, /* tp_new */
2203 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002204};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002205
2206
2207/***** C API functions *************************************************/
2208
2209PyObject *
2210PySet_New(PyObject *iterable)
2211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002213}
2214
2215PyObject *
2216PyFrozenSet_New(PyObject *iterable)
2217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002219}
2220
Neal Norwitz8c49c822006-03-04 18:41:19 +00002221Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002222PySet_Size(PyObject *anyset)
2223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 if (!PyAnySet_Check(anyset)) {
2225 PyErr_BadInternalCall();
2226 return -1;
2227 }
2228 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002229}
2230
2231int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002232PySet_Clear(PyObject *set)
2233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 if (!PySet_Check(set)) {
2235 PyErr_BadInternalCall();
2236 return -1;
2237 }
2238 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002239}
2240
2241int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002242PySet_Contains(PyObject *anyset, PyObject *key)
2243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 if (!PyAnySet_Check(anyset)) {
2245 PyErr_BadInternalCall();
2246 return -1;
2247 }
2248 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002249}
2250
2251int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002252PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 if (!PySet_Check(set)) {
2255 PyErr_BadInternalCall();
2256 return -1;
2257 }
2258 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002259}
2260
2261int
Christian Heimesfd66e512008-01-29 12:18:50 +00002262PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 if (!PySet_Check(anyset) &&
2265 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2266 PyErr_BadInternalCall();
2267 return -1;
2268 }
2269 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002270}
2271
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002272int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002273_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 if (!PyAnySet_Check(set)) {
2278 PyErr_BadInternalCall();
2279 return -1;
2280 }
2281 if (set_next((PySetObject *)set, pos, &entry) == 0)
2282 return 0;
2283 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002284 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002286}
2287
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002288PyObject *
2289PySet_Pop(PyObject *set)
2290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 if (!PySet_Check(set)) {
2292 PyErr_BadInternalCall();
2293 return NULL;
2294 }
2295 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002296}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002297
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002298int
2299_PySet_Update(PyObject *set, PyObject *iterable)
2300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (!PySet_Check(set)) {
2302 PyErr_BadInternalCall();
2303 return -1;
2304 }
2305 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002306}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002307
Raymond Hettingere259f132013-12-15 11:56:14 -08002308/* Exported for the gdb plugin's benefit. */
2309PyObject *_PySet_Dummy = dummy;
2310
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002311#ifdef Py_DEBUG
2312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002314 Returns True and original set is restored. */
2315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316#define assertRaises(call_return_value, exception) \
2317 do { \
2318 assert(call_return_value); \
2319 assert(PyErr_ExceptionMatches(exception)); \
2320 PyErr_Clear(); \
2321 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002322
2323static PyObject *
2324test_c_api(PySetObject *so)
2325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 Py_ssize_t count;
2327 char *s;
2328 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002329 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002331 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 /* Verify preconditions */
2335 assert(PyAnySet_Check(ob));
2336 assert(PyAnySet_CheckExact(ob));
2337 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 /* so.clear(); so |= set("abc"); */
2340 str = PyUnicode_FromString("abc");
2341 if (str == NULL)
2342 return NULL;
2343 set_clear_internal(so);
2344 if (set_update_internal(so, str) == -1) {
2345 Py_DECREF(str);
2346 return NULL;
2347 }
2348 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 /* Exercise type/size checks */
2351 assert(PySet_Size(ob) == 3);
2352 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 /* Raise TypeError for non-iterable constructor arguments */
2355 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2356 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 /* Raise TypeError for unhashable key */
2359 dup = PySet_New(ob);
2360 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2361 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2362 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* Exercise successful pop, contains, add, and discard */
2365 elem = PySet_Pop(ob);
2366 assert(PySet_Contains(ob, elem) == 0);
2367 assert(PySet_GET_SIZE(ob) == 2);
2368 assert(PySet_Add(ob, elem) == 0);
2369 assert(PySet_Contains(ob, elem) == 1);
2370 assert(PySet_GET_SIZE(ob) == 3);
2371 assert(PySet_Discard(ob, elem) == 1);
2372 assert(PySet_GET_SIZE(ob) == 2);
2373 assert(PySet_Discard(ob, elem) == 0);
2374 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 /* Exercise clear */
2377 dup2 = PySet_New(dup);
2378 assert(PySet_Clear(dup2) == 0);
2379 assert(PySet_Size(dup2) == 0);
2380 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 /* Raise SystemError on clear or update of frozen set */
2383 f = PyFrozenSet_New(dup);
2384 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2385 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2386 assert(PySet_Add(f, elem) == 0);
2387 Py_INCREF(f);
2388 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2389 Py_DECREF(f);
2390 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* Exercise direct iteration */
2393 i = 0, count = 0;
2394 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2395 s = _PyUnicode_AsString(x);
2396 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2397 count++;
2398 }
2399 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* Exercise updates */
2402 dup2 = PySet_New(NULL);
2403 assert(_PySet_Update(dup2, dup) == 0);
2404 assert(PySet_Size(dup2) == 3);
2405 assert(_PySet_Update(dup2, dup) == 0);
2406 assert(PySet_Size(dup2) == 3);
2407 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* Raise SystemError when self argument is not a set or frozenset. */
2410 t = PyTuple_New(0);
2411 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2412 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2413 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 /* Raise SystemError when self argument is not a set. */
2416 f = PyFrozenSet_New(dup);
2417 assert(PySet_Size(f) == 3);
2418 assert(PyFrozenSet_CheckExact(f));
2419 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2420 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2421 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 /* Raise KeyError when popping from an empty set */
2424 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2425 Py_DECREF(ob);
2426 assert(PySet_GET_SIZE(ob) == 0);
2427 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* Restore the set from the copy using the PyNumber API */
2430 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2431 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 /* Verify constructors accept NULL arguments */
2434 f = PySet_New(NULL);
2435 assert(f != NULL);
2436 assert(PySet_GET_SIZE(f) == 0);
2437 Py_DECREF(f);
2438 f = PyFrozenSet_New(NULL);
2439 assert(f != NULL);
2440 assert(PyFrozenSet_CheckExact(f));
2441 assert(PySet_GET_SIZE(f) == 0);
2442 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 Py_DECREF(elem);
2445 Py_DECREF(dup);
2446 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002447}
2448
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002449#undef assertRaises
2450
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002451#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002452
2453/***** Dummy Struct *************************************************/
2454
2455static PyObject *
2456dummy_repr(PyObject *op)
2457{
2458 return PyUnicode_FromString("<dummy key>");
2459}
2460
2461static void
2462dummy_dealloc(PyObject* ignore)
2463{
2464 Py_FatalError("deallocating <dummy key>");
2465}
2466
2467static PyTypeObject _PySetDummy_Type = {
2468 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2469 "<dummy key> type",
2470 0,
2471 0,
2472 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2473 0, /*tp_print*/
2474 0, /*tp_getattr*/
2475 0, /*tp_setattr*/
2476 0, /*tp_reserved*/
2477 dummy_repr, /*tp_repr*/
2478 0, /*tp_as_number*/
2479 0, /*tp_as_sequence*/
2480 0, /*tp_as_mapping*/
2481 0, /*tp_hash */
2482 0, /*tp_call */
2483 0, /*tp_str */
2484 0, /*tp_getattro */
2485 0, /*tp_setattro */
2486 0, /*tp_as_buffer */
2487 Py_TPFLAGS_DEFAULT, /*tp_flags */
2488};
2489
2490static PyObject _dummy_struct = {
2491 _PyObject_EXTRA_INIT
2492 2, &_PySetDummy_Type
2493};
2494