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 FBaseColMultiHashMap.h
19 * @brief This is the header file for the %MultiHashMap class.
21 * This header file contains the declarations of the %MultiHashMap class.
23 #ifndef _FBASE_COL_MULTI_HASH_MAP_H_
24 #define _FBASE_COL_MULTI_HASH_MAP_H_
26 #include <FBaseObject.h>
27 #include <FBaseColIComparer.h>
28 #include <FBaseColIHashCodeProvider.h>
29 #include <FBaseColIMultiMap.h>
31 namespace Tizen { namespace Base { namespace Collection
34 class _MultiHashMapEntry;
38 * @brief This class represents a collection of associated keys and values that are organized based on the hash code of the key.
42 * The %MultiHashMap class represents a collection of associated keys and values that are organized based on the hash code of the key.
43 * There is no limit on the number of elements with the same key, but duplicated elements with the same key are not allowed.
44 * The key and value cannot be @c null.
46 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/hashmap_multihashmap.htm">HashMap and MultiHashMap</a>.
48 * The following example demonstrates how to use the %MultiHashMap class.
53 * using namespace Tizen::Base;
54 * using namespace Tizen::Base::Collection;
57 * MyClass::MultiHashMapSample(void)
59 * MultiHashMap map(SingleObjectDeleter);
61 * // Constructs a MultiHashMap instance with default values for 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)
67 * map.Add(new String(L"Two"), new Integer(20)); // map.GetCount() : 4, map : (Zero -> 0), (One -> 1), (Two -> 2, 20)
69 * // Gets values with the specified key
70 * Integer* pValue = null;
71 * IEnumerator *pValueEnum = map.GetValuesN(String(L"Two"));
72 * while(pValueEnum->MoveNext() == E_SUCCESS)
74 * pValue = static_cast< Integer* > (pValueEnum->GetCurrent());
79 * // Removes values with the specified key // String(L"Two"), Integer(2), Integer(20) are removed
80 * map.Remove(String(L"Two"));
82 * // Uses an enumerator to access elements in the map
83 * IMapEnumerator* pMapEnum = map.GetMapEnumeratorN();
84 * String* pKey = null;
85 * while (pMapEnum->MoveNext() == E_SUCCESS)
87 * pKey = static_cast< String* > (pMapEnum->GetKey());
88 * pValue = static_cast< Integer* > (pMapEnum->GetValue());
93 * // Deallocates all objects
94 * // Because the destructor calls RemoveAll() internally, you do not need to call RemoveAll() to destroy all elements at the end.
99 class _OSP_EXPORT_ MultiHashMap
104 using IMultiMap::Add;
105 using IMultiMap::Remove;
106 using IMultiMap::RemoveAll;
107 using IMultiMap::SetValue;
108 using IMultiMap::Contains;
109 using IMultiMap::ContainsKey;
112 * The object is not fully constructed after this constructor is called. For full construction, @n
113 * the Construct() method must be called right after calling this constructor.
117 * @param[in] deleter The function pointer to type of the element deleter
118 * @remarks To create an owning collection, set the element deleter value as @c SingleObjectDeleter. This gives the collection the ownership of elements and the collection will destroy elements. @n
119 * 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.
120 * It means that you do not transfer the ownership of elements to the collection.
122 * @see SingleObjectDeleter()
123 * @see ArrayDeleter()
125 explicit MultiHashMap(DeleterFunctionType deleter = NoOpDeleter);
128 * This destructor overrides Tizen::Base::Object::~Object().
132 virtual ~MultiHashMap(void);
135 * Initializes a new instance of %MultiHashMap with the specified capacity and load factor.
139 * @return An error code
140 * @param[in] capacity The initial capacity
141 * @param[in] loadFactor The maximum ratio of elements to buckets
142 * @exception E_SUCCESS The method is successful.
143 * @exception E_INVALID_ARG A specified input parameter is invalid, or
144 * the @c capacity or the @c loadFactor is negative.
145 * @remarks The GetHashCode() method of the key object is used for hashing and the
146 * Equals() method of the key object is used for comparing the keys.
147 * @see MultiHashMap()
149 result Construct(int capacity = 16, float loadFactor = 0.75);
152 * Initializes a new instance of %MultiHashMap by copying the elements of the given map.
156 * @return An error code
157 * @param[in] map The map to copy
158 * @param[in] loadFactor The maximum ratio of elements to buckets @n
159 * If it is @c 0, the default load factor(0.75) is used.
160 * @exception E_SUCCESS The method is successful.
161 * @exception E_INVALID_ARG The specified @c loadFactor is negative.
162 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
163 * the @c map is modified during the operation of this method.
164 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
165 * @see MultiHashMap()
167 result Construct(const IMultiMap& map, float loadFactor = 0.75);
171 * Initializes a new instance of the %MultiHashMap class, with the specified initial capacity, load factor, hash code provider, and comparer.
175 * @return An error code
176 * @param[in] capacity The initial capacity @n
177 * If it is @c 0, the default capacity (16) is used.
178 * @param[in] loadFactor The maximum ratio of elements to buckets @n
179 * If it is @c 0, the default load factor (0.75) is used.
180 * @param[in] provider An instance of the IHashCodeProvider derived class that supplies the hash codes
181 * for all keys in this map
182 * @param[in] comparer An instance of the IComparer derived class to use when comparing keys
183 * @exception E_SUCCESS The method is successful.
184 * @exception E_INVALID_ARG A specified input parameter is invalid, or
185 * the @c capacity or the @c loadFactor is negative.
186 * @remarks The instances of hash code provider and comparer will not be deallocated later from this map.
187 * @see MultiHashMap()
189 result Construct(int capacity, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
192 * Initializes a new instance of the %MultiHashMap class by copying the elements of the specified map,
193 * with the specified load factor, hash code provider, and comparer.
197 * @return An error code
198 * @param[in] map A map to copy
199 * @param[in] loadFactor The maximum ratio of elements to buckets @n
200 * If it is @c 0, the default load factor (0.75) is used.
201 * @param[in] provider An instance of the IHashCodeProvider derived class that supplies the hash codes
202 * for all keys in this map
203 * @param[in] comparer An instance of the IComparer derived class to use when comparing keys
204 * @exception E_SUCCESS The method is successful.
205 * @exception E_INVALID_ARG A specified input parameter is invalid, or
206 * the @c loadFactor is negative.
207 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
208 * the @c map is modified during the operation of this method.
209 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
210 * The instances of hash code provider and comparer will not be deallocated later from this map.
211 * @see MultiHashMap()
213 result Construct(const IMultiMap& map, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
216 * Adds the specified key-value pair to this map.
220 * @return An error code
221 * @param[in] pKey The pointer to key to add
222 * @param[in] pValue The pointer to corresponding value to add
223 * @exception E_SUCCESS The method is successful.
224 * @exception E_OBJ_ALREADY_EXIST The specified pair of @c pKey and @c pValue already exists.
225 * @exception E_INVALID_ARG A specified input parameter is invalid, or
226 * the comparer has failed to compare the keys.
227 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
230 virtual result Add(Object* pKey, Object* pValue);
233 * Gets an enumerator of this map.
237 * @return An enumerator (an instance of the IEnumerator derived class) of this map, @n
238 * else @c null if some exception occurs
239 * @remarks If the key has multiple values, the enumeration proceeds as follows: {A: a}, {B: b}, {B: c}, {B, d}, {C: e}, ...
240 * The specific error code can be accessed using the GetLastResult() method.
241 * @see IMapEnumerator
243 virtual IEnumerator* GetEnumeratorN(void) const;
246 * Gets an enumerator of this map.
250 * @return An enumerator (an instance of the IMapEnumerator derived class) of this map, @n
251 * else @c null if some exception occurs
252 * @remarks If the key has multiple values, the enumeration proceeds as follows: {A: a}, {B: b}, {B: c}, {B, d}, {C: e}, ...
253 * The specific error code can be accessed using the GetLastResult() method.
256 virtual IMapEnumerator* GetMapEnumeratorN(void) const;
259 * Gets an enumerator of the values associated with the specified key.
263 * @return An enumerator (an instance of the IEnumerator derived class) of the values associated with the specified key, @n
264 * else @c null if some exception occurs
265 * @param[in] key A key to locate
266 * @exception E_SUCCESS The method is successful.
267 * @exception E_INVALID_ARG A specified input parameter is invalid, or
268 * the comparer has failed to compare the keys.
269 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
270 * @remarks The specific error code can be accessed using the GetLastResult() method.
273 virtual IEnumerator* GetValuesN(const Object& key) const;
276 * Gets a list of all unique keys in this map.
280 * @return A list of all unique keys in this map
281 * @remarks The %IList stores just the pointers to the elements in the map, not the elements themselves.
282 * The specific error code can be accessed using the GetLastResult() method.
285 virtual IList* GetKeysN(void) const;
288 * Gets a list of all the values in this map.
292 * @return A list of all the values in this map
293 * @remarks The IList stores just the pointers to the elements in the map, not the elements themselves.
294 * The specific error code can be accessed using the GetLastResult() method.
297 virtual IList* GetValuesN(void) const;
300 * Removes all the values with the specified key.
304 * @return An error code
305 * @param[in] key The key to remove
306 * @exception E_SUCCESS The method is successful.
307 * @exception E_INVALID_ARG A specified input parameter is invalid, or
308 * the comparer has failed to compare keys.
309 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
312 virtual result Remove(const Object& key);
315 * Removes the specified value associated with the specified key.
319 * @return An error code
320 * @param[in] key The key whose mapping is to remove from the map
321 * @param[in] value The value to remove
322 * @exception E_SUCCESS The method is successful.
323 * @exception E_INVALID_ARG A specified input parameter is invalid, or
324 * the comparer has failed to compare the keys.
325 * @exception E_OBJ_NOT_FOUND The specified @c key and @c value pair is not found in the map.
326 * @remarks The specified key is also removed if there are no more values associated with it.
329 virtual result Remove(const Object& key, const Object& value);
332 * Removes all the object pointers in the @c collection. @n
336 * @remarks This method can be called before deleting @c collection.
338 virtual void RemoveAll(void);
341 * Sets the value associated with the given key with a new value.
345 * @return An error code
346 * @param[in] key The key for which the associated value needs to replace
347 * @param[in] value The value to replace
348 * @param[in] pNewValue The pointer to new value to replace the existing value
349 * @exception E_SUCCESS The method is successful.
350 * @exception E_INVALID_ARG A specified input parameter is invalid, or
351 * the comparer has failed to compare the keys.
352 * @exception E_OBJ_NOT_FOUND The specified @c key and @c value pair is not found in the map.
353 * @remarks To add a new key-value pair, use the Add() method.
357 virtual result SetValue(const Object& key, const Object& value, Object* pNewValue);
360 * Gets the number of values currently stored in this map.
364 * @return The number of values currently stored in this map
366 virtual int GetCount(void) const;
369 * Gets the number of values whose key matches the key.
373 * @return An error code
374 * @param[in] key A key to locate
375 * @param[out] count The number of values whose key is the @c key
376 * @exception E_SUCCESS The method is successful.
377 * @exception E_INVALID_ARG A specified input parameter is invalid, or
378 * the comparer has failed to compare the keys.
379 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
381 virtual result GetCount(const Object& key, int& count) const;
384 * Checks whether the map contains the specified key and value.
388 * @return @c true if the map contains the specified key and value pair, @n
390 * @param[in] key The key to locate
391 * @param[in] value The value to locate
392 * @exception E_SUCCESS The method is successful.
393 * @exception E_INVALID_ARG A specified input parameter is invalid, or
394 * the comparer has failed to compare the keys.
395 * @remarks The specific error code can be accessed using the GetLastResult() method.
397 * @see ContainsValue()
399 virtual bool Contains(const Object& key, const Object& value) const;
402 * Checks whether the map contains the specified key.
406 * @return @c true if the map contains the specified key, @n
408 * @param[in] key The key to locate
409 * @exception E_SUCCESS The method is successful.
410 * @exception E_INVALID_ARG A specified input parameter is invalid, or
411 * the comparer has failed to compare the keys.
412 * @remarks The specific error code can be accessed using the GetLastResult() method.
413 * @see ContainsValue()
416 virtual bool ContainsKey(const Object& key) const;
419 * Checks whether the map contains the specified value.
423 * @return @c true if the map contains the specified value, @n
425 * @param[in] value The value to locate
430 virtual bool ContainsValue(const Object& value) const;
433 * Compares the specified instance to the current instance for equality.
437 * @return @c true if the two instances are equal, @n
439 * @param[in] obj The object to compare with the current instance
440 * @remarks This method returns @c true only if the specified object is also an instance of %MultiHashMap class,
441 * both maps have the same number of elements, and both maps contain the same elements.
443 virtual bool Equals(const Object& obj) const;
446 * Gets the hash value of the current instance.
450 * @return The hash value of the current instance
451 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
452 * the used hash function must generate a random distribution for all inputs.
454 virtual int GetHashCode(void) const;
457 * Gets the element deleter of the collection.
461 * @return A function pointer to the existing element deleter
463 virtual DeleterFunctionType GetDeleter(void) const;
467 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
469 * @param[in] map An instance of %MultiHashMap to initialize the current instance
471 MultiHashMap(const MultiHashMap& map);
474 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
476 * @param[in] map An instance of %MultiHashMap
478 MultiHashMap& operator =(const MultiHashMap& map);
481 * Copies all the pairs from the specified map to this map.
483 * @return An error code
484 * @param[in] map The map to copy
485 * @exception E_SUCCESS The method is successful.
486 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation. @n
487 * The @c map is modified during the operation of this method.
489 result AddAll(const IMultiMap& map);
492 * Gets a hash value for the specified object.
494 * @return An @c int hash value for the specified object
495 * @param[in] obj The object to get hash value
497 int Hash(const Object& obj) const;
500 * Rehashes the contents of this map into a new array with a
501 * larger capacity. @n
502 * This method is called automatically when the number of keys in this map reaches its threshold.
504 * @return An error code
505 * @param[in] newCapacity The new capacity @n
506 * It must be a power of two and be greater than current capacity.
507 * @exception E_SUCCESS The method is successful.
509 result Resize(int newCapacity);
512 * Clears all key-value pairs in this map.
517 * Sets the element deleter of the collection.
521 * @param[in] deleter A function pointer to the element deleter to set
523 virtual void SetDeleter(DeleterFunctionType deleter);
525 _MultiHashMapEntry** __pTable;
530 IHashCodeProvider* __pProvider;
531 IComparer* __pComparer;
532 bool __needToRemoveProviderComparer;
534 DeleterFunctionType __deleter;
535 static const int DEFAULT_CAPACITY = 16;
536 static const float DEFAULT_LOAD_FACTOR;
538 friend class _MultiHashMapEnumerator;
539 friend class _MultiHashMapImpl;
540 class _MultiHashMapImpl* __pMultiHashMapImpl;
544 }}} // Tizen::Base::Collection
546 #endif //_FBASE_COL_MULTI_HASH_MAP_H_