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