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