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