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