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