#include "hashmap.h"

class HashMap<KeyTypeValueType>

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()  O(1) Initializes a new empty map that associates keys and values of the specified types.
Methods
clear()  O(N) Removes all entries from this map.
containsKey(key)  O(1) Returns true if there is an entry for key in this map.
equals(map)  O(N log N) Returns true if the two maps contain the same elements.
get(key)  O(1) Returns the value associated with key in this map.
isEmpty()  O(1) Returns true if this map contains no entries.
keys()  O(N) Returns a Vector copy of all keys in this map.
mapAll(fn)  O(N) Iterates through the map entries and calls fn(key, value) for each one.
put(keyvalue)  O(1) Associates key with value in this map.
remove(key)  O(1) Removes any entry for key from this map.
size()  O(1) Returns the number of entries in this map.
toString()  O(N) Converts the map to a printable string representation.
values()  O(N) Returns a Vector copy of all values in this map.
Operators
map[key]  O(1) Selects the value associated with key.
map1 == map1  O(N) Returns true if map1 and map2 contain the same elements.
map1 != map2  O(N) Returns true if map1 and map2 are different.
ostream << map O(N) Outputs the contents of the map to the given output stream.
istream >> map O(N) Reads the contents of the given input stream into the map.

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;

Method detail


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

Usage:

map.clear();

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

Usage:

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

bool equals(const HashMap& map) const;
Returns true if the two maps contain exactly the same set of key/value pairs. Identical in behavior to the == operator.

Usage:

if (map.equals(map2)) ...

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 isEmpty() const;
Returns true if this map contains no entries.

Usage:

if (map.isEmpty()) ...

Vector<KeyType> keys() const;
Returns a Vector copy of all keys in this map. The elements will appear in the same order that a for-each loop over the map would produce them, though that order is seemingly random for hash maps. Because a map cannot contain duplicate keys, the elements of the vector will be unique.

Usage:

Vector<KeyType> keys = map.keys();

Available since: 2014/02/01 version of C++ library


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);

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);

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

Usage:

map.remove(key);

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

Usage:

int nEntries = map.size();

string toString();
Converts the map to a printable string representation, such as "{k1:v1, k2:v2, k3:v3}". The key/value pairs appear in an unpredictable order.

Usage:

string str = map.toString();

Vector<ValueType> values() const;
Returns a Vector copy of all values in this map. The elements will appear in the same order that a for-each loop over the map would produce them, though that order is seemingly random for hash maps. A map can contain duplicate values, so the elements of the vector are not guaranteed to be unique.

Usage:

Vector<ValueType> values = map.values();

Available since: 2014/02/01 version of C++ library


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]