From: Ben Pfaff Date: Tue, 5 May 2009 04:53:07 +0000 (-0700) Subject: sparse-array: Simplify code slightly. X-Git-Tag: v0.7.3~95 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02d26302aea9cb2c25c8cbb50bd120674de1f862;p=pspp-builds.git sparse-array: Simplify code slightly. Instead of checking whether the key is in range in each caller of find_leaf_node, do it in find_leaf_node itself. This also allows checking the cache before checking whether the key is in range, which might be an optimization. --- diff --git a/src/libpspp/sparse-array.c b/src/libpspp/sparse-array.c index ebc81ff4..28398d5c 100644 --- a/src/libpspp/sparse-array.c +++ b/src/libpspp/sparse-array.c @@ -294,12 +294,9 @@ sparse_array_insert (struct sparse_array *spar, unsigned long int key) void * sparse_array_get (const struct sparse_array *spar, unsigned long int key) { - if (index_in_range (spar, key)) - { - struct leaf_node *leaf = find_leaf_node (spar, key); - if (leaf != NULL && is_in_use (leaf, key)) - return leaf_element (spar, leaf, key); - } + struct leaf_node *leaf = find_leaf_node (spar, key); + if (leaf != NULL && is_in_use (leaf, key)) + return leaf_element (spar, leaf, key); return NULL; } @@ -319,9 +316,6 @@ sparse_array_remove (struct sparse_array *spar, unsigned long int key) union pointer *p; int level; - if (!index_in_range (spar, key)) - return false; - /* Find and free element in leaf. */ leaf = find_leaf_node (spar, key); if (leaf == NULL || !is_in_use (leaf, key)) @@ -587,12 +581,13 @@ find_leaf_node (const struct sparse_array *spar_, unsigned long int key) const union pointer *p; int level; - assert (index_in_range (spar, key)); - /* Check the cache first. */ if (key >> BITS_PER_LEVEL == spar->cache_ofs) return spar->cache; + if (!index_in_range (spar, key)) + return NULL; + /* Descend through internal nodes. */ p = &spar->root; for (level = spar->height - 1; level > 0; level--)