blob: 673490d6b8e85693aeb4d90b88d8bcf0bd1618ae [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 Hettingera35adf52013-09-02 03:23:21 -070059 size_t i = (size_t)hash; /* 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 Hettingera35adf52013-09-02 03:23:21 -070063 entry = &table[i & mask];
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;
87 }
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080088 if (entry->hash == -1 && freeslot == NULL) {
89 assert(entry->key == dummy);
Raymond Hettingerafe89092013-08-28 20:59:31 -070090 freeslot = entry;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080091 }
Raymond Hettingerafe89092013-08-28 20:59:31 -070092
Raymond Hettingerc70a2b72013-09-21 14:02:55 -070093 for (j = 1 ; j <= LINEAR_PROBES ; j++) {
94 entry = &table[(i + j) & mask];
Raymond Hettingera35adf52013-09-02 03:23:21 -070095 if (entry->key == NULL)
96 goto found_null;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080097 if (entry->hash == hash) {
Raymond Hettingera35adf52013-09-02 03:23:21 -070098 PyObject *startkey = entry->key;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -080099 assert(startkey != dummy);
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800100 if (startkey == key)
101 return entry;
Raymond Hettinger93035c42015-01-25 16:12:49 -0800102 if (PyUnicode_CheckExact(startkey)
103 && PyUnicode_CheckExact(key)
104 && unicode_eq(startkey, key))
105 return entry;
Raymond Hettingera35adf52013-09-02 03:23:21 -0700106 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;
115 }
Raymond Hettingera5ebbf62015-01-26 21:54:35 -0800116 if (entry->hash == -1 && freeslot == NULL) {
117 assert(entry->key == dummy);
Raymond Hettingera35adf52013-09-02 03:23:21 -0700118 freeslot = entry;
Raymond Hettingera5ebbf62015-01-26 21:54:35 -0800119 }
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 Hettingerc56e0e32013-09-02 16:32:27 -0700123 i = i * 5 + 1 + perturb;
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700124
Raymond Hettingera35adf52013-09-02 03:23:21 -0700125 entry = &table[i & mask];
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) {
156 for (j = 1; j <= LINEAR_PROBES; j++) {
157 entry = &table[i + j];
158 if (entry->key == NULL)
159 goto found_null;
160 }
161 } else {
162 for (j = 1; j <= LINEAR_PROBES; j++) {
163 entry = &table[(i + j) & mask];
164 if (entry->key == NULL)
165 goto found_null;
166 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700167 }
Raymond Hettinger3c0a4f52013-08-19 07:36:04 -0700168 perturb >>= PERTURB_SHIFT;
Raymond Hettinger3037e842015-01-26 21:33:48 -0800169 i = (i * 5 + 1 + perturb) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 }
Raymond Hettingera35adf52013-09-02 03:23:21 -0700171 found_null:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 entry->key = key;
173 entry->hash = hash;
Raymond Hettinger4ef05282013-09-21 15:39:49 -0700174 so->fill++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 so->used++;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176}
177
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700178/* ======== End logic for probing the hash table ========================== */
179/* ======================================================================== */
180
181
182/*
183Internal routine to insert a new key into the table.
184Used by the public insert routine.
185Eats a reference to key.
186*/
187static int
188set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
189{
190 setentry *entry;
191
Raymond Hettinger93035c42015-01-25 16:12:49 -0800192 entry = set_lookkey(so, key, hash);
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700193 if (entry == NULL)
194 return -1;
195 if (entry->key == NULL) {
196 /* UNUSED */
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700197 entry->key = key;
198 entry->hash = hash;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800199 so->fill++;
Raymond Hettinger04fd9dd2013-09-07 17:41:01 -0700200 so->used++;
201 } else if (entry->key == dummy) {
202 /* DUMMY */
203 entry->key = key;
204 entry->hash = hash;
205 so->used++;
206 } else {
207 /* ACTIVE */
208 Py_DECREF(key);
209 }
210 return 0;
211}
212
Thomas Wouters89f507f2006-12-13 04:49:30 +0000213/*
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000214Restructure the table by allocating a new table and reinserting all
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000215keys again. When entries have been deleted, the new table may
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000216actually be smaller than the old one.
217*/
218static int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000219set_table_resize(PySetObject *so, Py_ssize_t minused)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 Py_ssize_t newsize;
222 setentry *oldtable, *newtable, *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800223 Py_ssize_t oldfill = so->fill;
224 Py_ssize_t oldused = so->used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 int is_oldtable_malloced;
226 setentry small_copy[PySet_MINSIZE];
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 assert(minused >= 0);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 /* Find the smallest table size > minused. */
Raymond Hettinger426d9952014-05-18 21:40:20 +0100231 /* XXX speed-up with intrinsics */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 for (newsize = PySet_MINSIZE;
233 newsize <= minused && newsize > 0;
234 newsize <<= 1)
235 ;
236 if (newsize <= 0) {
237 PyErr_NoMemory();
238 return -1;
239 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 /* Get space for a new table. */
242 oldtable = so->table;
243 assert(oldtable != NULL);
244 is_oldtable_malloced = oldtable != so->smalltable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (newsize == PySet_MINSIZE) {
247 /* A large table is shrinking, or we can't get any smaller. */
248 newtable = so->smalltable;
249 if (newtable == oldtable) {
250 if (so->fill == so->used) {
251 /* No dummies, so no point doing anything. */
252 return 0;
253 }
254 /* We're not going to resize it, but rebuild the
255 table anyway to purge old dummy entries.
256 Subtle: This is *necessary* if fill==size,
257 as set_lookkey needs at least one virgin slot to
258 terminate failing searches. If fill < size, it's
259 merely desirable, as dummies slow searches. */
260 assert(so->fill > so->used);
261 memcpy(small_copy, oldtable, sizeof(small_copy));
262 oldtable = small_copy;
263 }
264 }
265 else {
266 newtable = PyMem_NEW(setentry, newsize);
267 if (newtable == NULL) {
268 PyErr_NoMemory();
269 return -1;
270 }
271 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 /* Make the set empty, using the new table. */
274 assert(newtable != oldtable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 memset(newtable, 0, sizeof(setentry) * newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 so->fill = 0;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800277 so->used = 0;
278 so->mask = newsize - 1;
279 so->table = newtable;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 /* Copy the data over; this is refcount-neutral for active entries;
282 dummy entries aren't copied over, of course */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800283 if (oldfill == oldused) {
284 for (entry = oldtable; oldused > 0; entry++) {
285 if (entry->key != NULL) {
286 oldused--;
287 set_insert_clean(so, entry->key, entry->hash);
288 }
289 }
290 } else {
291 for (entry = oldtable; oldused > 0; entry++) {
292 if (entry->key != NULL && entry->key != dummy) {
293 oldused--;
294 set_insert_clean(so, entry->key, entry->hash);
295 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 }
297 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (is_oldtable_malloced)
300 PyMem_DEL(oldtable);
301 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000302}
303
Raymond Hettingerc991db22005-08-11 07:58:45 +0000304/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
305
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000306static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200307set_add_entry(PySetObject *so, setentry *entry)
Raymond Hettingerc991db22005-08-11 07:58:45 +0000308{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200309 Py_ssize_t n_used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000310 PyObject *key = entry->key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100311 Py_hash_t hash = entry->hash;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 assert(so->fill <= so->mask); /* at least one empty slot */
314 n_used = so->used;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000315 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100316 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000317 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 return -1;
319 }
320 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
321 return 0;
322 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +0000323}
324
325static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200326set_add_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000327{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800328 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200329 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200332 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 hash = PyObject_Hash(key);
334 if (hash == -1)
335 return -1;
336 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800337 entry.key = key;
338 entry.hash = hash;
339 return set_add_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000340}
341
342#define DISCARD_NOTFOUND 0
343#define DISCARD_FOUND 1
344
345static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000346set_discard_entry(PySetObject *so, setentry *oldentry)
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800347{
348 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 PyObject *old_key;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000350
Raymond Hettinger93035c42015-01-25 16:12:49 -0800351 entry = set_lookkey(so, oldentry->key, oldentry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (entry == NULL)
353 return -1;
354 if (entry->key == NULL || entry->key == dummy)
355 return DISCARD_NOTFOUND;
356 old_key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800358 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 so->used--;
360 Py_DECREF(old_key);
361 return DISCARD_FOUND;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000362}
363
364static int
365set_discard_key(PySetObject *so, PyObject *key)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000366{
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800367 setentry entry;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200368 Py_hash_t hash;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 assert (PyAnySet_Check(so));
Christian Heimes0ded5b52007-12-10 15:50:56 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200373 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 hash = PyObject_Hash(key);
375 if (hash == -1)
376 return -1;
377 }
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800378 entry.key = key;
379 entry.hash = hash;
380 return set_discard_entry(so, &entry);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000381}
382
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700383static void
384set_empty_to_minsize(PySetObject *so)
385{
386 memset(so->smalltable, 0, sizeof(so->smalltable));
387 so->fill = 0;
388 so->used = 0;
389 so->mask = PySet_MINSIZE - 1;
390 so->table = so->smalltable;
391 so->hash = -1;
392}
393
Raymond Hettingerfe889f32005-08-06 05:43:39 +0000394static int
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000395set_clear_internal(PySetObject *so)
396{
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800397 setentry *entry;
398 setentry *table = so->table;
399 Py_ssize_t fill = so->fill;
400 Py_ssize_t used = so->used;
401 int table_is_malloced = table != so->smalltable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 setentry small_copy[PySet_MINSIZE];
Raymond Hettingera580c472005-08-05 17:19:54 +0000403
Raymond Hettinger583cd032013-09-07 22:06:35 -0700404 assert (PyAnySet_Check(so));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 assert(table != NULL);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* This is delicate. During the process of clearing the set,
408 * decrefs can cause the set to mutate. To avoid fatal confusion
409 * (voice of experience), we have to make the set empty before
410 * clearing the slots, and never refer to anything via so->ref while
411 * clearing.
412 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (table_is_malloced)
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700414 set_empty_to_minsize(so);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 else if (fill > 0) {
417 /* It's a small table with something that needs to be cleared.
418 * Afraid the only safe way is to copy the set entries into
419 * another small table first.
420 */
421 memcpy(small_copy, table, sizeof(small_copy));
422 table = small_copy;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700423 set_empty_to_minsize(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 }
425 /* else it's a small table that's already empty */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 /* Now we can finally clear things. If C had refcounts, we could
428 * assert that the refcount on table is 1 now, i.e. that this function
429 * has unique access to it, so decref side-effects can't alter it.
430 */
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800431 for (entry = table; used > 0; entry++) {
432 if (entry->key && entry->key != dummy) {
433 used--;
434 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (table_is_malloced)
439 PyMem_DEL(table);
440 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000441}
442
443/*
444 * Iterate over a set table. Use like so:
445 *
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000446 * Py_ssize_t pos;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000447 * setentry *entry;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000448 * pos = 0; # important! pos should not otherwise be changed by you
Raymond Hettingerc991db22005-08-11 07:58:45 +0000449 * while (set_next(yourset, &pos, &entry)) {
450 * Refer to borrowed reference in entry->key.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000451 * }
452 *
Raymond Hettingerc991db22005-08-11 07:58:45 +0000453 * CAUTION: In general, it isn't safe to use set_next in a loop that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 * mutates the table.
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000455 */
456static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 Py_ssize_t i;
460 Py_ssize_t mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200461 setentry *table;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 assert (PyAnySet_Check(so));
464 i = *pos_ptr;
465 assert(i >= 0);
466 table = so->table;
467 mask = so->mask;
468 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
469 i++;
470 *pos_ptr = i+1;
471 if (i > mask)
472 return 0;
473 assert(table[i].key != NULL);
474 *entry_ptr = &table[i];
475 return 1;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000476}
477
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000478static void
479set_dealloc(PySetObject *so)
480{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200481 setentry *entry;
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800482 Py_ssize_t used = so->used;
483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 PyObject_GC_UnTrack(so);
485 Py_TRASHCAN_SAFE_BEGIN(so)
486 if (so->weakreflist != NULL)
487 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000488
Raymond Hettinger08e3dc02014-12-26 20:14:00 -0800489 for (entry = so->table; used > 0; entry++) {
490 if (entry->key && entry->key != dummy) {
491 used--;
Raymond Hettingerfcf3b502013-08-22 08:20:31 -0700492 Py_DECREF(entry->key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
494 }
495 if (so->table != so->smalltable)
496 PyMem_DEL(so->table);
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700497 Py_TYPE(so)->tp_free(so);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 Py_TRASHCAN_SAFE_END(so)
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000499}
500
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000501static PyObject *
502set_repr(PySetObject *so)
503{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200504 PyObject *result=NULL, *keys, *listrepr, *tmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 int status = Py_ReprEnter((PyObject*)so);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (status != 0) {
508 if (status < 0)
509 return NULL;
510 return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
511 }
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 /* shortcut for the empty set */
514 if (!so->used) {
515 Py_ReprLeave((PyObject*)so);
516 return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
517 }
Georg Brandlc4996ba2006-08-28 19:37:11 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 keys = PySequence_List((PyObject *)so);
520 if (keys == NULL)
521 goto done;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000522
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200523 /* repr(keys)[1:-1] */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 listrepr = PyObject_Repr(keys);
525 Py_DECREF(keys);
526 if (listrepr == NULL)
527 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200528 tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 Py_DECREF(listrepr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200530 if (tmp == NULL)
531 goto done;
532 listrepr = tmp;
Victor Stinnera1a807b2011-05-26 14:24:30 +0200533
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200534 if (Py_TYPE(so) != &PySet_Type)
535 result = PyUnicode_FromFormat("%s({%U})",
536 Py_TYPE(so)->tp_name,
537 listrepr);
538 else
539 result = PyUnicode_FromFormat("{%U}", listrepr);
540 Py_DECREF(listrepr);
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000541done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 Py_ReprLeave((PyObject*)so);
543 return result;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000544}
545
Martin v. Löwis18e16552006-02-15 17:27:45 +0000546static Py_ssize_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000547set_len(PyObject *so)
548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 return ((PySetObject *)so)->used;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000550}
551
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000552static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000553set_merge(PySetObject *so, PyObject *otherset)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 PySetObject *other;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000556 PyObject *key;
Éric Araujobbcfc1f2011-03-23 03:43:22 +0100557 Py_hash_t hash;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200558 Py_ssize_t i;
559 setentry *entry;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 assert (PyAnySet_Check(so));
562 assert (PyAnySet_Check(otherset));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 other = (PySetObject*)otherset;
565 if (other == so || other->used == 0)
566 /* a.update(a) or a.update({}); nothing to do */
567 return 0;
568 /* Do one big resize at the start, rather than
569 * incrementally resizing as we insert new keys. Expect
570 * that there will be no (or few) overlapping keys.
571 */
572 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
573 if (set_table_resize(so, (so->used + other->used)*2) != 0)
574 return -1;
575 }
576 for (i = 0; i <= other->mask; i++) {
577 entry = &other->table[i];
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000578 key = entry->key;
Éric Araujo48049912011-03-23 02:08:07 +0100579 hash = entry->hash;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000580 if (key != NULL &&
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -0500581 key != dummy) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000582 Py_INCREF(key);
Éric Araujo48049912011-03-23 02:08:07 +0100583 if (set_insert_key(so, key, hash) == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +0000584 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 return -1;
586 }
587 }
588 }
589 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000590}
591
592static int
Raymond Hettingerc991db22005-08-11 07:58:45 +0000593set_contains_entry(PySetObject *so, setentry *entry)
594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyObject *key;
596 setentry *lu_entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000597
Raymond Hettinger93035c42015-01-25 16:12:49 -0800598 lu_entry = set_lookkey(so, entry->key, entry->hash);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 if (lu_entry == NULL)
600 return -1;
601 key = lu_entry->key;
602 return key != NULL && key != dummy;
Raymond Hettingerc991db22005-08-11 07:58:45 +0000603}
604
Raymond Hettinger8edf27c2014-12-26 23:08:58 -0800605static int
606set_contains_key(PySetObject *so, PyObject *key)
607{
608 setentry entry;
609 Py_hash_t hash;
610
611 if (!PyUnicode_CheckExact(key) ||
612 (hash = ((PyASCIIObject *) key)->hash) == -1) {
613 hash = PyObject_Hash(key);
614 if (hash == -1)
615 return -1;
616 }
617 entry.key = key;
618 entry.hash = hash;
619 return set_contains_entry(so, &entry);
620}
621
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000622static PyObject *
623set_pop(PySetObject *so)
624{
Raymond Hettinger9cd6a782015-01-18 16:06:18 -0800625 /* Make sure the search finger is in bounds */
626 Py_ssize_t i = so->finger & so->mask;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200627 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 PyObject *key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 assert (PyAnySet_Check(so));
631 if (so->used == 0) {
632 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
633 return NULL;
634 }
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000635
Raymond Hettinger1202a472015-01-18 13:12:42 -0800636 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
637 i++;
Raymond Hettingered741d42015-01-18 21:25:15 -0800638 if (i > so->mask)
639 i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 }
641 key = entry->key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 entry->key = dummy;
Raymond Hettingerb335dfe2015-01-25 16:38:52 -0800643 entry->hash = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 so->used--;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800645 so->finger = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 return key;
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000647}
648
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000649PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
650Raises KeyError if the set is empty.");
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000651
652static int
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000653set_traverse(PySetObject *so, visitproc visit, void *arg)
Raymond Hettingerce8185e2005-08-13 09:28:48 +0000654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 Py_ssize_t pos = 0;
656 setentry *entry;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 while (set_next(so, &pos, &entry))
659 Py_VISIT(entry->key);
660 return 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000661}
662
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000663static Py_hash_t
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000664frozenset_hash(PyObject *self)
665{
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800666 /* Most of the constants in this hash algorithm are randomly choosen
667 large primes with "interesting bit patterns" and that passed
668 tests for good collision statistics on a variety of problematic
669 datasets such as:
670
671 ps = []
672 for r in range(21):
673 ps += itertools.combinations(range(20), r)
674 num_distinct_hashes = len({hash(frozenset(s)) for s in ps})
675
676 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 PySetObject *so = (PySetObject *)self;
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800678 Py_uhash_t h, hash = 1927868237UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 setentry *entry;
680 Py_ssize_t pos = 0;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (so->hash != -1)
683 return so->hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000684
Mark Dickinson57e683e2011-09-24 18:18:40 +0100685 hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 while (set_next(so, &pos, &entry)) {
687 /* Work to increase the bit dispersion for closely spaced hash
688 values. The is important because some use cases have many
689 combinations of a small number of elements with nearby
690 hashes so that many distinct combinations collapse to only
691 a handful of distinct hash values. */
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000692 h = entry->hash;
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800693 hash ^= ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 }
Raymond Hettinger74fc8c42014-01-05 12:00:31 -0800695 /* Make the final result spread-out in a different pattern
Eric V. Smith6ba56652014-01-14 08:15:03 -0500696 than the algorithm for tuples or other python objects. */
Gregory P. Smithc2176e42012-12-10 18:32:53 -0800697 hash = hash * 69069U + 907133923UL;
Victor Stinner12174a52014-08-15 23:17:38 +0200698 if (hash == (Py_uhash_t)-1)
Gregory P. Smith27cbcd62012-12-10 18:15:46 -0800699 hash = 590923713UL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 so->hash = hash;
701 return hash;
Raymond Hettingerf408ddf2005-08-17 00:27:42 +0000702}
703
Raymond Hettingera9d99362005-08-05 00:01:15 +0000704/***** Set iterator type ***********************************************/
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000705
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000706typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyObject_HEAD
708 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
709 Py_ssize_t si_used;
710 Py_ssize_t si_pos;
711 Py_ssize_t len;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000712} setiterobject;
713
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000714static void
715setiter_dealloc(setiterobject *si)
716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 Py_XDECREF(si->si_set);
718 PyObject_GC_Del(si);
Antoine Pitrou7ddda782009-01-01 15:35:33 +0000719}
720
721static int
722setiter_traverse(setiterobject *si, visitproc visit, void *arg)
723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 Py_VISIT(si->si_set);
725 return 0;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000726}
727
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000728static PyObject *
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000729setiter_len(setiterobject *si)
730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 Py_ssize_t len = 0;
732 if (si->si_set != NULL && si->si_used == si->si_set->used)
733 len = si->len;
Antoine Pitrou671b4d92010-08-17 17:55:07 +0000734 return PyLong_FromSsize_t(len);
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000735}
736
Armin Rigof5b3e362006-02-11 21:32:43 +0000737PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000738
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000739static PyObject *setiter_iternext(setiterobject *si);
740
741static PyObject *
742setiter_reduce(setiterobject *si)
743{
744 PyObject *list;
745 setiterobject tmp;
746
747 list = PyList_New(0);
748 if (!list)
749 return NULL;
750
Ezio Melotti0e1af282012-09-28 16:43:40 +0300751 /* copy the iterator state */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000752 tmp = *si;
753 Py_XINCREF(tmp.si_set);
Ezio Melotti0e1af282012-09-28 16:43:40 +0300754
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000755 /* iterate the temporary into a list */
756 for(;;) {
757 PyObject *element = setiter_iternext(&tmp);
758 if (element) {
759 if (PyList_Append(list, element)) {
760 Py_DECREF(element);
761 Py_DECREF(list);
762 Py_XDECREF(tmp.si_set);
763 return NULL;
764 }
765 Py_DECREF(element);
766 } else
767 break;
768 }
769 Py_XDECREF(tmp.si_set);
770 /* check for error */
771 if (tmp.si_set != NULL) {
772 /* we have an error */
773 Py_DECREF(list);
774 return NULL;
775 }
Antoine Pitroua7013882012-04-05 00:04:20 +0200776 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000777}
778
779PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
780
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000781static PyMethodDef setiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000783 {"__reduce__", (PyCFunction)setiter_reduce, METH_NOARGS, reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 {NULL, NULL} /* sentinel */
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000785};
786
Raymond Hettinger06d8cf82005-07-31 15:36:06 +0000787static PyObject *setiter_iternext(setiterobject *si)
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200790 Py_ssize_t i, mask;
791 setentry *entry;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PySetObject *so = si->si_set;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (so == NULL)
795 return NULL;
796 assert (PyAnySet_Check(so));
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (si->si_used != so->used) {
799 PyErr_SetString(PyExc_RuntimeError,
800 "Set changed size during iteration");
801 si->si_used = -1; /* Make this state sticky */
802 return NULL;
803 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 i = si->si_pos;
806 assert(i>=0);
807 entry = so->table;
808 mask = so->mask;
809 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
810 i++;
811 si->si_pos = i+1;
812 if (i > mask)
813 goto fail;
814 si->len--;
815 key = entry[i].key;
816 Py_INCREF(key);
817 return key;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000818
819fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 Py_DECREF(so);
821 si->si_set = NULL;
822 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000823}
824
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000825PyTypeObject PySetIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 PyVarObject_HEAD_INIT(&PyType_Type, 0)
827 "set_iterator", /* tp_name */
828 sizeof(setiterobject), /* tp_basicsize */
829 0, /* tp_itemsize */
830 /* methods */
831 (destructor)setiter_dealloc, /* tp_dealloc */
832 0, /* tp_print */
833 0, /* tp_getattr */
834 0, /* tp_setattr */
835 0, /* tp_reserved */
836 0, /* tp_repr */
837 0, /* tp_as_number */
838 0, /* tp_as_sequence */
839 0, /* tp_as_mapping */
840 0, /* tp_hash */
841 0, /* tp_call */
842 0, /* tp_str */
843 PyObject_GenericGetAttr, /* tp_getattro */
844 0, /* tp_setattro */
845 0, /* tp_as_buffer */
846 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
847 0, /* tp_doc */
848 (traverseproc)setiter_traverse, /* tp_traverse */
849 0, /* tp_clear */
850 0, /* tp_richcompare */
851 0, /* tp_weaklistoffset */
852 PyObject_SelfIter, /* tp_iter */
853 (iternextfunc)setiter_iternext, /* tp_iternext */
854 setiter_methods, /* tp_methods */
855 0,
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000856};
857
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858static PyObject *
859set_iter(PySetObject *so)
860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
862 if (si == NULL)
863 return NULL;
864 Py_INCREF(so);
865 si->si_set = so;
866 si->si_used = so->used;
867 si->si_pos = 0;
868 si->len = so->used;
869 _PyObject_GC_TRACK(si);
870 return (PyObject *)si;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000871}
872
Raymond Hettingerd7946662005-08-01 21:39:29 +0000873static int
Raymond Hettingerd7946662005-08-01 21:39:29 +0000874set_update_internal(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +0000875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 PyObject *key, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (PyAnySet_Check(other))
879 return set_merge(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (PyDict_CheckExact(other)) {
882 PyObject *value;
883 Py_ssize_t pos = 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000884 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 Py_ssize_t dictsize = PyDict_Size(other);
Thomas Wouterscf297e42007-02-23 15:07:44 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 /* Do one big resize at the start, rather than
888 * incrementally resizing as we insert new keys. Expect
889 * that there will be no (or few) overlapping keys.
890 */
891 if (dictsize == -1)
892 return -1;
893 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
894 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
895 return -1;
896 }
897 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
898 setentry an_entry;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 an_entry.hash = hash;
901 an_entry.key = key;
902 if (set_add_entry(so, &an_entry) == -1)
903 return -1;
904 }
905 return 0;
906 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 it = PyObject_GetIter(other);
909 if (it == NULL)
910 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 while ((key = PyIter_Next(it)) != NULL) {
913 if (set_add_key(so, key) == -1) {
914 Py_DECREF(it);
915 Py_DECREF(key);
916 return -1;
917 }
918 Py_DECREF(key);
919 }
920 Py_DECREF(it);
921 if (PyErr_Occurred())
922 return -1;
923 return 0;
Raymond Hettingerd7946662005-08-01 21:39:29 +0000924}
925
926static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000927set_update(PySetObject *so, PyObject *args)
Raymond Hettingerd7946662005-08-01 21:39:29 +0000928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
932 PyObject *other = PyTuple_GET_ITEM(args, i);
933 if (set_update_internal(so, other) == -1)
934 return NULL;
935 }
936 Py_RETURN_NONE;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000937}
938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939PyDoc_STRVAR(update_doc,
Georg Brandlc28e1fa2008-06-10 19:20:26 +0000940"Update a set with the union of itself and others.");
Raymond Hettingera38123e2003-11-24 22:18:49 +0000941
Raymond Hettinger426d9952014-05-18 21:40:20 +0100942/* XXX Todo:
943 If aligned memory allocations become available, make the
944 set object 64 byte aligned so that most of the fields
945 can be retrieved or updated in a single cache line.
946*/
947
Raymond Hettingera38123e2003-11-24 22:18:49 +0000948static PyObject *
949make_new_set(PyTypeObject *type, PyObject *iterable)
950{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200951 PySetObject *so = NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 /* create PySetObject structure */
Raymond Hettinger8f8839e2013-09-07 20:26:50 -0700954 so = (PySetObject *)type->tp_alloc(type, 0);
955 if (so == NULL)
956 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +0000957
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700958 so->fill = 0;
959 so->used = 0;
960 so->mask = PySet_MINSIZE - 1;
961 so->table = so->smalltable;
Raymond Hettinger4ea90802013-09-07 21:01:29 -0700962 so->hash = -1;
Raymond Hettinger1202a472015-01-18 13:12:42 -0800963 so->finger = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 if (iterable != NULL) {
967 if (set_update_internal(so, iterable) == -1) {
968 Py_DECREF(so);
969 return NULL;
970 }
971 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +0000974}
975
Raymond Hettinger7d99f092008-11-16 11:44:54 +0000976static PyObject *
977make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
980 if (PyType_IsSubtype(type, &PySet_Type))
981 type = &PySet_Type;
982 else
983 type = &PyFrozenSet_Type;
984 }
985 return make_new_set(type, iterable);
Raymond Hettinger7d99f092008-11-16 11:44:54 +0000986}
987
Raymond Hettingerd7946662005-08-01 21:39:29 +0000988/* The empty frozenset is a singleton */
989static PyObject *emptyfrozenset = NULL;
990
Raymond Hettingera690a992003-11-16 16:17:49 +0000991static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000992frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +0000993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 PyObject *iterable = NULL, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
997 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1000 return NULL;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (type != &PyFrozenSet_Type)
1003 return make_new_set(type, iterable);
Raymond Hettingerd7946662005-08-01 21:39:29 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 if (iterable != NULL) {
1006 /* frozenset(f) is idempotent */
1007 if (PyFrozenSet_CheckExact(iterable)) {
1008 Py_INCREF(iterable);
1009 return iterable;
1010 }
1011 result = make_new_set(type, iterable);
1012 if (result == NULL || PySet_GET_SIZE(result))
1013 return result;
1014 Py_DECREF(result);
1015 }
1016 /* The empty frozenset is a singleton */
1017 if (emptyfrozenset == NULL)
1018 emptyfrozenset = make_new_set(type, NULL);
1019 Py_XINCREF(emptyfrozenset);
1020 return emptyfrozenset;
Raymond Hettingerd7946662005-08-01 21:39:29 +00001021}
1022
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001023int
1024PySet_ClearFreeList(void)
Raymond Hettingerd7946662005-08-01 21:39:29 +00001025{
Raymond Hettinger8f8839e2013-09-07 20:26:50 -07001026 return 0;
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001027}
1028
1029void
1030PySet_Fini(void)
1031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 Py_CLEAR(emptyfrozenset);
Raymond Hettingera690a992003-11-16 16:17:49 +00001033}
1034
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001035static PyObject *
1036set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1039 return NULL;
1040
1041 return make_new_set(type, NULL);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001042}
1043
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001044/* set_swap_bodies() switches the contents of any two sets by moving their
1045 internal data pointers and, if needed, copying the internal smalltables.
1046 Semantically equivalent to:
1047
1048 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1049
1050 The function always succeeds and it leaves both objects in a stable state.
Raymond Hettinger4d45c102015-01-25 16:27:40 -08001051 Useful for operations that update in-place (by allowing an intermediate
1052 result to be swapped into one of the original inputs).
Raymond Hettinger934d63e2005-07-31 01:33:10 +00001053*/
1054
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001055static void
1056set_swap_bodies(PySetObject *a, PySetObject *b)
Raymond Hettingera690a992003-11-16 16:17:49 +00001057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 Py_ssize_t t;
1059 setentry *u;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 setentry tab[PySet_MINSIZE];
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001061 Py_hash_t h;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 t = a->fill; a->fill = b->fill; b->fill = t;
1064 t = a->used; a->used = b->used; b->used = t;
1065 t = a->mask; a->mask = b->mask; b->mask = t;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 u = a->table;
1068 if (a->table == a->smalltable)
1069 u = b->smalltable;
1070 a->table = b->table;
1071 if (b->table == b->smalltable)
1072 a->table = a->smalltable;
1073 b->table = u;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (a->table == a->smalltable || b->table == b->smalltable) {
1076 memcpy(tab, a->smalltable, sizeof(tab));
1077 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1078 memcpy(b->smalltable, tab, sizeof(tab));
1079 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1082 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1083 h = a->hash; a->hash = b->hash; b->hash = h;
1084 } else {
1085 a->hash = -1;
1086 b->hash = -1;
1087 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001088}
1089
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001090static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001091set_copy(PySetObject *so)
1092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001094}
1095
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001096static PyObject *
1097frozenset_copy(PySetObject *so)
1098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (PyFrozenSet_CheckExact(so)) {
1100 Py_INCREF(so);
1101 return (PyObject *)so;
1102 }
1103 return set_copy(so);
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001104}
1105
Raymond Hettingera690a992003-11-16 16:17:49 +00001106PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1107
1108static PyObject *
Raymond Hettingerc991db22005-08-11 07:58:45 +00001109set_clear(PySetObject *so)
1110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 set_clear_internal(so);
1112 Py_RETURN_NONE;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001113}
1114
1115PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1116
1117static PyObject *
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001118set_union(PySetObject *so, PyObject *args)
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 PySetObject *result;
1121 PyObject *other;
1122 Py_ssize_t i;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 result = (PySetObject *)set_copy(so);
1125 if (result == NULL)
1126 return NULL;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1129 other = PyTuple_GET_ITEM(args, i);
1130 if ((PyObject *)so == other)
1131 continue;
1132 if (set_update_internal(result, other) == -1) {
1133 Py_DECREF(result);
1134 return NULL;
1135 }
1136 }
1137 return (PyObject *)result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001138}
1139
1140PyDoc_STRVAR(union_doc,
1141 "Return the union of sets as a new set.\n\
1142\n\
1143(i.e. all elements that are in either set.)");
1144
1145static PyObject *
1146set_or(PySetObject *so, PyObject *other)
1147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 PySetObject *result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001149
Brian Curtindfc80e32011-08-10 20:28:54 -05001150 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1151 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 result = (PySetObject *)set_copy(so);
1154 if (result == NULL)
1155 return NULL;
1156 if ((PyObject *)so == other)
1157 return (PyObject *)result;
1158 if (set_update_internal(result, other) == -1) {
1159 Py_DECREF(result);
1160 return NULL;
1161 }
1162 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001163}
1164
Raymond Hettingera690a992003-11-16 16:17:49 +00001165static PyObject *
1166set_ior(PySetObject *so, PyObject *other)
1167{
Brian Curtindfc80e32011-08-10 20:28:54 -05001168 if (!PyAnySet_Check(other))
1169 Py_RETURN_NOTIMPLEMENTED;
1170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (set_update_internal(so, other) == -1)
1172 return NULL;
1173 Py_INCREF(so);
1174 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001175}
1176
1177static PyObject *
1178set_intersection(PySetObject *so, PyObject *other)
1179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 PySetObject *result;
1181 PyObject *key, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 if ((PyObject *)so == other)
1184 return set_copy(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1187 if (result == NULL)
1188 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 if (PyAnySet_Check(other)) {
1191 Py_ssize_t pos = 0;
1192 setentry *entry;
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1195 tmp = (PyObject *)so;
1196 so = (PySetObject *)other;
1197 other = tmp;
1198 }
Raymond Hettingerb02c35e2005-08-12 20:48:39 +00001199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 while (set_next((PySetObject *)other, &pos, &entry)) {
1201 int rv = set_contains_entry(so, entry);
1202 if (rv == -1) {
1203 Py_DECREF(result);
1204 return NULL;
1205 }
1206 if (rv) {
1207 if (set_add_entry(result, entry) == -1) {
1208 Py_DECREF(result);
1209 return NULL;
1210 }
1211 }
1212 }
1213 return (PyObject *)result;
1214 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 it = PyObject_GetIter(other);
1217 if (it == NULL) {
1218 Py_DECREF(result);
1219 return NULL;
1220 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 while ((key = PyIter_Next(it)) != NULL) {
1223 int rv;
1224 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001225 Py_hash_t hash = PyObject_Hash(key);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 if (hash == -1) {
1228 Py_DECREF(it);
1229 Py_DECREF(result);
1230 Py_DECREF(key);
1231 return NULL;
1232 }
1233 entry.hash = hash;
1234 entry.key = key;
1235 rv = set_contains_entry(so, &entry);
1236 if (rv == -1) {
1237 Py_DECREF(it);
1238 Py_DECREF(result);
1239 Py_DECREF(key);
1240 return NULL;
1241 }
1242 if (rv) {
1243 if (set_add_entry(result, &entry) == -1) {
1244 Py_DECREF(it);
1245 Py_DECREF(result);
1246 Py_DECREF(key);
1247 return NULL;
1248 }
1249 }
1250 Py_DECREF(key);
1251 }
1252 Py_DECREF(it);
1253 if (PyErr_Occurred()) {
1254 Py_DECREF(result);
1255 return NULL;
1256 }
1257 return (PyObject *)result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001258}
1259
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001260static PyObject *
1261set_intersection_multi(PySetObject *so, PyObject *args)
1262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 Py_ssize_t i;
1264 PyObject *result = (PyObject *)so;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 if (PyTuple_GET_SIZE(args) == 0)
1267 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 Py_INCREF(so);
1270 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1271 PyObject *other = PyTuple_GET_ITEM(args, i);
1272 PyObject *newresult = set_intersection((PySetObject *)result, other);
1273 if (newresult == NULL) {
1274 Py_DECREF(result);
1275 return NULL;
1276 }
1277 Py_DECREF(result);
1278 result = newresult;
1279 }
1280 return result;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001281}
1282
Raymond Hettingera690a992003-11-16 16:17:49 +00001283PyDoc_STRVAR(intersection_doc,
1284"Return the intersection of two sets as a new set.\n\
1285\n\
1286(i.e. all elements that are in both sets.)");
1287
1288static PyObject *
1289set_intersection_update(PySetObject *so, PyObject *other)
1290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 PyObject *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 tmp = set_intersection(so, other);
1294 if (tmp == NULL)
1295 return NULL;
1296 set_swap_bodies(so, (PySetObject *)tmp);
1297 Py_DECREF(tmp);
1298 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001299}
1300
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001301static PyObject *
1302set_intersection_update_multi(PySetObject *so, PyObject *args)
1303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 PyObject *tmp;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 tmp = set_intersection_multi(so, args);
1307 if (tmp == NULL)
1308 return NULL;
1309 set_swap_bodies(so, (PySetObject *)tmp);
1310 Py_DECREF(tmp);
1311 Py_RETURN_NONE;
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001312}
1313
Raymond Hettingera690a992003-11-16 16:17:49 +00001314PyDoc_STRVAR(intersection_update_doc,
1315"Update a set with the intersection of itself and another.");
1316
1317static PyObject *
1318set_and(PySetObject *so, PyObject *other)
1319{
Brian Curtindfc80e32011-08-10 20:28:54 -05001320 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1321 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 return set_intersection(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001323}
1324
1325static PyObject *
1326set_iand(PySetObject *so, PyObject *other)
1327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001329
Brian Curtindfc80e32011-08-10 20:28:54 -05001330 if (!PyAnySet_Check(other))
1331 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 result = set_intersection_update(so, other);
1333 if (result == NULL)
1334 return NULL;
1335 Py_DECREF(result);
1336 Py_INCREF(so);
1337 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001338}
1339
Guido van Rossum58da9312007-11-10 23:39:45 +00001340static PyObject *
1341set_isdisjoint(PySetObject *so, PyObject *other)
1342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 PyObject *key, *it, *tmp;
Guido van Rossum58da9312007-11-10 23:39:45 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 if ((PyObject *)so == other) {
1346 if (PySet_GET_SIZE(so) == 0)
1347 Py_RETURN_TRUE;
1348 else
1349 Py_RETURN_FALSE;
1350 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if (PyAnySet_CheckExact(other)) {
1353 Py_ssize_t pos = 0;
1354 setentry *entry;
Guido van Rossum58da9312007-11-10 23:39:45 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1357 tmp = (PyObject *)so;
1358 so = (PySetObject *)other;
1359 other = tmp;
1360 }
1361 while (set_next((PySetObject *)other, &pos, &entry)) {
1362 int rv = set_contains_entry(so, entry);
1363 if (rv == -1)
1364 return NULL;
1365 if (rv)
1366 Py_RETURN_FALSE;
1367 }
1368 Py_RETURN_TRUE;
1369 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 it = PyObject_GetIter(other);
1372 if (it == NULL)
1373 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 while ((key = PyIter_Next(it)) != NULL) {
1376 int rv;
1377 setentry entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001378 Py_hash_t hash = PyObject_Hash(key);
Guido van Rossum58da9312007-11-10 23:39:45 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 if (hash == -1) {
1381 Py_DECREF(key);
1382 Py_DECREF(it);
1383 return NULL;
1384 }
1385 entry.hash = hash;
1386 entry.key = key;
1387 rv = set_contains_entry(so, &entry);
1388 Py_DECREF(key);
1389 if (rv == -1) {
1390 Py_DECREF(it);
1391 return NULL;
1392 }
1393 if (rv) {
1394 Py_DECREF(it);
1395 Py_RETURN_FALSE;
1396 }
1397 }
1398 Py_DECREF(it);
1399 if (PyErr_Occurred())
1400 return NULL;
1401 Py_RETURN_TRUE;
Guido van Rossum58da9312007-11-10 23:39:45 +00001402}
1403
1404PyDoc_STRVAR(isdisjoint_doc,
1405"Return True if two sets have a null intersection.");
1406
Neal Norwitz6576bd82005-11-13 18:41:28 +00001407static int
Raymond Hettingerc991db22005-08-11 07:58:45 +00001408set_difference_update_internal(PySetObject *so, PyObject *other)
1409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if ((PyObject *)so == other)
1411 return set_clear_internal(so);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 if (PyAnySet_Check(other)) {
1414 setentry *entry;
1415 Py_ssize_t pos = 0;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 while (set_next((PySetObject *)other, &pos, &entry))
1418 if (set_discard_entry(so, entry) == -1)
1419 return -1;
1420 } else {
1421 PyObject *key, *it;
1422 it = PyObject_GetIter(other);
1423 if (it == NULL)
1424 return -1;
1425
1426 while ((key = PyIter_Next(it)) != NULL) {
1427 if (set_discard_key(so, key) == -1) {
1428 Py_DECREF(it);
1429 Py_DECREF(key);
1430 return -1;
1431 }
1432 Py_DECREF(key);
1433 }
1434 Py_DECREF(it);
1435 if (PyErr_Occurred())
1436 return -1;
1437 }
1438 /* If more than 1/5 are dummies, then resize them away. */
1439 if ((so->fill - so->used) * 5 < so->mask)
1440 return 0;
1441 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
Raymond Hettingerc991db22005-08-11 07:58:45 +00001442}
1443
Raymond Hettingera690a992003-11-16 16:17:49 +00001444static PyObject *
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001445set_difference_update(PySetObject *so, PyObject *args)
Raymond Hettingera690a992003-11-16 16:17:49 +00001446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 Py_ssize_t i;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1450 PyObject *other = PyTuple_GET_ITEM(args, i);
1451 if (set_difference_update_internal(so, other) == -1)
1452 return NULL;
1453 }
1454 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001455}
1456
1457PyDoc_STRVAR(difference_update_doc,
1458"Remove all elements of another set from this set.");
1459
1460static PyObject *
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001461set_copy_and_difference(PySetObject *so, PyObject *other)
1462{
1463 PyObject *result;
1464
1465 result = set_copy(so);
1466 if (result == NULL)
1467 return NULL;
1468 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1469 return result;
1470 Py_DECREF(result);
1471 return NULL;
1472}
1473
1474static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001475set_difference(PySetObject *so, PyObject *other)
1476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 PyObject *result;
1478 setentry *entry;
1479 Py_ssize_t pos = 0;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001482 return set_copy_and_difference(so, other);
1483 }
1484
1485 /* If len(so) much more than len(other), it's more efficient to simply copy
1486 * so and then iterate other looking for common elements. */
1487 if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) {
1488 return set_copy_and_difference(so, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 }
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 result = make_new_set_basetype(Py_TYPE(so), NULL);
1492 if (result == NULL)
1493 return NULL;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 if (PyDict_CheckExact(other)) {
1496 while (set_next(so, &pos, &entry)) {
1497 setentry entrycopy;
1498 entrycopy.hash = entry->hash;
1499 entrycopy.key = entry->key;
Antoine Pitroufbb1c612010-10-23 17:37:54 +00001500 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1502 Py_DECREF(result);
1503 return NULL;
1504 }
1505 }
1506 }
1507 return result;
1508 }
1509
Antoine Pitrou715f3cd2010-11-30 22:23:20 +00001510 /* Iterate over so, checking for common elements in other. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 while (set_next(so, &pos, &entry)) {
1512 int rv = set_contains_entry((PySetObject *)other, entry);
1513 if (rv == -1) {
1514 Py_DECREF(result);
1515 return NULL;
1516 }
1517 if (!rv) {
1518 if (set_add_entry((PySetObject *)result, entry) == -1) {
1519 Py_DECREF(result);
1520 return NULL;
1521 }
1522 }
1523 }
1524 return result;
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001525}
1526
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001527static PyObject *
1528set_difference_multi(PySetObject *so, PyObject *args)
1529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 Py_ssize_t i;
1531 PyObject *result, *other;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 if (PyTuple_GET_SIZE(args) == 0)
1534 return set_copy(so);
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 other = PyTuple_GET_ITEM(args, 0);
1537 result = set_difference(so, other);
1538 if (result == NULL)
1539 return NULL;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1542 other = PyTuple_GET_ITEM(args, i);
1543 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1544 Py_DECREF(result);
1545 return NULL;
1546 }
1547 }
1548 return result;
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001549}
1550
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001551PyDoc_STRVAR(difference_doc,
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001552"Return the difference of two or more sets as a new set.\n\
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001553\n\
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001554(i.e. all elements that are in this set but not the others.)");
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +00001555static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001556set_sub(PySetObject *so, PyObject *other)
1557{
Brian Curtindfc80e32011-08-10 20:28:54 -05001558 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1559 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 return set_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001561}
1562
1563static PyObject *
1564set_isub(PySetObject *so, PyObject *other)
1565{
Brian Curtindfc80e32011-08-10 20:28:54 -05001566 if (!PyAnySet_Check(other))
1567 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (set_difference_update_internal(so, other) == -1)
1569 return NULL;
1570 Py_INCREF(so);
1571 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001572}
1573
1574static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001575set_symmetric_difference_update(PySetObject *so, PyObject *other)
1576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 PySetObject *otherset;
1578 PyObject *key;
1579 Py_ssize_t pos = 0;
1580 setentry *entry;
Raymond Hettingerc991db22005-08-11 07:58:45 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if ((PyObject *)so == other)
1583 return set_clear(so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (PyDict_CheckExact(other)) {
1586 PyObject *value;
1587 int rv;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001588 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1590 setentry an_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001591
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001592 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 an_entry.hash = hash;
1594 an_entry.key = key;
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 rv = set_discard_entry(so, &an_entry);
Georg Brandl2d444492010-09-03 10:52:55 +00001597 if (rv == -1) {
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001598 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 }
Raymond Hettingerfaf7b7f2010-09-03 10:00:50 +00001601 if (rv == DISCARD_NOTFOUND) {
1602 if (set_add_entry(so, &an_entry) == -1) {
1603 Py_DECREF(key);
1604 return NULL;
1605 }
1606 }
Georg Brandl2d444492010-09-03 10:52:55 +00001607 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 }
1609 Py_RETURN_NONE;
1610 }
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (PyAnySet_Check(other)) {
1613 Py_INCREF(other);
1614 otherset = (PySetObject *)other;
1615 } else {
1616 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1617 if (otherset == NULL)
1618 return NULL;
1619 }
Raymond Hettingera690a992003-11-16 16:17:49 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 while (set_next(otherset, &pos, &entry)) {
1622 int rv = set_discard_entry(so, entry);
1623 if (rv == -1) {
1624 Py_DECREF(otherset);
1625 return NULL;
1626 }
1627 if (rv == DISCARD_NOTFOUND) {
1628 if (set_add_entry(so, entry) == -1) {
1629 Py_DECREF(otherset);
1630 return NULL;
1631 }
1632 }
1633 }
1634 Py_DECREF(otherset);
1635 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001636}
1637
1638PyDoc_STRVAR(symmetric_difference_update_doc,
1639"Update a set with the symmetric difference of itself and another.");
1640
1641static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001642set_symmetric_difference(PySetObject *so, PyObject *other)
1643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 PyObject *rv;
1645 PySetObject *otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
1648 if (otherset == NULL)
1649 return NULL;
1650 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1651 if (rv == NULL)
1652 return NULL;
1653 Py_DECREF(rv);
1654 return (PyObject *)otherset;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001655}
1656
1657PyDoc_STRVAR(symmetric_difference_doc,
1658"Return the symmetric difference of two sets as a new set.\n\
1659\n\
1660(i.e. all elements that are in exactly one of the sets.)");
1661
1662static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001663set_xor(PySetObject *so, PyObject *other)
1664{
Brian Curtindfc80e32011-08-10 20:28:54 -05001665 if (!PyAnySet_Check(so) || !PyAnySet_Check(other))
1666 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 return set_symmetric_difference(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +00001668}
1669
1670static PyObject *
1671set_ixor(PySetObject *so, PyObject *other)
1672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 PyObject *result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001674
Brian Curtindfc80e32011-08-10 20:28:54 -05001675 if (!PyAnySet_Check(other))
1676 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 result = set_symmetric_difference_update(so, other);
1678 if (result == NULL)
1679 return NULL;
1680 Py_DECREF(result);
1681 Py_INCREF(so);
1682 return (PyObject *)so;
Raymond Hettingera690a992003-11-16 16:17:49 +00001683}
1684
1685static PyObject *
1686set_issubset(PySetObject *so, PyObject *other)
1687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 setentry *entry;
1689 Py_ssize_t pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 if (!PyAnySet_Check(other)) {
1692 PyObject *tmp, *result;
1693 tmp = make_new_set(&PySet_Type, other);
1694 if (tmp == NULL)
1695 return NULL;
1696 result = set_issubset(so, tmp);
1697 Py_DECREF(tmp);
1698 return result;
1699 }
1700 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1701 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 while (set_next(so, &pos, &entry)) {
1704 int rv = set_contains_entry((PySetObject *)other, entry);
1705 if (rv == -1)
1706 return NULL;
1707 if (!rv)
1708 Py_RETURN_FALSE;
1709 }
1710 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001711}
1712
1713PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1714
1715static PyObject *
1716set_issuperset(PySetObject *so, PyObject *other)
1717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 PyObject *tmp, *result;
Raymond Hettinger3fbec702003-11-21 07:56:36 +00001719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 if (!PyAnySet_Check(other)) {
1721 tmp = make_new_set(&PySet_Type, other);
1722 if (tmp == NULL)
1723 return NULL;
1724 result = set_issuperset(so, tmp);
1725 Py_DECREF(tmp);
1726 return result;
1727 }
1728 return set_issubset((PySetObject *)other, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +00001729}
1730
1731PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1732
Raymond Hettingera690a992003-11-16 16:17:49 +00001733static PyObject *
1734set_richcompare(PySetObject *v, PyObject *w, int op)
1735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 PyObject *r1, *r2;
Raymond Hettinger9f1a6792005-07-31 01:16:36 +00001737
Brian Curtindfc80e32011-08-10 20:28:54 -05001738 if(!PyAnySet_Check(w))
1739 Py_RETURN_NOTIMPLEMENTED;
1740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 switch (op) {
1742 case Py_EQ:
1743 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1744 Py_RETURN_FALSE;
1745 if (v->hash != -1 &&
1746 ((PySetObject *)w)->hash != -1 &&
1747 v->hash != ((PySetObject *)w)->hash)
1748 Py_RETURN_FALSE;
1749 return set_issubset(v, w);
1750 case Py_NE:
1751 r1 = set_richcompare(v, w, Py_EQ);
1752 if (r1 == NULL)
1753 return NULL;
1754 r2 = PyBool_FromLong(PyObject_Not(r1));
1755 Py_DECREF(r1);
1756 return r2;
1757 case Py_LE:
1758 return set_issubset(v, w);
1759 case Py_GE:
1760 return set_issuperset(v, w);
1761 case Py_LT:
1762 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1763 Py_RETURN_FALSE;
1764 return set_issubset(v, w);
1765 case Py_GT:
1766 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1767 Py_RETURN_FALSE;
1768 return set_issuperset(v, w);
1769 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001770 Py_RETURN_NOTIMPLEMENTED;
Raymond Hettingera690a992003-11-16 16:17:49 +00001771}
1772
Raymond Hettingera690a992003-11-16 16:17:49 +00001773static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001774set_add(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 if (set_add_key(so, key) == -1)
1777 return NULL;
1778 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001779}
1780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781PyDoc_STRVAR(add_doc,
Raymond Hettingera690a992003-11-16 16:17:49 +00001782"Add an element to a set.\n\
1783\n\
1784This has no effect if the element is already present.");
1785
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001786static int
1787set_contains(PySetObject *so, PyObject *key)
1788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 PyObject *tmpkey;
1790 int rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 rv = set_contains_key(so, key);
1793 if (rv == -1) {
1794 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1795 return -1;
1796 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001797 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 if (tmpkey == NULL)
1799 return -1;
Petri Lehtinen5acc27e2011-10-30 13:56:41 +02001800 rv = set_contains_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 Py_DECREF(tmpkey);
1802 }
1803 return rv;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001804}
1805
1806static PyObject *
1807set_direct_contains(PySetObject *so, PyObject *key)
1808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 long result;
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 result = set_contains(so, key);
1812 if (result == -1)
1813 return NULL;
1814 return PyBool_FromLong(result);
Raymond Hettingerce8185e2005-08-13 09:28:48 +00001815}
1816
1817PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1818
Raymond Hettingera690a992003-11-16 16:17:49 +00001819static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001820set_remove(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyObject *tmpkey;
1823 int rv;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 rv = set_discard_key(so, key);
1826 if (rv == -1) {
1827 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1828 return NULL;
1829 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001830 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 if (tmpkey == NULL)
1832 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 Py_DECREF(tmpkey);
1835 if (rv == -1)
1836 return NULL;
1837 }
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 if (rv == DISCARD_NOTFOUND) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001840 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 return NULL;
1842 }
1843 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001844}
1845
1846PyDoc_STRVAR(remove_doc,
1847"Remove an element from a set; it must be a member.\n\
1848\n\
1849If the element is not a member, raise a KeyError.");
1850
1851static PyObject *
Raymond Hettinger06d8cf82005-07-31 15:36:06 +00001852set_discard(PySetObject *so, PyObject *key)
Raymond Hettingera690a992003-11-16 16:17:49 +00001853{
Benjamin Peterson2b50a012011-10-30 14:24:44 -04001854 PyObject *tmpkey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 int rv;
Raymond Hettinger0deab622003-12-13 18:53:18 +00001856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 rv = set_discard_key(so, key);
1858 if (rv == -1) {
1859 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1860 return NULL;
1861 PyErr_Clear();
Raymond Hettinger38bf2cc2010-08-06 09:52:17 +00001862 tmpkey = make_new_set(&PyFrozenSet_Type, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (tmpkey == NULL)
1864 return NULL;
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001865 rv = set_discard_key(so, tmpkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 Py_DECREF(tmpkey);
Petri Lehtinene0aa8032011-10-30 14:31:27 +02001867 if (rv == -1)
1868 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 }
1870 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +00001871}
1872
1873PyDoc_STRVAR(discard_doc,
1874"Remove an element from a set if it is a member.\n\
1875\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876If the element is not a member, do nothing.");
Raymond Hettingera690a992003-11-16 16:17:49 +00001877
1878static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +00001879set_reduce(PySetObject *so)
1880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001882 _Py_IDENTIFIER(__dict__);
Raymond Hettingera690a992003-11-16 16:17:49 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 keys = PySequence_List((PyObject *)so);
1885 if (keys == NULL)
1886 goto done;
1887 args = PyTuple_Pack(1, keys);
1888 if (args == NULL)
1889 goto done;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001890 dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 if (dict == NULL) {
1892 PyErr_Clear();
1893 dict = Py_None;
1894 Py_INCREF(dict);
1895 }
1896 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
Raymond Hettingera690a992003-11-16 16:17:49 +00001897done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 Py_XDECREF(args);
1899 Py_XDECREF(keys);
1900 Py_XDECREF(dict);
1901 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +00001902}
1903
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001904static PyObject *
1905set_sizeof(PySetObject *so)
1906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 res = sizeof(PySetObject);
1910 if (so->table != so->smalltable)
1911 res = res + (so->mask + 1) * sizeof(setentry);
1912 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00001913}
1914
1915PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001916static int
1917set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 PyObject *iterable = NULL;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (!PyAnySet_Check(self))
1922 return -1;
1923 if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
1924 return -1;
1925 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1926 return -1;
1927 set_clear_internal(self);
1928 self->hash = -1;
1929 if (iterable == NULL)
1930 return 0;
1931 return set_update_internal(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001932}
1933
Raymond Hettingera690a992003-11-16 16:17:49 +00001934static PySequenceMethods set_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 set_len, /* sq_length */
1936 0, /* sq_concat */
1937 0, /* sq_repeat */
1938 0, /* sq_item */
1939 0, /* sq_slice */
1940 0, /* sq_ass_item */
1941 0, /* sq_ass_slice */
1942 (objobjproc)set_contains, /* sq_contains */
Raymond Hettingera690a992003-11-16 16:17:49 +00001943};
1944
1945/* set object ********************************************************/
1946
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001947#ifdef Py_DEBUG
1948static PyObject *test_c_api(PySetObject *so);
1949
1950PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
1951All is well if assertions don't fail.");
1952#endif
1953
Raymond Hettingera690a992003-11-16 16:17:49 +00001954static PyMethodDef set_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 {"add", (PyCFunction)set_add, METH_O,
1956 add_doc},
1957 {"clear", (PyCFunction)set_clear, METH_NOARGS,
1958 clear_doc},
1959 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1960 contains_doc},
1961 {"copy", (PyCFunction)set_copy, METH_NOARGS,
1962 copy_doc},
1963 {"discard", (PyCFunction)set_discard, METH_O,
1964 discard_doc},
1965 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
1966 difference_doc},
1967 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
1968 difference_update_doc},
1969 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
1970 intersection_doc},
1971 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
1972 intersection_update_doc},
1973 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
1974 isdisjoint_doc},
1975 {"issubset", (PyCFunction)set_issubset, METH_O,
1976 issubset_doc},
1977 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1978 issuperset_doc},
1979 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1980 pop_doc},
1981 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1982 reduce_doc},
1983 {"remove", (PyCFunction)set_remove, METH_O,
1984 remove_doc},
1985 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
1986 sizeof_doc},
1987 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1988 symmetric_difference_doc},
1989 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1990 symmetric_difference_update_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001991#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
1993 test_c_api_doc},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00001994#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 {"union", (PyCFunction)set_union, METH_VARARGS,
1996 union_doc},
1997 {"update", (PyCFunction)set_update, METH_VARARGS,
1998 update_doc},
1999 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002000};
2001
2002static PyNumberMethods set_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 0, /*nb_add*/
2004 (binaryfunc)set_sub, /*nb_subtract*/
2005 0, /*nb_multiply*/
2006 0, /*nb_remainder*/
2007 0, /*nb_divmod*/
2008 0, /*nb_power*/
2009 0, /*nb_negative*/
2010 0, /*nb_positive*/
2011 0, /*nb_absolute*/
2012 0, /*nb_bool*/
2013 0, /*nb_invert*/
2014 0, /*nb_lshift*/
2015 0, /*nb_rshift*/
2016 (binaryfunc)set_and, /*nb_and*/
2017 (binaryfunc)set_xor, /*nb_xor*/
2018 (binaryfunc)set_or, /*nb_or*/
2019 0, /*nb_int*/
2020 0, /*nb_reserved*/
2021 0, /*nb_float*/
2022 0, /*nb_inplace_add*/
2023 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2024 0, /*nb_inplace_multiply*/
2025 0, /*nb_inplace_remainder*/
2026 0, /*nb_inplace_power*/
2027 0, /*nb_inplace_lshift*/
2028 0, /*nb_inplace_rshift*/
2029 (binaryfunc)set_iand, /*nb_inplace_and*/
2030 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2031 (binaryfunc)set_ior, /*nb_inplace_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002032};
2033
2034PyDoc_STRVAR(set_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002035"set() -> new empty set object\n\
2036set(iterable) -> new set object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002037\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002038Build an unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002039
2040PyTypeObject PySet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2042 "set", /* tp_name */
2043 sizeof(PySetObject), /* tp_basicsize */
2044 0, /* tp_itemsize */
2045 /* methods */
2046 (destructor)set_dealloc, /* tp_dealloc */
2047 0, /* tp_print */
2048 0, /* tp_getattr */
2049 0, /* tp_setattr */
2050 0, /* tp_reserved */
2051 (reprfunc)set_repr, /* tp_repr */
2052 &set_as_number, /* tp_as_number */
2053 &set_as_sequence, /* tp_as_sequence */
2054 0, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002055 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 0, /* tp_call */
2057 0, /* tp_str */
2058 PyObject_GenericGetAttr, /* tp_getattro */
2059 0, /* tp_setattro */
2060 0, /* tp_as_buffer */
2061 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2062 Py_TPFLAGS_BASETYPE, /* tp_flags */
2063 set_doc, /* tp_doc */
2064 (traverseproc)set_traverse, /* tp_traverse */
2065 (inquiry)set_clear_internal, /* tp_clear */
2066 (richcmpfunc)set_richcompare, /* tp_richcompare */
Georg Brandl00da4e02010-10-18 07:32:48 +00002067 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2068 (getiterfunc)set_iter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 0, /* tp_iternext */
2070 set_methods, /* tp_methods */
2071 0, /* tp_members */
2072 0, /* tp_getset */
2073 0, /* tp_base */
2074 0, /* tp_dict */
2075 0, /* tp_descr_get */
2076 0, /* tp_descr_set */
2077 0, /* tp_dictoffset */
2078 (initproc)set_init, /* tp_init */
2079 PyType_GenericAlloc, /* tp_alloc */
2080 set_new, /* tp_new */
2081 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002082};
2083
2084/* frozenset object ********************************************************/
2085
2086
2087static PyMethodDef frozenset_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2089 contains_doc},
2090 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2091 copy_doc},
2092 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2093 difference_doc},
2094 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2095 intersection_doc},
2096 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2097 isdisjoint_doc},
2098 {"issubset", (PyCFunction)set_issubset, METH_O,
2099 issubset_doc},
2100 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2101 issuperset_doc},
2102 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2103 reduce_doc},
2104 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2105 sizeof_doc},
2106 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2107 symmetric_difference_doc},
2108 {"union", (PyCFunction)set_union, METH_VARARGS,
2109 union_doc},
2110 {NULL, NULL} /* sentinel */
Raymond Hettingera690a992003-11-16 16:17:49 +00002111};
2112
2113static PyNumberMethods frozenset_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 0, /*nb_add*/
2115 (binaryfunc)set_sub, /*nb_subtract*/
2116 0, /*nb_multiply*/
2117 0, /*nb_remainder*/
2118 0, /*nb_divmod*/
2119 0, /*nb_power*/
2120 0, /*nb_negative*/
2121 0, /*nb_positive*/
2122 0, /*nb_absolute*/
2123 0, /*nb_bool*/
2124 0, /*nb_invert*/
2125 0, /*nb_lshift*/
2126 0, /*nb_rshift*/
2127 (binaryfunc)set_and, /*nb_and*/
2128 (binaryfunc)set_xor, /*nb_xor*/
2129 (binaryfunc)set_or, /*nb_or*/
Raymond Hettingera690a992003-11-16 16:17:49 +00002130};
2131
2132PyDoc_STRVAR(frozenset_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002133"frozenset() -> empty frozenset object\n\
2134frozenset(iterable) -> frozenset object\n\
Raymond Hettingera690a992003-11-16 16:17:49 +00002135\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002136Build an immutable unordered collection of unique elements.");
Raymond Hettingera690a992003-11-16 16:17:49 +00002137
2138PyTypeObject PyFrozenSet_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2140 "frozenset", /* tp_name */
2141 sizeof(PySetObject), /* tp_basicsize */
2142 0, /* tp_itemsize */
2143 /* methods */
2144 (destructor)set_dealloc, /* tp_dealloc */
2145 0, /* tp_print */
2146 0, /* tp_getattr */
2147 0, /* tp_setattr */
2148 0, /* tp_reserved */
2149 (reprfunc)set_repr, /* tp_repr */
2150 &frozenset_as_number, /* tp_as_number */
2151 &set_as_sequence, /* tp_as_sequence */
2152 0, /* tp_as_mapping */
2153 frozenset_hash, /* tp_hash */
2154 0, /* tp_call */
2155 0, /* tp_str */
2156 PyObject_GenericGetAttr, /* tp_getattro */
2157 0, /* tp_setattro */
2158 0, /* tp_as_buffer */
2159 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2160 Py_TPFLAGS_BASETYPE, /* tp_flags */
2161 frozenset_doc, /* tp_doc */
2162 (traverseproc)set_traverse, /* tp_traverse */
2163 (inquiry)set_clear_internal, /* tp_clear */
2164 (richcmpfunc)set_richcompare, /* tp_richcompare */
2165 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2166 (getiterfunc)set_iter, /* tp_iter */
2167 0, /* tp_iternext */
2168 frozenset_methods, /* tp_methods */
2169 0, /* tp_members */
2170 0, /* tp_getset */
2171 0, /* tp_base */
2172 0, /* tp_dict */
2173 0, /* tp_descr_get */
2174 0, /* tp_descr_set */
2175 0, /* tp_dictoffset */
2176 0, /* tp_init */
2177 PyType_GenericAlloc, /* tp_alloc */
2178 frozenset_new, /* tp_new */
2179 PyObject_GC_Del, /* tp_free */
Raymond Hettingera690a992003-11-16 16:17:49 +00002180};
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002181
2182
2183/***** C API functions *************************************************/
2184
2185PyObject *
2186PySet_New(PyObject *iterable)
2187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 return make_new_set(&PySet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002189}
2190
2191PyObject *
2192PyFrozenSet_New(PyObject *iterable)
2193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 return make_new_set(&PyFrozenSet_Type, iterable);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002195}
2196
Neal Norwitz8c49c822006-03-04 18:41:19 +00002197Py_ssize_t
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002198PySet_Size(PyObject *anyset)
2199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 if (!PyAnySet_Check(anyset)) {
2201 PyErr_BadInternalCall();
2202 return -1;
2203 }
2204 return PySet_GET_SIZE(anyset);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002205}
2206
2207int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002208PySet_Clear(PyObject *set)
2209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 if (!PySet_Check(set)) {
2211 PyErr_BadInternalCall();
2212 return -1;
2213 }
2214 return set_clear_internal((PySetObject *)set);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002215}
2216
2217int
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002218PySet_Contains(PyObject *anyset, PyObject *key)
2219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 if (!PyAnySet_Check(anyset)) {
2221 PyErr_BadInternalCall();
2222 return -1;
2223 }
2224 return set_contains_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002225}
2226
2227int
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002228PySet_Discard(PyObject *set, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 if (!PySet_Check(set)) {
2231 PyErr_BadInternalCall();
2232 return -1;
2233 }
2234 return set_discard_key((PySetObject *)set, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002235}
2236
2237int
Christian Heimesfd66e512008-01-29 12:18:50 +00002238PySet_Add(PyObject *anyset, PyObject *key)
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 if (!PySet_Check(anyset) &&
2241 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2242 PyErr_BadInternalCall();
2243 return -1;
2244 }
2245 return set_add_key((PySetObject *)anyset, key);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002246}
2247
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002248int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002249_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 setentry *entry;
Guido van Rossumd8faa362007-04-27 19:54:29 +00002252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 if (!PyAnySet_Check(set)) {
2254 PyErr_BadInternalCall();
2255 return -1;
2256 }
2257 if (set_next((PySetObject *)set, pos, &entry) == 0)
2258 return 0;
2259 *key = entry->key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002260 *hash = entry->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002262}
2263
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002264PyObject *
2265PySet_Pop(PyObject *set)
2266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 if (!PySet_Check(set)) {
2268 PyErr_BadInternalCall();
2269 return NULL;
2270 }
2271 return set_pop((PySetObject *)set);
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002272}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002273
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002274int
2275_PySet_Update(PyObject *set, PyObject *iterable)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 if (!PySet_Check(set)) {
2278 PyErr_BadInternalCall();
2279 return -1;
2280 }
2281 return set_update_internal((PySetObject *)set, iterable);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002282}
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002283
Raymond Hettingere259f132013-12-15 11:56:14 -08002284/* Exported for the gdb plugin's benefit. */
2285PyObject *_PySet_Dummy = dummy;
2286
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002287#ifdef Py_DEBUG
2288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289/* Test code to be called with any three element set.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002290 Returns True and original set is restored. */
2291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292#define assertRaises(call_return_value, exception) \
2293 do { \
2294 assert(call_return_value); \
2295 assert(PyErr_ExceptionMatches(exception)); \
2296 PyErr_Clear(); \
2297 } while(0)
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002298
2299static PyObject *
2300test_c_api(PySetObject *so)
2301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 Py_ssize_t count;
2303 char *s;
2304 Py_ssize_t i;
Raymond Hettinger583cd032013-09-07 22:06:35 -07002305 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x=NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 PyObject *ob = (PyObject *)so;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002307 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 PyObject *str;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 /* Verify preconditions */
2311 assert(PyAnySet_Check(ob));
2312 assert(PyAnySet_CheckExact(ob));
2313 assert(!PyFrozenSet_CheckExact(ob));
Victor Stinner08b36bd2010-03-13 00:19:17 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 /* so.clear(); so |= set("abc"); */
2316 str = PyUnicode_FromString("abc");
2317 if (str == NULL)
2318 return NULL;
2319 set_clear_internal(so);
2320 if (set_update_internal(so, str) == -1) {
2321 Py_DECREF(str);
2322 return NULL;
2323 }
2324 Py_DECREF(str);
Victor Stinner08b36bd2010-03-13 00:19:17 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 /* Exercise type/size checks */
2327 assert(PySet_Size(ob) == 3);
2328 assert(PySet_GET_SIZE(ob) == 3);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 /* Raise TypeError for non-iterable constructor arguments */
2331 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2332 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 /* Raise TypeError for unhashable key */
2335 dup = PySet_New(ob);
2336 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2337 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2338 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 /* Exercise successful pop, contains, add, and discard */
2341 elem = PySet_Pop(ob);
2342 assert(PySet_Contains(ob, elem) == 0);
2343 assert(PySet_GET_SIZE(ob) == 2);
2344 assert(PySet_Add(ob, elem) == 0);
2345 assert(PySet_Contains(ob, elem) == 1);
2346 assert(PySet_GET_SIZE(ob) == 3);
2347 assert(PySet_Discard(ob, elem) == 1);
2348 assert(PySet_GET_SIZE(ob) == 2);
2349 assert(PySet_Discard(ob, elem) == 0);
2350 assert(PySet_GET_SIZE(ob) == 2);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 /* Exercise clear */
2353 dup2 = PySet_New(dup);
2354 assert(PySet_Clear(dup2) == 0);
2355 assert(PySet_Size(dup2) == 0);
2356 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 /* Raise SystemError on clear or update of frozen set */
2359 f = PyFrozenSet_New(dup);
2360 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2361 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2362 assert(PySet_Add(f, elem) == 0);
2363 Py_INCREF(f);
2364 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2365 Py_DECREF(f);
2366 Py_DECREF(f);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 /* Exercise direct iteration */
2369 i = 0, count = 0;
2370 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2371 s = _PyUnicode_AsString(x);
2372 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2373 count++;
2374 }
2375 assert(count == 3);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 /* Exercise updates */
2378 dup2 = PySet_New(NULL);
2379 assert(_PySet_Update(dup2, dup) == 0);
2380 assert(PySet_Size(dup2) == 3);
2381 assert(_PySet_Update(dup2, dup) == 0);
2382 assert(PySet_Size(dup2) == 3);
2383 Py_DECREF(dup2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 /* Raise SystemError when self argument is not a set or frozenset. */
2386 t = PyTuple_New(0);
2387 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2388 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2389 Py_DECREF(t);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* Raise SystemError when self argument is not a set. */
2392 f = PyFrozenSet_New(dup);
2393 assert(PySet_Size(f) == 3);
2394 assert(PyFrozenSet_CheckExact(f));
2395 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2396 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2397 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 /* Raise KeyError when popping from an empty set */
2400 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2401 Py_DECREF(ob);
2402 assert(PySet_GET_SIZE(ob) == 0);
2403 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* Restore the set from the copy using the PyNumber API */
2406 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2407 Py_DECREF(ob);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* Verify constructors accept NULL arguments */
2410 f = PySet_New(NULL);
2411 assert(f != NULL);
2412 assert(PySet_GET_SIZE(f) == 0);
2413 Py_DECREF(f);
2414 f = PyFrozenSet_New(NULL);
2415 assert(f != NULL);
2416 assert(PyFrozenSet_CheckExact(f));
2417 assert(PySet_GET_SIZE(f) == 0);
2418 Py_DECREF(f);
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 Py_DECREF(elem);
2421 Py_DECREF(dup);
2422 Py_RETURN_TRUE;
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002423}
2424
Raymond Hettinger9bda1d62005-09-16 07:14:21 +00002425#undef assertRaises
2426
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002427#endif
Raymond Hettingerbfc1e1a2013-08-23 03:22:15 -05002428
2429/***** Dummy Struct *************************************************/
2430
2431static PyObject *
2432dummy_repr(PyObject *op)
2433{
2434 return PyUnicode_FromString("<dummy key>");
2435}
2436
2437static void
2438dummy_dealloc(PyObject* ignore)
2439{
2440 Py_FatalError("deallocating <dummy key>");
2441}
2442
2443static PyTypeObject _PySetDummy_Type = {
2444 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2445 "<dummy key> type",
2446 0,
2447 0,
2448 dummy_dealloc, /*tp_dealloc*/ /*never called*/
2449 0, /*tp_print*/
2450 0, /*tp_getattr*/
2451 0, /*tp_setattr*/
2452 0, /*tp_reserved*/
2453 dummy_repr, /*tp_repr*/
2454 0, /*tp_as_number*/
2455 0, /*tp_as_sequence*/
2456 0, /*tp_as_mapping*/
2457 0, /*tp_hash */
2458 0, /*tp_call */
2459 0, /*tp_str */
2460 0, /*tp_getattro */
2461 0, /*tp_setattro */
2462 0, /*tp_as_buffer */
2463 Py_TPFLAGS_DEFAULT, /*tp_flags */
2464};
2465
2466static PyObject _dummy_struct = {
2467 _PyObject_EXTRA_INIT
2468 2, &_PySetDummy_Type
2469};
2470