#include "map.h"

class Map<KeyType,ValueType>

The Map class maintains an association between keys and values. The types used for keys and values are specified using templates, which makes it possible to use this structure with any data type.
Constructor
Map()
Map(cmpFn) 
Initializes a new empty map that associates keys and values of the specified types.
Methods
size() Returns the number of entries in this map.
isEmpty() Returns true if this map contains no entries.
put(key, value) Associates key with value in this map.
get(key) Returns the value associated with key in this map.
containsKey(key) Returns true if there is an entry for key in this map.
remove(key) Removes any entry for key from this map.
clear() Removes all entries from this map.
mapAll(fn)
mapAll(fn, data) 
Iterates through the keys in this map and calls fn(key) for each one.
Operator
map[key] Selects the value associated with key.
Macro
foreach(KeyType key in map) Iterates over the keys in the map.

Constructor detail


Map(int (*cmpFn)(KeyType, KeyType) = operatorCmp);
Initializes a new empty map that associates keys and values of the specified types. The optional argument specifies a comparison function, which is called to compare data values. This argument is typically omitted, in which case the implementation uses the operatorCmp function from cmpfn.h, which applies the built-in operators < and == to determine the ordering.

Usage:

Map<KeyType,ValueType> map;
Map<KeyType,ValueType> map(cmpFn);

Method detail


int size();
Returns the number of entries in this map.

Usage:

int nEntries = map.size();

bool isEmpty();
Returns true if this map contains no entries.

Usage:

if (map.isEmpty()) . . .

void put(KeyType key, ValueType value);
Associates key with value in this map. Any previous value associated with key is replaced by the new value.

Usage:

map.put(key, value);

ValueType get(KeyType key);
Returns the value associated with key in this map. If key is not found, the get method signals an error.

Usage:

ValueType value = map.get(key);

bool containsKey(KeyType key);
Returns true if there is an entry for key in this map.

Usage:

if (map.containsKey(key)) . . .

void remove(KeyType key);
Removes any entry for key from this map.

Usage:

map.remove(key);

void clear();
Removes all entries from this map.

Usage:

map.clear();

void mapAll(void (*fn)(KeyType key));
void mapAll(void (*fn)(KeyType, ClientDataType &), ClientDataType & data);
Iterates through the keys in this map and calls fn(key) for each one. The keys are processed in ascending order, as defined by the comparison function. The second form of the call allows the client to pass a data value of any type to the callback function.

Usage:

map.mapAll(fn);
map.mapAll(fn, data);

Operator detail


ValueType & operator[](KeyType key);
Selects the value associated with key. This syntax makes it easy to think of a map as an "associative array" indexed by the key type. If key is already present in the map, this function returns a reference to its associated value. If key is not present in the map, a new entry is created whose value is set to the default for the value type.

Usage:

map[key]

Macro detail


foreach (KeyType key in map) . . .
Iterates over the keys in the map. The keys are processed in ascending order, as defined by the comparison function.