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