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