The SimpleCxxLib package


#include "hashmap.h"

class HashMap<KeyType,ValueType>

This class implements an efficient association between keys and values. This class is identical to the Map class except for the fact that it uses a hash table as its underlying representation. Although the HashMap class operates in constant time, the iterator for HashMap returns the values in a seemingly random order.
Constructor
HashMap() Initializes a new empty map that associates keys and values of the specified types.
Methods
clear() Removes all entries from this map.
containsKey(key) Returns true if there is an entry for key in this map.
get(key) Returns the value associated with key in this map.
isEmpty() Returns true if this map contains no entries.
mapAll(fn) Iterates through the map entries and calls fn(key, value) for each one.
put(key, value) Associates key with value in this map.
remove(key) Removes any entry for key from this map.
size() Returns the number of entries in this map.
toString() Converts the map to a printable string representation.
Operator
map[key] Selects the value associated with key.

Constructor detail


HashMap();
Initializes a new empty map that associates keys and values of the specified types. The type used for the key must define the == operator, and there must be a free function with the following signature:
   int hashCode(KeyType key);
that returns a positive integer determined by the key. This interface exports hashCode functions for string and the C++ primitive types.

Usage:

HashMap<KeyType,ValueType> map;
HashMap<KeyType,ValueType> map = {{ k1, v1}, { k2, v2 }};

Method detail


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

Usage:

int nEntries = map.size();

bool isEmpty() const;
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) const;
Returns the value associated with key in this map. If key is not found, get returns the default value for ValueType.

Usage:

ValueType value = map.get(key);

bool containsKey(KeyType key) const;
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();

string toString();
Converts the map to a printable string representation.

Usage:

string str = map.toString();

void mapAll(void (*fn)(KeyType, ValueType)) const;
void mapAll(void (*fn)(const KeyType &, const ValueType &)) const;
void mapAll(FunctorType fn) const;
Iterates through the map entries and calls fn(key, value) for each one. The keys are processed in an undetermined order.

Usage:

map.mapAll(fn);

Operator detail


ValueType & operator[](KeyType key);
ValueType operator[](KeyType key) const;
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]