Hash tables

Author: Adrian Perez <aperez@igalia.com>
Copyright: 2008 Igalia S.L.
License:GPL v2

Abstract

Efficient dictionary-like structures i.e. a hash tables.

Contents

hash_new

hash_new

Creates a new hash table. Returns a has table identifier which is needed as first argument to all the other functions of the module.

hash_set

hash_set hash_id key value

Associates a key with a value. The hash_id parameter must be an identifier obtained with hash_new.

hash_get

hash_get hash_id key

Gets the value associated with a key. The hash_id parameter must be an identifier obtained with hash_new. The exit status is non-zero when an element is not found.

hash_del

hash_del hash_id key

Deletes a (key, value) pair from a hash table. The hash_id parameter must be an identifier obtained with hash_new.

hash_has

hash_has hash_id key

Checks whether a given key is set in a hash table. The hash_id parameter must be an identifier obtained with hash_new. The exit status is zero when the keys exists, and non-zero otherwise.

hash_keys_iter

hash_keys_iter hash_id callback

Iterates over all keys of the given hash table hash_id. The callback will be called with the hash_id as first argument

Warning

Do not add or remove keys while iterating over the elements, behaviour is undefined.

hash_keys

hash_keys hash_id

Gathers all keys of a given hash table and print one key per line.

hash_keys_escaped

hash_keys_escaped hash_id

Gathers all keys of a given hash table and print one key per line. Unlike hash_keys, elements are escaped so you can reuse the output for gathering the key names into an array:

h=$(hash_new)
for i in $(seq 10) ; do
    hash_set $h "key $i" "value $i"
done
h_heys=( $(hash_keys_escaped $h) )

hash_clear

hash_clear hash_id

Empties a hash table. The hash_id parameter must be a valid identifier obtained with hash_new.