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