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