Gorgon Game Engine
Hashmap< K_, T_, KeyFn, M_, C_ > Class Template Reference

This class is a reference based hashmap. More...

Inheritance diagram for Hashmap< K_, T_, KeyFn, M_, C_ >:
[legend]

Classes

class  ConstIterator
 Const iterator allows iteration of const collections. More...
 

Public Types

typedef Iterator_< typename std::map< K_, T_ * >::iterator, HashmapIterator
 Regular iterator. More...
 
typedef K_ KeyType
 
typedef T_ ValueType
 

Public Member Functions

 Hashmap ()
 Default constructor. More...
 
 Hashmap (const Hashmap &)=delete
 Copy constructor is disabled. More...
 
 Hashmap (Hashmap &&other)
 Move constructor. More...
 
 Hashmap (std::initializer_list< std::pair< const K_, T_ & >> list)
 Filling constructor. This constructor uses initializer list of std::pair<K_, T_&> More...
 
 Hashmap (std::initializer_list< std::pair< const K_, T_ * >> list)
 Filling constructor. More...
 
 Hashmap (std::initializer_list< T_ * > list)
 Filling constructor that takes the keys using KeyFn function. More...
 
void Add (const K_ &key, T_ &obj, bool deleteprev=false)
 Adds the given item with the related key. More...
 
void Add (const K_ &key, T_ *obj, bool deleteprev=false)
 Adds the given item with the related key. More...
 
void Add (T_ &obj, bool deleteprev=false)
 Adds the given item by retrieving the related key. More...
 
void Add (T_ *obj, bool deleteprev=false)
 Adds the given item by retrieving the related key. More...
 
void Collapse ()
 Clears the contents of the map and releases the memory used for the list. More...
 
void Delete (const K_ &key)
 Removes the item with the given key from the mapping and deletes it. More...
 
void DeleteAll ()
 Deletes and removes all the elements of this map. More...
 
void Destroy ()
 Deletes and removes all the elements of this map, in addition to destroying data used. More...
 
Hashmap Duplicate () const
 
bool Exists (const K_ &key) const
 Checks if an element with the given key exists. More...
 
Iterator Find (const K_ &key)
 Finds the given key in the hashmap and returns iterator for it. More...
 
ConstIterator Find (const K_ &key) const
 Finds the given key in the hashmap and returns iterator for it. More...
 
long GetCount () const
 Returns the number of elements in the map. More...
 
long GetSize () const
 Returns the number of elements in the map. More...
 
Hashmapoperator= (const Hashmap &other)=delete
 Copy constructor is disabled. More...
 
Hashmapoperator= (Hashmap &&other)
 Move constructor, does not delete elements. More...
 
T_ & operator[] (const K_ &key) const
 If not found throws. More...
 
void Remove (const K_ &key)
 Removes the item with the given key from the mapping. More...
 
void RemoveAll ()
 Removes all elements from this mapping without deleting them. More...
 
void Swap (Hashmap &other)
 Swaps two hashmaps. More...
 

Iterator related

begin iterator

Iterator begin ()
 
Iterator end ()
 end iterator More...
 
Iterator First ()
 returns the iterator to the first item More...
 
Iterator Last ()
 returns the iterator to the last item More...
 
ConstIterator begin () const
 begin iterator More...
 
ConstIterator end () const
 end iterator More...
 
ConstIterator First () const
 returns the iterator to the first item More...
 
ConstIterator Last () const
 returns the iterator to the last item More...
 

Detailed Description

template<class K_, class T_, K_(*)(const T_ &) KeyFn = (K_(*)(const T_&))nullptr, template< class ... > class M_ = std::map, class C_ = std::less<K_>>
class Gorgon::Containers::Hashmap< K_, T_, KeyFn, M_, C_ >

This class is a reference based hashmap.

It uses std::map as underlying mechanism and provides necessary services to manage a reference based container. Like other Gorgon containers, Hashmap also disallows copy construction to reduce expensive mistakes. If a copy of the container is required, Duplicate function can be used to create a copy. Hashmap uses move semantics and can be returned from functions by value even though copy construction is disabled. Last template parameter can be replaced by unsorted_map. Currently Hashmap does not allow the use of multi-maps.

Warning
This container uses value iterator which does not have -> operator and might not be compatible with all library functions. * operator returns a copy of the pair, not a reference to it.

Member Typedef Documentation

◆ Iterator

typedef Iterator_<typename std::map<K_, T_*>::iterator, Hashmap> Iterator

Regular iterator.

See also
Container::Iterator

◆ KeyType

typedef K_ KeyType

◆ ValueType

typedef T_ ValueType

Constructor & Destructor Documentation

◆ Hashmap() [1/6]

Hashmap ( )

Default constructor.

◆ Hashmap() [2/6]

