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