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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | register Py_ssize_t i; |
| 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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | i = hash & mask; |
| 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | register Py_ssize_t i; |
| 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 | } |
| 177 | i = hash & mask; |
| 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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | i = hash & mask; |
| 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) || |
| 389 | (hash = ((PyUnicodeObject *) key)->hash) == -1) { |
| 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) || |
| 437 | (hash = ((PyUnicodeObject *) key)->hash) == -1) { |
| 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | PyObject *keys, *result=NULL; |
| 583 | Py_UNICODE *u; |
| 584 | int status = Py_ReprEnter((PyObject*)so); |
| 585 | PyObject *listrepr; |
| 586 | Py_ssize_t newsize; |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | if (status != 0) { |
| 589 | if (status < 0) |
| 590 | return NULL; |
| 591 | return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name); |
| 592 | } |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | /* shortcut for the empty set */ |
| 595 | if (!so->used) { |
| 596 | Py_ReprLeave((PyObject*)so); |
| 597 | return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name); |
| 598 | } |
Georg Brandl | c4996ba | 2006-08-28 19:37:11 +0000 | [diff] [blame] | 599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | keys = PySequence_List((PyObject *)so); |
| 601 | if (keys == NULL) |
| 602 | goto done; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | listrepr = PyObject_Repr(keys); |
| 605 | Py_DECREF(keys); |
| 606 | if (listrepr == NULL) |
| 607 | goto done; |
| 608 | newsize = PyUnicode_GET_SIZE(listrepr); |
| 609 | result = PyUnicode_FromUnicode(NULL, newsize); |
| 610 | if (result) { |
| 611 | u = PyUnicode_AS_UNICODE(result); |
| 612 | *u++ = '{'; |
| 613 | /* Omit the brackets from the listrepr */ |
| 614 | Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1, |
| 615 | PyUnicode_GET_SIZE(listrepr)-2); |
| 616 | u += newsize-2; |
| 617 | *u++ = '}'; |
| 618 | } |
| 619 | Py_DECREF(listrepr); |
| 620 | if (Py_TYPE(so) != &PySet_Type) { |
| 621 | PyObject *tmp = PyUnicode_FromFormat("%s(%U)", |
| 622 | Py_TYPE(so)->tp_name, |
| 623 | result); |
| 624 | Py_DECREF(result); |
| 625 | result = tmp; |
| 626 | } |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 627 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | Py_ReprLeave((PyObject*)so); |
| 629 | return result; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 632 | static Py_ssize_t |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 633 | set_len(PyObject *so) |
| 634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | return ((PySetObject *)so)->used; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 638 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 639 | set_merge(PySetObject *so, PyObject *otherset) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | PySetObject *other; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 642 | PyObject *key; |
Éric Araujo | bbcfc1f | 2011-03-23 03:43:22 +0100 | [diff] [blame] | 643 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | register Py_ssize_t i; |
| 645 | register setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | assert (PyAnySet_Check(so)); |
| 648 | assert (PyAnySet_Check(otherset)); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | other = (PySetObject*)otherset; |
| 651 | if (other == so || other->used == 0) |
| 652 | /* a.update(a) or a.update({}); nothing to do */ |
| 653 | return 0; |
| 654 | /* Do one big resize at the start, rather than |
| 655 | * incrementally resizing as we insert new keys. Expect |
| 656 | * that there will be no (or few) overlapping keys. |
| 657 | */ |
| 658 | if ((so->fill + other->used)*3 >= (so->mask+1)*2) { |
| 659 | if (set_table_resize(so, (so->used + other->used)*2) != 0) |
| 660 | return -1; |
| 661 | } |
| 662 | for (i = 0; i <= other->mask; i++) { |
| 663 | entry = &other->table[i]; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 664 | key = entry->key; |
Éric Araujo | 4804991 | 2011-03-23 02:08:07 +0100 | [diff] [blame] | 665 | hash = entry->hash; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 666 | if (key != NULL && |
| 667 | key != dummy) { |
| 668 | Py_INCREF(key); |
Éric Araujo | 4804991 | 2011-03-23 02:08:07 +0100 | [diff] [blame] | 669 | if (set_insert_key(so, key, hash) == -1) { |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 670 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | return -1; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 679 | set_contains_key(PySetObject *so, PyObject *key) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 680 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 681 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | setentry *entry; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | if (!PyUnicode_CheckExact(key) || |
| 685 | (hash = ((PyUnicodeObject *) key)->hash) == -1) { |
| 686 | hash = PyObject_Hash(key); |
| 687 | if (hash == -1) |
| 688 | return -1; |
| 689 | } |
| 690 | entry = (so->lookup)(so, key, hash); |
| 691 | if (entry == NULL) |
| 692 | return -1; |
| 693 | key = entry->key; |
| 694 | return key != NULL && key != dummy; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 697 | static int |
| 698 | set_contains_entry(PySetObject *so, setentry *entry) |
| 699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | PyObject *key; |
| 701 | setentry *lu_entry; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 702 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 703 | lu_entry = (so->lookup)(so, entry->key, entry->hash); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | if (lu_entry == NULL) |
| 705 | return -1; |
| 706 | key = lu_entry->key; |
| 707 | return key != NULL && key != dummy; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 710 | static PyObject * |
| 711 | set_pop(PySetObject *so) |
| 712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | register Py_ssize_t i = 0; |
| 714 | register setentry *entry; |
| 715 | PyObject *key; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | assert (PyAnySet_Check(so)); |
| 718 | if (so->used == 0) { |
| 719 | PyErr_SetString(PyExc_KeyError, "pop from an empty set"); |
| 720 | return NULL; |
| 721 | } |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | /* Set entry to "the first" unused or dummy set entry. We abuse |
| 724 | * the hash field of slot 0 to hold a search finger: |
| 725 | * If slot 0 has a value, use slot 0. |
| 726 | * Else slot 0 is being used to hold a search finger, |
| 727 | * and we use its hash value as the first index to look. |
| 728 | */ |
| 729 | entry = &so->table[0]; |
| 730 | if (entry->key == NULL || entry->key == dummy) { |
| 731 | i = entry->hash; |
| 732 | /* The hash field may be a real hash value, or it may be a |
| 733 | * legit search finger, or it may be a once-legit search |
| 734 | * finger that's out of bounds now because it wrapped around |
| 735 | * or the table shrunk -- simply make sure it's in bounds now. |
| 736 | */ |
| 737 | if (i > so->mask || i < 1) |
| 738 | i = 1; /* skip slot 0 */ |
| 739 | while ((entry = &so->table[i])->key == NULL || entry->key==dummy) { |
| 740 | i++; |
| 741 | if (i > so->mask) |
| 742 | i = 1; |
| 743 | } |
| 744 | } |
| 745 | key = entry->key; |
| 746 | Py_INCREF(dummy); |
| 747 | entry->key = dummy; |
| 748 | so->used--; |
| 749 | so->table[0].hash = i + 1; /* next place to start */ |
| 750 | return key; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 753 | PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\ |
| 754 | Raises KeyError if the set is empty."); |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 755 | |
| 756 | static int |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 757 | set_traverse(PySetObject *so, visitproc visit, void *arg) |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | Py_ssize_t pos = 0; |
| 760 | setentry *entry; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | while (set_next(so, &pos, &entry)) |
| 763 | Py_VISIT(entry->key); |
| 764 | return 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 767 | static Py_hash_t |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 768 | frozenset_hash(PyObject *self) |
| 769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | PySetObject *so = (PySetObject *)self; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 771 | Py_hash_t h, hash = 1927868237L; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | setentry *entry; |
| 773 | Py_ssize_t pos = 0; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | if (so->hash != -1) |
| 776 | return so->hash; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 777 | |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 778 | hash *= PySet_GET_SIZE(self) + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | while (set_next(so, &pos, &entry)) { |
| 780 | /* Work to increase the bit dispersion for closely spaced hash |
| 781 | values. The is important because some use cases have many |
| 782 | combinations of a small number of elements with nearby |
| 783 | hashes so that many distinct combinations collapse to only |
| 784 | a handful of distinct hash values. */ |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 785 | h = entry->hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u; |
| 787 | } |
| 788 | hash = hash * 69069L + 907133923L; |
| 789 | if (hash == -1) |
| 790 | hash = 590923713L; |
| 791 | so->hash = hash; |
| 792 | return hash; |
Raymond Hettinger | f408ddf | 2005-08-17 00:27:42 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Raymond Hettinger | a9d9936 | 2005-08-05 00:01:15 +0000 | [diff] [blame] | 795 | /***** Set iterator type ***********************************************/ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 796 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 797 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | PyObject_HEAD |
| 799 | PySetObject *si_set; /* Set to NULL when iterator is exhausted */ |
| 800 | Py_ssize_t si_used; |
| 801 | Py_ssize_t si_pos; |
| 802 | Py_ssize_t len; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 803 | } setiterobject; |
| 804 | |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 805 | static void |
| 806 | setiter_dealloc(setiterobject *si) |
| 807 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | Py_XDECREF(si->si_set); |
| 809 | PyObject_GC_Del(si); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | static int |
| 813 | setiter_traverse(setiterobject *si, visitproc visit, void *arg) |
| 814 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | Py_VISIT(si->si_set); |
| 816 | return 0; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 819 | static PyObject * |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 820 | setiter_len(setiterobject *si) |
| 821 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | Py_ssize_t len = 0; |
| 823 | if (si->si_set != NULL && si->si_used == si->si_set->used) |
| 824 | len = si->len; |
Antoine Pitrou | 671b4d9 | 2010-08-17 17:55:07 +0000 | [diff] [blame] | 825 | return PyLong_FromSsize_t(len); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 828 | 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] | 829 | |
| 830 | static PyMethodDef setiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 831 | {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc}, |
| 832 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 833 | }; |
| 834 | |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 835 | static PyObject *setiter_iternext(setiterobject *si) |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 836 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | PyObject *key; |
| 838 | register Py_ssize_t i, mask; |
| 839 | register setentry *entry; |
| 840 | PySetObject *so = si->si_set; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | if (so == NULL) |
| 843 | return NULL; |
| 844 | assert (PyAnySet_Check(so)); |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | if (si->si_used != so->used) { |
| 847 | PyErr_SetString(PyExc_RuntimeError, |
| 848 | "Set changed size during iteration"); |
| 849 | si->si_used = -1; /* Make this state sticky */ |
| 850 | return NULL; |
| 851 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | i = si->si_pos; |
| 854 | assert(i>=0); |
| 855 | entry = so->table; |
| 856 | mask = so->mask; |
| 857 | while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) |
| 858 | i++; |
| 859 | si->si_pos = i+1; |
| 860 | if (i > mask) |
| 861 | goto fail; |
| 862 | si->len--; |
| 863 | key = entry[i].key; |
| 864 | Py_INCREF(key); |
| 865 | return key; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 866 | |
| 867 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | Py_DECREF(so); |
| 869 | si->si_set = NULL; |
| 870 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 873 | PyTypeObject PySetIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 875 | "set_iterator", /* tp_name */ |
| 876 | sizeof(setiterobject), /* tp_basicsize */ |
| 877 | 0, /* tp_itemsize */ |
| 878 | /* methods */ |
| 879 | (destructor)setiter_dealloc, /* tp_dealloc */ |
| 880 | 0, /* tp_print */ |
| 881 | 0, /* tp_getattr */ |
| 882 | 0, /* tp_setattr */ |
| 883 | 0, /* tp_reserved */ |
| 884 | 0, /* tp_repr */ |
| 885 | 0, /* tp_as_number */ |
| 886 | 0, /* tp_as_sequence */ |
| 887 | 0, /* tp_as_mapping */ |
| 888 | 0, /* tp_hash */ |
| 889 | 0, /* tp_call */ |
| 890 | 0, /* tp_str */ |
| 891 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 892 | 0, /* tp_setattro */ |
| 893 | 0, /* tp_as_buffer */ |
| 894 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 895 | 0, /* tp_doc */ |
| 896 | (traverseproc)setiter_traverse, /* tp_traverse */ |
| 897 | 0, /* tp_clear */ |
| 898 | 0, /* tp_richcompare */ |
| 899 | 0, /* tp_weaklistoffset */ |
| 900 | PyObject_SelfIter, /* tp_iter */ |
| 901 | (iternextfunc)setiter_iternext, /* tp_iternext */ |
| 902 | setiter_methods, /* tp_methods */ |
| 903 | 0, |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 904 | }; |
| 905 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 906 | static PyObject * |
| 907 | set_iter(PySetObject *so) |
| 908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 909 | setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type); |
| 910 | if (si == NULL) |
| 911 | return NULL; |
| 912 | Py_INCREF(so); |
| 913 | si->si_set = so; |
| 914 | si->si_used = so->used; |
| 915 | si->si_pos = 0; |
| 916 | si->len = so->used; |
| 917 | _PyObject_GC_TRACK(si); |
| 918 | return (PyObject *)si; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 921 | static int |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 922 | set_update_internal(PySetObject *so, PyObject *other) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | PyObject *key, *it; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | if (PyAnySet_Check(other)) |
| 927 | return set_merge(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | if (PyDict_CheckExact(other)) { |
| 930 | PyObject *value; |
| 931 | Py_ssize_t pos = 0; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 932 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | Py_ssize_t dictsize = PyDict_Size(other); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | /* Do one big resize at the start, rather than |
| 936 | * incrementally resizing as we insert new keys. Expect |
| 937 | * that there will be no (or few) overlapping keys. |
| 938 | */ |
| 939 | if (dictsize == -1) |
| 940 | return -1; |
| 941 | if ((so->fill + dictsize)*3 >= (so->mask+1)*2) { |
| 942 | if (set_table_resize(so, (so->used + dictsize)*2) != 0) |
| 943 | return -1; |
| 944 | } |
| 945 | while (_PyDict_Next(other, &pos, &key, &value, &hash)) { |
| 946 | setentry an_entry; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | an_entry.hash = hash; |
| 949 | an_entry.key = key; |
| 950 | if (set_add_entry(so, &an_entry) == -1) |
| 951 | return -1; |
| 952 | } |
| 953 | return 0; |
| 954 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | it = PyObject_GetIter(other); |
| 957 | if (it == NULL) |
| 958 | return -1; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | while ((key = PyIter_Next(it)) != NULL) { |
| 961 | if (set_add_key(so, key) == -1) { |
| 962 | Py_DECREF(it); |
| 963 | Py_DECREF(key); |
| 964 | return -1; |
| 965 | } |
| 966 | Py_DECREF(key); |
| 967 | } |
| 968 | Py_DECREF(it); |
| 969 | if (PyErr_Occurred()) |
| 970 | return -1; |
| 971 | return 0; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 975 | set_update(PySetObject *so, PyObject *args) |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 976 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | Py_ssize_t i; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 980 | PyObject *other = PyTuple_GET_ITEM(args, i); |
| 981 | if (set_update_internal(so, other) == -1) |
| 982 | return NULL; |
| 983 | } |
| 984 | Py_RETURN_NONE; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | PyDoc_STRVAR(update_doc, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 988 | "Update a set with the union of itself and others."); |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 989 | |
| 990 | static PyObject * |
| 991 | make_new_set(PyTypeObject *type, PyObject *iterable) |
| 992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | register PySetObject *so = NULL; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | if (dummy == NULL) { /* Auto-initialize dummy */ |
| 996 | dummy = PyUnicode_FromString("<dummy key>"); |
| 997 | if (dummy == NULL) |
| 998 | return NULL; |
| 999 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | /* create PySetObject structure */ |
| 1002 | if (numfree && |
| 1003 | (type == &PySet_Type || type == &PyFrozenSet_Type)) { |
| 1004 | so = free_list[--numfree]; |
| 1005 | assert (so != NULL && PyAnySet_CheckExact(so)); |
| 1006 | Py_TYPE(so) = type; |
| 1007 | _Py_NewReference((PyObject *)so); |
| 1008 | EMPTY_TO_MINSIZE(so); |
| 1009 | PyObject_GC_Track(so); |
| 1010 | } else { |
| 1011 | so = (PySetObject *)type->tp_alloc(type, 0); |
| 1012 | if (so == NULL) |
| 1013 | return NULL; |
| 1014 | /* tp_alloc has already zeroed the structure */ |
| 1015 | assert(so->table == NULL && so->fill == 0 && so->used == 0); |
| 1016 | INIT_NONZERO_SET_SLOTS(so); |
| 1017 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | so->lookup = set_lookkey_unicode; |
| 1020 | so->weakreflist = NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | if (iterable != NULL) { |
| 1023 | if (set_update_internal(so, iterable) == -1) { |
| 1024 | Py_DECREF(so); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 1028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Raymond Hettinger | 7d99f09 | 2008-11-16 11:44:54 +0000 | [diff] [blame] | 1032 | static PyObject * |
| 1033 | make_new_set_basetype(PyTypeObject *type, PyObject *iterable) |
| 1034 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | if (type != &PySet_Type && type != &PyFrozenSet_Type) { |
| 1036 | if (PyType_IsSubtype(type, &PySet_Type)) |
| 1037 | type = &PySet_Type; |
| 1038 | else |
| 1039 | type = &PyFrozenSet_Type; |
| 1040 | } |
| 1041 | return make_new_set(type, iterable); |
Raymond Hettinger | 7d99f09 | 2008-11-16 11:44:54 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1044 | /* The empty frozenset is a singleton */ |
| 1045 | static PyObject *emptyfrozenset = NULL; |
| 1046 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1047 | static PyObject * |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1048 | frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1049 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | PyObject *iterable = NULL, *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds)) |
| 1053 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) |
| 1056 | return NULL; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | if (type != &PyFrozenSet_Type) |
| 1059 | return make_new_set(type, iterable); |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | if (iterable != NULL) { |
| 1062 | /* frozenset(f) is idempotent */ |
| 1063 | if (PyFrozenSet_CheckExact(iterable)) { |
| 1064 | Py_INCREF(iterable); |
| 1065 | return iterable; |
| 1066 | } |
| 1067 | result = make_new_set(type, iterable); |
| 1068 | if (result == NULL || PySet_GET_SIZE(result)) |
| 1069 | return result; |
| 1070 | Py_DECREF(result); |
| 1071 | } |
| 1072 | /* The empty frozenset is a singleton */ |
| 1073 | if (emptyfrozenset == NULL) |
| 1074 | emptyfrozenset = make_new_set(type, NULL); |
| 1075 | Py_XINCREF(emptyfrozenset); |
| 1076 | return emptyfrozenset; |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | void |
| 1080 | PySet_Fini(void) |
| 1081 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | PySetObject *so; |
Raymond Hettinger | bc841a1 | 2005-08-07 13:02:53 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | while (numfree) { |
| 1085 | numfree--; |
| 1086 | so = free_list[numfree]; |
| 1087 | PyObject_GC_Del(so); |
| 1088 | } |
| 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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
| 1214 | Py_INCREF(Py_NotImplemented); |
| 1215 | return Py_NotImplemented; |
| 1216 | } |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | result = (PySetObject *)set_copy(so); |
| 1219 | if (result == NULL) |
| 1220 | return NULL; |
| 1221 | if ((PyObject *)so == other) |
| 1222 | return (PyObject *)result; |
| 1223 | if (set_update_internal(result, other) == -1) { |
| 1224 | Py_DECREF(result); |
| 1225 | return NULL; |
| 1226 | } |
| 1227 | return (PyObject *)result; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1230 | static PyObject * |
| 1231 | set_ior(PySetObject *so, PyObject *other) |
| 1232 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | if (!PyAnySet_Check(other)) { |
| 1234 | Py_INCREF(Py_NotImplemented); |
| 1235 | return Py_NotImplemented; |
| 1236 | } |
| 1237 | if (set_update_internal(so, other) == -1) |
| 1238 | return NULL; |
| 1239 | Py_INCREF(so); |
| 1240 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | static PyObject * |
| 1244 | set_intersection(PySetObject *so, PyObject *other) |
| 1245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | PySetObject *result; |
| 1247 | PyObject *key, *it, *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | if ((PyObject *)so == other) |
| 1250 | return set_copy(so); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL); |
| 1253 | if (result == NULL) |
| 1254 | return NULL; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | if (PyAnySet_Check(other)) { |
| 1257 | Py_ssize_t pos = 0; |
| 1258 | setentry *entry; |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) { |
| 1261 | tmp = (PyObject *)so; |
| 1262 | so = (PySetObject *)other; |
| 1263 | other = tmp; |
| 1264 | } |
Raymond Hettinger | b02c35e | 2005-08-12 20:48:39 +0000 | [diff] [blame] | 1265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | while (set_next((PySetObject *)other, &pos, &entry)) { |
| 1267 | int rv = set_contains_entry(so, entry); |
| 1268 | if (rv == -1) { |
| 1269 | Py_DECREF(result); |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | if (rv) { |
| 1273 | if (set_add_entry(result, entry) == -1) { |
| 1274 | Py_DECREF(result); |
| 1275 | return NULL; |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | return (PyObject *)result; |
| 1280 | } |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | it = PyObject_GetIter(other); |
| 1283 | if (it == NULL) { |
| 1284 | Py_DECREF(result); |
| 1285 | return NULL; |
| 1286 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | while ((key = PyIter_Next(it)) != NULL) { |
| 1289 | int rv; |
| 1290 | setentry entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1291 | Py_hash_t hash = PyObject_Hash(key); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | if (hash == -1) { |
| 1294 | Py_DECREF(it); |
| 1295 | Py_DECREF(result); |
| 1296 | Py_DECREF(key); |
| 1297 | return NULL; |
| 1298 | } |
| 1299 | entry.hash = hash; |
| 1300 | entry.key = key; |
| 1301 | rv = set_contains_entry(so, &entry); |
| 1302 | if (rv == -1) { |
| 1303 | Py_DECREF(it); |
| 1304 | Py_DECREF(result); |
| 1305 | Py_DECREF(key); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | if (rv) { |
| 1309 | if (set_add_entry(result, &entry) == -1) { |
| 1310 | Py_DECREF(it); |
| 1311 | Py_DECREF(result); |
| 1312 | Py_DECREF(key); |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | } |
| 1316 | Py_DECREF(key); |
| 1317 | } |
| 1318 | Py_DECREF(it); |
| 1319 | if (PyErr_Occurred()) { |
| 1320 | Py_DECREF(result); |
| 1321 | return NULL; |
| 1322 | } |
| 1323 | return (PyObject *)result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1326 | static PyObject * |
| 1327 | set_intersection_multi(PySetObject *so, PyObject *args) |
| 1328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | Py_ssize_t i; |
| 1330 | PyObject *result = (PyObject *)so; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | if (PyTuple_GET_SIZE(args) == 0) |
| 1333 | return set_copy(so); |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | Py_INCREF(so); |
| 1336 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1337 | PyObject *other = PyTuple_GET_ITEM(args, i); |
| 1338 | PyObject *newresult = set_intersection((PySetObject *)result, other); |
| 1339 | if (newresult == NULL) { |
| 1340 | Py_DECREF(result); |
| 1341 | return NULL; |
| 1342 | } |
| 1343 | Py_DECREF(result); |
| 1344 | result = newresult; |
| 1345 | } |
| 1346 | return result; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1349 | PyDoc_STRVAR(intersection_doc, |
| 1350 | "Return the intersection of two sets as a new set.\n\ |
| 1351 | \n\ |
| 1352 | (i.e. all elements that are in both sets.)"); |
| 1353 | |
| 1354 | static PyObject * |
| 1355 | set_intersection_update(PySetObject *so, PyObject *other) |
| 1356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | PyObject *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | tmp = set_intersection(so, other); |
| 1360 | if (tmp == NULL) |
| 1361 | return NULL; |
| 1362 | set_swap_bodies(so, (PySetObject *)tmp); |
| 1363 | Py_DECREF(tmp); |
| 1364 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1367 | static PyObject * |
| 1368 | set_intersection_update_multi(PySetObject *so, PyObject *args) |
| 1369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | PyObject *tmp; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | tmp = set_intersection_multi(so, args); |
| 1373 | if (tmp == NULL) |
| 1374 | return NULL; |
| 1375 | set_swap_bodies(so, (PySetObject *)tmp); |
| 1376 | Py_DECREF(tmp); |
| 1377 | Py_RETURN_NONE; |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1380 | PyDoc_STRVAR(intersection_update_doc, |
| 1381 | "Update a set with the intersection of itself and another."); |
| 1382 | |
| 1383 | static PyObject * |
| 1384 | set_and(PySetObject *so, PyObject *other) |
| 1385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
| 1387 | Py_INCREF(Py_NotImplemented); |
| 1388 | return Py_NotImplemented; |
| 1389 | } |
| 1390 | return set_intersection(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | static PyObject * |
| 1394 | set_iand(PySetObject *so, PyObject *other) |
| 1395 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | PyObject *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | if (!PyAnySet_Check(other)) { |
| 1399 | Py_INCREF(Py_NotImplemented); |
| 1400 | return Py_NotImplemented; |
| 1401 | } |
| 1402 | result = set_intersection_update(so, other); |
| 1403 | if (result == NULL) |
| 1404 | return NULL; |
| 1405 | Py_DECREF(result); |
| 1406 | Py_INCREF(so); |
| 1407 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1410 | static PyObject * |
| 1411 | set_isdisjoint(PySetObject *so, PyObject *other) |
| 1412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | PyObject *key, *it, *tmp; |
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 ((PyObject *)so == other) { |
| 1416 | if (PySet_GET_SIZE(so) == 0) |
| 1417 | Py_RETURN_TRUE; |
| 1418 | else |
| 1419 | Py_RETURN_FALSE; |
| 1420 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1422 | if (PyAnySet_CheckExact(other)) { |
| 1423 | Py_ssize_t pos = 0; |
| 1424 | setentry *entry; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) { |
| 1427 | tmp = (PyObject *)so; |
| 1428 | so = (PySetObject *)other; |
| 1429 | other = tmp; |
| 1430 | } |
| 1431 | while (set_next((PySetObject *)other, &pos, &entry)) { |
| 1432 | int rv = set_contains_entry(so, entry); |
| 1433 | if (rv == -1) |
| 1434 | return NULL; |
| 1435 | if (rv) |
| 1436 | Py_RETURN_FALSE; |
| 1437 | } |
| 1438 | Py_RETURN_TRUE; |
| 1439 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | it = PyObject_GetIter(other); |
| 1442 | if (it == NULL) |
| 1443 | return NULL; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | while ((key = PyIter_Next(it)) != NULL) { |
| 1446 | int rv; |
| 1447 | setentry entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1448 | Py_hash_t hash = PyObject_Hash(key); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | if (hash == -1) { |
| 1451 | Py_DECREF(key); |
| 1452 | Py_DECREF(it); |
| 1453 | return NULL; |
| 1454 | } |
| 1455 | entry.hash = hash; |
| 1456 | entry.key = key; |
| 1457 | rv = set_contains_entry(so, &entry); |
| 1458 | Py_DECREF(key); |
| 1459 | if (rv == -1) { |
| 1460 | Py_DECREF(it); |
| 1461 | return NULL; |
| 1462 | } |
| 1463 | if (rv) { |
| 1464 | Py_DECREF(it); |
| 1465 | Py_RETURN_FALSE; |
| 1466 | } |
| 1467 | } |
| 1468 | Py_DECREF(it); |
| 1469 | if (PyErr_Occurred()) |
| 1470 | return NULL; |
| 1471 | Py_RETURN_TRUE; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | PyDoc_STRVAR(isdisjoint_doc, |
| 1475 | "Return True if two sets have a null intersection."); |
| 1476 | |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 1477 | static int |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1478 | set_difference_update_internal(PySetObject *so, PyObject *other) |
| 1479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | if ((PyObject *)so == other) |
| 1481 | return set_clear_internal(so); |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | if (PyAnySet_Check(other)) { |
| 1484 | setentry *entry; |
| 1485 | Py_ssize_t pos = 0; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | while (set_next((PySetObject *)other, &pos, &entry)) |
| 1488 | if (set_discard_entry(so, entry) == -1) |
| 1489 | return -1; |
| 1490 | } else { |
| 1491 | PyObject *key, *it; |
| 1492 | it = PyObject_GetIter(other); |
| 1493 | if (it == NULL) |
| 1494 | return -1; |
| 1495 | |
| 1496 | while ((key = PyIter_Next(it)) != NULL) { |
| 1497 | if (set_discard_key(so, key) == -1) { |
| 1498 | Py_DECREF(it); |
| 1499 | Py_DECREF(key); |
| 1500 | return -1; |
| 1501 | } |
| 1502 | Py_DECREF(key); |
| 1503 | } |
| 1504 | Py_DECREF(it); |
| 1505 | if (PyErr_Occurred()) |
| 1506 | return -1; |
| 1507 | } |
| 1508 | /* If more than 1/5 are dummies, then resize them away. */ |
| 1509 | if ((so->fill - so->used) * 5 < so->mask) |
| 1510 | return 0; |
| 1511 | 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] | 1512 | } |
| 1513 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1514 | static PyObject * |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1515 | set_difference_update(PySetObject *so, PyObject *args) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | Py_ssize_t i; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1520 | PyObject *other = PyTuple_GET_ITEM(args, i); |
| 1521 | if (set_difference_update_internal(so, other) == -1) |
| 1522 | return NULL; |
| 1523 | } |
| 1524 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | PyDoc_STRVAR(difference_update_doc, |
| 1528 | "Remove all elements of another set from this set."); |
| 1529 | |
| 1530 | static PyObject * |
Antoine Pitrou | 715f3cd | 2010-11-30 22:23:20 +0000 | [diff] [blame] | 1531 | set_copy_and_difference(PySetObject *so, PyObject *other) |
| 1532 | { |
| 1533 | PyObject *result; |
| 1534 | |
| 1535 | result = set_copy(so); |
| 1536 | if (result == NULL) |
| 1537 | return NULL; |
| 1538 | if (set_difference_update_internal((PySetObject *) result, other) != -1) |
| 1539 | return result; |
| 1540 | Py_DECREF(result); |
| 1541 | return NULL; |
| 1542 | } |
| 1543 | |
| 1544 | static PyObject * |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1545 | set_difference(PySetObject *so, PyObject *other) |
| 1546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | PyObject *result; |
| 1548 | setentry *entry; |
| 1549 | Py_ssize_t pos = 0; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) { |
Antoine Pitrou | 715f3cd | 2010-11-30 22:23:20 +0000 | [diff] [blame] | 1552 | return set_copy_and_difference(so, other); |
| 1553 | } |
| 1554 | |
| 1555 | /* If len(so) much more than len(other), it's more efficient to simply copy |
| 1556 | * so and then iterate other looking for common elements. */ |
| 1557 | if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) { |
| 1558 | return set_copy_and_difference(so, other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | } |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | result = make_new_set_basetype(Py_TYPE(so), NULL); |
| 1562 | if (result == NULL) |
| 1563 | return NULL; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1565 | if (PyDict_CheckExact(other)) { |
| 1566 | while (set_next(so, &pos, &entry)) { |
| 1567 | setentry entrycopy; |
| 1568 | entrycopy.hash = entry->hash; |
| 1569 | entrycopy.key = entry->key; |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 1570 | if (!_PyDict_Contains(other, entry->key, entry->hash)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | if (set_add_entry((PySetObject *)result, &entrycopy) == -1) { |
| 1572 | Py_DECREF(result); |
| 1573 | return NULL; |
| 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | return result; |
| 1578 | } |
| 1579 | |
Antoine Pitrou | 715f3cd | 2010-11-30 22:23:20 +0000 | [diff] [blame] | 1580 | /* Iterate over so, checking for common elements in other. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | while (set_next(so, &pos, &entry)) { |
| 1582 | int rv = set_contains_entry((PySetObject *)other, entry); |
| 1583 | if (rv == -1) { |
| 1584 | Py_DECREF(result); |
| 1585 | return NULL; |
| 1586 | } |
| 1587 | if (!rv) { |
| 1588 | if (set_add_entry((PySetObject *)result, entry) == -1) { |
| 1589 | Py_DECREF(result); |
| 1590 | return NULL; |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | return result; |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1597 | static PyObject * |
| 1598 | set_difference_multi(PySetObject *so, PyObject *args) |
| 1599 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | Py_ssize_t i; |
| 1601 | PyObject *result, *other; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | if (PyTuple_GET_SIZE(args) == 0) |
| 1604 | return set_copy(so); |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | other = PyTuple_GET_ITEM(args, 0); |
| 1607 | result = set_difference(so, other); |
| 1608 | if (result == NULL) |
| 1609 | return NULL; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) { |
| 1612 | other = PyTuple_GET_ITEM(args, i); |
| 1613 | if (set_difference_update_internal((PySetObject *)result, other) == -1) { |
| 1614 | Py_DECREF(result); |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | } |
| 1618 | return result; |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 1621 | PyDoc_STRVAR(difference_doc, |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1622 | "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] | 1623 | \n\ |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 1624 | (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] | 1625 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1626 | set_sub(PySetObject *so, PyObject *other) |
| 1627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
| 1629 | Py_INCREF(Py_NotImplemented); |
| 1630 | return Py_NotImplemented; |
| 1631 | } |
| 1632 | return set_difference(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | static PyObject * |
| 1636 | set_isub(PySetObject *so, PyObject *other) |
| 1637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | if (!PyAnySet_Check(other)) { |
| 1639 | Py_INCREF(Py_NotImplemented); |
| 1640 | return Py_NotImplemented; |
| 1641 | } |
| 1642 | if (set_difference_update_internal(so, other) == -1) |
| 1643 | return NULL; |
| 1644 | Py_INCREF(so); |
| 1645 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1649 | set_symmetric_difference_update(PySetObject *so, PyObject *other) |
| 1650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | PySetObject *otherset; |
| 1652 | PyObject *key; |
| 1653 | Py_ssize_t pos = 0; |
| 1654 | setentry *entry; |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | if ((PyObject *)so == other) |
| 1657 | return set_clear(so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | if (PyDict_CheckExact(other)) { |
| 1660 | PyObject *value; |
| 1661 | int rv; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1662 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | while (_PyDict_Next(other, &pos, &key, &value, &hash)) { |
| 1664 | setentry an_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1665 | |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1666 | Py_INCREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | an_entry.hash = hash; |
| 1668 | an_entry.key = key; |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1670 | rv = set_discard_entry(so, &an_entry); |
Georg Brandl | 2d44449 | 2010-09-03 10:52:55 +0000 | [diff] [blame] | 1671 | if (rv == -1) { |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1672 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | } |
Raymond Hettinger | faf7b7f | 2010-09-03 10:00:50 +0000 | [diff] [blame] | 1675 | if (rv == DISCARD_NOTFOUND) { |
| 1676 | if (set_add_entry(so, &an_entry) == -1) { |
| 1677 | Py_DECREF(key); |
| 1678 | return NULL; |
| 1679 | } |
| 1680 | } |
Georg Brandl | 2d44449 | 2010-09-03 10:52:55 +0000 | [diff] [blame] | 1681 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1682 | } |
| 1683 | Py_RETURN_NONE; |
| 1684 | } |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | if (PyAnySet_Check(other)) { |
| 1687 | Py_INCREF(other); |
| 1688 | otherset = (PySetObject *)other; |
| 1689 | } else { |
| 1690 | otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other); |
| 1691 | if (otherset == NULL) |
| 1692 | return NULL; |
| 1693 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1695 | while (set_next(otherset, &pos, &entry)) { |
| 1696 | int rv = set_discard_entry(so, entry); |
| 1697 | if (rv == -1) { |
| 1698 | Py_DECREF(otherset); |
| 1699 | return NULL; |
| 1700 | } |
| 1701 | if (rv == DISCARD_NOTFOUND) { |
| 1702 | if (set_add_entry(so, entry) == -1) { |
| 1703 | Py_DECREF(otherset); |
| 1704 | return NULL; |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | Py_DECREF(otherset); |
| 1709 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | PyDoc_STRVAR(symmetric_difference_update_doc, |
| 1713 | "Update a set with the symmetric difference of itself and another."); |
| 1714 | |
| 1715 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1716 | set_symmetric_difference(PySetObject *so, PyObject *other) |
| 1717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | PyObject *rv; |
| 1719 | PySetObject *otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other); |
| 1722 | if (otherset == NULL) |
| 1723 | return NULL; |
| 1724 | rv = set_symmetric_difference_update(otherset, (PyObject *)so); |
| 1725 | if (rv == NULL) |
| 1726 | return NULL; |
| 1727 | Py_DECREF(rv); |
| 1728 | return (PyObject *)otherset; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | PyDoc_STRVAR(symmetric_difference_doc, |
| 1732 | "Return the symmetric difference of two sets as a new set.\n\ |
| 1733 | \n\ |
| 1734 | (i.e. all elements that are in exactly one of the sets.)"); |
| 1735 | |
| 1736 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1737 | set_xor(PySetObject *so, PyObject *other) |
| 1738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
| 1740 | Py_INCREF(Py_NotImplemented); |
| 1741 | return Py_NotImplemented; |
| 1742 | } |
| 1743 | return set_symmetric_difference(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | static PyObject * |
| 1747 | set_ixor(PySetObject *so, PyObject *other) |
| 1748 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1749 | PyObject *result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | if (!PyAnySet_Check(other)) { |
| 1752 | Py_INCREF(Py_NotImplemented); |
| 1753 | return Py_NotImplemented; |
| 1754 | } |
| 1755 | result = set_symmetric_difference_update(so, other); |
| 1756 | if (result == NULL) |
| 1757 | return NULL; |
| 1758 | Py_DECREF(result); |
| 1759 | Py_INCREF(so); |
| 1760 | return (PyObject *)so; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | static PyObject * |
| 1764 | set_issubset(PySetObject *so, PyObject *other) |
| 1765 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | setentry *entry; |
| 1767 | Py_ssize_t pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1769 | if (!PyAnySet_Check(other)) { |
| 1770 | PyObject *tmp, *result; |
| 1771 | tmp = make_new_set(&PySet_Type, other); |
| 1772 | if (tmp == NULL) |
| 1773 | return NULL; |
| 1774 | result = set_issubset(so, tmp); |
| 1775 | Py_DECREF(tmp); |
| 1776 | return result; |
| 1777 | } |
| 1778 | if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other)) |
| 1779 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | while (set_next(so, &pos, &entry)) { |
| 1782 | int rv = set_contains_entry((PySetObject *)other, entry); |
| 1783 | if (rv == -1) |
| 1784 | return NULL; |
| 1785 | if (!rv) |
| 1786 | Py_RETURN_FALSE; |
| 1787 | } |
| 1788 | Py_RETURN_TRUE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set."); |
| 1792 | |
| 1793 | static PyObject * |
| 1794 | set_issuperset(PySetObject *so, PyObject *other) |
| 1795 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1796 | PyObject *tmp, *result; |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 1797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | if (!PyAnySet_Check(other)) { |
| 1799 | tmp = make_new_set(&PySet_Type, other); |
| 1800 | if (tmp == NULL) |
| 1801 | return NULL; |
| 1802 | result = set_issuperset(so, tmp); |
| 1803 | Py_DECREF(tmp); |
| 1804 | return result; |
| 1805 | } |
| 1806 | return set_issubset((PySetObject *)other, (PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set."); |
| 1810 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1811 | static PyObject * |
| 1812 | set_richcompare(PySetObject *v, PyObject *w, int op) |
| 1813 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | PyObject *r1, *r2; |
Raymond Hettinger | 9f1a679 | 2005-07-31 01:16:36 +0000 | [diff] [blame] | 1815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | if(!PyAnySet_Check(w)) { |
| 1817 | Py_INCREF(Py_NotImplemented); |
| 1818 | return Py_NotImplemented; |
| 1819 | } |
| 1820 | switch (op) { |
| 1821 | case Py_EQ: |
| 1822 | if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w)) |
| 1823 | Py_RETURN_FALSE; |
| 1824 | if (v->hash != -1 && |
| 1825 | ((PySetObject *)w)->hash != -1 && |
| 1826 | v->hash != ((PySetObject *)w)->hash) |
| 1827 | Py_RETURN_FALSE; |
| 1828 | return set_issubset(v, w); |
| 1829 | case Py_NE: |
| 1830 | r1 = set_richcompare(v, w, Py_EQ); |
| 1831 | if (r1 == NULL) |
| 1832 | return NULL; |
| 1833 | r2 = PyBool_FromLong(PyObject_Not(r1)); |
| 1834 | Py_DECREF(r1); |
| 1835 | return r2; |
| 1836 | case Py_LE: |
| 1837 | return set_issubset(v, w); |
| 1838 | case Py_GE: |
| 1839 | return set_issuperset(v, w); |
| 1840 | case Py_LT: |
| 1841 | if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w)) |
| 1842 | Py_RETURN_FALSE; |
| 1843 | return set_issubset(v, w); |
| 1844 | case Py_GT: |
| 1845 | if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w)) |
| 1846 | Py_RETURN_FALSE; |
| 1847 | return set_issuperset(v, w); |
| 1848 | } |
| 1849 | Py_INCREF(Py_NotImplemented); |
| 1850 | return Py_NotImplemented; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1853 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1854 | set_add(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1855 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | if (set_add_key(so, key) == -1) |
| 1857 | return NULL; |
| 1858 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | PyDoc_STRVAR(add_doc, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1862 | "Add an element to a set.\n\ |
| 1863 | \n\ |
| 1864 | This has no effect if the element is already present."); |
| 1865 | |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1866 | static int |
| 1867 | set_contains(PySetObject *so, PyObject *key) |
| 1868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | PyObject *tmpkey; |
| 1870 | int rv; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | rv = set_contains_key(so, key); |
| 1873 | if (rv == -1) { |
| 1874 | if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1875 | return -1; |
| 1876 | PyErr_Clear(); |
Raymond Hettinger | 38bf2cc | 2010-08-06 09:52:17 +0000 | [diff] [blame] | 1877 | tmpkey = make_new_set(&PyFrozenSet_Type, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | if (tmpkey == NULL) |
| 1879 | return -1; |
Petri Lehtinen | 5acc27e | 2011-10-30 13:56:41 +0200 | [diff] [blame] | 1880 | rv = set_contains_key(so, tmpkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1881 | Py_DECREF(tmpkey); |
| 1882 | } |
| 1883 | return rv; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | static PyObject * |
| 1887 | set_direct_contains(PySetObject *so, PyObject *key) |
| 1888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | long result; |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | result = set_contains(so, key); |
| 1892 | if (result == -1) |
| 1893 | return NULL; |
| 1894 | return PyBool_FromLong(result); |
Raymond Hettinger | ce8185e | 2005-08-13 09:28:48 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x."); |
| 1898 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1899 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1900 | set_remove(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | PyObject *tmpkey; |
| 1903 | int rv; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 1904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1905 | rv = set_discard_key(so, key); |
| 1906 | if (rv == -1) { |
| 1907 | if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1908 | return NULL; |
| 1909 | PyErr_Clear(); |
Raymond Hettinger | 38bf2cc | 2010-08-06 09:52:17 +0000 | [diff] [blame] | 1910 | tmpkey = make_new_set(&PyFrozenSet_Type, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1911 | if (tmpkey == NULL) |
| 1912 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | rv = set_discard_key(so, tmpkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1914 | Py_DECREF(tmpkey); |
| 1915 | if (rv == -1) |
| 1916 | return NULL; |
| 1917 | } |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 1918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | if (rv == DISCARD_NOTFOUND) { |
| 1920 | set_key_error(key); |
| 1921 | return NULL; |
| 1922 | } |
| 1923 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | PyDoc_STRVAR(remove_doc, |
| 1927 | "Remove an element from a set; it must be a member.\n\ |
| 1928 | \n\ |
| 1929 | If the element is not a member, raise a KeyError."); |
| 1930 | |
| 1931 | static PyObject * |
Raymond Hettinger | 06d8cf8 | 2005-07-31 15:36:06 +0000 | [diff] [blame] | 1932 | set_discard(PySetObject *so, PyObject *key) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1933 | { |
Benjamin Peterson | 2b50a01 | 2011-10-30 14:24:44 -0400 | [diff] [blame] | 1934 | PyObject *tmpkey; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | int rv; |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | rv = set_discard_key(so, key); |
| 1938 | if (rv == -1) { |
| 1939 | if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1940 | return NULL; |
| 1941 | PyErr_Clear(); |
Raymond Hettinger | 38bf2cc | 2010-08-06 09:52:17 +0000 | [diff] [blame] | 1942 | tmpkey = make_new_set(&PyFrozenSet_Type, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1943 | if (tmpkey == NULL) |
| 1944 | return NULL; |
Petri Lehtinen | e0aa803 | 2011-10-30 14:31:27 +0200 | [diff] [blame] | 1945 | rv = set_discard_key(so, tmpkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1946 | Py_DECREF(tmpkey); |
Petri Lehtinen | e0aa803 | 2011-10-30 14:31:27 +0200 | [diff] [blame] | 1947 | if (rv == -1) |
| 1948 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | } |
| 1950 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | PyDoc_STRVAR(discard_doc, |
| 1954 | "Remove an element from a set if it is a member.\n\ |
| 1955 | \n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | If the element is not a member, do nothing."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1957 | |
| 1958 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1959 | set_reduce(PySetObject *so) |
| 1960 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1963 | keys = PySequence_List((PyObject *)so); |
| 1964 | if (keys == NULL) |
| 1965 | goto done; |
| 1966 | args = PyTuple_Pack(1, keys); |
| 1967 | if (args == NULL) |
| 1968 | goto done; |
| 1969 | dict = PyObject_GetAttrString((PyObject *)so, "__dict__"); |
| 1970 | if (dict == NULL) { |
| 1971 | PyErr_Clear(); |
| 1972 | dict = Py_None; |
| 1973 | Py_INCREF(dict); |
| 1974 | } |
| 1975 | result = PyTuple_Pack(3, Py_TYPE(so), args, dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1976 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1977 | Py_XDECREF(args); |
| 1978 | Py_XDECREF(keys); |
| 1979 | Py_XDECREF(dict); |
| 1980 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 1984 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1985 | static PyObject * |
| 1986 | set_sizeof(PySetObject *so) |
| 1987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1990 | res = sizeof(PySetObject); |
| 1991 | if (so->table != so->smalltable) |
| 1992 | res = res + (so->mask + 1) * sizeof(setentry); |
| 1993 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | 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] | 1997 | static int |
| 1998 | set_init(PySetObject *self, PyObject *args, PyObject *kwds) |
| 1999 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2000 | PyObject *iterable = NULL; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 2001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2002 | if (!PyAnySet_Check(self)) |
| 2003 | return -1; |
| 2004 | if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds)) |
| 2005 | return -1; |
| 2006 | if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable)) |
| 2007 | return -1; |
| 2008 | set_clear_internal(self); |
| 2009 | self->hash = -1; |
| 2010 | if (iterable == NULL) |
| 2011 | return 0; |
| 2012 | return set_update_internal(self, iterable); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2015 | static PySequenceMethods set_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | set_len, /* sq_length */ |
| 2017 | 0, /* sq_concat */ |
| 2018 | 0, /* sq_repeat */ |
| 2019 | 0, /* sq_item */ |
| 2020 | 0, /* sq_slice */ |
| 2021 | 0, /* sq_ass_item */ |
| 2022 | 0, /* sq_ass_slice */ |
| 2023 | (objobjproc)set_contains, /* sq_contains */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2024 | }; |
| 2025 | |
| 2026 | /* set object ********************************************************/ |
| 2027 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2028 | #ifdef Py_DEBUG |
| 2029 | static PyObject *test_c_api(PySetObject *so); |
| 2030 | |
| 2031 | PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\ |
| 2032 | All is well if assertions don't fail."); |
| 2033 | #endif |
| 2034 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2035 | static PyMethodDef set_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | {"add", (PyCFunction)set_add, METH_O, |
| 2037 | add_doc}, |
| 2038 | {"clear", (PyCFunction)set_clear, METH_NOARGS, |
| 2039 | clear_doc}, |
| 2040 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
| 2041 | contains_doc}, |
| 2042 | {"copy", (PyCFunction)set_copy, METH_NOARGS, |
| 2043 | copy_doc}, |
| 2044 | {"discard", (PyCFunction)set_discard, METH_O, |
| 2045 | discard_doc}, |
| 2046 | {"difference", (PyCFunction)set_difference_multi, METH_VARARGS, |
| 2047 | difference_doc}, |
| 2048 | {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS, |
| 2049 | difference_update_doc}, |
| 2050 | {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS, |
| 2051 | intersection_doc}, |
| 2052 | {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS, |
| 2053 | intersection_update_doc}, |
| 2054 | {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O, |
| 2055 | isdisjoint_doc}, |
| 2056 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 2057 | issubset_doc}, |
| 2058 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 2059 | issuperset_doc}, |
| 2060 | {"pop", (PyCFunction)set_pop, METH_NOARGS, |
| 2061 | pop_doc}, |
| 2062 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 2063 | reduce_doc}, |
| 2064 | {"remove", (PyCFunction)set_remove, METH_O, |
| 2065 | remove_doc}, |
| 2066 | {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS, |
| 2067 | sizeof_doc}, |
| 2068 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 2069 | symmetric_difference_doc}, |
| 2070 | {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O, |
| 2071 | symmetric_difference_update_doc}, |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2072 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS, |
| 2074 | test_c_api_doc}, |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2075 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | {"union", (PyCFunction)set_union, METH_VARARGS, |
| 2077 | union_doc}, |
| 2078 | {"update", (PyCFunction)set_update, METH_VARARGS, |
| 2079 | update_doc}, |
| 2080 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2081 | }; |
| 2082 | |
| 2083 | static PyNumberMethods set_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | 0, /*nb_add*/ |
| 2085 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 2086 | 0, /*nb_multiply*/ |
| 2087 | 0, /*nb_remainder*/ |
| 2088 | 0, /*nb_divmod*/ |
| 2089 | 0, /*nb_power*/ |
| 2090 | 0, /*nb_negative*/ |
| 2091 | 0, /*nb_positive*/ |
| 2092 | 0, /*nb_absolute*/ |
| 2093 | 0, /*nb_bool*/ |
| 2094 | 0, /*nb_invert*/ |
| 2095 | 0, /*nb_lshift*/ |
| 2096 | 0, /*nb_rshift*/ |
| 2097 | (binaryfunc)set_and, /*nb_and*/ |
| 2098 | (binaryfunc)set_xor, /*nb_xor*/ |
| 2099 | (binaryfunc)set_or, /*nb_or*/ |
| 2100 | 0, /*nb_int*/ |
| 2101 | 0, /*nb_reserved*/ |
| 2102 | 0, /*nb_float*/ |
| 2103 | 0, /*nb_inplace_add*/ |
| 2104 | (binaryfunc)set_isub, /*nb_inplace_subtract*/ |
| 2105 | 0, /*nb_inplace_multiply*/ |
| 2106 | 0, /*nb_inplace_remainder*/ |
| 2107 | 0, /*nb_inplace_power*/ |
| 2108 | 0, /*nb_inplace_lshift*/ |
| 2109 | 0, /*nb_inplace_rshift*/ |
| 2110 | (binaryfunc)set_iand, /*nb_inplace_and*/ |
| 2111 | (binaryfunc)set_ixor, /*nb_inplace_xor*/ |
| 2112 | (binaryfunc)set_ior, /*nb_inplace_or*/ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2113 | }; |
| 2114 | |
| 2115 | PyDoc_STRVAR(set_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2116 | "set() -> new empty set object\n\ |
| 2117 | set(iterable) -> new set object\n\ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2118 | \n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2119 | Build an unordered collection of unique elements."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2120 | |
| 2121 | PyTypeObject PySet_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2123 | "set", /* tp_name */ |
| 2124 | sizeof(PySetObject), /* tp_basicsize */ |
| 2125 | 0, /* tp_itemsize */ |
| 2126 | /* methods */ |
| 2127 | (destructor)set_dealloc, /* tp_dealloc */ |
| 2128 | 0, /* tp_print */ |
| 2129 | 0, /* tp_getattr */ |
| 2130 | 0, /* tp_setattr */ |
| 2131 | 0, /* tp_reserved */ |
| 2132 | (reprfunc)set_repr, /* tp_repr */ |
| 2133 | &set_as_number, /* tp_as_number */ |
| 2134 | &set_as_sequence, /* tp_as_sequence */ |
| 2135 | 0, /* tp_as_mapping */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2136 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | 0, /* tp_call */ |
| 2138 | 0, /* tp_str */ |
| 2139 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2140 | 0, /* tp_setattro */ |
| 2141 | 0, /* tp_as_buffer */ |
| 2142 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2143 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2144 | set_doc, /* tp_doc */ |
| 2145 | (traverseproc)set_traverse, /* tp_traverse */ |
| 2146 | (inquiry)set_clear_internal, /* tp_clear */ |
| 2147 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2148 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
| 2149 | (getiterfunc)set_iter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2150 | 0, /* tp_iternext */ |
| 2151 | set_methods, /* tp_methods */ |
| 2152 | 0, /* tp_members */ |
| 2153 | 0, /* tp_getset */ |
| 2154 | 0, /* tp_base */ |
| 2155 | 0, /* tp_dict */ |
| 2156 | 0, /* tp_descr_get */ |
| 2157 | 0, /* tp_descr_set */ |
| 2158 | 0, /* tp_dictoffset */ |
| 2159 | (initproc)set_init, /* tp_init */ |
| 2160 | PyType_GenericAlloc, /* tp_alloc */ |
| 2161 | set_new, /* tp_new */ |
| 2162 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2163 | }; |
| 2164 | |
| 2165 | /* frozenset object ********************************************************/ |
| 2166 | |
| 2167 | |
| 2168 | static PyMethodDef frozenset_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
| 2170 | contains_doc}, |
| 2171 | {"copy", (PyCFunction)frozenset_copy, METH_NOARGS, |
| 2172 | copy_doc}, |
| 2173 | {"difference", (PyCFunction)set_difference_multi, METH_VARARGS, |
| 2174 | difference_doc}, |
| 2175 | {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS, |
| 2176 | intersection_doc}, |
| 2177 | {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O, |
| 2178 | isdisjoint_doc}, |
| 2179 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 2180 | issubset_doc}, |
| 2181 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 2182 | issuperset_doc}, |
| 2183 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 2184 | reduce_doc}, |
| 2185 | {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS, |
| 2186 | sizeof_doc}, |
| 2187 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 2188 | symmetric_difference_doc}, |
| 2189 | {"union", (PyCFunction)set_union, METH_VARARGS, |
| 2190 | union_doc}, |
| 2191 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2192 | }; |
| 2193 | |
| 2194 | static PyNumberMethods frozenset_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2195 | 0, /*nb_add*/ |
| 2196 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 2197 | 0, /*nb_multiply*/ |
| 2198 | 0, /*nb_remainder*/ |
| 2199 | 0, /*nb_divmod*/ |
| 2200 | 0, /*nb_power*/ |
| 2201 | 0, /*nb_negative*/ |
| 2202 | 0, /*nb_positive*/ |
| 2203 | 0, /*nb_absolute*/ |
| 2204 | 0, /*nb_bool*/ |
| 2205 | 0, /*nb_invert*/ |
| 2206 | 0, /*nb_lshift*/ |
| 2207 | 0, /*nb_rshift*/ |
| 2208 | (binaryfunc)set_and, /*nb_and*/ |
| 2209 | (binaryfunc)set_xor, /*nb_xor*/ |
| 2210 | (binaryfunc)set_or, /*nb_or*/ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2211 | }; |
| 2212 | |
| 2213 | PyDoc_STRVAR(frozenset_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2214 | "frozenset() -> empty frozenset object\n\ |
| 2215 | frozenset(iterable) -> frozenset object\n\ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2216 | \n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2217 | Build an immutable unordered collection of unique elements."); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2218 | |
| 2219 | PyTypeObject PyFrozenSet_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2220 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2221 | "frozenset", /* tp_name */ |
| 2222 | sizeof(PySetObject), /* tp_basicsize */ |
| 2223 | 0, /* tp_itemsize */ |
| 2224 | /* methods */ |
| 2225 | (destructor)set_dealloc, /* tp_dealloc */ |
| 2226 | 0, /* tp_print */ |
| 2227 | 0, /* tp_getattr */ |
| 2228 | 0, /* tp_setattr */ |
| 2229 | 0, /* tp_reserved */ |
| 2230 | (reprfunc)set_repr, /* tp_repr */ |
| 2231 | &frozenset_as_number, /* tp_as_number */ |
| 2232 | &set_as_sequence, /* tp_as_sequence */ |
| 2233 | 0, /* tp_as_mapping */ |
| 2234 | frozenset_hash, /* tp_hash */ |
| 2235 | 0, /* tp_call */ |
| 2236 | 0, /* tp_str */ |
| 2237 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2238 | 0, /* tp_setattro */ |
| 2239 | 0, /* tp_as_buffer */ |
| 2240 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2241 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2242 | frozenset_doc, /* tp_doc */ |
| 2243 | (traverseproc)set_traverse, /* tp_traverse */ |
| 2244 | (inquiry)set_clear_internal, /* tp_clear */ |
| 2245 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
| 2246 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
| 2247 | (getiterfunc)set_iter, /* tp_iter */ |
| 2248 | 0, /* tp_iternext */ |
| 2249 | frozenset_methods, /* tp_methods */ |
| 2250 | 0, /* tp_members */ |
| 2251 | 0, /* tp_getset */ |
| 2252 | 0, /* tp_base */ |
| 2253 | 0, /* tp_dict */ |
| 2254 | 0, /* tp_descr_get */ |
| 2255 | 0, /* tp_descr_set */ |
| 2256 | 0, /* tp_dictoffset */ |
| 2257 | 0, /* tp_init */ |
| 2258 | PyType_GenericAlloc, /* tp_alloc */ |
| 2259 | frozenset_new, /* tp_new */ |
| 2260 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2261 | }; |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2262 | |
| 2263 | |
| 2264 | /***** C API functions *************************************************/ |
| 2265 | |
| 2266 | PyObject * |
| 2267 | PySet_New(PyObject *iterable) |
| 2268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | return make_new_set(&PySet_Type, iterable); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | PyObject * |
| 2273 | PyFrozenSet_New(PyObject *iterable) |
| 2274 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | return make_new_set(&PyFrozenSet_Type, iterable); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Neal Norwitz | 8c49c82 | 2006-03-04 18:41:19 +0000 | [diff] [blame] | 2278 | Py_ssize_t |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2279 | PySet_Size(PyObject *anyset) |
| 2280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | if (!PyAnySet_Check(anyset)) { |
| 2282 | PyErr_BadInternalCall(); |
| 2283 | return -1; |
| 2284 | } |
| 2285 | return PySet_GET_SIZE(anyset); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2289 | PySet_Clear(PyObject *set) |
| 2290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | if (!PySet_Check(set)) { |
| 2292 | PyErr_BadInternalCall(); |
| 2293 | return -1; |
| 2294 | } |
| 2295 | return set_clear_internal((PySetObject *)set); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | int |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2299 | PySet_Contains(PyObject *anyset, PyObject *key) |
| 2300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | if (!PyAnySet_Check(anyset)) { |
| 2302 | PyErr_BadInternalCall(); |
| 2303 | return -1; |
| 2304 | } |
| 2305 | return set_contains_key((PySetObject *)anyset, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | int |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2309 | PySet_Discard(PyObject *set, PyObject *key) |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2310 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | if (!PySet_Check(set)) { |
| 2312 | PyErr_BadInternalCall(); |
| 2313 | return -1; |
| 2314 | } |
| 2315 | return set_discard_key((PySetObject *)set, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | int |
Christian Heimes | fd66e51 | 2008-01-29 12:18:50 +0000 | [diff] [blame] | 2319 | PySet_Add(PyObject *anyset, PyObject *key) |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | if (!PySet_Check(anyset) && |
| 2322 | (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) { |
| 2323 | PyErr_BadInternalCall(); |
| 2324 | return -1; |
| 2325 | } |
| 2326 | return set_add_key((PySetObject *)anyset, key); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2329 | int |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2330 | _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] | 2331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | setentry *entry; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | if (!PyAnySet_Check(set)) { |
| 2335 | PyErr_BadInternalCall(); |
| 2336 | return -1; |
| 2337 | } |
| 2338 | if (set_next((PySetObject *)set, pos, &entry) == 0) |
| 2339 | return 0; |
| 2340 | *key = entry->key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2341 | *hash = entry->hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | return 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2345 | PyObject * |
| 2346 | PySet_Pop(PyObject *set) |
| 2347 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | if (!PySet_Check(set)) { |
| 2349 | PyErr_BadInternalCall(); |
| 2350 | return NULL; |
| 2351 | } |
| 2352 | return set_pop((PySetObject *)set); |
Raymond Hettinger | beb3101 | 2005-08-16 03:47:52 +0000 | [diff] [blame] | 2353 | } |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2354 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2355 | int |
| 2356 | _PySet_Update(PyObject *set, PyObject *iterable) |
| 2357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | if (!PySet_Check(set)) { |
| 2359 | PyErr_BadInternalCall(); |
| 2360 | return -1; |
| 2361 | } |
| 2362 | return set_update_internal((PySetObject *)set, iterable); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2363 | } |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2364 | |
| 2365 | #ifdef Py_DEBUG |
| 2366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2367 | /* Test code to be called with any three element set. |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2368 | Returns True and original set is restored. */ |
| 2369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | #define assertRaises(call_return_value, exception) \ |
| 2371 | do { \ |
| 2372 | assert(call_return_value); \ |
| 2373 | assert(PyErr_ExceptionMatches(exception)); \ |
| 2374 | PyErr_Clear(); \ |
| 2375 | } while(0) |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2376 | |
| 2377 | static PyObject * |
| 2378 | test_c_api(PySetObject *so) |
| 2379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | Py_ssize_t count; |
| 2381 | char *s; |
| 2382 | Py_ssize_t i; |
| 2383 | PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x; |
| 2384 | PyObject *ob = (PyObject *)so; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2385 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | PyObject *str; |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | /* Verify preconditions */ |
| 2389 | assert(PyAnySet_Check(ob)); |
| 2390 | assert(PyAnySet_CheckExact(ob)); |
| 2391 | assert(!PyFrozenSet_CheckExact(ob)); |
Victor Stinner | 08b36bd | 2010-03-13 00:19:17 +0000 | [diff] [blame] | 2392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | /* so.clear(); so |= set("abc"); */ |
| 2394 | str = PyUnicode_FromString("abc"); |
| 2395 | if (str == NULL) |
| 2396 | return NULL; |
| 2397 | set_clear_internal(so); |
| 2398 | if (set_update_internal(so, str) == -1) { |
| 2399 | Py_DECREF(str); |
| 2400 | return NULL; |
| 2401 | } |
| 2402 | Py_DECREF(str); |
Victor Stinner | 08b36bd | 2010-03-13 00:19:17 +0000 | [diff] [blame] | 2403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2404 | /* Exercise type/size checks */ |
| 2405 | assert(PySet_Size(ob) == 3); |
| 2406 | assert(PySet_GET_SIZE(ob) == 3); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | /* Raise TypeError for non-iterable constructor arguments */ |
| 2409 | assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError); |
| 2410 | assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2412 | /* Raise TypeError for unhashable key */ |
| 2413 | dup = PySet_New(ob); |
| 2414 | assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError); |
| 2415 | assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError); |
| 2416 | assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2418 | /* Exercise successful pop, contains, add, and discard */ |
| 2419 | elem = PySet_Pop(ob); |
| 2420 | assert(PySet_Contains(ob, elem) == 0); |
| 2421 | assert(PySet_GET_SIZE(ob) == 2); |
| 2422 | assert(PySet_Add(ob, elem) == 0); |
| 2423 | assert(PySet_Contains(ob, elem) == 1); |
| 2424 | assert(PySet_GET_SIZE(ob) == 3); |
| 2425 | assert(PySet_Discard(ob, elem) == 1); |
| 2426 | assert(PySet_GET_SIZE(ob) == 2); |
| 2427 | assert(PySet_Discard(ob, elem) == 0); |
| 2428 | assert(PySet_GET_SIZE(ob) == 2); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2430 | /* Exercise clear */ |
| 2431 | dup2 = PySet_New(dup); |
| 2432 | assert(PySet_Clear(dup2) == 0); |
| 2433 | assert(PySet_Size(dup2) == 0); |
| 2434 | Py_DECREF(dup2); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | /* Raise SystemError on clear or update of frozen set */ |
| 2437 | f = PyFrozenSet_New(dup); |
| 2438 | assertRaises(PySet_Clear(f) == -1, PyExc_SystemError); |
| 2439 | assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError); |
| 2440 | assert(PySet_Add(f, elem) == 0); |
| 2441 | Py_INCREF(f); |
| 2442 | assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError); |
| 2443 | Py_DECREF(f); |
| 2444 | Py_DECREF(f); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2446 | /* Exercise direct iteration */ |
| 2447 | i = 0, count = 0; |
| 2448 | while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) { |
| 2449 | s = _PyUnicode_AsString(x); |
| 2450 | assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c')); |
| 2451 | count++; |
| 2452 | } |
| 2453 | assert(count == 3); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2455 | /* Exercise updates */ |
| 2456 | dup2 = PySet_New(NULL); |
| 2457 | assert(_PySet_Update(dup2, dup) == 0); |
| 2458 | assert(PySet_Size(dup2) == 3); |
| 2459 | assert(_PySet_Update(dup2, dup) == 0); |
| 2460 | assert(PySet_Size(dup2) == 3); |
| 2461 | Py_DECREF(dup2); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2463 | /* Raise SystemError when self argument is not a set or frozenset. */ |
| 2464 | t = PyTuple_New(0); |
| 2465 | assertRaises(PySet_Size(t) == -1, PyExc_SystemError); |
| 2466 | assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError); |
| 2467 | Py_DECREF(t); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | /* Raise SystemError when self argument is not a set. */ |
| 2470 | f = PyFrozenSet_New(dup); |
| 2471 | assert(PySet_Size(f) == 3); |
| 2472 | assert(PyFrozenSet_CheckExact(f)); |
| 2473 | assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError); |
| 2474 | assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError); |
| 2475 | Py_DECREF(f); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2477 | /* Raise KeyError when popping from an empty set */ |
| 2478 | assert(PyNumber_InPlaceSubtract(ob, ob) == ob); |
| 2479 | Py_DECREF(ob); |
| 2480 | assert(PySet_GET_SIZE(ob) == 0); |
| 2481 | assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2483 | /* Restore the set from the copy using the PyNumber API */ |
| 2484 | assert(PyNumber_InPlaceOr(ob, dup) == ob); |
| 2485 | Py_DECREF(ob); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | /* Verify constructors accept NULL arguments */ |
| 2488 | f = PySet_New(NULL); |
| 2489 | assert(f != NULL); |
| 2490 | assert(PySet_GET_SIZE(f) == 0); |
| 2491 | Py_DECREF(f); |
| 2492 | f = PyFrozenSet_New(NULL); |
| 2493 | assert(f != NULL); |
| 2494 | assert(PyFrozenSet_CheckExact(f)); |
| 2495 | assert(PySet_GET_SIZE(f) == 0); |
| 2496 | Py_DECREF(f); |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2498 | Py_DECREF(elem); |
| 2499 | Py_DECREF(dup); |
| 2500 | Py_RETURN_TRUE; |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 2503 | #undef assertRaises |
| 2504 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 2505 | #endif |