Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2 | /* set object implementation |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 3 | Written and maintained by Raymond D. Hettinger <python@rcn.com> |
| 4 | Derived from Lib/sets.py and Objects/dictobject.c. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 5 | |
Raymond Hettinger | 2212e52 | 2008-11-30 23:43:36 +0000 | [diff] [blame] | 6 | Copyright (c) 2003-2008 Python Software Foundation. |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 7 | All rights reserved. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 11 | #include "structmember.h" |
Christian Heimes | 0ded5b5 | 2007-12-10 15:50:56 +0000 | [diff] [blame] | 12 | #include "stringlib/eq.h" |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 14 | /* Set a key error with the specified argument, wrapping it in a |
| 15 | * tuple automatically so that tuple keys are not unpacked as the |
| 16 | * exception arguments. */ |
| 17 | static void |
| 18 | set_key_error(PyObject *arg) |
| 19 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | PyObject *tup; |
| 21 | tup = PyTuple_Pack(1, arg); |
| 22 | if (!tup) |
| 23 | return; /* caller will expect error to be set anyway */ |
| 24 | PyErr_SetObject(PyExc_KeyError, tup); |
| 25 | Py_DECREF(tup); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 28 | /* This must be >= 1. */ |
| 29 | #define PERTURB_SHIFT 5 |
| 30 | |
| 31 | /* Object used as dummy key to fill deleted entries */ |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 32 | static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 33 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 34 | #ifdef Py_REF_DEBUG |
| 35 | PyObject * |
| 36 | _PySet_Dummy(void) |
| 37 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | return dummy; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 39 | } |
| 40 | #endif |
| 41 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | #define INIT_NONZERO_SET_SLOTS(so) do { \ |
| 43 | (so)->table = (so)->smalltable; \ |
| 44 | (so)->mask = PySet_MINSIZE - 1; \ |
| 45 | (so)->hash = -1; \ |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 46 | } while(0) |
| 47 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | #define EMPTY_TO_MINSIZE(so) do { \ |
| 49 | memset((so)->smalltable, 0, sizeof((so)->smalltable)); \ |
| 50 | (so)->used = (so)->fill = 0; \ |
| 51 | INIT_NONZERO_SET_SLOTS(so); \ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 52 | } while(0) |
| 53 | |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 54 | /* Reuse scheme to save calls to malloc, free, and memset */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #ifndef PySet_MAXFREELIST |
| 56 | #define PySet_MAXFREELIST 80 |
| 57 | #endif |
| 58 | static PySetObject *free_list[PySet_MAXFREELIST]; |
| 59 | static int numfree = 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 60 | |
Christian Heimes | 0ded5b5 | 2007-12-10 15:50:56 +0000 | [diff] [blame] | 61 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 62 | /* |
| 63 | The basic lookup function used by all operations. |
| 64 | This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4. |
| 65 | Open addressing is preferred over chaining since the link overhead for |
| 66 | chaining would be substantial (100% with typical malloc overhead). |
| 67 | |
| 68 | The initial probe index is computed as hash mod the table size. Subsequent |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 69 | probe indices are computed as explained in Objects/dictobject.c. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 70 | |
| 71 | All arithmetic on hash should ignore overflow. |
| 72 | |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 73 | Unlike the dictionary implementation, the lookkey functions can return |
| 74 | NULL if the rich comparison returns an error. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 75 | */ |
| 76 | |
| 77 | static setentry * |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 78 | set_lookkey(PySetObject *so, PyObject *key, register Py_hash_t hash) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 79 | { |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 80 | register size_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | register size_t perturb; |
| 82 | register setentry *freeslot; |
| 83 | register size_t mask = so->mask; |
| 84 | setentry *table = so->table; |
| 85 | register setentry *entry; |
| 86 | register int cmp; |
| 87 | PyObject *startkey; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 88 | |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 89 | i = (size_t)hash & mask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | entry = &table[i]; |
| 91 | if (entry->key == NULL || entry->key == key) |
| 92 | return entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 93 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | if (entry->key == dummy) |
| 95 | freeslot = entry; |
| 96 | else { |
| 97 | if (entry->hash == hash) { |
| 98 | startkey = entry->key; |
| 99 | Py_INCREF(startkey); |
| 100 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 101 | Py_DECREF(startkey); |
| 102 | if (cmp < 0) |
| 103 | return NULL; |
| 104 | if (table == so->table && entry->key == startkey) { |
| 105 | if (cmp > 0) |
| 106 | return entry; |
| 107 | } |
| 108 | else { |
| 109 | /* The compare did major nasty stuff to the |
| 110 | * set: start over. |
| 111 | */ |
| 112 | return set_lookkey(so, key, hash); |
| 113 | } |
| 114 | } |
| 115 | freeslot = NULL; |
| 116 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | /* In the loop, key == dummy is by far (factor of 100s) the |
| 119 | least likely outcome, so test for that last. */ |
| 120 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 121 | i = (i << 2) + i + perturb + 1; |
| 122 | entry = &table[i & mask]; |
| 123 | if (entry->key == NULL) { |
| 124 | if (freeslot != NULL) |
| 125 | entry = freeslot; |
| 126 | break; |
| 127 | } |
| 128 | if (entry->key == key) |
| 129 | break; |
| 130 | if (entry->hash == hash && entry->key != dummy) { |
| 131 | startkey = entry->key; |
| 132 | Py_INCREF(startkey); |
| 133 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 134 | Py_DECREF(startkey); |
| 135 | if (cmp < 0) |
| 136 | return NULL; |
| 137 | if (table == so->table && entry->key == startkey) { |
| 138 | if (cmp > 0) |
| 139 | break; |
| 140 | } |
| 141 | else { |
| 142 | /* The compare did major nasty stuff to the |
| 143 | * set: start over. |
| 144 | */ |
| 145 | return set_lookkey(so, key, hash); |
| 146 | } |
| 147 | } |
| 148 | else if (entry->key == dummy && freeslot == NULL) |
| 149 | freeslot = entry; |
| 150 | } |
| 151 | return entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
Christian Heimes | 0ded5b5 | 2007-12-10 15:50:56 +0000 | [diff] [blame] | 155 | * Hacked up version of set_lookkey which can assume keys are always unicode; |
| 156 | * This means we can always use unicode_eq directly and not have to check to |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 157 | * see if the comparison altered the table. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 158 | */ |
| 159 | static setentry * |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 160 | set_lookkey_unicode(PySetObject *so, PyObject *key, register Py_hash_t hash) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 161 | { |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 162 | register size_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | register size_t perturb; |
| 164 | register setentry *freeslot; |
| 165 | register size_t mask = so->mask; |
| 166 | setentry *table = so->table; |
| 167 | register setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | /* Make sure this function doesn't have to handle non-unicode keys, |
| 170 | including subclasses of str; e.g., one reason to subclass |
| 171 | strings is to override __eq__, and for speed we don't cater to |
| 172 | that here. */ |
| 173 | if (!PyUnicode_CheckExact(key)) { |
| 174 | so->lookup = set_lookkey; |
| 175 | return set_lookkey(so, key, hash); |
| 176 | } |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 177 | i = (size_t)hash & mask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | entry = &table[i]; |
| 179 | if (entry->key == NULL || entry->key == key) |
| 180 | return entry; |
| 181 | if (entry->key == dummy) |
| 182 | freeslot = entry; |
| 183 | else { |
| 184 | if (entry->hash == hash && unicode_eq(entry->key, key)) |
| 185 | return entry; |
| 186 | freeslot = NULL; |
| 187 | } |
Raymond Hettinger | ed6c1ef | 2005-08-13 08:28:03 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | /* In the loop, key == dummy is by far (factor of 100s) the |
| 190 | least likely outcome, so test for that last. */ |
| 191 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 192 | i = (i << 2) + i + perturb + 1; |
| 193 | entry = &table[i & mask]; |
| 194 | if (entry->key == NULL) |
| 195 | return freeslot == NULL ? entry : freeslot; |
| 196 | if (entry->key == key |
| 197 | || (entry->hash == hash |
| 198 | && entry->key != dummy |
| 199 | && unicode_eq(entry->key, key))) |
| 200 | return entry; |
| 201 | if (entry->key == dummy && freeslot == NULL) |
| 202 | freeslot = entry; |
| 203 | } |
| 204 | assert(0); /* NOT REACHED */ |
| 205 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 209 | Internal routine to insert a new key into the table. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 210 | Used by the public insert routine. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 211 | Eats a reference to key. |
| 212 | */ |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 213 | static int |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 214 | set_insert_key(register PySetObject *so, PyObject *key, Py_hash_t hash) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 215 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | register setentry *entry; |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 217 | typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, Py_hash_t); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | assert(so->lookup != NULL); |
| 220 | entry = so->lookup(so, key, hash); |
| 221 | if (entry == NULL) |
| 222 | return -1; |
| 223 | if (entry->key == NULL) { |
| 224 | /* UNUSED */ |
| 225 | so->fill++; |
| 226 | entry->key = key; |
| 227 | entry->hash = hash; |
| 228 | so->used++; |
| 229 | } else if (entry->key == dummy) { |
| 230 | /* DUMMY */ |
| 231 | entry->key = key; |
| 232 | entry->hash = hash; |
| 233 | so->used++; |
| 234 | Py_DECREF(dummy); |
| 235 | } else { |
| 236 | /* ACTIVE */ |
| 237 | Py_DECREF(key); |
| 238 | } |
| 239 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 243 | Internal routine used by set_table_resize() to insert an item which is |
| 244 | known to be absent from the set. This routine also assumes that |
| 245 | the set contains no deleted entries. Besides the performance benefit, |
| 246 | using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209). |
| 247 | Note that no refcounts are changed by this routine; if needed, the caller |
| 248 | is responsible for incref'ing `key`. |
| 249 | */ |
| 250 | static void |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 251 | set_insert_clean(register PySetObject *so, PyObject *key, Py_hash_t hash) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 252 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | register size_t i; |
| 254 | register size_t perturb; |
| 255 | register size_t mask = (size_t)so->mask; |
| 256 | setentry *table = so->table; |
| 257 | register setentry *entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 258 | |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 259 | i = (size_t)hash & mask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | entry = &table[i]; |
| 261 | for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) { |
| 262 | i = (i << 2) + i + perturb + 1; |
| 263 | entry = &table[i & mask]; |
| 264 | } |
| 265 | so->fill++; |
| 266 | entry->key = key; |
| 267 | entry->hash = hash; |
| 268 | so->used++; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 272 | Restructure the table by allocating a new table and reinserting all |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 273 | keys again. When entries have been deleted, the new table may |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 274 | actually be smaller than the old one. |
| 275 | */ |
| 276 | static int |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 277 | set_table_resize(PySetObject *so, Py_ssize_t minused) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 278 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | Py_ssize_t newsize; |
| 280 | setentry *oldtable, *newtable, *entry; |
| 281 | Py_ssize_t i; |
| 282 | int is_oldtable_malloced; |
| 283 | setentry small_copy[PySet_MINSIZE]; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | assert(minused >= 0); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | /* Find the smallest table size > minused. */ |
| 288 | for (newsize = PySet_MINSIZE; |
| 289 | newsize <= minused && newsize > 0; |
| 290 | newsize <<= 1) |
| 291 | ; |
| 292 | if (newsize <= 0) { |
| 293 | PyErr_NoMemory(); |
| 294 | return -1; |
| 295 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | /* Get space for a new table. */ |
| 298 | oldtable = so->table; |
| 299 | assert(oldtable != NULL); |
| 300 | is_oldtable_malloced = oldtable != so->smalltable; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | if (newsize == PySet_MINSIZE) { |
| 303 | /* A large table is shrinking, or we can't get any smaller. */ |
| 304 | newtable = so->smalltable; |
| 305 | if (newtable == oldtable) { |
| 306 | if (so->fill == so->used) { |
| 307 | /* No dummies, so no point doing anything. */ |
| 308 | return 0; |
| 309 | } |
| 310 | /* We're not going to resize it, but rebuild the |
| 311 | table anyway to purge old dummy entries. |
| 312 | Subtle: This is *necessary* if fill==size, |
| 313 | as set_lookkey needs at least one virgin slot to |
| 314 | terminate failing searches. If fill < size, it's |
| 315 | merely desirable, as dummies slow searches. */ |
| 316 | assert(so->fill > so->used); |
| 317 | memcpy(small_copy, oldtable, sizeof(small_copy)); |
| 318 | oldtable = small_copy; |
| 319 | } |
| 320 | } |
| 321 | else { |
| 322 | newtable = PyMem_NEW(setentry, newsize); |
| 323 | if (newtable == NULL) { |
| 324 | PyErr_NoMemory(); |
| 325 | return -1; |
| 326 | } |
| 327 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | /* Make the set empty, using the new table. */ |
| 330 | assert(newtable != oldtable); |
| 331 | so->table = newtable; |
| 332 | so->mask = newsize - 1; |
| 333 | memset(newtable, 0, sizeof(setentry) * newsize); |
| 334 | so->used = 0; |
| 335 | i = so->fill; |
| 336 | so->fill = 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | /* Copy the data over; this is refcount-neutral for active entries; |
| 339 | dummy entries aren't copied over, of course */ |
| 340 | for (entry = oldtable; i > 0; entry++) { |
| 341 | if (entry->key == NULL) { |
| 342 | /* UNUSED */ |
| 343 | ; |
| 344 | } else if (entry->key == dummy) { |
| 345 | /* DUMMY */ |
| 346 | --i; |
| 347 | assert(entry->key == dummy); |
| 348 | Py_DECREF(entry->key); |
| 349 | } else { |
| 350 | /* ACTIVE */ |
| 351 | --i; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 352 | set_insert_clean(so, entry->key, entry->hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | if (is_oldtable_malloced) |
| 357 | PyMem_DEL(oldtable); |
| 358 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 361 | /* CAUTION: set_add_key/entry() must guarantee it won't resize the table */ |
| 362 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 363 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 364 | set_add_entry(register PySetObject *so, setentry *entry) |
| 365 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | register Py_ssize_t n_used; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 367 | PyObject *key = entry->key; |
Éric Araujo | bbcfc1f | 2011-03-23 03:43:22 +0100 | [diff] [blame] | 368 | Py_hash_t hash = entry->hash; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | assert(so->fill <= so->mask); /* at least one empty slot */ |
| 371 | n_used = so->used; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 372 | Py_INCREF(key); |
Éric Araujo | 4804991 | 2011-03-23 02:08:07 +0100 | [diff] [blame] | 373 | if (set_insert_key(so, key, hash) == -1) { |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 374 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | return -1; |
| 376 | } |
| 377 | if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2)) |
| 378 | return 0; |
| 379 | return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int |
| 383 | set_add_key(register PySetObject *so, PyObject *key) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 384 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 385 | register Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | register Py_ssize_t n_used; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 389 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | hash = PyObject_Hash(key); |
| 391 | if (hash == -1) |
| 392 | return -1; |
| 393 | } |
| 394 | assert(so->fill <= so->mask); /* at least one empty slot */ |
| 395 | n_used = so->used; |
| 396 | Py_INCREF(key); |
| 397 | if (set_insert_key(so, key, hash) == -1) { |
| 398 | Py_DECREF(key); |
| 399 | return -1; |
| 400 | } |
| 401 | if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2)) |
| 402 | return 0; |
| 403 | return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | #define DISCARD_NOTFOUND 0 |
| 407 | #define DISCARD_FOUND 1 |
| 408 | |
| 409 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 410 | set_discard_entry(PySetObject *so, setentry *oldentry) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | { register setentry *entry; |
| 412 | PyObject *old_key; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 413 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 414 | entry = (so->lookup)(so, oldentry->key, oldentry->hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | if (entry == NULL) |
| 416 | return -1; |
| 417 | if (entry->key == NULL || entry->key == dummy) |
| 418 | return DISCARD_NOTFOUND; |
| 419 | old_key = entry->key; |
| 420 | Py_INCREF(dummy); |
| 421 | entry->key = dummy; |
| 422 | so->used--; |
| 423 | Py_DECREF(old_key); |
| 424 | return DISCARD_FOUND; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static int |
| 428 | set_discard_key(PySetObject *so, PyObject *key) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 429 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 430 | register Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | register setentry *entry; |
| 432 | PyObject *old_key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | assert (PyAnySet_Check(so)); |
Christian Heimes | 0ded5b5 | 2007-12-10 15:50:56 +0000 | [diff] [blame] | 435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 437 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | hash = PyObject_Hash(key); |
| 439 | if (hash == -1) |
| 440 | return -1; |
| 441 | } |
| 442 | entry = (so->lookup)(so, key, hash); |
| 443 | if (entry == NULL) |
| 444 | return -1; |
| 445 | if (entry->key == NULL || entry->key == dummy) |
| 446 | return DISCARD_NOTFOUND; |
| 447 | old_key = entry->key; |
| 448 | Py_INCREF(dummy); |
| 449 | entry->key = dummy; |
| 450 | so->used--; |
| 451 | Py_DECREF(old_key); |
| 452 | return DISCARD_FOUND; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Raymond Hettinger | fe889f3 | 2005-08-06 05:43:39 +0000 | [diff] [blame] | 455 | static int |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 456 | set_clear_internal(PySetObject *so) |
| 457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | setentry *entry, *table; |
| 459 | int table_is_malloced; |
| 460 | Py_ssize_t fill; |
| 461 | setentry small_copy[PySet_MINSIZE]; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 462 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | Py_ssize_t i, n; |
| 464 | assert (PyAnySet_Check(so)); |
Raymond Hettinger | a580c47 | 2005-08-05 17:19:54 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | n = so->mask + 1; |
| 467 | i = 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 468 | #endif |
| 469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | table = so->table; |
| 471 | assert(table != NULL); |
| 472 | table_is_malloced = table != so->smalltable; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | /* This is delicate. During the process of clearing the set, |
| 475 | * decrefs can cause the set to mutate. To avoid fatal confusion |
| 476 | * (voice of experience), we have to make the set empty before |
| 477 | * clearing the slots, and never refer to anything via so->ref while |
| 478 | * clearing. |
| 479 | */ |
| 480 | fill = so->fill; |
| 481 | if (table_is_malloced) |
| 482 | EMPTY_TO_MINSIZE(so); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | else if (fill > 0) { |
| 485 | /* It's a small table with something that needs to be cleared. |
| 486 | * Afraid the only safe way is to copy the set entries into |
| 487 | * another small table first. |
| 488 | */ |
| 489 | memcpy(small_copy, table, sizeof(small_copy)); |
| 490 | table = small_copy; |
| 491 | EMPTY_TO_MINSIZE(so); |
| 492 | } |
| 493 | /* else it's a small table that's already empty */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | /* Now we can finally clear things. If C had refcounts, we could |
| 496 | * assert that the refcount on table is 1 now, i.e. that this function |
| 497 | * has unique access to it, so decref side-effects can't alter it. |
| 498 | */ |
| 499 | for (entry = table; fill > 0; ++entry) { |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 500 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | assert(i < n); |
| 502 | ++i; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 503 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | if (entry->key) { |
| 505 | --fill; |
| 506 | Py_DECREF(entry->key); |
| 507 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 508 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | else |
| 510 | assert(entry->key == NULL); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 511 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | if (table_is_malloced) |
| 515 | PyMem_DEL(table); |
| 516 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Iterate over a set table. Use like so: |
| 521 | * |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 522 | * Py_ssize_t pos; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 523 | * setentry *entry; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 524 | * pos = 0; # important! pos should not otherwise be changed by you |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 525 | * while (set_next(yourset, &pos, &entry)) { |
| 526 | * Refer to borrowed reference in entry->key. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 527 | * } |
| 528 | * |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 529 | * CAUTION: In general, it isn't safe to use set_next in a loop that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | * mutates the table. |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 531 | */ |
| 532 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 533 | set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | Py_ssize_t i; |
| 536 | Py_ssize_t mask; |
| 537 | register setentry *table; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | assert (PyAnySet_Check(so)); |
| 540 | i = *pos_ptr; |
| 541 | assert(i >= 0); |
| 542 | table = so->table; |
| 543 | mask = so->mask; |
| 544 | while (i <= mask && (table[i].key == NULL || table[i].key == dummy)) |
| 545 | i++; |
| 546 | *pos_ptr = i+1; |
| 547 | if (i > mask) |
| 548 | return 0; |
| 549 | assert(table[i].key != NULL); |
| 550 | *entry_ptr = &table[i]; |
| 551 | return 1; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 554 | static void |
| 555 | set_dealloc(PySetObject *so) |
| 556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | register setentry *entry; |
| 558 | Py_ssize_t fill = so->fill; |
| 559 | PyObject_GC_UnTrack(so); |
| 560 | Py_TRASHCAN_SAFE_BEGIN(so) |
| 561 | if (so->weakreflist != NULL) |
| 562 | PyObject_ClearWeakRefs((PyObject *) so); |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | for (entry = so->table; fill > 0; entry++) { |
| 565 | if (entry->key) { |
| 566 | --fill; |
| 567 | Py_DECREF(entry->key); |
| 568 | } |
| 569 | } |
| 570 | if (so->table != so->smalltable) |
| 571 | PyMem_DEL(so->table); |
| 572 | if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so)) |
| 573 | free_list[numfree++] = so; |
| 574 | else |
| 575 | Py_TYPE(so)->tp_free(so); |
| 576 | Py_TRASHCAN_SAFE_END(so) |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 579 | static PyObject * |
| 580 | set_repr(PySetObject *so) |
| 581 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 582 | PyObject *result=NULL, *keys, *listrepr, *tmp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | int status = Py_ReprEnter((PyObject*)so); |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | if (status != 0) { |
| 586 | if (status < 0) |
| 587 | return NULL; |
| 588 | return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name); |
| 589 | } |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | /* shortcut for the empty set */ |
| 592 | if (!so->used) { |
| 593 | Py_ReprLeave((PyObject*)so); |
| 594 | return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name); |
| 595 | } |
Georg Brandl | c4996ba | 2006-08-28 19:37:11 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | keys = PySequence_List((PyObject *)so); |
| 598 | if (keys == NULL) |
| 599 | goto done; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 600 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 601 | /* repr(keys)[1:-1] */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | listrepr = PyObject_Repr(keys); |
| 603 | Py_DECREF(keys); |
| 604 | if (listrepr == NULL) |
| 605 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 606 | tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | Py_DECREF(listrepr); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 608 | if (tmp == NULL) |
| 609 | goto done; |
| 610 | listrepr = tmp; |
Victor Stinner | a1a807b | 2011-05-26 14:24:30 +0200 | [diff] [blame] | 611 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 612 | if (Py_TYPE(so) != &PySet_Type) |
| 613 | result = PyUnicode_FromFormat("%s({%U})", |
| 614 | Py_TYPE(so)->tp_name, |
| 615 | listrepr); |
| 616 | else |
| 617 | result = PyUnicode_FromFormat("{%U}", listrepr); |
| 618 | Py_DECREF(listrepr); |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 619 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | Py_ReprLeave((PyObject*)so); |
| 621 | return result; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 624 | static Py_ssize_t |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 625 | set_len(PyObject *so) |
| 626 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | return ((PySetObject *)so)->used; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 630 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 631 | set_merge(PySetObject *so, PyObject *otherset) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | PySetObject *other; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 634 | PyObject *key; |
Éric Araujo | bbcfc1f | 2011-03-23 03:43:22 +0100 | [diff] [blame] | 635 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | register Py_ssize_t i; |
| 637 | register setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | assert (PyAnySet_Check(so)); |
| 640 | assert (PyAnySet_Check(otherset)); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | other = (PySetObject*)otherset; |
| 643 | if (other == so || other->used == 0) |
| 644 | /* a.update(a) or a.update({}); nothing to do */ |
| 645 | return 0; |
| 646 | /* Do one big resize at the start, rather than |
| 647 | * incrementally resizing as we insert new keys. Expect |
| 648 | * that there will be no (or few) overlapping keys. |
| 649 | */ |
| 650 | if ((so->fill + other->used)*3 >= (so->mask+1)*2) { |
| 651 | if (set_table_resize(so, (so->used + other->used)*2) != 0) |
| 652 | return -1; |
| 653 | } |
| 654 | for (i = 0; i <= other->mask; i++) { |
| 655 | entry = &other->table[i]; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 656 | key = entry->key; |
Éric Araujo | 4804991 | 2011-03-23 02:08:07 +0100 | [diff] [blame] | 657 | hash = entry->hash; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 658 | if (key != NULL && |
| 659 | key != dummy) { |
| 660 | Py_INCREF(key); |
Éric Araujo | 4804991 | 2011-03-23 02:08:07 +0100 | [diff] [blame] | 661 | if (set_insert_key(so, key, hash) == -1) { |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 662 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | return -1; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 671 | set_contains_key(PySetObject *so, PyObject *key) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 672 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 673 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 677 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | hash = PyObject_Hash(key); |
| 679 | if (hash == -1) |
| 680 | return -1; |
| 681 | } |
| 682 | entry = (so->lookup)(so, key, hash); |
| 683 | if (entry == NULL) |
| 684 | return -1; |
| 685 | key = entry->key; |
| 686 | return key != NULL && key != dummy; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 689 | static int |
| 690 | set_contains_entry(PySetObject *so, setentry *entry) |
| 691 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | PyObject *key; |
| 693 | setentry *lu_entry; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 694 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 695 | lu_entry = (so->lookup)(so, entry->key, entry->hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | if (lu_entry == NULL) |
| 697 | return -1; |
| 698 | key = lu_entry->key; |
| 699 | return key != NULL && key != dummy; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 702 | static PyObject * |
| 703 | set_pop(PySetObject *so) |
| 704 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | register Py_ssize_t i = 0; |
| 706 | register setentry *entry; |
| 707 | PyObject *key; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | assert (PyAnySet_Check(so)); |
| 710 | if (so->used == 0) { |
| 711 | PyErr_SetString(PyExc_KeyError, "pop from an empty set"); |
| 712 | return NULL; |
| 713 | } |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | /* Set entry to "the first" unused or dummy set entry. We abuse |
| 716 | * the hash field of slot 0 to hold a search finger: |
| 717 | * If slot 0 has a value, use slot 0. |
| 718 | * Else slot 0 is being used to hold a search finger, |
| 719 | * and we use its hash value as the first index to look. |
| 720 | */ |
| 721 | entry = &so->table[0]; |
| 722 | if (entry->key == NULL || entry->key == dummy) { |
| 723 | i = entry->hash; |
| 724 | /* The hash field may be a real hash value, or it may be a |
| 725 | * legit search finger, or it may be a once-legit search |
| 726 | * finger that's out of bounds now because it wrapped around |
| 727 | * or the table shrunk -- simply make sure it's in bounds now. |
| 728 | */ |
| 729 | if (i > so->mask || i < 1) |
| 730 | i = 1; /* skip slot 0 */ |
| 731 | while ((entry = &so->table[i])->key == NULL || entry->key==dummy) { |
| 732 | i++; |
| 733 | if (i > so->mask) |
| 734 | i = 1; |
| 735 | } |
| 736 | } |
| 737 | key = entry->key; |
| 738 | Py_INCREF(dummy); |
| 739 | entry->key = dummy; |
| 740 | so->used--; |
| 741 | so->table[0].hash = i + 1; /* next place to start */ |
| 742 | return key; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 745 | PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\ |
| 746 | Raises KeyError if the set is empty."); |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 747 | |
| 748 | static int |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 749 | set_traverse(PySetObject *so, visitproc visit, void *arg) |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | Py_ssize_t pos = 0; |
| 752 | setentry *entry; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | while (set_next(so, &pos, &entry)) |
| 755 | Py_VISIT(entry->key); |
| 756 | return 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 759 | static Py_hash_t |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 760 | frozenset_hash(PyObject *self) |
| 761 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | PySetObject *so = (PySetObject *)self; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 763 | Py_uhash_t h, hash = 1927868237U; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | setentry *entry; |
| 765 | Py_ssize_t pos = 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | if (so->hash != -1) |
| 768 | return so->hash; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 769 | |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 770 | hash *= (Py_uhash_t)PySet_GET_SIZE(self) + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | while (set_next(so, &pos, &entry)) { |
| 772 | /* Work to increase the bit dispersion for closely spaced hash |
| 773 | values. The is important because some use cases have many |
| 774 | combinations of a small number of elements with nearby |
| 775 | hashes so that many distinct combinations collapse to only |
| 776 | a handful of distinct hash values. */ |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 777 | h = entry->hash; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 778 | hash ^= (h ^ (h << 16) ^ 89869747U) * 3644798167U; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | } |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 780 | hash = hash * 69069U + 907133923U; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | if (hash == -1) |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 782 | hash = 590923713U; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | so->hash = hash; |
| 784 | return hash; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 787 | /***** Set iterator type ***********************************************/ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 788 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 789 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 790 | PyObject_HEAD |
| 791 | PySetObject *si_set; /* Set to NULL when iterator is exhausted */ |
| 792 | Py_ssize_t si_used; |
| 793 | Py_ssize_t si_pos; |
| 794 | Py_ssize_t len; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 795 | } setiterobject; |
| 796 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 797 | static void |
| 798 | setiter_dealloc(setiterobject *si) |
| 799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | Py_XDECREF(si->si_set); |
| 801 | PyObject_GC_Del(si); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | static int |
| 805 | setiter_traverse(setiterobject *si, visitproc visit, void *arg) |
| 806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | Py_VISIT(si->si_set); |
| 808 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 811 | static PyObject * |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 812 | setiter_len(setiterobject *si) |
| 813 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | Py_ssize_t len = 0; |
| 815 | if (si->si_set != NULL && si->si_used == si->si_set->used) |
| 816 | len = si->len; |
Antoine Pitrou | 671b4d9 | 2010-08-17 17:55:07 +0000 | [diff] [blame] | 817 | return PyLong_FromSsize_t(len); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 820 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 821 | |
| 822 | static PyMethodDef setiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc}, |
| 824 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 825 | }; |
| 826 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 827 | static PyObject *setiter_iternext(setiterobject *si) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 828 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | PyObject *key; |
| 830 | register Py_ssize_t i, mask; |
| 831 | register setentry *entry; |
| 832 | PySetObject *so = si->si_set; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | if (so == NULL) |
| 835 | return NULL; |
| 836 | assert (PyAnySet_Check(so)); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | if (si->si_used != so->used) { |
| 839 | PyErr_SetString(PyExc_RuntimeError, |
| 840 | "Set changed size during iteration"); |
| 841 | si->si_used = -1; /* Make this state sticky */ |
| 842 | return NULL; |
| 843 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | i = si->si_pos; |
| 846 | assert(i>=0); |
| 847 | entry = so->table; |
| 848 | mask = so->mask; |
| 849 | while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) |
| 850 | i++; |
| 851 | si->si_pos = i+1; |
| 852 | if (i > mask) |
| 853 | goto fail; |
| 854 | si->len--; |
| 855 | key = entry[i].key; |
| 856 | Py_INCREF(key); |
| 857 | return key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 858 | |
| 859 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | Py_DECREF(so); |
| 861 | si->si_set = NULL; |
| 862 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 865 | PyTypeObject PySetIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 867 | "set_iterator", /* tp_name */ |
| 868 | sizeof(setiterobject), /* tp_basicsize */ |
| 869 | 0, /* tp_itemsize */ |
| 870 | /* methods */ |
| 871 | (destructor)setiter_dealloc, /* tp_dealloc */ |
| 872 | 0, /* tp_print */ |
| 873 | 0, /* tp_getattr */ |
| 874 | 0, /* tp_setattr */ |
| 875 | 0, /* tp_reserved */ |
| 876 | 0, /* tp_repr */ |
| 877 | 0, /* tp_as_number */ |
| 878 | 0, /* tp_as_sequence */ |
| 879 | 0, /* tp_as_mapping */ |
| 880 | 0, /* tp_hash */ |
| 881 | 0, /* tp_call */ |
| 882 | 0, /* tp_str */ |
| 883 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 884 | 0, /* tp_setattro */ |
| 885 | 0, /* tp_as_buffer */ |
| 886 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 887 | 0, /* tp_doc */ |
| 888 | (traverseproc)setiter_traverse, /* tp_traverse */ |
| 889 | 0, /* tp_clear */ |
| 890 | 0, /* tp_richcompare */ |
| 891 | 0, /* tp_weaklistoffset */ |
| 892 | PyObject_SelfIter, /* tp_iter */ |
| 893 | (iternextfunc)setiter_iternext, /* tp_iternext */ |
| 894 | setiter_methods, /* tp_methods */ |
| 895 | 0, |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 896 | }; |
| 897 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 898 | static PyObject * |
| 899 | set_iter(PySetObject *so) |
| 900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type); |
| 902 | if (si == NULL) |
| 903 | return NULL; |
| 904 | Py_INCREF(so); |
| 905 | si->si_set = so; |
| 906 | si->si_used = so->used; |
| 907 | si->si_pos = 0; |
| 908 | si->len = so->used; |
| 909 | _PyObject_GC_TRACK(si); |
| 910 | return (PyObject *)si; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 913 | static int |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 914 | set_update_internal(PySetObject *so, PyObject *other) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | PyObject *key, *it; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | if (PyAnySet_Check(other)) |
| 919 | return set_merge(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | if (PyDict_CheckExact(other)) { |
| 922 | PyObject *value; |
| 923 | Py_ssize_t pos = 0; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 924 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | Py_ssize_t dictsize = PyDict_Size(other); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | /* Do one big resize at the start, rather than |
| 928 | * incrementally resizing as we insert new keys. Expect |
| 929 | * that there will be no (or few) overlapping keys. |
| 930 | */ |
| 931 | if (dictsize == -1) |
| 932 | return -1; |
| 933 | if ((so->fill + dictsize)*3 >= (so->mask+1)*2) { |
| 934 | if (set_table_resize(so, (so->used + dictsize)*2) != 0) |
| 935 | return -1; |
| 936 | } |
| 937 | while (_PyDict_Next(other, &pos, &key, &value, &hash)) { |
| 938 | setentry an_entry; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | an_entry.hash = hash; |
| 941 | an_entry.key = key; |
| 942 | if (set_add_entry(so, &an_entry) == -1) |
| 943 | return -1; |
| 944 | } |
| 945 | return 0; |
| 946 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | it = PyObject_GetIter(other); |
| 949 | if (it == NULL) |
| 950 | return -1; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | while ((key = PyIter_Next(it)) != NULL) { |
| 953 | if (set_add_key(so, key) == -1) { |
| 954 | Py_DECREF(it); |
| 955 | Py_DECREF(key); |
| 956 | return -1; |
| 957 | } |
| 958 | Py_DECREF(key); |
| 959 | } |
| 960 | Py_DECREF(it); |
| 961 | if (PyErr_Occurred()) |
| 962 | return -1; |
| 963 | return 0; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 967 | set_update(PySetObject *so, PyObject *args) |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | Py_ssize_t i; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 972 | PyObject *other = PyTuple_GET_ITEM(args, i); |
| 973 | if (set_update_internal(so, other) == -1) |
| 974 | return NULL; |
| 975 | } |
| 976 | Py_RETURN_NONE; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | PyDoc_STRVAR(update_doc, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 980 | "Update a set with the union of itself and others."); |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 981 | |
| 982 | static PyObject * |
| 983 | make_new_set(PyTypeObject *type, PyObject *iterable) |
| 984 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | register PySetObject *so = NULL; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | if (dummy == NULL) { /* Auto-initialize dummy */ |
| 988 | dummy = PyUnicode_FromString("<dummy key>"); |
| 989 | if (dummy == NULL) |
| 990 | return NULL; |
| 991 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | /* create PySetObject structure */ |
| 994 | if (numfree && |
| 995 | (type == &PySet_Type || type == &PyFrozenSet_Type)) { |
| 996 | so = free_list[--numfree]; |
| 997 | assert (so != NULL && PyAnySet_CheckExact(so)); |
| 998 | Py_TYPE(so) = type; |
| 999 | _Py_NewReference((PyObject *)so); |
| 1000 | EMPTY_TO_MINSIZE(so); |
| 1001 | PyObject_GC_Track(so); |
| 1002 | } else { |
| 1003 | so = (PySetObject *)type->tp_alloc(type, 0); |
| 1004 | if (so == NULL) |
| 1005 | return NULL; |
| 1006 | /* tp_alloc has already zeroed the structure */ |
| 1007 | assert(so->table == NULL && so->fill == 0 && so->used == 0); |
| 1008 | INIT_NONZERO_SET_SLOTS(so); |
| 1009 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | so->lookup = set_lookkey_unicode; |
| 1012 | so->weakreflist = NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | if (iterable != NULL) { |
| 1015 | if (set_update_internal(so, iterable) == -1) { |
| 1016 | Py_DECREF(so); |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Raymond Hettinger | 7d99f09 | 2008-11-16 11:44:54 +0000 | [diff] [blame] | 1024 | static PyObject * |
| 1025 | make_new_set_basetype(PyTypeObject *type, PyObject *iterable) |
| 1026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | if (type != &PySet_Type && type != &PyFrozenSet_Type) { |
| 1028 | if (PyType_IsSubtype(type, &PySet_Type)) |
| 1029 | type = &PySet_Type; |
| 1030 | else |
| 1031 | type = &PyFrozenSet_Type; |
| 1032 | } |
| 1033 | return make_new_set(type, iterable); |
Raymond Hettinger | 7d99f09 | 2008-11-16 11:44:54 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1036 | /* The empty frozenset is a singleton */ |
| 1037 | static PyObject *emptyfrozenset = NULL; |
| 1038 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1039 | static PyObject * |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1040 | frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1041 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | PyObject *iterable = NULL, *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds)) |
| 1045 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) |
| 1048 | return NULL; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | if (type != &PyFrozenSet_Type) |
| 1051 | return make_new_set(type, iterable); |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | if (iterable != NULL) { |
| 1054 | /* frozenset(f) is idempotent */ |
| 1055 | if (PyFrozenSet_CheckExact(iterable)) { |
| 1056 | Py_INCREF(iterable); |
| 1057 | return iterable; |
| 1058 | } |
| 1059 | result = make_new_set(type, iterable); |
| 1060 | if (result == NULL || PySet_GET_SIZE(result)) |
| 1061 | return result; |
| 1062 | Py_DECREF(result); |
| 1063 | } |
| 1064 | /* The empty frozenset is a singleton */ |
| 1065 | if (emptyfrozenset == NULL) |
| 1066 | emptyfrozenset = make_new_set(type, NULL); |
| 1067 | Py_XINCREF(emptyfrozenset); |
| 1068 | return emptyfrozenset; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Antoine Pitrou | 093ce9c | 2011-12-16 11:24:27 +0100 | [diff] [blame] | 1071 | int |
| 1072 | PySet_ClearFreeList(void) |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1073 | { |
Antoine Pitrou | 093ce9c | 2011-12-16 11:24:27 +0100 | [diff] [blame] | 1074 | int freelist_size = numfree; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | PySetObject *so; |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | while (numfree) { |
| 1078 | numfree--; |
| 1079 | so = free_list[numfree]; |
| 1080 | PyObject_GC_Del(so); |
| 1081 | } |
Antoine Pitrou | 093ce9c | 2011-12-16 11:24:27 +0100 | [diff] [blame] | 1082 | return freelist_size; |
| 1083 | } |
| 1084 | |
| 1085 | void |
| 1086 | PySet_Fini(void) |
| 1087 | { |
| 1088 | PySet_ClearFreeList(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | Py_CLEAR(dummy); |
| 1090 | Py_CLEAR(emptyfrozenset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1093 | static PyObject * |
| 1094 | set_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1095 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds)) |
| 1097 | return NULL; |
| 1098 | |
| 1099 | return make_new_set(type, NULL); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame] | 1102 | /* set_swap_bodies() switches the contents of any two sets by moving their |
| 1103 | internal data pointers and, if needed, copying the internal smalltables. |
| 1104 | Semantically equivalent to: |
| 1105 | |
| 1106 | t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t |
| 1107 | |
| 1108 | The function always succeeds and it leaves both objects in a stable state. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | Useful for creating temporary frozensets from sets for membership testing |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame] | 1110 | in __contains__(), discard(), and remove(). Also useful for operations |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | that update in-place (by allowing an intermediate result to be swapped |
Raymond Hettinger | 9dcb17c | 2005-07-31 13:09:28 +0000 | [diff] [blame] | 1112 | into one of the original inputs). |
Raymond Hettinger | 934d63e | 2005-07-31 01:33:10 +0000 | [diff] [blame] | 1113 | */ |
| 1114 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1115 | static void |
| 1116 | set_swap_bodies(PySetObject *a, PySetObject *b) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | Py_ssize_t t; |
| 1119 | setentry *u; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1120 | setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | setentry tab[PySet_MINSIZE]; |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 1122 | Py_hash_t h; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | t = a->fill; a->fill = b->fill; b->fill = t; |
| 1125 | t = a->used; a->used = b->used; b->used = t; |
| 1126 | t = a->mask; a->mask = b->mask; b->mask = t; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | u = a->table; |
| 1129 | if (a->table == a->smalltable) |
| 1130 | u = b->smalltable; |
| 1131 | a->table = b->table; |
| 1132 | if (b->table == b->smalltable) |
| 1133 | a->table = a->smalltable; |
| 1134 | b->table = u; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | f = a->lookup; a->lookup = b->lookup; b->lookup = f; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | if (a->table == a->smalltable || b->table == b->smalltable) { |
| 1139 | memcpy(tab, a->smalltable, sizeof(tab)); |
| 1140 | memcpy(a->smalltable, b->smalltable, sizeof(tab)); |
| 1141 | memcpy(b->smalltable, tab, sizeof(tab)); |
| 1142 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) && |
| 1145 | PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) { |
| 1146 | h = a->hash; a->hash = b->hash; b->hash = h; |
| 1147 | } else { |
| 1148 | a->hash = -1; |
| 1149 | b->hash = -1; |
| 1150 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1153 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1154 | set_copy(PySetObject *so) |
| 1155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | return make_new_set_basetype(Py_TYPE(so), (PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1159 | static PyObject * |
| 1160 | frozenset_copy(PySetObject *so) |
| 1161 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | if (PyFrozenSet_CheckExact(so)) { |
| 1163 | Py_INCREF(so); |
| 1164 | return (PyObject *)so; |
| 1165 | } |
| 1166 | return set_copy(so); |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1169 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set."); |
| 1170 | |
| 1171 | static PyObject * |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1172 | set_clear(PySetObject *so) |
| 1173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | set_clear_internal(so); |
| 1175 | Py_RETURN_NONE; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | PyDoc_STRVAR(clear_doc, "Remove all elements from this set."); |
| 1179 | |
| 1180 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1181 | set_union(PySetObject *so, PyObject *args) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | PySetObject *result; |
| 1184 | PyObject *other; |
| 1185 | Py_ssize_t i; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | result = (PySetObject *)set_copy(so); |
| 1188 | if (result == NULL) |
| 1189 | return NULL; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1192 | other = PyTuple_GET_ITEM(args, i); |
| 1193 | if ((PyObject *)so == other) |
| 1194 | continue; |
| 1195 | if (set_update_internal(result, other) == -1) { |
| 1196 | Py_DECREF(result); |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | } |
| 1200 | return (PyObject *)result; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | PyDoc_STRVAR(union_doc, |
| 1204 | "Return the union of sets as a new set.\n\ |
| 1205 | \n\ |
| 1206 | (i.e. all elements that are in either set.)"); |
| 1207 | |
| 1208 | static PyObject * |
| 1209 | set_or(PySetObject *so, PyObject *other) |
| 1210 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | PySetObject *result; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1212 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1213 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) |
| 1214 | Py_RETURN_NOTIMPLEMENTED; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | result = (PySetObject *)set_copy(so); |
| 1217 | if (result == NULL) |
| 1218 | return NULL; |
| 1219 | if ((PyObject *)so == other) |
| 1220 | return (PyObject *)result; |
| 1221 | if (set_update_internal(result, other) == -1) { |
| 1222 | Py_DECREF(result); |
| 1223 | return NULL; |
| 1224 | } |
| 1225 | return (PyObject *)result; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1228 | static PyObject * |
| 1229 | set_ior(PySetObject *so, PyObject *other) |
| 1230 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1231 | if (!PyAnySet_Check(other)) |
| 1232 | Py_RETURN_NOTIMPLEMENTED; |
| 1233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | if (set_update_internal(so, other) == -1) |
| 1235 | return NULL; |
| 1236 | Py_INCREF(so); |
| 1237 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | static PyObject * |
| 1241 | set_intersection(PySetObject *so, PyObject *other) |
| 1242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | PySetObject *result; |
| 1244 | PyObject *key, *it, *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | if ((PyObject *)so == other) |
| 1247 | return set_copy(so); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL); |
| 1250 | if (result == NULL) |
| 1251 | return NULL; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | if (PyAnySet_Check(other)) { |
| 1254 | Py_ssize_t pos = 0; |
| 1255 | setentry *entry; |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) { |
| 1258 | tmp = (PyObject *)so; |
| 1259 | so = (PySetObject *)other; |
| 1260 | other = tmp; |
| 1261 | } |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1263 | while (set_next((PySetObject *)other, &pos, &entry)) { |
| 1264 | int rv = set_contains_entry(so, entry); |
| 1265 | if (rv == -1) { |
| 1266 | Py_DECREF(result); |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | if (rv) { |
| 1270 | if (set_add_entry(result, entry) == -1) { |
| 1271 | Py_DECREF(result); |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | return (PyObject *)result; |
| 1277 | } |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | it = PyObject_GetIter(other); |
| 1280 | if (it == NULL) { |
| 1281 | Py_DECREF(result); |
| 1282 | return NULL; |
| 1283 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | while ((key = PyIter_Next(it)) != NULL) { |
| 1286 | int rv; |
| 1287 | setentry entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1288 | Py_hash_t hash = PyObject_Hash(key); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | if (hash == -1) { |
| 1291 | Py_DECREF(it); |
| 1292 | Py_DECREF(result); |
| 1293 | Py_DECREF(key); |
| 1294 | return NULL; |
| 1295 | } |
| 1296 | entry.hash = hash; |
| 1297 | entry.key = key; |
| 1298 | rv = set_contains_entry(so, &entry); |
| 1299 | if (rv == -1) { |
| 1300 | Py_DECREF(it); |
| 1301 | Py_DECREF(result); |
| 1302 | Py_DECREF(key); |
| 1303 | return NULL; |
| 1304 | } |
| 1305 | if (rv) { |
| 1306 | if (set_add_entry(result, &entry) == -1) { |
| 1307 | Py_DECREF(it); |
| 1308 | Py_DECREF(result); |
| 1309 | Py_DECREF(key); |
| 1310 | return NULL; |
| 1311 | } |
| 1312 | } |
| 1313 | Py_DECREF(key); |
| 1314 | } |
| 1315 | Py_DECREF(it); |
| 1316 | if (PyErr_Occurred()) { |
| 1317 | Py_DECREF(result); |
| 1318 | return NULL; |
| 1319 | } |
| 1320 | return (PyObject *)result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1323 | static PyObject * |
| 1324 | set_intersection_multi(PySetObject *so, PyObject *args) |
| 1325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | Py_ssize_t i; |
| 1327 | PyObject *result = (PyObject *)so; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | if (PyTuple_GET_SIZE(args) == 0) |
| 1330 | return set_copy(so); |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | Py_INCREF(so); |
| 1333 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1334 | PyObject *other = PyTuple_GET_ITEM(args, i); |
| 1335 | PyObject *newresult = set_intersection((PySetObject *)result, other); |
| 1336 | if (newresult == NULL) { |
| 1337 | Py_DECREF(result); |
| 1338 | return NULL; |
| 1339 | } |
| 1340 | Py_DECREF(result); |
| 1341 | result = newresult; |
| 1342 | } |
| 1343 | return result; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1346 | PyDoc_STRVAR(intersection_doc, |
| 1347 | "Return the intersection of two sets as a new set.\n\ |
| 1348 | \n\ |
| 1349 | (i.e. all elements that are in both sets.)"); |
| 1350 | |
| 1351 | static PyObject * |
| 1352 | set_intersection_update(PySetObject *so, PyObject *other) |
| 1353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1354 | PyObject *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | tmp = set_intersection(so, other); |
| 1357 | if (tmp == NULL) |
| 1358 | return NULL; |
| 1359 | set_swap_bodies(so, (PySetObject *)tmp); |
| 1360 | Py_DECREF(tmp); |
| 1361 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1364 | static PyObject * |
| 1365 | set_intersection_update_multi(PySetObject *so, PyObject *args) |
| 1366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | PyObject *tmp; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | tmp = set_intersection_multi(so, args); |
| 1370 | if (tmp == NULL) |
| 1371 | return NULL; |
| 1372 | set_swap_bodies(so, (PySetObject *)tmp); |
| 1373 | Py_DECREF(tmp); |
| 1374 | Py_RETURN_NONE; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1377 | PyDoc_STRVAR(intersection_update_doc, |
| 1378 | "Update a set with the intersection of itself and another."); |
| 1379 | |
| 1380 | static PyObject * |
| 1381 | set_and(PySetObject *so, PyObject *other) |
| 1382 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1383 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) |
| 1384 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | return set_intersection(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | static PyObject * |
| 1389 | set_iand(PySetObject *so, PyObject *other) |
| 1390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | PyObject *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1392 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1393 | if (!PyAnySet_Check(other)) |
| 1394 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | result = set_intersection_update(so, other); |
| 1396 | if (result == NULL) |
| 1397 | return NULL; |
| 1398 | Py_DECREF(result); |
| 1399 | Py_INCREF(so); |
| 1400 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1403 | static PyObject * |
| 1404 | set_isdisjoint(PySetObject *so, PyObject *other) |
| 1405 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1406 | PyObject *key, *it, *tmp; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | if ((PyObject *)so == other) { |
| 1409 | if (PySet_GET_SIZE(so) == 0) |
| 1410 | Py_RETURN_TRUE; |
| 1411 | else |
| 1412 | Py_RETURN_FALSE; |
| 1413 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | if (PyAnySet_CheckExact(other)) { |
| 1416 | Py_ssize_t pos = 0; |
| 1417 | setentry *entry; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) { |
| 1420 | tmp = (PyObject *)so; |
| 1421 | so = (PySetObject *)other; |
| 1422 | other = tmp; |
| 1423 | } |
| 1424 | while (set_next((PySetObject *)other, &pos, &entry)) { |
| 1425 | int rv = set_contains_entry(so, entry); |
| 1426 | if (rv == -1) |
| 1427 | return NULL; |
| 1428 | if (rv) |
| 1429 | Py_RETURN_FALSE; |
| 1430 | } |
| 1431 | Py_RETURN_TRUE; |
| 1432 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1434 | it = PyObject_GetIter(other); |
| 1435 | if (it == NULL) |
| 1436 | return NULL; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | while ((key = PyIter_Next(it)) != NULL) { |
| 1439 | int rv; |
| 1440 | setentry entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1441 | Py_hash_t hash = PyObject_Hash(key); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1443 | if (hash == -1) { |
| 1444 | Py_DECREF(key); |
| 1445 | Py_DECREF(it); |
| 1446 | return NULL; |
| 1447 | } |
| 1448 | entry.hash = hash; |
| 1449 | entry.key = key; |
| 1450 | rv = set_contains_entry(so, &entry); |
| 1451 | Py_DECREF(key); |
| 1452 | if (rv == -1) { |
| 1453 | Py_DECREF(it); |
| 1454 | return NULL; |
| 1455 | } |
| 1456 | if (rv) { |
| 1457 | Py_DECREF(it); |
| 1458 | Py_RETURN_FALSE; |
| 1459 | } |
| 1460 | } |
| 1461 | Py_DECREF(it); |
| 1462 | if (PyErr_Occurred()) |
| 1463 | return NULL; |
| 1464 | Py_RETURN_TRUE; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | PyDoc_STRVAR(isdisjoint_doc, |
| 1468 | "Return True if two sets have a null intersection."); |
| 1469 | |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 1470 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1471 | set_difference_update_internal(PySetObject *so, PyObject *other) |
| 1472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | if ((PyObject *)so == other) |
| 1474 | return set_clear_internal(so); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | if (PyAnySet_Check(other)) { |
| 1477 | setentry *entry; |
| 1478 | Py_ssize_t pos = 0; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | while (set_next((PySetObject *)other, &pos, &entry)) |
| 1481 | if (set_discard_entry(so, entry) == -1) |
| 1482 | return -1; |
| 1483 | } else { |
| 1484 | PyObject *key, *it; |
| 1485 | it = PyObject_GetIter(other); |
| 1486 | if (it == NULL) |
| 1487 | return -1; |
| 1488 | |
| 1489 | while ((key = PyIter_Next(it)) != NULL) { |
| 1490 | if (set_discard_key(so, key) == -1) { |
| 1491 | Py_DECREF(it); |
| 1492 | Py_DECREF(key); |
| 1493 | return -1; |
| 1494 | } |
| 1495 | Py_DECREF(key); |
| 1496 | } |
| 1497 | Py_DECREF(it); |
| 1498 | if (PyErr_Occurred()) |
| 1499 | return -1; |
| 1500 | } |
| 1501 | /* If more than 1/5 are dummies, then resize them away. */ |
| 1502 | if ((so->fill - so->used) * 5 < so->mask) |
| 1503 | return 0; |
| 1504 | return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1507 | static PyObject * |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1508 | set_difference_update(PySetObject *so, PyObject *args) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | Py_ssize_t i; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1513 | PyObject *other = PyTuple_GET_ITEM(args, i); |
| 1514 | if (set_difference_update_internal(so, other) == -1) |
| 1515 | return NULL; |
| 1516 | } |
| 1517 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | PyDoc_STRVAR(difference_update_doc, |
| 1521 | "Remove all elements of another set from this set."); |
| 1522 | |
| 1523 | static PyObject * |
Antoine Pitrou | 715f3cd | 2010-11-30 22:23:20 +0000 | [diff] [blame] | 1524 | set_copy_and_difference(PySetObject *so, PyObject *other) |
| 1525 | { |
| 1526 | PyObject *result; |
| 1527 | |
| 1528 | result = set_copy(so); |
| 1529 | if (result == NULL) |
| 1530 | return NULL; |
| 1531 | if (set_difference_update_internal((PySetObject *) result, other) != -1) |
| 1532 | return result; |
| 1533 | Py_DECREF(result); |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | |
| 1537 | static PyObject * |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1538 | set_difference(PySetObject *so, PyObject *other) |
| 1539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | PyObject *result; |
| 1541 | setentry *entry; |
| 1542 | Py_ssize_t pos = 0; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) { |
Antoine Pitrou | 715f3cd | 2010-11-30 22:23:20 +0000 | [diff] [blame] | 1545 | return set_copy_and_difference(so, other); |
| 1546 | } |
| 1547 | |
| 1548 | /* If len(so) much more than len(other), it's more efficient to simply copy |
| 1549 | * so and then iterate other looking for common elements. */ |
| 1550 | if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) { |
| 1551 | return set_copy_and_difference(so, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | } |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1554 | result = make_new_set_basetype(Py_TYPE(so), NULL); |
| 1555 | if (result == NULL) |
| 1556 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | if (PyDict_CheckExact(other)) { |
| 1559 | while (set_next(so, &pos, &entry)) { |
| 1560 | setentry entrycopy; |
| 1561 | entrycopy.hash = entry->hash; |
| 1562 | entrycopy.key = entry->key; |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 1563 | if (!_PyDict_Contains(other, entry->key, entry->hash)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | if (set_add_entry((PySetObject *)result, &entrycopy) == -1) { |
| 1565 | Py_DECREF(result); |
| 1566 | return NULL; |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | return result; |
| 1571 | } |
| 1572 | |
Antoine Pitrou | 715f3cd | 2010-11-30 22:23:20 +0000 | [diff] [blame] | 1573 | /* Iterate over so, checking for common elements in other. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | while (set_next(so, &pos, &entry)) { |
| 1575 | int rv = set_contains_entry((PySetObject *)other, entry); |
| 1576 | if (rv == -1) { |
| 1577 | Py_DECREF(result); |
| 1578 | return NULL; |
| 1579 | } |
| 1580 | if (!rv) { |
| 1581 | if (set_add_entry((PySetObject *)result, entry) == -1) { |
| 1582 | Py_DECREF(result); |
| 1583 | return NULL; |
| 1584 | } |
| 1585 | } |
| 1586 | } |
| 1587 | return result; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1590 | static PyObject * |
| 1591 | set_difference_multi(PySetObject *so, PyObject *args) |
| 1592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | Py_ssize_t i; |
| 1594 | PyObject *result, *other; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1596 | if (PyTuple_GET_SIZE(args) == 0) |
| 1597 | return set_copy(so); |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | other = PyTuple_GET_ITEM(args, 0); |
| 1600 | result = set_difference(so, other); |
| 1601 | if (result == NULL) |
| 1602 | return NULL; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1605 | other = PyTuple_GET_ITEM(args, i); |
| 1606 | if (set_difference_update_internal((PySetObject *)result, other) == -1) { |
| 1607 | Py_DECREF(result); |
| 1608 | return NULL; |
| 1609 | } |
| 1610 | } |
| 1611 | return result; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1614 | PyDoc_STRVAR(difference_doc, |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1615 | "Return the difference of two or more sets as a new set.\n\ |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1616 | \n\ |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1617 | (i.e. all elements that are in this set but not the others.)"); |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1618 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1619 | set_sub(PySetObject *so, PyObject *other) |
| 1620 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1621 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) |
| 1622 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | return set_difference(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | static PyObject * |
| 1627 | set_isub(PySetObject *so, PyObject *other) |
| 1628 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1629 | if (!PyAnySet_Check(other)) |
| 1630 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | if (set_difference_update_internal(so, other) == -1) |
| 1632 | return NULL; |
| 1633 | Py_INCREF(so); |
| 1634 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1638 | set_symmetric_difference_update(PySetObject *so, PyObject *other) |
| 1639 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | PySetObject *otherset; |
| 1641 | PyObject *key; |
| 1642 | Py_ssize_t pos = 0; |
| 1643 | setentry *entry; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | if ((PyObject *)so == other) |
| 1646 | return set_clear(so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1648 | if (PyDict_CheckExact(other)) { |
| 1649 | PyObject *value; |
| 1650 | int rv; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1651 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | while (_PyDict_Next(other, &pos, &key, &value, &hash)) { |
| 1653 | setentry an_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1654 | |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1655 | Py_INCREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | an_entry.hash = hash; |
| 1657 | an_entry.key = key; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | rv = set_discard_entry(so, &an_entry); |
Georg Brandl | 2d44449 | 2010-09-03 10:52:55 +0000 | [diff] [blame] | 1660 | if (rv == -1) { |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1661 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | } |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1664 | if (rv == DISCARD_NOTFOUND) { |
| 1665 | if (set_add_entry(so, &an_entry) == -1) { |
| 1666 | Py_DECREF(key); |
| 1667 | return NULL; |
| 1668 | } |
| 1669 | } |
Georg Brandl | 2d44449 | 2010-09-03 10:52:55 +0000 | [diff] [blame] | 1670 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | } |
| 1672 | Py_RETURN_NONE; |
| 1673 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | if (PyAnySet_Check(other)) { |
| 1676 | Py_INCREF(other); |
| 1677 | otherset = (PySetObject *)other; |
| 1678 | } else { |
| 1679 | otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other); |
| 1680 | if (otherset == NULL) |
| 1681 | return NULL; |
| 1682 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | while (set_next(otherset, &pos, &entry)) { |
| 1685 | int rv = set_discard_entry(so, entry); |
| 1686 | if (rv == -1) { |
| 1687 | Py_DECREF(otherset); |
| 1688 | return NULL; |
| 1689 | } |
| 1690 | if (rv == DISCARD_NOTFOUND) { |
| 1691 | if (set_add_entry(so, entry) == -1) { |
| 1692 | Py_DECREF(otherset); |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | Py_DECREF(otherset); |
| 1698 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | PyDoc_STRVAR(symmetric_difference_update_doc, |
| 1702 | "Update a set with the symmetric difference of itself and another."); |
| 1703 | |
| 1704 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1705 | set_symmetric_difference(PySetObject *so, PyObject *other) |
| 1706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 | PyObject *rv; |
| 1708 | PySetObject *otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other); |
| 1711 | if (otherset == NULL) |
| 1712 | return NULL; |
| 1713 | rv = set_symmetric_difference_update(otherset, (PyObject *)so); |
| 1714 | if (rv == NULL) |
| 1715 | return NULL; |
| 1716 | Py_DECREF(rv); |
| 1717 | return (PyObject *)otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | PyDoc_STRVAR(symmetric_difference_doc, |
| 1721 | "Return the symmetric difference of two sets as a new set.\n\ |
| 1722 | \n\ |
| 1723 | (i.e. all elements that are in exactly one of the sets.)"); |
| 1724 | |
| 1725 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1726 | set_xor(PySetObject *so, PyObject *other) |
| 1727 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1728 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) |
| 1729 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | return set_symmetric_difference(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | static PyObject * |
| 1734 | set_ixor(PySetObject *so, PyObject *other) |
| 1735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | PyObject *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1737 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1738 | if (!PyAnySet_Check(other)) |
| 1739 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | result = set_symmetric_difference_update(so, other); |
| 1741 | if (result == NULL) |
| 1742 | return NULL; |
| 1743 | Py_DECREF(result); |
| 1744 | Py_INCREF(so); |
| 1745 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | static PyObject * |
| 1749 | set_issubset(PySetObject *so, PyObject *other) |
| 1750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | setentry *entry; |
| 1752 | Py_ssize_t pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | if (!PyAnySet_Check(other)) { |
| 1755 | PyObject *tmp, *result; |
| 1756 | tmp = make_new_set(&PySet_Type, other); |
| 1757 | if (tmp == NULL) |
| 1758 | return NULL; |
| 1759 | result = set_issubset(so, tmp); |
| 1760 | Py_DECREF(tmp); |
| 1761 | return result; |
| 1762 | } |
| 1763 | if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other)) |
| 1764 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | while (set_next(so, &pos, &entry)) { |
| 1767 | int rv = set_contains_entry((PySetObject *)other, entry); |
| 1768 | if (rv == -1) |
| 1769 | return NULL; |
| 1770 | if (!rv) |
| 1771 | Py_RETURN_FALSE; |
| 1772 | } |
| 1773 | Py_RETURN_TRUE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set."); |
| 1777 | |
| 1778 | static PyObject * |
| 1779 | set_issuperset(PySetObject *so, PyObject *other) |
| 1780 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | PyObject *tmp, *result; |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | if (!PyAnySet_Check(other)) { |
| 1784 | tmp = make_new_set(&PySet_Type, other); |
| 1785 | if (tmp == NULL) |
| 1786 | return NULL; |
| 1787 | result = set_issuperset(so, tmp); |
| 1788 | Py_DECREF(tmp); |
| 1789 | return result; |
| 1790 | } |
| 1791 | return set_issubset((PySetObject *)other, (PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set."); |
| 1795 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1796 | static PyObject * |
| 1797 | set_richcompare(PySetObject *v, PyObject *w, int op) |
| 1798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | PyObject *r1, *r2; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1800 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1801 | if(!PyAnySet_Check(w)) |
| 1802 | Py_RETURN_NOTIMPLEMENTED; |
| 1803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1804 | switch (op) { |
| 1805 | case Py_EQ: |
| 1806 | if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w)) |
| 1807 | Py_RETURN_FALSE; |
| 1808 | if (v->hash != -1 && |
| 1809 | ((PySetObject *)w)->hash != -1 && |
| 1810 | v->hash != ((PySetObject *)w)->hash) |
| 1811 | Py_RETURN_FALSE; |
| 1812 | return set_issubset(v, w); |
| 1813 | case Py_NE: |
| 1814 | r1 = set_richcompare(v, w, Py_EQ); |
| 1815 | if (r1 == NULL) |
| 1816 | return NULL; |
| 1817 | r2 = PyBool_FromLong(PyObject_Not(r1)); |
| 1818 | Py_DECREF(r1); |
| 1819 | return r2; |
| 1820 | case Py_LE: |
| 1821 | return set_issubset(v, w); |
| 1822 | case Py_GE: |
| 1823 | return set_issuperset(v, w); |
| 1824 | case Py_LT: |
| 1825 | if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w)) |
| 1826 | Py_RETURN_FALSE; |
| 1827 | return set_issubset(v, w); |
| 1828 | case Py_GT: |
| 1829 | if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w)) |
| 1830 | Py_RETURN_FALSE; |
| 1831 | return set_issuperset(v, w); |
| 1832 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1833 | Py_RETURN_NOTIMPLEMENTED; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1836 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1837 | set_add(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1838 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | if (set_add_key(so, key) == -1) |
| 1840 | return NULL; |
| 1841 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | PyDoc_STRVAR(add_doc, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1845 | "Add an element to a set.\n\ |
| 1846 | \n\ |
| 1847 | This has no effect if the element is already present."); |
| 1848 | |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1849 | static int |
| 1850 | set_contains(PySetObject *so, PyObject *key) |
| 1851 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | PyObject *tmpkey; |
| 1853 | int rv; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1855 | rv = set_contains_key(so, key); |
| 1856 | if (rv == -1) { |
| 1857 | if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1858 | return -1; |
| 1859 | PyErr_Clear(); |
Raymond Hettinger | 38bf2cc | 2010-08-06 09:52:17 +0000 | [diff] [blame] | 1860 | tmpkey = make_new_set(&PyFrozenSet_Type, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | if (tmpkey == NULL) |
| 1862 | return -1; |
Petri Lehtinen | 5acc27e | 2011-10-30 13:56:41 +0200 | [diff] [blame] | 1863 | rv = set_contains_key(so, tmpkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | Py_DECREF(tmpkey); |
| 1865 | } |
| 1866 | return rv; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | static PyObject * |
| 1870 | set_direct_contains(PySetObject *so, PyObject *key) |
| 1871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | long result; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1874 | result = set_contains(so, key); |
| 1875 | if (result == -1) |
| 1876 | return NULL; |
| 1877 | return PyBool_FromLong(result); |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x."); |
| 1881 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1882 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1883 | set_remove(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | PyObject *tmpkey; |
| 1886 | int rv; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | rv = set_discard_key(so, key); |
| 1889 | if (rv == -1) { |
| 1890 | if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1891 | return NULL; |
| 1892 | PyErr_Clear(); |
Raymond Hettinger | 38bf2cc | 2010-08-06 09:52:17 +0000 | [diff] [blame] | 1893 | tmpkey = make_new_set(&PyFrozenSet_Type, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | if (tmpkey == NULL) |
| 1895 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | rv = set_discard_key(so, tmpkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | Py_DECREF(tmpkey); |
| 1898 | if (rv == -1) |
| 1899 | return NULL; |
| 1900 | } |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 1901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | if (rv == DISCARD_NOTFOUND) { |
| 1903 | set_key_error(key); |
| 1904 | return NULL; |
| 1905 | } |
| 1906 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | PyDoc_STRVAR(remove_doc, |
| 1910 | "Remove an element from a set; it must be a member.\n\ |
| 1911 | \n\ |
| 1912 | If the element is not a member, raise a KeyError."); |
| 1913 | |
| 1914 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1915 | set_discard(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1916 | { |
Benjamin Peterson | 2b50a01 | 2011-10-30 14:24:44 -0400 | [diff] [blame] | 1917 | PyObject *tmpkey; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | int rv; |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | rv = set_discard_key(so, key); |
| 1921 | if (rv == -1) { |
| 1922 | if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1923 | return NULL; |
| 1924 | PyErr_Clear(); |
Raymond Hettinger | 38bf2cc | 2010-08-06 09:52:17 +0000 | [diff] [blame] | 1925 | tmpkey = make_new_set(&PyFrozenSet_Type, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | if (tmpkey == NULL) |
| 1927 | return NULL; |
Petri Lehtinen | e0aa803 | 2011-10-30 14:31:27 +0200 | [diff] [blame] | 1928 | rv = set_discard_key(so, tmpkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1929 | Py_DECREF(tmpkey); |
Petri Lehtinen | e0aa803 | 2011-10-30 14:31:27 +0200 | [diff] [blame] | 1930 | if (rv == -1) |
| 1931 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1932 | } |
| 1933 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | PyDoc_STRVAR(discard_doc, |
| 1937 | "Remove an element from a set if it is a member.\n\ |
| 1938 | \n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1939 | If the element is not a member, do nothing."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1940 | |
| 1941 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1942 | set_reduce(PySetObject *so) |
| 1943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1944 | PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1945 | _Py_IDENTIFIER(__dict__); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1947 | keys = PySequence_List((PyObject *)so); |
| 1948 | if (keys == NULL) |
| 1949 | goto done; |
| 1950 | args = PyTuple_Pack(1, keys); |
| 1951 | if (args == NULL) |
| 1952 | goto done; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1953 | dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | if (dict == NULL) { |
| 1955 | PyErr_Clear(); |
| 1956 | dict = Py_None; |
| 1957 | Py_INCREF(dict); |
| 1958 | } |
| 1959 | result = PyTuple_Pack(3, Py_TYPE(so), args, dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1960 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | Py_XDECREF(args); |
| 1962 | Py_XDECREF(keys); |
| 1963 | Py_XDECREF(dict); |
| 1964 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 1968 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1969 | static PyObject * |
| 1970 | set_sizeof(PySetObject *so) |
| 1971 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1972 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | res = sizeof(PySetObject); |
| 1975 | if (so->table != so->smalltable) |
| 1976 | res = res + (so->mask + 1) * sizeof(setentry); |
| 1977 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes"); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1981 | static int |
| 1982 | set_init(PySetObject *self, PyObject *args, PyObject *kwds) |
| 1983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | PyObject *iterable = NULL; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | if (!PyAnySet_Check(self)) |
| 1987 | return -1; |
| 1988 | if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds)) |
| 1989 | return -1; |
| 1990 | if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable)) |
| 1991 | return -1; |
| 1992 | set_clear_internal(self); |
| 1993 | self->hash = -1; |
| 1994 | if (iterable == NULL) |
| 1995 | return 0; |
| 1996 | return set_update_internal(self, iterable); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1999 | static PySequenceMethods set_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2000 | set_len, /* sq_length */ |
| 2001 | 0, /* sq_concat */ |
| 2002 | 0, /* sq_repeat */ |
| 2003 | 0, /* sq_item */ |
| 2004 | 0, /* sq_slice */ |
| 2005 | 0, /* sq_ass_item */ |
| 2006 | 0, /* sq_ass_slice */ |
| 2007 | (objobjproc)set_contains, /* sq_contains */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2008 | }; |
| 2009 | |
| 2010 | /* set object ********************************************************/ |
| 2011 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2012 | #ifdef Py_DEBUG |
| 2013 | static PyObject *test_c_api(PySetObject *so); |
| 2014 | |
| 2015 | PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\ |
| 2016 | All is well if assertions don't fail."); |
| 2017 | #endif |
| 2018 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2019 | static PyMethodDef set_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2020 | {"add", (PyCFunction)set_add, METH_O, |
| 2021 | add_doc}, |
| 2022 | {"clear", (PyCFunction)set_clear, METH_NOARGS, |
| 2023 | clear_doc}, |
| 2024 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
| 2025 | contains_doc}, |
| 2026 | {"copy", (PyCFunction)set_copy, METH_NOARGS, |
| 2027 | copy_doc}, |
| 2028 | {"discard", (PyCFunction)set_discard, METH_O, |
| 2029 | discard_doc}, |
| 2030 | {"difference", (PyCFunction)set_difference_multi, METH_VARARGS, |
| 2031 | difference_doc}, |
| 2032 | {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS, |
| 2033 | difference_update_doc}, |
| 2034 | {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS, |
| 2035 | intersection_doc}, |
| 2036 | {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS, |
| 2037 | intersection_update_doc}, |
| 2038 | {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O, |
| 2039 | isdisjoint_doc}, |
| 2040 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 2041 | issubset_doc}, |
| 2042 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 2043 | issuperset_doc}, |
| 2044 | {"pop", (PyCFunction)set_pop, METH_NOARGS, |
| 2045 | pop_doc}, |
| 2046 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 2047 | reduce_doc}, |
| 2048 | {"remove", (PyCFunction)set_remove, METH_O, |
| 2049 | remove_doc}, |
| 2050 | {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS, |
| 2051 | sizeof_doc}, |
| 2052 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 2053 | symmetric_difference_doc}, |
| 2054 | {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O, |
| 2055 | symmetric_difference_update_doc}, |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2056 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2057 | {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS, |
| 2058 | test_c_api_doc}, |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2059 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2060 | {"union", (PyCFunction)set_union, METH_VARARGS, |
| 2061 | union_doc}, |
| 2062 | {"update", (PyCFunction)set_update, METH_VARARGS, |
| 2063 | update_doc}, |
| 2064 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2065 | }; |
| 2066 | |
| 2067 | static PyNumberMethods set_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | 0, /*nb_add*/ |
| 2069 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 2070 | 0, /*nb_multiply*/ |
| 2071 | 0, /*nb_remainder*/ |
| 2072 | 0, /*nb_divmod*/ |
| 2073 | 0, /*nb_power*/ |
| 2074 | 0, /*nb_negative*/ |
| 2075 | 0, /*nb_positive*/ |
| 2076 | 0, /*nb_absolute*/ |
| 2077 | 0, /*nb_bool*/ |
| 2078 | 0, /*nb_invert*/ |
| 2079 | 0, /*nb_lshift*/ |
| 2080 | 0, /*nb_rshift*/ |
| 2081 | (binaryfunc)set_and, /*nb_and*/ |
| 2082 | (binaryfunc)set_xor, /*nb_xor*/ |
| 2083 | (binaryfunc)set_or, /*nb_or*/ |
| 2084 | 0, /*nb_int*/ |
| 2085 | 0, /*nb_reserved*/ |
| 2086 | 0, /*nb_float*/ |
| 2087 | 0, /*nb_inplace_add*/ |
| 2088 | (binaryfunc)set_isub, /*nb_inplace_subtract*/ |
| 2089 | 0, /*nb_inplace_multiply*/ |
| 2090 | 0, /*nb_inplace_remainder*/ |
| 2091 | 0, /*nb_inplace_power*/ |
| 2092 | 0, /*nb_inplace_lshift*/ |
| 2093 | 0, /*nb_inplace_rshift*/ |
| 2094 | (binaryfunc)set_iand, /*nb_inplace_and*/ |
| 2095 | (binaryfunc)set_ixor, /*nb_inplace_xor*/ |
| 2096 | (binaryfunc)set_ior, /*nb_inplace_or*/ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2097 | }; |
| 2098 | |
| 2099 | PyDoc_STRVAR(set_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2100 | "set() -> new empty set object\n\ |
| 2101 | set(iterable) -> new set object\n\ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2102 | \n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2103 | Build an unordered collection of unique elements."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2104 | |
| 2105 | PyTypeObject PySet_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2107 | "set", /* tp_name */ |
| 2108 | sizeof(PySetObject), /* tp_basicsize */ |
| 2109 | 0, /* tp_itemsize */ |
| 2110 | /* methods */ |
| 2111 | (destructor)set_dealloc, /* tp_dealloc */ |
| 2112 | 0, /* tp_print */ |
| 2113 | 0, /* tp_getattr */ |
| 2114 | 0, /* tp_setattr */ |
| 2115 | 0, /* tp_reserved */ |
| 2116 | (reprfunc)set_repr, /* tp_repr */ |
| 2117 | &set_as_number, /* tp_as_number */ |
| 2118 | &set_as_sequence, /* tp_as_sequence */ |
| 2119 | 0, /* tp_as_mapping */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2120 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | 0, /* tp_call */ |
| 2122 | 0, /* tp_str */ |
| 2123 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2124 | 0, /* tp_setattro */ |
| 2125 | 0, /* tp_as_buffer */ |
| 2126 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2127 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2128 | set_doc, /* tp_doc */ |
| 2129 | (traverseproc)set_traverse, /* tp_traverse */ |
| 2130 | (inquiry)set_clear_internal, /* tp_clear */ |
| 2131 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2132 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
| 2133 | (getiterfunc)set_iter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | 0, /* tp_iternext */ |
| 2135 | set_methods, /* tp_methods */ |
| 2136 | 0, /* tp_members */ |
| 2137 | 0, /* tp_getset */ |
| 2138 | 0, /* tp_base */ |
| 2139 | 0, /* tp_dict */ |
| 2140 | 0, /* tp_descr_get */ |
| 2141 | 0, /* tp_descr_set */ |
| 2142 | 0, /* tp_dictoffset */ |
| 2143 | (initproc)set_init, /* tp_init */ |
| 2144 | PyType_GenericAlloc, /* tp_alloc */ |
| 2145 | set_new, /* tp_new */ |
| 2146 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2147 | }; |
| 2148 | |
| 2149 | /* frozenset object ********************************************************/ |
| 2150 | |
| 2151 | |
| 2152 | static PyMethodDef frozenset_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
| 2154 | contains_doc}, |
| 2155 | {"copy", (PyCFunction)frozenset_copy, METH_NOARGS, |
| 2156 | copy_doc}, |
| 2157 | {"difference", (PyCFunction)set_difference_multi, METH_VARARGS, |
| 2158 | difference_doc}, |
| 2159 | {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS, |
| 2160 | intersection_doc}, |
| 2161 | {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O, |
| 2162 | isdisjoint_doc}, |
| 2163 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 2164 | issubset_doc}, |
| 2165 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 2166 | issuperset_doc}, |
| 2167 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 2168 | reduce_doc}, |
| 2169 | {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS, |
| 2170 | sizeof_doc}, |
| 2171 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 2172 | symmetric_difference_doc}, |
| 2173 | {"union", (PyCFunction)set_union, METH_VARARGS, |
| 2174 | union_doc}, |
| 2175 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2176 | }; |
| 2177 | |
| 2178 | static PyNumberMethods frozenset_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | 0, /*nb_add*/ |
| 2180 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 2181 | 0, /*nb_multiply*/ |
| 2182 | 0, /*nb_remainder*/ |
| 2183 | 0, /*nb_divmod*/ |
| 2184 | 0, /*nb_power*/ |
| 2185 | 0, /*nb_negative*/ |
| 2186 | 0, /*nb_positive*/ |
| 2187 | 0, /*nb_absolute*/ |
| 2188 | 0, /*nb_bool*/ |
| 2189 | 0, /*nb_invert*/ |
| 2190 | 0, /*nb_lshift*/ |
| 2191 | 0, /*nb_rshift*/ |
| 2192 | (binaryfunc)set_and, /*nb_and*/ |
| 2193 | (binaryfunc)set_xor, /*nb_xor*/ |
| 2194 | (binaryfunc)set_or, /*nb_or*/ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2195 | }; |
| 2196 | |
| 2197 | PyDoc_STRVAR(frozenset_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2198 | "frozenset() -> empty frozenset object\n\ |
| 2199 | frozenset(iterable) -> frozenset object\n\ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2200 | \n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2201 | Build an immutable unordered collection of unique elements."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2202 | |
| 2203 | PyTypeObject PyFrozenSet_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2205 | "frozenset", /* tp_name */ |
| 2206 | sizeof(PySetObject), /* tp_basicsize */ |
| 2207 | 0, /* tp_itemsize */ |
| 2208 | /* methods */ |
| 2209 | (destructor)set_dealloc, /* tp_dealloc */ |
| 2210 | 0, /* tp_print */ |
| 2211 | 0, /* tp_getattr */ |
| 2212 | 0, /* tp_setattr */ |
| 2213 | 0, /* tp_reserved */ |
| 2214 | (reprfunc)set_repr, /* tp_repr */ |
| 2215 | &frozenset_as_number, /* tp_as_number */ |
| 2216 | &set_as_sequence, /* tp_as_sequence */ |
| 2217 | 0, /* tp_as_mapping */ |
| 2218 | frozenset_hash, /* tp_hash */ |
| 2219 | 0, /* tp_call */ |
| 2220 | 0, /* tp_str */ |
| 2221 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2222 | 0, /* tp_setattro */ |
| 2223 | 0, /* tp_as_buffer */ |
| 2224 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2225 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2226 | frozenset_doc, /* tp_doc */ |
| 2227 | (traverseproc)set_traverse, /* tp_traverse */ |
| 2228 | (inquiry)set_clear_internal, /* tp_clear */ |
| 2229 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
| 2230 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
| 2231 | (getiterfunc)set_iter, /* tp_iter */ |
| 2232 | 0, /* tp_iternext */ |
| 2233 | frozenset_methods, /* tp_methods */ |
| 2234 | 0, /* tp_members */ |
| 2235 | 0, /* tp_getset */ |
| 2236 | 0, /* tp_base */ |
| 2237 | 0, /* tp_dict */ |
| 2238 | 0, /* tp_descr_get */ |
| 2239 | 0, /* tp_descr_set */ |
| 2240 | 0, /* tp_dictoffset */ |
| 2241 | 0, /* tp_init */ |
| 2242 | PyType_GenericAlloc, /* tp_alloc */ |
| 2243 | frozenset_new, /* tp_new */ |
| 2244 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2245 | }; |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2246 | |
| 2247 | |
| 2248 | /***** C API functions *************************************************/ |
| 2249 | |
| 2250 | PyObject * |
| 2251 | PySet_New(PyObject *iterable) |
| 2252 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | return make_new_set(&PySet_Type, iterable); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | PyObject * |
| 2257 | PyFrozenSet_New(PyObject *iterable) |
| 2258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | return make_new_set(&PyFrozenSet_Type, iterable); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Neal Norwitz | 8c49c82 | 2006-03-04 18:41:19 +0000 | [diff] [blame] | 2262 | Py_ssize_t |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2263 | PySet_Size(PyObject *anyset) |
| 2264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | if (!PyAnySet_Check(anyset)) { |
| 2266 | PyErr_BadInternalCall(); |
| 2267 | return -1; |
| 2268 | } |
| 2269 | return PySet_GET_SIZE(anyset); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2273 | PySet_Clear(PyObject *set) |
| 2274 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | if (!PySet_Check(set)) { |
| 2276 | PyErr_BadInternalCall(); |
| 2277 | return -1; |
| 2278 | } |
| 2279 | return set_clear_internal((PySetObject *)set); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | int |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2283 | PySet_Contains(PyObject *anyset, PyObject *key) |
| 2284 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2285 | if (!PyAnySet_Check(anyset)) { |
| 2286 | PyErr_BadInternalCall(); |
| 2287 | return -1; |
| 2288 | } |
| 2289 | return set_contains_key((PySetObject *)anyset, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | int |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2293 | PySet_Discard(PyObject *set, PyObject *key) |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | if (!PySet_Check(set)) { |
| 2296 | PyErr_BadInternalCall(); |
| 2297 | return -1; |
| 2298 | } |
| 2299 | return set_discard_key((PySetObject *)set, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | int |
Christian Heimes | fd66e51 | 2008-01-29 12:18:50 +0000 | [diff] [blame] | 2303 | PySet_Add(PyObject *anyset, PyObject *key) |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 | if (!PySet_Check(anyset) && |
| 2306 | (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) { |
| 2307 | PyErr_BadInternalCall(); |
| 2308 | return -1; |
| 2309 | } |
| 2310 | return set_add_key((PySetObject *)anyset, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2313 | int |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2314 | _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2315 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2316 | setentry *entry; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2318 | if (!PyAnySet_Check(set)) { |
| 2319 | PyErr_BadInternalCall(); |
| 2320 | return -1; |
| 2321 | } |
| 2322 | if (set_next((PySetObject *)set, pos, &entry) == 0) |
| 2323 | return 0; |
| 2324 | *key = entry->key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2325 | *hash = entry->hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | return 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2329 | PyObject * |
| 2330 | PySet_Pop(PyObject *set) |
| 2331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | if (!PySet_Check(set)) { |
| 2333 | PyErr_BadInternalCall(); |
| 2334 | return NULL; |
| 2335 | } |
| 2336 | return set_pop((PySetObject *)set); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2337 | } |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2338 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2339 | int |
| 2340 | _PySet_Update(PyObject *set, PyObject *iterable) |
| 2341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | if (!PySet_Check(set)) { |
| 2343 | PyErr_BadInternalCall(); |
| 2344 | return -1; |
| 2345 | } |
| 2346 | return set_update_internal((PySetObject *)set, iterable); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2347 | } |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2348 | |
| 2349 | #ifdef Py_DEBUG |
| 2350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | /* Test code to be called with any three element set. |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2352 | Returns True and original set is restored. */ |
| 2353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | #define assertRaises(call_return_value, exception) \ |
| 2355 | do { \ |
| 2356 | assert(call_return_value); \ |
| 2357 | assert(PyErr_ExceptionMatches(exception)); \ |
| 2358 | PyErr_Clear(); \ |
| 2359 | } while(0) |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2360 | |
| 2361 | static PyObject * |
| 2362 | test_c_api(PySetObject *so) |
| 2363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2364 | Py_ssize_t count; |
| 2365 | char *s; |
| 2366 | Py_ssize_t i; |
| 2367 | PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x; |
| 2368 | PyObject *ob = (PyObject *)so; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2369 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | PyObject *str; |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2372 | /* Verify preconditions */ |
| 2373 | assert(PyAnySet_Check(ob)); |
| 2374 | assert(PyAnySet_CheckExact(ob)); |
| 2375 | assert(!PyFrozenSet_CheckExact(ob)); |
Victor Stinner | 08b36bd | 2010-03-13 00:19:17 +0000 | [diff] [blame] | 2376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2377 | /* so.clear(); so |= set("abc"); */ |
| 2378 | str = PyUnicode_FromString("abc"); |
| 2379 | if (str == NULL) |
| 2380 | return NULL; |
| 2381 | set_clear_internal(so); |
| 2382 | if (set_update_internal(so, str) == -1) { |
| 2383 | Py_DECREF(str); |
| 2384 | return NULL; |
| 2385 | } |
| 2386 | Py_DECREF(str); |
Victor Stinner | 08b36bd | 2010-03-13 00:19:17 +0000 | [diff] [blame] | 2387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | /* Exercise type/size checks */ |
| 2389 | assert(PySet_Size(ob) == 3); |
| 2390 | assert(PySet_GET_SIZE(ob) == 3); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | /* Raise TypeError for non-iterable constructor arguments */ |
| 2393 | assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError); |
| 2394 | assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | /* Raise TypeError for unhashable key */ |
| 2397 | dup = PySet_New(ob); |
| 2398 | assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError); |
| 2399 | assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError); |
| 2400 | assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2402 | /* Exercise successful pop, contains, add, and discard */ |
| 2403 | elem = PySet_Pop(ob); |
| 2404 | assert(PySet_Contains(ob, elem) == 0); |
| 2405 | assert(PySet_GET_SIZE(ob) == 2); |
| 2406 | assert(PySet_Add(ob, elem) == 0); |
| 2407 | assert(PySet_Contains(ob, elem) == 1); |
| 2408 | assert(PySet_GET_SIZE(ob) == 3); |
| 2409 | assert(PySet_Discard(ob, elem) == 1); |
| 2410 | assert(PySet_GET_SIZE(ob) == 2); |
| 2411 | assert(PySet_Discard(ob, elem) == 0); |
| 2412 | assert(PySet_GET_SIZE(ob) == 2); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2414 | /* Exercise clear */ |
| 2415 | dup2 = PySet_New(dup); |
| 2416 | assert(PySet_Clear(dup2) == 0); |
| 2417 | assert(PySet_Size(dup2) == 0); |
| 2418 | Py_DECREF(dup2); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | /* Raise SystemError on clear or update of frozen set */ |
| 2421 | f = PyFrozenSet_New(dup); |
| 2422 | assertRaises(PySet_Clear(f) == -1, PyExc_SystemError); |
| 2423 | assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError); |
| 2424 | assert(PySet_Add(f, elem) == 0); |
| 2425 | Py_INCREF(f); |
| 2426 | assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError); |
| 2427 | Py_DECREF(f); |
| 2428 | Py_DECREF(f); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2430 | /* Exercise direct iteration */ |
| 2431 | i = 0, count = 0; |
| 2432 | while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) { |
| 2433 | s = _PyUnicode_AsString(x); |
| 2434 | assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c')); |
| 2435 | count++; |
| 2436 | } |
| 2437 | assert(count == 3); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | /* Exercise updates */ |
| 2440 | dup2 = PySet_New(NULL); |
| 2441 | assert(_PySet_Update(dup2, dup) == 0); |
| 2442 | assert(PySet_Size(dup2) == 3); |
| 2443 | assert(_PySet_Update(dup2, dup) == 0); |
| 2444 | assert(PySet_Size(dup2) == 3); |
| 2445 | Py_DECREF(dup2); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | /* Raise SystemError when self argument is not a set or frozenset. */ |
| 2448 | t = PyTuple_New(0); |
| 2449 | assertRaises(PySet_Size(t) == -1, PyExc_SystemError); |
| 2450 | assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError); |
| 2451 | Py_DECREF(t); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | /* Raise SystemError when self argument is not a set. */ |
| 2454 | f = PyFrozenSet_New(dup); |
| 2455 | assert(PySet_Size(f) == 3); |
| 2456 | assert(PyFrozenSet_CheckExact(f)); |
| 2457 | assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError); |
| 2458 | assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError); |
| 2459 | Py_DECREF(f); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2461 | /* Raise KeyError when popping from an empty set */ |
| 2462 | assert(PyNumber_InPlaceSubtract(ob, ob) == ob); |
| 2463 | Py_DECREF(ob); |
| 2464 | assert(PySet_GET_SIZE(ob) == 0); |
| 2465 | assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2467 | /* Restore the set from the copy using the PyNumber API */ |
| 2468 | assert(PyNumber_InPlaceOr(ob, dup) == ob); |
| 2469 | Py_DECREF(ob); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2471 | /* Verify constructors accept NULL arguments */ |
| 2472 | f = PySet_New(NULL); |
| 2473 | assert(f != NULL); |
| 2474 | assert(PySet_GET_SIZE(f) == 0); |
| 2475 | Py_DECREF(f); |
| 2476 | f = PyFrozenSet_New(NULL); |
| 2477 | assert(f != NULL); |
| 2478 | assert(PyFrozenSet_CheckExact(f)); |
| 2479 | assert(PySet_GET_SIZE(f) == 0); |
| 2480 | Py_DECREF(f); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2482 | Py_DECREF(elem); |
| 2483 | Py_DECREF(dup); |
| 2484 | Py_RETURN_TRUE; |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 2487 | #undef assertRaises |
| 2488 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2489 | #endif |