Maps

Author: Adrián Pérez <aperez@igalia.com>
Copyright: 2008-2009 Igalia S.L.
License:GPL3

Abstract

Provides a dictionary-like data structure for use in shell code.

Contents

Introduction

This module adds functions to manage pseudo hash tables using arrays. It it not as efficient as a proper hash table implementation, but that would be difficult to do in shell code and this suffices for a vast amount of cases where performance is not critical. In fact, when maps are used to store a small amount of items, they may even be faster.

An array containing paired items is considered to be a map, like the following example:

mymap=(
    "key1"    "value1"
    "key2"    "value2"
    # ...
)

Obviously enough, a map must contain an even number of elements.

Warning

This map implementation is neither CPU and memory efficient. Each algorithm has O(n) complexity. This is not considered to be an error.

Functions

map_get

map_get map key

Obtains the value associated with a particular key. The exit status is non-zero if the specified key does not exist.

map_key

map_key map value

Finds the first key which has the given value associated. Exits status is non-zero if the item is not found.

map_keys

map_keys map

Gets a list of all the keys in a map. Exit status is non-zero if the map is empty.

map_size

map_size map

Calculates the number of elements present in a map.

map_item

map_item map index

Returns item at the given position of the map. Usually you do not need to use this function, as it will be used internally by the rest of the functions of the module. It could be useful to build additional features in other modules.

map_values

map_values map

Obtains a list of all the values in a map. Exit status is non-zero if the map is empty.