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