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