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