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