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