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