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