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