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