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