Unigine::HashSet Class
Header: | #include <UnigineHashSet.h> |
A hash set container template. The hash set stores keys in an unspecified order. It allows for fast lookup of the keys. For example, you can implement fast searching among nodes by their IDs.
To create a new hash set, you can use the default constructor or one of the following ways:
- Create a hash set using an initializer list:
// constructor that takes an initializer list const HashSet<int> hashSet{ 4,7,2 }; // check the result for (const auto &k : hashSet) Log::message("%d ", k.key);
- Create a hash set using a copy constructor:
// constructor that takes an initializer list const HashSet<String> initial{ "a", "e" }; // copy constructor const HashSet<String> expected(initial); // check the result for (const auto &k : expected) Log::message("%s ", k.key.get());
- Create a new hash set by appending a vector of keys to it by using the fromKeys() function:
// create a vector of keys Vector<String> keys{ "1", "2", "3" }; // append keys to an empty hash set HashSet<String> hashSet = HashSet<String>::fromKeys(keys); // check the result for (const auto &k : hashSet) Log::message("%s ", k.key.get());
You can change the created hash set by using the class member functions described in the article. Check some of the usage examples:
- To add keys to the current hash set, you can use one of the append() or insert() functions:
Also it is possible to insert keys by using operator+=:
// create hash sets using initializer lists HashSet<String> initial{ "a", "b", "c", "d", "e" }; HashSet<String> to_add{ "m", "n", "o" }; // append keys of the "to_add" hash set to the "initial" hash set initial.append(to_add); // insert a key into the "initial" hash set initial.insert("s"); // check the result for (const auto &k : initial) Log::message("%s ", k.key.get());
// create hash sets using initializer lists HashSet<String> initial{ "a", "e" }; const HashSet<String> to_add{ "b", "c", "d" }; const HashSet<String> expected{ "a", "b", "c", "d", "e" }; // add one hash set to another initial += to_add; // check the result if (initial == expected) Log::message("The hash set has been added successfully\n");
If the key already exists in the hash set, it won't be added.// create a hash set HashSet<String> set{ "s" }; // assign one hash set to another const HashSet<String> original = set; // insert the hash set into the same hash set set.insert(set); // check the result - the hash set remains the same if (set == original) Log::message("Sets are equal\n");
// 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());
- To remove keys from the hash set, you can use the subtract() or remove() function:
// create hash sets HashSet<String> initial{ "a", "b", "c", "d", "e" }; const HashSet<String> to_subtract{ "a", "c", "e" }; const HashSet<String> expected{ "b", "d" }; // remove keys of one hash set from another initial.subtract(to_subtract); // check if the hash sets are equal if (initial == expected) Log::message("Sets are equal\n");
Also the operator-= can be used:// create hash sets HashSet<String> set{ "1", "2", "3", "4", "5" }; HashSet<String> to_remove{ "3", "5" }; // remove keys of one hash set from another set.remove(to_remove); // check the result for (const auto &k : set) Log::message("%s ", k.key.get());
// create hash sets HashSet<String> initial{ "a", "b", "c", "d", "e" }; const HashSet<String> to_subtract{ "a", "c", "e" }; const HashSet<String> expected{ "b", "d" }; // remove keys of one hash set from another initial -= to_subtract; // check if the hash sets are equal if (initial == expected) Log::message("Sets are equal\n");
- To clear the hash set, use the clear() function of the Hash class:
// 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");
HashSet Class
Members
HashSet ( ) #
Default constructor that produces an empty hash set.HashSet ( const HashSet& o ) #
Constructor. Creates a hash set by copying a source hash set.Arguments
- const HashSet& o - Hash set.
HashSet ( HashSet&& o ) #
Constructor. Creates a hash set by copying a source hash set.Arguments
- HashSet&& o - Hash set.
HashSet ( std::initializer_list<Key> list ) #
Constructor. Creates a hash set from given list of keys.Arguments
- std::initializer_list<Key> list - List of keys.
HashSet<Key, Counter> & operator= ( const HashSet& o ) #
Assignment operator for the hash set.Arguments
- const HashSet& o - Hash set.
HashSet<Key, Counter> & operator= ( HashSet&& o ) #
Assignment operator for the hash set.Arguments
- HashSet&& o - Hash set.
void append ( const Key& key ) #
Appends a key to the hash set.Arguments
- const Key& key - Key.
void append ( Key&& key ) #
Appends a key to the hash set.Arguments
- Key&& key - Key.
void append ( const HashSet& o ) #
Appends a given hash set to the current one.Arguments
- const HashSet& o - Hash set.
void append ( HashSet&& o ) #
Appends a given hash set to the current one.Arguments
- HashSet&& o - Hash set.
void remove ( const HashSet& o ) #
Removes all keys of a given hash set from the current one.Arguments
- const HashSet& o - Hash set.
void insert ( const Key& key ) #
Inserts a key into the hash set.Arguments
- const Key& key - Key.
void insert ( Key&& key ) #
Inserts a key into the hash set.Arguments
- Key&& key - Key.
void insert ( const HashSet& o ) #
Inserts a given hash set into the current one.Arguments
- const HashSet& o - Hash set.
void insert ( HashSet&& o ) #
Inserts a given hash set into the current one.Arguments
- HashSet&& o - Hash set.
void subtract ( const HashSet& o ) #
Removes all keys of a given hash set from the current one.Arguments
- const HashSet& o - Hash set.
bool operator== ( const HashSet& o ) const#
Checks if two hash sets are equal. The hash sets are considered equal if their keys are the same.Arguments
- const HashSet& o - Hash set.
Return value
Returns 1 if sets are equal; otherwise, 0 is returned.bool operator!= ( const HashSet& o ) const#
Checks if two hash sets are not equal. The hash sets are considered equal if their keys are the same.Arguments
- const HashSet& o - Hash set.
Return value
Returns 1 if sets are not equal; otherwise, 0 is returned.HashSet<Key, Counter> & operator+= ( const Key& key ) #
Inserts a given key into the hash set and returns the updated set.Arguments
- const Key& key - Key.
Return value
Updated hash set.HashSet<Key, Counter> & operator+= ( const HashSet& o ) #
Inserts a given hash set into the current one and returns the updated set.Arguments
- const HashSet& o - Hash set.
Return value
Updated hash set.HashSet<Key, Counter> & operator-= ( const Key& key ) #
Removes a given key from the current hash set and returns the updated set.Arguments
- const Key& key - Key.
Return value
Updated hash set.HashSet<Key, Counter> & operator-= ( const HashSet& o ) #
Removes a given hash set from the current one and returns the updated set.Arguments
- const HashSet& o - Hash set.
Return value
Updated hash set.HashSet<Key, Counter> fromKeys ( const Key* keys, size_t size ) #
Appends keys to the hash set and returns the updated set.Arguments
- const Key* keys - Keys pointer.
- size_t size - Keys size.
Return value
Updated hash set.HashSet<Key, Counter> fromKeys ( const Vector<Key>& keys ) #
Appends keys to the hash set and returns the updated set.Arguments
- const Vector<Key>& keys - Vector of keys.
Return value
Updated hash set.HashSet<Key, Counter> fromKeys ( Vector<Key>& keys ) #
Appends keys to the hash set and returns the updated set.Arguments
- Vector<Key>& keys - Vector of keys.
Return value
Updated hash set.Last update:
2022-10-10
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)