2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FBaseColMultiHashMap.h
20 * @brief This is the header file for the %MultiHashMap class.
22 * This header file contains the declarations of the %MultiHashMap class.
24 #ifndef _FBASE_COL_MULTI_HASH_MAP_H_
25 #define _FBASE_COL_MULTI_HASH_MAP_H_
27 #include <FBaseObject.h>
28 #include <FBaseColIComparer.h>
29 #include <FBaseColIHashCodeProvider.h>
30 #include <FBaseColIMultiMap.h>
33 namespace Tizen { namespace Base { namespace Collection
36 class _MultiHashMapEntry;
40 * @brief This class represents a collection of associated keys and values that are organized based on the hash code of the key.
44 * The %MultiHashMap class represents a collection of associated keys and values that are organized based on the hash code of the key.
45 * There is no limit on the number of elements with the same key, but duplicated elements with the same key are not allowed.
46 * The key and value cannot be @c null.
48 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/hashmap_multihashmap.htm">HashMap and MultiHashMap</a>.
50 * The following example demonstrates how to use the %MultiHashMap class.
55 * using namespace Tizen::Base;
56 * using namespace Tizen::Base::Collection;
59 * MyClass::MultiHashMapSample(void)
61 * MultiHashMap map(SingleObjectDeleter);
63 * // Constructs a MultiHashMap instance with default values for capacity, load factor, hash code provider, and comparer
66 * map.Add(new String(L"Zero"), new Integer(0)); // map.GetCount() : 1, map : (Zero -> 0)
67 * map.Add(new String(L"One"), new Integer(1)); // map.GetCount() : 2, map : (Zero -> 0), (one -> 1)
68 * map.Add(new String(L"Two"), new Integer(2)); // map.GetCount() : 3, map : (Zero -> 0), (one -> 1), (Two -> 2)
69 * map.Add(new String(L"Two"), new Integer(20)); // map.GetCount() : 4, map : (Zero -> 0), (One -> 1), (Two -> 2, 20)
71 * // Gets values with the specified key
72 * Integer* pValue = null;
73 * IEnumerator *pValueEnum = map.GetValuesN(String(L"Two"));
74 * while(pValueEnum->MoveNext() == E_SUCCESS)
76 * pValue = static_cast< Integer* > (pValueEnum->GetCurrent());
81 * // Removes values with the specified key // String(L"Two"), Integer(2), Integer(20) are removed
82 * map.Remove(String(L"Two"));
84 * // Uses an enumerator to access elements in the map
85 * IMapEnumerator* pMapEnum = map.GetMapEnumeratorN();
86 * String* pKey = null;
87 * while (pMapEnum->MoveNext() == E_SUCCESS)
89 * pKey = static_cast< String* > (pMapEnum->GetKey());
90 * pValue = static_cast< Integer* > (pMapEnum->GetValue());
95 * // Deallocates all objects
96 * // Because the destructor calls RemoveAll() internally, you do not need to call RemoveAll() to destroy all elements at the end.
101 class _OSP_EXPORT_ MultiHashMap
106 using IMultiMap::Add;
107 using IMultiMap::Remove;
108 using IMultiMap::RemoveAll;
109 using IMultiMap::SetValue;
110 using IMultiMap::Contains;
111 using IMultiMap::ContainsKey;
114 * The object is not fully constructed after this constructor is called. For full construction, @n
115 * the Construct() method must be called right after calling this constructor.
119 * @param[in] deleter The function pointer to type of the element deleter
120 * @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
121 * 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.
122 * It means that you do not transfer the ownership of elements to the collection.
124 * @see SingleObjectDeleter()
125 * @see ArrayDeleter()
127 explicit MultiHashMap(DeleterFunctionType deleter = NoOpDeleter);
130 * This destructor overrides Tizen::Base::Object::~Object().
134 virtual ~MultiHashMap(void);
137 * Initializes a new instance of %MultiHashMap with the specified capacity and load factor.
141 * @return An error code
142 * @param[in] capacity The initial capacity
143 * @param[in] loadFactor The maximum ratio of elements to buckets
144 * @exception E_SUCCESS The method is successful.
145 * @exception E_INVALID_ARG A specified input parameter is invalid, or
146 * the @c capacity or the @c loadFactor is negative.
147 * @remarks The GetHashCode() method of the key object is used for hashing and the
148 * Equals() method of the key object is used for comparing the keys.
149 * @see MultiHashMap()
151 result Construct(int capacity = 16, float loadFactor = 0.75);
154 * Initializes a new instance of %MultiHashMap by copying the elements of the given map.
158 * @return An error code
159 * @param[in] map The map to copy
160 * @param[in] loadFactor The maximum ratio of elements to buckets @n
161 * If it is @c 0, the default load factor(0.75) is used.
162 * @exception E_SUCCESS The method is successful.
163 * @exception E_INVALID_ARG The specified @c loadFactor is negative.
164 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
165 * the @c map is modified during the operation of this method.
166 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
167 * @see MultiHashMap()
169 result Construct(const IMultiMap& map, float loadFactor = 0.75);
173 * Initializes a new instance of the %MultiHashMap class, with the specified initial capacity, load factor, hash code provider, and comparer.
177 * @return An error code
178 * @param[in] capacity The initial capacity @n
179 * If it is @c 0, the default capacity (16) is used.
180 * @param[in] loadFactor The maximum ratio of elements to buckets @n
181 * If it is @c 0, the default load factor (0.75) is used.
182 * @param[in] provider An instance of the IHashCodeProvider derived class that supplies the hash codes
183 * for all keys in this map
184 * @param[in] comparer An instance of the IComparer derived class to use when comparing keys
185 * @exception E_SUCCESS The method is successful.
186 * @exception E_INVALID_ARG A specified input parameter is invalid, or
187 * the @c capacity or the @c loadFactor is negative.
188 * @remarks The instances of hash code provider and comparer will not be deallocated later from this map.
189 * @see MultiHashMap()
191 result Construct(int capacity, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
194 * Initializes a new instance of the %MultiHashMap class by copying the elements of the specified map,
195 * with the specified load factor, hash code provider, and comparer.
199 * @return An error code
200 * @param[in] map A map to copy
201 * @param[in] loadFactor The maximum ratio of elements to buckets @n
202 * If it is @c 0, the default load factor (0.75) is used.
203 * @param[in] provider An instance of the IHashCodeProvider derived class that supplies the hash codes
204 * for all keys in this map
205 * @param[in] comparer An instance of the IComparer derived class to use when comparing keys
206 * @exception E_SUCCESS The method is successful.
207 * @exception E_INVALID_ARG A specified input parameter is invalid, or
208 * the @c loadFactor is negative.
209 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
210 * the @c map is modified during the operation of this method.
211 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
212 * The instances of hash code provider and comparer will not be deallocated later from this map.
213 * @see MultiHashMap()
215 result Construct(const IMultiMap& map, float loadFactor, const IHashCodeProvider& provider, const IComparer& comparer);
218 * Adds the specified key-value pair to this map.
222 * @return An error code
223 * @param[in] pKey The pointer to key to add
224 * @param[in] pValue The pointer to corresponding value to add
225 * @exception E_SUCCESS The method is successful.
226 * @exception E_OBJ_ALREADY_EXIST The specified pair of @c pKey and @c pValue already exists.
227 * @exception E_INVALID_ARG A specified input parameter is invalid, or
228 * the comparer has failed to compare the keys.
229 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
232 virtual result Add(Object* pKey, Object* pValue);
235 * Gets an enumerator of this map.
239 * @return An enumerator (an instance of the IEnumerator derived class) of this map, @n
240 * else @c null if some exception occurs
241 * @remarks If the key has multiple values, the enumeration proceeds as follows: {A: a}, {B: b}, {B: c}, {B, d}, {C: e}, ...
242 * The specific error code can be accessed using the GetLastResult() method.
244 * @see IMapEnumerator()
246 virtual IEnumerator* GetEnumeratorN(void) const;
249 * Gets an enumerator of this map.
253 * @return An enumerator (an instance of the IMapEnumerator derived class) of this map, @n
254 * else @c null if some exception occurs
255 * @remarks If the key has multiple values, the enumeration proceeds as follows: {A: a}, {B: b}, {B: c}, {B, d}, {C: e}, ...
256 * The specific error code can be accessed using the GetLastResult() method.
258 * @see IMapEnumerator()
260 virtual IMapEnumerator* GetMapEnumeratorN(void) const;
263 * Gets an enumerator of the values associated with the specified key.
267 * @return An enumerator (an instance of the IEnumerator derived class) of the values associated with the specified key, @n
268 * else @c null if some exception occurs
269 * @param[in] key A 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 IEnumerator* GetValuesN(const Object& key) const;
280 * Gets a list of all unique keys in this map.
284 * @return A list of all unique keys in this map
285 * @remarks The %IList stores just the pointers to the elements in the map, not the elements themselves.
286 * The specific error code can be accessed using the GetLastResult() method.
289 virtual IList* GetKeysN(void) const;
292 * Gets a list of all the values in this map.
296 * @return A list of all the values in this map
297 * @remarks The IList stores just the pointers to the elements in the map, not the elements themselves.
298 * The specific error code can be accessed using the GetLastResult() method.
301 virtual IList* GetValuesN(void) const;
304 * Removes all the values with the specified key.
308 * @return An error code
309 * @param[in] key The key to remove
310 * @exception E_SUCCESS The method is successful.
311 * @exception E_INVALID_ARG A specified input parameter is invalid, or
312 * the comparer has failed to compare keys.
313 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
316 virtual result Remove(const Object& key);
319 * Removes the specified value associated with the specified key.
323 * @return An error code
324 * @param[in] key The key whose mapping is to remove from the map
325 * @param[in] value The value to remove
326 * @exception E_SUCCESS The method is successful.
327 * @exception E_INVALID_ARG A specified input parameter is invalid, or
328 * the comparer has failed to compare the keys.
329 * @exception E_OBJ_NOT_FOUND The specified @c key and @c value pair is not found in the map.
330 * @remarks The specified key is also removed if there are no more values associated with it.
333 virtual result Remove(const Object& key, const Object& value);
336 * Removes all the object pointers in the @c collection. @n
340 * @remarks This method can be called before deleting @c collection.
342 virtual void RemoveAll(void);
345 * Sets the value associated with the given key with a new value.
349 * @return An error code
350 * @param[in] key The key for which the associated value needs to replace
351 * @param[in] value The value to replace
352 * @param[in] pNewValue The pointer to new value to replace the existing value
353 * @exception E_SUCCESS The method is successful.
354 * @exception E_INVALID_ARG A specified input parameter is invalid, or
355 * the comparer has failed to compare the keys.
356 * @exception E_OBJ_NOT_FOUND The specified @c key and @c value pair is not found in the map.
357 * @remarks To add a new key-value pair, use the Add() method.
361 virtual result SetValue(const Object& key, const Object& value, Object* pNewValue);
364 * Gets the number of values currently stored in this map.
368 * @return The number of values currently stored in this map
370 virtual int GetCount(void) const;
373 * Gets the number of values whose key matches the key.
377 * @return An error code
378 * @param[in] key A key to locate
379 * @param[out] count The number of values whose key is the @c key
380 * @exception E_SUCCESS The method is successful.
381 * @exception E_INVALID_ARG A specified input parameter is invalid, or
382 * the comparer has failed to compare the keys.
383 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
385 virtual result GetCount(const Object& key, int& count) const;
388 * Checks whether the map contains the specified key and value.
392 * @return @c true if the map contains the specified key and value pair, @n
394 * @param[in] key The key to locate
395 * @param[in] value The value to locate
396 * @exception E_SUCCESS The method is successful.
397 * @exception E_INVALID_ARG A specified input parameter is invalid, or
398 * the comparer has failed to compare the keys.
399 * @remarks The specific error code can be accessed using the GetLastResult() method.
401 * @see ContainsValue()
403 virtual bool Contains(const Object& key, const Object& value) const;
406 * Checks whether the map contains the specified key.
410 * @return @c true if the map contains the specified key, @n
412 * @param[in] key The key to locate
413 * @exception E_SUCCESS The method is successful.
414 * @exception E_INVALID_ARG A specified input parameter is invalid, or
415 * the comparer has failed to compare the keys.
416 * @remarks The specific error code can be accessed using the GetLastResult() method.
417 * @see ContainsValue()
420 virtual bool ContainsKey(const Object& key) const;
423 * Checks whether the map contains the specified value.
427 * @return @c true if the map contains the specified value, @n
429 * @param[in] value The value to locate
434 virtual bool ContainsValue(const Object& value) const;
437 * Compares the specified instance to the current instance for equality.
441 * @return @c true if the two instances are equal, @n
443 * @param[in] obj The object to compare with the current instance
444 * @remarks This method returns @c true only if the specified object is also an instance of %MultiHashMap class,
445 * both maps have the same number of elements, and both maps contain the same elements.
447 virtual bool Equals(const Object& obj) const;
450 * Gets the hash value of the current instance.
454 * @return The hash value of the current instance
455 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
456 * the used hash function must generate a random distribution for all inputs.
458 virtual int GetHashCode(void) const;
461 * Gets the element deleter of the collection.
465 * @return A function pointer to the existing element deleter
467 virtual DeleterFunctionType GetDeleter(void) const;
471 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
473 * @param[in] map An instance of %MultiHashMap to initialize the current instance
475 MultiHashMap(const MultiHashMap& map);
478 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
480 * @param[in] map An instance of %MultiHashMap
482 MultiHashMap& operator =(const MultiHashMap& map);
485 * Copies all the pairs from the specified map to this map.
487 * @return An error code
488 * @param[in] map The map to copy
489 * @exception E_SUCCESS The method is successful.
490 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation. @n
491 * The @c map is modified during the operation of this method.
493 result AddAll(const IMultiMap& map);
496 * Gets a hash value for the specified object.
498 * @return An @c int hash value for the specified object
499 * @param[in] obj The object to get hash value
501 int Hash(const Object& obj) const;
504 * Rehashes the contents of this map into a new array with a
505 * larger capacity. @n
506 * This method is called automatically when the number of keys in this map reaches its threshold.
508 * @return An error code
509 * @param[in] newCapacity The new capacity @n
510 * It must be a power of two and be greater than current capacity.
511 * @exception E_SUCCESS The method is successful.
513 result Resize(int newCapacity);
516 * Clears all key-value pairs in this map.
521 * Sets the element deleter of the collection.
525 * @param[in] deleter A function pointer to the element deleter to set
527 virtual void SetDeleter(DeleterFunctionType deleter);
529 _MultiHashMapEntry** __pTable;
534 IHashCodeProvider* __pProvider;
535 IComparer* __pComparer;
536 bool __needToRemoveProviderComparer;
538 DeleterFunctionType __deleter;
539 static const int DEFAULT_CAPACITY = 16;
540 static const float DEFAULT_LOAD_FACTOR;
542 friend class _MultiHashMapEnumerator;
543 friend class _MultiHashMapImpl;
544 class _MultiHashMapImpl* __pMultiHashMapImpl;
548 }}} // Tizen::Base::Collection
550 #endif //_FBASE_COL_MULTI_HASH_MAP_H_