The HashMap class contains unique keys and each key maps to one single value. The key and value cannot be a null reference.
For more information on the class features, see HashMap and MultiHashMap.
The following example demonstrates how to create and initialize a HashMap instance and how to use its methods.
#include <FBase.h> using namespace Osp::Base; using namespace Osp::Base::Collection; void MyClass::HashMapSample(void) { HashMap map; // Construct a HashMap instance with default capacity, load factor, hash code provider, and comparer. map.Construct(); String* pStr0 = new String(L"Zero"); String* pStr1 = new String(L"One"); String* pStr2 = new String(L"Two"); map.Add(*pStr0, *(new Integer(0))); // map.GetCount() : 1, map : (Zero -> 0) map.Add(*pStr1, *(new Integer(1))); // map.GetCount() : 2, map : (Zero -> 0), (one -> 1) map.Add(*pStr2, *(new Integer(2))); // map.GetCount() : 3, map : (Zero -> 0), (one -> 1), (Two -> 2) // Get a value with the specified key (GetValue) String* pKey = new String(L"Zero"); Integer* pValue = static_cast<Integer*> (map.GetValue(*pKey)); // pValue : 0 // Remove a value with the specified key (Remove) map.Remove(*pKey, true); delete pKey; // Use enumerator to access elements in the list IMapEnumerator* pMapEnum = map.GetMapEnumeratorN(); while (pMapEnum->MoveNext() == E_SUCCESS) { pKey = static_cast<String*> (pMapEnum->GetKey()); pValue = static_cast<Integer*> (pMapEnum->GetValue()); } delete pMapEnum; // Deallocate all objects map.RemoveAll(true); }
Public Member Functions | |
| virtual result | Add (const Object &key, const Object &value) |
| result | Construct (const IMap &map, float loadFactor=0.75) |
| result | Construct (int capacity, float loadFactor, const IHashCodeProvider &provider, const IComparer &comparer) |
| result | Construct (int capacity=16, float loadFactor=0.75) |
| result | Construct (const IMap &map, float loadFactor, const IHashCodeProvider &provider, const IComparer &comparer) |
| virtual result | ContainsKey (const Object &key, bool &out) const |
| virtual bool | ContainsValue (const Object &value) const |
| virtual bool | Equals (const Object &obj) const |
| virtual int | GetCount (void) const |
| virtual IEnumerator * | GetEnumeratorN (void) const |
| virtual IList * | GetKeysN (void) const |
| virtual IMapEnumerator * | GetMapEnumeratorN (void) const |
| virtual const Object * | GetValue (const Object &key) const |
| virtual Object * | GetValue (const Object &key) |
| virtual IList * | GetValuesN (void) const |
| HashMap (void) | |
| virtual result | Remove (const Object &key, bool deallocate=false) |
| virtual void | RemoveAll (bool deallocate=false) |
| virtual result | SetValue (const Object &key, const Object &value, bool deallocate=false) |
| virtual | ~HashMap (void) |
| Osp::Base::Collection::HashMap::HashMap | ( | void | ) |
This is the default constructor for this class.
| virtual Osp::Base::Collection::HashMap::~HashMap | ( | void | ) | [virtual] |
This is the destructor for this class.
| virtual result Osp::Base::Collection::HashMap::Add | ( | const Object & | key, |
| const Object & | value | ||
| ) | [virtual] |
Adds the specified key-value pair to a map.
| [in] | key | The key of the value to add |
| [in] | value | The value to add |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, or the comparer has failed to compare the keys. |
| E_OBJ_ALREADY_EXIST | The specified key already exists. |
| E_OUT_OF_MEMORY | The memory is insufficient. |
Implements Osp::Base::Collection::IMap.
| result Osp::Base::Collection::HashMap::Construct | ( | int | capacity = 16, |
| float | loadFactor = 0.75 |
||
| ) |
Initializes an instance of an empty HashMap class with the initial capacity and load factor.
| [in] | capacity | The initial capacity |
| [in] | loadFactor | The maximum ratio of elements to buckets |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, the capacity or the loadFactor is negative. |
| E_OUT_OF_MEMORY | The memory is insufficient. |
| result Osp::Base::Collection::HashMap::Construct | ( | const IMap & | map, |
| float | loadFactor, | ||
| const IHashCodeProvider & | provider, | ||
| const IComparer & | comparer | ||
| ) |
Initializes an instance of a HashMap class by copying the elements of a specified map with a specified load factor, hash code provider, and comparer.
| [in] | map | The map to copy |
| [in] | loadFactor | The maximum ratio of elements to buckets If it is 0, the default load factor(0.75) is used. |
| [in] | provider | An instance of the IHashCodeProvider derived class that supplies the hash codes for all keys in this map |
| [in] | comparer | An instance of the IComparer derived class to use when comparing keys |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, or the loadFactor is negative. |
| E_INVALID_OPERATION | The current state of the instance prohibits the execution of the specified operation, or the map is modified during the operation of this method. |
| E_OUT_OF_MEMORY | The memory is insufficient. |
Initializes an instance of a HashMap class by copying the elements of the specified map.
| [in] | map | The map to copy |
| [in] | loadFactor | The maximum ratio of elements to buckets |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, or the loadFactor is negative. |
| E_INVALID_OPERATION | The current state of the instance prohibits the execution of the specified operation, or the map is modified during the operation of this method. |
| E_OUT_OF_MEMORY | Insufficient memory. |
| result Osp::Base::Collection::HashMap::Construct | ( | int | capacity, |
| float | loadFactor, | ||
| const IHashCodeProvider & | provider, | ||
| const IComparer & | comparer | ||
| ) |
Initializes an instance of an empty HashMap class with the specified initial capacity, load factor, hash code provider, and comparer.
| [in] | capacity | The initial capacity If it is 0, the default capacity(16) is used. |
| [in] | loadFactor | The maximum ratio of elements to buckets If it is 0, the default load factor(0.75) is used. |
| [in] | provider | An instance of the IHashCodeProvider derived class that supplies the hash codes for all the keys in this map |
| [in] | comparer | An instance of the IComparer derived class to use when comparing the keys |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, or the capacity or the loadFactor is negative. |
| E_OUT_OF_MEMORY | The memory is insufficient. |
| virtual result Osp::Base::Collection::HashMap::ContainsKey | ( | const Object & | key, |
| bool & | out | ||
| ) | const [virtual] |
Checks whether the map contains the specified key.
| [in] | key | The key to locate |
| [out] | out | Set to true if the map contains the specified key, else false |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, or the comparer has failed to compare the keys. |
Implements Osp::Base::Collection::IMap.
| virtual bool Osp::Base::Collection::HashMap::ContainsValue | ( | const Object & | value | ) | const [virtual] |
Checks whether the map contains the specified value.
true if the map contains the specified value, false | [in] | value | The value to locate |
Implements Osp::Base::Collection::IMap.
| virtual bool Osp::Base::Collection::HashMap::Equals | ( | const Object & | obj | ) | const [virtual] |
Compares two instances of the HashMap class.
true if the two instances match, false | [in] | obj | The object to compare with the current instance |
true if and only if the two instances contain the same number of elements and all the elements contained in each other. Reimplemented from Osp::Base::Object.
| virtual int Osp::Base::Collection::HashMap::GetCount | ( | void | ) | const [virtual] |
Gets the number of pairs currently stored in the map.
Implements Osp::Base::Collection::ICollection.
| virtual IEnumerator* Osp::Base::Collection::HashMap::GetEnumeratorN | ( | void | ) | const [virtual] |
Gets an enumerator (an instance of the IMapEnumerator derived class) of this map.
null if an exception occurs | E_SUCCESS | The method is successful. |
| E_OUT_OF_MEMORY | The memory is insufficient. |
Implements Osp::Base::Collection::ICollection.
| virtual IList* Osp::Base::Collection::HashMap::GetKeysN | ( | void | ) | const [virtual] |
Gets a list of all the keys in a map.
null if an exception occurs | E_SUCCESS | The method is successful. |
| E_OUT_OF_MEMORY | The memory is insufficient. |
Implements Osp::Base::Collection::IMap.
| virtual IMapEnumerator* Osp::Base::Collection::HashMap::GetMapEnumeratorN | ( | void | ) | const [virtual] |
Gets the elements of the map in an instance of the IMapEnumerator derived class.
null if an exception occurs | E_SUCCESS | The method is successful. |
| E_OUT_OF_MEMORY | The memory is insufficient. |
Implements Osp::Base::Collection::IMap.
| virtual const Object* Osp::Base::Collection::HashMap::GetValue | ( | const Object & | key | ) | const [virtual] |
Gets the value associated with the specified key.
null if an exception occurs | [in] | key | The key to locate |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, or the comparer has failed to compare the keys. |
| E_OBJ_NOT_FOUND | The specified key is not found in the map. |
Implements Osp::Base::Collection::IMap.
Gets the value associated with the specified key.
null if an exception occurs | [in] | key | The key to locate |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, or the comparer has failed to compare the keys. |
| E_OBJ_NOT_FOUND | The specified key is not found in the map. |
Implements Osp::Base::Collection::IMap.
| virtual IList* Osp::Base::Collection::HashMap::GetValuesN | ( | void | ) | const [virtual] |
Gets all the values in the map.
null if an exception occurs | E_SUCCESS | The method is successful. |
| E_OUT_OF_MEMORY | The memory is insufficient. |
Implements Osp::Base::Collection::IMap.
| virtual result Osp::Base::Collection::HashMap::Remove | ( | const Object & | key, |
| bool | deallocate = false |
||
| ) | [virtual] |
Removes the values associated with a specified key.
| [in] | key | The key to be removed |
| [in] | deallocate | Set to true to deallocate the object, else false |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, or the comparer has failed to compare keys. |
| E_OBJ_NOT_FOUND | The specified key is not found in the map. |
Implements Osp::Base::Collection::IMap.
| virtual void Osp::Base::Collection::HashMap::RemoveAll | ( | bool | deallocate = false | ) | [virtual] |
Removes all the object pointers in the collection.
If the deallocate param is true, it also removes all of the objects. This method can be called before deleting a collection.
| [in] | deallocate | Set to true to deallocate all the objects, else false |
Implements Osp::Base::Collection::IMap.
| virtual result Osp::Base::Collection::HashMap::SetValue | ( | const Object & | key, |
| const Object & | value, | ||
| bool | deallocate = false |
||
| ) | [virtual] |
Replaces the value associated with a specified key by allocating it a new value.
| [in] | key | The key whose value is to be replaced |
| [in] | value | The new value to replace |
| [in] | deallocate | Set to true to deallocate the object, else false |
| E_SUCCESS | The method is successful. |
| E_INVALID_ARG | A specified input parameter is invalid, or the comparer has failed to compare keys. |
| E_OBJ_NOT_FOUND | The specified key is not found in the map. |
Implements Osp::Base::Collection::IMap.