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