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