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