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