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