ROSE
0.11.96.11
|
Container associating values with keys.
This container is similar to TL's unordered_map
in that it stores a value for each key using a hash-based mechanism, although it works for C++ versions before C++11. The naming scheme is similar to other Sawyer containers. If you're used to the STL, the main differences are described in the documentation for the Sawyer::Container name space.
See also, Map.
#include <HashMap.h>
Classes | |
class | ConstKeyIterator |
Forward iterator over keys. More... | |
class | ConstNodeIterator |
Forward iterator over key/value nodes. More... | |
class | ConstValueIterator |
Forward iterator over values. More... | |
class | Node |
Type for stored nodes. More... | |
class | NodeIterator |
Forward iterator over key/value nodes. More... | |
class | ValueIterator |
Forward iterator over values. More... | |
Public Types | |
typedef K | Key |
Type of keys. | |
typedef T | Value |
Type of values. | |
typedef H | Hasher |
Functor for hashing keys. | |
typedef C | Comparator |
Functor for comparing keys. | |
typedef A | Allocator |
Functor for allocating node memory. | |
Public Member Functions | |
HashMap () | |
Default constructor. More... | |
HashMap (size_t n, const Hasher &hasher=Hasher(), const Comparator &cmp=Comparator(), const Allocator &alloc=Allocator()) | |
Constructs a hash map with at least n buckets. | |
HashMap (const HashMap &other) | |
Copy constructor. | |
template<class K2 , class T2 , class H2 , class C2 , class A2 > | |
HashMap (const HashMap< K2, T2, H2, C2, A2 > &other) | |
Copy constructor. More... | |
template<class K2 , class T2 , class H2 , class C2 , class A2 > | |
HashMap & | operator= (const HashMap< K2, T2, H2, C2, A2 > &other) |
Assignment operator. | |
bool | isEmpty () const |
Determine whether this container is empty. More... | |
size_t | size () const |
Number of nodes, keys, or values in this container. More... | |
size_t | nBuckets () const |
Number of buckets. | |
double | loadFactor () const |
Average number of nodes per bucket. | |
void | rehash (size_t nBuckets) |
Change number of buckets. | |
bool | exists (const Key &key) const |
Determine if a key exists. More... | |
Optional< Value > | getOptional (const Key &key) const |
Lookup and return a value or nothing. More... | |
const Value & | getOrDefault (const Key &key) const |
Lookup and return a value or a default. More... | |
HashMap & | insert (const Key &key, const Value &value) |
Insert or update a key/value pair. More... | |
HashMap & | insertDefault (const Key &key) |
Insert or update a key with a default value. More... | |
Value & | insertMaybe (const Key &key, const Value &value) |
Conditionally insert a new key/value pair. More... | |
Value & | insertMaybeDefault (const Key &key) |
Conditionally insert a new key with default value. More... | |
template<class OtherNodeIterator > | |
HashMap & | insertMaybeMultiple (const boost::iterator_range< OtherNodeIterator > &range) |
Conditionally insert multiple key/value pairs. More... | |
HashMap & | clear () |
Remove all nodes. More... | |
HashMap & | erase (const Key &key) |
Remove a node with specified key. More... | |
template<class OtherKeyIterator > | |
HashMap & | eraseMultiple (const boost::iterator_range< OtherKeyIterator > &range) |
Remove keys stored in another HashMap. More... | |
boost::iterator_range< NodeIterator > | nodes () |
Iterators for container nodes. More... | |
boost::iterator_range< ConstNodeIterator > | nodes () const |
Iterators for container nodes. More... | |
boost::iterator_range< ConstKeyIterator > | keys () |
Iterators for container keys. More... | |
boost::iterator_range< ConstKeyIterator > | keys () const |
Iterators for container keys. More... | |
boost::iterator_range< ValueIterator > | values () |
Iterators for container values. More... | |
boost::iterator_range< ConstValueIterator > | values () const |
Iterators for container values. More... | |
double | maxLoadFactor () const |
Property: Maximum allowed load faster before automatic rehash. | |
void | maxLoadFactor (double mlf) |
Property: Maximum allowed load faster before automatic rehash. | |
NodeIterator | find (const Key &key) |
Find a node by key. More... | |
ConstNodeIterator | find (const Key &key) const |
Find a node by key. More... | |
Value & | operator[] (const Key &key) |
Return a reference to an existing value. More... | |
const Value & | operator[] (const Key &key) const |
Return a reference to an existing value. More... | |
Value & | get (const Key &key) |
Lookup and retun an existing value. More... | |
const Value & | get (const Key &key) const |
Lookup and retun an existing value. More... | |
Value & | getOrElse (const Key &key, Value &dflt) |
Lookup and return a value or something else. More... | |
const Value & | getOrElse (const Key &key, const Value &dflt) const |
Lookup and return a value or something else. More... | |
template<class OtherNodeIterator > | |
HashMap & | insertMultiple (const OtherNodeIterator &begin, const OtherNodeIterator &end) |
Insert multiple values. More... | |
template<class OtherNodeIterator > | |
HashMap & | insertMultiple (const boost::iterator_range< OtherNodeIterator > &range) |
Insert multiple values. More... | |
HashMap & | eraseAt (const NodeIterator &iter) |
Remove a node by iterator. More... | |
HashMap & | eraseAt (const ConstKeyIterator &iter) |
Remove a node by iterator. More... | |
HashMap & | eraseAt (const ValueIterator &iter) |
Remove a node by iterator. More... | |
template<class Iter > | |
HashMap & | eraseAtMultiple (const Iter &begin, const Iter &end) |
Remove multiple nodes by iterator range. More... | |
template<class Iter > | |
HashMap & | eraseAtMultiple (const boost::iterator_range< Iter > &range) |
Remove multiple nodes by iterator range. More... | |
|
inline |
|
inline |
Copy constructor.
Initializes the new map with copies of the nodes of the other
map. The keys and values must be convertible from the ohter map to this map.
Definition at line 237 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::nodes().
|
inline |
Iterators for container nodes.
This returns a range of node-iterators that will traverse all nodes (key/value pairs) of this container.
Definition at line 265 of file HashMap.h.
Referenced by Sawyer::Container::HashMap< K, T, H, C, A >::HashMap(), and Sawyer::Container::HashMap< K, T, H, C, A >::operator=().
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Return a reference to an existing value.
Returns a reference to the value at the node with the specified key
. Unlike std::map
, this container does not instantiate a new key/value pair if the key
is not in the map's domain. In other words, the array operator for this class is more like an array operator on arrays or vectors–such objects are not automatically extended if dereferenced with an operand that is outside the domain.
If the key
is not part of this map's domain then an std::domain_error
is thrown.
Definition at line 390 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::get().
|
inline |
Return a reference to an existing value.
Returns a reference to the value at the node with the specified key
. Unlike std::map
, this container does not instantiate a new key/value pair if the key
is not in the map's domain. In other words, the array operator for this class is more like an array operator on arrays or vectors–such objects are not automatically extended if dereferenced with an operand that is outside the domain.
If the key
is not part of this map's domain then an std::domain_error
is thrown.
Definition at line 393 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::get().
|
inline |
Lookup and retun an existing value.
Returns a reference to the value at the node with the specified key
, which must exist. If the key
is not part of this map's domain, then an std::domain_error
is thrown.
Definition at line 406 of file HashMap.h.
Referenced by Sawyer::Container::HashMap< K, T, H, C, A >::operator[]().
|
inline |
Lookup and retun an existing value.
Returns a reference to the value at the node with the specified key
, which must exist. If the key
is not part of this map's domain, then an std::domain_error
is thrown.
|
inline |
Lookup and return a value or nothing.
Looks up the node with the specified key and returns either a copy of its value, or nothing.
Here's an example of one convenient way to use this:
The equivalent STL approach is:
|
inline |
Lookup and return a value or something else.
This is similar to the get method, except a default can be provided. If a node with the specified key
is present in this container, then a reference to that node's value is returned, otherwise the (reference to) supplied default is returned.
|
inline |
Lookup and return a value or something else.
This is similar to the get method, except a default can be provided. If a node with the specified key
is present in this container, then a reference to that node's value is returned, otherwise the (reference to) supplied default is returned.
|
inline |
|
inline |
Insert or update a key/value pair.
Inserts the key/value pair into the container. If a previous node already had the same key then it is replaced by the new node.
Definition at line 485 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::insert().
Referenced by Sawyer::Container::HashMap< K, T, H, C, A >::insert(), Sawyer::Container::HashMap< K, T, H, C, A >::insertDefault(), and Sawyer::Container::HashMap< K, T, H, C, A >::insertMultiple().
|
inline |
Insert or update a key with a default value.
The value associated with key
in the map is replaced with a default-constructed value. If the key does not exist then it is inserted with a default value. This operation is similar to the array operator of std::map
.
Definition at line 498 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::insert().
|
inline |
Insert multiple values.
Inserts copies of the nodes in the specified node iterator range. The iterators must iterate over objects that have key
and value
methods that return keys and values that are convertible to the types used by this container.
The normal way to insert the contents of one map into another is:
Definition at line 520 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::insert().
Referenced by Sawyer::Container::HashMap< K, T, H, C, A >::insertMultiple().
|
inline |
Insert multiple values.
Inserts copies of the nodes in the specified node iterator range. The iterators must iterate over objects that have key
and value
methods that return keys and values that are convertible to the types used by this container.
The normal way to insert the contents of one map into another is:
Definition at line 526 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::insertMultiple().
|
inline |
Conditionally insert a new key/value pair.
Inserts the key/value pair into the container if the container does not yet have a node with the same key. The return value is a reference to the value that is in the container, either the value that previously existed or a copy of the specified value
.
Definition at line 538 of file HashMap.h.
Referenced by Sawyer::Container::HashMap< K, T, H, C, A >::insertMaybeDefault(), and Sawyer::Container::HashMap< K, T, H, C, A >::insertMaybeMultiple().
|
inline |
Conditionally insert a new key with default value.
Inserts a key/value pair into the container if the container does not yet have a node with the same key. The value is default-constructed. The return value is a reference to the value that is in the container, either the value that previously existed or the new default-constructed value.
Definition at line 549 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::insertMaybe().
|
inline |
Conditionally insert multiple key/value pairs.
Inserts each of the specified key/value pairs into this container where this container does not already contain a value for the key. The return value is a reference to the container itself so that this method can be chained with others.
Definition at line 560 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::insertMaybe().
|
inline |
Remove all nodes.
All nodes are removed from this container. This method executes in linear time in the number of nodes in this container.
Definition at line 570 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::clear().
Referenced by Sawyer::Container::HashMap< K, T, H, C, A >::clear(), and Sawyer::Container::HashMap< K, T, H, C, A >::operator=().
|
inline |
Remove a node with specified key.
Removes the node whose key is equal to the specified key, or does nothing if no such node exists. Two keys are considered equal if this container's Comparator object returns false reflexively.
Definition at line 581 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::erase().
Referenced by Sawyer::Container::HashMap< K, T, H, C, A >::erase(), Sawyer::Container::HashMap< K, T, H, C, A >::eraseAt(), Sawyer::Container::HashMap< K, T, H, C, A >::eraseAtMultiple(), and Sawyer::Container::HashMap< K, T, H, C, A >::eraseMultiple().
|
inline |
Remove keys stored in another HashMap.
All nodes of this container whose keys are equal to any key in the other
container are removed from this container. The keys of the other container must be convertible to the types used by this container, and two keys are considered equal if this container's Comparator object returns false reflexively.
Definition at line 594 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::erase().
|
inline |
Remove a node by iterator.
Removes the node referenced by iter
. The iterator must reference a valid node in this container.
Definition at line 607 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::erase().
|
inline |
Remove a node by iterator.
Removes the node referenced by iter
. The iterator must reference a valid node in this container.
Definition at line 611 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::erase().
|
inline |
Remove a node by iterator.
Removes the node referenced by iter
. The iterator must reference a valid node in this container.
Definition at line 615 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::erase().
|
inline |
Remove multiple nodes by iterator range.
The iterator range must contain iterators that point into this container.
Definition at line 629 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::erase().
|
inline |
Remove multiple nodes by iterator range.
The iterator range must contain iterators that point into this container.
Definition at line 634 of file HashMap.h.
References Sawyer::Container::HashMap< K, T, H, C, A >::erase().