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