Unigine::HashMap Class
Header: | #include <UnigineHashMap.h> |
A hash map container template. The hash map stores items represented by key-value pairs in an unspecified order. It can be used, for example, for searching among nodes by their names.
To create a new hash map, you can use the default constructor or one of the following ways:
- Create a hash map using an initializer list:
The hash map that stores values of a custom type can be created as follows:
// create a hash map using an initializer list const HashMap<String, int> hashMap_0{ { "a", 1 }, { "b", 2 }, { "c", 3 }, }; // check the result for (const auto &k : hashMap_0) Log::message("%s : %d \n", k.key.get(), k.data);
// declare an enumeration enum class MyEnum { One, Two, Three, }; // create a hash map using an initializer list const HashMap<String, MyEnum> hashMap_1 { { "One", MyEnum::One }, { "Two", MyEnum::Two }, { "Three", MyEnum::Three }, }; // check the result for (const auto &k : hashMap_1) Log::message("%s : %d \n", k.key.get(), k.data);
- Create a hash set using a copy constructor:
// create a hash map using an initializer list const HashMap<String, int> initial{ { "a", 1 },{ "b", 2 },{ "c", 3 },{ "d", 4 }, }; // copy constructor HashMap<String, int> copied(initial); //check the result for (const auto &k : copied) Log::message("%s : %d \n", k.key.get(), k.data);
You can change the created hash map by using the class member functions described in the article. Check some of the usage examples:
- To add items to the current hash map, you can use one of the append() or insert() functions:
// create hash maps using initializer lists HashMap<String, int> initial{ { "a", 1 }, { "b", 2 }, { "c", 3 }, }; const HashMap<String, int> to_append{ { "m", 7 }, { "n", 8 }, { "o", 9 }, }; // append items of one hash map to another initial.append(to_append); // insert an item into the "initial" hash map initial.insert("s", 10); // check the result for (const auto &k : initial) Log::message("%s : %d \n", k.key.get(), k.data);
- Insert an item with a value constructed in-place to avoid unnecessary copying by using the emplace() function:
// declare a named structure struct Foo { Foo(int a, int b) : a_(a), b_(b) {} int a_, b_; }; // create an empty hash map HashMap<String, Foo> hashMap; // insert items into the hash map by specifying a key and an arguments for the Foo constructor hashMap.emplace("foo_1_2", 1, 2); hashMap.emplace("foo_1_3", 1, 3); // check the result for (const auto &it : hashMap) Log::message("Key %s, Value %d %d\n", it.key.get(), it.data.a_, it.data.b_);
- Get a value by the key from the hash map and insert an item if there is no such key:
// creare an empty hash map HashMap<int, int> hashMap{ { 1, 10 } }; // try to get values by the specified keys Log::message("Value %d\n", hashMap[1]); // if there is no such key, it will be added to the hash map with the default value Log::message("Value %d\n", hashMap[2]); // check the result - the new item with the default value is added to the hash map for (const auto &k: hashMap) Log::message("%d : %d \n", k.key, k.data);
- Take an item from the hash map:
// create a hash map HashMap<int, int> hashMap { {1, 1}, {2, 2}, {3, 3}, }; // declare a key and a value const int key = 0; const int value = 4; // append a new item to the hash map hashMap.append(key,value); // get an item iterator by its key auto it = hashMap.find(key); // take the item from the hash map int c = hashMap.take(it); // check the result - the taken item is removed from the hash map for (const auto &k : hashMap) Log::message("%d : %d \n", k.key, k.data);
- Remove an item from the hash map:
// create a hash map HashMap<int, int> hashMap{ { 1, 1 },{ 2, 2 },{ 3, 3 }, }; // get an item iterator by its key auto it = hashMap.find(2); // remove the item from the hash map hashMap.remove(it); // check the result for (const auto &k: hashMap) Log::message("%d : %d \n", k.key, k.data);
- Get items by the values from the hash map:
// create a hash map HashMap<String, int> hashMap{ { "a", 1 }, { "b", 2 }, { "c", 3 }, }; // get an item iterator by its value auto it = hashMap.findData(2); // check the result Log::message("%s \n", it->key.get());
- Get a value/values from the hash map:
// create a hash map HashMap<String, int> hashMap{ { "a", 1 },{ "b", 2 },{ "c", 3 }, }; // get a key by its value Log::message("%d \n", hashMap.value("a")); // get a vector of all values of the hash map Vector<int> ret = hashMap.values(); for (int i = 0; i < ret.size(); i++) Log::message("%d \n",ret[i]);
HashMap Class
Members
HashMap ( ) #
Default constructor that produces an empty hash map.HashMap ( std::initializer_list<Pair<Key,Type>> list ) #
Constructor. Creates a hash map from given key-value pairs.Arguments
- std::initializer_list<Pair<Key,Type>> list - List of pairs.
HashMap ( const HashMap& o ) #
Constructor. Creates a hash map by copying a source hash map.Arguments
- const HashMap& o - Hash map.
HashMap ( HashMap&& o ) #
Constructor. Creates a hash map by copying a source hash map.Arguments
- HashMap&& o - Hash map.
HashMap<Key, Type, Counter> & operator= ( const HashMap& o ) #
Assignment operator for the hash map.Arguments
- const HashMap& o - Hash map.
HashMap<Key, Type, Counter> & operator= ( HashMap&& o ) #
Assignment operator for the hash map.Arguments
- HashMap&& o - Hash map.
Iterator append ( const Key& key, const Type& value ) #
Appends an item with a given key and value to the hash map.Arguments
- const Key& key - Key.
- const Type& value - Value.
Return value
Item iterator.Iterator append ( const Key& key, Type&& value ) #
Appends an item with a given key and value to the hash map.Arguments
- const Key& key - Key.
- Type&& value - Value.
Return value
Item iterator.Iterator append ( Key&& key, const Type& value ) #
Appends an item with a given key and value to the hash map.Arguments
- Key&& key - Key.
- const Type& value - Value.
Return value
Item iterator.Iterator append ( Key&& key, Type&& value ) #
Appends an item with a given key and value to the hash map.Arguments
- Key&& key - Key.
- Type&& value - Value.
Return value
Item iterator.void append ( const HashMap& o ) #
Appends a given hash map to the current one.Arguments
- const HashMap& o - Hash map.
void append ( HashMap&& o ) #
Appends a given hash map to the current one.Arguments
- HashMap&& o - Hash map.
Iterator insert ( const Key& key, const Type& value ) #
Inserts an item with a given key and value into the hash map.Arguments
- const Key& key - Key.
- const Type& value - Value.
Return value
Item iterator.Iterator insert ( const Key& key, Type&& value ) #
Inserts an item with a given key and value into the hash map.Arguments
- const Key& key - Key.
- Type&& value - Value.
Return value
Item iterator.Iterator insert ( Key&& key, const Type& value ) #
Inserts an item with a given key and value into the hash map.Arguments
- Key&& key - Key.
- const Type& value - Value.
Return value
Item iterator.Iterator insert ( Key&& key, Type&& value ) #
Inserts an item with a given key and value into the hash map.Arguments
- Key&& key - Key.
- Type&& value - Value.
Return value
Item iterator.void insert ( const HashMap& o ) #
Inserts a given hash map into the current one.Arguments
- const HashMap& o - Hash map.
void insert ( HashMap&& o ) #
Inserts a given hash map into the current one.Arguments
- HashMap&& o - Hash map.
Type & append ( const Key& key ) #
Appends an item with a specified key to the hash map. The item value is default-constructed.Arguments
- const Key& key - Key.
Return value
Added item value.Type & append ( Key&& key ) #
Appends an item with a specified key to the hash map. The item value is default-constructed.Arguments
- Key&& key - Key.
Return value
Added item value.Type & insert ( const Key& key ) #
Inserts an item with a specified key into the hash map. The item value is default-constructed.Arguments
- const Key& key - Key.
Return value
Inserted item value.Type & insert ( Key&& key ) #
Inserts an item with a specified key into the hash map. The item value is default-constructed.Arguments
- Key&& key - Key.
Return value
Inserted item value.Type & emplace ( const Key& key, Args&& args ) #
Inserts an item with a specified key into the hash map. The new item value is constructed in-place with the given arguments avoiding unnecessary copying.Arguments
- const Key& key - Key.
- Args&& args - Arguments for an item value constructor.
Return value
Inserted item value.Type & emplace ( Key&& key, Args&& args ) #
Inserts an item with a specified key into the hash map. The new item value is constructed in-place with the given arguments avoiding unnecessary copying.Arguments
- Key&& key - Key.
- Args&& args - Arguments for an item value constructor.
Return value
Inserted item value.Type take ( const Key& key, const Type& value ) #
Removes an item with a specified key from the hash map and returns an item value. If there is no item with the specified key, a default value is returned.Arguments
- const Key& key - Key.
- const Type& value - Default value.
Return value
Removed item value.Type take ( const Key& key ) #
Removes an item with a specified key from the hash map and returns its value. If there is no item with the specified key, a default-constructed value is returned.Arguments
- const Key& key - Key.
Return value
Removed item value.Type take ( const Iterator& it ) #
Removes an item from the hash map by its iterator and returns an item value. If there is no such item, a default-constructed value is returned.Arguments
- const Iterator& it - Item iterator.
Return value
Removed item value.Type take ( const ConstIterator& it ) #
Removes an item from the hash map by its iterator and returns an item value. If there is no such item, a default-constructed value is returned.Arguments
- const ConstIterator& it - Item iterator.
Return value
Removed item value.Type & operator[] ( const Key& key ) #
Hash map item access.Arguments
- const Key& key - Key.
Return value
Accessed item value.Type & operator[] ( Key&& key ) #
Hash map item access.Arguments
- Key&& key - Key.
Return value
Accessed item value.const Type & operator[] ( const Key& key ) const#
Hash map item access.Arguments
- const Key& key - Key.
Return value
Accessed item value.Type & get ( Key&& key ) #
Returns a value by a specified key. If there is no item with the key, inserts a new value.Arguments
- Key&& key - Key.
Return value
Value.Type & get ( const Key& key ) #
Returns a value by a specified key. If there is no item with the key, inserts a new value.Arguments
- const Key& key - Key.
Return value
Value.const Type & get ( const T& key ) const#
Returns a value by a specified key.Arguments
- const T& key - Key.
Return value
Value.const Type & get ( const T& key, const Type& value ) const#
Returns a value by a specified key. If there is no item with the key, the default value is returned.Arguments
- const T& key - Key.
- const Type& value - Default value.
Return value
Value.bool contains ( const T& key, const Type& value ) const#
Checks if an item with a specified key and value exists in the hash map.Arguments
- const T& key - Key.
- const Type& value - Value.
Return value
Returns 1 if an item exists; otherwise, 0 is returned.Iterator findData ( const Type& t ) #
Searches for an item with a specified value in the hash map.Arguments
- const Type& t - Value.
Return value
Item iterator.ConstIterator findData ( const Type& t ) const#
Searches for an item with a specified value in the hash map.Arguments
- const Type& t - Value.
Return value
Item iterator.Type value ( const T& key ) const#
Returns a value with a specified key from the hash map. If there is no such key, returns a default-constructed value.Arguments
- const T& key - Key.
Return value
Value.Type value ( const T& key, const Type& def ) const#
Returns a value with a specified key from the hash map. If there is no such key, returns a default value.Arguments
- const T& key - Key.
- const Type& def - Default value.
Return value
Value.const Type & valueRef ( const T& key, const Type& def ) const#
Returns a value with a specified key from the hash map. If there is no such key, returns a default value.Arguments
- const T& key - Key.
- const Type& def - Default value.
Return value
Value.int values ( ) const#
Returns a vector of all values of the hash map.Return value
Vector of hash map values.void getValues ( Vector<Type>& values ) const#
Appends hash map values to a given vector.Arguments
- Vector<Type>& values - Vector of hash map values.
void getPairs ( Vector<Pair<Key,Type >>& pairs ) const#
Appends hash map key-value pairs to a given vector.Arguments
bool operator== ( const HashMap& o ) const#
Checks if two hash maps are equal. The hash maps are considered equal if their key-value pairs are the same.Arguments
- const HashMap& o - Hash map.
Return value
Returns 1 if hash maps are equal; otherwise, 0 is returned.bool operator!= ( const HashMap& o ) const#
Checks if two hash maps are not equal. The hash maps are considered equal if their key-value pairs are the same.Arguments
- const HashMap& o - Hash map.
Return value
Returns 1 if hash maps are not equal; otherwise, 0 is returned.Last update:
2024-12-13
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)