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