HashMap
A class template for hash maps and hash sets. Allows you to create various kinds of associative maps using hashed key lookups.
HashMap
is a versatile class template. It provides the functionality of both std::unordered_map
and std::unordered_set
while supporting use cases that aren't possible with either of those class
templates:
-
You can create an index into an existing container (which could be an
Array
,FixedArray
,std::vector
orPool
) without duplicating any of the data stored in that container. This is made possible by the optionalcontext
parameter accepted byfind()
andinsertOrFind()
. -
The type used for key lookup can differ from the storage type. For example, a
HashMap
can containString
objects but useStringView
objects for lookup, thus avoiding the need to create temporaryString
objects during lookup.std::unordered_map
only began to support this behavior in C++20.
The find()
and insertOrFind()
functions return Cursor
(or ConstCursor
) objects. With a
Cursor
object, you can determine whether the given key was found, retrieve the item associated
with that key, or erase the item from the map.
Internally, HashMap
uses leapfrog probing.
HashMap
is not thread-safe. If you manipulate a map object from multiple threads, you must
enforce mutual exclusion yourself.
See HashMap Traits.
Header File
#include <ply-runtime/container/HashMap.h>
Also included from <ply-runtime/Base.h>
.
Member Functions
-
Constructs an empty
HashMap
. -
Move constructor.
other
is set to an invalid state. After this call, it's not legal to insert or find items inother
unless it is set back to a valid state using move assignment. void HashMap::operator=(HashMap&& other)
[code]-
Move assignment operator.
other
is set to an invalid state. After this call, it's not legal to insert or find items inother
unless it is set back to a valid state using move assignment. -
Destructs all
Item
s in theHashMap
and sets it to an invalid state. After this call, it's not legal to insert or find items in the hash map unless it is set back to a valid state using move assignment. -
Returns
true
if the hash map is empty. -
Returns the number of items in the hash map.
-
Find
Key
in the hash map. If no matchingItem
exists, a new item is inserted. CallCursor::wasFound()
on the return value to determine whether the item was found or inserted. Note that when a new item is inserted, it might be the caller's responsibility to ensure the hash table is left in a consistent state, depending on the behavior of the Traits class'sconstruct
function. -
Attempts to find
Key
in the hash map. CallCursor::wasFound()
on the return value to determine whether a matchingItem
was found. If the hash map isconst
, aConstCursor
is returned instead of aCursor
. The difference betweenCursor
andConstCursor
is that a non-constCursor
can be used to erase existing items from the map. -
Required functions to support range-for syntax.