ASL
HashMap< K, T > Class Template Reference

Detailed Description

template<class K, class T>
class asl::HashMap< K, T >

This class implements a hash map, an unordered map of keys to values.

It is similar to class Map but elements will not keep a defined order. There must be a global function hash(const K&) for the key type K (there is a default hash function that may fit). Inserting and finding elements is usually faster than in a Map. The class has reference counting as all containers.

HashMap<String, float> constants;
constants["pi"] = 3.1415927;
float a = constants["pi"] * sqr(radius);
if(constants.has("pi"))
{...}
T sqr(T x)
Returns x squared.
Definition: defs.h:193

The contents can be iterated with range-based for in C++11:

for(auto& e : constants)
{
cout << "Contstant " << *e.key << " has value: " << e.value << endl;
}

Or with the foreach2 macro loop in older compilers:

foreach2(String& name, float value, constants)
{
cout << "Contstant " << *name << " has value: " << value << endl;
}
#define foreach2(key, variable, set)
A for loop for associative containers resembling C++17 range-based for with structured binding.
Definition: foreach1.h:72

#include <HashMap.h>

Public Member Functions

HashMap clone () const
 Returns an independent copy of this map.
 
void clear ()
 Clears the map removing all elements.
 
const T & operator[] (const K &key) const
 Returns a reference to the value associated to the given key, the key has to exist.
 
T & operator[] (const K &key)
 Returns a reference to the value associated to the given key, creating one if the key does not exist.
 
T * find (const K &key)
 Returns a pointer to the element with key key or a null pointer if it is not found.
 
const T & get (const K &key, const T &def) const
 Returns the value for the given key or the value def if it is not found.
 
void remove (const K &key)
 Removes the given key.
 
bool has (const K &key) const
 Checks if the given key exists in the map.
 
int length () const
 Returns the number of elements in the map.
 
bool operator== (const HashMap &b) const
 Returns true if both maps are equal (equal keys and values)
 

The documentation for this class was generated from the following file: