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