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