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