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