nCine 2022.04.r504-20bd01e
A cross-platform 2D game engine
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Friends | List of all members
nctl::HashMap< K, T, HashFunc > Class Template Reference

A template based hashmap implementation with open addressing and leapfrog probing. More...

#include <HashMap.h>

Public Types

using Iterator = HashMapIterator< K, T, HashFunc, false >
 Iterator type.
 
using ConstIterator = HashMapIterator< K, T, HashFunc, true >
 Constant iterator type.
 
using ReverseIterator = nctl::ReverseIterator< Iterator >
 Reverse iterator type.
 
using ConstReverseIterator = nctl::ReverseIterator< ConstIterator >
 Reverse constant iterator type.
 

Public Member Functions

 HashMap (unsigned int capacity)
 
 HashMap (const HashMap &other)
 Copy constructor.
 
 HashMap (HashMap &&other)
 Move constructor.
 
HashMapoperator= (const HashMap &other)
 Assignment operator.
 
HashMapoperator= (HashMap &&other)
 Move assignment operator.
 
void swap (HashMap &first, HashMap &second)
 Swaps two hashmaps without copying their data.
 
Iterator begin ()
 Returns an iterator to the first element.
 
ReverseIterator rBegin ()
 Returns a reverse iterator to the last element.
 
Iterator end ()
 Returns an iterator to past the last element.
 
ReverseIterator rEnd ()
 Returns a reverse iterator to prior the first element.
 
ConstIterator begin () const
 Returns a constant iterator to the first element.
 
ConstReverseIterator rBegin () const
 Returns a constant reverse iterator to the last element.
 
ConstIterator end () const
 Returns a constant iterator to past the last lement.
 
ConstReverseIterator rEnd () const
 Returns a constant reverse iterator to prior the first element.
 
ConstIterator cBegin () const
 Returns a constant iterator to the first element.
 
ConstReverseIterator crBegin () const
 Returns a constant reverse iterator to the last element.
 
ConstIterator cEnd () const
 Returns a constant iterator to past the last lement.
 
ConstReverseIterator crEnd () const
 Returns a constant reverse iterator to prior the first element.
 
T & operator[] (const K &key)
 Subscript operator.
 
bool insert (const K &key, const T &value)
 Inserts an element if no other has the same key.
 
bool insert (const K &key, T &&value)
 Moves an element if no other has the same key.
 
template<typename... Args>
bool emplace (const K &key, Args &&... args)
 Constructs an element if no other has the same key.
 
unsigned int capacity () const
 Returns the capacity of the hashmap.
 
bool isEmpty () const
 Returns true if the hashmap is empty.
 
unsigned int size () const
 Returns the number of elements in the hashmap.
 
float loadFactor () const
 Returns the ratio between used and total buckets.
 
hash_t hash (const K &key) const
 Returns the hash of a given key.
 
void clear ()
 Clears the hashmap.
 
bool contains (const K &key, T &returnedValue) const
 Checks whether an element is in the hashmap or not.
 
T * find (const K &key)
 Checks whether an element is in the hashmap or not.
 
const T * find (const K &key) const
 Checks whether an element is in the hashmap or not (read-only)
 
bool remove (const K &key)
 Removes a key from the hashmap, if it exists.
 
void rehash (unsigned int count)
 Sets the number of buckets to the new specified size and rehashes the container.
 

Friends

class HashMapIterator< K, T, HashFunc, false >
 
class HashMapIterator< K, T, HashFunc, true >
 
struct HashMapHelperTraits< K, T, HashFunc, false >
 
struct HashMapHelperTraits< K, T, HashFunc, true >
 

Detailed Description

template<class K, class T, class HashFunc = FNV1aHashFunc<K>>
class nctl::HashMap< K, T, HashFunc >

A template based hashmap implementation with open addressing and leapfrog probing.

Member Function Documentation

◆ emplace()

template<class K , class T , class HashFunc >
template<typename... Args>
bool nctl::HashMap< K, T, HashFunc >::emplace ( const K &  key,
Args &&...  args 
)

Constructs an element if no other has the same key.

Returns
True if the element has been emplaced

◆ find() [1/2]

template<class K , class T , class HashFunc >
T * nctl::HashMap< K, T, HashFunc >::find ( const K &  key)

Checks whether an element is in the hashmap or not.

Note
Prefer this method if copying T is expensive, but always check the validity of returned pointer.

◆ find() [2/2]

template<class K , class T , class HashFunc >
const T * nctl::HashMap< K, T, HashFunc >::find ( const K &  key) const

Checks whether an element is in the hashmap or not (read-only)

Note
Prefer this method if copying T is expensive, but always check the validity of returned pointer.

◆ insert() [1/2]

template<class K , class T , class HashFunc >
bool nctl::HashMap< K, T, HashFunc >::insert ( const K &  key,
const T &  value 
)

Inserts an element if no other has the same key.

Returns
True if the element has been inserted

◆ insert() [2/2]

template<class K , class T , class HashFunc >
bool nctl::HashMap< K, T, HashFunc >::insert ( const K &  key,
T &&  value 
)

Moves an element if no other has the same key.

Returns
True if the element has been inserted

◆ remove()

template<class K , class T , class HashFunc >
bool nctl::HashMap< K, T, HashFunc >::remove ( const K &  key)

Removes a key from the hashmap, if it exists.

Returns
True if the element has been found and removed

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