2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FBaseColHashMap.h
19 * @brief This is the header file for the %HashMap class.
21 * This header file contains the declarations of the %HashMap class.
23 #ifndef _FBASE_COL_HASH_MAP_H_
24 #define _FBASE_COL_HASH_MAP_H_
26 #include <FBaseColIComparer.h>
27 #include <FBaseColIHashCodeProvider.h>
28 #include <FBaseColIMap.h>
30 namespace Tizen { namespace Base { namespace Collection
37 * @brief This class represents a collection of associated keys and values that are organized based on the hash code of the key.
41 * The %HashMap class contains unique keys and each key maps to one single value.
42 * The key and value cannot be a @c null reference.
44 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/hashmap_multihashmap.htm">HashMap and MultiHashMap</a>.
46 * The following example demonstrates how to use the %HashMap class to create and initialize a %HashMap instance and to use its methods.
52 * using namespace Tizen::Base;
53 * using namespace Tizen::Base::Collection;
57 * MyClass::HashMapSample(void)
59 * HashMap map(SingleObjectDeleter);
61 * // Constructs a %HashMap instance with default capacity, load factor, hash code provider, and comparer
64 * map.Add(new String(L"Zero"), new Integer(0)); // map.GetCount() : 1, map : (Zero -> 0)
65 * map.Add(new String(L"One"), new Integer(1)); // map.GetCount() : 2, map : (Zero -> 0), (one -> 1)
66 * map.Add(new String(L"Two"), new Integer(2)); // map.GetCount() : 3, map : (Zero -> 0), (one -> 1), (Two -> 2)
68 * // Gets a value with the specified key
69 * Integer* pValue = static_cast< Integer* > (map.GetValue(String(L"Zero"))); // pValue : 0
71 * // Removes the value with the specified key
72 * map.Remove(String(L"Zero")); // map.GetCount() : 2, map : (one -> 1), (Two -> 2)
74 * // Uses an enumerator to access elements in the list
75 * IMapEnumerator* pMapEnum = map.GetMapEnumeratorN();
76 * String* pKey = null;
77 * while (pMapEnum->MoveNext() == E_SUCCESS)
79 * pKey = static_cast< String* > (pMapEnum->GetKey());
80 * pValue = static_cast< Integer* > (pMapEnum->GetValue());
85 * // Deallocates all objects
86 * // Because the destructor calls RemoveAll() internally, you do not need to call RemoveAll() to destroy all elements at the end.
91 class _OSP_EXPORT_ HashMap
98 using IMap::RemoveAll;
100 using IMap::ContainsKey;
102 * The object is not fully constructed after this constructor is called. For full construction, @n
103 * the Construct() method must be called right after calling this constructor.
107 * @param[in] deleter The function pointer to type of the element deleter
108 * @remarks To create an owing collection, set the element deleter value as @c SingleObjectDeleter. This gives the collection the ownership of elements and the collection will destroy elements. @n
109 * On the other hand, to create a non-owning collection, you do not need to set the element deleter value, as @c NoOpDeleter is the default element deleter.
110 * It means that you do not transfer the ownership of elements to the collection.
112 * @see SingleObjectDeleter()
113 * @see ArrayDeleter()
115 explicit HashMap(DeleterFunctionType deleter = NoOpDeleter);
118 * This destructor overrides Tizen::Base::Object::~Object().
122 virtual ~HashMap(void);
125 * Initializes an instance of an empty %HashMap class with the initial capacity and load factor.
129 * @return An error code
130 * @param[in] capacity The initial capacity
131 * @param[in] loadFactor The maximum ratio of elements to buckets
132 * @exception E_SUCCESS The method is successful.
133 * @exception E_INVALID_ARG A specified input parameter is invalid, @n
134 * the @c capacity or the @c loadFactor is negative.
135 * @remarks The GetHashCode() method of key objects is used for hashing and
136 * the Equals() method of key objects is used for comparing keys.
139 result Construct(int capacity = 16, float loadFactor = 0.75);
142 * Initializes an instance of a %HashMap class by copying the elements of the specified @c map.
146 * @return An error code
147 * @param[in] map The map to copy
148 * @param[in] loadFactor The maximum ratio of elements to buckets
149 * @exception E_SUCCESS The method is successful.
150 * @exception E_INVALID_ARG A specified input parameter is invalid, or
151 * the @c loadFactor is negative.
152 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
153 * the @c map is modified during the operation of this method.
154 * @remarks This method performs a shallow copy. It copies only the pointer; not the element itself.
157 result Construct(const IMap& map, float loadFactor = 0.75);
160 * Initializes an instance of an empty %HashMap class with the specified initial capacity, load factor, hash code provider, and comparer.
164 * @return An error code
165 * @param[in] capacity The initial capacity @n
166 * If it is @c 0, the default capacity(16) is used.
167 * @param[in] loadFactor The maximum ratio of elements to buckets @n
168 * If it is @c 0, the default load factor(0.75) is used.
169 * @param[in] provider An instance of the IHashCodeProvider derived class that supplies the hash codes for all the keys in this map
170 * @param[in] comparer An instance of the IComparer derived class to use when comparing the keys
172 * @exception E_SUCCESS The method is successful.
173 * @exception E_INVALID_ARG A specified input parameter is invalid, or
174 * the @c capacity or the @c loadFactor is negative.
175 * @remarks The instances of hash code provider and comparer will not be deallocated later from this map.
178 result Construct(int capacity, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
181 * Initializes an instance of a %HashMap class by copying the elements of the specified map with the specified load factor, hash code provider, and comparer.
185 * @return An error code
186 * @param[in] map The map to copy
187 * @param[in] loadFactor The maximum ratio of elements to buckets @n
188 * If it is @c 0, the default load factor(0.75) is used.
189 * @param[in] provider An instance of the IHashCodeProvider derived class that supplies the hash codes for all keys in this map
190 * @param[in] comparer An instance of the IComparer derived class to use when comparing keys
191 * @exception E_SUCCESS The method is successful.
192 * @exception E_INVALID_ARG A specified input parameter is invalid, or
193 * the @c loadFactor is negative.
194 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
195 * the @c map is modified during the operation of this method.
196 * @remarks This method performs a shallow copy. It copies only the pointer; not the element itself.
197 * @remarks The instances of hash code provider and comparer will not be deallocated later from this map.
200 result Construct(const IMap& map, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
203 * Adds the specified key-value pair to a map.
207 * @return An error code
208 * @param[in] pKey The pointer to key of the value to add
209 * @param[in] pValue The pointer to value to add
210 * @exception E_SUCCESS The method is successful.
211 * @exception E_INVALID_ARG A specified input parameter is invalid, or
212 * the comparer has failed to compare the keys.
213 * @exception E_OBJ_ALREADY_EXIST The specified @c pKey already exists.
214 * @remarks This method performs a shallow copy. It adds only the pointer; not the element itself.
217 virtual result Add(Object* pKey, Object* pValue);
220 * Gets an enumerator (an instance of the IMapEnumerator derived class) of this map.
224 * @return An instance of the IMapEnumerator derived class, if successful @n
225 * else @c null if an exception occurs
226 * @remarks The specific error code can be accessed using the GetLastResult() method.
227 * @see Tizen::Base::Collection::IEnumerator
228 * @see Tizen::Base::Collection::IMapEnumerator
230 virtual IEnumerator* GetEnumeratorN(void) const;
233 * Gets the elements of the map in an instance of the IMapEnumerator derived class.
237 * @return An instance of the IMapEnumerator derived class, @n
238 * else @c null if an exception occurs
239 * @remarks The specific error code can be accessed using the GetLastResult() method.
240 * @see Tizen::Base::Collection::IEnumerator
241 * @see Tizen::Base::Collection::IMapEnumerator
243 virtual IMapEnumerator* GetMapEnumeratorN(void) const;
246 * Gets the value associated with the specified @c key.
250 * @return The value associated with the key, @n
251 * else @c null if an exception occurs
252 * @param[in] key The key to locate
253 * @exception E_SUCCESS The method is successful.
254 * @exception E_INVALID_ARG The specified input parameter is invalid, or
255 * the comparer has failed to compare the keys.
256 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
257 * @remarks The specific error code can be accessed using the GetLastResult() method.
260 virtual const Object* GetValue(const Object& key) const;
263 * Gets the value associated with the specified @c key.
267 * @return The value associated with the key, @n
268 * else @c null if an exception occurs
269 * @param[in] key The key to locate
270 * @exception E_SUCCESS The method is successful.
271 * @exception E_INVALID_ARG A specified input parameter is invalid, or
272 * the comparer has failed to compare the keys.
273 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
274 * @remarks The specific error code can be accessed using the GetLastResult() method.
277 virtual Object* GetValue(const Object& key);
280 * Gets a list of all the keys in a map.
284 * @return A pointer to an IList object containing all the keys in the map, @n
285 * else @c null if an exception occurs
286 * @remarks The order of the keys is the same as the corresponding values in the IList interface returned by the GetValuesN() method.
287 * The IList stores just the pointers to the elements in the map, not the elements themselves.
288 * The specific error code can be accessed using the GetLastResult() method.
291 virtual IList* GetKeysN(void) const;
294 * Gets all the values in the map.
298 * @return A pointer to an IList object containing all the values in the map, @n
299 * else @c null if an exception occurs
300 * @remarks The IList stores just the pointers to the elements in the map, not the elements themselves.
301 * @remarks The specific error code can be accessed using the GetLastResult() method.
304 virtual IList* GetValuesN(void) const;
307 * Removes the values associated with the specified @c key.
311 * @return An error code
312 * @param[in] key The key to remove
313 * @exception E_SUCCESS The method is successful.
314 * @exception E_INVALID_ARG The specified input parameter is invalid, or
315 * the comparer has failed to compare keys.
316 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
319 virtual result Remove(const Object& key);
322 * Removes all the object pointers in the collection. @n
323 * The %RemoveAll() method can be called before deleting a collection.
327 virtual void RemoveAll(void);
330 * Sets the value associated with the specified @c key by allocating it a new value.
334 * @return An error code
335 * @param[in] key The key whose value is to replace
336 * @param[in] pValue The pointer to new value to replace
337 * @exception E_SUCCESS The method is successful.
338 * @exception E_INVALID_ARG A specified input parameter is invalid, or
339 * the comparer has failed to compare keys.
340 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
341 * @remarks To add a new key-value pair, use the Add() method.
345 virtual result SetValue(const Object& key, Object* pValue);
348 * Gets the number of pairs currently stored in the map.
352 * @return The pairs stored in the map
354 virtual int GetCount(void) const;
357 * Checks whether the map contains the specified @c key.
361 * @return @c true if the map contains the specified @c key, @n
363 * @param[in] key The key to locate
364 * @exception E_SUCCESS The method is successful.
365 * @exception E_INVALID_ARG The specified input parameter is invalid, or
366 * the comparer has failed to compare the keys.
367 * @remarks The specific error code can be accessed using the GetLastResult() method.
368 * @see ContainsValue()
370 virtual bool ContainsKey(const Object& key) const;
373 * Checks whether the map contains the specified @c value.
377 * @return @c true if the map contains the specified @c value, @n
379 * @param[in] value The value to locate
383 virtual bool ContainsValue(const Object& value) const;
386 * Compares two instances of the %HashMap class.
390 * @return @c true if the two instances match, @n
392 * @param[in] obj The object to compare with the current instance
393 * @remarks This method returns @c true if and only if the two instances contain the same number of elements and all the elements contained in each other.
395 virtual bool Equals(const Object& obj) const;
398 * Gets the hash value of the current instance.
402 * @return The hash value of the current instance
403 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
404 * the used hash function must generate a random distribution for all inputs.
406 virtual int GetHashCode(void) const;
409 * Gets the element deleter of the collection.
413 * @return A function pointer to the existing element deleter
415 virtual DeleterFunctionType GetDeleter(void) const;
419 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
421 * @param[in] map The instance of the %HashMap class to copy from
423 HashMap(const HashMap& map);
426 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
428 * @param[in] map An instance of %HashMap
430 HashMap& operator =(const HashMap& map);
433 * Copies all the pairs from the specified @c map to this map.
435 * @return An error code
436 * @param[in] map The map to copy
437 * @exception E_SUCCESS The method is successful.
438 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation. @n
439 * The @c map is modified during the operation of this method.
441 result AddAll(const IMap& map);
444 * Gets the hash value for the specified object.
446 * @return The hash value for the specified object
449 int Hash(const Object& obj) const;
452 * Rehashes the content of a map to a new array with a greater capacity.
454 * @return An error code
455 * @param[in[ newCapacity The new capacity @n
456 * It must be a power of two and must be greater than the current capacity.
457 * @exception E_SUCCESS The method is successful.
458 * @remarks This method is called automatically when the number of keys in a map reaches its threshold.
460 result Resize(int newCapacity);
463 * Clears all key-value pairs in a map.
468 * Sets the element deleter of the collection.
472 * @param[in] deleter A function pointer to the element deleter to set
474 virtual void SetDeleter(DeleterFunctionType deleter);
476 _HashMapEntry** __pTable;
481 IHashCodeProvider* __pProvider;
482 IComparer* __pComparer;
483 bool __needToRemoveProviderComparer;
485 DeleterFunctionType __deleter;
486 static const int DEFAULT_CAPACITY = 16;
487 static const float DEFAULT_LOAD_FACTOR;
489 friend class _HashMapEnumerator;
490 friend class _HashMapImpl;
491 class _HashMapImpl* __pHashMapImpl;
495 }}} // Tizen::Base::Collection
497 #endif //_FBASE_COL_HASH_MAP_H_