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