diff options
author | Raymond Hettinger <python@rcn.com> | 2014-12-27 07:08:58 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2014-12-27 07:08:58 (GMT) |
commit | 8edf27c1340dc3928e3a52e0c61a4b56db18c7ad (patch) | |
tree | 05005571f8dbdf99aab644161f74f58f72c415ee /Objects | |
parent | 08e3dc0ad6b6637628e4b19425d3f0fccf4aeb9f (diff) | |
download | cpython-8edf27c1340dc3928e3a52e0c61a4b56db18c7ad.zip cpython-8edf27c1340dc3928e3a52e0c61a4b56db18c7ad.tar.gz cpython-8edf27c1340dc3928e3a52e0c61a4b56db18c7ad.tar.bz2 |
Small clean-up. Factor-out common code for add, contains, and discard function pairs.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/setobject.c | 70 |
1 files changed, 27 insertions, 43 deletions
diff --git a/Objects/setobject.c b/Objects/setobject.c index 30645e2..4fba897 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -362,8 +362,8 @@ set_add_entry(PySetObject *so, setentry *entry) static int set_add_key(PySetObject *so, PyObject *key) { + setentry entry; Py_hash_t hash; - Py_ssize_t n_used; if (!PyUnicode_CheckExact(key) || (hash = ((PyASCIIObject *) key)->hash) == -1) { @@ -371,16 +371,9 @@ set_add_key(PySetObject *so, PyObject *key) if (hash == -1) return -1; } - assert(so->fill <= so->mask); /* at least one empty slot */ - n_used = so->used; - Py_INCREF(key); - if (set_insert_key(so, key, hash) == -1) { - Py_DECREF(key); - return -1; - } - if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2)) - return 0; - return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4); + entry.key = key; + entry.hash = hash; + return set_add_entry(so, &entry); } #define DISCARD_NOTFOUND 0 @@ -388,7 +381,8 @@ set_add_key(PySetObject *so, PyObject *key) static int set_discard_entry(PySetObject *so, setentry *oldentry) -{ setentry *entry; +{ + setentry *entry; PyObject *old_key; entry = (so->lookup)(so, oldentry->key, oldentry->hash); @@ -406,9 +400,8 @@ set_discard_entry(PySetObject *so, setentry *oldentry) static int set_discard_key(PySetObject *so, PyObject *key) { + setentry entry; Py_hash_t hash; - setentry *entry; - PyObject *old_key; assert (PyAnySet_Check(so)); @@ -418,16 +411,9 @@ set_discard_key(PySetObject *so, PyObject *key) if (hash == -1) return -1; } - entry = (so->lookup)(so, key, hash); - if (entry == NULL) - return -1; - if (entry->key == NULL || entry->key == dummy) - return DISCARD_NOTFOUND; - old_key = entry->key; - entry->key = dummy; - so->used--; - Py_DECREF(old_key); - return DISCARD_FOUND; + entry.key = key; + entry.hash = hash; + return set_discard_entry(so, &entry); } static void @@ -640,25 +626,6 @@ set_merge(PySetObject *so, PyObject *otherset) } static int -set_contains_key(PySetObject *so, PyObject *key) -{ - Py_hash_t hash; - setentry *entry; - - if (!PyUnicode_CheckExact(key) || - (hash = ((PyASCIIObject *) key)->hash) == -1) { - hash = PyObject_Hash(key); - if (hash == -1) - return -1; - } - entry = (so->lookup)(so, key, hash); - if (entry == NULL) - return -1; - key = entry->key; - return key != NULL && key != dummy; -} - -static int set_contains_entry(PySetObject *so, setentry *entry) { PyObject *key; @@ -671,6 +638,23 @@ set_contains_entry(PySetObject *so, setentry *entry) return key != NULL && key != dummy; } +static int +set_contains_key(PySetObject *so, PyObject *key) +{ + setentry entry; + Py_hash_t hash; + + if (!PyUnicode_CheckExact(key) || + (hash = ((PyASCIIObject *) key)->hash) == -1) { + hash = PyObject_Hash(key); + if (hash == -1) + return -1; + } + entry.key = key; + entry.hash = hash; + return set_contains_entry(so, &entry); +} + static PyObject * set_pop(PySetObject *so) { |