Hashmap ( std::initializer_list< std::pair< const K_, T_ * >>  list)

Filling constructor.

This constructor uses initializer list of std::pair<K_, T_*>. This function works faster by forwarding the lsit to underlying storage. However, it cannot deal with nullptr entries, thus can leave the container in undefined state. A test agains this case is performed for debug builds.

◆ Hashmap() [3/6]

Hashmap ( std::initializer_list< std::pair< const K_, T_ & >>  list)

Filling constructor. This constructor uses initializer list of std::pair<K_, T_&>

◆ Hashmap() [4/6]

Hashmap ( std::initializer_list< T_ * >  list)

Filling constructor that takes the keys using KeyFn function.

This constructor handles nullptr entries by ignoring them.

◆ Hashmap() [5/6]

Hashmap ( const Hashmap< K_, T_, KeyFn, M_, C_ > &  )
delete

Copy constructor is disabled.

◆ Hashmap() [6/6]

Hashmap ( Hashmap< K_, T_, KeyFn, M_, C_ > &&  other)

Move constructor.

Member Function Documentation

◆ Add() [1/4]

void Add ( const K_ &  key,
T_ &  obj,
bool  deleteprev = false 
)

Adds the given item with the related key.

If the key already exists, the object it points to is changed. If deleteprev is set, previous object at the key is deleted.

◆ Add() [2/4]

void Add ( const K_ &  key,
T_ *  obj,
bool  deleteprev = false 
)

Adds the given item with the related key.

If the key already exists, the object it points to is changed. If deleteprev is set, previous object at the key is deleted. If obj is nullptr and the key exists in the map, it is removed.

◆ Add() [3/4]

void Add ( T_ &  obj,
bool  deleteprev = false 
)

Adds the given item by retrieving the related key.

If the key already exists, the object it points to is changed. If deleteprev is set, previous object at the key is deleted.

◆ Add() [4/4]

void Add ( T_ *  obj,
bool  deleteprev = false 
)

Adds the given item by retrieving the related key.

If the key already exists, the object it points to is changed. If deleteprev is set, previous object at the key is deleted. If obj is nullptr and the key exists in the map, it is removed.

◆ begin() [1/2]

Iterator begin ( )

◆ begin() [2/2]

ConstIterator begin ( ) const

begin iterator

◆ Collapse()

void Collapse ( )

Clears the contents of the map and releases the memory used for the list.

Items are not freed.

◆ Delete()

void Delete ( const K_ &  key)

Removes the item with the given key from the mapping and deletes it.

If the item does not exists, this request is simply ignored

◆ DeleteAll()

void DeleteAll ( )

Deletes and removes all the elements of this map.

◆ Destroy()

void Destroy ( )

Deletes and removes all the elements of this map, in addition to destroying data used.

◆ Duplicate()

Hashmap Duplicate ( ) const

◆ end() [1/2]

Iterator end ( )

end iterator

◆ end() [2/2]

ConstIterator end ( ) const

end iterator

◆ Exists()

bool Exists ( const K_ &  key) const

Checks if an element with the given key exists.

◆ Find() [1/2]

Iterator Find ( const K_ &  key)

Finds the given key in the hashmap and returns iterator for it.

An !IsValid() iterator is returned if item is not found

◆ Find() [2/2]

ConstIterator Find ( const K_ &  key) const

Finds the given key in the hashmap and returns iterator for it.

An !IsValid() iterator is returned if item is not found

◆ First() [1/2]

Iterator First ( )

returns the iterator to the first item

◆ First() [2/2]

ConstIterator First ( ) const

returns the iterator to the first item

◆ GetCount()

long GetCount ( ) const

Returns the number of elements in the map.

◆ GetSize()

long GetSize ( ) const

Returns the number of elements in the map.

◆ Last() [1/2]

Iterator Last ( )

returns the iterator to the last item

◆ Last() [2/2]

ConstIterator Last ( ) const

returns the iterator to the last item

◆ operator=() [1/2]

Hashmap& operator= ( const Hashmap< K_, T_, KeyFn, M_, C_ > &  other)
delete

Copy constructor is disabled.

◆ operator=() [2/2]

Hashmap& operator= ( Hashmap< K_, T_, KeyFn, M_, C_ > &&  other)

Move constructor, does not delete elements.

◆ operator[]()

T_& operator[] ( const K_ &  key) const

If not found throws.

◆ Remove()

void Remove ( const K_ &  key)

Removes the item with the given key from the mapping.

If the item does not exists, this request is simply ignored. This function does not delete the item.

◆ RemoveAll()

void RemoveAll ( )

Removes all elements from this mapping without deleting them.

Additonally, any memory that is being used by std::map is not freed.

◆ Swap()

void Swap ( Hashmap< K_, T_, KeyFn, M_, C_ > &  other)

Swaps two hashmaps.


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