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