Unigine::Hash class
Header: | #include <UnigineHash.h> |
A Hash is a collection of unique keys and their values, much like a dictionary. Hashes are also referred to as associative arrays; they are similar to arrays, but while an array uses integers as an index, a Hash can use any type of object.
Let's check some examples of class functions usage:
- You can get the hash size and check whether the hash contains a specific key:
// create an empty hash set HashSet<String> set; // create two equal strings const char *initial_str = "aaa"; const String aaa0(initial_str), aaa1(initial_str); // add one of the strings as a key to the hash set set.insert(aaa0); // check the result if (set.contains(initial_str)) Log::message("The hash set contains the recently added key\n"); Log::message("The size is %d\n", set.size()); // add the duplicate string as a key to the hash set set.insert(aaa1); // check the result - the size of the hash set remains the same Log::message("The size is %d\n", set.size());
- You can get the iterator pointing to the first key in the hash. Also, you can remove some specific key from the hash if you know its iterator.
// create a hash set HashSet<String> set0{ "1", "2", "3" }; // copy one hash set to another HashSet<String> set1(set0); // remove the first key from the first hash set set0.erase(set0.begin()); // check the result if (set0 != set1) { for (const auto &i : set0) Log::message("%s ", i.key.get()); Log::message("\n"); for (const auto &j : set1) Log::message("%s ", j.key.get()); }
- To clear the hash set, you should use the clear() function:
// create a hash set HashSet<String> set{ "1", "2", "3", "4", "5" }; // remove all keys from the hash set set.clear(); // check if the set is empty if (set.empty()) Log::message("The set is empty\n");
Hash Class
Members
bool is_need_realloc ( ) #
Indicates whether reallocation is needed.Return value
true if reallocation is needed, otherwise false.void shrink ( ) #
Reduces memory usage by removing unused capacity.Data * do_append ( const Key & key ) #
Appends an element with the specified key to the hash.Arguments
- const Key & key - Key of an element to be appended.
Return value
Pointer to Data.void rehash ( Counter new_capacity ) #
Rebuilds the hash based on a specified capacity.Arguments
- Counter new_capacity - New hash capacity value.
Iterator begin ( ) #
Returns an iterator that points to the first element in the hash.Return value
Iterator pointing to the first element.ConstIterator end ( ) #
Returns a const iterator that points to the location succeeding the last element in the hash.Return value
Const iterator pointing to the location after the last element.Vector<Key> keys ( ) #
Returns a vector containing all the keys in the hash.Return value
Vector of keys.bool remove ( const ConstIterator & it ) #
Removes an element currently pointed to by the iterator from the hash.Arguments
- const ConstIterator & it - Iterator pointing to an element to be removed.
Return value
true on success, otherwise false.Counter space ( ) #
Returns current capacity (number of elements the hash can currently contain).Return value
Number of elements the hash can currently contain.Data * do_append ( Key && key ) #
Appends an element with the specified key to the hash.Arguments
- Key && key - Key of an element to be appended.
Return value
Pointer to Data.bool erase ( const ConstIterator & it ) #
Removes an element currently pointed to by the iterator from the hash.Arguments
- const ConstIterator & it - Iterator pointing to the element to be removed.
Return value
true on success, otherwise false.bool erase ( const Iterator & it ) #
Removes an element currently pointed to by the iterator from the hash.Arguments
- const Iterator & it - Iterator pointing to the element to be removed.
Return value
true on success, otherwise false.Counter round_up ( Counter v ) #
Performs rounding and returns the result.Arguments
- Counter v
void swap ( Hash<Key, Data, HashType, Counter> & hash ) #
Swaps this hash with the specified one.Arguments
- Hash<Key, Data, HashType, Counter> & hash - Specified hash.
Data * do_append ( HashType hash, const Key & key ) #
Appends an element with the specified key to the specified hash.Arguments
- HashType hash - Specified hash.
- const Key & key - Key of an element to be appended.
Return value
Pointer to Data.void rehash_data ( Counter index ) #
Rebuilds the hash.Arguments
- Counter index
Counter empty ( ) #
Checks if the hash is empty or not.Return value
1 if the hash is empty, otherwise 0bool contains ( const Key & key ) #
Checks if the given key is present in the hash.Arguments
- const Key & key - Key to be looked for.
Return value
true if the given key is present in the hash, otherwise false.void clear ( ) #
Removes all key-value pairs from the hash.bool do_remove ( HashType hash, const Key & key ) #
Removes an element with the specified key from the specified hash.Arguments
- HashType hash - Specified hash.
- const Key & key - Key of an element to be removed.
Return value
true on success, otherwise false.void getKeys ( Vector<Key> & keys ) #
Adds keys of the hash to the specified vector.Arguments
- Vector<Key> & keys - Specified vector to store the keys in.
void reserve ( Counter size ) #
Reserves storage to avoid repeated reallocation.Arguments
- Counter size - Hash size to be reserved.
bool erase ( const Key & key ) #
Removes an element having the specified key from the hash.Arguments
- const Key & key - Key of an element to be removed.
Return value
true on success, otherwise false.Data ** do_find ( const Key & key ) #
Finds an element with a specified key.Arguments
- const Key & key - Key to look for.
Return value
Data of the element with the specified key if it exists, otherwise nullptr.void realloc ( ) #
Performs memory reallocation.bool remove ( const Key & key ) #
Removes an element having the specified key from the hash.Arguments
- const Key & key - Key of an element to be removed.
bool remove ( const Iterator & it ) #
Removes an element currently pointed to by the iterator from the hash.Arguments
- const Iterator & it - Iterator pointing to the element to be removed.
Return value
true on success, otherwise false.void destroy ( ) #
Deletes the hash.Counter size ( ) #
Returns the number of key-value pairs in the hash.Return value
Number of key-value pairs in the hash.Iterator end ( ) #
Returns an iterator that points to the location succeeding the last element in the hash.Return value
Iterator pointing to the location after the last element.ConstIterator begin ( ) #
Returns a const iterator that points to the first element in the hash.Return value
Iterator pointing to the first element.ConstIterator find ( const Key & key ) #
Finds an element with a specified key.Arguments
- const Key & key - Key to look for.
Return value
Iterator pointing to the element with a specified key if it exists, or to the end of the hash if it does not.Iterator find ( const Key & key ) #
Finds an element with a specified key.Arguments
- const Key & key - Key to look for.
Return value
Iterator pointing to the element with a specified key if it exists, or to the end of the hash if it does not.Last update:
13.12.2024
Помогите сделать статью лучше
Была ли эта статья полезной?
(или выберите слово/фразу и нажмите Ctrl+Enter